Skip to content
238 changes: 232 additions & 6 deletions projects/parsers/loop_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ def __parse(self, path, file_name) -> dict:
if Sections.DOSE_STORE in dict:
try:
dose_store = dict[Sections.DOSE_STORE]


basal_profile = json.loads(
dose_store["basalProfile"]
.replace("[", "{")
Expand All @@ -173,8 +175,124 @@ def __parse(self, path, file_name) -> dict:
).group(1)
)


substr = dose_store["basalProfileApplyingOverrideHistory"]
value = "timeZone";
start_index = substr.index(value)
value_temp = substr[start_index:]
value_temp = value_temp.replace('"', '')
last_index = 0
if value_temp.find(",") == -1:
last_index = value_temp.index("]")
else:
last_index = value_temp.find(",")
loop_report_dict["basalProfileApplyingOverrideHistory_timeZone"] = value_temp[len(
value) + 1:last_index]



value = "items";
substr = dose_store["basalProfileApplyingOverrideHistory"]
start_index = substr.index(value)
value_temp = substr[start_index:]
last_index = value_temp.index("]]")
items_val = '{"items": [' + value_temp[len(value) + 2:last_index + 2].replace(" ", "") \
.replace("(", "{").replace("]", "}").replace("[", "{").replace("{{", "{").replace("}}", "}") + ']}'
loop_report_dict["basalProfileApplyingOverrideHistory_items"] = json.loads(items_val)[
"items"]



except:
logger.debug("handled error dose store")
if Sections.CARB_STORE in dict:
try:
"""
["timeZone": -25200, "items": [["startTime": 0.0, "value": 60.0], [
"startTime": 23400.0, "value": 40.0], [
"value": 66.66666666666667, "startTime": 28369.19808101654], [
"startTime": 35569.19808101654, "value": 40.0], [
"value": 80.0, "startTime": 50400.0]], "unit": "mg/dL"]

["timeZone": -25200, "unit": "mg/dL",
"items": [["startTime": 0.0, "value": 45.0], ["value": 45.0, "startTime": 16200.0], [
"startTime": 32400.0, "value": 55.0]]]
"""

carb_store = dict[Sections.CARB_STORE]
###insulinSensitivityScheduleApplyingOverrideHistory###
substr = carb_store["insulinSensitivityScheduleApplyingOverrideHistory"]
value = "timeZone";
start_index = substr.index(value)
value_temp = substr[start_index:]
value_temp = value_temp.replace('"', '')
last_index = 0
if value_temp.find(",") == -1:
last_index = value_temp.index("]")
else:
last_index = value_temp.find(",")

loop_report_dict["insulinSensitivityScheduleApplyingOverrideHistory_timeZone"] = value_temp[len(value) + 1:last_index]

value = "unit";
substr = carb_store["insulinSensitivityScheduleApplyingOverrideHistory"]
start_index = substr.index(value)
value_temp = substr[start_index:]
value_temp = value_temp.replace('"', '')
last_index = value_temp.index("]")
if last_index > 12:
last_index = value_temp.index(",")
loop_report_dict["insulinSensitivityScheduleApplyingOverrideHistory_units"] = value_temp[len(value) + 1:last_index]

value = "items";
start_index = substr.index(value)
value_temp = substr[start_index:]
last_index = value_temp.index("]]")
items_val = '{"items": [' + value_temp[len(value) + 2:last_index+2].replace(" ", "")\
.replace("(", "{").replace("]", "}").replace("[", "{").replace("{{", "{").replace("}}", "}") + ']}'
loop_report_dict["insulinSensitivityScheduleApplyingOverrideHistory_items"] =json.loads(items_val)["items"]


####carbRatioScheduleApplyingOverrideHistory###
substr = carb_store["carbRatioScheduleApplyingOverrideHistory"]
value = "timeZone";
start_index = substr.index(value)
value_temp = substr[start_index:]
value_temp = value_temp.replace('"', '')
last_index = 0
if value_temp.find(",") == -1:
last_index = value_temp.index("]")
else:
last_index = value_temp.find(",")

loop_report_dict["carbRatioScheduleApplyingOverrideHistory_timeZone"] = value_temp[len(
value) + 1:last_index]

value = "unit";
substr = carb_store["carbRatioScheduleApplyingOverrideHistory"]
start_index = substr.index(value)
value_temp = substr[start_index:]
value_temp = value_temp.replace('"', '')
last_index = value_temp.index("]")
if last_index > 12:
last_index = value_temp.index(",")
loop_report_dict["carbRatioScheduleApplyingOverrideHistory_units"] = value_temp[len(value) + 1:last_index]

value = "items";
substr = carb_store["carbRatioScheduleApplyingOverrideHistory"]
start_index = substr.index(value)
value_temp = substr[start_index:]
last_index = value_temp.index("]]")
items_val = '{"items": [' + value_temp[len(value) + 2:last_index + 2].replace(" ", "") \
.replace("(", "{").replace("]", "}").replace("[", "{").replace("{{", "{").replace("}}", "}") + ']}'
loop_report_dict["carbRatioScheduleApplyingOverrideHistory_items"] = json.loads(items_val)[
"items"]



except Exception as error:

logger.debug("handled error dose store")

minimed_pump_manager = None
omnipod_pump_manager = None
Expand Down Expand Up @@ -411,12 +529,30 @@ def __parse(self, path, file_name) -> dict:
if temp:
loop_report_dict["retrospective_correction_enabled"] = temp.group(1)

loop_report_dict["suspend_threshold"] = float(
re.search(
r"Loop.GlucoseThreshold\(value: (.+?), unit",
loop_data_manager["settings"],
).group(1)
)
try:
loop_report_dict["suspend_threshold"] = float(
re.search(
r"Loop.GlucoseThreshold\(value: (.+?), unit",
loop_data_manager["settings"],
).group(1)

)
except Exception as e:
logger.debug("handled error LOOP_DATA_MANAGER - suspend_threshold_unit - Loop.GlucoseThreshold miss")
logger.debug(e)

try:

if "suspend_threshold" not in loop_report_dict or loop_report_dict["suspend_threshold"] is None or loop_report_dict["suspend_threshold"] == "":
loop_report_dict["suspend_threshold"] = float(
re.search(
r"LoopCore.GlucoseThreshold\(value: (.+?), unit",
loop_data_manager["settings"],
).group(1)
)
except Exception as e:
logger.debug("handled error LOOP_DATA_MANAGER - suspend_threshold_unit - LoopCore.GlucoseThreshold miss")
logger.debug(e)
except Exception as e:
logger.debug("handled error LOOP_DATA_MANAGER - retrospective_correction_enabled")
logger.debug(e)
Expand Down Expand Up @@ -518,6 +654,27 @@ def __parse(self, path, file_name) -> dict:
logger.debug("preMeal is not in loop data")
logger.debug(e)


try:
unit = substr.index("unit")

start_index = unit + 8
end_index = start_index
check = ""

while check != '"':
end_index += 1
check = substr[end_index]

override_units = substr[start_index : end_index]

loop_report_dict["override_units"] = override_units

except Exception as e:
logger.debug("override_units is not in loop data")
logger.debug(e)


except Exception as e:
logger.debug("handled error loop data manager")
logger.debug(e)
Expand Down Expand Up @@ -1338,6 +1495,75 @@ def __parse(self, path, file_name) -> dict:
logger.debug("handled error CACHED_GLUCOSE_SAMPLES")
logger.debug(e)

if Sections.G4_CGM_MANAGER in dict:
try:
temp_dict = dict[Sections.G4_CGM_MANAGER]
dictionary_complete = {}
if "receiver" in temp_dict:
dictionary_complete["receiver"] = temp_dict["receiver"]

if "providesBLEHeartbeat" in temp_dict:
dictionary_complete["providesBLEHeartbeat"] = temp_dict["providesBLEHeartbeat"]

if "latestBackfill" in temp_dict:
dictionary_complete["latestBackfill"] = temp_dict["latestBackfill"]

latestReading_dict = temp_dict["latestReading"]
latestReading_dict = latestReading_dict.replace(
"Optional(G4ShareSpy.GlucoseG4(",
"",
)
latestReading_dict = latestReading_dict.replace("))", "")
split_list = latestReading_dict.split(",")
dictionary = {}
latestReading = {}
for item in split_list:
item = item.replace(")", "")
keyvalue = item.split(":")
m = keyvalue[0].strip("'")
m = m.replace('"', "").strip()
dictionary[m] = keyvalue[1].strip("\"'")

if "sequence" in dictionary:
latestReading["sequence"] = dictionary["sequence"]

if "glucose" in dictionary:
latestReading["glucose"] = dictionary["glucose"]

if "isDisplayOnly" in dictionary:
latestReading["isDisplayOnly"] = dictionary["isDisplayOnly"]

if "trend" in dictionary:
latestReading["trend"] = dictionary["trend"]

if "time" in dictionary:
latestReading["time"] = dictionary["time"]

if "wallTime" in dictionary:
latestReading["wallTime"] = dictionary["wallTime"]

if "systemTime" in dictionary:
latestReading["systemTime"] = dictionary["systemTime"]

dictionary_complete["latestReading"] = latestReading

loop_report_dict["g4_cgm_manager"] = dictionary_complete

except Exception as e:
logger.debug("handled error G4_CGM_MANAGER")
logger.debug(e)

if Sections.INTEGRAL_RETROSPECTIVE_CORRECTION in dict:
try:
local_list = dict[Sections.INTEGRAL_RETROSPECTIVE_CORRECTION]
loop_report_dict["integral_retrospective_correction"] = local_list
except Exception as e:
logger.debug("handled error G4_CGM_MANAGER")
logger.debug(e)




return loop_report_dict

def add_to_dictionary(self, dictionary, item):
Expand Down
22 changes: 21 additions & 1 deletion projects/parsers/loop_report_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"""
import os


class Sections:
G5_CGM_MANAGER = "g5_cgm_manager"
DEX_CGM_MANAGER = "dex_cgm_manager"
Expand Down Expand Up @@ -48,6 +47,8 @@ class Sections:
GET_NORMALIZED_DOSE_ENTRIES = "get_normalized_dose_entries"
CACHED_DOSE_ENTRIES = "cached_dose_entries"
STATUS_EXTENSION_DATA_MANAGER = "status_extension_data_manager"
INTEGRAL_RETROSPECTIVE_CORRECTION = "integral_retrospective_correction"
G4_CGM_MANAGER = "g4_cgm_manager"

"""
#not sure this one is used
Expand Down Expand Up @@ -323,6 +324,18 @@ def parse_loop_report(path: str, file_name: str):
all_sections["g6_cgm_manager"] = g6_cgm_manager
new_line = False

elif line.startswith("## G4CGMManager"):
g4_cgm_manager = {}
current_section = "g4_cgm_manager"
all_sections["g4_cgm_manager"] = g4_cgm_manager
new_line = False

elif line.startswith("## IntegralRetrospectiveCorrection"):
integral_retrospective_correction = {}
current_section = "integral_retrospective_correction"
all_sections["integral_retrospective_correction"] = integral_retrospective_correction
new_line = False

elif line.startswith("## ShareClientManager"):
share_client_manager = {}
current_section = "share_client_manager"
Expand Down Expand Up @@ -407,6 +420,10 @@ def parse_loop_report(path: str, file_name: str):
and current_section == Sections.LOOP_DATA_MANAGER
):
one = "one"
elif(line.startswith("* basalProfileApplyingOverrideHistory")):
dict = all_sections[current_section]
dict["basalProfileApplyingOverrideHistory"] = line.replace("* basalProfileApplyingOverrideHistory", "")

elif current_section:
new_line = False
dict = all_sections[current_section]
Expand All @@ -426,6 +443,9 @@ def parse_loop_report(path: str, file_name: str):
return all_sections





def parse_key_value(all_sections, line):
dict = all_sections["loop_data_manager"]
key, value = _split_key_value(line, ":")
Expand Down
Loading