88from functools import partial
99from itertools import product
1010from types import MappingProxyType
11- from typing import TYPE_CHECKING , Any , Literal , TypeAlias
11+ from typing import TYPE_CHECKING , Any , Literal
1212
1313import numpy as np
1414import pandas as pd
1717from scipy .sparse import csc_matrix
1818from spatialdata import SpatialData
1919
20- from squidpy ._backends import dispatch
20+ from squidpy ._backends import backend_dispatch
2121from squidpy ._constants ._constants import ComplexPolicy , CorrAxis
2222from squidpy ._constants ._pkg_constants import Key
2323from squidpy ._docs import d , inject_docs
24- from squidpy ._utils import NDArrayA , Signal , SigQueue , _get_n_cores , parallelize
24+ from squidpy ._utils import NDArrayA , Signal , SigQueue , _deprecate_backend_as_parallel_backend , _get_n_cores , parallelize
2525from squidpy ._validators import assert_positive , check_tuple_needles
2626from squidpy .gr ._utils import (
2727 _assert_categorical_obs ,
3131
3232__all__ = ["ligrec" , "PermutationTest" ]
3333
34- StrSeq : TypeAlias = Sequence [str ]
35- SeqTuple : TypeAlias = Sequence [tuple [str , str ]]
36- Interaction_t : TypeAlias = pd .DataFrame | Mapping [str , StrSeq ] | StrSeq | tuple [StrSeq , StrSeq ] | SeqTuple
34+ type StrSeq = Sequence [str ]
35+ type SeqTuple = Sequence [tuple [str , str ]]
36+ type Interaction_t = pd .DataFrame | Mapping [str , StrSeq ] | StrSeq | tuple [StrSeq , StrSeq ] | SeqTuple
3737
38- Cluster_t : TypeAlias = StrSeq | tuple [StrSeq , StrSeq ] | SeqTuple
38+ type Cluster_t = StrSeq | tuple [StrSeq , StrSeq ] | SeqTuple
3939
4040SOURCE = "source"
4141TARGET = "target"
@@ -314,6 +314,7 @@ def prepare(
314314 @d .get_sections (base = "PT_test" , sections = ["Parameters" ])
315315 @d .dedent
316316 @inject_docs (src = SOURCE , tgt = TARGET , fa = CorrAxis )
317+ @_deprecate_backend_as_parallel_backend
317318 def test (
318319 self ,
319320 cluster_key : str ,
@@ -327,6 +328,7 @@ def test(
327328 copy : bool = False ,
328329 key_added : str | None = None ,
329330 numba_parallel : bool | None = None ,
331+ parallel_backend : str = "loky" ,
330332 ** kwargs : Any ,
331333 ) -> Mapping [str , pd .DataFrame ] | None :
332334 """
@@ -357,6 +359,8 @@ def test(
357359 If `None`, ``'{{cluster_key}}_ligrec'`` will be used.
358360 %(numba_parallel)s
359361 %(parallelize)s
362+ parallel_backend
363+ Which joblib backend to use for permutation parallelism.
360364
361365 Returns
362366 -------
@@ -423,6 +427,7 @@ def test(
423427 seed = seed ,
424428 n_jobs = n_jobs ,
425429 numba_parallel = numba_parallel ,
430+ parallel_backend = parallel_backend ,
426431 ** kwargs ,
427432 )
428433 index = pd .MultiIndex .from_frame (interactions , names = [SOURCE , TARGET ])
@@ -630,7 +635,8 @@ def prepare(
630635
631636
632637@d .dedent
633- @dispatch
638+ @_deprecate_backend_as_parallel_backend
639+ @backend_dispatch
634640def ligrec (
635641 adata : AnnData | SpatialData ,
636642 cluster_key : str ,
@@ -643,6 +649,7 @@ def ligrec(
643649 copy : bool = False ,
644650 key_added : str | None = None ,
645651 gene_symbols : str | None = None ,
652+ parallel_backend : str = "loky" ,
646653 ** kwargs : Any ,
647654) -> Mapping [str , pd .DataFrame ] | None :
648655 """
@@ -655,6 +662,8 @@ def ligrec(
655662 %(PT_test.parameters)s
656663 gene_symbols
657664 Key in :attr:`anndata.AnnData.var` to use instead of :attr:`anndata.AnnData.var_names`.
665+ parallel_backend
666+ Which joblib backend to use for permutation parallelism.
658667
659668 Returns
660669 -------
@@ -673,6 +682,7 @@ def ligrec(
673682 corr_axis = corr_axis ,
674683 copy = copy ,
675684 key_added = key_added ,
685+ parallel_backend = parallel_backend ,
676686 ** kwargs ,
677687 )
678688 )
@@ -688,6 +698,7 @@ def _analysis(
688698 seed : int | None = None ,
689699 n_jobs : int = 1 ,
690700 numba_parallel : bool | None = None ,
701+ parallel_backend : str = "loky" ,
691702 ** kwargs : Any ,
692703) -> TempResult :
693704 """
@@ -711,8 +722,10 @@ def _analysis(
711722 Number of parallel jobs to launch.
712723 numba_parallel
713724 Whether to use :func:`numba.prange` or not. If `None`, it's determined automatically.
725+ parallel_backend
726+ Which joblib backend to use for permutation parallelism.
714727 kwargs
715- Keyword arguments for :func:`squidpy._utils.parallelize`, such as ``n_jobs`` or ``backend` `.
728+ Additional keyword arguments for :func:`squidpy._utils.parallelize`.
716729
717730 Returns
718731 -------
@@ -756,6 +769,7 @@ def extractor(res: Sequence[TempResult]) -> TempResult:
756769 _analysis_helper ,
757770 np .arange (n_perms , dtype = np .int32 ).tolist (),
758771 n_jobs = n_jobs ,
772+ backend = parallel_backend ,
759773 unit = "permutation" ,
760774 extractor = extractor ,
761775 ** kwargs ,
0 commit comments