In [2]:
import sys
In [3]:
x = 36
In [4]:
pts_l = [x+10, x+20, x+30]
pts_l
Out[4]:
In [5]:
?eval
In [6]:
import sys
sys.path.insert(0, '/home/claudius/Downloads/dadi')
import dadi
In [7]:
dadi_model = 'dadi.Demographics1D.bottlegrowth'
In [8]:
f = eval(dadi_model)
In [9]:
%pdoc f
In [17]:
p_init = [0] * len(pts_l)
p_init
Out[17]:
In [11]:
for i in range(len(pts_l)):
p_init[i] = range(pts_l[i], pts_l[i]+10)
In [12]:
p_init
Out[12]:
In [13]:
from itertools import product
In [14]:
?product
In [15]:
for i, comb in enumerate(product(*p_init)):
print comb
if i > 20: break
In [21]:
print "these are the starting values \'{}\'".format(p_init)
In [24]:
% ll dadiExercises/
In [26]:
path_to_spectrum_file = "dadiExercises/ERY.FOLDED.sfs.dadi_format"
dadi_model = "dadi.Demographics1D.growth"
upper = [1e3, 4]
lower = [1e-3, 0]
p_init = [1, 1]
dadi_opt_func = "dadi.Inference.optimize_log"
In [30]:
cmd = "./run_dadi.py -p {} -m {} -u \'{}\' -l \'{}\' -i \'{}\' -d {}".format(path_to_spectrum_file, dadi_model, upper, lower, p_init, dadi_opt_func)
cmd
Out[30]:
In [31]:
import os
In [32]:
os.system(cmd)
Out[32]:
In [1]:
import pickle, glob
In [9]:
pickled = glob.glob('OUT_run_dadi/ERY*pickle')
sorted(pickled[:10])
Out[9]:
In [10]:
opt_out = []
for filename in glob.iglob('OUT_run_dadi/ERY*pickle'):
opt_out.append( pickle.load(open(filename, "r")) )
In [4]:
def get_flag_count(out, NM=True):
"""
out: list of tuples, each containing p_init and popt + additional info, including warnflags
as produced by run_dadi.py
"""
from collections import defaultdict
if NM: # if ar from Nelder-Mead
i = 4 # the warnflag is reported at index position 4 in the output array
else: # ar from BFGS optimisation
i = 6
warnflag = defaultdict(int)
for res in out:
if res[1][i] == 1: # notice the change in indexing
warnflag[1] +=1
elif res[1][i] == 2:
warnflag[2] += 1
elif res[1][i] == 0:
warnflag[0] += 1
else:
warnflag[999] +=1
if NM:
print "success", warnflag[0]
print "Maximum number of function evaluations made.", warnflag[1]
print "Maximum number of iterations reached.", warnflag[2]
print "unknown flag", warnflag[999]
else:
print "success", warnflag[0]
print "Maximum number of iterations exceeded.", warnflag[1]
print "Gradient and/or function calls not changing.", warnflag[2]
print "unknown flag", warnflag[999]
In [11]:
get_flag_count(opt_out, NM=True)
In [ ]: