Skip to content

Commit

Permalink
Scale clips low (#2913)
Browse files Browse the repository at this point in the history
Co-authored-by: Philipp A <flying-sheep@web.de>
  • Loading branch information
Intron7 and flying-sheep committed Mar 14, 2024
1 parent 1fee6a1 commit c6a7ae1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/release-notes/1.10.0.md
Expand Up @@ -25,6 +25,7 @@
* {func}`scanpy.external.pp.harmony_integrate` now runs with 64 bit floats improving reproducibility {pr}`2655` {smaller}`S Dicks`
* {func}`scanpy.tl.rank_genes_groups` no longer warns that it's default was changed from t-test_overestim_var to t-test {pr}`2798` {smaller}`L Heumos`
* `scanpy.pp.calculate_qc_metrics` now allows `qc_vars` to be passed as a string {pr}`2859` {smaller}`N Teyssier`
* {func}`scanpy.pp.scale` now clips `np.ndarray` also at `- max_value` for zero-centering {pr}`2913` {smaller}`S Dicks`

```{rubric} Docs
```
Expand Down
5 changes: 4 additions & 1 deletion scanpy/preprocessing/_simple.py
Expand Up @@ -871,7 +871,10 @@ def scale_array(
# do the clipping
if max_value is not None:
logg.debug(f"... clipping at max_value {max_value}")
X[X > max_value] = max_value
if zero_center:
X = np.clip(X, a_min=-max_value, a_max=max_value)
else:
X[X > max_value] = max_value
if return_mean_std:
return X, mean, std
else:
Expand Down
9 changes: 9 additions & 0 deletions scanpy/tests/test_scaling.py
Expand Up @@ -104,3 +104,12 @@ def test_mask_string():
sc.pp.scale(adata, mask_obs="some cells")
assert np.array_equal(adata.X, X_centered_for_mask)
assert "mean of some cells" in adata.var.keys()


@pytest.mark.parametrize("zero_center", [True, False])
def test_clip(zero_center):
adata = sc.datasets.pbmc3k()
sc.pp.scale(adata, max_value=1, zero_center=zero_center)
if zero_center:
assert adata.X.min() >= -1
assert adata.X.max() <= 1

0 comments on commit c6a7ae1

Please sign in to comment.