Skip to content

Commit da34ae4

Browse files
committed
Added active carbohydrate and active insulin function
1 parent f3b7b6a commit da34ae4

2 files changed

Lines changed: 72 additions & 37 deletions

File tree

README.md

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ swift_lib = ctypes.CDLL('./libLoopAlgorithmToPython.dylib')
4747
swift_lib.generatePrediction.argtypes = [ctypes.c_char_p]
4848
swift_lib.generatePrediction.restype = ctypes.POINTER(ctypes.c_double)
4949
50+
swift_lib.getPredictionDates.argtypes = [ctypes.c_char_p]
51+
swift_lib.getPredictionDates.restype = ctypes.c_char_p
52+
53+
swift_lib.getActiveCarbs.argtypes = [ctypes.c_char_p]
54+
swift_lib.getActiveCarbs.restype = ctypes.c_double
55+
56+
swift_lib.getActiveInsulin.argtypes = [ctypes.c_char_p]
57+
swift_lib.getActiveInsulin.restype = ctypes.c_double
58+
5059
# Read JSON file
5160
def read_json_file(file_path):
5261
with open(file_path, 'r') as f:
@@ -60,22 +69,26 @@ json_bytes = json_str.encode('utf-8') # Convert JSON string to bytes
6069
# Prepare a variable to receive the length of the predicted values
6170
length = 82
6271
63-
# Call the Swift function
64-
result = swift_lib.generatePrediction(json_bytes)
72+
# Call the Swift functions
73+
result_prediction_values = swift_lib.generatePrediction(json_bytes)
74+
result_prediction_dates = swift_lib.getPredictionDates(json_bytes).decode('utf-8')
75+
result_active_carbs = swift_lib.getActiveCarbs(json_bytes)
76+
result_active_insulin = swift_lib.getActiveInsulin(json_bytes)
6577
6678
# Read the generated predictions
67-
array = [result[i] for i in range(length)]
79+
array = [result_prediction_values[i] for i in range(length)]
6880
print(array[0])
6981
print(f"The result from generatePrediction is: {array}")
7082
71-
# Specify the argument types and return type of the prediction dates
72-
swift_lib.getPredictionDates.argtypes = [ctypes.c_char_p]
73-
swift_lib.getPredictionDates.restype = ctypes.c_char_p
74-
75-
# Call the Swift function
76-
result = swift_lib.getPredictionDates(json_bytes).decode('utf-8')
83+
# Read the dates
7784
date_list = result.split(',')[:-1]
7885
print(f"The result from getPredictionDates is: {date_list}")
86+
87+
# Read the active carbohydrates
88+
print(f"The result from getActiveCarbs is: {result_active_carbs}")
89+
90+
# Read the active insulin
91+
print(f"The result from getActiveInsulin is: {result_active_insulin}")
7992
```
8093

8194
Adjust the paths, function names, and details as per your specific project setup and requirements.

Sources/LoopAlgorithmToPython/LoopAlgorithmToPython.swift

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,13 @@ import HealthKit
1111

1212
@_cdecl("generatePrediction") // Use @_cdecl to expose the function with a C-compatible name
1313
public func generatePrediction(jsonData: UnsafePointer<Int8>?) -> UnsafeMutablePointer<Double> {
14-
/// To generate a lib file, run:
15-
/// swiftc -emit-library -o libMySwiftModule.dylib Sources/LoopAlgorithmToPython/LoopAlgorithmToPython.swift
16-
1714
// TODO: Add opportunity to get prediction effects from only one factor at a time
18-
let decoder = JSONDecoder()
19-
decoder.dateDecodingStrategy = .iso8601
20-
21-
guard let jsonData = jsonData else {
22-
fatalError("No JSON data provided")
23-
}
2415

25-
// Convert JSON data to Data
26-
let data = Data(bytes: jsonData, count: strlen(jsonData))
16+
let data = getDataFromJson(jsonData: jsonData)
2717

2818
do {
2919
// Decode JSON data
30-
let input = try decoder.decode(LoopPredictionInput.self, from: data)
20+
let input = try getDecoder().decode(LoopPredictionInput.self, from: data)
3121

3222
let prediction = LoopAlgorithm.generatePrediction(
3323
start: input.glucoseHistory.last?.startDate ?? Date(),
@@ -55,24 +45,12 @@ public func generatePrediction(jsonData: UnsafePointer<Int8>?) -> UnsafeMutableP
5545
}
5646
}
5747

58-
@_cdecl("getPredictionDates") // Use @_cdecl to expose the function with a C-compatible name
48+
@_cdecl("getPredictionDates")
5949
public func getPredictionDates(jsonData: UnsafePointer<Int8>?) -> UnsafePointer<CChar> {
60-
/// To generate a lib file, run:
61-
/// swiftc -emit-library -o libMySwiftModule.dylib Sources/LoopAlgorithmToPython/LoopAlgorithmToPython.swift
62-
63-
let decoder = JSONDecoder()
64-
decoder.dateDecodingStrategy = .iso8601
65-
66-
guard let jsonData = jsonData else {
67-
fatalError("No JSON data provided")
68-
}
69-
70-
// Convert JSON data to Data
71-
let data = Data(bytes: jsonData, count: strlen(jsonData))
72-
50+
let data = getDataFromJson(jsonData: jsonData)
51+
7352
do {
74-
// Decode JSON data
75-
let input = try decoder.decode(LoopPredictionInput.self, from: data)
53+
let input = try getDecoder().decode(LoopPredictionInput.self, from: data)
7654

7755
let prediction = LoopAlgorithm.generatePrediction(
7856
start: input.glucoseHistory.last?.startDate ?? Date(),
@@ -99,3 +77,47 @@ public func getPredictionDates(jsonData: UnsafePointer<Int8>?) -> UnsafePointer<
9977
}
10078

10179

80+
@_cdecl("getActiveCarbs")
81+
public func getActiveCarbs(jsonData: UnsafePointer<Int8>?) -> Double {
82+
let data = getDataFromJson(jsonData: jsonData)
83+
84+
do {
85+
let input = try getDecoder().decode(AlgorithmInputFixture.self, from: data)
86+
let output = LoopAlgorithm.run(input: input)
87+
88+
return output.activeCarbs!
89+
} catch {
90+
fatalError("Error reading or decoding JSON file: \(error)")
91+
}
92+
}
93+
94+
95+
@_cdecl("getActiveInsulin")
96+
public func getActiveInsulin(jsonData: UnsafePointer<Int8>?) -> Double {
97+
let data = getDataFromJson(jsonData: jsonData)
98+
99+
do {
100+
let input = try getDecoder().decode(AlgorithmInputFixture.self, from: data)
101+
let output = LoopAlgorithm.run(input: input)
102+
103+
return output.activeInsulin!
104+
} catch {
105+
fatalError("Error reading or decoding JSON file: \(error)")
106+
}
107+
}
108+
109+
func getDataFromJson(jsonData: UnsafePointer<Int8>?) -> Data {
110+
guard let jsonData = jsonData else {
111+
fatalError("No JSON data provided")
112+
}
113+
// Convert JSON data to Data
114+
let data = Data(bytes: jsonData, count: strlen(jsonData))
115+
return data
116+
}
117+
118+
func getDecoder() -> JSONDecoder {
119+
let decoder = JSONDecoder()
120+
decoder.dateDecodingStrategy = .iso8601
121+
return decoder
122+
}
123+

0 commit comments

Comments
 (0)