Views
No views yet
Schema correction (2026-09-06). The model reads five of its fourteen declared inputs —RMSSD,Mean_RR,HRV_SDNN,pNN50,HR. The nine frequency-domain and nonlinear features (HRV_HF,HRV_LF,HRV_HF_nu,HRV_LF_nu,HRV_LFHF,HRV_TP,HRV_SD1SD2,HRV_Sampen,HRV_DFA_alpha1) were constant at training time and the graph does not read them: sweeping each across its plausible range changes P(stress) by 0.000. The input tensor is still[N, 14]; pass any finite value in those slots. The model consumes raw feature values — the earliernormalizationblock was not applied by the graph and has been removed from the metadata. The reported LOSO figure (78.4% / F1 0.726, WESAD chest) was earned by the five live features and stands.



pip install onnxruntime numpy1import numpy as np
2import onnxruntime as ort
3import json
4from pathlib import Path
5
6class WesadInferenceEngine:
7 def __init__(self, model_path, metadata_path=None):
8 # Initialize ONNX Runtime session
9 self.session = ort.InferenceSession(str(model_path))
10 self.input_name = self.session.get_inputs()[0].name
11
12 # Default labels
13 self.label_order = ["Baseline", "Stress"]
14
15 # Load metadata if provided
16 if metadata_path and Path(metadata_path).exists():
17 with open(metadata_path, 'r') as f:
18 self.metadata = json.load(f)
19 print(f"Model Loaded: {self.metadata.get('model_id', 'Unknown')}")
20
21 def predict(self, hrv_features):
22 """
23 Predict emotion class from HRV features.
24
25 Args:
26 hrv_features: list or np.array of 14 features in order:
27 ['RMSSD', 'Mean_RR', 'HRV_SDNN', 'pNN50', 'HRV_HF', 'HRV_LF',
28 'HRV_HF_nu', 'HRV_LF_nu', 'HRV_LFHF', 'HRV_TP', 'HRV_SD1SD2',
29 'HRV_Sampen', 'HRV_DFA_alpha1', 'HR']
30
31 Returns:
32 tuple: (predicted_label, confidence_score)
33 """
34 input_data = np.array(hrv_features, dtype=np.float32).reshape(1, -1)
35 outputs = self.session.run(None, {self.input_name: input_data})
36
37 predicted_idx = int(outputs[0][0])
38 probabilities = outputs[1][0]
39
40 label_name = self.label_order[predicted_idx]
41 confidence = probabilities[predicted_idx]
42
43 return label_name, confidence
44
45# Usage Example
46if __name__ == "__main__":
47 MODEL_FILE = "w120s60_binary/models/ExtraTrees.onnx"
48 METADATA_FILE = "w120s60_binary/models/ExtraTrees_metadata.json"
49
50 # Example HRV features (14 features in correct order)
51 example_input = [35.5, 950.2, 55.1, 15.2, 430.0, 620.0,
52 0.42, 0.78, 1.45, 1450.0, 0.72, 1.3, 1.1, 91.0]
53
54 engine = WesadInferenceEngine(MODEL_FILE, METADATA_FILE)
55 label, confidence = engine.predict(example_input)
56
57 print(f"Predicted Label: {label}")
58 print(f"Confidence: {confidence:.2%}")1├── w120s60_binary/ # Window: 120s, Step: 60s
2│ ├── models/
3│ │ ├── ExtraTrees.onnx
4│ │ └── ExtraTrees_metadata.json
5│ └── figures/
6│ └── confusion_matrix_ExtraTrees_loso_binary.png
7├── w120s5_binary/ # Window: 120s, Step: 5s
8│ ├── models/
9│ │ ├── ExtraTrees.onnx
10│ │ ├── LogReg.onnx
11│ │ ├── RF.onnx
12│ │ └── [metadata files]
13│ └── figures/
14│ ├── confusion_matrix_ExtraTrees_loso_binary.png
15│ ├── confusion_matrix_LinearSVM_loso_binary.png
16│ ├── confusion_matrix_LogReg_loso_binary.png
17│ ├── confusion_matrix_RF_loso_binary.png
18│ └── confusion_matrix_XGB_loso_binary.png
19└── w60s5_binary/ # Window: 60s, Step: 5s
20 ├── models/
21 │ ├── ExtraTrees.onnx
22 │ └── ExtraTrees_metadata.json
23 └── figures/
24 └── confusion_matrix_ExtraTrees_loso_binary.png"Baseline" or "Stress"pip install synheart-emotion