Add comprehensive spectrogram data access and visualization support#50
Add comprehensive spectrogram data access and visualization support#50
Conversation
…port Resolves Issue #46 Add complete spectrogram support to enable programmatic access to the same high-quality constant-Q spectrograms used in the IDTAP web application, with extensive matplotlib integration for computational musicology research. ## New Features: ### SpectrogramData Class (`idtap/spectrogram.py`) - **Data Loading**: - `from_audio_id(audio_id, client)` - Download from server - `from_piece(piece, client)` - Load from Piece object - Auto-decompresses gzipped spectrogram data from swara.studio - **Transformations**: - `apply_intensity(power)` - Power-law contrast enhancement (1.0-5.0) - `apply_colormap(data, cmap)` - 35+ matplotlib colormaps - `crop_frequency(min_hz, max_hz)` - Frequency range cropping - `crop_time(start_time, end_time)` - Time range cropping - **Matplotlib Integration** (for research workflows): - `plot_on_axis(ax, ...)` - Plot on existing axis for overlays - `get_plot_data(power, apply_cmap, cmap)` - Get processed data + extent - `get_extent()` - Get matplotlib extent [left, right, bottom, top] - **Image Generation**: - `to_image(width, height, power, cmap)` - Generate PIL Image - `to_matplotlib(figsize, power, cmap)` - Generate standalone figure - `save(filepath, ...)` - Save to file (PNG, JPG, etc.) - **Properties**: - `shape`, `duration`, `time_resolution`, `freq_bins` ### SwaraClient Updates (`idtap/client.py`) - `download_spectrogram_data(audio_id)` - Download compressed data - `download_spectrogram_metadata(audio_id)` - Download shape metadata ### Dependencies (`pyproject.toml`, `Pipfile`) - Added `numpy>=1.20.0` for array processing - Added `pillow>=9.0.0` for image generation - Added `matplotlib>=3.5.0` for visualization ## Testing: - 36 comprehensive test cases covering: - Data loading and initialization - Intensity transforms and colormap application - Frequency and time cropping - Matplotlib integration methods - Image generation and saving - All properties and edge cases - **All 401 tests pass** (365 existing + 36 new) ## Usage Example: ```python from idtap import SpectrogramData, get_piece import matplotlib.pyplot as plt # Load spectrogram piece = get_piece("transcription_id") spec = SpectrogramData.from_piece(piece) # Create visualization with spectrogram underlay fig, ax = plt.subplots(figsize=(12, 6)) spec.plot_on_axis(ax, power=2.5, cmap='viridis', alpha=0.6, zorder=0) # Overlay pitch contour ax.plot(times, freqs, 'r-', linewidth=2, zorder=1) ax.set_xlabel('Time (s)') ax.set_ylabel('Frequency (Hz)') plt.savefig('figure.png', dpi=300) ``` ## Documentation Updates: - Updated CLAUDE.md with testing warning about browser authorization ## Design Decisions: - Follows librosa/matplotlib patterns for research workflows - Optional client parameter (creates if not provided) - No caching in MVP (users can cache manually) - Uses matplotlib colormaps (close enough to D3, simpler) - Loads entire spectrogram into memory (suitable for typical sizes) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
- Add log_freq parameter to plot_on_axis() and to_matplotlib() (default: True) - Fixes issue where CQT log-spaced frequency bins were displayed with linear scale - Sets y-axis to log scale automatically for proper CQT representation
- Create docs/api/spectrogram.rst with SpectrogramData class documentation - Add usage examples for visualization and matplotlib overlays - Include technical details (CQT, frequency range, bins per octave) - Update docs/api/index.rst to include spectrogram in toctree and quick reference - Update docs/index.rst to list spectrogram feature
|
📦 Test Package Built Successfully! This PR has been automatically built and uploaded to TestPyPI for testing. 🔗 TestPyPI Link: https://test.pypi.org/project/idtap/ To test this version: pip install --index-url https://test.pypi.org/simple/ idtap✅ All tests passed and package builds successfully. |
|
@claude review |
|
Claude encountered an error —— View job I'll analyze this and get back to you. |
|
@claude review |
|
Claude finished @jon-myers's task —— View job Code Review for Spectrogram Support ImplementationReview Tasks
Code Quality Assessment✅ Excellent Implementation QualityThis is a very well-designed and thoroughly implemented feature. The code demonstrates excellent software engineering practices: Strong Architecture:
Robust Error Handling:
🎯 Design Decisions - Well JustifiedLogarithmic frequency scale default (
No caching by default - Simplified architecture, appropriate for research workflows Client optional pattern ( Test Coverage Analysis✅ Comprehensive TestingThe test suite is exemplary with 36 tests covering:
Test Quality Highlights:
Dependencies & Security✅ Well-Managed DependenciesAdded dependencies are appropriate:
Dependency sync maintained between 🔒 Security Assessment - GoodData Handling Security:
HTTP Client Security:
Performance Considerations⚡ Performance DesignEfficient Processing:
Memory Management:
Potential Improvements:
Minor Issues Found🔍 Small Areas for Improvement
Client.py Integration✅ Clean IntegrationNew methods are well-designed (
API Design:
Documentation Quality✅ Excellent DocumentationSphinx/RST documentation (
Code Documentation:
Overall Assessment🌟 Outstanding ImplementationThis is production-ready code that demonstrates:
🎯 Recommendation: ApproveThis PR implements a complex feature with exceptional quality. The code is:
The implementation successfully balances simplicity for researchers with the power needed for computational musicology workflows. |
Summary
Implements Issue #46 - Adds comprehensive spectrogram data access and visualization capabilities to the IDTAP Python API, specifically designed for computational musicology workflows like DN-Book-Figures.
Key Features
Implementation Details
New Files
idtap/spectrogram.py(643 lines) - Complete SpectrogramData classidtap/tests/spectrogram_test.py(450 lines) - 36 comprehensive testsdocs/api/spectrogram.rst- Full API documentation with examplesModified Files
idtap/client.py- Addeddownload_spectrogram_data()anddownload_spectrogram_metadata()idtap/__init__.py- ExportedSpectrogramDataandSUPPORTED_COLORMAPSpyproject.toml- Added dependencies: numpy, pillow, matplotlibPipfile- Synced dependenciesdocs/api/index.rst- Added spectrogram to API referencedocs/index.rst- Added spectrogram to features listKey Methods
Loading:
SpectrogramData.from_audio_id(audio_id, client)- Load from audio IDSpectrogramData.from_piece(piece, client)- Load from Piece objectVisualization:
plot_on_axis(ax, power, cmap, alpha, log_freq)- Plot on matplotlib axis for overlaysto_matplotlib(figsize, power, cmap, show_colorbar, log_freq)- Generate standalone figureget_plot_data(power, apply_cmap, cmap)- Low-level data accessProcessing:
apply_intensity(power)- Power-law contrast enhancementapply_colormap(data, cmap)- Apply matplotlib colormapscrop_frequency(min_hz, max_hz)- Crop to frequency rangecrop_time(start_time, end_time)- Crop to time segmentExport:
save(filepath, width, height, power, cmap)- Save as image fileto_image(width, height, power, cmap)- Generate PIL ImageTechnical Specifications
Testing
Dependencies
Added to
pyproject.toml:Example Usage
Design Decisions
Documentation
docs/api/spectrogram.rst🤖 Generated with Claude Code
Co-Authored-By: Claude noreply@anthropic.com