File size: 1,282 Bytes
fb61547
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import joblib
import numpy as np
import mne

def extract_features(raw, channels=None):
    """
    Fonction de base pour extraire des features simples à partir du signal EEG.
    Ici, on calcule la moyenne par canal.
    À améliorer avec tes features plus avancées !
    """
    if channels:
        raw = raw.copy().pick_channels(channels)
    data = raw.get_data()  # shape : (n_channels, n_times)
    features = np.mean(data, axis=1)  # Moyenne par canal
    return features

def predict_epilepsy(raw, model_path, channels=None):
    """
    Prédiction de l'état épileptique à partir du signal brut et d'un modèle sauvegardé.
    Retourne aussi la probabilité associée.
    """
    try:
        model = joblib.load(model_path)
        if channels:
            raw = raw.copy().pick_channels(channels)

        features = extract_features(raw).reshape(1, -1)
        prediction = model.predict(features)[0]

        if hasattr(model, "predict_proba"):
            prob = model.predict_proba(features)[0][1]  # proba que ce soit 1 (epilepsie)
        else:
            prob = None

        label = "Épilepsie détectée" if prediction == 1 else "Pas d’épilepsie détectée"
        return label, prob

    except Exception as e:
        return f"Erreur : {e}", None