Skip to content

Commit 24fda2d

Browse files
author
Nick Harding
authored
Merge pull request #6 from hardingnj/implement_cache
added cacheing
2 parents 6dd0b6d + 1ee9d49 commit 24fda2d

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

xpclr/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
__version__ = "0.10.1"
1+
__version__ = "0.11.0"
22

33
from .xpclr import *

xpclr/xpclr.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import allel
1717
from math import sqrt, pi
1818
import warnings
19+
from functools import lru_cache
1920

2021

2122
## FUNCTIONS ###################################################################
@@ -32,7 +33,18 @@ def estimate_omega(q1, q2):
3233
return w
3334

3435

35-
def determine_c(r, s, effective_pop_size=20000, min_rd=1e-7):
36+
def determine_var(w, q2):
37+
38+
"""
39+
:param w: omega as estimated
40+
:param q2: allele freq of SNP in p2
41+
:return: sigma2, estimate of variation
42+
"""
43+
44+
return w * (q2 * (1 - q2))
45+
46+
47+
def determine_c(r, s, effective_pop_size=20000, min_rd=1e-7, sf=5):
3648
"""
3749
:param effective_pop_size: Ne of population
3850
:param r: recombination fraction
@@ -43,7 +55,8 @@ def determine_c(r, s, effective_pop_size=20000, min_rd=1e-7):
4355
if s <= 0:
4456
return 1.0
4557
else:
46-
return 1 - np.exp(-np.log(2*effective_pop_size)*max(r, min_rd)/s)
58+
c = 1 - np.exp(-np.log(2*effective_pop_size)*max(r, min_rd)/s)
59+
return np.round(c, sf)
4760

4861

4962
# This is the function that needs to be called alot and could be cythonized
@@ -107,6 +120,7 @@ def pdf_integral(p1, data):
107120
# additionally they neglect the probability of p1 being 0 or 1, I presume to
108121
# allow the romberg integration to converge ok.
109122
# This calculates the likelihood of a given SNP
123+
@lru_cache(maxsize=2**16)
110124
def chen_likelihood(values):
111125

112126
"""
@@ -191,10 +205,10 @@ def calculate_cl_romberg(sc, indata):
191205

192206
xj, nj, rd, p2freq, omega, weight = indata[j]
193207
var = determine_var(w=omega, q2=p2freq)
194-
c = determine_c(rd, sc)
208+
c = determine_c(rd, sc, sf=5)
195209

196210
# allow this function to change
197-
cl = calculate_likelihood(values=np.array([xj, nj, c, p2freq, var]))
211+
cl = calculate_likelihood(values=(xj, nj, c, p2freq, var))
198212

199213
marginall[j] = weight * cl
200214

@@ -205,17 +219,6 @@ def calculate_cl_romberg(sc, indata):
205219
return -ml
206220

207221

208-
def determine_var(w, q2):
209-
210-
"""
211-
:param w: omega as estimated
212-
:param q2: allele freq of SNP in p2
213-
:return: sigma2, estimate of variation
214-
"""
215-
216-
return w * (q2 * (1 - q2))
217-
218-
219222
def compute_xpclr(dat):
220223

221224
# values of dist cannot be 0. Equiv to ~10 bp

0 commit comments

Comments
 (0)