-
Notifications
You must be signed in to change notification settings - Fork 4
/
show_soa.py
130 lines (111 loc) · 3.52 KB
/
show_soa.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
"""
take some previously save the neural activity for SOA (from run_exp_soa.py),
and plot/save the SOA figure
"""
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from stroop_stimulus import TASKS, CONDITIONS
"""helper funcs
"""
def compute_rt(act_r, act_g, threshold):
"""compute reaction time
take the activity of the decision layer...
check the earliest time point when activity > threshold...
call that RT
*RT=np.nan if timeout
"""
rt_red = compute_rt_(act_r, threshold)
rt_grn = compute_rt_(act_g, threshold)
rt = np.nanmin([rt_red, rt_grn])
return rt
def compute_rt_(act, threshold):
tps_pass_threshold = np.where(act > threshold)[0]
if len(tps_pass_threshold) > 0:
return tps_pass_threshold[0]
# return np.nan
return n_time_steps
# save the record file
log_path = 'log_temp'
img_path = 'imgs_temp'
# experiment params
SOAs = [-10, 0, 10]
n_time_steps = 120
n_repeats = 20
# load the data record
fname = f'record_t{n_time_steps}_n{n_repeats}.csv'
record = pd.read_csv(os.path.join(log_path, fname))
# compute rt and responses
threshold = .95
column_names = [
'Rep-id', 'Condition',
'Task', 'SOA', 'RT'
]
df = pd.DataFrame(columns=column_names, dtype=int)
# compute reaction time
for SOA in SOAs:
for k, task in enumerate(TASKS):
for j, condition in enumerate(CONDITIONS):
# print(SOA, task, condition)
# define the current selection operator
sel_op = (record['SOA'] == SOA) & (record['Task'] == task) & (
record['Condition'] == condition)
# the record for the current SOA / task / condition
record_stc = record[sel_op]
# loop over all repeats
n_repeats = int(np.max(record_stc['Rep-id']))
for i in range(n_repeats):
# get single trial data
record_stci = record_stc[record_stc['Rep-id'] == i]
# compute behavioral data
rt_stci = compute_rt(
record_stci['Act-Red'], record_stci['Act-Green'],
threshold
)
# shift the RT
# ... to the earliest time point when response is possible
if task == 'word reading':
soa = -SOA
if soa < 0:
rt_stci -= np.abs(soa)
elif task == 'color naming':
soa = SOA
if soa < 0:
rt_stci -= np.abs(soa)
else:
raise ValueError('Unrecognizable task')
# update the data df
df = df.append({
'Rep-id': i,
'Condition': condition,
'Task': task,
'SOA': soa,
'RT': rt_stci,
}, ignore_index=True)
# show the df
df.head()
# plot the data
sns.set(
style='white', context='talk', palette="colorblind",
rc={'legend.frameon': False},
)
f, ax = plt.subplots(1, 1, figsize=(8, 5))
sns.lineplot(
x='SOA', y='RT',
hue='Condition', style='Task', markers=True,
ci=99,
data=df,
ax=ax,
)
ax.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0)
title_text = """
Effects of varying SOA between word & color stimuli
"""
ax.set_title(title_text)
ax.set_ylabel('Reaction time')
ax.set_xlabel('Stimulus onset asynchrony (SOA)')
f.tight_layout()
sns.despine()
# f.savefig(os.path.join(img_path, 'soa.png'))