Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -202,9 +202,69 @@ class PSDAnalyzer:
|
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
-
|
|
|
|
| 206 |
if show_bands:
|
| 207 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
seen_edges = set()
|
| 209 |
for low, high in FREQ_BANDS.values():
|
| 210 |
for edge in [low, high]:
|
|
@@ -212,7 +272,7 @@ class PSDAnalyzer:
|
|
| 212 |
fig.add_vline(
|
| 213 |
x=edge,
|
| 214 |
line=dict(color="lightgray", width=1, dash="dot"),
|
| 215 |
-
opacity=0.
|
| 216 |
layer="below"
|
| 217 |
)
|
| 218 |
seen_edges.add(edge)
|
|
|
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
+
|
| 206 |
+
# Add frequency band visualization - Colored bands with legend
|
| 207 |
if show_bands:
|
| 208 |
+
# Use paper coordinates for shapes to avoid affecting data scale
|
| 209 |
+
# We'll draw rectangles in "paper" space, so they don't distort the plot
|
| 210 |
+
|
| 211 |
+
# Get current y-axis range in paper coordinates
|
| 212 |
+
try:
|
| 213 |
+
y_min_data = min(trace.y.min() for trace in fig.data if hasattr(trace, 'y'))
|
| 214 |
+
y_max_data = max(trace.y.max() for trace in fig.data if hasattr(trace, 'y'))
|
| 215 |
+
except Exception:
|
| 216 |
+
y_min_data, y_max_data = 1e-5, 1.0
|
| 217 |
+
|
| 218 |
+
# Map data y-range to paper y-coordinates (0 to 1)
|
| 219 |
+
# For log scale, we map log(y) to linear paper space
|
| 220 |
+
y_log_min = np.log10(y_min_data)
|
| 221 |
+
y_log_max = np.log10(y_max_data)
|
| 222 |
+
y_log_range = y_log_max - y_log_min
|
| 223 |
+
|
| 224 |
+
# Now loop through bands
|
| 225 |
+
for band, (low, high) in FREQ_BANDS.items():
|
| 226 |
+
# Only show if band overlaps visible freq range
|
| 227 |
+
if high < freq_range[0] or low > freq_range[1]:
|
| 228 |
+
continue
|
| 229 |
+
|
| 230 |
+
band_low = max(low, freq_range[0])
|
| 231 |
+
band_high = min(high, freq_range[1])
|
| 232 |
+
|
| 233 |
+
# Map y-data to paper coordinates (for log scale)
|
| 234 |
+
# We want the band to cover full y-axis in paper space
|
| 235 |
+
# So we set y0=0, y1=1 in paper coordinates
|
| 236 |
+
# But we must use `yref="paper"` and `xref="x"`
|
| 237 |
+
fig.add_shape(
|
| 238 |
+
type="rect",
|
| 239 |
+
x0=band_low,
|
| 240 |
+
x1=band_high,
|
| 241 |
+
y0=0,
|
| 242 |
+
y1=1,
|
| 243 |
+
xref="x",
|
| 244 |
+
yref="paper",
|
| 245 |
+
fillcolor=band_colors[band],
|
| 246 |
+
opacity=0.15,
|
| 247 |
+
layer="below",
|
| 248 |
+
line_width=0,
|
| 249 |
+
name=band,
|
| 250 |
+
)
|
| 251 |
+
|
| 252 |
+
# Add label above the plot (outside the plot area)
|
| 253 |
+
center_x = (band_low + band_high) / 2
|
| 254 |
+
fig.add_annotation(
|
| 255 |
+
x=center_x,
|
| 256 |
+
y=1.02, # Slightly above the top of the plot
|
| 257 |
+
text=band,
|
| 258 |
+
showarrow=False,
|
| 259 |
+
font=dict(size=9, color="dimgray"),
|
| 260 |
+
xanchor="center",
|
| 261 |
+
yanchor="bottom",
|
| 262 |
+
xref="x",
|
| 263 |
+
yref="paper",
|
| 264 |
+
opacity=0.8,
|
| 265 |
+
)
|
| 266 |
+
|
| 267 |
+
# Optional: Add vertical lines at band edges
|
| 268 |
seen_edges = set()
|
| 269 |
for low, high in FREQ_BANDS.values():
|
| 270 |
for edge in [low, high]:
|
|
|
|
| 272 |
fig.add_vline(
|
| 273 |
x=edge,
|
| 274 |
line=dict(color="lightgray", width=1, dash="dot"),
|
| 275 |
+
opacity=0.5,
|
| 276 |
layer="below"
|
| 277 |
)
|
| 278 |
seen_edges.add(edge)
|