-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_itch_task.py
245 lines (188 loc) · 8.56 KB
/
run_itch_task.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
# !/Users/bramzandbelt/anaconda/envs/psychopyenv/bin python
# -*- coding: utf-8 -*-
"""
This script runs the inertemporal choice task
"""
__author__ = "bramzandbelt (bramzandbelt@gmail.com)"
__copyright__ = "Copyright (c) 2018 Bram Zandbelt"
__license__ = "CC BY 4.0"
__version__ = "0.1"
__vcs_id__ = ""
###############################################################################
# IMPORT MODULES
###############################################################################
import psychopy as pp
from psychopy import iohub as ppiohub
from psychopy import gui as ppgui
import itch as itch
import sys
import glob
# Assure that text files are read in as Unicode (utf8) instead of ASCII
reload(sys)
sys.setdefaultencoding('utf8')
class ExperimentRuntime(ppiohub.ioHubExperimentRuntime):
"""
Create class
"""
def run(self,*args):
"""
The run method contains your experiment logic. It is equal to what would be in your main psychopy experiment
script.py file in a standard psychopy experiment setup. That is all there is too it really.
:param args:
"""
# from psychopy import gui
# 1. Parse and process configuration
# ---------------------------------------------------------------------
mod_dir = args[0][1]
config = itch.init_config(self,mod_dir)
session_ix = config['session']['session_ix']
if config['instruction']['enable']:
# Present general instruction
itch.present_instruction(config, 'start')
# 2. Run practice blocks
# ---------------------------------------------------------------------
stage_id = 'practice'
if config[stage_id]['enable']:
trial_list = itch.make_trial_list(config, stage_id=stage_id)
if config['instruction']['enable']:
# Present practice instruction
itch.present_instruction(config, stage_id, 0)
itch.run_stage(config=config,
stage_id=stage_id,
trial_list=trial_list)
else:
pass
# 2. Run indifference point procedure
# ---------------------------------------------------------------------
stage_id = 'ip_procedure'
if config[stage_id]['enable']:
trial_list = itch.make_trial_list(config, stage_id=stage_id)
if config['instruction']['enable']:
# Present indifference point procedure instruction
itch.present_instruction(config, stage_id, 0)
itch.run_stage(config=config,
stage_id=stage_id,
trial_list=trial_list)
else:
pass
# 3. Run experimental blocks
# ---------------------------------------------------------------------
stage_id = 'experiment'
if config[stage_id]['enable']:
trial_list = itch.make_trial_list(config, stage_id=stage_id)
if config['instruction']['enable']:
# Present experiment instruction
itch.present_instruction(config, stage_id, 0)
itch.run_stage(config=config,
stage_id=stage_id,
trial_list=trial_list)
else:
pass
# 4.Terminate experiment
# ---------------------------------------------------------------------
itch.present_instruction(config, 'end')
pp.core.quit()
####### Main Script Launching Code Below #######
if __name__ == "__main__":
import os
import psychopy as pp
def main(mod_dir):
"""
:param mod_dir: Directory where module files reside
"""
config_dir = os.path.normcase(os.path.join(mod_dir,'config/'))
# Let user select response device
# ---------------------------------------------------------------------
rd_config_files = {'Keyboard':
'iohub_keyboard.yaml',
'Serial':
'iohub_serial.yaml',
'fORP':
'iohub_forp.yaml'}
info = {'Response Device = ': ['Select',
'Keyboard',
'Serial',
'fORP']}
dlg_info = dict(info)
info_dlg = ppgui.DlgFromDict(dictionary = dlg_info,
title = 'Select response device')
if not info_dlg.OK:
return -1
while dlg_info.values()[0] == u'Select' and info_dlg.OK:
dlg_info = dict(info)
info_dlg = ppgui.DlgFromDict(dictionary=dlg_info,
title='SELECT Response device to continue...')
if not info_dlg.OK:
return -1
# Determine which iohub base configuration file to use
# ---------------------------------------------------------------------
os.chdir(config_dir)
iohub_base_files = ['Select']
iohub_base_files.extend(glob.glob('./iohub_base*.yaml'))
os.chdir(mod_dir)
iohub_base_config_info = {'IOHub base config file = ': iohub_base_files}
iohub_base_config_info = dict(iohub_base_config_info)
iohub_base_config_dlg = ppgui.DlgFromDict(dictionary = iohub_base_config_info,
title = 'Select IOHub base config file')
if not iohub_base_config_dlg.OK:
return -1
while iohub_base_config_info.values()[0] == u'Select' and iohub_base_config_dlg.OK:
iohub_base_config_info = dict(iohub_base_config_info)
iohub_base_config_dlg = ppgui.DlgFromDict(dictionary = iohub_base_config_info,
title = 'SELECT IOHub base config file to continue...')
# Merge iohub configuration files
# ---------------------------------------------------------------------
base_config_file = os.path.normcase(
os.path.join(
config_dir,
'iohub_base_itch_experiment_macbook.yaml'.replace('./', '')
# iohub_base_config_info.values()[0].replace('./','')
)
)
resp_dev_config_file = \
os.path.normcase(
os.path.join(
config_dir,
'iohub_keyboard.yaml'
# rd_config_files[dlg_info.values()[0]]
)
)
combined_config_file = \
os.path.normcase(os.path.join(config_dir,
'iohub_config.yaml'
)
)
ExperimentRuntime.mergeConfigurationFiles(base_config_file,
resp_dev_config_file,
combined_config_file)
# Determine which experiment configuration file to use
# ---------------------------------------------------------------------
os.chdir(config_dir)
expt_files = ['Select']
expt_files.extend(glob.glob('./expt_*.yaml'))
os.chdir(mod_dir)
expt_config_info = {'Experiment config file = ': expt_files}
expt_config_info = dict(expt_config_info)
expt_config_dlg = ppgui.DlgFromDict(dictionary = expt_config_info,
title = 'Select experiment config file')
if not expt_config_dlg.OK:
return -1
while expt_config_info.values()[0] == u'Select' and expt_config_dlg.OK:
expt_config_info = \
dict(expt_config_info)
expt_config_dlg = \
ppgui.DlgFromDict(dictionary=expt_config_info,
title='SELECT experiment config file to continue...')
if not expt_config_dlg.OK:
return -1
# Start the experiment
# ---------------------------------------------------------------------
# runtime = ExperimentRuntime(config_dir, 'expt_itch.yaml')
# runtime.start(('iohub_keyboard.yaml', mod_dir))
runtime = ExperimentRuntime(config_dir, expt_config_info.values()[0])
runtime.start((dlg_info.values()[0],mod_dir))
# Get the current directory, using a method that does not rely on __FILE__
# or the accuracy of the value of __FILE__.
mod_dir = ppiohub.module_directory(main)
# Run the main function, which starts the experiment runtime
main(mod_dir)