diff --git a/decoupler/benchmark.py b/decoupler/benchmark.py index 92c24b3..cf91173 100644 --- a/decoupler/benchmark.py +++ b/decoupler/benchmark.py @@ -149,9 +149,9 @@ def _benchmark(mat, obs, net, perturb, sign, metrics=['auroc', 'auprc'], groupby return df -def benchmark(mat, obs, net, perturb, sign, metrics=['auroc', 'auprc', 'mcauroc', 'mcauprc', 'rank', 'nrank', 'recall'], groupby=None, - by='experiment', f_expr=True, f_srcs=False, use_pval=None, min_exp=5, pi0=0.5, n_iter=1000, seed=42, - verbose=True, use_raw=True, decouple_kws={}): +def benchmark(mat, obs, net, perturb, sign, metrics=['auroc', 'auprc', 'mcauroc', 'mcauprc', 'rank', 'nrank', 'recall'], + groupby=None, by='experiment', f_expr=True, f_srcs=False, use_pval=None, min_exp=5, pi0=0.5, n_iter=1000, + seed=42, verbose=True, use_raw=True, decouple_kws={}): """ Benchmark methods or networks on a given set of perturbation experiments using activity inference with decoupler. diff --git a/decoupler/method_aucell.py b/decoupler/method_aucell.py index 9723061..522030d 100644 --- a/decoupler/method_aucell.py +++ b/decoupler/method_aucell.py @@ -12,7 +12,6 @@ from .pre import extract, rename_net, filt_min_n, return_data -from anndata import AnnData import numba as nb diff --git a/decoupler/method_gsea.py b/decoupler/method_gsea.py index f1219ef..177a5b6 100644 --- a/decoupler/method_gsea.py +++ b/decoupler/method_gsea.py @@ -12,7 +12,6 @@ from .pre import extract, rename_net, filt_min_n, return_data from .utils import p_adjust_fdr -from anndata import AnnData from tqdm import tqdm import numba as nb diff --git a/decoupler/method_gsva.py b/decoupler/method_gsva.py index 69a047c..e1725ef 100644 --- a/decoupler/method_gsva.py +++ b/decoupler/method_gsva.py @@ -13,7 +13,6 @@ from .pre import extract, rename_net, filt_min_n, return_data from .method_gsea import std -from anndata import AnnData from tqdm import tqdm import numba as nb diff --git a/decoupler/method_mdt.py b/decoupler/method_mdt.py index 6e44785..7cfa0fc 100644 --- a/decoupler/method_mdt.py +++ b/decoupler/method_mdt.py @@ -9,7 +9,6 @@ from .pre import extract, match, rename_net, get_net_mat, filt_min_n, return_data -from anndata import AnnData from tqdm import tqdm diff --git a/decoupler/method_mlm.py b/decoupler/method_mlm.py index 998cc31..9c4a48e 100644 --- a/decoupler/method_mlm.py +++ b/decoupler/method_mlm.py @@ -9,7 +9,6 @@ from .pre import extract, match, rename_net, get_net_mat, filt_min_n, return_data -from anndata import AnnData from scipy import stats from tqdm import tqdm diff --git a/decoupler/method_ora.py b/decoupler/method_ora.py index 5233e4f..469940e 100644 --- a/decoupler/method_ora.py +++ b/decoupler/method_ora.py @@ -15,7 +15,6 @@ from .pre import extract, rename_net, filt_min_n, return_data from .utils import p_adjust_fdr -from anndata import AnnData from tqdm import tqdm import numba as nb diff --git a/decoupler/method_udt.py b/decoupler/method_udt.py index f41957c..f3f0039 100644 --- a/decoupler/method_udt.py +++ b/decoupler/method_udt.py @@ -9,7 +9,6 @@ from .pre import extract, match, rename_net, get_net_mat, filt_min_n, return_data -from anndata import AnnData from tqdm import tqdm diff --git a/decoupler/method_ulm.py b/decoupler/method_ulm.py index 53de352..b0fa871 100644 --- a/decoupler/method_ulm.py +++ b/decoupler/method_ulm.py @@ -11,7 +11,6 @@ from .pre import extract, match, rename_net, get_net_mat, filt_min_n, return_data -from anndata import AnnData from tqdm import tqdm diff --git a/decoupler/method_viper.py b/decoupler/method_viper.py index 09cfbb2..d81bd49 100644 --- a/decoupler/method_viper.py +++ b/decoupler/method_viper.py @@ -12,7 +12,6 @@ from .pre import extract, match, rename_net, get_net_mat, filt_min_n, return_data -from anndata import AnnData from tqdm import tqdm import numba as nb diff --git a/decoupler/method_wmean.py b/decoupler/method_wmean.py index 68b8b7e..2de7f7e 100644 --- a/decoupler/method_wmean.py +++ b/decoupler/method_wmean.py @@ -10,7 +10,6 @@ from .pre import extract, match, rename_net, get_net_mat, filt_min_n, return_data from .method_gsea import std -from anndata import AnnData from tqdm import tqdm import numba as nb diff --git a/decoupler/method_wsum.py b/decoupler/method_wsum.py index dbd4eda..f3e7a6c 100644 --- a/decoupler/method_wsum.py +++ b/decoupler/method_wsum.py @@ -10,7 +10,6 @@ from .pre import extract, match, rename_net, get_net_mat, filt_min_n, return_data from .method_gsea import std -from anndata import AnnData from tqdm import tqdm import numba as nb diff --git a/decoupler/omnip.py b/decoupler/omnip.py index 2c704d7..82b3735 100644 --- a/decoupler/omnip.py +++ b/decoupler/omnip.py @@ -73,31 +73,32 @@ def _check_if_omnipath() -> ModuleType: def _check_if_pypath() -> None: + def ver(v): + return tuple(map(int, v.split('.'))) + # Check if pypath is installed try: - - def ver(v): - return tuple(map(int, v.split('.'))) - import pypath - if ( - getattr(pypath, '__version__', None) and - ver(pypath.__version__) < ver(PYPATH_MIN_VERSION) - ): - - msg = ( - 'The installed version of pypath-omnipath is too old, ' - f'the oldest compatible version is {PYPATH_MIN_VERSION}.' - ) - _misc.log_traceback(msg) - raise RuntimeError(msg) - except Exception: - msg = ( 'pypath-omnipath is not installed. Please install it with: ' - 'pip install git+https://github.com/saezlab/pypath.git' + 'pip install pypath-omnipath' + ) + _misc.log_traceback(msg) + raise ImportError(msg) + + if (getattr(pypath, '__version__', None) and ver(pypath.__version__) < ver(PYPATH_MIN_VERSION)): + msg = ( + 'The installed version of pypath-omnipath is too old, ' + f'the oldest compatible version is {PYPATH_MIN_VERSION}.' + ) + _misc.log_traceback(msg) + raise ImportError(msg) + elif not callable(getattr(pypath, "disclaimer", False)): + msg = ( + 'pypath is installed instead of pypath-omnipath. Please, remove' + 'pypath (pip uninstall pypath) and install pypath-omnipath (pip install pypath-omnipath)' ) _misc.log_traceback(msg) raise ImportError(msg) @@ -193,7 +194,7 @@ def get_progeny( None ) = None, **kwargs - ) -> pd.DataFrame: + ) -> pd.DataFrame: """ Pathway RespOnsive GENes for activity inference (PROGENy). @@ -244,7 +245,7 @@ def get_progeny( p = p.unstack('label').droplevel(axis=1, level=0) p.columns = np.array(p.columns) p = p.reset_index() - p = p.drop('record_id', axis = 1) + p = p.drop('record_id', axis=1) p.columns.name = None p = _annotation_identifiers(p, organism, genesymbol_resource) p = p[['pathway', 'genesymbol', 'weight', 'p_value']] @@ -257,7 +258,7 @@ def get_progeny( groupby('pathway'). head(top). sort_values(['pathway', 'p_value']). - reset_index(drop = True) + reset_index(drop=True) ) p.columns = ['source', 'target', 'weight', 'p_value'] p = op._misc.dtypes.auto_dtype(p) @@ -613,11 +614,11 @@ def get_collectri( if genesymbol_resource and genesymbol_resource != 'uniprot': mirna = add_genesymbols( - net = mirna, - column = 'source', - target_column = 'source_genesymbol', - organism = organism, - resource = genesymbol_resource, + net=mirna, + column='source', + target_column='source_genesymbol', + organism=organism, + resource=genesymbol_resource, ) elif genesymbol_resource is False: @@ -658,7 +659,7 @@ def get_collectri( # Select and rename columns ct = ct.rename( - columns = { + columns={ 'source_genesymbol': 'source', 'target_genesymbol': 'target', 'references_stripped': 'PMID', @@ -780,7 +781,7 @@ def add_genesymbols( Literal['uniprot', 'ensembl'] | dict[str, set[str]] ) = 'uniprot', - ) -> pd.DataFrame: + ) -> pd.DataFrame: """ Add or update Gene Symbols in data frame. @@ -819,15 +820,15 @@ def add_genesymbols( raise ValueError('Resource must be either "uniprot" or "ensembl".') mapping_def = mapping_cls( - id_type_a = 'genesymbol', - id_type_b = 'uniprot', - ncbi_tax_id = _organism, + id_type_a='genesymbol', + id_type_b='uniprot', + ncbi_tax_id=_organism, ) resource = mapping.MapReader( mapping_def, - load_b_to_a = True, - load_a_to_b = False, + load_b_to_a=True, + load_a_to_b=False, ).b_to_a mapping_table = pd.DataFrame( @@ -837,19 +838,19 @@ def add_genesymbols( resource.items() for g in gs ), - columns = (column, target_column), + columns=(column, target_column), ) if target_column in net.columns: col_idx = net.columns.get_loc(target_column) - net = net.drop(target_column, axis = 1) + net = net.drop(target_column, axis=1) else: col_idx = net.columns.get_loc(column) + 1 - net = net.merge(mapping_table, how = 'left', on = column) + net = net.merge(mapping_table, how='left', on=column) net = net[ list(net.columns[0:col_idx]) + [target_column] + @@ -868,7 +869,7 @@ def _network_identifiers( bool | None ) = None, - ) -> pd.DataFrame: + ) -> pd.DataFrame: _organism = _the_organism(organism) @@ -887,11 +888,11 @@ def _network_identifiers( for side in ('source', 'target'): net = add_genesymbols( - net = net, - column = side, - target_column = f'{side}_genesymbol', - organism = organism, - resource = genesymbol_resource, + net=net, + column=side, + target_column=f'{side}_genesymbol', + organism=organism, + resource=genesymbol_resource, ) elif genesymbol_resource is False: @@ -911,7 +912,7 @@ def _annotation_identifiers( bool | None ) = None, - ) -> pd.DataFrame: + ) -> pd.DataFrame: if not _is_human(organism): @@ -932,11 +933,11 @@ def _annotation_identifiers( genesymbol_resource = genesymbol_resource or 'uniprot' net = add_genesymbols( - net = net, - column = 'uniprot', - target_column = 'genesymbol', - organism = organism, - resource = genesymbol_resource, + net=net, + column='uniprot', + target_column='genesymbol', + organism=organism, + resource=genesymbol_resource, ) return net @@ -944,7 +945,7 @@ def _annotation_identifiers( def get_ksn_omnipath( organism: str | int = 'human', - ) -> pd.DataFrame: + ) -> pd.DataFrame: """ OmniPath kinase-substrate network @@ -964,7 +965,7 @@ def get_ksn_omnipath( _organism = _the_organism(organism) # Load Kinase-Substrate Network - ksn = op.requests.Enzsub.get(genesymbols=True, organism = _organism) + ksn = op.requests.Enzsub.get(genesymbols=True, organism=_organism) # Filter by phosphorilation cols = ['enzyme_genesymbol', 'substrate_genesymbol', 'residue_type', diff --git a/decoupler/utils.py b/decoupler/utils.py index c3e4cd2..2bb319b 100644 --- a/decoupler/utils.py +++ b/decoupler/utils.py @@ -335,7 +335,7 @@ def p_adjust_fdr(p): """ # Code adapted from: https://stackoverflow.com/a/33532498/8395875 - p = np.asarray(p, dtype = 'float64') + p = np.asarray(p, dtype='float64') by_descend = p.argsort()[::-1] by_orig = by_descend.argsort() steps = float(len(p)) / np.arange(len(p), 0, -1) diff --git a/decoupler/utils_benchmark.py b/decoupler/utils_benchmark.py index 976a374..65c6f38 100644 --- a/decoupler/utils_benchmark.py +++ b/decoupler/utils_benchmark.py @@ -7,7 +7,7 @@ from numpy.random import default_rng import pandas as pd import scipy -from scipy.sparse import csr_matrix, issparse +from scipy.sparse import issparse from .utils import get_toy_data from .pre import match