-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_script.py
More file actions
58 lines (41 loc) · 1.38 KB
/
client_script.py
File metadata and controls
58 lines (41 loc) · 1.38 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
"""
client_script.py
Client-side demonstration of real EEG inference.
Workflow:
1. Load a saved raw EEG epoch (22 × T).
2. Load the fitted CSP transformer.
3. Compute CSP features.
4. Send the features to the FastAPI server.
5. Print the predicted motor-imagery class.
"""
import numpy as np
import joblib
import requests
from pathlib import Path
API_URL = "http://127.0.0.1:8000/predict"
def main():
# 1. Load sample EEG epoch
epoch_path = Path("sample_data/sample_epoch.npy")
if not epoch_path.exists():
raise FileNotFoundError("sample_epoch.npy not found.")
epoch = np.load(epoch_path) # shape (22, T)
# 2. Load CSP transformer
csp_path = Path("models/csp_transformer.joblib")
if not csp_path.exists():
raise FileNotFoundError("csp_transformer.joblib missing.")
csp = joblib.load(csp_path)
# 3. Compute CSP features
epoch_reshaped = epoch.reshape(1, epoch.shape[0], epoch.shape[1])
features = csp.transform(epoch_reshaped)[0].tolist()
print("Extracted features:", features)
# 4. Send features to API
payload = {"features": features}
response = requests.post(API_URL, json=payload)
if response.status_code != 200:
print("API error:", response.text)
return
# 5. Print prediction
result = response.json()
print("Predicted class:", result["prediction"])
if __name__ == "__main__":
main()