-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_stimuli.py
executable file
·271 lines (217 loc) · 9.65 KB
/
process_stimuli.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
import xlrd
import sys
import csv
from Tkinter import *
import tkFileDialog
#
# if len(sys.argv) != 4:
# print "missing arguments"
# print "usage: process_stimuli.py <template.xlsx> <stimuli.xslx> <output.csv>"
# sys.exit(0)
#
# stimuli_book = xlrd.open_workbook(sys.argv[2])
# stimuli_sheet = stimuli_book.sheet_by_index(0)
# template_book = xlrd.open_workbook(sys.argv[1])
# template_sheet = template_book.sheet_by_index(0)
#
# data = []
#
# #0) keep the header row, ...
# hdr = []
# for c in range(13):
# hdr.append(template_sheet.cell_value(0, c))
# data.append(hdr)
# print hdr
# #get column G's value
# order = stimuli_sheet.cell_value(1, 6)
#
# #find the first row corresponding to the G subset
# row = 1
# while True:
# if template_sheet.cell_value(row, 12) == order:
# break
# row += 1
#
# #0) ..., and then take the subset of the template that is the order corresponding to column G's value (1-4)
# while template_sheet.cell_value(row, 12) == order:
# #print "row: " + str(row) + " template value: " + str(template_sheet.cell_value(row, 12)) + " order: " + str(order)
# row_data = []
# for col in range(13):
# val = template_sheet.cell_value(row, col)
# if template_sheet.cell_type(row, col) == 2:
# row_data.append(int(val))
# else:
# row_data.append(val)
# data.append(row_data)
# #print "_dimnrows: " + str(template_sheet._dimnrows)
#
# if row >= template_sheet._dimnrows - 1:
# break
# else:
# row += 1
#
# #1) Use the 20 rows (after the header row) in columns A and B to write into columns B through D of the spreadsheet
# #3) replace 1-16 in the .wav and .jpg with the words numbered 1-16 (e.g. 1.jpg becomes banana.jpg and can_banana.jpg)
# #4) IF there is something in column e that is not NA, replace with that instead of with the word in column B (e.g. sock3 instead of sock) ONLY in columns B&C not in column D
# for r in range(5, len(data)):
# index = int(data[r][1].split('.')[0])
# col_e = stimuli_sheet.cell_value(index + 4, 4)
# if col_e == "NA":
# data[r][1] = stimuli_sheet.cell_value(index + 4, 1) + ".jpg"
# else:
# data[r][1] = stimuli_sheet.cell_value(index + 4, 4) + ".jpg"
#
# index = int(data[r][2].split('.')[0])
# col_e = stimuli_sheet.cell_value(index + 4, 4)
# if col_e == "NA":
# data[r][2] = stimuli_sheet.cell_value(index + 4, 1) + ".jpg"
# else:
# data[r][2] = stimuli_sheet.cell_value(index + 4, 4) + ".jpg"
#
# prefix = data[r][3].split('.')[0].split('_')[0]
# index = int(data[r][3].split('.')[0].split('_')[1])
# data[r][3] = "%s_%s.wav" % (prefix, stimuli_sheet.cell_value(index + 4, 1))
#
# #5) replace A:H in column F with the pairs corresponding to A:H in column I of the stimuli spreadsheet
# data[r][5] = stimuli_sheet.cell_value(ord(data[r][5]) - ord('A') + 1, 8)
#
#
# #2) replace practice1.jpg-practice4.jpg with the first four words of 'stimuli' labeled p1-p4
# for r in range(1, 5):
# word = stimuli_sheet.cell_value(r, 1)
# data[r][1] = data[r][1].replace("practice%d" % r, word)
# data[r][3] = data[r][3].replace("practice%d" % r, word)
#
#
# print "data: " + str(data)
#
#
# # with open(sys.argv[3], 'wb') as f:
# # for row in data:
# # first = True
# # for item in row:
# # if not first:
# # f.write('\t')
# # first = False
# # f.write(str(item))
# # f.write('\n')
#
#
# # This replaces the commented section above.
# with open(sys.argv[3], 'w') as file:
#
# csvwriter = csv.writer(file, delimiter='\t')
#
# csvwriter.writerow(data[0]) # write the header row
#
# for row in data[1:]: # write each subsequent row (skipping the header)
# csvwriter.writerow(row)
#
class MainWindow:
def __init__(self, master):
self.root = master
root.title("Stimuli Datasource")
self.root.geometry("500x300")
self.main_frame = Frame(root)
self.main_frame.pack()
self.template_file = None
self.stimuli_file = None
self.export_file = None
self.template_book = None
self.template_sheet = None
self.stimuli_book = None
self.stimuli_sheet = None
self.data = []
self.load_template_button = Button(self.main_frame,
text="Load Template",
command=self.load_template)
self.load_template_button.grid(row=2,column=1)
self.load_stimuli_button = Button(self.main_frame,
text="Load Stimuli",
command=self.load_stimuli)
self.load_stimuli_button.grid(row=2,column=2)
self.export_button = Button(self.main_frame,
text="Export",
command=self.export)
self.export_button.grid(row=2,column=3)
self.template_loaded_label = Label(self.main_frame, text="Template Loaded", fg="blue")
self.stimuli_loaded_label = Label(self.main_frame, text="Stimuli Loaded", fg="green")
def load_template(self):
self.template_file = tkFileDialog.askopenfilename()
self.template_book = xlrd.open_workbook(self.template_file)
self.template_sheet = self.template_book.sheet_by_index(0)
self.template_loaded_label.grid(row=3, column=1)
def load_stimuli(self):
self.stimuli_file = tkFileDialog.askopenfilename()
self.stimuli_book = xlrd.open_workbook(self.stimuli_file)
self.stimuli_sheet = self.stimuli_book.sheet_by_index(0)
self.stimuli_loaded_label.grid(row=3, column=2)
def run(self):
#0) keep the header row, ...
hdr = []
for c in range(13):
hdr.append(self.template_sheet.cell_value(0, c))
self.data.append(hdr)
print hdr
#get column G's value
order = self.stimuli_sheet.cell_value(1, 6)
#find the first row corresponding to the G subset
row = 1
while True:
if self.template_sheet.cell_value(row, 12) == order:
break
row += 1
#0) ..., and then take the subset of the template that is the order corresponding to column G's value (1-4)
while self.template_sheet.cell_value(row, 12) == order:
#print "row: " + str(row) + " template value: " + str(template_sheet.cell_value(row, 12)) + " order: " + str(order)
row_data = []
for col in range(13):
val = self.template_sheet.cell_value(row, col)
if self.template_sheet.cell_type(row, col) == 2:
row_data.append(int(val))
else:
row_data.append(val)
self.data.append(row_data)
#print "_dimnrows: " + str(template_sheet._dimnrows)
if row >= self.template_sheet._dimnrows - 1:
break
else:
row += 1
#1) Use the 20 rows (after the header row) in columns A and B to write into columns B through D of the spreadsheet
#3) replace 1-16 in the .wav and .jpg with the words numbered 1-16 (e.g. 1.jpg becomes banana.jpg and can_banana.jpg)
#4) IF there is something in column e that is not NA, replace with that instead of with the word in column B (e.g. sock3 instead of sock) ONLY in columns B&C not in column D
for r in range(5, len(self.data)):
index = int(self.data[r][1].split('.')[0])
col_e = self.stimuli_sheet.cell_value(index + 4, 4)
if col_e == "NA":
self.data[r][1] = self.stimuli_sheet.cell_value(index + 4, 1) + ".jpg"
else:
self.data[r][1] = self.stimuli_sheet.cell_value(index + 4, 4) + ".jpg"
index = int(self.data[r][2].split('.')[0])
col_e = self.stimuli_sheet.cell_value(index + 4, 4)
if col_e == "NA":
self.data[r][2] = self.stimuli_sheet.cell_value(index + 4, 1) + ".jpg"
else:
self.data[r][2] = self.stimuli_sheet.cell_value(index + 4, 4) + ".jpg"
prefix = self.data[r][3].split('.')[0].split('_')[0]
index = int(self.data[r][3].split('.')[0].split('_')[1])
self.data[r][3] = "%s_%s.wav" % (prefix, self.stimuli_sheet.cell_value(index + 4, 1))
#5) replace A:H in column F with the pairs corresponding to A:H in column I of the stimuli spreadsheet
self.data[r][5] = self.stimuli_sheet.cell_value(ord(self.data[r][5]) - ord('A') + 1, 8)
#2) replace practice1.jpg-practice4.jpg with the first four words of 'stimuli' labeled p1-p4
for r in range(1, 5):
word = self.stimuli_sheet.cell_value(r, 1)
self.data[r][1] = self.data[r][1].replace("practice%d" % r, word)
self.data[r][3] = self.data[r][3].replace("practice%d" % r, word)
def export(self):
self.run()
self.export_file = tkFileDialog.asksaveasfilename()
with open(self.export_file, 'w') as file:
csvwriter = csv.writer(file, delimiter='\t')
csvwriter.writerow(self.data[0]) # write the header row
for row in self.data[1:]: # write each subsequent row (skipping the header)
csvwriter.writerow(row)
if __name__ == "__main__":
root = Tk()
MainWindow(root)
root.mainloop()