Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +37 -0
src/streamlit_app.py
CHANGED
|
@@ -9,6 +9,7 @@ from matplotlib.animation import FuncAnimation
|
|
| 9 |
import requests
|
| 10 |
import streamlit as st
|
| 11 |
import tempfile
|
|
|
|
| 12 |
|
| 13 |
# Set page config
|
| 14 |
st.set_page_config(page_title="Muse EEG Topomap Viewer", layout="centered")
|
|
@@ -204,3 +205,39 @@ if spectrogram_button:
|
|
| 204 |
except Exception as e:
|
| 205 |
st.error(f"β Error: {str(e)}")
|
| 206 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
import requests
|
| 10 |
import streamlit as st
|
| 11 |
import tempfile
|
| 12 |
+
from scipy.signal import welch
|
| 13 |
|
| 14 |
# Set page config
|
| 15 |
st.set_page_config(page_title="Muse EEG Topomap Viewer", layout="centered")
|
|
|
|
| 205 |
except Exception as e:
|
| 206 |
st.error(f"β Error: {str(e)}")
|
| 207 |
|
| 208 |
+
# Function to compute band power
|
| 209 |
+
def bandpower(data, sf, band, window_sec=None):
|
| 210 |
+
band = np.asarray(band)
|
| 211 |
+
low, high = band
|
| 212 |
+
|
| 213 |
+
if window_sec is not None:
|
| 214 |
+
nperseg = int(window_sec * sf)
|
| 215 |
+
else:
|
| 216 |
+
nperseg = None
|
| 217 |
+
|
| 218 |
+
freqs, psd = welch(data, sf, nperseg=nperseg)
|
| 219 |
+
idx_band = np.logical_and(freqs >= low, freqs <= high)
|
| 220 |
+
return np.trapz(psd[idx_band], freqs[idx_band])
|
| 221 |
+
|
| 222 |
+
# Button to calculate and plot band powers
|
| 223 |
+
if st.button("Show Band Power Chart (TP9)"):
|
| 224 |
+
# Define bands
|
| 225 |
+
bands = {
|
| 226 |
+
"Delta (1β4 Hz)": (1, 4),
|
| 227 |
+
"Theta (4β8 Hz)": (4, 8),
|
| 228 |
+
"Alpha (8β12 Hz)": (8, 12),
|
| 229 |
+
"Beta (12β30 Hz)": (12, 30),
|
| 230 |
+
"Gamma (30β50 Hz)": (30, 50)
|
| 231 |
+
}
|
| 232 |
+
|
| 233 |
+
# Compute power for each band
|
| 234 |
+
powers = [bandpower(tp10_filtered_mne, sampling_rate, b) for b in bands.values()]
|
| 235 |
+
|
| 236 |
+
# Plot
|
| 237 |
+
fig, ax = plt.subplots()
|
| 238 |
+
ax.bar(bands.keys(), powers, color='skyblue')
|
| 239 |
+
ax.set_title("Band Power - TP9")
|
| 240 |
+
ax.set_ylabel("Power")
|
| 241 |
+
ax.tick_params(axis='x', rotation=45)
|
| 242 |
+
st.pyplot(fig)
|
| 243 |
+
|