-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdualtask_labeling_configuration.py
284 lines (228 loc) · 10.4 KB
/
dualtask_labeling_configuration.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
"""
dualtask_labeling_configuration.py
This module configures the environment for a dual-task labeling experiment using PsychoPy. It includes functionalities
for resource path management, window creation, stimuli initialization, participant information collection, and
progress tracking. It supports essential operations such as accessing stimuli files, saving experiment results, and
managing the participant's progress through the experiment phases.
"""
from psychopy import monitors, visual, gui, core
import random
import os
import datetime
import sys
import csv
def resource_path(relative_path):
"""
Constructs an absolute path to a resource, taking into account whether the application is running from a compiled
executable or a standard Python script.
Parameters:
- relative_path (str): The relative path to the resource.
Returns:
- str: The absolute path to the resource.
"""
if getattr(sys, 'frozen', False):
# If we're running as a bundled exe, set the base path as one level above the executable
base_path = os.path.join(os.path.dirname(sys.executable), "..")
else:
# If we're running in a normal Python environment
base_path = os.path.dirname(os.path.abspath(__file__))
return os.path.join(base_path, relative_path)
# Define paths for accessing various experiment resources.
test_stimuli_path = resource_path('stimuli/test/')
practice_stimuli_path = resource_path('stimuli/practice/')
results_path = resource_path('results/')
pics_path = resource_path('pics/')
random_path = resource_path('randomization_lists/')
def create_window():
"""
Creates and returns a PsychoPy window object with predefined settings for the experiment.
Returns:
- psychoPy.visual.Window: A window object for displaying the experiment's visual stimuli.
"""
current_monitor = monitors.Monitor(name='testMonitor')
# Create and return a window for the experiment
return visual.Window(monitors.Monitor.getSizePix(current_monitor),
monitor="testMonitor",
allowGUI=True,
fullscr=True,
color=(255, 255, 255)
)
def save_pictogram_order(labeler_id, pictograms_order, positions, labels):
"""
Saves the order of pictograms used in the experiment to a CSV file for a given labeler.
Parameters:
- labeler_id (str): Unique identifier for the participant.
- pictograms_order (list): List of pictogram filenames.
- positions (list): List of positions for each pictogram.
- labels (list): List of labels associated with each pictogram.
"""
filename = f"{labeler_id}_pictogram_order.csv"
filepath = os.path.join(results_path, labeler_id, filename)
os.makedirs(os.path.dirname(filepath), exist_ok=True)
with open(filepath, 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['Pictogram', 'PositionX', 'PositionY', 'Label'])
for pictogram, (posX, posY), label in zip(pictograms_order, positions, labels):
writer.writerow([pictogram, posX, posY, label])
print(f"Saved pictogram order to {filepath}")
def load_pictogram_order(labeler_id):
"""
Loads the order of pictograms from a CSV file for a given labeler, if available.
Parameters:
- labeler_id (str): Unique identifier for the participant.
Returns:
- tuple: (pictograms_order, positions, labels) if file found, otherwise (None, None, None).
"""
filename = f"{labeler_id}_pictogram_order.csv"
filepath = os.path.join(results_path, labeler_id, filename)
if os.path.exists(filepath):
with open(filepath, 'r') as csvfile:
reader = csv.DictReader(csvfile)
pictograms_order = []
positions = []
labels = []
for row in reader:
pictograms_order.append(row['Pictogram'])
positions.append((float(row['PositionX']), float(row['PositionY'])))
labels.append(row['Label'])
print(f"Loaded pictogram order from {filepath}")
return pictograms_order, positions, labels
print("No pictogram order file found.")
return None, None, None
def initialize_stimuli(window, labeler_id):
"""
Initializes and returns the stimuli to be used in the experiment based on the labeler's pictogram order.
Parameters:
- window (psychoPy.visual.Window): The window where stimuli will be displayed.
- labeler_id (str): Unique identifier for the participant.
Returns:
- tuple: Collection of PsychoPy visual stimuli objects.
"""
pictograms_order, loaded_positions, loaded_labels = load_pictogram_order(labeler_id)
if not pictograms_order:
print("Initializing with default pictogram order.")
# Define default order and positions if not loaded
pictograms_order = [os.path.join(pics_path, 'no_bracket.png'), os.path.join(pics_path, 'bracket.png')]
positions = [(-0.65, 0), (0.65, 0)]
labels = ['left', 'right']
random.shuffle(positions) # Shuffle positions to randomize
save_pictogram_order(labeler_id, pictograms_order, positions, labels)
else:
positions = loaded_positions
labels = loaded_labels
print("Using loaded pictogram order.")
# Create fixation cross
fixation_cross = visual.ShapeStim(window,
vertices=((0, -0.13), (0, 0.13), (0, 0), (-0.09, 0), (0.09, 0)),
lineWidth=15,
closeShape=False,
lineColor="black",
name='fixation'
)
# Create pictograms
bracket_pic = visual.ImageStim(window,
image=pictograms_order[1],
pos=positions[1]
)
nobracket_pic = visual.ImageStim(window,
image=pictograms_order[0],
pos=positions[0]
)
questionmark_pic = visual.ImageStim(window,
image=os.path.join(pics_path, 'question.png'),
pos=(0, 0)
)
audio_pic = visual.ImageStim(window,
image=pics_path + 'audio.png',
pos=(0, 0),
name='audio_pic')
# Labels for the positions
bracket_pos_label = labels[1]
nobracket_pos_label = labels[0]
# arrow parameters
arrowPositions = [[0.7, -0.7], [-0.7, -0.7], [0, -0.8]] # position in the x-y-coordinate system
arrowOrientations = [0, -180, -270] # rotation in degrees
arrowSize = 0.2 # size in pixels?
arrows = [] # empty list to append to
# generate all arrows in correct orientation
for i in range(3):
arrow = visual.ImageStim(window,
image=os.path.join(pics_path, 'next.png'),
pos=arrowPositions[i],
size=arrowSize,
ori=arrowOrientations[i]
)
arrows.append(arrow)
return fixation_cross, bracket_pic, bracket_pos_label, nobracket_pic, nobracket_pos_label, pictograms_order, \
audio_pic, questionmark_pic, arrows
def get_participant_info():
"""
Displays a GUI dialog to collect participant information at the start of the experiment.
Returns:
- dict: Collected participant information if dialog is confirmed, otherwise quits the experiment.
"""
exp_data = {
'experiment': 'labeling_experiment',
'cur_date': datetime.datetime.now().strftime("%Y-%m-%d %H:%M"),
'labeler': 'labelerID'
}
# Dialogue box to get participant information
info_dialog = gui.DlgFromDict(dictionary=exp_data,
title='Labeling Production Data from Dualtask',
fixed=['experiment', 'cur_date']
)
if info_dialog.OK:
return exp_data
else:
core.quit()
# Function to append a single result to the CSV file
def append_result_to_csv(writer, result, output_file):
"""
Appends the result of a trial to the appropriate CSV file.
Parameters:
writer (csv.writer): The CSV writer object to use for writing.
result (list): A list containing the data for a single trial.
output_file (File object): The CSV file object to use for writing.
"""
writer.writerow(result)
output_file.flush()
os.fsync(output_file.fileno())
def get_progress_file_path(labeler_id, phase):
"""
Generate the file path for a labeler's progress file for a given phase.
Parameters:
labeler_id (str): The ID of the labeler.
phase (str): The phase of the experiment ('practice' or 'test').
Returns:
str: File path for the labeler's progress file for the given phase.
"""
progress_directory = os.path.join(results_path, labeler_id)
os.makedirs(progress_directory, exist_ok=True) # Ensure the directory exists
filename = f"{labeler_id}_{phase}_progress.csv"
return os.path.join(progress_directory, filename)
def get_randomization_file_path(labeler_id, phase):
"""
Generate the file path for a labeler's randomization file based on the phase (practice or test).
Parameters:
labeler_id (str): The ID of the labeler.
phase (str): The phase of the experiment ('practice' or 'test').
Returns:
str: File path for the labeler's randomization file for the given phase.
"""
randomization_directory = os.path.join(random_path, labeler_id)
os.makedirs(randomization_directory, exist_ok=True) # Ensure the directory exists
filename = f"{labeler_id}_{phase}_randomized_stimuli.csv"
return os.path.join(randomization_directory, filename)
def load_progress(progress_file_path):
"""
Load the progress from a given CSV file.
Parameters:
progress_file_path (str): Path to the progress CSV file.
Returns:
list: List of stimuli filenames that have been processed.
"""
if not os.path.exists(progress_file_path):
return [] # Return empty list if file doesn't exist
with open(progress_file_path, 'r') as csvfile:
reader = csv.reader(csvfile)
return [row[0] for row in reader]