forked from EoinTravers/QuickstartMousetracking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexperiment.opensesame
312 lines (283 loc) · 8.36 KB
/
experiment.opensesame
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
# Generated by OpenSesame 2.8.3 (Gutsy Gibson)
# Mon May 11 11:45:32 2015 (nt)
# <http://www.cogsci.nl/opensesame>
set mouse_backend "legacy"
set subject_parity "even"
set height "1080"
set font_family "mono"
set font_italic "no"
set custom_cursor "no"
set synth_backend "legacy"
set title "MouseTrack True or False"
set coordinates "relative"
set start "experiment"
set sampler_backend "legacy"
set transparent_variables "no"
set foreground "black"
set font_bold "no"
set description "A simple template for the mouse-tracking paradigm"
set background "white"
set font_size "18"
set keyboard_backend "legacy"
set canvas_backend "legacy"
set compensation "0"
set bidi "no"
set subject_nr "0"
set width "1920"
define sequence experiment
run setup_script "always"
run trial_loop "always"
define logger logger
set ignore_missing "yes"
set description "Logs experimental data"
set auto_log "no"
set use_quotes "yes"
log "height"
log "subject_nr"
log "width"
log "probe"
log "condition"
log "response"
log "accuracy"
log "rt"
log "xTrajectory"
log "yTrajectory"
log "tTrajectory"
log "count_trial_sequence"
define inline_script pythonlogging_script
___run__
# Saves data in as Python variables
# Make a dict to hold data from this trial
trial_data = {}
trial_data['height'] = exp.get('height')
trial_data['subject_nr'] = exp.get('subject_nr')
trial_data['width'] = exp.get('width')
trial_data['probe'] = exp.get('probe')
trial_data['condition'] = exp.get('condition')
trial_data['response'] = exp.get('response')
trial_data['accuracy'] = exp.get('accuracy')
trial_data['rt'] = exp.get('rt')
trial_data['xTrajectory'] = exp.get('xTrajectory')
trial_data['yTrajectory'] = exp.get('yTrajectory')
trial_data['tTrajectory'] = exp.get('tTrajectory')
trial_data['count_trial_sequence'] = exp.get('count_trial_sequence')
# Add this dict to the list
exp.data.append(trial_data)
__end__
set _prepare ""
set description "Executes Python code"
define inline_script question_script
___run__
# Draw Start Button
my_canvas.clear()
my_canvas.image(start_button, True, mx, (2*my) - half_start_h)
my_canvas.image(yes_button, False, 0, 0)
my_canvas.image(no_button, False, 2*mx - response_w, 0)
my_canvas.show()
#Wait for click on start button
while 1:
button, position, timestamp = my_mouse.get_click()
x, y = position
if x > mx-half_start_w and x < mx+half_start_w and y > (2*my) - 2*half_start_h:
my_canvas.clear()
break
# Hide the mouse
my_mouse.set_visible(visible=False)
# Fixation
tick = fixation_length / 3
my_canvas.clear()
my_canvas.image(yes_button, False, 0, 0)
my_canvas.image(no_button, False, 2*mx - response_w, 0)
my_canvas.show()
exp.sleep(tick)
my_canvas.text('+')
my_canvas.image(yes_button, False, 0, 0)
my_canvas.image(no_button, False, 2*mx - response_w, 0)
my_canvas.show()
exp.sleep(tick)
my_canvas.clear()
my_canvas.image(yes_button, False, 0, 0)
my_canvas.image(no_button, False, 2*mx - response_w, 0)
my_canvas.show()
exp.sleep(tick)
# Show the stimuli
my_canvas.clear()
my_canvas.text(probe)
my_canvas.image(yes_button, False, 0, 0)
my_canvas.image(no_button, False, 2*mx - response_w, 0)
my_canvas.show()
# Show the mouse, and move it to the starting point
my_mouse.set_visible(visible=True)
my_mouse.set_pos(pos=(mx, (2*my)-half_start_h))
# The actual mouse tracking
t0 = start = exp.time()
t1 = t0 + sample_rate
resp = 0
timed_out = False
slow_start = False
while 1:
position, timestamp = my_mouse.get_pos()
if my_mouse.get_pressed()[0]: # A click
if x < response_w and y < response_h:
# Clicked response 1
rt = timestamp - start
resp = 1
print 'response 1'
break
elif x > (2*mx) - response_w and y < response_h:
# Clicked response 2
rt = timestamp - start
resp = 2
print 'response 2'
break
if timestamp > t1:
# It's time to record the mouse position
t1 += sample_rate
t = timestamp - start
x, y = position
xList.append(x)
yList.append(y)
tList.append(t)
if t > max_response_time:
# Out of time, record a null response.
timed_out = True
resp = -1
rt = None
print 'Timeout'
break
# Let's figure out if the response was correct
if condition == 'truth':
correct_response = 1
else:
correct_response = 2
accuracy = int(resp == correct_response)
# Figure out if the mouse had left the start button by max_init_time
for i in range(len(yList)):
y = yList[i]
if y < ((2*my) - (2*half_start_h)):
init_step = i # The sample where y left the button
break
init_time = tList[init_step] # The time of that sample
slow_start = int(init_time > max_init_time)
# Show a message if wrong answer (optional)
if accuracy == 0 and not timed_out:
my_canvas.clear()
my_canvas.text(error_message)
my_canvas.show()
exp.sleep(error_message_duration)
# Show a message if the trial has timed out without a response
if timed_out:
my_canvas.clear()
my_canvas.text(timeout_message)
my_canvas.show()
timed_out = False
my_keyboard.get_key()
# Show a message if the trial has timed out without a response
if (slow_start and not timed_out):
my_canvas.clear()
my_canvas.text(slow_start_message)
my_canvas.show()
timed_out = False
my_keyboard.get_key()
# Standard Logging (the probe and code variables are taken care of automatically)
self.experiment.set("response", resp)
self.experiment.set("accuracy", accuracy)
self.experiment.set("rt", rt)
self.experiment.set("xTrajectory", str(xList))
self.experiment.set("yTrajectory", str(yList))
self.experiment.set("tTrajectory", str(tList))
__end__
___prepare__
# Constants
max_response_time = 3000
fixation_length = 900
error_message_duration = 1000
max_init_time = 800
sample_rate = 30
# Images
start_button = exp.get_file('materials/start.png')
yes_button = exp.get_file('materials/yes.png')
no_button = exp.get_file('materials/no.png')
# Text
timeout_message = "Too slow!U+005CnU+005C
Try to respond more quickly.U+005CnU+005C
Press any key to continue."
slow_start_message = "U+005C
Please try to move the mouse as soonU+005CnU+005C
as you see the target, even if you're notU+005CnU+005C
sure of your response yetU+005CnU+005C
Press any key to continue."
error_message = "<span color='red'>Wrong!</span>"
# Turn our OpenSesame variables into plain Python ones
probe = exp.get('probe')
condition = exp.get('condition')
# Some dimensions
# Our start button is 80x80 pixels.
# Change these values if using a different sized image.
half_start_w = 40
half_start_h = 40
# Likewise for the response images
response_w = 256
response_h = 157
# Get the size of the screen
mx = my_canvas.xcenter()
my = my_canvas.ycenter()
# Some empty lists for recording mouse data
xList, yList, tList = [], [], []
__end__
set description "Executes Python code"
define inline_script save_data
___run__
import os
subject_nr = exp.get('subject_nr')
path = os.path.join(exp.experiment_path, 'subject-%i.py' % subject_nr)
saveFile = open(path, "w")
out = "data = " + repr(exp.data)
saveFile.write(out)
saveFile.close()
__end__
set _prepare ""
set description "Executes Python code"
define inline_script setup_script
___run__
# Create our input and output routines
from openexp.mouse import mouse
from openexp.keyboard import keyboard
from openexp.canvas import canvas
my_mouse = mouse(exp, visible=True)
my_keyboard = keyboard(exp)
my_canvas = canvas(exp)
# Declare them as global, so we can use them later on.
global my_mouse, my_keyboard, my_canvas
__end__
set _prepare ""
set description "Executes Python code"
define loop trial_loop
set repeat "1"
set description "Repeatedly runs another item"
set item "trial_sequence"
set column_order "probe;condition"
set cycles "8"
set order "random"
setcycle 0 probe "The sky is blue"
setcycle 0 condition "truth"
setcycle 1 probe "Grass is green"
setcycle 1 condition "truth"
setcycle 2 probe "Ice is cold"
setcycle 2 condition "truth"
setcycle 3 probe "Humans have 2 hands"
setcycle 3 condition "truth"
setcycle 4 probe "Pigs can fly"
setcycle 4 condition "lie"
setcycle 5 probe "Nighttime is sunny"
setcycle 5 condition "lie"
setcycle 6 probe "Water is dry"
setcycle 6 condition "lie"
setcycle 7 probe "Elephants are purple"
setcycle 7 condition "lie"
run trial_sequence
define sequence trial_sequence
set flush_keyboard "yes"
set description "Runs a number of items in sequence"
run question_script "always"
run logger "always"