Skip to content
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

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open

Create cat regressor #3353

wants to merge 20 commits into from

Conversation

Intron7
Copy link
Member

@Intron7 Intron7 commented Nov 11, 2024

Use numba to create the regressor for categorical regression

@Intron7 Intron7 added this to the 1.11.0 milestone Nov 11, 2024
Copy link

codecov bot commented Nov 11, 2024

Codecov Report

Attention: Patch coverage is 53.84615% with 6 lines in your changes missing coverage. Please review.

Project coverage is 76.46%. Comparing base (6dd0a7a) to head (2421bd5).
Report is 7 commits behind head on main.

Files with missing lines Patch % Lines
src/scanpy/preprocessing/_simple.py 53.84% 6 Missing ⚠️
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     
Files with missing lines Coverage Δ
src/scanpy/preprocessing/_simple.py 88.46% <53.84%> (-1.46%) ⬇️

... and 7 files with indirect coverage changes

---- 🚨 Try these New Features:

tests/test_preprocessing.py Outdated Show resolved Hide resolved
src/scanpy/preprocessing/_simple.py Outdated Show resolved Hide resolved
np.testing.assert_array_almost_equal(adata.X, tester)


def test_regressor_categorical():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would

  1. 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)
  2. refactor the "Create org regressors" into a helper function like create_original

Copy link
Member Author

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

Copy link
Contributor

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?

tests/test_preprocessing.py Outdated Show resolved Hide resolved
Copy link
Contributor

@ilan-gold ilan-gold left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests/test_preprocessing.py Outdated Show resolved Hide resolved
tests/test_preprocessing.py Outdated Show resolved Hide resolved
tests/test_preprocessing.py Outdated Show resolved Hide resolved
src/scanpy/preprocessing/_simple.py Outdated Show resolved Hide resolved
@@ -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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

Copy link
Contributor

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

Copy link
Member Author

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

Copy link
Member

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.

Intron7 and others added 4 commits November 12, 2024 14:44
Co-authored-by: Ilan Gold <ilanbassgold@gmail.com>
Co-authored-by: Ilan Gold <ilanbassgold@gmail.com>
Co-authored-by: Ilan Gold <ilanbassgold@gmail.com>
tests/test_preprocessing.py Outdated Show resolved Hide resolved
src/scanpy/preprocessing/_simple.py Outdated Show resolved Hide resolved
np.testing.assert_array_almost_equal(adata.X, tester)


def test_regressor_categorical():
Copy link
Contributor

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?

src/scanpy/preprocessing/_simple.py Outdated Show resolved Hide resolved
Comment on lines +740 to +742
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)
Copy link
Member

@flying-sheep flying-sheep Nov 14, 2024

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 …

Suggested change
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()

Copy link
Member Author

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

Copy link
Contributor

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!

Copy link
Member

@flying-sheep flying-sheep Nov 21, 2024

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

  1. convert the cat codes into a numpy array
  2. 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.

Copy link
Member

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

Suggested change
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))

tests/test_preprocessing.py Outdated Show resolved Hide resolved
Comment on lines +740 to +742
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)
Copy link
Contributor

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
Copy link
Contributor

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?

Copy link

Benchmark changes

Change Before [6dd0a7a] After [2421bd5] Ratio Benchmark (Parameter)
+ 366M 405M 1.11 preprocessing_counts.peakmem_scrublet('pbmc68k_reduced', 'counts')
- 1.39±0.03ms 1.24±0.02ms 0.9 preprocessing_log.FastSuite.time_mean_var('pbmc3k', 'off-axis')
- 584M 480M 0.82 preprocessing_log.peakmem_pca('pbmc3k', 'off-axis')

Comparison: https://github.com/scverse/scanpy/compare/6dd0a7a72c7f8f57a082cca0f6a369dc47937b04..2421bd55496036151b73c46c5ec7ffa7e5ef71eb
Last changed: Thu, 21 Nov 2024 11:39:20 +0000

More details: https://github.com/scverse/scanpy/pull/3353/checks?check_run_id=33316268173

@flying-sheep flying-sheep removed their request for review November 21, 2024 11:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants