Skip to content

Commit

Permalink
uses scikit learn to calc median for sparse matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
farhadmd7 committed Sep 2, 2024
1 parent 4d40e71 commit 1faf70f
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions src/scanpy/get/_aggregated.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pandas as pd
from anndata import AnnData, utils
from scipy import sparse
from sklearn.utils.sparsefuncs import csc_median_axis_0

from .._utils import _resolve_axis
from .get import _check_mask
Expand Down Expand Up @@ -146,23 +147,19 @@ def median(self) -> Array:
-------
Array of median.
"""
import warnings


medians = []
for group in np.unique(self.groupby.codes):
group_mask = self.groupby.codes == group
group_data = self.data[group_mask]
if sparse.issparse(group_data):
warnings.warn(
"Converting sparse matrix to dense for median calculation. "
"This may be very memory intensive for large datasets.",
UserWarning,
)
group_data = group_data.toarray()
medians.append(np.median(group_data, axis=0))
if group_data.format != 'csc':
group_data = group_data.tocsc()
medians.append(csc_median_axis_0(group_data))
else:
medians.append(np.median(group_data, axis=0))
return np.array(medians)



def _power(X: Array, power: float | int) -> Array:
"""\
Generate elementwise power of a matrix.
Expand Down

0 comments on commit 1faf70f

Please sign in to comment.