Skip to content

Commit 65ee7bb

Browse files
committed
Added api + example for all functions
1 parent 309f4c1 commit 65ee7bb

4 files changed

Lines changed: 1340 additions & 30 deletions

File tree

Sources/LoopAlgorithmToPython/LoopAlgorithmToPython.swift

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -232,24 +232,11 @@ public func getDynamicCarbsOnBoard(jsonData: UnsafePointer<Int8>?) -> Double {
232232

233233
let inputICE = loadICEInputFixture(from: input.inputICE)
234234
let carbEntries = loadCarbEntryFixture(from: encodeCarbValuesToJsonData(carbValues: input.carbEntries)!)
235-
print("carbentries ok")
236-
print(input.inputICE[0].startAt)
237-
238-
// Parse the date string
239-
if let date = dateFormatter.date(from: input.inputICE[0].startAt) {
240-
print("Parsed date: \(date)")
241-
} else {
242-
print("Failed to parse date string.")
243-
}
244235

245236
let startDate = dateFormatter.date(from: input.inputICE[0].startAt)!
246-
print("startdate ok")
247237
let endDate = dateFormatter.date(from: input.inputICE.last!.startAt)!
248-
print("enddate ok")
249238
let carbRatio = [AbsoluteScheduleValue(startDate: startDate, endDate: endDate, value: input.carbRatio)]
250-
print("carbratio ok")
251239
let isf = [AbsoluteScheduleValue(startDate: startDate, endDate: endDate, value: HKQuantity(unit: HKUnit(from: "mg/dL"), doubleValue: input.sensitivity))]
252-
print("isf ok")
253240

254241
let statuses = [carbEntries[0]].map(
255242
to: inputICE,
@@ -258,23 +245,14 @@ public func getDynamicCarbsOnBoard(jsonData: UnsafePointer<Int8>?) -> Double {
258245
initialAbsorptionTimeOverrun: 2.0,
259246
absorptionModel: PiecewiseLinearAbsorption()
260247
)
261-
print("STATUSES!")
262-
print(statuses)
263248
// TODO: Add a function for different absorbrionmodels
264-
265-
print(inputICE[0].startDate)
266-
print(inputICE[0].startDate.addingTimeInterval(TimeInterval(60*60*6)))
267-
249+
268250
// The output here is a list of CarbValues with startDate, endDate (equal to startDate), and value (ICE?)
269251
let carbsOnBoard = statuses.dynamicCarbsOnBoard(
270252
from: inputICE[0].startDate,
271253
to: inputICE[0].startDate.addingTimeInterval(TimeInterval(60*60*6)),
272254
absorptionModel: PiecewiseLinearAbsorption())
273-
274-
print("CARBS ON BOARD")
275-
print(carbsOnBoard)
276-
277-
// Do something
255+
278256
return carbsOnBoard.first?.value ?? 100.0
279257
} catch {
280258
fatalError("Error reading or decoding JSON file: \(error)")

python_api/python_api.py

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,28 +86,96 @@ def get_active_carbs(json_file):
8686
return swift_lib.getActiveCarbs(json_bytes)
8787

8888

89+
def get_active_insulin(json_file):
90+
json_bytes = get_bytes_from_json(json_file)
91+
92+
swift_lib.getActiveInsulin.argtypes = [ctypes.c_char_p]
93+
swift_lib.getActiveInsulin.restype = ctypes.c_double
94+
95+
return swift_lib.getActiveInsulin(json_bytes)
96+
97+
98+
# Calculating the percentage of carbohydrate absorption at the percent time, with piecewise linear model
99+
# Input is percent as fraction
100+
def percent_absorption_at_percent_time(percent_time):
101+
swift_lib.percentAbsorptionAtPercentTime.argtypes = [ctypes.c_double]
102+
swift_lib.percentAbsorptionAtPercentTime.restype = ctypes.c_double
103+
104+
return swift_lib.percentAbsorptionAtPercentTime(percent_time)
105+
106+
107+
# Calculating the percentage rate of carbohydrate absorption at the percent time, with piecewise linear model
108+
# Input is percent as fraction
109+
def piecewise_linear_percent_rate_at_percent_time(percent_time):
110+
swift_lib.percentRateAtPercentTime.argtypes = [ctypes.c_double]
111+
swift_lib.percentRateAtPercentTime.restype = ctypes.c_double
112+
113+
return swift_lib.percentRateAtPercentTime(percent_time)
114+
115+
116+
# Input is percent as fraction
117+
def linear_percent_rate_at_percent_time(percent_time):
118+
swift_lib.linearPercentRateAtPercentTime.argtypes = [ctypes.c_double]
119+
swift_lib.linearPercentRateAtPercentTime.restype = ctypes.c_double
120+
121+
return swift_lib.linearPercentRateAtPercentTime(percent_time)
122+
123+
124+
def get_dynamic_carbs_on_board(json_file):
125+
json_bytes = get_bytes_from_json(json_file)
126+
127+
swift_lib.getDynamicCarbsOnBoard.argtypes = [ctypes.c_char_p]
128+
swift_lib.getDynamicCarbsOnBoard.restype = ctypes.c_double
129+
130+
return swift_lib.getDynamicCarbsOnBoard(json_bytes)
131+
132+
89133

90134

91135
# THIS IS FOR TESTING, REMOVE WHEN DONE!
92136

93137
with open('python_tests/test_files/generate_prediction_input.json', 'r') as f:
94-
json_file = json.load(f)
138+
prediction_input = json.load(f)
139+
140+
141+
with open('python_tests/test_files/loop_algorithm_input.json', 'r') as f:
142+
loop_algorithm_input = json.load(f)
143+
144+
145+
with open('python_tests/test_files/dynamic_carbs_input.json', 'r') as f:
146+
dynamic_carbs_input = json.load(f)
95147

96148

97149
initialize_exception_handlers()
98-
prediction_values = generate_prediction(json_file)
150+
prediction_values = generate_prediction(prediction_input)
99151
print("prediction values", prediction_values)
100152
print(" ")
101-
prediction_dates = get_prediction_dates(json_file)
153+
prediction_dates = get_prediction_dates(prediction_input)
102154
print("prediction dates", prediction_dates)
103155
print(" ")
104-
glucose_effect_velocity = get_glucose_effect_velocity(json_file)
156+
glucose_effect_velocity = get_glucose_effect_velocity(prediction_input)
105157
print("glucose_effect_velocity", glucose_effect_velocity)
106158
print(" ")
107-
glucose_effect_velocity_dates = get_glucose_effect_velocity_dates(json_file)
159+
glucose_effect_velocity_dates = get_glucose_effect_velocity_dates(prediction_input)
108160
print("glucose_effect_velocity_dates", glucose_effect_velocity_dates)
109161
print(" ")
110-
active_carbs = get_active_carbs(json_file)
162+
active_carbs = get_active_carbs(loop_algorithm_input)
111163
print("active_carbs", active_carbs)
112164
print(" ")
165+
active_insulin = get_active_insulin(loop_algorithm_input)
166+
print("active_insulin", active_insulin)
167+
print(" ")
168+
percent_absorption_at_percent_time = percent_absorption_at_percent_time(0.2)
169+
print("percent_absorption_at_percent_time", percent_absorption_at_percent_time)
170+
print(" ")
171+
piecewise_linear_percent_rate_at_percent_time = piecewise_linear_percent_rate_at_percent_time(0.2)
172+
print("piecewise_linear_percent_rate_at_percent_time", piecewise_linear_percent_rate_at_percent_time)
173+
print(" ")
174+
linear_percent_rate_at_percent_time = linear_percent_rate_at_percent_time(0.2)
175+
print("linear_percent_rate_at_percent_time", linear_percent_rate_at_percent_time)
176+
print(" ")
177+
dynamic_carbs_on_board = get_dynamic_carbs_on_board(dynamic_carbs_input)
178+
print("dynamic_carbs_on_board", dynamic_carbs_on_board)
179+
print(" ")
180+
113181

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"inputICE": [
3+
{
4+
"velocity": 0.0,
5+
"start_at": "2015-10-15T21:30:12",
6+
"end_at": "2015-10-15T21:35:12"
7+
},
8+
{
9+
"velocity": 5.0,
10+
"start_at": "2015-10-15T21:35:12",
11+
"end_at": "2015-10-15T21:40:12"
12+
},
13+
{
14+
"velocity": 3.0,
15+
"start_at": "2015-10-15T21:40:12",
16+
"end_at": "2015-10-15T21:45:12"
17+
},
18+
{
19+
"velocity": 2.0,
20+
"start_at": "2015-10-15T21:45:12",
21+
"end_at": "2015-10-15T21:50:12"
22+
},
23+
{
24+
"velocity": 3.0,
25+
"start_at": "2015-10-15T21:50:12",
26+
"end_at": "2015-10-15T21:55:12"
27+
},
28+
{
29+
"velocity": 5.0,
30+
"start_at": "2015-10-15T21:55:12",
31+
"end_at": "2015-10-15T22:00:12"
32+
},
33+
{
34+
"velocity": -2.0,
35+
"start_at": "2015-10-15T22:00:12",
36+
"end_at": "2015-10-15T22:05:12"
37+
}
38+
],
39+
"carbEntries": [
40+
{
41+
"grams": 44,
42+
"date": "2015-10-15T21:35:12Z",
43+
"absorptionTime": 120,
44+
"unit": "g"
45+
},
46+
{
47+
"grams": 30,
48+
"date": "2015-10-15T21:55:00Z",
49+
"absorptionTime": 120,
50+
"unit": "g"
51+
},
52+
{
53+
"grams": 30,
54+
"date": "2015-10-15T23:00:00Z",
55+
"absorptionTime": 120,
56+
"unit": "g"
57+
}
58+
],
59+
"sensitivity": 63,
60+
"carbRatio": 10.0
61+
}

0 commit comments

Comments
 (0)