Skip to content

Adds slice method and associated tests#167

Open
MohamedAli1937 wants to merge 4 commits into
sunpy:mainfrom
MohamedAli1937:improve-slice
Open

Adds slice method and associated tests#167
MohamedAli1937 wants to merge 4 commits into
sunpy:mainfrom
MohamedAli1937:improve-slice

Conversation

@MohamedAli1937
Copy link
Copy Markdown

PR Description

This PR adds the slice() method to GenericSpectrogram to make it easier to extract a subset of the spectrogram by time and/or frequency. Previously, users had to manually compute array indices for slicing, which was error-prone.

fixes: #166

With this method:

  • spec.slice(time=(t_start, t_end), freq=(f_min, f_max)) returns a new GenericSpectrogram containing only the requested subset.

  • Metadata (times, freqs, start_time, end_time, wavelength) is automatically updated.

  • Original spectrogram remains unchanged.
    Example:

# Before slice method
sliced_data = data[1:5, 1:4]
sliced_times = times[1:5]
sliced_freqs = freqs[1:4]

# After slice method
sliced_spec = spec.slice(time=(times[1], times[4]), freq=(freqs[1], freqs[3]))
print(sliced_spec.times)       # [t1, t2, t3, t4]
print(sliced_spec.frequencies) # [f1, f2, f3]

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.

Screenshots:

Design sans titre (3)

test passed:

slice py (4)

@MohamedAli1937 MohamedAli1937 changed the title Adds slice method and propriete tests Adds slice method and associated tests Mar 1, 2026
@MohamedAli1937 MohamedAli1937 force-pushed the improve-slice branch 2 times, most recently from a856b6a to b926fcc Compare March 1, 2026 15:41
@samaloney
Copy link
Copy Markdown
Member

Hey thanks for the PR the implementation looks solid. But please see my comment on the corresponding issue.

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.

Make .slice() in GenericSpectrogram usage clearer

2 participants