Skip to content

Commit

Permalink
Bugfix log1p for non-float dense arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
ivirshup committed Sep 2, 2020
1 parent 43379e0 commit f704f72
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
7 changes: 7 additions & 0 deletions docs/release-latest.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
.. role:: small
.. role:: smaller

On master
~~~~~~~~~

.. rubric:: Bugfixes

* Fixed `log1p` inplace on integer dense arrays :pr:`1400` :smaller:`I Virshup`

1.6.0 :small:`2020-08-15`
~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
6 changes: 5 additions & 1 deletion scanpy/preprocessing/_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,13 +308,17 @@ def log1p_sparse(X, *, base: Optional[Number] = None, copy: bool = False):

@log1p.register(np.ndarray)
def log1p_array(X, *, base: Optional[Number] = None, copy: bool = False):
# Can force arrays to be np.ndarrays, but would be useful
# Can force arrays to be np.ndarrays, but would be useful to not
# X = check_array(X, dtype=(np.float64, np.float32), ensure_2d=False, copy=copy)
if copy:
if not np.issubdtype(X.dtype, np.floating):
X = X.astype(np.floating)
else:
X = X.copy()
elif not (
np.issubdtype(X.dtype, np.floating) or np.issubdtype(X.dtype, np.complex)
):
X = X.astype(np.floating)
np.log1p(X, out=X)
if base is not None:
np.divide(X, np.log(base), out=X)
Expand Down
2 changes: 1 addition & 1 deletion scanpy/tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def check_rep_mutation(func, X, **kwargs):
X=X.copy(),
layers={"layer": X.copy()},
obsm={"obsm": X.copy()},
dtype=np.float64,
dtype=X.dtype,
)
adata_X = func(adata, copy=True, **kwargs)
adata_layer = func(adata, layer="layer", copy=True, **kwargs)
Expand Down
6 changes: 4 additions & 2 deletions scanpy/tests/test_preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ def base(request):
return request.param


def test_log1p_rep(count_matrix_format, base):
X = count_matrix_format(sp.random(100, 200, density=0.3).toarray())
def test_log1p_rep(count_matrix_format, base, dtype):
X = count_matrix_format(
np.abs(sp.random(100, 200, density=0.3, dtype=dtype)).toarray()
)
check_rep_mutation(sc.pp.log1p, X, base=base)
check_rep_results(sc.pp.log1p, X, base=base)

Expand Down

0 comments on commit f704f72

Please sign in to comment.