-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01-BEH_figures.py
342 lines (328 loc) · 17 KB
/
01-BEH_figures.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import os
import pandas as pd
import numpy as np
from pathlib import Path
import matplotlib.pyplot as plt
import seaborn as sns
from helper_function.helper_plotter import soa_boxplot
import environment_variables as ev
from helper_function.helper_general import load_beh_data
SMALL_SIZE = 14
MEDIUM_SIZE = 16
BIGGER_SIZE = 18
dpi = 300
plt.rcParams['svg.fonttype'] = 'none'
plt.rc('font', size=SMALL_SIZE) # controls default text sizes
plt.rc('axes', titlesize=SMALL_SIZE) # fontsize of the axes title
plt.rc('axes', labelsize=MEDIUM_SIZE) # fontsize of the x and y labels
plt.rc('xtick', labelsize=SMALL_SIZE) # fontsize of the tick labels
plt.rc('ytick', labelsize=SMALL_SIZE) # fontsize of the tick labels
plt.rc('legend', fontsize=SMALL_SIZE) # legend fontsize
plt.rc('figure', titlesize=BIGGER_SIZE) # fontsize of the figure title
# ======================================================================================================================
# Experiment 1:
ses = "1"
task = "prp"
file_name = "sub-{}_ses-{}_run-all_task-{}_events.csv"
save_root = Path(ev.bids_root, "derivatives", "figures")
if not os.path.isdir(save_root):
os.makedirs(save_root)
# Load the data without trial exclusion:
data_df_raw = load_beh_data(ev.bids_root, ev.subjects_lists_beh[task], file_name, session=ses, task=task,
do_trial_exclusion=False)
# Load the data:
data_df = load_beh_data(ev.bids_root, ev.subjects_lists_beh[task], file_name, session=ses, task=task,
do_trial_exclusion=True)
# ==================================
# Figure 2:
# Figure 2a:
colors_onset = [ev.colors["soa_onset_locked"][str(soa)] for soa in np.sort(data_df["SOA"].unique())]
colors_offset = [ev.colors["soa_offset_locked"][str(soa)] for soa in np.sort(data_df["SOA"].unique())]
# Target:
d = 1.5
# RT vis:
fig_ta, ax_ta = soa_boxplot(data_df[data_df["task_relevance"] == 'target'],
"RT_vis",
colors_onset_locked=[[0.5, 0.5, 0.5]] * 4, colors_offset_locked=[[0.5, 0.5, 0.5]] * 4,
fig_size=[8.3 / 2, 11.7 / 2], avg_line_color=[0.5, 0.5, 0.5], zorder=0,
alpha=0.5, label="RT visual", jitter=-0.04, linestyle="solid")
# RT audio
soa_boxplot(data_df[data_df["task_relevance"] == 'target'],
"RT_aud", ax=ax_ta,
colors_onset_locked=colors_onset, colors_offset_locked=colors_offset,
fig_size=[8.3 / 2, 11.7 / 2], zorder=10000, label="RT audio", jitter=0.04, linestyle="solid")
for ax in ax_ta:
ax.axhline(0.64, linestyle="--", color=[0.7, 0.7, 0.7], alpha=0.8)
# Task relevant:
fig_tr, ax_tr = soa_boxplot(data_df[data_df["task_relevance"] == 'non-target'],
"RT_aud",
colors_onset_locked=colors_onset, colors_offset_locked=colors_offset,
fig_size=[8.3 / 2, 11.7 / 2], label="RT audio", linestyle="solid")
# Task irrelevant:
fig_ti, ax_ti = soa_boxplot(data_df[data_df["task_relevance"] == 'irrelevant'],
"RT_aud",
colors_onset_locked=colors_onset, colors_offset_locked=colors_offset,
fig_size=[8.3 / 2, 11.7 / 2], label="RT audio", linestyle="solid")
# Set the y limit to be the same for both plots:
lims = [[ax_tr[0].get_ylim()[0], ax_ti[0].get_ylim()[0]], [ax_tr[0].get_ylim()[1], ax_ti[0].get_ylim()[1]]]
max_lims = [min(min(lims)), max(max(lims))]
ax_tr[0].set_ylim(max_lims)
ax_ti[0].set_ylim(max_lims)
# Axes decoration:
fig_ta.suptitle("Target")
fig_tr.suptitle("Relevant non-target")
fig_ti.suptitle("Irrelevant non-target")
fig_ta.text(0.5, 0, 'Time (s)', ha='center', va='center')
fig_tr.text(0.5, 0, 'Time (s)', ha='center', va='center')
fig_ti.text(0.5, 0, 'Time (s)', ha='center', va='center')
fig_ta.text(0, 0.9, 'RT (s)', ha='center', va='center', fontsize=18)
fig_tr.text(0, 0.9, 'RT (s)', ha='center', va='center', fontsize=18)
fig_ti.text(0, 0.9, 'RT (s)', ha='center', va='center', fontsize=18)
# Save the figures:
fig_ta.savefig(Path(save_root, "Experiment1-figure2a_target.svg"), transparent=True, dpi=dpi)
fig_ta.savefig(Path(save_root, "Experiment1-figure2a_target.png"), transparent=True, dpi=dpi)
fig_tr.savefig(Path(save_root, "Experiment1-figure2a_tr.svg"), transparent=True, dpi=dpi)
fig_tr.savefig(Path(save_root, "Experiment1-figure2a_tr.png"), transparent=True, dpi=dpi)
fig_ti.savefig(Path(save_root, "Experiment1-figure2a_ti.svg"), transparent=True, dpi=dpi)
fig_ti.savefig(Path(save_root, "Experiment1-figure2a_ti.png"), transparent=True, dpi=dpi)
plt.close(fig_ta)
plt.close(fig_tr)
plt.close(fig_ti)
# =======================================================================================
# Figure 2b:
figsize = [8.3, 8.3 * (2 / 3)]
tasks = ["target", "non-target", "irrelevant"]
soas = list(np.sort(list(data_df["SOA"].unique())))
durations = list(np.sort(list(data_df["duration"].unique())))
fig, ax = plt.subplots(nrows=4, ncols=3, figsize=figsize, sharex=True, sharey=True)
xlims = []
# ===============================================
# Onset locked:
# Plotting the onset locked separately for each task relevances:
for i, tsk in enumerate(tasks):
for soa in soas:
data = data_df[(data_df["task_relevance"] == tsk)
& (data_df["SOA_lock"] == 'onset')
& (data_df["SOA"] == soa)]["RT_aud"].to_numpy()
# Remove the nans:
data = data[~np.isnan(data)]
# Compute the 1 and 99 percentile of the distribution to clip the data
xlims.append(np.percentile(data, [2.5, 97.5]))
ax[0, i].ecdf(data, label=str(int(soa * 1000)) + " ms", color=ev.colors["soa_onset_locked"][str(soa)])
# ===============================================
# Offset locked:
# For the offset locked, plot separately per task demands and duration:
for dur_i, dur in enumerate(durations):
for tsk_i, tsk in enumerate(tasks):
for soa in soas:
data = data_df[(data_df["task_relevance"] == tsk)
& (data_df["SOA_lock"] == 'offset')
& (data_df["SOA"] == soa)
& (data_df["duration"] == dur)]["RT_aud"].to_numpy()
# Remove the nans:
data = data[~np.isnan(data)]
xlims.append(np.percentile(data, [2.5, 97.5]))
ax[dur_i + 1, tsk_i].ecdf(data, label=str(int(soa * 1000)) + " ms",
color=ev.colors["soa_offset_locked"][str(soa)])
# Add axis decorations:
fig.supxlabel("RT (s)")
fig.supylabel("CDF")
ax[0, 0].set_xlim([np.min(xlims), np.max(xlims)])
fig.savefig(Path(save_root, "Experiment1-figure2b.svg"), transparent=True, dpi=dpi)
fig.savefig(Path(save_root, "Experiment1-figure2b.png"), transparent=True, dpi=dpi)
plt.close(fig)
# ======================================================================================================================
# Experiment 2:
sessions = ["2", "3"]
task = "introspection"
file_name = "sub-{}_ses-{}_run-all_task-{}_events.csv"
save_root = Path(ev.bids_root, "derivatives", "figures")
if not os.path.isdir(save_root):
os.makedirs(save_root)
data_df_raw = []
data_df = []
for ses in sessions:
# Load the data without trial exclusion:
data_df_raw.append(load_beh_data(ev.bids_root, ev.subjects_lists_beh[task], file_name, session=ses, task=task,
do_trial_exclusion=False))
# Load the data:
data_df.append(load_beh_data(ev.bids_root, ev.subjects_lists_beh[task], file_name, session=ses[0], task=task,
do_trial_exclusion=True))
data_df_raw = pd.concat(data_df_raw).reset_index(drop=True)
data_df = pd.concat(data_df).reset_index(drop=True)
# Convert iRT to sec:
data_df["iRT_aud"] = data_df["iRT_aud"] * 0.001
data_df["iRT_vis"] = data_df["iRT_vis"] * 0.001
# ==================================
# Figure 2:
# Figure 2a:
onset_lock_rt = [ev.colors["soa_onset_locked"][str(soa)] for soa in list(np.sort(data_df["SOA"].unique()))]
offset_lock_rt = [ev.colors["soa_offset_locked"][str(soa)] for soa in list(np.sort(data_df["SOA"].unique()))]
onset_lock_it = [ev.colors["soa_onset_locked_iRT"][str(soa)] for soa in list(np.sort(data_df["SOA"].unique()))]
offset_lock_it = [ev.colors["soa_offset_locked_iRT"][str(soa)] for soa in list(np.sort(data_df["SOA"].unique()))]
onset_lock_visit = [ev.colors["soa_onset_locked_visiRT"][str(soa)] for soa in list(np.sort(data_df["SOA"].unique()))]
offset_lock_visit = [ev.colors["soa_offset_locked_visiRT"][str(soa)] for soa in list(np.sort(data_df["SOA"].unique()))]
# Target:
d = 1.5
# RT vis:
fig_ta, ax_ta = soa_boxplot(data_df[data_df["task_relevance"] == 'target'],
"RT_vis",
colors_onset_locked=[[0.5, 0.5, 0.5]] * 4, colors_offset_locked=[[0.5, 0.5, 0.5]] * 4,
fig_size=[8.3 / 2, 11.7 / 2], avg_line_color=[0.5, 0.5, 0.5], zorder=0,
alpha=0.4, label="RT visual", jitter=-0.04, linestyle="solid")
# RT audio
soa_boxplot(data_df[data_df["task_relevance"] == 'target'],
"RT_aud", ax=ax_ta,
colors_onset_locked=onset_lock_rt, colors_offset_locked=offset_lock_rt,
fig_size=[8.3 / 2, 11.7 / 2], zorder=10000, label="RT audio", jitter=0.04, linestyle="solid")
# iRT audio
soa_boxplot(data_df[data_df["task_relevance"] == 'target'],
"iRT_aud", ax=ax_ta,
colors_onset_locked=onset_lock_rt, colors_offset_locked=offset_lock_rt,
fig_size=[8.3 / 2, 11.7 / 2], zorder=10000, label="iT audio", jitter=-0.04, linestyle="dashed",
marker='D')
# iRT vis
soa_boxplot(data_df[data_df["task_relevance"] == 'target'],
"iRT_vis", ax=ax_ta,
colors_onset_locked=[[0.5, 0.5, 0.5]] * 4, colors_offset_locked=[[0.5, 0.5, 0.5]] * 4, alpha=0.4,
fig_size=[8.3 / 2, 11.7 / 2], zorder=10000, label="iT visual", jitter=0.04, linestyle="dashed",
marker='D')
for ax in ax_ta:
ax.axhline(0.64, linestyle="--", color=[0.7, 0.7, 0.7], alpha=0.8)
# Task relevant:
fig_tr, ax_tr = soa_boxplot(data_df[data_df["task_relevance"] == 'non-target'],
"RT_aud",
colors_onset_locked=onset_lock_rt, colors_offset_locked=offset_lock_rt,
fig_size=[8.3 / 2, 11.7 / 2], label="RT audio", linestyle="solid")
soa_boxplot(data_df[data_df["task_relevance"] == 'non-target'],
"iRT_aud", ax=ax_tr,
colors_onset_locked=onset_lock_rt, colors_offset_locked=offset_lock_rt,
fig_size=[8.3 / 2, 11.7 / 2], label="iT audio", linestyle="dashed",
marker='D')
soa_boxplot(data_df[data_df["task_relevance"] == 'non-target'],
"iRT_vis", ax=ax_tr,
colors_onset_locked=[[0.5, 0.5, 0.5]] * 4, colors_offset_locked=[[0.5, 0.5, 0.5]] * 4,
fig_size=[8.3 / 2, 11.7 / 2], alpha=0.4, label="iT visual", linestyle="dashed",
marker='D')
# Task irrelevant:
fig_ti, ax_ti = soa_boxplot(data_df[data_df["task_relevance"] == 'irrelevant'],
"RT_aud",
colors_onset_locked=colors_onset, colors_offset_locked=colors_offset,
fig_size=[8.3 / 2, 11.7 / 2], label="RT audio", linestyle="solid")
soa_boxplot(data_df[data_df["task_relevance"] == 'irrelevant'],
"iRT_aud", ax=ax_ti,
colors_onset_locked=onset_lock_rt, colors_offset_locked=offset_lock_rt,
fig_size=[8.3 / 2, 11.7 / 2], label="iT audio", linestyle="dashed",
marker='D')
soa_boxplot(data_df[data_df["task_relevance"] == 'irrelevant'],
"iRT_vis", ax=ax_ti,
colors_onset_locked=[[0.5, 0.5, 0.5]] * 4, colors_offset_locked=[[0.5, 0.5, 0.5]] * 4,
fig_size=[8.3 / 2, 11.7 / 2], label="iT visual", linestyle="dashed",
marker='D', alpha=0.4)
# Set the y limit to be the same for both plots:
lims = [[ax_tr[0].get_ylim()[0], ax_ti[0].get_ylim()[0]], [ax_tr[0].get_ylim()[1], ax_ti[0].get_ylim()[1]]]
max_lims = [min(min(lims)), max(max(lims))]
ax_tr[0].set_ylim(max_lims)
ax_ti[0].set_ylim(max_lims)
# Axes decoration:
fig_ta.suptitle("Target")
fig_tr.suptitle("Relevant non-target")
fig_ti.suptitle("Irrelevant non-target")
fig_ta.text(0.5, 0, 'Time (s)', ha='center', va='center')
fig_tr.text(0.5, 0, 'Time (s)', ha='center', va='center')
fig_ti.text(0.5, 0, 'Time (s)', ha='center', va='center')
fig_ta.text(0, 0.9, 'RT (s)', ha='center', va='center', fontsize=18)
fig_tr.text(0, 0.9, 'RT (s)', ha='center', va='center', fontsize=18)
fig_ti.text(0, 0.9, 'RT (s)', ha='center', va='center', fontsize=18)
# Save the figures:
fig_ta.savefig(Path(save_root, "Experiment2-figure2a_target.svg"), transparent=True, dpi=dpi)
fig_ta.savefig(Path(save_root, "Experiment2-figure2a_target.png"), transparent=True, dpi=dpi)
fig_tr.savefig(Path(save_root, "Experiment2-figure2a_tr.svg"), transparent=True, dpi=dpi)
fig_tr.savefig(Path(save_root, "Experiment2-figure2a_tr.png"), transparent=True, dpi=dpi)
fig_ti.savefig(Path(save_root, "Experiment2-figure2a_ti.svg"), transparent=True, dpi=dpi)
fig_ti.savefig(Path(save_root, "Experiment2-figure2a_ti.png"), transparent=True, dpi=dpi)
plt.close(fig_ta)
plt.close(fig_tr)
plt.close(fig_ti)
# =======================================================================================
# Figure 2b:
figsize = [8.3, 8.3 * (2 / 3)]
tasks = ["target", "non-target", "irrelevant"]
soas = list(np.sort(list(data_df["SOA"].unique())))
durations = list(np.sort(list(data_df["duration"].unique())))
fig, ax = plt.subplots(nrows=4, ncols=3, figsize=figsize, sharex=True, sharey=True)
xlims = []
# ===============================================
# Onset locked:
# Plotting the onset locked separately for each task relevances:
for i, tsk in enumerate(tasks):
for soa in soas:
if i == 1:
label_rt = "RT " + str(int(soa * 1000)) + " ms"
label_irt = None
elif i == 2:
label_rt = None
label_irt = "iT " + str(int(soa * 1000)) + " ms"
else:
label_rt = None
label_irt = None
# Objective RT:
data_rt = data_df[(data_df["task_relevance"] == tsk)
& (data_df["SOA_lock"] == 'onset')
& (data_df["SOA"] == soa)]["RT_aud"].to_numpy()
# Remove the nans:
data_rt = data_rt[~np.isnan(data_rt)]
# Compute the 1 and 99 percentile of the distribution to clip the data
xlims.append(np.percentile(data_rt, [2.5, 97.5]))
ax[0, i].ecdf(data_rt, label=label_rt, color=ev.colors["soa_onset_locked"][str(soa)])
# Introspective RT:
data_irt = data_df[(data_df["task_relevance"] == tsk)
& (data_df["SOA_lock"] == 'onset')
& (data_df["SOA"] == soa)]["iRT_aud"].to_numpy()
# Remove the nans:
data_irt = data_irt[~np.isnan(data_irt)]
xlims.append(np.percentile(data_irt, [2.5, 97.5]))
# Compute the 1 and 99 percentile of the distribution to clip the data
ax[0, i].ecdf(data_irt, label=label_irt, color=ev.colors["soa_onset_locked"][str(soa)], linestyle="dotted")
# ===============================================
# Offset locked:
# For the offset locked, plot separately per task demands and duration:
for dur_i, dur in enumerate(durations):
for tsk_i, tsk in enumerate(tasks):
for soa in soas:
if dur_i == 2 and tsk_i == 2:
label_rt = None
label_irt = "iT " + str(int(soa * 1000)) + " ms"
elif dur_i == 2 and tsk_i == 1:
label_rt = "RT " + str(int(soa * 1000)) + " ms"
label_irt = None
else:
label_rt = None
label_irt = None
# Objective RT:
data_rt = data_df[(data_df["task_relevance"] == tsk)
& (data_df["SOA_lock"] == 'offset')
& (data_df["SOA"] == soa)
& (data_df["duration"] == dur)]["RT_aud"].to_numpy()
# Remove the nans:
data_rt = data_rt[~np.isnan(data_rt)]
xlims.append(np.percentile(data_rt, [2.5, 97.5]))
ax[dur_i + 1, tsk_i].ecdf(data_rt, label=label_rt,
color=ev.colors["soa_offset_locked"][str(soa)])
# Introspective RT:
data_irt = data_df[(data_df["task_relevance"] == tsk)
& (data_df["SOA_lock"] == 'offset')
& (data_df["SOA"] == soa)
& (data_df["duration"] == dur)]["iRT_aud"].to_numpy()
# Remove the nans:
data_irt = data_irt[~np.isnan(data_irt)]
xlims.append(np.percentile(data_irt, [2.5, 97.5]))
ax[dur_i + 1, tsk_i].ecdf(data_irt, label=label_irt,
color=ev.colors["soa_offset_locked"][str(soa)], linestyle="dotted")
# Add axis decorations:
fig.supxlabel("RT (s)")
fig.supylabel("CDF")
ax[0, 0].set_xlim([0, np.max(xlims)])
fig.savefig(Path(save_root, "Experiment2-figure2b.svg"), transparent=True, dpi=dpi)
fig.savefig(Path(save_root, "Experiment2-figure2b.png"), transparent=True, dpi=dpi)
plt.close(fig)