def run_models(mname, project, regress=False): #bayes fit all subject allsx_df = fit_sx(mname, project=project, regress=regress) #parse model output subdf = parse_allsx(allsx_df) pdict = subdf_to_pdict(subdf) #simulate and compare with observed data data = defmod.find_data(mname, project=project) simdf = vis.predict(pdict, data, ntrials=160, nsims=100, save=True, RTname="SimRT_EvT.jpeg", ACCname="SimACC_EvT.jpeg") simdf.to_csv("simdf_sxbayes.csv") #save pdict; can be reloaded and transformed back into #the original pdict format by the following commands #1. pdict=pd.read_csv("sxbayes_pdict.csv") #2. pdict=pdict.to_dict() params = pd.DataFrame(pdict) params.to_csv("sxbayes_pdict.csv", index=False)
def optimize_sx(mname, project='imaging'): m = defmod.define_model(mname, project=project) data = m.data if 'z' in m.depends_on.keys(): bias = True else: bias = False grp_dict = {} subj_params = [] aic_list = [] bic_list = [] dic_list = [] ic_dict = {} for subj_idx, subj_data in data.groupby('subj_idx'): m_subj = hddm.HDDM(subj_data, depends_on=m.depends_on, bias=bias, include=m.include) sx_params = m_subj.optimize('ML') pdict = opt.get_pdict(sx_params) subj_params.append(sx_params) aic_list.append(aic(m_subj)) bic_list.append(bic(m_subj)) #dic_list.append(m_subj.dic) grp_dict[subj_idx] = pdict ic_dict = {'aic': aic_list, 'bic': bic_list} ic_df = pd.DataFrame(ic_dict) ic_df.to_csv(mname + "_IC_Rank.csv") #write grp_dict to .txt file for reloading later f = open('mle_params.txt', 'w') f.write('grp_dict=' + repr(grp_dict) + '\n') f.close() params = pd.DataFrame(subj_params) simdf = vis.predict(grp_dict, data, ntrials=100, nsims=100, save=True, RTname="dbmz_RT.jpeg", ACCname="dbmz_ACC.jpeg") #simdf=vis.predict(grp_dict, df, ntrials=160, nsims=100, save=True, RTname="SimRT_EvT.jpeg", ACCname="SimACC_EvT.jpeg") simdf.to_csv("simdf_opt.csv") params.to_csv("subj_params_opt.csv", index=False) sdt.plot_rho_sdt(data, simdf) empvsim = sdt.rho_sdt(data, simdf) return grp_dict, ic_df
def run_models(mname, project, regress=False): #bayes fit all subject allsx_df=fit_sx(mname, project=project, regress=regress) #parse model output subdf=parse_allsx(allsx_df) pdict=subdf_to_pdict(subdf) #simulate and compare with observed data data=defmod.find_data(mname, project=project) simdf=vis.predict(pdict, data, ntrials=160, nsims=100, save=True, RTname="SimRT_EvT.jpeg", ACCname="SimACC_EvT.jpeg") simdf.to_csv("simdf_sxbayes.csv") #save pdict; can be reloaded and transformed back into #the original pdict format by the following commands #1. pdict=pd.read_csv("sxbayes_pdict.csv") #2. pdict=pdict.to_dict() params=pd.DataFrame(pdict) params.to_csv("sxbayes_pdict.csv", index=False)
def optimize_sx(mname, project='imaging'): m=defmod.define_model(mname, project=project) data=m.data if 'z' in m.depends_on.keys(): bias=True else: bias=False grp_dict={}; subj_params=[]; aic_list=[]; bic_list=[]; dic_list=[]; ic_dict={} for subj_idx, subj_data in data.groupby('subj_idx'): m_subj=hddm.HDDM(subj_data, depends_on=m.depends_on, bias=bias, include=m.include) sx_params=m_subj.optimize('ML') pdict=opt.get_pdict(sx_params) subj_params.append(sx_params) aic_list.append(aic(m_subj)); bic_list.append(bic(m_subj)); #dic_list.append(m_subj.dic) grp_dict[subj_idx]=pdict ic_dict={'aic':aic_list, 'bic':bic_list} ic_df=pd.DataFrame(ic_dict) ic_df.to_csv(mname+"_IC_Rank.csv") #write grp_dict to .txt file for reloading later f=open('mle_params.txt', 'w') f.write('grp_dict=' + repr(grp_dict) + '\n') f.close() params = pd.DataFrame(subj_params) simdf=vis.predict(grp_dict, data, ntrials=100, nsims=100, save=True, RTname="dbmz_RT.jpeg", ACCname="dbmz_ACC.jpeg") #simdf=vis.predict(grp_dict, df, ntrials=160, nsims=100, save=True, RTname="SimRT_EvT.jpeg", ACCname="SimACC_EvT.jpeg") simdf.to_csv("simdf_opt.csv") params.to_csv("subj_params_opt.csv", index=False) sdt.plot_rho_sdt(data, simdf) empvsim=sdt.rho_sdt(data, simdf) return grp_dict, ic_df
def postproc(m, mname='model', varlvl='grp', traces_info=None): if not os.path.isdir("avg_pdf"): os.makedirs("avg_pdf") if not os.path.isdir("avg_quant"): os.makedirs("avg_quant") if not os.path.isdir("posteriors"): os.makedirs("posteriors") if not os.path.isdir("correlations"): os.makedirs("correlations") if not os.path.isdir("simulations"): os.makedirs("simulations") os.chdir("posteriors") m.plot_posteriors(save=True) plt.close('all') os.chdir("../") m.print_stats(fname=mname+'_stats.txt') parsed = parse.parse_stats(minput=m, varlvl=varlvl, input_isdf=False) subdf=parsed[0] condsdf=parsed[1] pdict=parsed[2] os.chdir('simulations') #pdict=mle.optimize_sx(mname) print "simulating from mle optimized params" simdf=vis.predict(params=pdict, data=m.data, simfx=sims.sim_exp, ntrials=240, pslow=0.0, pfast=0.0, nsims=100, save=True, RTname=mname+'_RT_simexp', ACCname=mname+'_ACC_simexp') simdf.to_csv("simdf.csv", index=False) simdf=pd.read_csv("simdf.csv") vis.predict_from_simdfs(m.data, simdf) plt.close('all') os.chdir('../') simdf.to_csv("simdf_opt.csv") os.chdir('correlations') print "estimating emp-theo sdt correlation, saving..." sdt_corr=sdt.rho_sdt(m.data, simdf) print "plotting emp-theo sdt correlation, saving..." sdt.plot_evs_corr(m.data, simdf) sdt_corr.to_csv("sdt_corr.csv") plt.close('all') os.chdir('../') #mname, traces_name=defmod.find_traces_imaging(m) avgm=m.get_average_model() #avgm.load_db(dbname=traces_name, db='pickle') avgm.load_db(dbname="msmIO_traces.db", db='pickle') os.chdir("./avg_pdf") avgm.plot_posterior_predictive(save=True, value_range=np.linspace(-6, 6, 100), figsize=(12, 10)) os.chdir("../avg_quant") avgm.plot_posterior_quantiles(save=True, samples=250, figsize=(12, 10)) plt.close('all')