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