-
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
base: main
Are you sure you want to change the base?
Conversation
❌ 7 Tests Failed:
View the top 3 failed test(s) by shortest run time
To view more test analytics, go to the Test Analytics Dashboard |
tests/test_preprocessing.py
Outdated
np.testing.assert_array_almost_equal(adata.X, tester) | ||
|
||
|
||
def test_regressor_categorical(): |
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.
I would
- explain why this test exists (to test against a previous implementation? I am impartial whether it's necessary TBH since we are already testing for reproducibility, could see getting rid of this)
- refactor the "Create org regressors" into a helper function like
create_original
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.
I can see your point here
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.
Do you have an an opinion on the first point? Is this test necessary? If so, perhaps a comment then?
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.
I think this is missing: #3353 (comment) and the first part of https://github.com/scverse/scanpy/pull/3353/files#r1836830351
src/scanpy/preprocessing/_simple.py
Outdated
@@ -722,13 +737,13 @@ def regress_out( | |||
"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 | |||
cats = np.int64(len(adata.obs[keys[0]].cat.categories)) |
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.
Ditto
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.
Also comment why np.int64
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.
because it has be done because of weird typing from pandas. So this ensures that it works within the kernel
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.
so len
doesn’t return a Python int
? That’s a pandas bug.
Co-authored-by: Ilan Gold <[email protected]>
Co-authored-by: Ilan Gold <[email protected]>
Co-authored-by: Ilan Gold <[email protected]>
tests/test_preprocessing.py
Outdated
np.testing.assert_array_almost_equal(adata.X, tester) | ||
|
||
|
||
def test_regressor_categorical(): |
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.
Do you have an an opinion on the first point? Is this test necessary? If so, perhaps a comment then?
I renamed one variable to make is clearer what it is. Added some comments that should add more context what the code is doing. |
Co-authored-by: Ilan Gold <[email protected]>
src/scanpy/preprocessing/_simple.py
Outdated
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Yes! discoverered a bug! regressors
needs to be float32
to match old behavior, not sure why it was hardcoded
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.
Why no test for the dtype if we're also fixing that bug here? or in #3461?
Co-authored-by: Philipp A. <[email protected]>
Co-authored-by: Ilan Gold <[email protected]>
… create_cat_regressor
Use numba to create the regressor for categorical regression