MorganBrizon commited on
Commit
3685f9a
·
verified ·
1 Parent(s): 4db6683

Delete preprocessing.py

Browse files
Files changed (1) hide show
  1. preprocessing.py +0 -96
preprocessing.py DELETED
@@ -1,96 +0,0 @@
1
- import os
2
- import mne
3
- import numpy as np
4
- import pandas as pd
5
-
6
-
7
- def standardize_dataframe(df):
8
- # Make a copy to avoid modifying the original dataframe
9
- df_standardized = df.copy()
10
-
11
- # Only standardize numeric columns
12
- numeric_columns = df.select_dtypes(include=np.number).columns
13
-
14
- for column in numeric_columns:
15
- mean = df[column].mean()
16
- std = df[column].std()
17
-
18
- df_standardized[column] = (df[column] - mean) / std
19
-
20
- return df_standardized
21
-
22
- def select_relevant_channels(raw):
23
- # For relevant channel criteria check documentation
24
- '''“EEG FP1-REF” for the left frontal pole
25
-
26
- “EEG FP2-REF” for the right frontal pole
27
-
28
- “EEG F3-REF” for the left frontal region
29
-
30
- “EEG F4-REF” for the right frontal region
31
-
32
- “EEG C3-REF” for the left central region'''
33
-
34
- desired = ["EEG FP1-REF", "EEG FP2-REF", "EEG F3-REF", "EEG F4-REF", "EEG C3-REF"]
35
- #check if all desired channels are present; if not, skip this file
36
- if not all(ch in raw.ch_names for ch in desired):
37
- print("Skipping file because it doesn't have the full set of desired channels.")
38
- return None
39
- raw.pick_channels(desired, verbose=False)
40
- return raw
41
-
42
-
43
- def collapse_epoch_df_by_channel(epoch_df):
44
- # Identify channel columns (exclude time, epoch, condition)
45
- channel_cols = [col for col in epoch_df.columns if col not in ['time', 'epoch']]
46
- # Group by epoch
47
- grouped = epoch_df.groupby('epoch')
48
- rows = []
49
- for epoch_num, group in grouped:
50
- group_sorted = group.sort_values('time')
51
- # For each channel, extract the 1D array for this epoch
52
- row = {'epoch': epoch_num}
53
- for ch in channel_cols:
54
- row[ch] = group_sorted[ch].values # 1D array of length = number of time samples in the epoch
55
- rows.append(row)
56
- return pd.DataFrame(rows)
57
-
58
- def preprocess_eeg_file(edf_path, fmin=1.0, fmax=45.0, segment_lenght=5, overlap=2):
59
-
60
- # 1. Charger le fichier EDF avec MNE
61
- raw = mne.io.read_raw_edf(edf_path, preload=True, verbose=False)
62
-
63
- # Resample (to 250 because it's the lowest sampling rate )
64
- raw.resample(250, verbose=False)
65
-
66
- # Filtrage passe-bande (1-45 Hz)
67
- raw.filter(fmin, fmax, fir_design='firwin', verbose=False)
68
-
69
- # Skip EEGs less than 5s
70
- if raw.times[-1] < 5:
71
- print(f"Skipping {edf_path}: duration ({raw.times[-1]:.2f} s) is less than required 5s.")
72
- return None
73
-
74
- # Suppression des canaux non EEG
75
- eeg_channels = mne.pick_types(raw.info, eeg=True, exclude=[])
76
- raw.pick(eeg_channels, verbose=False)
77
-
78
- # Selectionner les channels pertinents (channel selection from EDA ?)
79
- print(raw.ch_names)
80
- raw = select_relevant_channels(raw)
81
- if raw is None:
82
- return None
83
-
84
- # Segmentation
85
- epochs = mne.make_fixed_length_epochs(raw, duration=segment_lenght, preload=False, overlap=overlap, verbose=False)
86
-
87
- # Transform to dataframe and standadize
88
-
89
- df = epochs.to_data_frame() # epochs is returned by preprocess_eeg_file()
90
- df_std = standardize_dataframe(df.drop(['time','epoch', 'condition'], axis=1))
91
- result = pd.concat([df[['time','epoch']], df_std], axis=1)
92
-
93
- return collapse_epoch_df_by_channel(result)
94
-
95
-
96
-