Skip to content

Commit

Permalink
Flake8
Browse files Browse the repository at this point in the history
  • Loading branch information
PauBadiaM committed Aug 7, 2024
1 parent 9798266 commit 4ad1b06
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 16 deletions.
1 change: 0 additions & 1 deletion decoupler/method_aucell.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import pandas as pd
from scipy.sparse import csr_matrix

from numpy.random import default_rng
from tqdm.auto import tqdm

from .pre import extract, rename_net, filt_min_n, return_data, break_ties
Expand Down
3 changes: 1 addition & 2 deletions decoupler/method_gsva.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
import numpy as np
import pandas as pd

from scipy.stats import norm
from scipy.sparse import csr_matrix
from numpy.random import default_rng
import math

from .pre import extract, rename_net, filt_min_n, return_data, break_ties
Expand Down Expand Up @@ -36,6 +34,7 @@ def erf(x):
y = 1.0 - (((((a5 * t + a4) * t + a3) * t + a2) * t + a1) * t * np.exp(-abs_x * abs_x))
return sign * y


@nb.njit(nb.f8[:](nb.f8[:], nb.f8, nb.f8), cache=True)
def norm_cdf(x, mu=0.0, sigma=1.0):
e = erf((x - mu) / (sigma * np.sqrt(2.0)))
Expand Down
1 change: 0 additions & 1 deletion decoupler/method_ora.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import numpy as np
import pandas as pd

from numpy.random import default_rng
from scipy.sparse import csr_matrix

from scipy.stats import rankdata
Expand Down
1 change: 1 addition & 0 deletions decoupler/method_udt.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from tqdm.auto import tqdm


def check_if_sklearn():
try:
import sklearn as sk
Expand Down
11 changes: 6 additions & 5 deletions decoupler/method_zscore.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ def zscore(m, net, flavor='RoKAI', verbose=False):


def run_zscore(mat, net, source='source', target='target', weight='weight', batch_size=10000, flavor='RoKAI',
min_n=5, verbose=False, use_raw=True):
min_n=5, verbose=False, use_raw=True):
"""
z-score.
Calculates regulatory activities using a z-score as descibed in KSEA or RoKAI. The z-score calculates the mean of the molecular features of the
known targets for each regulator and adjusts it for the number of identified targets for the regulator, the standard deviation of all molecular
features (RoKAI), as well as the mean of all moleculare features (KSEA).
Calculates regulatory activities using a z-score as descibed in KSEA or RoKAI. The z-score calculates the mean of the
molecular features of the known targets for each regulator and adjusts it for the number of identified targets for the
regulator, the standard deviation of all molecular features (RoKAI), as well as the mean of all moleculare features (KSEA).
Parameters
----------
Expand Down Expand Up @@ -74,7 +74,8 @@ def run_zscore(mat, net, source='source', target='target', weight='weight', batc
net = match(c, targets, net)

if verbose:
print('Running zscore on mat with {0} samples and {1} targets for {2} sources.'.format(m.shape[0], len(c), net.shape[1]))
print('Running zscore on mat with {0} samples and {1} targets for {2} sources.'.format(
m.shape[0], len(c), net.shape[1]))

# Run ULM
estimate, pvals = zscore(m, net, flavor=flavor)
Expand Down
1 change: 0 additions & 1 deletion decoupler/pre.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,4 +307,3 @@ def break_ties(m, c, seed):
idx = rng.choice(idx, c.size, replace=False)
m, c = m[:, idx], c[idx]
return m, c

3 changes: 2 additions & 1 deletion decoupler/tests/test_gsva.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def test_ppois():


def test_norm_cdf():
assert np.isclose(norm_cdf(np.array([1, 2, 3], dtype=float), mu=0.0, sigma=1.0), np.array([0.8413447, 0.9772499, 0.9986501])).all()
assert np.isclose(norm_cdf(np.array([1, 2, 3], dtype=float), mu=0.0, sigma=1.0),
np.array([0.8413447, 0.9772499, 0.9986501])).all()
assert np.isclose(norm_cdf(np.array([0, 9, 1], dtype=float), mu=0.0, sigma=1.0), np.array([0.5, 1., 0.8413447])).all()


Expand Down
13 changes: 8 additions & 5 deletions decoupler/tests/test_zscore.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
from anndata import AnnData
from ..method_zscore import zscore, run_zscore


def test_zscore():
m = np.array([[-7., -1., 1., 1.], [-4., -2., 1., 2.], [1., 2., 5., 1.], [1., 1., 6., 2.], [-8., -7., 1., 1.]], dtype=np.float32)
m = np.array([[-7., -1., 1., 1.], [-4., -2., 1., 2.], [1., 2., 5., 1.],
[1., 1., 6., 2.], [-8., -7., 1., 1.]], dtype=np.float32)
net = np.array([[1., 0.], [1, 0.], [0., -1.], [0., -1.]], dtype=np.float32)
act, pvl = zscore(m, net)
assert act[0, 0] < 0
Expand All @@ -13,7 +15,7 @@ def test_zscore():
assert act[3, 0] > 0
assert act[4, 0] < 0
assert np.all((0. <= pvl) * (pvl <= 1.))

act2, pvl2 = zscore(m, net, flavor='KSEA')
assert act2[0, 0] < 0
assert act2[1, 0] < 0
Expand All @@ -22,6 +24,7 @@ def test_zscore():
assert act2[4, 0] < 0
assert np.all((0. <= pvl2) * (pvl2 <= 1.))


def test_run_zscore():
m = np.array([[-7., -1., 1., 1.], [-4., -2., 1., 2.], [1., 2., 5., 1.], [1., 1., -6., -8.], [-8., -7., 1., 1.]])
r = np.array(['S1', 'S2', 'S3', 'S4', 'S5'])
Expand All @@ -36,14 +39,14 @@ def test_run_zscore():
assert res[0].loc['S4', 'T2'] > 0
assert res[0].loc['S5', 'T2'] < 0
assert res[1].map(lambda x: 0 <= x <= 1).all().all()

res2 = run_zscore(df, net, verbose=True, use_raw=False, min_n=0, flavor='KSEA')
assert res2[0].loc['S1', 'T2'] > 0
assert res2[0].loc['S2', 'T2'] < 0
assert res2[0].loc['S3', 'T2'] < 0
assert res2[0].loc['S4', 'T2'] > 0
assert res2[0].loc['S5', 'T2'] > 0
assert res2[1].map(lambda x: 0 <= x <= 1).all().all()

adata = AnnData(df.astype(np.float32))
run_zscore(adata, net, verbose=True, use_raw=False, min_n=0)
run_zscore(adata, net, verbose=True, use_raw=False, min_n=0)

0 comments on commit 4ad1b06

Please sign in to comment.