Skip to content

Adds peek() function for GenericSpectrogram#188

Open
MohamedAli1937 wants to merge 5 commits intosunpy:mainfrom
MohamedAli1937:add-spectrogram-peek
Open

Adds peek() function for GenericSpectrogram#188
MohamedAli1937 wants to merge 5 commits intosunpy:mainfrom
MohamedAli1937:add-spectrogram-peek

Conversation

@MohamedAli1937
Copy link
Copy Markdown

PR Description

Fixes #185

Summary:
This PR adds a peek() method to the GenericSpectrogram base class to provide quick-look plotting functionality, aligning the object with the design philosophy and API conventions already established by sunpy.map.Map.peek() and radiospectra.spectrum.Spectrum.peek().

Motivation and Context:
Currently, taking a "quick look" at a GenericSpectrogram with a colorbar requires users to write roughly 5 lines of boilerplate code: manually importing matplotlib, setting up a figure and subplot, calling the underlying .plot() method, and manually passing the returned QuadMesh to a fig.colorbar(ret) call.

Providing a built-in peek() method reduces this to a single spec.peek() call, enabling users to immediately view their spectral data with sensible visualization defaults (like automatic colorbars and labels) upon instantiation.

Changes:

  • Added peek(self, **kwargs) to GenericSpectrogram in spectrogrambase.py.
  • The method safely wraps self.plot(**kwargs) (inherited from PcolormeshPlotMixin), catching the returned QuadMesh and automatically mapping it to a Figure.colorbar(). All extra **kwargs are cleanly passed down into the pcolormesh logic.
  • Included an RST changelog feature entry.

AI Assistance Disclosure

AI tools were used for:

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding
  • No AI tools were used

Regardless of AI use, the human contributor remains fully responsible for correctness, design choices, licensing compatibility, and long-term maintainability.

Adds a peek function with sensible defaults and a colorbar, similar to the API provided by sunpy.map.Map and the 1D Spectrum class, facilitating quick-look visualization.
Copy link
Copy Markdown
Member

@samaloney samaloney left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks ok, should add some narrative style documentation to the root https://github.com/sunpy/radiospectra/blob/main/docs/code_ref/spectrogram.rst describing, showing this.

f" {self.start_time.isot} to {self.end_time.isot}>"
)

def peek(self, **kwargs):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should probably use @peek_show like in sunpy so the figure isn't returned to have a similar API

import matplotlib.pyplot as plt

figure = plt.figure()
axes = figure.add_subplot(111)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think this is needed

Suggested change
axes = figure.add_subplot(111)
axes = figure.add_subplot()

figure = plt.figure()
axes = figure.add_subplot(111)
ret = self.plot(axes=axes, **kwargs)
figure.colorbar(ret)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be an option in the method e.g. show_coloarbar=True

@MohamedAli1937
Copy link
Copy Markdown
Author

Hi @samaloney, could you please take a look? My local tests pass with Python 3.14/SunPy 7.1, but I'm hitting 'Unknown problems' on the CI/RTD builds. I suspect a version compatibility issue with the decorator or a documentation timeout. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Adds a peek() method to GenericSpectrogram for quick-look visualizations

2 participants