Spaces:
Sleeping
Sleeping
Create visualization.py
Browse files- visualization.py +68 -0
visualization.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import librosa
|
| 2 |
+
import librosa.display
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
def generate_visualizations(audio_path):
|
| 7 |
+
"""
|
| 8 |
+
Takes an audio file path, processes it, and returns two matplotlib figures:
|
| 9 |
+
1. A Time-Domain PCG Waveform
|
| 10 |
+
2. A Frequency-Domain Mel-Spectrogram tuned for heart sounds.
|
| 11 |
+
"""
|
| 12 |
+
if not audio_path:
|
| 13 |
+
return None, None
|
| 14 |
+
|
| 15 |
+
try:
|
| 16 |
+
# Load the audio file
|
| 17 |
+
# sr=None preserves the original sample rate.
|
| 18 |
+
y, sr = librosa.load(audio_path, sr=None)
|
| 19 |
+
|
| 20 |
+
# --- 1. Generate PCG Waveform (Time Domain) ---
|
| 21 |
+
# Close any existing plots to prevent memory leaks in Gradio
|
| 22 |
+
plt.close('all')
|
| 23 |
+
|
| 24 |
+
fig_pcg, ax_pcg = plt.subplots(figsize=(10, 4))
|
| 25 |
+
librosa.display.waveshow(y, sr=sr, ax=ax_pcg, color="#1f77b4", alpha=0.8)
|
| 26 |
+
|
| 27 |
+
ax_pcg.set_title("Phonocardiogram (PCG) Waveform", fontsize=14, fontweight="bold")
|
| 28 |
+
ax_pcg.set_xlabel("Time (seconds)")
|
| 29 |
+
ax_pcg.set_ylabel("Amplitude")
|
| 30 |
+
ax_pcg.grid(True, linestyle="--", alpha=0.6)
|
| 31 |
+
fig_pcg.tight_layout()
|
| 32 |
+
|
| 33 |
+
# --- 2. Generate Mel-Spectrogram (Frequency Domain) ---
|
| 34 |
+
fig_spec, ax_spec = plt.subplots(figsize=(10, 4))
|
| 35 |
+
|
| 36 |
+
# Compute the Mel-spectrogram
|
| 37 |
+
# We set fmax=2000 because heart sounds and murmurs rarely exceed 2000 Hz.
|
| 38 |
+
# This focuses the graph on the relevant medical data.
|
| 39 |
+
S = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=128, fmax=2000)
|
| 40 |
+
|
| 41 |
+
# Convert power to decibels (log scale) for better visualization
|
| 42 |
+
S_dB = librosa.power_to_db(S, ref=np.max)
|
| 43 |
+
|
| 44 |
+
# Display the spectrogram
|
| 45 |
+
img = librosa.display.specshow(
|
| 46 |
+
S_dB,
|
| 47 |
+
x_axis='time',
|
| 48 |
+
y_axis='mel',
|
| 49 |
+
sr=sr,
|
| 50 |
+
fmax=2000,
|
| 51 |
+
ax=ax_spec,
|
| 52 |
+
cmap='magma' # 'magma' or 'viridis' looks very professional for medical imaging
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
ax_spec.set_title("Mel-Spectrogram (Low-Frequency Focus)", fontsize=14, fontweight="bold")
|
| 56 |
+
ax_spec.set_xlabel("Time (seconds)")
|
| 57 |
+
ax_spec.set_ylabel("Frequency (Hz)")
|
| 58 |
+
fig_spec.colorbar(img, ax=ax_spec, format='%+2.0f dB', label="Intensity (dB)")
|
| 59 |
+
fig_spec.tight_layout()
|
| 60 |
+
|
| 61 |
+
return fig_pcg, fig_spec
|
| 62 |
+
|
| 63 |
+
except Exception as e:
|
| 64 |
+
print(f"Error generating visualizations: {e}")
|
| 65 |
+
# Return empty figures in case of an error so the UI doesn't crash
|
| 66 |
+
fig, ax = plt.subplots()
|
| 67 |
+
ax.text(0.5, 0.5, "Error generating graph", ha="center")
|
| 68 |
+
return fig, fig
|