Umang-Bansal commited on
Commit
5c8bfc0
1 Parent(s): cc09ded

Update app.py

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