-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev_handler.py
More file actions
311 lines (251 loc) · 9.67 KB
/
dev_handler.py
File metadata and controls
311 lines (251 loc) · 9.67 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
"""
Development ML Inference Handler
Uses local models instead of S3 for development and testing
"""
import json
import numpy as np
from datetime import datetime
from typing import Dict, Any, Tuple
import logging
import os
from local_model_loader import LocalModelLoader
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()
# Initialize local model loader
MODEL_LOADER = LocalModelLoader("models")
def lambda_handler(event, context=None):
"""
Development Lambda handler for ML inference using local models
"""
try:
logger.info(f"ML inference request: {json.dumps(event)}")
# Extract input data
device_id = event['deviceId']
timestamp = event['timestamp']
readings = event['readings']
location = event['location']
# Load ML models
models = MODEL_LOADER.load_models("1.0")
# Prepare features for inference
features = prepare_features(readings, location, timestamp)
# Calculate WQI
wqi = calculate_wqi(features, models)
# Detect anomalies
anomaly_type, confidence = detect_anomaly(features, models)
# Get feature importance for explainability
feature_importance = get_feature_importance(models, features)
result = {
'deviceId': device_id,
'timestamp': timestamp,
'wqi': round(wqi, 2),
'anomalyType': anomaly_type,
'confidence': round(confidence, 3),
'modelVersion': models['version'],
'featureImportance': feature_importance
}
logger.info(f"ML inference completed: WQI={wqi}, Anomaly={anomaly_type}")
return {
'statusCode': 200,
'body': json.dumps(result)
}
except Exception as e:
logger.error(f"ML inference error: {e}")
return {
'statusCode': 500,
'body': json.dumps({
'error': str(e),
'timestamp': datetime.utcnow().isoformat()
})
}
def prepare_features(readings: Dict[str, float], location: Dict[str, float],
timestamp: str) -> np.ndarray:
"""
Prepare feature vector for ML inference
"""
try:
# Parse timestamp for temporal features
dt = datetime.fromisoformat(timestamp.replace('Z', '+00:00'))
# Base sensor features
features = [
readings['pH'],
readings['turbidity'],
readings['tds'],
readings['temperature'],
readings.get('humidity', 0)
]
# Location features
features.extend([
location['latitude'],
location['longitude']
])
# Temporal features
features.extend([
dt.hour, # Hour of day (0-23)
dt.month, # Month (1-12)
dt.weekday() # Day of week (0-6)
])
# Derived features
features.extend([
readings['pH'] * readings['temperature'], # pH-temperature interaction
readings['turbidity'] / max(readings['tds'], 1), # Turbidity-TDS ratio
abs(readings['pH'] - 7.0), # pH deviation from neutral
readings['temperature'] - 25.0, # Temperature deviation from 25°C
])
return np.array(features).reshape(1, -1)
except Exception as e:
logger.error(f"Error preparing features: {e}")
raise
def calculate_wqi(features: np.ndarray, models: Dict[str, Any]) -> float:
"""
Calculate Water Quality Index using trained model
"""
try:
# Scale features
scaled_features = models['scaler'].transform(features)
# Predict WQI
wqi_prediction = models['wqi_model'].predict(scaled_features)[0]
# Ensure WQI is in valid range (0-100)
wqi = max(0, min(100, wqi_prediction))
return float(wqi)
except Exception as e:
logger.error(f"Error calculating WQI: {e}")
# Fallback WQI calculation
return calculate_fallback_wqi(features)
def detect_anomaly(features: np.ndarray, models: Dict[str, Any]) -> Tuple[str, float]:
"""
Detect anomalies using trained model
"""
try:
# Scale features
scaled_features = models['scaler'].transform(features)
# Predict anomaly class
anomaly_prediction = models['anomaly_model'].predict(scaled_features)[0]
# Get prediction probabilities for confidence
probabilities = models['anomaly_model'].predict_proba(scaled_features)[0]
confidence = max(probabilities)
# Map prediction to anomaly type
anomaly_classes = ['normal', 'sensor_fault', 'contamination']
anomaly_type = anomaly_classes[anomaly_prediction]
return anomaly_type, float(confidence)
except Exception as e:
logger.error(f"Error detecting anomaly: {e}")
# Fallback anomaly detection
return detect_fallback_anomaly(features)
def get_feature_importance(models: Dict[str, Any], features: np.ndarray) -> Dict[str, float]:
"""
Get feature importance for model explainability
"""
try:
feature_names = [
'pH', 'turbidity', 'tds', 'temperature', 'latitude', 'longitude', 'hour', 'month', 'weekday',
'pH_temp_interaction', 'turbidity_tds_ratio',
'pH_deviation', 'temp_deviation'
]
# Get feature importance from Random Forest
importance_scores = models['wqi_model'].feature_importances_
# Create importance dictionary
feature_importance = {}
for i, name in enumerate(feature_names):
if i < len(importance_scores):
feature_importance[name] = round(float(importance_scores[i]), 4)
return feature_importance
except Exception as e:
logger.warning(f"Error getting feature importance: {e}")
return {}
def calculate_fallback_wqi(features: np.ndarray) -> float:
"""Fallback WQI calculation using simple weighted formula"""
try:
pH, turbidity, tds, temperature= features[0][:5]
# Simple WQI calculation
pH_score = max(0, 100 - abs(pH - 7.0) * 20)
turbidity_score = max(0, 100 - turbidity * 2)
tds_score = max(0, 100 - (tds - 300) * 0.05) if tds > 300 else 100
temp_score = max(0, 100 - abs(temperature - 25) * 3)
humidity_score = max(0, 100 - abs(humidity - 60) * 1)
wqi = (pH_score * 0.25 + turbidity_score * 0.25 + tds_score * 0.20 +
temp_score * 0.15 + humidity_score * 0.15)
return max(0, min(100, wqi))
except Exception as e:
logger.error(f"Fallback WQI calculation error: {e}")
return 50.0
def detect_fallback_anomaly(features: np.ndarray) -> Tuple[str, float]:
"""Fallback anomaly detection using rule-based approach"""
try:
pH, turbidity, tds, temperature= features[0][:5]
if pH < 6.0 or pH > 9.0:
return 'contamination' if turbidity > 10 else 'sensor_fault', 0.7
if turbidity > 50:
return 'contamination', 0.9
if tds > 2000:
return 'contamination', 0.8
if temperature < 0 or temperature > 50:
return 'sensor_fault', 0.7
if humidity > 95 or humidity < 10:
return 'sensor_fault', 0.6
return 'normal', 0.8
except Exception as e:
logger.error(f"Fallback anomaly detection error: {e}")
return 'unknown', 0.0
def test_inference():
"""
Test the inference handler with sample data
"""
test_cases = [
{
'name': 'Normal water quality',
'event': {
'deviceId': 'DEV-001',
'timestamp': '2025-10-19T13:00:00Z',
'readings': {
'pH': 7.0,
'turbidity': 1.5,
'tds': 200,
'temperature': 25.0
},
'location': {'latitude': 10.0, 'longitude': 76.0}
}
},
{
'name': 'Contaminated water',
'event': {
'deviceId': 'DEV-002',
'timestamp': '2025-10-19T13:00:00Z',
'readings': {
'pH': 4.5,
'turbidity': 50.0,
'tds': 2000,
'temperature': 25.0
},
'location': {'latitude': 10.0, 'longitude': 76.0}
}
},
{
'name': 'Sensor fault',
'event': {
'deviceId': 'DEV-003',
'timestamp': '2025-10-19T13:00:00Z',
'readings': {
'pH': 12.0,
'turbidity': 1.0,
'tds': 200,
'temperature': -5.0
},
'location': {'latitude': 10.0, 'longitude': 76.0}
}
}
]
print("Testing ML inference with local models...\n")
for test_case in test_cases:
print(f"Test: {test_case['name']}")
result = lambda_handler(test_case['event'])
if result['statusCode'] == 200:
body = json.loads(result['body'])
print(f" ✅ WQI: {body['wqi']}")
print(f" ✅ Anomaly: {body['anomalyType']} (confidence: {body['confidence']})")
print(f" ✅ Model version: {body['modelVersion']}")
else:
print(f" ❌ Error: {result['body']}")
print()
if __name__ == "__main__":
test_inference()