-
Notifications
You must be signed in to change notification settings - Fork 603
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
Create cat regressor #3353
base: main
Are you sure you want to change the base?
Create cat regressor #3353
Changes from 17 commits
086f70d
37244a9
b4ecb0a
36858d9
be1bccc
a1a59ae
7b41bc8
119a142
d77fa9c
236e356
bb9cde4
bbb5035
2a92193
c7b78c0
b001c0e
c3ce03e
c50226a
c6665f4
858e247
2421bd5
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 | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -628,6 +628,20 @@ | |||||||||||||||||||||
DT = TypeVar("DT") | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
@njit | ||||||||||||||||||||||
def _create_regressor_categorical( | ||||||||||||||||||||||
X: np.ndarray, number_categories: int, filters: np.ndarray | ||||||||||||||||||||||
) -> np.ndarray: | ||||||||||||||||||||||
# create regressor matrix faster for categorical variables | ||||||||||||||||||||||
regressors = np.zeros(X.shape, dtype=X.dtype) | ||||||||||||||||||||||
XT = X.T | ||||||||||||||||||||||
for category in range(number_categories): | ||||||||||||||||||||||
mask = category == filters | ||||||||||||||||||||||
for ix in numba.prange(XT.shape[0]): | ||||||||||||||||||||||
regressors[mask, ix] = XT[ix][mask].mean() | ||||||||||||||||||||||
flying-sheep marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
return regressors | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
@njit | ||||||||||||||||||||||
def get_resid( | ||||||||||||||||||||||
data: np.ndarray, | ||||||||||||||||||||||
|
@@ -722,13 +736,13 @@ | |||||||||||||||||||||
"we regress on the mean for each category." | ||||||||||||||||||||||
) | ||||||||||||||||||||||
logg.debug("... regressing on per-gene means within categories") | ||||||||||||||||||||||
regressors = np.zeros(X.shape, dtype="float32") | ||||||||||||||||||||||
# Create numpy array's from categorical variable | ||||||||||||||||||||||
number_categories = np.int64(len(adata.obs[keys[0]].cat.categories)) | ||||||||||||||||||||||
filters = adata.obs[keys[0]].cat.codes.to_numpy() | ||||||||||||||||||||||
number_categories = number_categories.astype(filters.dtype) | ||||||||||||||||||||||
Comment on lines
+740
to
+742
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. Either this or add a comment (to the code) explaining why it needs to be the other way.
Suggested change
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. I added a comment. Other wise you have a dtype missmatch and crash of the kernel 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.
I would say that this is the important part for the comment! 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. 100%!
I see that you’re
So you don’t need to comment that you do any of that. I asked because I’m confused why a Python integer is converted to a numpy scalar: Usually APIs accept either and do the converting themselves. So I’d like to see a comment removing that confusion by explaining why you convert to a numpy scalar. (a crash is a great reason) but I also see that I can’t reproduce the crash. leaving the thing as a Python int just works for me. 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. Also the way to do this in one step is
Suggested change
|
||||||||||||||||||||||
|
||||||||||||||||||||||
X = _to_dense(X, order="F") if issparse(X) else X | ||||||||||||||||||||||
# 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, filters) | ||||||||||||||||||||||
variable_is_categorical = True | ||||||||||||||||||||||
# regress on one or several ordinal variables | ||||||||||||||||||||||
else: | ||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does this comment mean?