Skip to content

Commit db938f9

Browse files
committed
Started adding functions to the API that work
1 parent 661e12a commit db938f9

6 files changed

Lines changed: 996 additions & 7 deletions

File tree

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ Adjust the paths, function names, and details as per your specific project setup
101101

102102

103103
To do:
104-
- [ ] add a python folder
105-
- [ ] add a test folder
106-
- [ ] add the dlib build script
104+
- [X] add a python folder
105+
- [X] add a test folder
106+
- [X] add the dlib build script
107107
- build to python folder
108108
- add readme
109109
- [ ] write python functions api
@@ -116,10 +116,13 @@ To do:
116116
- [ ] clean up code python
117117
- [ ] update readme with new changes
118118
- explanation, separating between python and swift code
119+
- example usage of the api (with signal handlers)
120+
- refer to example input files
119121
- build, venv, run commands
120122
- pypi
121123
- running tests
122124
- [ ] merge to main
125+
- [ ] next project: take a df as input, convert to json (or do it in tidepool study?)
123126

124127

125128

Sources/LoopAlgorithmToPython/LoopAlgorithmToPython.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -439,10 +439,6 @@ extension ISO8601DateFormatter {
439439

440440

441441

442-
443-
444-
445-
446442
// Copied code because the struct was not public
447443
struct LinearAbsorption: CarbAbsorptionComputable {
448444
func percentAbsorptionAtPercentTime(_ percentTime: Double) -> Double {

python_api/helpers.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"""
2+
Helper functions not directly related to the API.
3+
"""
4+
import json
5+
6+
7+
def get_bytes_from_json(json_file):
8+
json_str = json.dumps(json_file) # Convert JSON data to JSON string
9+
json_bytes = json_str.encode('utf-8') # Convert JSON string to bytes
10+
return json_bytes
11+

python_api/python_api.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""
2+
This file provides an API for calling the functions in the dynamic library. These functions are c-embeddings
3+
for swift functions, found in Sources/LoopAlgorithmToPython/LoopAlgorithmToPython.swift.
4+
"""
5+
from helpers import get_bytes_from_json
6+
7+
import ctypes
8+
import json
9+
10+
11+
swift_lib = ctypes.CDLL('python_api/libLoopAlgorithmToPython.dylib')
12+
13+
14+
# This function helps with providing more informative error messages if the code fails
15+
def initialize_exception_handlers():
16+
# Define the function prototypes
17+
initializeExceptionHandler = swift_lib.initializeExceptionHandler
18+
initializeExceptionHandler.restype = None
19+
20+
initializeSignalHandlers = swift_lib.initializeSignalHandlers
21+
initializeSignalHandlers.restype = None
22+
23+
# Initialize the exception handler and signal handlers
24+
initializeExceptionHandler()
25+
initializeSignalHandlers()
26+
27+
28+
def generate_prediction(json_file, len=82):
29+
json_bytes = get_bytes_from_json(json_file)
30+
31+
swift_lib.generatePrediction.argtypes = [ctypes.c_char_p]
32+
swift_lib.generatePrediction.restype = ctypes.POINTER(ctypes.c_double)
33+
34+
result = swift_lib.generatePrediction(json_bytes)
35+
result_array = [result[i] for i in range(len)]
36+
37+
return result_array
38+
39+
40+
41+
42+
43+
44+
45+
46+
47+
48+
with open('python_tests/test_files/generate_prediction_input.json', 'r') as f:
49+
json_file = json.load(f)
50+
51+
52+
initialize_exception_handlers()
53+
res = generate_prediction(json_file)
54+
55+
print(res)

python_tests/python_tests.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import pytest
2+
3+
4+

0 commit comments

Comments
 (0)