Skip to content

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

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

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 42.85714% with 8 lines in your changes missing coverage. Please review.

Project coverage is 75.46%. Comparing base (bdcef41) to head (eedb314).

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
src/scanpy/preprocessing/_simple.py 42.85% 8 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3353      +/-   ##
==========================================
+ Coverage   73.09%   75.46%   +2.36%     
==========================================
  Files         113      113              
  Lines       13139    13148       +9     
==========================================
+ Hits         9604     9922     +318     
+ Misses       3535     3226     -309     
Files with missing lines Coverage Δ
src/scanpy/preprocessing/_simple.py 88.50% <42.85%> (+0.52%) ⬆️

... and 22 files with indirect coverage changes

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?

@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.

@@ -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
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
@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
@Intron7
Copy link
Member Author

Intron7 commented Feb 10, 2025

I renamed one variable to make is clearer what it is. Added some comments that should add more context what the code is doing.

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

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

@Intron7 Intron7 requested a review from ilan-gold February 13, 2025 14:47
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.

Why no test for the dtype if we're also fixing that bug here? or in #3461?

(["bulk_labels"], "regress_test_small_cat.npy", 1e-6),
],
)
def test_regress_out_reproducible(keys, test_file, atol):
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't we add a test for integer + float as a param to this test?

@flying-sheep flying-sheep modified the milestones: 1.12.0, 1.11.1 Feb 18, 2025
@flying-sheep flying-sheep changed the title Create cat regressor Speed up categorical regressor with numba Feb 18, 2025
@flying-sheep flying-sheep modified the milestones: 1.11.1, 1.11.2 Mar 31, 2025
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