forked from tknapen/hrf_mapper
-
Notifications
You must be signed in to change notification settings - Fork 1
/
exporimapper.py
182 lines (158 loc) · 7.74 KB
/
exporimapper.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
from genericpath import isfile
from exptools2.core import PylinkEyetrackerSession
import numpy as np
import h5py
import yaml
import os
import sys
import time
import pandas as pd
from psychopy.visual import GratingStim, Circle
from psychopy.data.staircase import QuestPlusHandler
from trial import InstructionTrial, \
DummyWaiterTrial, OutroTrial, \
ExpOriMapperTrial, PositioningTrial
class ExpOriMapperSession(PylinkEyetrackerSession):
def __init__(self, sub, run_id, ses, task, output_str, settings_file, eyetracker_on):
super().__init__(output_str=output_str, output_dir=None, settings_file=settings_file, eyetracker_on=eyetracker_on)
self.sub = sub
self.run_id = run_id
self.ses = ses
self.task = task
self.create_stimuli()
self.create_trials()
self.create_staircase()
def create_staircase(self):
""" Creates a staircase for the session """
quest_plus_s = self.settings['questplus']
self.staircase = QuestPlusHandler(
nTrials=self.n_trials+25, **quest_plus_s)
def update_stimulus_position(self):
""" Updates the stimulus position """
self.grating.pos = (
self.stim_position_info['x_offset'], self.stim_position_info['y_offset'])
self.grating.size = (
self.stim_position_info['width'], self.stim_position_info['height'])
for dot in [self.center_fixation_dot, self.surround_fixation_dot]:
dot.pos = (
self.stim_position_info['x_offset'], self.stim_position_info['y_offset'])
def save_stimulus_position_settings(self):
""" Saves the stimulus position settings to a file """
with open(self.stim_position_settings_file, 'w') as f:
yaml.dump(self.stim_position_info, f)
def create_stimuli(self):
""" Creates stimuli for the session """
exp_s = self.settings['experiment']
self.center_fixation_dot = Circle(
self.win, radius=exp_s['fixation_center_size'], edges=200, color='black')
self.surround_fixation_dot = GratingStim(
self.win,
size=exp_s['fixation_surround_size'],
contrast=0,
mask='raisedCos',
maskParams={'fringeWidth': exp_s['fixation_surround_fw']})
self.grating = GratingStim(win=self.win,
tex='sin',
size=exp_s['grating_size'],
sf=exp_s['grating_sf'],
contrast=exp_s['grating_contrast'],
ori=0,
phase=0,
mask='raisedCos',
maskParams={'fringeWidth': exp_s['grating_fringewidth']},
texRes=1024)
def create_trial(self, trial_nr):
pass
def create_trials(self):
""" Creates trials before running the session"""
exp_s = self.settings['experiment']
instruction_trial = InstructionTrial(session=self,
trial_nr=0,
phase_durations=[np.inf],
txt=exp_s['instruction_text'],
keys=['space'],
draw_each_frame=False)
dummy_trial = DummyWaiterTrial(session=self,
trial_nr=1,
phase_durations=[
np.inf, exp_s['start_end_period']],
txt=exp_s['pretrigger_text'],
draw_each_frame=False)
# paths
default_settings_path = os.path.join(os.path.dirname(__file__),
'defaults.yml')
tsv_path = os.path.join(os.path.dirname(__file__),
f'exp_designs/run_designs/sub-{str(self.sub).zfill(2)}/sub-{str(self.sub).zfill(2)}_task-{str(self.task)}_run-{str(self.run_id).zfill(2)}.tsv')
with open(default_settings_path, 'r', encoding='utf8') as f_in:
self.default_settings = yaml.safe_load(f_in)
self.trial_df = pd.read_csv(
tsv_path, sep='\t', index_col=0, na_values='NA')
self.n_trials = len(self.trial_df)
# read in or set up stimulus positioning
self.stim_position_settings_file = f'data/sub-{str(self.sub).zfill(2)}_ses-{str(self.ses).zfill(2)}.yml'
if os.path.isfile(self.stim_position_settings_file):
with open(self.stim_position_settings_file, 'r', encoding='utf8') as f_in:
self.stim_position_info = yaml.safe_load(f_in)
self.trials = [instruction_trial, dummy_trial]
else:
self.stim_position_info = self.default_settings['stim_position_info']
if self.stim_position_info['repositioning_required']:
position_trial = PositioningTrial(session=self)
self.trials = [position_trial, instruction_trial, dummy_trial]
else:
self.trials = [instruction_trial, dummy_trial]
stim_pres_duration = 2 * \
exp_s['stim_duration']+exp_s['interstim_interval']
# remainder makes sure we flip to the next trial in time for the next trial
# stim presentation in the scanner should be triggered by the scanner
remainder_trial_duration = -0.1 + \
exp_s['total_trial_duration'] - \
(stim_pres_duration + exp_s['warn_duration'])
trial_counter = len(self.trials)
for i in range(self.n_trials):
# add task settings to parameters of the trial
parameters = self.trial_df.iloc[i].to_dict()
parameters.update(self.default_settings['experiment'])
parameters.update(self.default_settings['stim_position_info'])
# add task placeholders to parameters of the trial
parameters.update({'response_value': np.nan,
'response_key': np.nan,
'response_sign': np.nan,
'response_time': np.nan,
'response_correct': np.nan,
'button_pressed': np.nan,
'stim_value_p1': np.nan,
'stim_value_p2': np.nan,
'correct_response_sign': np.random.choice([-1, 1])})
self.trials.append(ExpOriMapperTrial(
session=self,
trial_nr=i,
phase_durations=[
1.0,
exp_s['warn_duration'],
stim_pres_duration,
remainder_trial_duration
],
phase_names=['fix', 'warning', 'stim', 'response'],
parameters=parameters,
timing='seconds',
load_next_during_phase=None,
verbose=True,
condition=self.task)
)
trial_counter += 1
outro_trial = OutroTrial(session=self,
trial_nr=trial_counter,
phase_durations=[
exp_s['start_end_period']],
txt='',
draw_each_frame=False)
self.trials.append(outro_trial)
def run(self):
""" Loops over trials and runs them! """
self.start_experiment()
print('running eomapper experiment')
for trial in self.trials:
trial.parameters['staircase_value'] = self.staircase.next()
trial.run()
self.close()