-
Notifications
You must be signed in to change notification settings - Fork 638
Speed up categorical regressor with numba #3353
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
Changes from 25 commits
086f70d
37244a9
b4ecb0a
36858d9
be1bccc
a1a59ae
7b41bc8
119a142
d77fa9c
236e356
bb9cde4
bbb5035
2a92193
c7b78c0
b001c0e
c3ce03e
c50226a
c6665f4
858e247
2421bd5
2e16c45
1b7d7e1
3b7fe6e
726a625
104a0f3
f9b13be
6eafd04
1dae8f4
39ad1c0
4f3db86
2d578c8
eedb314
6a19bc6
0053bce
75be7e2
2fdd0bf
2b3f1f1
0ab9fee
832981b
d0eb0cf
512b04e
8e6f212
9f6a57b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* Speed up for a categorical regressor in {func}`~scanpy.pp.regress_out` {smaller}`S Dicks` | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -637,6 +637,22 @@ | |
DT = TypeVar("DT") | ||
|
||
|
||
@njit | ||
def _create_regressor_categorical( | ||
X: np.ndarray, number_categories: int, cat_array: np.ndarray | ||
) -> np.ndarray: | ||
# create regressor matrix for categorical variables | ||
regressors = np.zeros(X.shape, dtype=X.dtype) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. check dtype for behavior with integer dtype i.e., need to ensure this is a floating point matrix There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes! discoverered a bug! |
||
# iterate over categories | ||
for category in range(number_categories): | ||
# iterate over genes and calculate mean expression | ||
# for each gene per category | ||
mask = category == cat_array | ||
for ix in numba.prange(X.T.shape[0]): | ||
regressors[mask, ix] = X.T[ix, mask].mean() | ||
return regressors | ||
|
||
|
||
@njit | ||
def get_resid( | ||
data: np.ndarray, | ||
|
@@ -732,13 +748,13 @@ | |
) | ||
raise ValueError(msg) | ||
logg.debug("... regressing on per-gene means within categories") | ||
regressors = np.zeros(X.shape, dtype="float32") | ||
|
||
# set number of categories to the same dtype as the categories | ||
cat_array = adata.obs[keys[0]].cat.codes.to_numpy() | ||
number_categories = cat_array.dtype.type(len(adata.obs[keys[0]].cat.categories)) | ||
|
||
X = _to_dense(X, order="F") if issparse(X) else X | ||
ilan-gold marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# TODO figure out if we should use a numba kernel for this | ||
for category in adata.obs[keys[0]].cat.categories: | ||
mask = (category == adata.obs[keys[0]]).values | ||
for ix, x in enumerate(X.T): | ||
regressors[mask, ix] = x[mask].mean() | ||
regressors = _create_regressor_categorical(X, number_categories, cat_array) | ||
variable_is_categorical = True | ||
# regress on one or several ordinal variables | ||
else: | ||
|
Uh oh!
There was an error while loading. Please reload this page.