-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update all-genes and per-gene mode to use the Python's version to fin…
…d thresholds
- Loading branch information
Mai To Uyen
authored and
Mai To Uyen
committed
Jan 10, 2021
1 parent
fc2c79c
commit c04e29e
Showing
2 changed files
with
58 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from scipy.stats import gaussian_kde,lognorm | ||
from math import log, exp | ||
from statistics import mean,stdev | ||
|
||
EPS = 1e-5 | ||
|
||
def find_threshold_loglnorm(data,quantiles): | ||
x = [log(log(y)) for y in data] | ||
mu = mean(x) | ||
sigma = stdev(x) | ||
return [exp(t) for t in lognorm.ppf(quantiles, sigma, scale=exp(mu))] | ||
|
||
def find_threshold_lkernel(data,quantiles): | ||
x = sorted([log(y) for y in data]) | ||
kernel = gaussian_kde(x,'silverman') | ||
|
||
# compute cdf | ||
cdf = [kernel.integrate_box_1d(-float("inf"),x[0])] | ||
for i in range(len(x)-1): | ||
cdf.append(cdf[-1]+kernel.integrate_box_1d(x[i],x[i+1])) | ||
cdf.append(cdf[-1]+kernel.integrate_box_1d(x[-1],float("inf"))) | ||
|
||
thresholds = [0]*len(quantiles) | ||
for i,q in enumerate(quantiles): | ||
cutoff_idx = __find_cutoff_idx__(cdf,q) | ||
if cutoff_idx < 0: | ||
t = exp(x[0]) - EPS | ||
else: | ||
t = exp(x[cutoff_idx]) + EPS | ||
thresholds[i] = t | ||
return thresholds | ||
|
||
|
||
def __find_cutoff_idx__(cdf,q): | ||
# normalize cdf | ||
s = cdf[-1] | ||
cdf = [y/s for y in cdf] | ||
|
||
# find the cutoff | ||
for i,c in enumerate(cdf): | ||
if c > q: | ||
return i-1 | ||
# only get here if q == 1.0 | ||
return i-1 |