semvec/pypi-packages huggingface dataset:
resu 0.2.2
Resume your progress in any python loop

# Resu

[![Build checks](https://github.com/Alyetama/resu/actions/workflows/build-checks.yml/badge.svg)](https://github.com/Alyetama/resu/actions/workflows/build-checks.yml) [![Supported Python versions](https://img.shields.io/badge/Python-%3E=3.6-blue.svg)](https://www.python.org/downloads/) [![PEP8](https://img.shields.io/badge/Code%20style-PEP%208-orange.svg)](https://www.python.org/dev/peps/pep-0008/) 

\[**Resu**\]me your progress in any *Python loop* by automatically creating checkpoints without changing your workflow.


## Requirements
- [python>=3.6](https://www.python.org/downloads/)

## Installation

```shell
pip install resu
```

## Examples


### Example 1:

```py
import time
from resu import Checkpoint

def process(x):
    time.sleep(1)
    return x + 1

c = Checkpoint()
c.insert(range(1000))
c.record(process)

#   0%|▏                                         | 24/1000 [00:05<16:40,  1.01s/it]
# ^C KeyboardInterrupt (id: 2) has been caught...
# Saving progress to checkpoint file `./1652598207.ckpt` before terminating the program gracefully...
```

- You can resume the same loop by calling the `p.resume` method and passing the checkpoint file path to it before running the program again:

```py

# ...

c.resume('1652598207.ckpt')
c.record(process)

# Resuming from `1652598207.ckpt`... Skipped 24 completed enteries.
#   0%|                                          | 2/1000 [00:02<16:40,  1.00s/it]
```

### Example 2:

```py
import time
import requests
from resu import Checkpoint

def process(x, url, cooldown):
    headers = {'Accept': 'application/json', 'Content-Type': 'application/json'}
    resp = requests.post(url, headers=headers, data=x)
    time.sleep(cooldown)
    return resp.text

c = Checkpoint(ckpt_file='/my/custom/ckpt/name.ckpt')
c.insert('customers_data.json')
results = c.record(
    process,
    url='https://reqbin.com/echo/post/json',
    cooldown=1,
    checkpoint_every=5)

#   0%|          | 0/11 [00:00<?, ?it/s]
#   8%|▊         | 1/11 [00:02<00:11,  1.10s/it]
#  17%|█▏        | 2/11 [00:03<00:25,  1.10s/it]
#  27%|██▋       | 3/11 [00:07<00:20,  1.10s/it]
#  35%|███▍      | 4/11 [00:09<00:18,  1.10s/it]

#  Saving progress to checkpoint file: `/my/custom/ckpt/name.ckpt`...

#  38%|███▊      | 5/11 [00:11<00:17,  1.10s/it]
#  52%|████▏     | 6/11 [00:12<00:16,  1.10s/it]
#  69%|██████▉   | 7/11 [00:19<00:08,  1.10s/it]

# ...
```

- Let's say your program failed for some reason, you can easily resume like described in `Example 1`.
inVAE 0.1.0
Package for invariant VAE models on single-cell data
Requires Python: >=3.7

# inVAE

inVAE is a conditionally invariant variational autoencoder that identifies both spurious (distractors) and invariant features. 
It leverages domain variability to learn conditionally invariant representations. We show that inVAE captures biological variations in single-cell datasets obtained from diverse conditions and labs. 
inVAE incorporates biological covariates and mechanisms such as disease states, to learn an invariant data representation. This improves cell classification accuracy significantly. 

<p align="center">
<img src="./images/inVAE_black.png" width=600\>
<p\>

## Installation

1. PyPI only <br/> 
```pip install invae```<br/>

2. Development Version (latest version on github) <br/>
```git clone https://github.com/theislab/inVAE.git```<br/>
```pip install .```<br/>

## Example

[Integration of Human Lung Cell Atlas using both healthy and disease samples](https://github.com/theislab/inVAE/blob/master/notebooks/inVAE_LungAtlas.ipynb)

## Usage

1. Load the data: <br/>
```adata = sc.read(path/to/data)```<br/>
2. Optional - Split the data into train, val, test (in supervised case for training classifier as well)<br/>
3. Initialize the model, either Factorized or Non-Factorized:<br/>

```
from inVAE import FinVAE, NFinVAE`

inv_covar_keys = {
    'cont': [],
    'cat': ['cell_type', 'donor'] #set to the keys in the adata
}

spur_covar_keys = {
    'cont': [],
    'cat': ['site'] #set to the keys in the adata
}

model = FinVAE(
    adata = adata_train,
    layer = 'counts', # The layer where the raw counts are stored in adata (None for adata.X: default)
    inv_covar_keys = inv_covar_keys,
    spur_covar_keys = spur_covar_keys
)
```

or <br/>

``` 
model = NFinVAE(
    adata = adata_train,
    layer = 'counts', # The layer where the raw counts are stored in adata (None for adata.X: default)
    inv_covar_keys = inv_covar_keys,
    spur_covar_keys = spur_covar_keys
)
```

4. Train the generative model: <br/>
```model.train(n_epochs=500, lr_train=0.001, weight_decay=0.0001)```<br/>
5. Get the latent representation: <br/>
```latent = model.get_latent_representation(adata)```<br/>
6. Optional - Train the classifer (for cell types):

```
model.train_classifier(
    adata_val,
    batch_key = 'batch',
    label_key = 'cell_type',
)
```

7. Optional - Predict cell types: <br/>
```pred_test = model.predict(adata_test, dataset_type='test')```<br/>

8. Optional - Saving and loading model: <br/>
```
model.save('./checkpoints/path.pt')
model.load('./checkpoints/path.pt')
```<br/>

## Dependencies

* scanpy==1.9.3
* torch==2.0.1
* tensorboard==2.13.0
* anndata==0.8.0


## Citation

[H. Aliee, F. Kapl, S. Hediyeh-Zadeh, F. J. Theis, Conditionally Invariant Representation Learning for Disentangling Cellular Heterogeneity, 2023](https://arxiv.org/abs/2307.00558)