-
Notifications
You must be signed in to change notification settings - Fork 0
/
unpack.py
executable file
·300 lines (244 loc) · 12.7 KB
/
unpack.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
#!/usr/bin/env python
import logging
import sys
import csv
import json
import os
import pandas as pd
import numpy as np
from itertools import groupby
from collections import defaultdict
import argparse
csv.field_size_limit(sys.maxsize)
# Some of this is mirrored from the Stark lab's psychopy implementation
# Sorry it's kinda monolithic and ugly, as usual I was in a hurry
global trial_counter
parser = argparse.ArgumentParser()
parser.add_argument("input")
parser.add_argument("output")
parser.add_argument("--tsv",
help="File is a tsv, not a csv",
action="store_true")
parser.add_argument("--studytest",
help="Run study/test version instead of continuous",
action="store_true")
parser.add_argument("--qualtrics",
help="CSV is from Qualtrics (otherwise assumes sqlite backend export)",
action="store_true")
args = parser.parse_args()
observations = []
if args.qualtrics:
with open(args.input, newline='', encoding='utf16') as tsvfile:
reader = csv.DictReader(tsvfile, dialect=csv.excel_tab)
# First two rows out of Qualtrics are header-y stuff
next(reader)
next(reader)
for row in reader:
stuff = row['labjs-data'] or "{}"
observations.append(json.loads(stuff))
else:
df = pd.read_csv(args.input, header=0,
sep='\t' if args.tsv else ',',
low_memory=False)
# Need to group the data by observation id
groups = df.groupby(by='observation')
os.makedirs(args.output, exist_ok=True)
# list(groups) returns a tuple of (observation <string>, df)
# and we use to_dict to make the results look like the unpacked
# JSON from Qualtrics above so we can do the following nonsense
# to either kind of data, yay
observations = [x[1].to_dict('records') for x in list(groups)]
logging.debug(f"Dupe check starting with {len(observations)}")
# Check for multiple observations by the same participant id and session
# number and delete any after the first one, by timestamp
# Pull out some helpful values into a list of tuples
by_ppt = [(x['ppt'], x['session'], x['observation'], x['trial_number'], x['timestamp']) for o in observations for x in o]
# Build a set ofjust ppt, session, and observation
by_session = set([x[:3] for x in by_ppt])
# Now group those observations by ppt and session
# I can't get itertools groupby to work reliably - it seems to be nondeterministic
# so uh, I'm probably really confused.
possible_dupes = defaultdict(list)
for ppt, session, observation in by_session:
possible_dupes[(ppt, session)].append(observation)
# Now iterate over those results
for (ppt, session), possibles in possible_dupes.items():
if len(possibles) > 1:
# More than 1 observation for a ppt and session found! Time to dig in.
dupes = []
# Find the highest trial number and the latest timestamp in each observation
for obs in possibles:
timestamp = ""
trial_number = np.nan
data = [x for o in observations for x in o if x['ppt'] == ppt and x['session'] == session and x['observation'] == obs]
trial_numbers = [x['trial_number'] for x in data]
timestamps = [x['timestamp'] for x in data]
dupes.append((obs, max(trial_numbers), max(timestamps)))
dupes = sorted(dupes, key=lambda x: x[2])
# Now we take the first observation *that has trials*
# and remove any other ones from the later
keep_obs = None
for obs, trial_number, _ in dupes:
if trial_number > 0 and keep_obs is None:
keep_obs = obs
# Edge case: No observations had any trials? Just take the first one
if keep_obs is None:
keep_obs = dupes[0][0]
logging.debug(f"Doing dupe removal for {possibles}. Keeping {keep_obs}. Started with {len(observations)}")
# Now for any ones we're not keeping, delete them from the main observations list
for obs, _, _ in dupes:
if obs != keep_obs:
observations = [o for o in observations if o[0]['observation'] != obs]
logging.debug(f"Now have {len(observations)}")
logging.debug(f"After duplicate check, we have {len(observations)}")
for data in observations:
if len(data) == 0:
logging.warning(f'Got an observation with no data')
else:
first = data[0]
ppt = str(first['ppt'])
session = first['session'] or 'session'
stim_set = int(first.get('stimulusSet') or first.get('stimulus_set'))
order_num = first.get('orderNum') or first.get('order_num')
if args.qualtrics:
user_agent = first['meta']['userAgent']
else:
user_agent = first['meta_user_agent']
ppt_dir = os.path.join(args.output, ppt)
os.makedirs(ppt_dir, exist_ok=True)
with open(os.path.join(ppt_dir, f"{session}_time.txt"), mode='w') as out:
out.write(f"{first['timestamp']}\n")
with open(os.path.join(ppt_dir, f"{session}_metadata.txt"), mode='w') as out:
out.write(f"PPT: {ppt}\n")
out.write(f"Session: {session}\n")
out.write(f"Stimulus set: {stim_set}\n")
if not args.studytest:
out.write(f"Ordering file number: {int(order_num)}\n")
out.write(f"User agent: {user_agent}\n")
out.write('\n')
# Stark analysis stuff
ncorrect = 0
trial_counter = 0
TLF_trials = np.zeros(3)
TLF_response_matrix = np.zeros((3,3)) # Rows = O,(S),N Cols = T,L,R
lure_bin_matrix = np.zeros((4,5)) # Rows: O,S,N,NR Cols=Lure bins
# Load the bin file from json
script_path = os.path.dirname(os.path.realpath(__file__))
stim_set_path = os.path.join(script_path, "..", "lag_difficulty_for_sets", f"set{stim_set}.json")
with open(os.path.realpath(stim_set_path)) as set_file:
set_bins = json.load(set_file)
with open(os.path.join(ppt_dir, f"{session}_trials.txt"), mode='w') as out:
writer = csv.writer(out, delimiter='\t')
header = [
'Trial Number', 'Trial Type', 'Stimulus Number',
'Repetition', 'Lag', 'Lure Bin',
'Correct Was', 'Participant Response',
'Response Time']
if args.studytest:
header.remove('Lag')
writer.writerow(header)
for comp in data[1:]:
component_name = 'Stimulus'
if args.studytest:
component_name = 'Trial stimulus'
if comp['sender'] == component_name:
trial_counter += 1
if 'correct_answer' in comp:
correctWas = comp['correct_answer']
else:
correctWas = comp.get('correctResponse') or comp.get('correct_response')
if 'response' in comp:
response = comp['response']
try:
duration = int(comp['duration'])
except:
duration = "NA"
else:
response = "NA"
duration = "NA"
if response == correctWas:
ncorrect += 1
# Find the lure bin for difficulty.
# Kind of wacky coercion because set_bins is a hash of string -> string
lure_bin_index = int(set_bins[str(int(comp['stimulus_number']))])
# Calculating totals for Stark analysis
if response == 'old':
response_index = 0
elif response == 'similar':
response_index = 1
elif response == 'new':
response_index = 2
elif response == 'NA' or str(response) == 'nan':
response_index = 3
else:
raise Exception(f"Unknown response: {response}")
if comp['trial_type'] == 'repeat' and comp['repetition'] == 'b':
trial_type = 0
elif comp['trial_type'] == 'lure' and comp['repetition'] == 'b':
trial_type = 1
# Need to do add one to this response and lure bin
lure_bin_matrix[response_index,int(lure_bin_index-1)] += 1
else:
trial_type = 2
if response != 'NA' and str(response) != 'nan':
TLF_trials[trial_type] += 1
TLF_response_matrix[response_index,trial_type] += 1
output = [
comp['trial_number'],
comp['trial_type'],
comp['stimulus_number'],
comp['repetition'],
lure_bin_index,
correctWas,
response,
duration]
if not args.studytest:
output.insert(4, comp['lag'])
writer.writerow(output)
# Again, all this is from the Stark analysis
with open(os.path.join(ppt_dir, f"{session}_stats.txt"), mode='w') as log:
log.write('\nValid responses:\nTargets, {0:.0f}\nlures, {1:.0f}\nfoils, {2:.0f}'.format(
TLF_trials[0], TLF_trials[1], TLF_trials[2]))
log.write('\nCorrected rates\n')
log.write('\nRateMatrix,Targ,Lure,Foil\n')
# Fix up any no-response cell here so we don't divide by zero
TLF_trials[TLF_trials==0.0]=0.00001
log.write('Old,{0:.2f},{1:.2f},{2:.2f}\n'.format(
TLF_response_matrix[0,0] / TLF_trials[0],
TLF_response_matrix[0,1] / TLF_trials[1],
TLF_response_matrix[0,2] / TLF_trials[2]))
log.write('Similar,{0:.2f},{1:.2f},{2:.2f}\n'.format(
TLF_response_matrix[1,0] / TLF_trials[0],
TLF_response_matrix[1,1] / TLF_trials[1],
TLF_response_matrix[1,2] / TLF_trials[2]))
log.write('New,{0:.2f},{1:.2f},{2:.2f}\n'.format(
TLF_response_matrix[2,0] / TLF_trials[0],
TLF_response_matrix[2,1] / TLF_trials[1],
TLF_response_matrix[2,2] / TLF_trials[2]))
log.write('\nRaw counts')
log.write('\nRawRespMatrix,Targ,Lure,Foil\n')
log.write('Old,{0:.0f},{1:.0f},{2:.0f}\n'.format(
TLF_response_matrix[0,0], TLF_response_matrix[0,1],TLF_response_matrix[0,2]))
log.write('Similar,{0:.0f},{1:.0f},{2:.0f}\n'.format(
TLF_response_matrix[1,0], TLF_response_matrix[1,1],TLF_response_matrix[1,2]))
log.write('New,{0:.0f},{1:.0f},{2:.0f}\n'.format(
TLF_response_matrix[2,0], TLF_response_matrix[2,1],TLF_response_matrix[2,2]))
log.write('\n\nLureRawRespMatrix,Bin1,Bin2,Bin3,Bin4,Bin5\n')
log.write('Old,{0:.0f},{1:.0f},{2:.0f},{3:.0f},{4:.0f}\n'.format(
lure_bin_matrix[0,0], lure_bin_matrix[0,1], lure_bin_matrix[0,2], lure_bin_matrix[0,3], lure_bin_matrix[0,4]))
log.write('Similar,{0:.0f},{1:.0f},{2:.0f},{3:.0f},{4:.0f}\n'.format(
lure_bin_matrix[1,0], lure_bin_matrix[1,1], lure_bin_matrix[1,2], lure_bin_matrix[1,3], lure_bin_matrix[1,4]))
log.write('New,{0:.0f},{1:.0f},{2:.0f},{3:.0f},{4:.0f}\n'.format(
lure_bin_matrix[2,0], lure_bin_matrix[2,1], lure_bin_matrix[2,2], lure_bin_matrix[2,3], lure_bin_matrix[2,4]))
log.write('NR,{0:.0f},{1:.0f},{2:.0f},{3:.0f},{4:.0f}\n'.format(
lure_bin_matrix[3,0], lure_bin_matrix[3,1], lure_bin_matrix[3,2], lure_bin_matrix[3,3], lure_bin_matrix[3,4]))
log.write('\nCorrect: {0} Trials sum {1} Trial counter {2}\n'.format(ncorrect, TLF_trials.sum(), trial_counter))
if trial_counter > 0:
log.write('\nPercent-correct (corrected),{0:.2}\n'.format(ncorrect / TLF_trials.sum()))
log.write('Percent-correct (raw),{0:.2}\n'.format(ncorrect / trial_counter))
hit_rate = TLF_response_matrix[0,0] / TLF_trials[0]
false_rate = TLF_response_matrix[0,2] / TLF_trials[2]
log.write('\nCorrected recognition (p(Old|Target)-p(Old|Foil)), {0:.2f}\n'.format(hit_rate - false_rate))
sim_lure_rate = TLF_response_matrix[1,1] / TLF_trials[1]
sim_foil_rate = TLF_response_matrix[1,2] / TLF_trials[2]
log.write('\nLDI,{0:.2f}\n'.format(sim_lure_rate - sim_foil_rate))