Skip to content

Commit acfbd11

Browse files
committed
Refactored scanpy.tools._sparse_nanmean to eliminate unnecessary data copying
1 parent bdcef41 commit acfbd11

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

src/scanpy/tools/_score_genes.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,19 @@ def _sparse_nanmean(X: CSBase, axis: Literal[0, 1]) -> NDArray[np.float64]:
3434
msg = "X must be a compressed sparse matrix"
3535
raise TypeError(msg)
3636

37-
# count the number of nan elements per row/column (dep. on axis)
3837
Z = X.copy()
39-
Z.data = np.isnan(Z.data)
40-
Z.eliminate_zeros()
41-
n_elements = Z.shape[axis] - Z.sum(axis)
4238

43-
# set the nans to 0, so that a normal .sum() works
44-
Y = X.copy()
45-
Y.data[np.isnan(Y.data)] = 0
46-
Y.eliminate_zeros()
39+
# count the number of nonzero elements (include nans) per row/column (dep. on axis)
40+
nonzeros_and_nones=Z.count_nonzero(axis=axis)
41+
42+
# just sum the data withput nan
43+
Z.data[np.isnan(Z.data)] = 0
44+
Z.eliminate_zeros()
45+
s = Z.sum(axis, dtype="float64")
4746

48-
# the average
49-
s = Y.sum(axis, dtype="float64") # float64 for score_genes function compatibility)
47+
# Z.count_nonzero(axis=axis) is now non-zero not-nan elements in X
48+
#diff between nonzeros_and_nones and curr nonzero is nans
49+
n_elements=(Z.shape[axis] - (nonzeros_and_nones - Z.count_nonzero(axis=axis))).reshape(s.shape, copy=False)
5050
m = s / n_elements
5151

5252
return m

0 commit comments

Comments
 (0)