- Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBehavioralBrain.py
93 lines (65 loc) · 3.24 KB
/
BehavioralBrain.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
importnumpy
importpandasaspd
importmatplotlib
matplotlib.use('Agg')
importmatplotlib.pyplotasplt
importseabornassns
importscipy
fromanalysisimportBehavioralSubjective, BrainActivationAnalysis
fromconfigimportROOT_DIR
plt.rcParams.update({'font.size': 26})
graph_label=dict(color='#202020', alpha=0.9)
defplot_correlation(df, computeResponseTime, ba, activation=True):
ifcomputeResponseTime:
variable='ResponseTime'
else:
variable='Correct'
ax1=df.plot(kind='scatter', x=variable, y=ba, s=50, figsize=(7, 4))
z=numpy.polyfit(df[variable], df[ba], 1)
p=numpy.poly1d(z)
plt.plot(df[variable], p(df[variable]), linewidth=1)
ifcomputeResponseTime:
plt.xlabel("Response Time (in sec.)")
else:
plt.xlabel("Correctness (in %)")
ifactivation:
plt.ylabel("Activation in %\n"+ba)
else:
plt.ylabel("Deactivation in %\n"+ba)
corr=df[ba].corr(df[variable], method='kendall')
print('Kendall corr:', corr)
slope, intercept, r_value, p_value, std_err=scipy.stats.linregress(df[ba], df[variable])
print('r squared:', r_value**2)
left, right=plt.xlim()
bottom, top=plt.ylim()
ax1.text(left+((right-left)/40), bottom+ ((top-bottom) /7), 'Kendall τ: '+format(corr, '.2f'), fontdict=graph_label)
ax1.text(left+((right-left)/40), bottom+ ((top-bottom) /40), 'r squared: '+format(r_value**2, '.2f'), fontdict=graph_label)
sns.despine()
plt.tight_layout()
ifactivation:
pre='Activation_'
else:
pre='Deactivation_'
plt.savefig(ROOT_DIR+'/analysis/output/'+pre+ba+'_'+variable+'.pdf', dpi=300, bbox_inches='tight', pad_inches=0)
defcompute_behavioral_brain(df_ba_cond, behavioral_data, activation):
behavioral_ba=pd.merge(df_ba_cond, behavioral_data, how='left', left_on=['participant', 'Snippet'], right_on=['Participant', 'Snippet'])
# check whether there is response times < 5s and exclude them since they are accidental clicks
behavioral_ba["ResponseTime"].fillna(60000, inplace=True)
behavioral_ba.loc[behavioral_ba['ResponseTime'] <5000, 'ResponseTime'] =numpy.nan
behavioral_ba=behavioral_ba.dropna(subset=['ResponseTime'])
behavioral_ba["ResponseTime"] =behavioral_ba['ResponseTime'].apply(BehavioralSubjective.convert_to_second)
# compute correct correctness
behavioral_ba=behavioral_ba.groupby('Snippet').mean()
behavioral_ba["Correct"] =behavioral_ba['Correct'].apply(BehavioralSubjective.convert_to_percent)
forbainBrainActivationAnalysis.get_bas(activation):
plot_correlation(behavioral_ba, True, ba, activation)
plot_correlation(behavioral_ba, False, ba, activation)
defmain():
df_ba_part_cond_act=pd.read_csv(ROOT_DIR+'/data/fMRI/fMRI_Analyzed_BA_Snippet_Participant_Activation.csv')
df_ba_part_cond_deact=pd.read_csv(ROOT_DIR+'/data/fMRI/fMRI_Analyzed_BA_Snippet_Participant_Deactivation.csv')
behavioral_data=BehavioralSubjective.load_behavioral_data()
compute_behavioral_brain(df_ba_part_cond_act, behavioral_data, True)
compute_behavioral_brain(df_ba_part_cond_deact, behavioral_data, False)
print('\n##### \n-> all done \o/')
if__name__=="__main__":
main()