forked from miriamkw/LoopAlgorithmToPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
152 lines (96 loc) · 5.15 KB
/
api.py
File metadata and controls
152 lines (96 loc) · 5.15 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
"""
This file provides an API for calling the functions in the dynamic library. These functions are c-embeddings
for swift functions, found in Sources/LoopAlgorithmToPython/LoopAlgorithmToPython.swift.
"""
from loop_to_python_api.helpers import get_bytes_from_json
import ctypes
import os
# swift_lib = ctypes.CDLL('python_api/libLoopAlgorithmToPython.dylib')
current_dir = os.path.dirname(os.path.abspath(__file__))
lib_path = os.path.join(current_dir, 'libLoopAlgorithmToPython.dylib')
swift_lib = ctypes.CDLL(lib_path)
# This function helps with providing more informative error messages if the code fails
def initialize_exception_handlers():
# Define the function prototypes
initializeExceptionHandler = swift_lib.initializeExceptionHandler
initializeExceptionHandler.restype = None
initializeSignalHandlers = swift_lib.initializeSignalHandlers
initializeSignalHandlers.restype = None
# Initialize the exception handler and signal handlers
initializeExceptionHandler()
initializeSignalHandlers()
def generate_prediction(json_file, len=72):
json_bytes = get_bytes_from_json(json_file)
swift_lib.generatePrediction.argtypes = [ctypes.c_char_p]
swift_lib.generatePrediction.restype = ctypes.POINTER(ctypes.c_double)
result = swift_lib.generatePrediction(json_bytes)
return [result[i] for i in range(len)]
def get_prediction_dates(json_file):
json_bytes = get_bytes_from_json(json_file)
swift_lib.getPredictionDates.argtypes = [ctypes.c_char_p]
swift_lib.getPredictionDates.restype = ctypes.c_char_p
result = swift_lib.getPredictionDates(json_bytes).decode('utf-8')
date_list = result.split(',')[:-1]
#date_list = [pd.to_datetime(date) for date in date_list]
return date_list
def get_prediction_values_and_dates(json_file):
dates = get_prediction_dates(json_file)
values = generate_prediction(json_file, len(dates))
return values, dates
# "Glucose effect velocity" is equivalent to insulin counteraction effect (ICE)
def get_glucose_effect_velocity(json_file, len=72):
json_bytes = get_bytes_from_json(json_file)
swift_lib.getGlucoseEffectVelocity.argtypes = [ctypes.c_char_p]
swift_lib.getGlucoseEffectVelocity.restype = ctypes.POINTER(ctypes.c_double)
result = swift_lib.getGlucoseEffectVelocity(json_bytes)
return [result[i] for i in range(len)]
def get_glucose_effect_velocity_dates(json_file):
json_bytes = get_bytes_from_json(json_file)
swift_lib.getGlucoseEffectVelocityDates.argtypes = [ctypes.c_char_p]
swift_lib.getGlucoseEffectVelocityDates.restype = ctypes.c_char_p
result = swift_lib.getGlucoseEffectVelocityDates(json_bytes).decode('utf-8')
date_list = result.split(',')[:-1]
#date_list = [pd.to_datetime(date) for date in date_list]
return date_list
def get_glucose_velocity_values_and_dates(json_file):
# TODO: Add validation of json dates here to be more flexible?
dates = get_glucose_effect_velocity_dates(json_file)
values = get_glucose_effect_velocity(json_file, len(dates))
return values, dates
def get_active_carbs(json_file):
json_bytes = get_bytes_from_json(json_file)
swift_lib.getActiveCarbs.argtypes = [ctypes.c_char_p]
swift_lib.getActiveCarbs.restype = ctypes.c_double
return swift_lib.getActiveCarbs(json_bytes)
def get_active_insulin(json_file):
json_bytes = get_bytes_from_json(json_file)
swift_lib.getActiveInsulin.argtypes = [ctypes.c_char_p]
swift_lib.getActiveInsulin.restype = ctypes.c_double
return swift_lib.getActiveInsulin(json_bytes)
def get_loop_recommendations(json_file, len=72):
json_bytes = get_bytes_from_json(json_file)
swift_lib.getLoopRecommendations.argtypes = [ctypes.c_char_p]
swift_lib.getLoopRecommendations.restype = ctypes.c_char_p
return swift_lib.getLoopRecommendations(json_bytes)
# Calculating the percentage of carbohydrate absorption at the percent time, with piecewise linear model
# Input is percent as fraction
def percent_absorption_at_percent_time(percent_time):
swift_lib.percentAbsorptionAtPercentTime.argtypes = [ctypes.c_double]
swift_lib.percentAbsorptionAtPercentTime.restype = ctypes.c_double
return swift_lib.percentAbsorptionAtPercentTime(percent_time)
# Calculating the percentage rate of carbohydrate absorption at the percent time, with piecewise linear model
# Input is percent as fraction
def piecewise_linear_percent_rate_at_percent_time(percent_time):
swift_lib.percentRateAtPercentTime.argtypes = [ctypes.c_double]
swift_lib.percentRateAtPercentTime.restype = ctypes.c_double
return swift_lib.percentRateAtPercentTime(percent_time)
# Input is percent as fraction
def linear_percent_rate_at_percent_time(percent_time):
swift_lib.linearPercentRateAtPercentTime.argtypes = [ctypes.c_double]
swift_lib.linearPercentRateAtPercentTime.restype = ctypes.c_double
return swift_lib.linearPercentRateAtPercentTime(percent_time)
def get_dynamic_carbs_on_board(json_file):
json_bytes = get_bytes_from_json(json_file)
swift_lib.getDynamicCarbsOnBoard.argtypes = [ctypes.c_char_p]
swift_lib.getDynamicCarbsOnBoard.restype = ctypes.c_double
return swift_lib.getDynamicCarbsOnBoard(json_bytes)