fadzwan commited on
Commit
6aca538
·
verified ·
1 Parent(s): 095893d

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +53 -0
src/streamlit_app.py CHANGED
@@ -151,3 +151,56 @@ if plot_waveform_button:
151
 
152
  except Exception as e:
153
  st.error(f"❌ Error: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151
 
152
  except Exception as e:
153
  st.error(f"❌ Error: {str(e)}")
154
+
155
+ spectrogram_button = st.button("Plot TP9 Spectrograms")
156
+
157
+ if spectrogram_button:
158
+ try:
159
+ # Download and read CSV
160
+ response = requests.get(csv_url)
161
+ response.raise_for_status()
162
+ data = pd.read_csv(io.StringIO(response.text))
163
+ st.write(f"Loaded data with shape: {data.shape}")
164
+
165
+ # Extract timestamps and TP9
166
+ timestamps = data['timestamps'] if 'timestamps' in data.columns else data['TimeStamp']
167
+ tp10_raw = data['TP9']
168
+
169
+ # Calculate sampling rate
170
+ time_diffs = np.diff(timestamps)
171
+ average_interval = np.mean(time_diffs)
172
+ sampling_rate = 1 / average_interval
173
+
174
+ # Create MNE Raw object
175
+ info = mne.create_info(ch_names=['TP9'], sfreq=sampling_rate, ch_types='eeg')
176
+ raw = mne.io.RawArray(np.array([tp10_raw]), info)
177
+
178
+ # Apply bandpass filter
179
+ raw.filter(1, 50, fir_design='firwin')
180
+ tp10_filtered_mne = raw.get_data()[0]
181
+
182
+ from scipy.signal import spectrogram
183
+
184
+ # Spectrogram before filtering
185
+ frequencies_pre, times_pre, spectrogram_pre = spectrogram(tp10_raw, fs=sampling_rate, nperseg=256)
186
+ fig_spec_pre, ax_pre = plt.subplots(figsize=(10, 6))
187
+ p1 = ax_pre.pcolormesh(times_pre, frequencies_pre, np.log10(spectrogram_pre + 1e-10), shading='gouraud')
188
+ fig_spec_pre.colorbar(p1, ax=ax_pre, label='Log Power Spectral Density')
189
+ ax_pre.set_title('Spectrogram of TP9 (Raw)')
190
+ ax_pre.set_xlabel('Time (seconds)')
191
+ ax_pre.set_ylabel('Frequency (Hz)')
192
+ st.pyplot(fig_spec_pre)
193
+
194
+ # Spectrogram after filtering
195
+ frequencies_post, times_post, spectrogram_post = spectrogram(tp10_filtered_mne, fs=sampling_rate, nperseg=256)
196
+ fig_spec_post, ax_post = plt.subplots(figsize=(10, 6))
197
+ p2 = ax_post.pcolormesh(times_post, frequencies_post, np.log10(spectrogram_post + 1e-10), shading='gouraud')
198
+ fig_spec_post.colorbar(p2, ax=ax_post, label='Log Power Spectral Density')
199
+ ax_post.set_title('Spectrogram of TP9 (Filtered 1–50Hz)')
200
+ ax_post.set_xlabel('Time (seconds)')
201
+ ax_post.set_ylabel('Frequency (Hz)')
202
+ st.pyplot(fig_spec_post)
203
+
204
+ except Exception as e:
205
+ st.error(f"❌ Error: {str(e)}")
206
+