# Batch integration and evaluation

Integration should remove unwanted technical variation while preserving
biological structure. Always retain an unintegrated representation for
comparison.

## Before integration

- Define whether the key represents a technical batch or biological condition.
- Do not integrate away the main biological variable of interest.
- Select HVGs with awareness of batch structure.
- Check that every batch contains enough cells and relevant cell populations.

## Harmony

Start from PCA:

```python
sctk.pp.harmony_integrate(
    adata,
    key="batch",
    basis="X_pca",
    adjusted_basis="X_pca_harmony",
    verbose=False,
)
```

Harmony is imported only when called. The corrected representation is stored in
`adata.obsm["X_pca_harmony"]`; counts and normalized expression are unchanged.

Recompute the neighbor graph and UMAP from the corrected representation:

```python
sc.pp.neighbors(adata, use_rep="X_pca_harmony")
sc.tl.umap(adata, random_state=0)
```

## scVI

scVI requires raw counts:

```bash
pip install -e ".[integration]"
```

```python
model = sctk.pp.scvi_integrate(
    adata,
    layer="counts",
    batch_key="batch",
    key_added="X_scvi",
    n_latent=30,
    return_model=True,
)

sc.pp.neighbors(adata, use_rep="X_scvi")
sc.tl.umap(adata, random_state=0)
```

Known technical covariates can be supplied explicitly:

```python
sctk.pp.scvi_integrate(
    adata,
    layer="counts",
    batch_key="batch",
    categorical_covariate_keys=["chemistry"],
    continuous_covariate_keys=["percent_mito"],
)
```

Only include covariates whose removal matches the scientific question.

## Quantitative evaluation

After constructing neighbors from the integrated representation:

```python
metrics = sctk.tl.integration_metrics(
    adata,
    batch_key="batch",
    label_key="cell_type",
    basis="X_pca_harmony",
)
metrics
```

All returned metrics are oriented so larger is better:

| Metric | Interpretation |
| --- | --- |
| `batch_asw_mixing` | batches are less separable in the embedding |
| `neighbor_batch_entropy` | local neighborhoods contain diverse batches |
| `label_asw` | known biological labels remain compact |
| `graph_connectivity` | cells of each biological label remain connected |

These lightweight metrics are comparative diagnostics, not universal pass/fail
scores.

## Required visual checks

For integrated and unintegrated embeddings, compare:

- color by batch, sample, condition, and cell type;
- major lineage and state markers;
- cell-type abundance by sample;
- rare populations;
- batch-specific missing populations;
- cluster stability and differential markers.

## Avoiding overcorrection

Warning signs include:

- known condition-specific states disappearing;
- unrelated lineages merging;
- marker gradients becoming flat;
- rare populations being absorbed into abundant populations;
- strong mixing caused by removing a variable confounded with biology.

Report the integration method, version, covariates, representation key,
neighbor parameters, random seed, and evaluation metrics.
