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 22 commits into
base: main
Choose a base branch
from
Open

Create cat regressor #3353

wants to merge 22 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 50.00000% with 6 lines in your changes missing coverage. Please review.

Project coverage is 75.41%. Comparing base (8ce811a) to head (1b7d7e1).

Files with missing lines Patch % Lines
src/scanpy/preprocessing/_simple.py 50.00% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3353      +/-   ##
==========================================
- Coverage   75.44%   75.41%   -0.04%     
==========================================
  Files         113      113              
  Lines       13250    13257       +7     
==========================================
+ Hits         9997     9998       +1     
- Misses       3253     3259       +6     
Files with missing lines Coverage Δ
src/scanpy/preprocessing/_simple.py 88.83% <50.00%> (-1.32%) ⬇️

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
@Intron7 Intron7 requested a review from ilan-gold November 11, 2024 15:36
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 Intron7 requested a review from ilan-gold November 12, 2024 15:18
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?

@Intron7 Intron7 requested a review from ilan-gold November 13, 2024 10:55
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
@Intron7 Intron7 requested a review from flying-sheep November 21, 2024 10:45
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

scverse-benchmark bot commented Nov 21, 2024

Benchmark changes

Change Before [8ce811a] After [1b7d7e1] Ratio Benchmark (Parameter)
- 59.9±0.8ms 43.6±0.5ms 0.73 preprocessing_counts.time_filter_genes('pbmc3k', 'counts-off-axis')
- 3.23±0.1ms 2.67±0.06ms 0.83 preprocessing_log.FastSuite.time_mean_var('pbmc3k', None)
+ 8.87±0.03ms 9.78±0.7ms 1.1 preprocessing_log.time_highly_variable_genes('pbmc68k_reduced', 'off-axis')

Comparison: https://github.com/scverse/scanpy/compare/8ce811ac3ab6674a7d6235f44afda3e10e366682..1b7d7e177b9f1c8bc67eb6dad8d4617b1d16e443
Last changed: Thu, 23 Jan 2025 15:39:10 +0000

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

@flying-sheep flying-sheep removed their request for review November 21, 2024 11:39
@flying-sheep flying-sheep modified the milestones: 1.11.0, 1.12.0 Dec 20, 2024
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