@@ -11,8 +11,41 @@ def get_bytes_from_json(json_file):
1111 return json_bytes
1212
1313
14- def get_json_loop_prediction_input_from_df (data , basal , isf , cr , prediction_start , insulin_type ):
14+ def get_json_loop_prediction_input_from_df (data , basal , isf , cr , prediction_start , insulin_type = 'novolog' ,
15+ max_basal = 4.0 , max_bolus = 9 , recommendation_type = 'automaticBolus' ,
16+ suspend_threshold = 78 , target_lower = 101 , target_upper = 115 ):
17+ """
18+ Convert a glucose and insulin DataFrame into a JSON payload suitable for loop prediction input in several
19+ functions within `api.py`.
20+
21+ Args:
22+ data (pd.DataFrame): DataFrame containing the following columns:
23+ - 'CGM' (mg/dL)
24+ - 'bolus' (U)
25+ - 'basal' (U/hr)
26+ - 'carbs' (g)
27+ - 'date' (datetime)
28+ basal (float): Scheduled basal insulin rate to use in the prediction.
29+ isf (float): Insulin sensitivity factor (mg/dL per unit insulin).
30+ cr (float): Carbohydrate ratio (grams per unit insulin).
31+ prediction_start (datetime or str): Timestamp for the start of the prediction period.
32+ insulin_type (str): Type of insulin used. Must be one of:
33+ "novolog", "humalog", "apidra", "fiasp", "lyumjev", "afrezza".
34+ max_basal (float, optional): Maximum allowable basal rate in closed loop (U/hr). Defaults to 4.0.
35+ max_bolus (float, optional): Maximum allowable bolus dose (U). Defaults to 9.
36+ recommendation_type (str, optional): Type of recommendation to generate. Defaults to 'automaticBolus'.
37+ Must be one of: "automaticBolus", "tempBasal", "manualBolus".
38+ suspend_threshold (float, optional): Glucose level below which insulin delivery is suspended (mg/dL).
39+ Defaults to 78.
40+ target_lower (float, optional): Lower bound of target glucose range (mg/dL). Defaults to 101.
41+ target_upper (float, optional): Upper bound of target glucose range (mg/dL). Defaults to 115.
42+
43+ Returns:
44+ dict: JSON-serializable dictionary structured for loop prediction input,
45+ including glucose values, insulin information, and metadata.
46+ """
1547 validate_insulin_type (insulin_type )
48+ validate_recommendation_type (recommendation_type )
1649
1750 def get_dates_and_values (column , data ):
1851 if column in data .columns :
@@ -104,17 +137,17 @@ def get_dates_and_values(column, data):
104137 "sensitivity" : isf ,
105138 }
106139 # Adding other mandatory default values for recommendations
107- json_data ['maxBasalRate' ] = 4.1
108- json_data ['maxBolus' ] = 9
140+ json_data ['maxBasalRate' ] = max_basal
141+ json_data ['maxBolus' ] = max_bolus
109142 json_data ['predictionStart' ] = prediction_start .strftime ('%Y-%m-%dT%H:%M:%SZ' )
110143 json_data ['recommendationInsulinType' ] = insulin_type
111- json_data ['recommendationType' ] = "automaticBolus"
112- json_data ['suspendThreshold' ] = 78
144+ json_data ['recommendationType' ] = recommendation_type
145+ json_data ['suspendThreshold' ] = suspend_threshold
113146 json_data ['target' ] = [{
114147 "endDate" : data .index [- 1 ].strftime ('%Y-%m-%dT%H:%M:%SZ' ),
115- "lowerBound" : 101 ,
148+ "lowerBound" : target_lower ,
116149 "startDate" : data .index [0 ].strftime ('%Y-%m-%dT%H:%M:%SZ' ),
117- "upperBound" : 115
150+ "upperBound" : target_upper
118151 }]
119152 return json_data
120153
@@ -124,3 +157,9 @@ def validate_insulin_type(insulin_type):
124157 if insulin_type not in insulin_options :
125158 raise ValueError (f"Invalid insulin type: '{ insulin_type } '. Must be one of { insulin_options } ." )
126159
160+
161+ def validate_recommendation_type (recommendation_type ):
162+ recommendation_options = ["automaticBolus" , "tempBasal" , "manualBolus" ]
163+ if recommendation_type not in recommendation_options :
164+ raise ValueError (f"Invalid insulin type: '{ recommendation_type } '. Must be one of { recommendation_options } ." )
165+
0 commit comments