forked from tknapen/hrf_mapper
-
Notifications
You must be signed in to change notification settings - Fork 1
/
trial.py
337 lines (285 loc) · 14.5 KB
/
trial.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
import numpy as np
from psychopy.visual import TextStim, ShapeStim, ImageStim
from exptools2.core import Trial
class InstructionTrial(Trial):
""" Simple trial with instruction text. """
def __init__(self, session, trial_nr, phase_durations=[np.inf],
txt=None, keys=None, draw_each_frame=False, **kwargs):
super().__init__(session, trial_nr, phase_durations,
draw_each_frame=draw_each_frame, **kwargs)
txt_height = self.session.settings['various'].get('text_height')
txt_width = self.session.settings['various'].get('text_width')
text_position_x = self.session.settings['various'].get(
'text_position_x')
text_position_y = self.session.settings['various'].get(
'text_position_y')
if txt is None:
txt = '''Press any button to continue.'''
self.gammastim = ImageStim(win=self.session.win,
image='data/gamma.png',
size=self.session.settings['various'].get('gamma_stim_size'))
self.text = TextStim(self.session.win, txt,
height=txt_height,
wrapWidth=txt_width,
pos=[text_position_x, text_position_y],
font='Helvetica',
alignText='center',
anchorHoriz='center',
anchorVert='center')
self.text.setSize(txt_height)
self.keys = keys
def draw(self):
exp_s = self.session.settings['experiment']
self.gammastim.draw()
self.session.center_fixation_dot.setColor(
exp_s['fixation_center_color'])
self.session.surround_fixation_dot.draw()
self.session.center_fixation_dot.draw()
self.text.draw()
self.session.win.flip()
def get_events(self):
events = super().get_events()
if self.keys is None:
if events:
self.stop_phase()
else:
for key, t in events:
if key in self.keys:
self.stop_phase()
class DummyWaiterTrial(InstructionTrial):
""" Simple trial with text (trial x) and fixation. """
def __init__(self, session, trial_nr, phase_durations=None,
txt="Waiting for scanner triggers.", draw_each_frame=False, **kwargs):
super().__init__(session, trial_nr, phase_durations,
txt, draw_each_frame=draw_each_frame, **kwargs)
self.text.setOpacity(0.25)
def draw(self):
self.session.surround_fixation_dot.draw()
self.session.center_fixation_dot.draw()
if self.phase == 0:
self.text.draw()
self.session.win.flip()
def get_events(self):
events = super().get_events()
if events:
for key, t in events:
if key == self.session.mri_trigger:
if self.phase == 0:
self.stop_phase()
self.session.win.flip()
class OutroTrial(InstructionTrial):
""" Simple trial with only fixation cross. """
def __init__(self, session, trial_nr, phase_durations, txt='', draw_each_frame=False, **kwargs):
txt = ''''''
super().__init__(session, trial_nr, phase_durations,
txt=txt, draw_each_frame=draw_each_frame, **kwargs)
def get_events(self):
events = super().get_events()
if events:
for key, t in events:
if key == 'space':
self.stop_phase()
def draw(self):
self.session.surround_fixation_dot.draw()
self.session.center_fixation_dot.draw()
if self.phase == 0:
self.text.draw()
self.session.win.flip()
class ExpOriMapperTrial(Trial):
def __init__(self, session, trial_nr, phase_durations, phase_names,
parameters, timing, load_next_during_phase,
verbose, condition='train'):
""" Initializes a ExpOriMapperTrial object.
Parameters
----------
session : exptools Session object
A Session object (needed for metadata)
trial_nr: int
Trial nr of trial
phase_durations : array-like
List/tuple/array with phase durations
phase_names : array-like
List/tuple/array with names for phases (only for logging),
optional (if None, all are named 'stim')
parameters : dict
Dict of parameters that needs to be added to the log of this trial
timing : str
The "units" of the phase durations. Default is 'seconds', where we
assume the phase-durations are in seconds. The other option is
'frames', where the phase-"duration" refers to the number of frames.
load_next_during_phase : int (or None)
If not None, the next trial will be loaded during this phase
verbose : bool
Whether to print extra output (mostly timing info)
condition : str
Condition of the trial (either 'train' or 'test')
"""
super().__init__(session, trial_nr, phase_durations, phase_names,
parameters, timing, verbose, load_next_during_phase)
self.condition = condition
self.last_fix_time, self.last_warn_time, self.last_stim_time = 0.0, 0.0, 0.0
self.trial_answered = False
def draw(self):
exp_s = self.session.settings['experiment']
if self.phase == 1: # warn phase, change color of fixation marker
self.session.center_fixation_dot.setColor(self.parameters['color'])
self.last_warn_time = self.session.clock.getTime()
self.session.grating.phase = self.parameters['grating_phase']
self.session.grating.contrast = self.parameters['grating_contrast_multiplier'] * \
self.parameters['grating_contrast']
else:
self.session.center_fixation_dot.setColor(
exp_s['fixation_center_color'])
if self.phase == 2: # stimulus phase
self.last_stim_time = self.session.clock.getTime()
draw_grating = False
if (self.last_stim_time - self.last_warn_time) < exp_s['stim_duration']:
draw_grating = True
self.parameters['stim_value_p1'] = self.parameters['correct_response_sign'] * \
self.parameters['staircase_value'] / 2
self.session.grating.ori = self.parameters['rounded_orientation_degrees'] + \
self.parameters['stim_value_p1']
elif (self.last_stim_time - self.last_warn_time) < (exp_s['stim_duration'] + exp_s['interstim_interval']):
draw_grating = False
elif (self.last_stim_time - self.last_warn_time) < (2*exp_s['stim_duration'] + exp_s['interstim_interval']):
draw_grating = True
self.parameters['stim_value_p2'] = -self.parameters['correct_response_sign'] * \
self.parameters['staircase_value'] / 2
self.session.grating.ori = self.parameters['rounded_orientation_degrees'] + \
self.parameters['stim_value_p2']
if draw_grating:
self.session.grating.draw()
self.session.surround_fixation_dot.draw()
self.session.center_fixation_dot.draw()
def get_events(self):
exp_s = self.session.settings['experiment']
events = super().get_events()
if events:
for key, t in events:
if key == 't':
if self.phase == 0:
self.stop_phase()
self.session.win.flip()
if self.phase == 3:
if not self.trial_answered:
if key in exp_s['cw_buttons']:
self.parameters['response_key'] = key
self.parameters['response_value'] = exp_s['cw_buttons'].index(
key)
self.parameters['response_sign'] = 1
self.parameters['response_time'] = t - self.last_warn_time
if self.parameters['correct_response_sign'] == 1:
self.parameters['response_correct'] = 1
else:
self.parameters['response_correct'] = 0
self.session.staircase.addResponse(
self.parameters['response_correct'])
elif key in exp_s['ccw_buttons']:
self.parameters['response_key'] = key
self.parameters['response_value'] = exp_s['ccw_buttons'].index(
key)
self.parameters['response_sign'] = -1
self.parameters['response_time'] = t - self.last_warn_time
if self.parameters['correct_response_sign'] == -1:
self.parameters['response_correct'] = 1
else:
self.parameters['response_correct'] = 0
self.session.staircase.addResponse(
self.parameters['response_correct'])
self.log_phase_info(None)
self.trial_answered = True
class PositioningTrial(Trial):
""" Simple trial with text (trial x) and fixation. """
def __init__(self, session, trial_nr, phase_durations=(1e9,), **kwargs):
super().__init__(session, trial_nr, phase_durations, **kwargs)
self.keys = self.session.settings['position_experiment']['keys']
self.current_topic = 'x_offset'
txt_height = self.session.settings['various'].get('text_height')
txt_width = self.session.settings['various'].get('text_width')
text_position_x = self.session.settings['various'].get(
'text_position_x')
text_position_y = self.session.settings['various'].get(
'text_position_y')
self.info = TextStim(self.session.win, """""",
height=txt_height,
wrapWidth=txt_width,
pos=[text_position_x, text_position_y],
font='Helvetica',
alignText='center',
anchorHoriz='center',
anchorVert='center')
self.text.setSize(txt_height)
angles = np.linspace(0, 2*np.pi, 300)
self.shape = np.array([[np.sin(a), np.cos(a)] for a in angles])
self.pos_stim = ShapeStim(win=self.session.win,
vertices=self.shape,
size=[self.session.stim_position_info['width'],
self.session.stim_position_info['height']],
pos=[self.session.stim_position_info['x_offset'],
self.session.stim_position_info['y_offset']])
def draw(self):
""" Draws stimuli """
self.pos_stim.draw()
self.session.surround_fixation_dot.draw()
self.session.stim_position_info.draw()
self.info.draw()
def get_events(self):
events = super().get_events()
if events:
if 'q' in [ev[0] for ev in events]: # specific key in settings?
self.session.close()
self.session.quit()
for e in events:
if e[0] in self.keys:
ix = self.keys.index(e[0])
if ix == 0:
if self.current_topic == 'x_offset':
self.session.stim_position_info['x_offset'] -= 0.1
elif self.current_topic == 'y_offset':
self.session.stim_position_info['y_offset'] -= 0.1
elif self.current_topic == 'width':
self.session.stim_position_info['width'] -= 0.1
elif self.current_topic == 'height':
self.session.stim_position_info['height'] -= 0.1
elif ix == 1:
if self.current_topic == 'x_offset':
self.session.stim_position_info['x_offset'] += 0.1
elif self.current_topic == 'y_offset':
self.session.stim_position_info['y_offset'] += 0.1
elif self.current_topic == 'width':
self.session.stim_position_info['width'] += 0.1
elif self.current_topic == 'height':
self.session.stim_position_info['height'] += 0.1
elif ix == 2:
if self.current_topic == 'x_offset':
self.current_topic = 'y_offset'
elif self.current_topic == 'y_offset':
self.current_topic = 'width'
elif self.current_topic == 'width':
self.current_topic = 'height'
elif self.current_topic == 'height':
self.current_topic = 'x_offset'
if self.current_topic == 'x_offset':
self.info.text = 'x: {:0.2f}'.format(
self.session.stim_position_info['x_offset'])
elif self.current_topic == 'y_offset':
self.info.text = 'y: {:0.2f}'.format(
self.session.stim_position_info['y_offset'])
elif self.current_topic == 'width':
self.info.text = 'width: {:0.2f}'.format(
self.session.stim_position_info['width'])
elif self.current_topic == 'height':
self.info.text = 'height: {:0.2f}'.format(
self.session.stim_position_info['height'])
self.session.update_stim_position()
self.pos_stim.setPos(
[self.session.stim_position_info['x_offset'], self.session.stim_position_info['y_offset']])
self.pos_stim.setSize(
[self.session.stim_position_info['width'], self.session.stim_position_info['height']])
self.info.setPos([self.session.stim_position_info['x_offset'],
self.session.stim_position_info['y_offset']])
self.info.setSize([self.session.stim_position_info['width'],
self.session.stim_position_info['height']])
def stop_trial(self):
super().stop_trial()
self.session.save_stimulus_position_settings()