forked from jnicosia/latarprocessing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
laTAR_parser.py
376 lines (307 loc) · 12.9 KB
/
laTAR_parser.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
"""
Python program to post-process LaTAR output json file into per-objective CSVs
"""
import json
import pandas as pd
import numpy as np
import os
from os.path import exists, normpath
# Path to input JSON files
INPUT_JSON_DIR = "./01-JSON_data"
OUTPUT_CSV_DIR = "./02-CSV_data"
# True to prompt before overwriting, False to simply overwrite
IGNORE_OVERWRITE = True
# Definitions
STIMULUS = 0
RESPONSE = 1
SOURCE = 2
COLUMN = 4
LABEL = 5
# Debug print
DEBUG = False
# Definitions for merging fixture and mobile data
# - Must place the primary pair ("real" latency) pair first. longer/secondary latency should appear after the first
TYPENAME_STIMULUS_RESPONSE = {
"Display Latency" : [
# Primary latency
{
LABEL: "display_latency_us",
STIMULUS: {
SOURCE: "mobile",
COLUMN: "corrected_displayTime_mobile",
},
RESPONSE: {
SOURCE: "fixture",
COLUMN: "corrected_detectTime_fixture",
},
},
# Seconary latencies
{
LABEL: "callback_latency_us",
STIMULUS: {
SOURCE: "mobile",
COLUMN: "corrected_callbackTime_mobile",
},
RESPONSE: {
SOURCE: "fixture",
COLUMN: "corrected_detectTime_fixture",
},
},
],
"Capacitive Tap Latency" : [
# Primary latency
{
LABEL: "action_latency_us",
STIMULUS: {
SOURCE: "fixture",
COLUMN: "corrected_timestamp_fixture",
},
RESPONSE: {
SOURCE: "mobile",
COLUMN: "corrected_actionTime_mobile",
},
},
# Seconary latencies
{
LABEL: "callback_latency_us",
STIMULUS: {
SOURCE: "fixture",
COLUMN: "corrected_timestamp_fixture",
},
RESPONSE: {
SOURCE: "mobile",
COLUMN: "corrected_callbackTime_mobile",
},
},
],
"Solenoid Tap Latency" : [
# Primary latency
{
LABEL: "action_latency_us",
STIMULUS: {
SOURCE: "fixture",
COLUMN: "corrected_timestamp_fixture",
},
RESPONSE: {
SOURCE: "mobile",
COLUMN: "corrected_callbackTime_mobile",
},
},
# Seconary latencies
{
LABEL: "callback_latency_us",
STIMULUS: {
SOURCE: "fixture",
COLUMN: "corrected_timestamp_fixture",
},
RESPONSE: {
SOURCE: "mobile",
COLUMN: "corrected_actionTime_mobile",
},
},
],
}
# Debug print
def dprint(s):
if DEBUG:
print("DEBUG: " + str(s))
def parse_objective_to_sources(objective):
sources = {}
for sample in objective["data"]:
# Build dataframe
source = sample["source"]
if source in sources:
# Already have this source
for key in sample:
sources[source][key] += [sample[key]]
else:
# Initialize source
dprint(f"Discovered new source '{source}'")
sources[source] = {}
# Add the first sample
for key in sample:
dprint(f" - Adding new key to source '{key}'")
sources[source][key] = [sample[key]]
return sources
# Given t0 and offset, corrects the time
def correct_time(timestamp, t0, offset):
return timestamp + t0 - offset
def convert_sources_dicts_to_dataframes(json_data, sources):
dataframes = {}
source_keys_list = list(sources)
for source_key in source_keys_list:
source = sources[source_key]
tempDf = pd.DataFrame.from_dict(source)
# tempDf.set_index("index") # Index could be offset later during validation, so don't index off of it
# Delete source column
tempDf.pop('source')
# Append suffix to all columns
tempDf = tempDf.add_suffix("_" + source_key)
# Pull time parameters for this column
t0_us = json_data["clockSync"][source_key]["t0"]
offset_us = json_data["clockSync"][source_key]["offset"]
# Save offset and t0 to df
tempDf["t0_us_" + source_key] = len(tempDf.index) * [t0_us]
tempDf["offset_us_" + source_key] = len(tempDf.index) * [offset_us]
# Add columns for corrected time
for column in tempDf.columns:
# Process any column with the word "time" in it
if "time" in column.lower():
tempDf["corrected_" + column] = correct_time(tempDf[column], t0_us, offset_us)
# Save dataframe to the source
dataframes[source_key] = tempDf
return dataframes
# Inserts a row of NaNs at the specified row index
def insert_df_row(df, row_idx):
pre_df = df.iloc[:row_idx]
na_row = pd.DataFrame([[np.nan]*len(df.iloc[0])], columns=list(df))
na_row.index = range(row_idx, row_idx + 1)
post_df = df.iloc[row_idx:]
post_df.index += 1
return pre_df.append(na_row).append(post_df)
# For a stimulus and response to be aligned:
# - stimulus must be <= response timestamp (increment response until satisfied)
# - response timestamp must be < next stimulus timestamp (increment stimulus until satisfied)
def align_dataframes(dataframes, typeName):
# Determine source and destination columns based on objective type
stimulus = TYPENAME_STIMULUS_RESPONSE[typeName][0][STIMULUS]
response = TYPENAME_STIMULUS_RESPONSE[typeName][0][RESPONSE]
# Initialize stimulus pointer to first index
row_idx = 0
while row_idx < len(dataframes[stimulus[SOURCE]]) and row_idx < len(dataframes[response[SOURCE]]):
# Insert rows in stimulus before row_idx so stimulus < response
while (
row_idx < len(dataframes[stimulus[SOURCE]]) and
row_idx < len(dataframes[response[SOURCE]]) and
dataframes[stimulus[SOURCE]].iloc[row_idx][stimulus[COLUMN]] > dataframes[response[SOURCE]].iloc[row_idx][response[COLUMN]]
):
dprint("WARN: Stimulus > response. Inserting row before stimulus at index {row_idx}")
dataframes[stimulus[SOURCE]] = insert_df_row(dataframes[stimulus[SOURCE]], row_idx)
row_idx += 1
dprint(f" new row idx: {row_idx}")
# Insert rows in response before row_idx so response < next stimulus
while (
((row_idx + 1) < len(dataframes[stimulus[SOURCE]])) and
(row_idx < len(dataframes[response[SOURCE]])) and
(dataframes[response[SOURCE]].iloc[row_idx][response[COLUMN]] > dataframes[stimulus[SOURCE]].iloc[row_idx + 1][stimulus[COLUMN]])
):
print(f"WARN: Response > next stimulus. Inserting row before response at index {row_idx}")
dataframes[response[SOURCE]] = insert_df_row(dataframes[response[SOURCE]], row_idx)
row_idx += 1
dprint(f" new stim idx: {row_idx} new resp idx: {row_idx}")
if row_idx < len(dataframes[stimulus[SOURCE]]) and row_idx < len(dataframes[response[SOURCE]]):
dprint(f"Matched pair: stim={dataframes[stimulus[SOURCE]].iloc[row_idx][stimulus[COLUMN]]} resp={dataframes[response[SOURCE]].iloc[row_idx][response[COLUMN]]}")
row_idx += 1
dprint(f" new row idx: {row_idx}")
# No need to assert this b/c there could be extra rows at end (eg., if stim has no paired response)
# assert len(dataframes[stimulus[SOURCE]]) == len(dataframes[response[SOURCE]]), "ERROR: at end of validation, stim and resp rows not equal!"
return dataframes
# Merges dataframes from multiple sources (mobile, fixture, etc)
def merge_source_dataframes(dataframes, typeName):
# Take the stimulus as the far-left
source_keys_list = list(dataframes)
df = dataframes[TYPENAME_STIMULUS_RESPONSE[typeName][0][STIMULUS][SOURCE]]
for source_key in source_keys_list:
# Skip the source dataframe when it shows up again
if source_key == TYPENAME_STIMULUS_RESPONSE[typeName][0][STIMULUS][SOURCE]:
continue
# Grab current dataframe from source
tempDf = dataframes[source_key]
# Make sure sources have same length
if len(tempDf) != len(df):
# raise Exception(f"Length mismatch due to source '{source_key}'. Length {str(len(tempDf))} vs {str(len(df))}")
print(f"WARN: Length mismatch due to source '{source_key}'. Length {str(len(tempDf))} vs {str(len(df))}")
#warning = True # Disabled this warning b/c it's not very useful. It's possible to have length mismatch due to missing samples on fixture or mobile
df = df.join(tempDf, how='outer')
return df
# Export CSV
def export_csv(csv_filepath, df):
# True if file conflict exists
conflict = False
# Uncomment the following to prompt file overwrite if it exists
if exists(csv_filepath):
print("**********************************************************")
print(f"WARN: Output CSV filename '{csv_filepath}' already exists!")
print("**********************************************************")
conflict = True
if not IGNORE_OVERWRITE:
val = input(f"Overwrite CSV? (Y/n): ")
if val != "Y":
print("Not overwriting file. Skipping this objective...")
return conflict
try:
df.to_csv(csv_filepath, index=False)
print(f"INFO: Successfully wrote '{csv_filepath}'")
return conflict
except Exception as e:
print(f"ERROR: Failed to write to '{csv_filepath}': {str(e)}")
return True
#######################################################
# Processes a JSON file and returns the number of failures
def process_file(filename, output_dir):
print(f"INFO: Processing file '{filename}'...")
f = open(filename, 'r')
# returns JSON object as a dictionary
data = json.load(f)
# Close file
f.close()
# Pull static metadata
phone_name = data["mobile"]["name"]
# Iterating through the json
objectives = data["objectives"]
procedure_objectives = data["procedure"]["objectives"]
conflicts = 0
# Process each objective
for i,objective in enumerate(objectives):
# Adds flag in filename
warning = False
# Pull metadata from this objective
typeName = procedure_objectives[i]["typeName"]
interval = procedure_objectives[i]["parameters"]["interval"]
count = procedure_objectives[i]["parameters"]["count"]
print(f"INFO: Processing objective '{typeName}'")
# Parse json object to per-source dictionaries
sources = parse_objective_to_sources(objective)
# Convert each source dictionary to dataframe
dataframes = convert_sources_dicts_to_dataframes(data, sources)
# Align rows between stimulus and response dataframes
dataframes = align_dataframes(dataframes, typeName)
# Merge this objective's dataframes and export as .csv
df = merge_source_dataframes(dataframes, typeName)
# add latency (difference between stimulus and response values)
for stim_resp in TYPENAME_STIMULUS_RESPONSE[typeName]:
df[stim_resp[LABEL]] = df[stim_resp[RESPONSE][COLUMN]] - df[stim_resp[STIMULUS][COLUMN]]
# Add metadata as new columns
df["phone_model"] = len(df.index) * [phone_name]
# Make sure all elements used in output CSV filename are strings
csv_filename_elts = [phone_name, typeName, interval, "ms", count, "x"]
for i,elt in enumerate(csv_filename_elts):
csv_filename_elts[i] = str(elt)
# Export CSV
csv_filename = ("_".join(csv_filename_elts) + ".csv").replace(" ", "_")
if warning:
csv_filename = "WARN-" + csv_filename
csv_folder = normpath(output_dir + "/" + typeName.replace(" ", "_"))
csv_filepath = normpath(csv_folder + "/" + csv_filename)
if not os.path.exists(csv_folder):
os.makedirs(csv_folder)
if export_csv(csv_filepath, df):
conflicts += 1
return conflicts
#######################################################
# Main program
#######################################################
files = os.listdir(normpath(INPUT_JSON_DIR))
conflicts = 0
num_processed_files = 0
for i,file in enumerate(files):
print(f"========[{i}/{len(files)}]=======")
filepath = normpath(INPUT_JSON_DIR + "/" + file)
if file.endswith(".json"):
conflicts += process_file(filepath, OUTPUT_CSV_DIR)
num_processed_files += 1
else:
print(f"INFO: Ignoring non-json file '{filepath}'")
print(f"\n\nINFO: Done. Processed {num_processed_files} JSON files with {conflicts} conflicts/failures.")
if conflicts > 0:
print("See 'WARN' flags in log output above to see what went wrong. Conflicts are when the output file already exists.")