File size: 2,402 Bytes
ea7d209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Test the DiabetesSignificanceModel"""

from collections import deque
from dataclasses import dataclass
from typing import Dict, Any, List, Tuple

# Mock ProcessingContext if not available
@dataclass
class ProcessingContext:
    timestamp: float
    sequence_id: int
    features: Dict[str, Any]
    history: List[Dict[str, Any]]
    metadata: Dict[str, Any]

class DiabetesSignificanceModel:
    """Test version of the model"""
    def __init__(self, config: Dict[str, Any]):
        self.hypo_threshold = config.get("hypo_threshold", 70.0)
        self.hyper_threshold = config.get("hyper_threshold", 180.0)
        self.target_glucose = config.get("target_glucose", 100.0)
        self.weights = {
            "glycemic_deviation": 0.35,
            "velocity_risk": 0.25,
            "iob_risk": 0.15,
            "cob_risk": 0.10,
            "variability": 0.10,
            "activity_risk": 0.05,
        }
        self.glucose_history: deque = deque(maxlen=12)
        self.significance_ema = 0.5
        self.ema_alpha = 0.15

    def compute_significance(self, context: ProcessingContext) -> Tuple[float, Dict[str, Any]]:
        # THIS IS THE FIXED VERSION
        features = context.features if hasattr(context, 'features') else {}

        # Extract features safely with proper dict access
        glucose = float(features.get("glucose_mgdl", 120.0)) if isinstance(features, dict) else 120.0

        print(f"✅ Successfully accessed features!")
        print(f"   Glucose: {glucose}")
        print(f"   Features type: {type(features)}")
        print(f"   Features is dict: {isinstance(features, dict)}")

        return 0.5, {"components": {"test": 0.5}}

# Test it
if __name__ == "__main__":
    print("Testing DiabetesSignificanceModel...")

    context = ProcessingContext(
        timestamp=1234567890.0,
        sequence_id=0,
        features={
            'glucose_mgdl': 150.0,
            'roc_mgdl_min': 2.0,
        },
        history=[],
        metadata={},
    )

    config = {
        'hypo_threshold': 70.0,
        'hyper_threshold': 180.0,
        'target_glucose': 100.0,
    }

    model = DiabetesSignificanceModel(config)
    sig, explanation = model.compute_significance(context)

    print(f"\nSignificance: {sig:.3f}")
    print(f"✅ Model works correctly!")