Example #1
0
File: vis.py Project: ctw/myhddm
def predict(params, data, simfx=sims.sim_exp, ntrials=160, pslow=0.0, pfast=0.0, nsims=100, nsims_per_sub=1, errors=False, save=False, RTname='RT_simexp', ACCname='Acc_simexp'):
	"""
	Arguments:

		params (dict):			hierarchical dictionary created with
								either parse_stats() or reformat_sims_input()

		data (pandas df):		pandas dataframe with the empirical data used
								to fit the model to generate the simulation parameters


	*Simulates subject-wise data using parameter estimates for each subj/condition
	 and calls rt and acc plotting functions to plot behavioral data against model predictions

	*If save=True, will save RT and ACC plots to working dir
	"""
	from myhddm import parse

	simdf_list=[]

	if len(data.cue.unique())==3:
		x=np.array([1,2,3])
		code_type='HNL'
	else:
		x=np.array([1, 2, 3, 4, 5])
		code_type='AllP'
		
	if errors:
		face_rt, house_rt=parse.get_emp_error_rt(data=data)
		face_acc, house_acc, cf, ch=parse.get_empirical_means(data=data, code_type=code_type)
	
	else:
		face_acc, house_acc, face_rt, house_rt=parse.get_empirical_means(data=data, code_type=code_type)
	
	face_rt_error, house_rt_error=parse.get_emp_error_rt(data=data)
	
	sem_list=parse.get_emp_SE(data, code_type)

	#init sep figure, axes for RT & ACC data
	fig_rt, ax_rt=plt.subplots(1)
	fig_acc, ax_acc=plt.subplots(1)

	fig_acc.subplots_adjust(top=0.9, left=0.15, right=0.88, bottom=0.15)
	fig_rt.subplots_adjust(top=0.9, left=0.15, right=0.88, bottom=0.15)

	flast_rt=np.zeros([5])
	hlast_rt=np.zeros([5])
	flast_acc=np.zeros([5])
	hlast_acc=np.zeros([5])

	for i in range(nsims):

		simdf, params_used=simfx(pdict=params, ntrials=ntrials, pfast=pfast, pslow=pslow, nsims_per_sub=nsims_per_sub)
		simdf['sim_n']=[i]*len(simdf.index)
		simdf_list.append(simdf)
		Ftheo_acc, Htheo_acc = parse.get_theo_acc(simdf=simdf, code_type=code_type)
		flast_acc, hlast_acc = pred_accPLOT(code_type=code_type, acc_ax=ax_acc, xacc=x, yaccFace=Ftheo_acc, yaccHouse=Htheo_acc, ind=i, flast_acc=flast_acc, hlast_acc=hlast_acc)
		
		if errors:
			Ftheo_rt, Htheo_rt=parse.get_theo_error_rt(simdf=simdf)
		else:
			Ftheo_rt, Htheo_rt=parse.get_theo_rt(simdf=simdf, code_type=code_type)
		
		flast_rt, hlast_rt = pred_rtPLOT(code_type=code_type, rt_ax=ax_rt, xrt=x, yrtFace=Ftheo_rt, yrtHouse=Htheo_rt, ind=i, flast_rt=flast_rt, hlast_rt=hlast_rt)

	simdf_concat=pd.concat(simdf_list)
	#plot empirical ACC
	#ax_acc.grid()
	#f_emp_acc=ax_acc.errorbar(x, face_acc, yerr=sem_list[0], elinewidth=3.5, ecolor='k', color='Blue', lw=6.0)
	#h_emp_acc=ax_acc.errorbar(x, house_acc, yerr=sem_list[1], elinewidth=3.5, ecolor='k', color='Green', lw=6.0)
	f_emp_acc=ax_acc.errorbar(x, face_acc, yerr=sem_list[0], elinewidth=3.5, ecolor='k', color='Blue', lw=6.0)
	h_emp_acc=ax_acc.errorbar(x, house_acc, yerr=sem_list[1], elinewidth=3.5, ecolor='k', color='Red', lw=6.0)
	#ax_acc.legend((ax_acc.lines[-4], ax_acc.lines[-1], ax_acc.lines[0], ax_acc.lines[1]), ('Face Data', 'House Data', 'Face Model', 'House Model'), loc=0, fontsize=18)

	#plot empirical RT
	#ax_rt.grid()
	#f_emp_rt=ax_rt.errorbar(x, face_rt, yerr=sem_list[2], elinewidth=3.5, ecolor='k', color='Blue', lw=6.0)
	#h_emp_rt=ax_rt.errorbar(x, house_rt, yerr=sem_list[3], elinewidth=3.5, ecolor='k', color='Green', lw=6.0)
	f_emp_rt=ax_rt.errorbar(x, face_rt, yerr=sem_list[2], elinewidth=3.5, ecolor='k', color='Blue', lw=6.0)
	h_emp_rt=ax_rt.errorbar(x, house_rt, yerr=sem_list[3], elinewidth=3.5, ecolor='k', color='Red', lw=6.0)
	#ax_rt.legend((ax_rt.lines[-4], ax_rt.lines[-1], ax_rt.lines[0], ax_rt.lines[1]), ('Face Data', 'House Data', 'Face Model', 'House Model'), loc=0, fontsize=18)

	simdf_concat.to_csv("simdf.csv")
	if save:
		fig_rt.savefig(RTname+'.jpeg', dpi=900)
		fig_acc.savefig(ACCname+'.jpeg', dpi=900)
		#fig_rt.savefig(RTname+'.png', format='png', dpi=500)
		#fig_acc.savefig(ACCname+'.png', format='png', dpi=500)
		#fig_rt.savefig(RTname+'.tif', format='tif', dpi=500)
		#fig_acc.savefig(ACCname+'.tif', fortmat='tif', dpi=500)
	return simdf_concat
Example #2
0
File: vis.py Project: ctw/myhddm
def predict_from_simdfs(data, simdfs, save=True, mname='EvT'):
	"""
	Arguments:

		data (pandas df):		pandas dataframe with the empirical data used
								to fit the model to generate the simulation parameters

		simdfs (pandas df):     pandas dataframe with multiple simulated datasets


	*plot behavioral data against model predictions from multiple simulations

	*If save=True, will save RT and ACC plots to working dir
	"""

	sns.set_style("white")

	if len(data.cue.unique())==3:
		x=np.array([1,2,3])
		code_type='HNL'
	else:
		x=np.array([1, 2, 3, 4, 5])
		code_type='AllP'

	face_acc, house_acc, face_rt, house_rt=parse.get_empirical_means(data=data, code_type=code_type)
	sem_list=parse.get_emp_SE(data, code_type)

	#init sep figure, axes for RT & ACC data
	fig_rt, ax_rt=plt.subplots(1)

	sns.despine()

	fig_acc, ax_acc=plt.subplots(1)

	sns.despine()

	fig_acc.subplots_adjust(top=0.9, left=0.15, right=0.88, bottom=0.15)
	fig_rt.subplots_adjust(top=0.9, left=0.15, right=0.88, bottom=0.15)

	flast_rt=np.zeros([5])
	hlast_rt=np.zeros([5])
	flast_acc=np.zeros([5])
	hlast_acc=np.zeros([5])

	for simn, rest in simdfs.groupby('sim_num'):

		Ftheo_acc, Htheo_acc = parse.get_theo_acc(simdf=rest, code_type=code_type)
		flast_acc, hlast_acc = pred_accPLOT(code_type=code_type, acc_ax=ax_acc, xacc=x, yaccFace=Ftheo_acc, yaccHouse=Htheo_acc, ind=simn, flast_acc=flast_acc, hlast_acc=hlast_acc, mname=mname)

		Ftheo_rt, Htheo_rt=parse.get_theo_rt(simdf=rest, code_type=code_type)
		flast_rt, hlast_rt = pred_rtPLOT(code_type=code_type, rt_ax=ax_rt, xrt=x, yrtFace=Ftheo_rt, yrtHouse=Htheo_rt, ind=simn, flast_rt=flast_rt, hlast_rt=hlast_rt, mname=mname)

	#plot empirical ACC
	f_emp_acc=ax_acc.errorbar(x, face_acc, yerr=sem_list[0], elinewidth=2.5, ecolor='k', color='Blue', lw=4.0)
	h_emp_acc=ax_acc.errorbar(x, house_acc, yerr=sem_list[1], elinewidth=2.5, ecolor='k', color='Red', lw=4.0)
	
	if mname=='pbm':
		ax_acc.legend((ax_acc.lines[-4], ax_acc.lines[-1], ax_acc.lines[0], ax_acc.lines[1]), ('Face Data', 'House Data', 'Face Model', 'House Model'), loc=0, fontsize=18)

	sns.despine()

	#plot empirical RT
	f_emp_rt=ax_rt.errorbar(x, face_rt, yerr=sem_list[2], elinewidth=2.5, ecolor='k', color='Blue', lw=4.0)
	h_emp_rt=ax_rt.errorbar(x, house_rt, yerr=sem_list[3], elinewidth=2.5, ecolor='k', color='Red', lw=4.0)
	
	if mname=='pbm':
		ax_rt.legend((ax_rt.lines[-4], ax_rt.lines[-1], ax_rt.lines[0], ax_rt.lines[1]), ('Face Data', 'House Data', 'Face Model', 'House Model'), loc=0, fontsize=18)

	sns.despine()

	flist=[fig_rt, fig_acc]

	if save:
		fig_rt.savefig(mname+'_rt.png', dpi=400)
		fig_acc.savefig(mname+'_acc.png', dpi=400)