Spaces:
Sleeping
Sleeping
Update prediction.py
Browse files- prediction.py +10 -17
prediction.py
CHANGED
|
@@ -1,34 +1,27 @@
|
|
| 1 |
import numpy as np
|
| 2 |
-
import tensorflow as tf
|
| 3 |
import pandas as pd
|
| 4 |
-
from sklearn.metrics import confusion_matrix, f1_score, accuracy_score
|
| 5 |
from preprocessing import preprocess_eeg_file
|
| 6 |
from preprocessing_2dcnn import convert_epoch_to_spectrogram
|
| 7 |
-
import random
|
| 8 |
-
random.seed(42)
|
| 9 |
|
| 10 |
def aggregate_predictions(spectrogram_list, model, threshold=0.5):
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
preds = model.predict(X)
|
| 16 |
-
mean_prob = np.mean(preds[:, 1])
|
| 17 |
final_label = 1 if mean_prob >= threshold else 0
|
| 18 |
-
return final_label, mean_prob
|
| 19 |
|
| 20 |
def predict_eeg_recording(edf_path, model, threshold=0.5):
|
| 21 |
-
#Process the edf file
|
| 22 |
preprocessed_df = preprocess_eeg_file(edf_path, fmin=1.0, fmax=45.0, segment_lenght=5, overlap=2)
|
| 23 |
-
|
| 24 |
if preprocessed_df is None or preprocessed_df.empty:
|
| 25 |
raise ValueError("EEG file could not be preprocessed or no valid segments found.")
|
| 26 |
|
| 27 |
channels = ["EEG FP1-REF", "EEG FP2-REF", "EEG F3-REF", "EEG F4-REF", "EEG C3-REF"]
|
| 28 |
-
|
| 29 |
spectrogram_list = preprocessed_df.apply(
|
| 30 |
-
lambda row: convert_epoch_to_spectrogram(row, channels, fs=250, nperseg=128, noverlap=64),
|
|
|
|
| 31 |
).tolist()
|
| 32 |
-
|
| 33 |
-
return aggregate_predictions(spectrogram_list, model, threshold)
|
| 34 |
|
|
|
|
|
|
| 1 |
import numpy as np
|
|
|
|
| 2 |
import pandas as pd
|
|
|
|
| 3 |
from preprocessing import preprocess_eeg_file
|
| 4 |
from preprocessing_2dcnn import convert_epoch_to_spectrogram
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def aggregate_predictions(spectrogram_list, model, threshold=0.5):
|
| 7 |
+
X = np.array([np.transpose(s, (1, 2, 0)) for s in spectrogram_list]) # (n, H, W, C)
|
| 8 |
+
preds = model.predict(X, verbose=0)
|
| 9 |
+
segment_probs = preds[:, 1] # probas pour la classe "épilepsie"
|
| 10 |
+
mean_prob = np.mean(segment_probs)
|
|
|
|
|
|
|
| 11 |
final_label = 1 if mean_prob >= threshold else 0
|
| 12 |
+
return final_label, mean_prob, segment_probs
|
| 13 |
|
| 14 |
def predict_eeg_recording(edf_path, model, threshold=0.5):
|
|
|
|
| 15 |
preprocessed_df = preprocess_eeg_file(edf_path, fmin=1.0, fmax=45.0, segment_lenght=5, overlap=2)
|
| 16 |
+
|
| 17 |
if preprocessed_df is None or preprocessed_df.empty:
|
| 18 |
raise ValueError("EEG file could not be preprocessed or no valid segments found.")
|
| 19 |
|
| 20 |
channels = ["EEG FP1-REF", "EEG FP2-REF", "EEG F3-REF", "EEG F4-REF", "EEG C3-REF"]
|
| 21 |
+
|
| 22 |
spectrogram_list = preprocessed_df.apply(
|
| 23 |
+
lambda row: convert_epoch_to_spectrogram(row, channels, fs=250, nperseg=128, noverlap=64),
|
| 24 |
+
axis=1
|
| 25 |
).tolist()
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
return aggregate_predictions(spectrogram_list, model, threshold)
|