Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -33,87 +33,93 @@ def label_data(ranges):
|
|
33 |
|
34 |
def preprocess_data():
|
35 |
global global_data
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
|
|
|
|
|
|
|
|
115 |
|
116 |
-
|
|
|
|
|
117 |
|
118 |
with gr.Blocks() as demo:
|
119 |
file_input = gr.File(label="Upload CSV File")
|
|
|
33 |
|
34 |
def preprocess_data():
|
35 |
global global_data
|
36 |
+
try:
|
37 |
+
if 'Unnamed: 0' in global_data.columns:
|
38 |
+
global_data.drop(columns='Unnamed: 0', axis=1, inplace=True)
|
39 |
+
global_data.columns = ['raw_eeg', 'label']
|
40 |
+
raw_data = global_data['raw_eeg']
|
41 |
+
labels_old = global_data['label']
|
42 |
+
|
43 |
+
sampling_rate = 512
|
44 |
+
notch_freq = 50.0
|
45 |
+
lowcut, highcut = 0.5, 30.0
|
46 |
+
|
47 |
+
nyquist = (0.5 * sampling_rate)
|
48 |
+
notch_freq_normalized = notch_freq / nyquist
|
49 |
+
b_notch, a_notch = signal.iirnotch(notch_freq_normalized, Q=0.05, fs=sampling_rate)
|
50 |
+
|
51 |
+
lowcut_normalized = lowcut / nyquist
|
52 |
+
highcut_normalized = highcut / nyquist
|
53 |
+
b_bandpass, a_bandpass = signal.butter(4, [lowcut_normalized, highcut_normalized], btype='band')
|
54 |
+
|
55 |
+
features = []
|
56 |
+
labels = []
|
57 |
+
|
58 |
+
def calculate_psd_features(segment, sampling_rate):
|
59 |
+
f, psd_values = scipy.signal.welch(segment, fs=sampling_rate, nperseg=len(segment))
|
60 |
+
alpha_indices = np.where((f >= 8) & (f <= 13))
|
61 |
+
beta_indices = np.where((f >= 14) & (f <= 30))
|
62 |
+
theta_indices = np.where((f >= 4) & (f <= 7))
|
63 |
+
delta_indices = np.where((f >= 0.5) & (f <= 3))
|
64 |
+
energy_alpha = np.sum(psd_values[alpha_indices])
|
65 |
+
energy_beta = np.sum(psd_values[beta_indices])
|
66 |
+
energy_theta = np.sum(psd_values[theta_indices])
|
67 |
+
energy_delta = np.sum(psd_values[delta_indices])
|
68 |
+
alpha_beta_ratio = energy_alpha / energy_beta
|
69 |
+
return {
|
70 |
+
'E_alpha': energy_alpha,
|
71 |
+
'E_beta': energy_beta,
|
72 |
+
'E_theta': energy_theta,
|
73 |
+
'E_delta': energy_delta,
|
74 |
+
'alpha_beta_ratio': alpha_beta_ratio
|
75 |
+
}
|
76 |
+
|
77 |
+
def calculate_additional_features(segment, sampling_rate):
|
78 |
+
f, psd = scipy.signal.welch(segment, fs=sampling_rate, nperseg=len(segment))
|
79 |
+
peak_frequency = f[np.argmax(psd)]
|
80 |
+
spectral_centroid = np.sum(f * psd) / np.sum(psd)
|
81 |
+
log_f = np.log(f[1:])
|
82 |
+
log_psd = np.log(psd[1:])
|
83 |
+
spectral_slope = np.polyfit(log_f, log_psd, 1)[0]
|
84 |
+
return {
|
85 |
+
'peak_frequency': peak_frequency,
|
86 |
+
'spectral_centroid': spectral_centroid,
|
87 |
+
'spectral_slope': spectral_slope
|
88 |
+
}
|
89 |
+
|
90 |
+
for i in range(0, len(raw_data) - 512, 256):
|
91 |
+
print(f"Processing segment {i} to {i + 512}")
|
92 |
+
segment = raw_data.loc[i:i+512]
|
93 |
+
segment = pd.to_numeric(segment, errors='coerce')
|
94 |
+
segment = signal.filtfilt(b_notch, a_notch, segment)
|
95 |
+
segment = signal.filtfilt(b_bandpass, a_bandpass, segment)
|
96 |
+
segment_features = calculate_psd_features(segment, 512)
|
97 |
+
additional_features = calculate_additional_features(segment, 512)
|
98 |
+
segment_features = {**segment_features, **additional_features}
|
99 |
+
features.append(segment_features)
|
100 |
+
labels.append(labels_old[i])
|
101 |
+
|
102 |
+
columns = ['E_alpha', 'E_beta', 'E_theta', 'E_delta', 'alpha_beta_ratio', 'peak_frequency', 'spectral_centroid', 'spectral_slope']
|
103 |
+
df_features = pd.DataFrame(features, columns=columns)
|
104 |
+
df_features['label'] = labels
|
105 |
+
|
106 |
+
scaler = StandardScaler()
|
107 |
+
X_scaled = scaler.fit_transform(df_features.drop('label', axis=1))
|
108 |
+
df_scaled = pd.DataFrame(X_scaled, columns=columns)
|
109 |
+
df_scaled['label'] = df_features['label']
|
110 |
+
|
111 |
+
processed_data_filename = 'processed_data.csv'
|
112 |
+
df_scaled.to_csv(processed_data_filename, index=False)
|
113 |
+
|
114 |
+
scaler_filename = 'scaler.pkl'
|
115 |
+
with open(scaler_filename, 'wb') as file:
|
116 |
+
pickle.dump(scaler, file)
|
117 |
+
|
118 |
+
return "Data preprocessing complete! Download the processed data and scaler below.", processed_data_filename, scaler_filename
|
119 |
|
120 |
+
except Exception as e:
|
121 |
+
print(f"An error occurred during preprocessing: {e}")
|
122 |
+
return f"An error occurred during preprocessing: {e}", None, None
|
123 |
|
124 |
with gr.Blocks() as demo:
|
125 |
file_input = gr.File(label="Upload CSV File")
|