Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scale clips low #2913

Merged
merged 6 commits into from Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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