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 39 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

❌ 7 Tests Failed:

Tests completed Failed Passed Skipped
2140 7 2133 96
View the top 3 failed test(s) by shortest run time
tests/test_preprocessing.py::test_regress_out_constants
Stack Traces | 0.013s run time
#x1B[0m#x1B[94mdef#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[92mtest_regress_out_constants#x1B[39;49;00m():#x1B[90m#x1B[39;49;00m
        adata = AnnData(np.hstack((np.full((#x1B[94m10#x1B[39;49;00m, #x1B[94m1#x1B[39;49;00m), #x1B[94m0.0#x1B[39;49;00m), np.full((#x1B[94m10#x1B[39;49;00m, #x1B[94m1#x1B[39;49;00m), #x1B[94m1.0#x1B[39;49;00m))))#x1B[90m#x1B[39;49;00m
        adata.obs[#x1B[33m"#x1B[39;49;00m#x1B[33mpercent_mito#x1B[39;49;00m#x1B[33m"#x1B[39;49;00m] = np.random.rand(adata.X.shape[#x1B[94m0#x1B[39;49;00m])#x1B[90m#x1B[39;49;00m
        adata.obs[#x1B[33m"#x1B[39;49;00m#x1B[33mn_counts#x1B[39;49;00m#x1B[33m"#x1B[39;49;00m] = adata.X.sum(axis=#x1B[94m1#x1B[39;49;00m)#x1B[90m#x1B[39;49;00m
        adata_copy = adata.copy()#x1B[90m#x1B[39;49;00m
    #x1B[90m#x1B[39;49;00m
>       sc.pp.regress_out(adata, keys=[#x1B[33m"#x1B[39;49;00m#x1B[33mn_counts#x1B[39;49;00m#x1B[33m"#x1B[39;49;00m, #x1B[33m"#x1B[39;49;00m#x1B[33mpercent_mito#x1B[39;49;00m#x1B[33m"#x1B[39;49;00m])#x1B[90m#x1B[39;49;00m

#x1B[1m#x1B[31mtests/test_preprocessing.py#x1B[0m:436: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
#x1B[1m#x1B[.../hostedtoolcache/Python/3.13.3.........................../x64/lib/python3.13.../site-packages/legacy_api_wrap/__init__.py#x1B[0m:82: in fn_compatible
    #x1B[0m#x1B[94mreturn#x1B[39;49;00m fn(*args_all, **kw)#x1B[90m#x1B[39;49;00m
#x1B[1m#x1B[.../hostedtoolcache/Python/3.13.3.........................../x64/lib/python3.13.../scanpy/preprocessing/_simple.py#x1B[0m:793: in regress_out
    #x1B[0mres = Parallel(n_jobs=n_jobs)(#x1B[90m#x1B[39;49;00m
#x1B[1m#x1B[.../hostedtoolcache/Python/3.13.3.........................../x64/lib/python3.13....../site-packages/joblib/parallel.py#x1B[0m:1985: in __call__
    #x1B[0m#x1B[94mreturn#x1B[39;49;00m output #x1B[94mif#x1B[39;49;00m #x1B[96mself#x1B[39;49;00m.return_generator #x1B[94melse#x1B[39;49;00m #x1B[96mlist#x1B[39;49;00m(output)#x1B[90m#x1B[39;49;00m
#x1B[1m#x1B[.../hostedtoolcache/Python/3.13.3.........................../x64/lib/python3.13....../site-packages/joblib/parallel.py#x1B[0m:1913: in _get_sequential_output
    #x1B[0mres = func(*args, **kwargs)#x1B[90m#x1B[39;49;00m
#x1B[1m#x1B[.../hostedtoolcache/Python/3.13.3.........................../x64/lib/python3.13.../scanpy/preprocessing/_simple.py#x1B[0m:815: in _regress_out_chunk
    #x1B[0m#x1B[94mimport#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[04m#x1B[96mstatsmodels#x1B[39;49;00m#x1B[04m#x1B[96m.#x1B[39;49;00m#x1B[04m#x1B[96mapi#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[94mas#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[04m#x1B[96msm#x1B[39;49;00m#x1B[90m#x1B[39;49;00m
#x1B[1m#x1B[.../hostedtoolcache/Python/3.13.3.........................../x64/lib/python3.13.../site-packages/statsmodels/api.py#x1B[0m:76: in <module>
    #x1B[0m#x1B[94mfrom#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[04m#x1B[96m.#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[94mimport#x1B[39;49;00m datasets, distributions, iolib, regression, robust, tools#x1B[90m#x1B[39;49;00m
#x1B[1m#x1B[.../hostedtoolcache/Python/3.13.3.........................../x64/lib/python3.13.../statsmodels/distributions/__init__.py#x1B[0m:7: in <module>
    #x1B[0m#x1B[94mfrom#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[04m#x1B[96m.#x1B[39;49;00m#x1B[04m#x1B[96mdiscrete#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[94mimport#x1B[39;49;00m (#x1B[90m#x1B[39;49;00m
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

    #x1B[0m#x1B[94mimport#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[04m#x1B[96mnumpy#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[94mas#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[04m#x1B[96mnp#x1B[39;49;00m#x1B[90m#x1B[39;49;00m
    #x1B[90m#x1B[39;49;00m
    #x1B[94mfrom#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[04m#x1B[96mscipy#x1B[39;49;00m#x1B[04m#x1B[96m.#x1B[39;49;00m#x1B[04m#x1B[96mstats#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[94mimport#x1B[39;49;00m rv_discrete, poisson, nbinom#x1B[90m#x1B[39;49;00m
    #x1B[94mfrom#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[04m#x1B[96mscipy#x1B[39;49;00m#x1B[04m#x1B[96m.#x1B[39;49;00m#x1B[04m#x1B[96mspecial#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[94mimport#x1B[39;49;00m gammaln#x1B[90m#x1B[39;49;00m
>   #x1B[94mfrom#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[04m#x1B[96mscipy#x1B[39;49;00m#x1B[04m#x1B[96m.#x1B[39;49;00m#x1B[04m#x1B[96m_lib#x1B[39;49;00m#x1B[04m#x1B[96m.#x1B[39;49;00m#x1B[04m#x1B[96m_util#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[94mimport#x1B[39;49;00m _lazywhere#x1B[90m#x1B[39;49;00m
#x1B[1m#x1B[31mE   ImportError: cannot import name '_lazywhere' from 'scipy._lib._util' (.../hostedtoolcache/Python/3.13.3.........................../x64/lib/python3.13.../scipy/_lib/_util.py)#x1B[0m

#x1B[1m#x1B[.../hostedtoolcache/Python/3.13.3.........................../x64/lib/python3.13.../statsmodels/distributions/discrete.py#x1B[0m:5: ImportError
tests/test_preprocessing.py::test_regress_out_int[float64]
Stack Traces | 0.104s run time
dtype = dtype('float64')

    #x1B[0m#x1B[37m@pytest#x1B[39;49;00m.mark.parametrize(#x1B[33m"#x1B[39;49;00m#x1B[33mdtype#x1B[39;49;00m#x1B[33m"#x1B[39;49;00m, [np.uint32, np.float64, np.uint64])#x1B[90m#x1B[39;49;00m
    #x1B[94mdef#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[92mtest_regress_out_int#x1B[39;49;00m(dtype):#x1B[90m#x1B[39;49;00m
        adata = pbmc3k()[:#x1B[94m200#x1B[39;49;00m, :#x1B[94m200#x1B[39;49;00m].copy()#x1B[90m#x1B[39;49;00m
        adata.X = adata.X.astype(np.float64 #x1B[94mif#x1B[39;49;00m dtype != np.uint32 #x1B[94melse#x1B[39;49;00m np.float32)#x1B[90m#x1B[39;49;00m
        dtype = adata.X.dtype#x1B[90m#x1B[39;49;00m
        adata.obs[#x1B[33m"#x1B[39;49;00m#x1B[33mlabels#x1B[39;49;00m#x1B[33m"#x1B[39;49;00m] = pd.Categorical(#x1B[90m#x1B[39;49;00m
            ([#x1B[33m"#x1B[39;49;00m#x1B[33mA#x1B[39;49;00m#x1B[33m"#x1B[39;49;00m] * (adata.X.shape[#x1B[94m0#x1B[39;49;00m] - #x1B[94m100#x1B[39;49;00m)) + ([#x1B[33m"#x1B[39;49;00m#x1B[33mB#x1B[39;49;00m#x1B[33m"#x1B[39;49;00m] * #x1B[94m100#x1B[39;49;00m)#x1B[90m#x1B[39;49;00m
        )#x1B[90m#x1B[39;49;00m
        adata_other = adata.copy()#x1B[90m#x1B[39;49;00m
        adata_other.X = adata_other.X.astype(dtype)#x1B[90m#x1B[39;49;00m
        #x1B[90m# results using only one processor#x1B[39;49;00m#x1B[90m#x1B[39;49;00m
>       sc.pp.regress_out(adata, keys=[#x1B[33m"#x1B[39;49;00m#x1B[33mlabels#x1B[39;49;00m#x1B[33m"#x1B[39;49;00m])#x1B[90m#x1B[39;49;00m

#x1B[1m#x1B[31mtests/test_preprocessing.py#x1B[0m:366: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
#x1B[1m#x1B[.../hostedtoolcache/Python/3.13.3.........................../x64/lib/python3.13.../site-packages/legacy_api_wrap/__init__.py#x1B[0m:82: in fn_compatible
    #x1B[0m#x1B[94mreturn#x1B[39;49;00m fn(*args_all, **kw)#x1B[90m#x1B[39;49;00m
#x1B[1m#x1B[.../hostedtoolcache/Python/3.13.3.........................../x64/lib/python3.13.../scanpy/preprocessing/_simple.py#x1B[0m:793: in regress_out
    #x1B[0mres = Parallel(n_jobs=n_jobs)(#x1B[90m#x1B[39;49;00m
#x1B[1m#x1B[.../hostedtoolcache/Python/3.13.3.........................../x64/lib/python3.13....../site-packages/joblib/parallel.py#x1B[0m:1985: in __call__
    #x1B[0m#x1B[94mreturn#x1B[39;49;00m output #x1B[94mif#x1B[39;49;00m #x1B[96mself#x1B[39;49;00m.return_generator #x1B[94melse#x1B[39;49;00m #x1B[96mlist#x1B[39;49;00m(output)#x1B[90m#x1B[39;49;00m
#x1B[1m#x1B[.../hostedtoolcache/Python/3.13.3.........................../x64/lib/python3.13....../site-packages/joblib/parallel.py#x1B[0m:1913: in _get_sequential_output
    #x1B[0mres = func(*args, **kwargs)#x1B[90m#x1B[39;49;00m
#x1B[1m#x1B[.../hostedtoolcache/Python/3.13.3.........................../x64/lib/python3.13.../scanpy/preprocessing/_simple.py#x1B[0m:815: in _regress_out_chunk
    #x1B[0m#x1B[94mimport#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[04m#x1B[96mstatsmodels#x1B[39;49;00m#x1B[04m#x1B[96m.#x1B[39;49;00m#x1B[04m#x1B[96mapi#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[94mas#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[04m#x1B[96msm#x1B[39;49;00m#x1B[90m#x1B[39;49;00m
#x1B[1m#x1B[.../hostedtoolcache/Python/3.13.3.........................../x64/lib/python3.13.../site-packages/statsmodels/api.py#x1B[0m:76: in <module>
    #x1B[0m#x1B[94mfrom#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[04m#x1B[96m.#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[94mimport#x1B[39;49;00m datasets, distributions, iolib, regression, robust, tools#x1B[90m#x1B[39;49;00m
#x1B[1m#x1B[.../hostedtoolcache/Python/3.13.3.........................../x64/lib/python3.13.../statsmodels/distributions/__init__.py#x1B[0m:7: in <module>
    #x1B[0m#x1B[94mfrom#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[04m#x1B[96m.#x1B[39;49;00m#x1B[04m#x1B[96mdiscrete#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[94mimport#x1B[39;49;00m (#x1B[90m#x1B[39;49;00m
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

    #x1B[0m#x1B[94mimport#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[04m#x1B[96mnumpy#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[94mas#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[04m#x1B[96mnp#x1B[39;49;00m#x1B[90m#x1B[39;49;00m
    #x1B[90m#x1B[39;49;00m
    #x1B[94mfrom#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[04m#x1B[96mscipy#x1B[39;49;00m#x1B[04m#x1B[96m.#x1B[39;49;00m#x1B[04m#x1B[96mstats#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[94mimport#x1B[39;49;00m rv_discrete, poisson, nbinom#x1B[90m#x1B[39;49;00m
    #x1B[94mfrom#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[04m#x1B[96mscipy#x1B[39;49;00m#x1B[04m#x1B[96m.#x1B[39;49;00m#x1B[04m#x1B[96mspecial#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[94mimport#x1B[39;49;00m gammaln#x1B[90m#x1B[39;49;00m
>   #x1B[94mfrom#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[04m#x1B[96mscipy#x1B[39;49;00m#x1B[04m#x1B[96m.#x1B[39;49;00m#x1B[04m#x1B[96m_lib#x1B[39;49;00m#x1B[04m#x1B[96m.#x1B[39;49;00m#x1B[04m#x1B[96m_util#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[94mimport#x1B[39;49;00m _lazywhere#x1B[90m#x1B[39;49;00m
#x1B[1m#x1B[31mE   ImportError: cannot import name '_lazywhere' from 'scipy._lib._util' (.../hostedtoolcache/Python/3.13.3.........................../x64/lib/python3.13.../scipy/_lib/_util.py)#x1B[0m

#x1B[1m#x1B[.../hostedtoolcache/Python/3.13.3.........................../x64/lib/python3.13.../statsmodels/distributions/discrete.py#x1B[0m:5: ImportError
tests/test_preprocessing.py::test_regress_out_int[uint32]
Stack Traces | 0.108s run time
dtype = dtype('float32')

    #x1B[0m#x1B[37m@pytest#x1B[39;49;00m.mark.parametrize(#x1B[33m"#x1B[39;49;00m#x1B[33mdtype#x1B[39;49;00m#x1B[33m"#x1B[39;49;00m, [np.uint32, np.float64, np.uint64])#x1B[90m#x1B[39;49;00m
    #x1B[94mdef#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[92mtest_regress_out_int#x1B[39;49;00m(dtype):#x1B[90m#x1B[39;49;00m
        adata = pbmc3k()[:#x1B[94m200#x1B[39;49;00m, :#x1B[94m200#x1B[39;49;00m].copy()#x1B[90m#x1B[39;49;00m
        adata.X = adata.X.astype(np.float64 #x1B[94mif#x1B[39;49;00m dtype != np.uint32 #x1B[94melse#x1B[39;49;00m np.float32)#x1B[90m#x1B[39;49;00m
        dtype = adata.X.dtype#x1B[90m#x1B[39;49;00m
        adata.obs[#x1B[33m"#x1B[39;49;00m#x1B[33mlabels#x1B[39;49;00m#x1B[33m"#x1B[39;49;00m] = pd.Categorical(#x1B[90m#x1B[39;49;00m
            ([#x1B[33m"#x1B[39;49;00m#x1B[33mA#x1B[39;49;00m#x1B[33m"#x1B[39;49;00m] * (adata.X.shape[#x1B[94m0#x1B[39;49;00m] - #x1B[94m100#x1B[39;49;00m)) + ([#x1B[33m"#x1B[39;49;00m#x1B[33mB#x1B[39;49;00m#x1B[33m"#x1B[39;49;00m] * #x1B[94m100#x1B[39;49;00m)#x1B[90m#x1B[39;49;00m
        )#x1B[90m#x1B[39;49;00m
        adata_other = adata.copy()#x1B[90m#x1B[39;49;00m
        adata_other.X = adata_other.X.astype(dtype)#x1B[90m#x1B[39;49;00m
        #x1B[90m# results using only one processor#x1B[39;49;00m#x1B[90m#x1B[39;49;00m
>       sc.pp.regress_out(adata, keys=[#x1B[33m"#x1B[39;49;00m#x1B[33mlabels#x1B[39;49;00m#x1B[33m"#x1B[39;49;00m])#x1B[90m#x1B[39;49;00m

#x1B[1m#x1B[31mtests/test_preprocessing.py#x1B[0m:366: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
#x1B[1m#x1B[.../hostedtoolcache/Python/3.13.3.........................../x64/lib/python3.13.../site-packages/legacy_api_wrap/__init__.py#x1B[0m:82: in fn_compatible
    #x1B[0m#x1B[94mreturn#x1B[39;49;00m fn(*args_all, **kw)#x1B[90m#x1B[39;49;00m
#x1B[1m#x1B[.../hostedtoolcache/Python/3.13.3.........................../x64/lib/python3.13.../scanpy/preprocessing/_simple.py#x1B[0m:793: in regress_out
    #x1B[0mres = Parallel(n_jobs=n_jobs)(#x1B[90m#x1B[39;49;00m
#x1B[1m#x1B[.../hostedtoolcache/Python/3.13.3.........................../x64/lib/python3.13....../site-packages/joblib/parallel.py#x1B[0m:1985: in __call__
    #x1B[0m#x1B[94mreturn#x1B[39;49;00m output #x1B[94mif#x1B[39;49;00m #x1B[96mself#x1B[39;49;00m.return_generator #x1B[94melse#x1B[39;49;00m #x1B[96mlist#x1B[39;49;00m(output)#x1B[90m#x1B[39;49;00m
#x1B[1m#x1B[.../hostedtoolcache/Python/3.13.3.........................../x64/lib/python3.13....../site-packages/joblib/parallel.py#x1B[0m:1913: in _get_sequential_output
    #x1B[0mres = func(*args, **kwargs)#x1B[90m#x1B[39;49;00m
#x1B[1m#x1B[.../hostedtoolcache/Python/3.13.3.........................../x64/lib/python3.13.../scanpy/preprocessing/_simple.py#x1B[0m:815: in _regress_out_chunk
    #x1B[0m#x1B[94mimport#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[04m#x1B[96mstatsmodels#x1B[39;49;00m#x1B[04m#x1B[96m.#x1B[39;49;00m#x1B[04m#x1B[96mapi#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[94mas#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[04m#x1B[96msm#x1B[39;49;00m#x1B[90m#x1B[39;49;00m
#x1B[1m#x1B[.../hostedtoolcache/Python/3.13.3.........................../x64/lib/python3.13.../site-packages/statsmodels/api.py#x1B[0m:76: in <module>
    #x1B[0m#x1B[94mfrom#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[04m#x1B[96m.#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[94mimport#x1B[39;49;00m datasets, distributions, iolib, regression, robust, tools#x1B[90m#x1B[39;49;00m
#x1B[1m#x1B[.../hostedtoolcache/Python/3.13.3.........................../x64/lib/python3.13.../statsmodels/distributions/__init__.py#x1B[0m:7: in <module>
    #x1B[0m#x1B[94mfrom#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[04m#x1B[96m.#x1B[39;49;00m#x1B[04m#x1B[96mdiscrete#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[94mimport#x1B[39;49;00m (#x1B[90m#x1B[39;49;00m
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

    #x1B[0m#x1B[94mimport#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[04m#x1B[96mnumpy#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[94mas#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[04m#x1B[96mnp#x1B[39;49;00m#x1B[90m#x1B[39;49;00m
    #x1B[90m#x1B[39;49;00m
    #x1B[94mfrom#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[04m#x1B[96mscipy#x1B[39;49;00m#x1B[04m#x1B[96m.#x1B[39;49;00m#x1B[04m#x1B[96mstats#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[94mimport#x1B[39;49;00m rv_discrete, poisson, nbinom#x1B[90m#x1B[39;49;00m
    #x1B[94mfrom#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[04m#x1B[96mscipy#x1B[39;49;00m#x1B[04m#x1B[96m.#x1B[39;49;00m#x1B[04m#x1B[96mspecial#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[94mimport#x1B[39;49;00m gammaln#x1B[90m#x1B[39;49;00m
>   #x1B[94mfrom#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[04m#x1B[96mscipy#x1B[39;49;00m#x1B[04m#x1B[96m.#x1B[39;49;00m#x1B[04m#x1B[96m_lib#x1B[39;49;00m#x1B[04m#x1B[96m.#x1B[39;49;00m#x1B[04m#x1B[96m_util#x1B[39;49;00m#x1B[90m #x1B[39;49;00m#x1B[94mimport#x1B[39;49;00m _lazywhere#x1B[90m#x1B[39;49;00m
#x1B[1m#x1B[31mE   ImportError: cannot import name '_lazywhere' from 'scipy._lib._util' (.../hostedtoolcache/Python/3.13.3.........................../x64/lib/python3.13.../scipy/_lib/_util.py)#x1B[0m

#x1B[1m#x1B[.../hostedtoolcache/Python/3.13.3.........................../x64/lib/python3.13.../statsmodels/distributions/discrete.py#x1B[0m:5: ImportError

To view more test analytics, go to the Test Analytics Dashboard
📋 Got 3 mins? Take this short survey to help us improve Test Analytics.

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
@Intron7 Intron7 requested a review from ilan-gold February 10, 2025 16:16
@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

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes! discoverered a bug! regressors needs to be float32 to match old behavior, not sure why it was hardcoded

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

@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