-
Notifications
You must be signed in to change notification settings - Fork 604
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
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3353 +/- ##
==========================================
- Coverage 76.58% 76.46% -0.12%
==========================================
Files 111 111
Lines 12862 12874 +12
==========================================
- Hits 9850 9844 -6
- Misses 3012 3030 +18
|
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?
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) |
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.
Either this or add a comment (to the code) explaining why it needs to be the other way.
Also if I do this, the test still passes, so …
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) | |
number_categories = len(adata.obs[keys[0]].cat.categories) | |
filters = adata.obs[keys[0]].cat.codes.to_numpy() |
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 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Other wise you have a dtype missmatch and crash of the kernel
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 comment
The reason will be displayed to describe this comment to others. Learn more.
100%!
- refactor your code until the “what” is obvious.
- if the “why” isn’t obvious from understanding the “what”, add the missing parts as a comment
I see that you’re
- convert the cat codes into a numpy array
- creating a numpy scalar with the same dtype as
filters
, holding the number of categories
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 _create_regressor_categorical
has number_categories: int
and then does range(number_categories)
, so I’m still very confused why numba crashes unless the dtypes match.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Also the way to do this in one step is
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) | |
filters = adata.obs[keys[0]].cat.codes.to_numpy() | |
number_categories = filters.dtype.type(len(adata.obs[keys[0]].cat.categories)) |
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) |
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.
Other wise you have a dtype missmatch and crash of the kernel
I would say that this is the important part for the comment!
def _create_regressor_categorical( | ||
X: np.ndarray, number_categories: int, filters: np.ndarray | ||
) -> np.ndarray: | ||
# create regressor matrix faster for categorical variables |
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?
Benchmark changes
Comparison: https://github.com/scverse/scanpy/compare/6dd0a7a72c7f8f57a082cca0f6a369dc47937b04..2421bd55496036151b73c46c5ec7ffa7e5ef71eb More details: https://github.com/scverse/scanpy/pull/3353/checks?check_run_id=33316268173 |
Use numba to create the regressor for categorical regression