Skip to content

Commit

Permalink
Fix using custom layer with highly_variable_genes (scverse#2302)
Browse files Browse the repository at this point in the history
* Fix using custom layer with highly_variable_genes

* Add tests

* Add release note

* Move release note to correct section
  • Loading branch information
DriesSchaumont authored Aug 11, 2022
1 parent ee39688 commit 4623bb7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/release-notes/1.9.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

```{rubric} Bug fixes
```
- {func}`~scanpy.pp.highly_variable_genes` `layer` argument now works in tandem with `batches` {pr}`2302` {smaller}`D Schaumont`


```{rubric} Performance
```
1 change: 1 addition & 0 deletions scanpy/preprocessing/_highly_variable_genes.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ def highly_variable_genes(

hvg = _highly_variable_genes_single_batch(
adata_subset,
layer=layer,
min_disp=min_disp,
max_disp=max_disp,
min_mean=min_mean,
Expand Down
17 changes: 16 additions & 1 deletion scanpy/tests/test_highly_variable_genes.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,26 @@ def test_highly_variable_genes_basic():
assert 'highly_variable_intersection' in adata.var.columns

adata = sc.datasets.blobs()
adata.obs['batch'] = np.random.binomial(4, 0.5, size=(adata.n_obs))
batch = np.random.binomial(4, 0.5, size=(adata.n_obs))
adata.obs['batch'] = batch
adata.obs['batch'] = adata.obs['batch'].astype('category')
sc.pp.highly_variable_genes(adata, batch_key='batch', n_top_genes=3)
assert 'highly_variable_nbatches' in adata.var.columns
assert adata.var['highly_variable'].sum() == 3
highly_var_first_layer = adata.var['highly_variable']

adata = sc.datasets.blobs()
new_layer = adata.X.copy()
np.random.shuffle(new_layer)
adata.layers['test_layer'] = new_layer
adata.obs['batch'] = batch
adata.obs['batch'] = adata.obs['batch'].astype('category')
sc.pp.highly_variable_genes(
adata, batch_key='batch', n_top_genes=3, layer='test_layer'
)
assert 'highly_variable_nbatches' in adata.var.columns
assert adata.var['highly_variable'].sum() == 3
assert (highly_var_first_layer != adata.var['highly_variable']).any()

sc.pp.highly_variable_genes(adata)
no_batch_hvg = adata.var.highly_variable.copy()
Expand Down

0 comments on commit 4623bb7

Please sign in to comment.