[8d2107]: / loader.py

Download this file

115 lines (101 with data), 4.1 kB

  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
import json
import os
patients_path = '/PHShome/ju601/crt/data/json/'
#patients_path = '/PHShome/ju601/crt/data/MGHpallcare/json/'
# Deprecated
#loads old annonomyzed data from JSON file into nested dictionary
def get_old_data():
with open('data/anon_data_OLD.json', 'r') as f:
data = json.load(f)
return data
# Gets a specific patient by their EMPI
def get_patient_by_EMPI(empi):
file_path = patients_path + empi + '.json'
if os.path.isfile(file_path):
with open(file_path, 'r') as f:
patient = json.load(f)
return patient
else:
raise ValueError("Patient with EMPI: " + empi + " does not exist")
def save(patient):
file_path = patients_path + patient['EMPI'] + '.json'
if os.path.isfile(file_path):
with open(file_path, 'w') as f:
json.dump(patient, f)
return patient
else:
raise ValueError("Patient with EMPI: " + empi + " does not exist")
def get_data(patient_range=range(0,10)):
if len(patient_range) > 50:
raise ValueError("You've attempted to load too many patients into memory at once.")
files = os.listdir(patients_path)
# Sort by number order
# Changed for pal care
#files = sorted(files, key=lambda x: int(x.split('.json')[0].split('_')[2]))
files = sorted(files, key=lambda x: int(x.split('.json')[0]))
# Get all patients in patient range
patients = []
for i in patient_range:
if i < len(files):
file_path = patients_path + files[i]
with open(file_path, 'r') as f:
try:
patient = json.load(f)
patients.append(patient)
except Exception as e:
print "Error loading file: " + file_path
raise e
return patients
def get_dummy_non_anonymized_patient():
return {'First_Name':'Josh', 'Last_Name':'Haimson', 'EMPI':'1234emPI', 'NEW_EMPI':'FAKE_EMPI_1', 'MRNS':['mrn12','mrn34']}
#Removes empty fields from data
#Recursive, modifies object, returns nothing
def clean_data(data):
if type(data) == type(dict()):
for k in data.keys():
clean_data(data[k])
if data[k] in [u'', [], "", dict(), None]:
data.pop(k)
elif type(data) == type([]):
for i in range(len(data)):
clean_data(data[i])
#little function to help with exploring the data from commandline
def explore(data):
if type(data) == type(dict()) or (type(data) == type([]) and len(data)>1):
again = True
while again:
if type(data) == type(dict()):
keys = data.keys()
for i in range(len(keys)):
size = ""
if type(data[keys[i]]) == type([]):
if len(data[keys[i]]) > 1:
size = " (" + str(len(data[keys[i]])) + ")"
print str(i+1) + ".\t", keys[i], size
inp = unicode("-1")
while not(inp == "" or inp in keys or (inp.isnumeric() and ( int(inp) > 0 and int(inp) <= len(keys)))):
inp = unicode(raw_input("Select a key: "))
print ""
if inp != "" and inp.isnumeric():
print keys[int(inp)-1], ": "
explore(data[keys[int(inp)-1]])
elif inp == "":
again = False
else:
print inp, ": "
explore(data[inp])
else:
inp = unicode("-1")
while not(inp == "" or (inp.isnumeric() and int(inp) > 0 and int(inp) <= len(data))):
inp = unicode(raw_input("Select a file from 1-" + str(len(data)) +": "))
if inp != "":
explore(data[int(inp)-1])
else:
again = False
elif type(data) == type([]):
explore(data[0])
else:
print data.strip("\n")
print
inp = raw_input("[ Press enter to continue ]")
print