-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonToRoot.py
More file actions
238 lines (193 loc) · 9.06 KB
/
jsonToRoot.py
File metadata and controls
238 lines (193 loc) · 9.06 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
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
import ROOT
import sys
import time
from TimeClass import MyTime
import ImportHelpers
from ImportHelpers import branches
from ImportHelpers import dataStorageInstance as storage
import json
##
## This converts the json format of TidePool to root TTree format.
##
def main(options,args) :
# All of the setup is now done in this wrapper function
manager = ImportHelpers.ImportManager(options,args)
inputfilenames = manager.GetInputFiles()
for inputfilename in inputfilenames :
manager.ProcessFile(inputfilename,ProcessFileJSON)
# Save output
manager.Finish()
return
def ResetAddresses(bgstruct) :
# reset addresses:
for br in dir(bgstruct) :
if '__' in br :
continue
if br in branches.keys() :
setattr(bgstruct,br,branches[br].DefaultValue())
elif type(getattr(bgstruct,br)) == type(long(1)) :
setattr(bgstruct,br,0)
else :
setattr(bgstruct,br,-1)
return
def ToMgDL(value,cfactor) :
if cfactor == 1 :
return value
return int( round(value * cfactor) )
def ProcessFileJSON(inputfilename,treeDetailed,sDetailed,
treeSummary,sSummary,
basal_histograms,sensi_histograms,ric_histograms,duration_histograms,
options) :
keys = []
json_file = open(inputfilename)
data = json.load(json_file)
# Process from earliest to latest
for line in reversed(data) :
if 'deviceTime' not in line.keys() :
continue
# reset addresses:
ResetAddresses(sDetailed)
ResetAddresses(sSummary)
for j in line.keys() :
if j in keys :
continue
keys.append(j)
uTime = MyTime.TimeFromString(line['deviceTime'])
#
# Summary info (long-term)
#
sSummary.UniversalTime = uTime
sSummary.WeekOfYear = MyTime.GetWeekOfYear(uTime)
# Conversion from mmol/L to mg/dL (found in TidePool code)
cfactor = 1
if line.get('units',None) == 'mmol/L' or line.get('units',{}).get('bg',None) == 'mmol/L' :
cfactor = 18.01559
itype = line.get('type',None)
#
# Collect basal, sensitivity, insulin-carb ratio information
#
if itype == 'pumpSettings' :
timestamp = line['deviceTime']
# Some entries are incomplete (and wrong for some reason - avoid these).
if not 'basal' in line.keys() :
continue
duration = line["bolus"]["calculator"]["insulin"]["duration"]
if not duration_histograms.hist_list :
# There is only one per day.
duration_histograms.AddSettingToHistogram(timestamp,0,duration)
elif duration != duration_histograms.GetSettingFromHistogram(0) :
duration_histograms.AddSettingToHistogram(timestamp,0,duration)
for entries in line['basalSchedules']['standard'] :
start_time = entries['start'] / float(MyTime.MillisecondsInAnHour)
basal_histograms.AddSettingToHistogram(timestamp,start_time,entries['rate'])
for entries in line['insulinSensitivity'] :
start_time = entries['start'] / float(MyTime.MillisecondsInAnHour)
amount = ToMgDL(entries['amount'],cfactor)
sensi_histograms.AddSettingToHistogram(timestamp,start_time,amount)
for entries in line['carbRatio'] :
start_time = entries['start'] / float(MyTime.MillisecondsInAnHour)
ric_histograms.AddSettingToHistogram(timestamp,start_time,entries['amount'])
#
# Rewind, BGReading
#
elif itype == 'deviceEvent' and line['subType'] == 'reservoirChange' :
sSummary.Rewind = 1
elif itype == 'smbg' :
sSummary.BGReading = ToMgDL(line.get('value'),cfactor)
if sSummary.BGReading > 0 or sSummary.BWZFoodEstimate > 0 or sSummary.Rewind :
if options.summary :
treeSummary.Fill()
#
# If it's older than 4 weeks old, do not do a detailed review.
#
print '%s %d \r'%(MyTime.StringFromTime(uTime),MyTime.WeeksOld(uTime)),
if MyTime.WeeksOld(uTime) > options.ndetailed :
continue
#
# Filling the detailed info, below:
#
sDetailed.UniversalTime = uTime
sDetailed.WeekOfYear = MyTime.GetWeekOfYear(uTime)
sDetailed.DayOfWeekFromMonday = MyTime.GetDayOfWeek(uTime)
sDetailed.HourOfDayFromFourAM = MyTime.GetHourOfDay(uTime)
sDetailed.TimeOfDayFromFourAM = float(MyTime.GetTimeOfDay(uTime))
sDetailed.BGReading = sSummary.BGReading
sDetailed.Rewind = sSummary.Rewind
if itype == 'bolus' :
# to-do: handle subType
if line['subType'] == 'normal' :
sDetailed.BolusVolumeDelivered = line[line['subType']] # bolus volume Delivered? Selected?
elif line['subType'] == 'square' :
sDetailed.BolusVolumeDeliveredDelayed = line['extended'] # bolus volume Delivered? Selected?
sDetailed.ProgrammedBolusDuration = line['duration'] / MyTime.MillisecondsInAnHour
elif line['subType'] == 'dual/square':
sDetailed.BolusVolumeDelivered = line['normal']
sDetailed.BolusVolumeDeliveredDelayed = line['extended']
sDetailed.ProgrammedBolusDuration = line['duration'] / MyTime.MillisecondsInAnHour
else :
print 'Error - need to handle %s (non-normal) subType!'%(line['subType'])
print line
import sys; sys.exit()
# Food (manual input)
elif itype == 'food' :
sDetailed.BWZCarbInput = line['carbInput']
elif itype == 'wizard' :
sDetailed.BWZEstimate = line['recommended']['net']
sDetailed.BWZTargetHighBG = ToMgDL(line['bgTarget']['high'],cfactor)
sDetailed.BWZTargetLowBG = ToMgDL(line['bgTarget']['low'] ,cfactor)
sDetailed.BWZCarbRatio = line['insulinCarbRatio']
sDetailed.BWZInsulinSensitivity = ToMgDL(line['insulinSensitivity'],cfactor)
sDetailed.BWZCarbInput = line['carbInput']
sDetailed.BWZBGInput = ToMgDL(line.get('bgInput',0),cfactor)
sDetailed.BWZCorrectionEstimate = line['recommended']['correction']
sDetailed.BWZFoodEstimate = line['recommended']['carb']
if 'insulinOnBoard' in line.keys() :
sDetailed.BWZActiveInsulin = line['insulinOnBoard']
# Temp basal
elif itype == 'basal' :
if line['deliveryType'] == 'temp':
if storage.temp_basal_in_progress :
continue
sDetailed.TempBasalType = 'Percent' if line.get('percent',None) else 'Unknown'
if sDetailed.TempBasalType == 'Unknown' :
sDetailed.TempBasalAmount = round(line['rate']/float(line['suppressed']['rate']),2)
#print 'Warning - need to handle unknown (not Percent). For now, setting percent to %.2f'%(sDetailed.TempBasalAmount)
else :
sDetailed.TempBasalAmount = round(line['percent'],2)
sDetailed.TempBasalDuration = line['duration'] / MyTime.MillisecondsInAnHour
storage.temp_basal_in_progress = True
# Cancel a temp-basal-in-progress, for any non-temp basal
elif storage.temp_basal_in_progress == True :
sDetailed.TempBasalEnd = True
storage.temp_basal_in_progress = False
# In any case, cancel a suspend-in-progress
if storage.suspend_in_progress == True :
sDetailed.SuspendEnd = True
storage.suspend_in_progress = False
# Suspend (start)
elif itype == 'deviceEvent' :
if 'status' in line.keys() and line['status'] == 'suspended' :
sDetailed.SuspendStart = True
storage.suspend_in_progress = True
# Annotation
elif itype == 'annotation' :
sDetailed.annotation = str(line['annotation'])
# Exercise (manual input)
elif itype == 'exercise' :
sDetailed.ExerciseDuration = float(line['duration'])
sDetailed.ExerciseIntensity = float(line['intensity'])
treeDetailed.Fill()
if False :
print keys
return
if __name__ == '__main__' :
from optparse import OptionParser
p = OptionParser()
p.add_option('--summary' ,action='store_true',default=False,dest='summary' ,help='Make summary root file')
p.add_option('--ndetailed',type='int',default=4,dest='ndetailed',help='Number of weeks of detail (4)')
p.add_option('--outname' ,type='string',default='output_tidepool.root',dest='outname',help='Output root file name')
p.add_option('--datadir' ,type='string',default='data',dest='datadir',help='Data directory')
options,args = p.parse_args()
# We will only process Tidepool json files:
options.match_regexp = ['Tidepool_Export.*json']
main(options,args)