-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.py
455 lines (314 loc) · 13.9 KB
/
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#!/bin/python3
"""
About this Script
These helper functions convert *long* participant responses (derived from a JSON file)
into *wide* particticipant responses, such that one row in a DataFrame represents a complete
ping. Several of these functions can be better optimized (stripping out leading/trailing quoation
marks is a fine example) ... this project is fully functional as of 11/16/2021
Ian Ferguson | Stanford University
"""
# ----------- Imports
import os, pathlib, json
import pandas as pd
import numpy as np
from tqdm import tqdm
from time import sleep
# ----------- Definitions
# ----- Global
def setup(PATH):
"""
PATH => Relative path to project directory
Runs before parsing
Creates required output directories if they don't already exist
"""
for output_path in ["00-Subjects", "01-Aggregate"]:
# Subjects => Subject specific CSVs
# Aggregate => Subject CSV, Device CSV, Parent errors JSON file
if not os.path.exists(os.path.join(".", PATH, output_path)):
print(f"Creating {output_path}...")
# Create the output directory if it doesn't exist
pathlib.Path(os.path.join(".", PATH, output_path)).mkdir(exist_ok=True,
parents=True)
sleep(1.5)
else:
print(f"{output_path} exists...")
def isolate_json_file(PATH):
"""
PATH => Relative path to project directory
You should have ONE JSON file in your project directory
This function isolates it and returns:
* The JSON itself
* The isolated filename for later use
"""
# Should be a list of length 1
files = [x for x in os.listdir(os.path.join(".", PATH)) if ".json" in x]
# Raise error if there are more than 1 JSON file
if len(files) > 1:
raise OSError(f"Your project directory should only have one JSON file ... check {PATH} again")
# E.g., test_data.json => test_data
filename = files[0].split('.json')[0]
return os.path.join(".", PATH, files[0]), filename
def output(KEY, PINGS, ANSWERS, OUTPUT_DIR, KICKOUT):
"""
KEY => Key from JSON file
PINGS => Pandas DataFrame object
ANSWERS => Pandas DataFrame object
OUTPUT_DIR => Relative path to aggregates directory
KICKOUT => Boolean, determiens if CSV will be saved
Merges pings and answers dataframes
Returns DataFrame object
"""
# Isolate username
KEY = KEY.split('-')[0]
# Combine dataframes on ping identifier (e.g., modalStream1)
composite_dataframe = PINGS.merge(ANSWERS, on="id")
# Option to save locally or not
if KICKOUT:
output_name = os.path.join(f"{OUTPUT_DIR}/{KEY}.csv")
# Avoid duplicates (possible with same username / different login IDs)
if os.path.exists(output_name):
output_name = os.path.join(f"{OUTPUT_DIR}/{KEY}_b.csv")
composite_dataframe.to_csv(output_name, index=False, encoding="utf-8-sig")
return composite_dataframe
def sanity_check(JSON, OUTPUT_DIR):
"""
JSON => Relative path to data dictionary
OUTPUT_DIR => Relative path to output directory
This function performs the following operations
* Read in JSON file as dictionary
* Isolate list of unique subject ID's
* If subject ID appears more than once, store in JSON
* Kick out data JSON with indent, kick out duplicate JSON
Returns nothing, functions inplace
"""
# Read in subject data JSON as dictionary
with open(JSON) as incoming:
data = json.load(incoming)
keys = list(data.keys()) # List of keys from JSON
sub_ids = set([x.split('-')[0] for x in keys]) # Unique subject IDs
output_dict = {} # Empty dictionary to append into
print("\nIdentifying duplicate subject responses...\n")
for sub in tqdm(sub_ids):
instances = [x for x in keys if sub in x] # Number of responses from single sub
if len(instances) > 1: # Add to output dict if multiples exist
output_dict[sub] = {}
output_dict[sub]['count'] = len(instances)
output_dict[sub]['keys'] = instances
print("\nSaving response-duplicates JSON file...\n")
# Push to local JSON file
with open(os.path.join(OUTPUT_DIR, "response-duplicates.json"), "w") as outgoing:
json.dump(output_dict, outgoing, indent=4)
# ----- Answers
def derive_answers(SUBSET, LOG, USER):
"""
SUBSET => Reduced dictionary of subject information (pings/user/answers)
LOG => Text file to log issues
USER => Username, used in error log
This function isolates participant respones and converts from long to wide
Returns DataFrame object
"""
def isolate_values(DF):
"""
DF => Dataframe object
While data is still "long", we'll isolate the participant response
"""
if DF['preferNotToAnswer']:
return "PNA"
try:
# Raw data is optimized for dictionary expresson, we'll save the values
temp = dict(DF['data']).values()
return list(temp)
except:
# NOTE: Consider returning empty string instead
return None
# Isolated participant response dictionary
answers = pd.DataFrame(SUBSET['answers'])
try:
# Create new "value" column with aggregated response
answers['value'] = answers.apply(isolate_values, axis=1)
except Exception as e:
# Write to error log
LOG.write(f"\nCaught @ {USER} + isolate_values: {e}\n\n")
try:
# Apply cleanup_values function (removes extra characters)
answers['value'] = answers['value'].apply(lambda x: cleanup_values(x))
except Exception as e:
# Write to error log
LOG.write(f"\nCaught @ {USER} + cleanup_values: {e}\n\n")
answers = answers.drop_duplicates(subset="date", keep="first").reset_index(drop=True)
answers["IX"] = answers.groupby("questionId", as_index=False).cumcount()
# Drop extraneous columns
answers.drop(columns=['data', 'preferNotToAnswer'], inplace=True)
# Pivot long to wide
answers = answers.pivot(index="pingId", columns="questionId", values="value").reset_index()
# Rename Ping ID column (for merge with pings DF)
answers.rename(columns={'pingId':'id'}, inplace=True)
return answers
def cleanup_values(x):
"""
x => Isolated value derived from lambda
This function is applied via lambda, serialized per column
"""
# Parse out parentheses
#temp = str(x).replace('(', '').replace(')', '')
temp = str(x)
"""
The conditional statements below will strip out square brackets
and leading / trailing parentheses
Yields a clean value to work with in the resulting dataframe
"""
if temp[0] == "[":
temp = temp[1:]
if temp[-1] == "]":
temp = temp[:-1]
if temp[0] == "\'":
temp = temp[1:]
elif temp[0] == "\"":
temp = temp[1:]
if temp[-1] == "\'":
temp = temp[:-1]
elif temp[-1] == "\"":
temp = temp[:-1]
return temp
def parse_nominations(DF):
"""
DF => Dataframe object
This function is named nominations ... e.g., Dean Baltiansky
The following columns are parsed...
* SU_Nom => 1,2,3
* SU_Nom_None_Nom => 1,2,3
* NSU_Rel => 1,2,3
* NSU_Nom_None_Nom => 1,2,3
"""
# Keys are existing columns, Values are new columns
voi = {'SU_Nom': 'SU_Nom_{}',
'SU_Nom_None_Nom': 'SU_Nom_None_Nom_{}',
'NSU_Rel': 'NSU{}_Rel',
'NSU_Nom_None_Nom': 'NSU{}_None_Rel'}
for parent in list(voi.keys()):
# Not every participant has every variable ... this will standardize it
if parent not in list(DF.columns):
DF[parent] = [] * len(DF)
continue
for k in [1, 2, 3]:
new_var = voi[parent].format(k) # E.g., SU_Nom_1
DF[new_var] = [''] * len(DF) # Create empty column
for ix, value in enumerate(DF[parent]):
try:
check_nan = np.isnan(value) # If value is null we'll skip over it
continue
except:
if str(value) == "None": # Skip over "None" and "PNA" values
continue
elif value == "PNA":
continue
value = value.replace("\"", "\'").split("\',") # Replace double-quotes, split on comma b/w nominees
for k in range(len(value)):
new_var = voi[parent].format(k+1)
try:
new_val = value[k] # Isolate nominee by list position
except:
continue # Skip over index error
for char in ["[", "]"]:
new_val = new_val.replace(char, "") # Strip out square brackets
new_val = new_val.strip() # Remove leading / trailing space
DF.loc[ix, new_var] = new_val # Push isolated nominee to DF
for parent in list(voi.keys()):
for k in [1,2,3]:
new_var = voi[parent].format(k)
# Run cleanup_values function again to strip out leading / trailing characters (for roster matching)
DF[new_var] = DF[new_var].apply(lambda x: cleanup_values(x))
return DF
def parse_race(DF):
"""
DF => DataFrame object
This function un-nests race responses
Returns list of all responses marked True (may be more than one)
"""
def isolate_race_value(x):
"""
x => Isolated value derived from lambda
This function will be applied via lambda function
Returns list of True values
"""
try:
# Strip out all quotes
temp = x.replace("\"", "").replace("\'", "")
# Split on category and isolate responses that were marked true
race_vals = [k.split(',')[0] for k in temp.split('],') if "True" in k]
# Strip out square brackets
race_vals = [k.strip().replace('[', '').replace(']', '') for k in race_vals]
return race_vals
except:
# In the case of missing data
return None
try:
# Make sure Race column is present
check = list(DF['Race'])
# Apply isolate_race_value helper function
DF['Race'] = DF['Race'].apply(lambda x: isolate_race_value(x))
except:
# If key doesn't exist, create empty column
DF['Race'] = [] * len(DF)
return DF
# ----- Pings
def derive_pings(SUBSET, KEY):
"""
SUBSET => Reduced dictionary containing
KEY => Key from the master JSON
This function isolates ping data from the participant's dictionary
Returns wide DataFrame object with select columns
"""
pings = pd.DataFrame(SUBSET['pings']) # Convert JSON to DataFrame
pings['username'] = KEY.split('-')[0] # Add username column
login_node = KEY.split('-')[1:]
login_node = "".join(login_node)
pings['login-node'] = login_node
return pings.loc[:, ['username', 'login-node', 'streamName', 'startTime',
'notificationTime', 'endTime', 'id', 'tzOffset']]
# ----- Concat
def agg_drop_duplicates(DF):
"""
NOTE: This helper is functional but not in use
"""
users = list(DF['username'].unique())
keepers = []
for user in tqdm(users):
temp = DF[DF['username'] == user].reset_index(drop=True)
temp = temp.drop_duplicates(subset="id", keep="first").reset_index(drop=True)
keepers.append(temp)
return pd.concat(keepers)
# ----- Run
def parse_responses(KEY, SUBSET, LOG, OUTPUT_DIR, KICKOUT):
"""
KEY => Key from the master data dictionary
SUBSET => Reduced dictionary of participant-only data
LOG => Text file to store errors and exceptions
OUTPUT_DIR => Relative path to output directory
KICKOUT => Boolean, if True a local CSV is saved
This function wraps everything defined above
Returns a clean DataFrame object
"""
username = KEY.split('-')[0] # Isolate username
try:
answers = derive_answers(SUBSET=SUBSET, LOG=LOG, USER=username) # Create answers DataFrame
except Exception as e:
LOG.write(f"\nCaught @ {username} + derive_answers: {e}\n\n")
try:
answers = parse_race(answers) # Isolate race responses
except Exception as e:
LOG.write(f"\nCaught @ {username} + parse_race: {e}\n\n")
try:
answers = parse_nominations(answers) # Isolate nomination responses
except Exception as e:
LOG.write(f"\nCaught @ {username} + parse_nominations: {e}\n\n")
try:
pings = derive_pings(SUBSET=SUBSET, KEY=KEY) # Create pings DataFrame
except Exception as e:
LOG.write(f"\nCaught @ {username} + derive_pings: {e}\n\n")
# Isolate a few device parameters to include in pings CSV
# The exhaustive device info is in another CSV in the same directory
devices = pd.DataFrame(SUBSET['user']['installation']['device'], index=[0])
devices['username'] = username
pings = pings.merge(devices, on="username")
return output(KEY, pings, answers, OUTPUT_DIR, KICKOUT)