-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredict_glucose.py
More file actions
422 lines (356 loc) · 14 KB
/
predict_glucose.py
File metadata and controls
422 lines (356 loc) · 14 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
import joblib
import numpy as np
import pandas as pd
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
import matplotlib.pyplot as plt
import seaborn as sns
import os
import time
import sys
class AS726xSensor:
"""Class to interface with the SparkFun AS726x NIR sensor."""
# SparkFun AS726x I2C Address
AS726X_ADDR = 0x49
# Register addresses
DEVICE_TYPE = 0x00
HW_VERSION = 0x01
CONTROL_SETUP = 0x04
INT_T = 0x05
DEVICE_TEMP = 0x06
LED_CONTROL = 0x07
# Data registers (AS7263 NIR sensor)
R_G = 0x08
S_H = 0x0A
T_I = 0x0C
U_J = 0x0E
V_K = 0x10
W_L = 0x12
# Control register bits
DATA_RDY = 0x02
GAIN_1X = 0x00
GAIN_3X = 0x01
GAIN_6X = 0x02
GAIN_9X = 0x03
GAIN_18X = 0x04
# Integration time options (in ms)
INTEG_TIME = {
0: 1, # 1ms
1: 2.8, # 2.8ms
2: 5.6, # 5.6ms
3: 11.2, # 11.2ms
4: 22.4, # 22.4ms
5: 44.8, # 44.8ms
6: 89.6, # 89.6ms
7: 179.2, # 179.2ms
8: 358.4, # 358.4ms
9: 716.8, # 716.8ms
10: 1433.6 # 1433.6ms
}
def __init__(self):
"""Initialize the AS726x sensor."""
self.connected = False
self.hardware_available = False
try:
# Import hardware-specific modules only if needed
try:
import smbus2 as smbus # For SparkFun AS726x I2C communication
self.smbus = smbus
self.hardware_available = True
except ImportError:
print("smbus2 not available. Hardware sensor will not work.")
self.hardware_available = False
if not self.hardware_available:
print("AS726xSensor: Hardware modules not available, running in simulation mode.")
return
# Initialize I2C bus
self.bus = self.smbus.SMBus(1) # Using I2C bus 1 on Raspberry Pi
# Check if sensor is connected
try:
device_type = self.read_register(self.DEVICE_TYPE)
if device_type != 0x63: # AS7263 type should be 0x63
print(f"Warning: Unexpected device type: {device_type}")
except Exception as e:
print(f"Error reading device type: {e}")
raise
# Configure the sensor
self.set_gain(self.GAIN_18X) # Higher gain for better signal
self.set_integration_time(7) # 179.2ms integration time
print("SparkFun AS726x sensor initialized successfully")
self.connected = True
except Exception as e:
print(f"Failed to initialize SparkFun AS726x sensor: {e}")
self.connected = False
def read_register(self, reg_addr):
"""Read a register from the AS726x."""
if not self.hardware_available:
return None
try:
# Write the register address to the virtual register
self.bus.write_byte_data(self.AS726X_ADDR, 0x00, reg_addr)
# Read the data from the virtual register
return self.bus.read_byte_data(self.AS726X_ADDR, 0x00)
except Exception as e:
print(f"Error reading register {reg_addr}: {e}")
return None
def write_register(self, reg_addr, data):
"""Write to a register on the AS726x."""
if not self.hardware_available:
return False
try:
# Write the register address to the virtual register
self.bus.write_byte_data(self.AS726X_ADDR, 0x01, reg_addr)
# Write the data to the virtual register
self.bus.write_byte_data(self.AS726X_ADDR, 0x01, data)
return True
except Exception as e:
print(f"Error writing to register {reg_addr}: {e}")
return False
def set_gain(self, gain):
"""Set the gain of the sensor."""
if not self.hardware_available:
return
# Read current control setup
control = self.read_register(self.CONTROL_SETUP)
# Clear gain bits (bits 4:2)
control &= 0xE3
# Set new gain
control |= (gain << 2)
# Write back to control setup
self.write_register(self.CONTROL_SETUP, control)
def set_integration_time(self, time_setting):
"""Set the integration time of the sensor."""
if not self.hardware_available:
return False
if time_setting < 0 or time_setting > 10:
print("Integration time setting must be between 0 and 10")
return False
# Write integration time
self.write_register(self.INT_T, time_setting)
return True
def data_ready(self):
"""Check if sensor data is ready."""
if not self.hardware_available:
return False
control = self.read_register(self.CONTROL_SETUP)
return (control & self.DATA_RDY) != 0
def read_channels(self):
"""Read all NIR channel values.
Returns:
dict: Dictionary with keys 'R', 'S', 'T', 'U', 'V', 'W' containing
the 16-bit values for each channel.
"""
if not self.hardware_available:
return {}
channels = {}
# Read R channel (760nm - closest to glucose absorption)
r_low = self.read_register(self.R_G)
r_high = self.read_register(self.R_G + 1)
channels['R'] = (r_high << 8) | r_low
# Read S channel (810nm)
s_low = self.read_register(self.S_H)
s_high = self.read_register(self.S_H + 1)
channels['S'] = (s_high << 8) | s_low
# Read T channel (860nm)
t_low = self.read_register(self.T_I)
t_high = self.read_register(self.T_I + 1)
channels['T'] = (t_high << 8) | t_low
# Read U channel (910nm)
u_low = self.read_register(self.U_J)
u_high = self.read_register(self.U_J + 1)
channels['U'] = (u_high << 8) | u_low
# Read V channel (940nm)
v_low = self.read_register(self.V_K)
v_high = self.read_register(self.V_K + 1)
channels['V'] = (v_high << 8) | v_low
# Read W channel (990nm)
w_low = self.read_register(self.W_L)
w_high = self.read_register(self.W_L + 1)
channels['W'] = (w_high << 8) | w_low
return channels
def read_nir_value(self, average_readings=5, delay=0.5):
"""Read the NIR value from the sensor.
Args:
average_readings (int): Number of readings to average
delay (float): Delay between readings in seconds
Returns:
float: Averaged NIR reading
"""
if not self.connected or not self.hardware_available:
return None
readings = []
for _ in range(average_readings):
# Wait for data to be ready
for _ in range(10): # Timeout after 10 attempts
if self.data_ready():
break
time.sleep(0.1)
# Read all channels
channels = self.read_channels()
# Use R channel (760nm) which is closest to glucose absorption features
nir_value = channels['R']
readings.append(nir_value)
time.sleep(delay)
# Average the readings
if not readings:
return None
avg_reading = sum(readings) / len(readings)
return avg_reading
class GlucosePredictor:
"""Class for making glucose predictions from NIR readings using blended ensemble models."""
def __init__(self, model_paths=None, scaler_path='models/scaler.pkl'):
# Default model paths for ensemble
if model_paths is None:
model_paths = {
'rf': 'models/rf.pkl',
'knn': 'models/knn.pkl',
'svr': 'models/svr.pkl',
'lgbm': 'models/lgbm.pkl'
}
self.models = {}
for name, path in model_paths.items():
try:
self.models[name] = joblib.load(path)
except Exception as e:
print(f"Error loading {name} model: {e}")
try:
self.scaler = joblib.load(scaler_path)
print("Scaler loaded successfully")
except Exception as e:
print(f"Error loading scaler: {e}")
self.scaler = None
def predict_glucose(self, nir_value):
if not self.models or self.scaler is None:
return None
if nir_value is None or not np.isfinite(nir_value):
return None
# Load PCA
try:
pca = joblib.load('models/pca.pkl')
except Exception as e:
print(f"Error loading PCA: {e}")
return None
X_features = preprocess_single_nir(nir_value, self.scaler, pca)
preds = []
for model in self.models.values():
preds.append(model.predict(X_features)[0])
if preds:
return float(np.mean(preds))
return None
def get_prediction_interval(self, nir_value, percentile=95):
# Not supported for ensemble; return None
return None, None
def preprocess_single_nir(nir_value, scaler, pca):
# Apply the same preprocessing as in train_model.py
# 1. Smooth (not needed for single value)
# 2. Scale
nir_scaled = scaler.transform([[nir_value]])
# 3. PCA
X_pca = pca.transform(nir_scaled)
# 4. Statistical features (use the same logic as in extract_features)
stats = np.array([
nir_value, # mean (single value)
0.0, # skew (not defined for single value)
0.0 # kurtosis (not defined for single value)
]).reshape(1, -1)
X_features = np.hstack([X_pca, stats])
return X_features
def simulate_nir_reading(glucose_level):
"""Simulate a NIR reading for testing without hardware.
This function simulates the relationship between glucose level and NIR reading
based on the provided dataset's pattern, with some random noise.
"""
base = 300 + glucose_level * 0.8
noise = np.random.normal(0, 30) # Add random noise
return max(100, base + noise) # Ensure positive NIR value
def categorize_glucose(glucose_level):
"""Categorize glucose level into clinical ranges."""
if glucose_level < 70:
return "Hypoglycemia (Low)", "red"
elif glucose_level < 100:
return "Normal", "green"
elif glucose_level < 126:
return "Prediabetes", "orange"
elif glucose_level < 200:
return "Diabetes", "red"
else:
return "Severe Hyperglycemia", "darkred"
def create_trend_data(new_reading, history=None):
"""Add new reading to history and return updated history."""
if history is None:
history = []
# Add timestamp and reading to history
timestamp = pd.Timestamp.now()
history.append((timestamp, new_reading))
# Keep only the last 24 hours of data
cutoff = pd.Timestamp.now() - pd.Timedelta(hours=24)
history = [(ts, val) for ts, val in history if ts > cutoff]
return history
def plot_glucose_trend(history, save_path='images/trend.png'):
"""Plot glucose trend over time."""
if not history:
return None
# Create dataframe from history
df = pd.DataFrame(history, columns=['Timestamp', 'Glucose'])
# Plot trend
plt.figure(figsize=(10, 5))
plt.plot(df['Timestamp'], df['Glucose'], 'b-o')
# Add reference lines for glucose ranges
plt.axhspan(0, 70, color='red', alpha=0.1, label='Hypoglycemia')
plt.axhspan(70, 100, color='green', alpha=0.1, label='Normal')
plt.axhspan(100, 126, color='orange', alpha=0.1, label='Prediabetes')
plt.axhspan(126, 200, color='red', alpha=0.1, label='Diabetes')
plt.axhspan(200, 400, color='darkred', alpha=0.1, label='Severe Hyperglycemia')
plt.xlabel('Time')
plt.ylabel('Glucose Level (mg/dL)')
plt.title('Glucose Trend Over Time')
plt.legend(loc='upper left')
plt.grid(True, alpha=0.3)
# Format time axis
plt.gcf().autofmt_xdate()
# Save plot
os.makedirs(os.path.dirname(save_path), exist_ok=True)
plt.savefig(save_path)
plt.close()
return save_path
def moving_average(predictions, window=3):
return np.convolve(predictions, np.ones(window)/window, mode='valid')
def main(use_real_sensor=False):
"""Main function for prediction."""
# Initialize the sensor interface
if use_real_sensor:
sensor = AS726xSensor()
if not sensor.connected:
print("Using simulated data because sensor is not connected")
use_real_sensor = False
# Initialize the predictor
predictor = GlucosePredictor()
# Get NIR reading (either from sensor or simulation)
if use_real_sensor:
nir_value = sensor.read_nir_value()
else:
# For simulation, we'll assume a glucose level and generate NIR
true_glucose = 120 # mg/dL (for simulation only)
nir_value = simulate_nir_reading(true_glucose)
print(f"NIR Reading: {nir_value}")
# Make prediction
glucose_prediction = predictor.predict_glucose(nir_value)
if glucose_prediction is not None:
lower, upper = predictor.get_prediction_interval(nir_value)
category, color = categorize_glucose(glucose_prediction)
print(f"Predicted Glucose: {glucose_prediction:.1f} mg/dL")
print(f"Prediction Interval: [{lower}, {upper}] mg/dL")
print(f"Category: {category}")
else:
print("Unable to make prediction")
lower, upper, category, color = None, None, None, None
return {
'nir_value': nir_value,
'glucose_prediction': glucose_prediction,
'lower_bound': lower,
'upper_bound': upper,
'category': category,
'color': color
}
if __name__ == "__main__":
result = main(use_real_sensor=False)
print(result)