def _sensitivity(exposure, workspace, protocol, statistic, par=None, seed=None): """ Sensitivity analysis. >>> _sensitivity("11df840d0150d34c9716cd4cbdd164c8", "bondarenko_szigeti_bett_kim_rasmusson_2004_apical", "protocol", "apd90", ("g_Na", "Nao"), seed=1) TODO: Make optional arguments of exposure, lower, upper, etc. TODO: Accept json dict of model_kwargs, morris_kwargs """ m = Model(exposure + "/" + workspace, maxsteps=1e6, chunksize=1e5, reltol=1e-8) phenotypes(m, m.name) # initialize and cache default par = [str(i) for i in par] # Numpy cannot handle Unicode names if not par: # Default: sample within plus/minus 50% of all nonzero parameters par = [k for k in m.dtype.p.names if m.pr[k] != 0] baseline = unstruct(m.pr[par]).squeeze() lower = 0.5 * baseline upper = 1.5 * baseline if seed is not None: r.set_seed(seed) r("fun <- function(func, x) func(x)") raise Exception(str(r.fun(r.sum, baseline))) fun = scalar_pheno(m, statistic) r.as_vector(baseline) return r.morris(fun, factors=par, r=2, design={"type": "oat", "levels": 10, "grid.jump": 5}, binf=lower, bsup=upper)
"""Make a function to return a named field of the phenotype array.""" @ri.rternalize def fun(rmatrix): """Scalar function for use with R's sensitivity::morris().""" ph = np.concatenate([phenotypes(i) for i in mat2par(rmatrix)]) return py2ri(ph[field]) return fun if __name__ == "__main__": # Sensitivity analysis baseline = unstruct(m.pr[factors]) lower = 0.5 * baseline upper = 1.5 * baseline result = dict() for field in "appeak", "apd90", "ctpeak", "ctbase", "ctd90": r.set_seed(20120221) # repeatable random sampling result[field] = r.morris(scalar_pheno(field), factors=factors, r=2, design={"type": "oat", "levels": 10, "grid.jump": 5}, binf=lower, bsup=upper) # Print and visualize results r.png("sensitivity.png", width=1024, height=768, pointsize=24) r.par(mfrow=(2, 3)) # multiple figures in two rows, three columns for k, v in result.items(): print "====================================================" print k print v r.plot(v, log="y", main=k) r.dev_off() # close image file
ph = np.concatenate([phenotypes(i) for i in mat2par(mat)]) result = ph["apd90"] result[np.isnan(result)] = 0 return py2ri(result) @ri.rternalize def appeak(mat): ph = np.concatenate([phenotypes(i) for i in mat2par(mat)]) result = ph["appeak"] result[np.isnan(result)] = 0 return py2ri(result) # mu mu.star sigma #Cm -0.4348831 0.6603604 0.9338907 #Vmyo 0.1264869 0.1264869 0.1657938 # mu mu.star sigma #Cm 0.6716927 0.6716927 0.3328038 #Vmyo -0.3135712 0.3135712 0.3033346 if __name__ == "__main__": binf = 0.5 * unstruct(m.pr[factors]) bsup = 1.5 * unstruct(m.pr[factors]) r.pdf("morrisplot.pdf") r.plot(r.morris(apd90, factors=factors, r=2, design={"type": "oat", "levels": 10, "grid.jump": 5}, binf=binf, bsup=bsup)) r.dev_off() # saved = np.load("/tmp/saveme.npy") # ph = [phenotypes(i) for i in saved] # fail = [np.isnan(i).all() for i in ph] # result = np.logical_not(fail).astype(float)