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

Regress out #2456

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions scanpy/preprocessing/_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import numba
import numpy as np
import scipy as sp
import time
from scipy.sparse import issparse, isspmatrix_csr, csr_matrix, spmatrix
from sklearn.utils import sparsefuncs, check_array
from pandas.api.types import is_categorical_dtype
Expand Down Expand Up @@ -566,6 +567,44 @@ def normalize_per_cell(
return X if copy else None


@numba.njit(cache=True, parallel=True)
def to_dense(shp, indptr, indices, data):
X = np.empty(shp, dtype=data.dtype)
for r in numba.prange(shp[0]):
X[r] = 0
for i in range(indptr[r], indptr[r + 1]):
X[r, indices[i]] = data[i]
return X


def get_resid(X, A, coeff):
for i in numba.prange(X.shape[0]):
X[i] -= A[i] @ coeff


def numpy_regress_out(adata: AnnData, regr) -> Optional[AnnData]:
start = time.time()
if issparse(adata.X):
adata.X = to_dense(adata.X.shape, adata.X.indptr, adata.X.indices, adata.X.data)
print("convert to dense done at", time.time() - start)
A = np.empty((adata.shape[0], len(regr) + 1), dtype=adata.X.dtype)
A[:, 0] = 1
for i, c in enumerate(regr):
A[:, i + 1] = adata.obs[c]
print("prep done at", time.time() - start)
tmp0 = A.T @ A
print(time.time() - start)
tmp = np.linalg.inv(tmp0)
print(time.time() - start)
tmp0 = A.T @ adata.X
print(time.time() - start)
coeff = tmp @ tmp0
# print (coeff)
print("regression done at", time.time() - start)
get_resid(adata.X, A, coeff)
print("residuals done at", time.time() - start)


def regress_out(
adata: AnnData,
keys: Union[str, Sequence[str]],
Expand Down