Skip to content

Commit

Permalink
update doc strings and clean up names
Browse files Browse the repository at this point in the history
  • Loading branch information
Intron7 committed Feb 10, 2025
1 parent 3b7fe6e commit 726a625
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/scanpy/preprocessing/_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,12 +639,15 @@ def normalize_per_cell(

@njit
def _create_regressor_categorical(
X: np.ndarray, number_categories: int, filters: np.ndarray
X: np.ndarray, number_categories: int, cat_array: np.ndarray
) -> np.ndarray:
# create regressor matrix faster for categorical variables
# create regressor matrix for categorical variables
regressors = np.zeros(X.shape, dtype=X.dtype)

Check warning on line 645 in src/scanpy/preprocessing/_simple.py

View check run for this annotation

Codecov / codecov/patch

src/scanpy/preprocessing/_simple.py#L645

Added line #L645 was not covered by tests
# iterate over categories
for category in range(number_categories):

Check warning on line 647 in src/scanpy/preprocessing/_simple.py

View check run for this annotation

Codecov / codecov/patch

src/scanpy/preprocessing/_simple.py#L647

Added line #L647 was not covered by tests
mask = category == filters
# iterate over genes and calculate mean expression
# for each gene per category
mask = category == cat_array
for ix in numba.prange(X.T.shape[0]):
regressors[mask, ix] = X.T[ix][mask].mean()
return regressors

Check warning on line 653 in src/scanpy/preprocessing/_simple.py

View check run for this annotation

Codecov / codecov/patch

src/scanpy/preprocessing/_simple.py#L650-L653

Added lines #L650 - L653 were not covered by tests
Expand Down Expand Up @@ -745,14 +748,13 @@ def regress_out(
)
raise ValueError(msg)
logg.debug("... regressing on per-gene means within categories")
# Create numpy array's from categorical variable
# The dtype of the array needs to be the dtype of the 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)

# set number of categories to the same dtype as the categories
cat_array = adata.obs[keys[0]].cat.codes.to_numpy()
number_categories = cat_array.dtype.type(len(adata.obs[keys[0]].cat.categories))

X = _to_dense(X, order="F") if issparse(X) else X
regressors = _create_regressor_categorical(X, number_categories, filters)
regressors = _create_regressor_categorical(X, number_categories, cat_array)
variable_is_categorical = True
# regress on one or several ordinal variables
else:
Expand Down

0 comments on commit 726a625

Please sign in to comment.