Spaces:
Sleeping
Sleeping
Delete preprocessing_2dcnn.py
Browse files- preprocessing_2dcnn.py +0 -52
preprocessing_2dcnn.py
DELETED
|
@@ -1,52 +0,0 @@
|
|
| 1 |
-
import numpy as np
|
| 2 |
-
import pandas as pd
|
| 3 |
-
from scipy.signal import spectrogram
|
| 4 |
-
|
| 5 |
-
#METHOD 1: STFT spectrogram
|
| 6 |
-
def convert_epoch_to_spectrogram(epoch_row, channels, fs=250, nperseg=128, noverlap=64):
|
| 7 |
-
"""
|
| 8 |
-
Given a pandas Series representing one epoch, where each channel column contains
|
| 9 |
-
a 1D numpy array of time series data, compute a spectrogram for each channel and
|
| 10 |
-
stack them into a 3D array of shape (n_channels, freq_bins, time_bins).
|
| 11 |
-
|
| 12 |
-
Parameters:
|
| 13 |
-
epoch_row: pandas Series
|
| 14 |
-
One row of your preprocessed dataframe (one epoch).
|
| 15 |
-
channels: list of str
|
| 16 |
-
The list of channel names to process.
|
| 17 |
-
fs: int
|
| 18 |
-
Sampling frequency (default 250 Hz).
|
| 19 |
-
nperseg: int
|
| 20 |
-
Length of each segment for spectrogram calculation.
|
| 21 |
-
noverlap: int
|
| 22 |
-
Number of overlapping samples between segments.
|
| 23 |
-
|
| 24 |
-
Returns:
|
| 25 |
-
spec_stack: numpy array
|
| 26 |
-
A 3D array with shape (n_channels, freq_bins, time_bins) containing the
|
| 27 |
-
spectrogram (in dB) for each channel.
|
| 28 |
-
"""
|
| 29 |
-
spec_list = []
|
| 30 |
-
for ch in channels:
|
| 31 |
-
ts = epoch_row[ch] # this is the 1D time series for the channel
|
| 32 |
-
# Compute the spectrogram
|
| 33 |
-
f, t, Sxx = spectrogram(ts, fs=fs, nperseg=nperseg, noverlap=noverlap)
|
| 34 |
-
# Convert the power spectrogram to dB scale
|
| 35 |
-
Sxx_db = 10 * np.log10(Sxx + 1e-10)
|
| 36 |
-
spec_list.append(Sxx_db)
|
| 37 |
-
spec_stack = np.stack(spec_list, axis=0)
|
| 38 |
-
return spec_stack
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
def convert_preprocessed_df_to_2d(preprocessed_df, channels=["EEG FP1-REF", "EEG FP2-REF", "EEG F3-REF", "EEG F4-REF", "EEG C3-REF"],
|
| 42 |
-
fs=250, nperseg=128, noverlap=64):
|
| 43 |
-
|
| 44 |
-
# Make a copy to avoid modifying the original dataframe
|
| 45 |
-
df = preprocessed_df.copy()
|
| 46 |
-
|
| 47 |
-
# Compute the spectrogram for each row (epoch)
|
| 48 |
-
df["spectrogram"] = df.apply(lambda row: convert_epoch_to_spectrogram(row, channels, fs, nperseg, noverlap), axis=1)
|
| 49 |
-
|
| 50 |
-
#drop channel columns and other useless ones
|
| 51 |
-
df_final = df[["spectrogram", "epilepsy", "age", "gender",'subject_id', 'edf_path']]
|
| 52 |
-
return df_final
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|