Skip to content

Add gallery examples for cropping and extracting profiles - #263

Merged
samaloney merged 15 commits into
sunpy:ndcube-refactorfrom
Amityush-lgtm:slicing-examples
Aug 20, 2026
Merged

Add gallery examples for cropping and extracting profiles#263
samaloney merged 15 commits into
sunpy:ndcube-refactorfrom
Amityush-lgtm:slicing-examples

Conversation

@Amityush-lgtm

@Amityush-lgtm Amityush-lgtm commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

PR Description

This PR adds a new gallery examples demonstrating slicing and cropping using the ndcube API as discussed in #262.

Changes:

  • Added plot_cropping.py to demonstrate cropping spectrograms and extracting time profiles and line profiles using NDCube's crop() and crop_by_values().

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.

Comment thread examples/plot_cropping.py Outdated
@@ -0,0 +1,89 @@
"""
Cropping a WIND/WAVES spectrogram

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.

Suggested change
Cropping a WIND/WAVES spectrogram
Cropping a Spectrogram

as it not specifically to WIND/WAVES that just the data being used for the example

Comment thread examples/plot_cropping.py Outdated
# Two key methods are available:
#
# * `~ndcube.NDCube.crop` — accepts high-level coordinate objects
# (e.g. `~astropy.time.Time`).

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.

add SpectralCoord too

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Got it, i'll add SpectralCoord :)

Comment thread examples/plot_cropping.py
spec = waves_specs[0]

###############################################################################
# Cropping by time

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.

Hum maybe just Crop and show how to crop using only one axes e.g. Time then the other e.g. SpectralCoord and the using both?

Comment thread examples/plot_cropping.py
time_cropped = spec.crop((start_time, None), (end_time, None))

###############################################################################
# Cropping by frequency

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.

again maybe keep same as above for by time using u.s then frequency as a quantity an then both?

Comment thread examples/plot_profiles.py Outdated

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.

I'm not sure but could maybe fold this into the other example as its an extreme version of cropping to a single axis

@Amityush-lgtm

Copy link
Copy Markdown
Contributor Author

Ive pushed the suggested changes and updated the examples. While testing locally, the NASA SPDF server is having some issues so fido.search() was getting stuck query, i tried with some other instruments and they are working

@samaloney

Copy link
Copy Markdown
Member

Need to megre / rebase main into here or at least update the miniforge3-latest change on the RTD config so the docs build.

@Amityush-lgtm

Copy link
Copy Markdown
Contributor Author

I have updated the RTD config

@Amityush-lgtm

Copy link
Copy Markdown
Contributor Author

@samaloney i have brought all the requirements that were added in the Waves Example gallery setup here to check the example here, let me know if this looks right

@samaloney

Copy link
Copy Markdown
Member

Yea I guess we should keep the ndcube branch up today with main to avoid having to make the same changes manually

@Amityush-lgtm

Amityush-lgtm commented Aug 12, 2026

Copy link
Copy Markdown
Contributor Author

That sounds good, but since @hayesla created the ndcube-refactor branch, i dont believe i have permissions to merge main into it,
Would you like me to open a PR from main into ndcube-refactor instead???

Comment thread changelog/263.doc.rst Outdated
@@ -0,0 +1 @@
Add two new gallery examples (`plot_cropping.py` and `plot_profiles.py`) demonstrating how to slice, crop and extract 1D profiles from a `~radiospectra.spectrogram.GenericSpectrogram` using native `ndcube` methods.

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.

I think this changelog needs to be updated also for the links to work you'll need to link to the build doc rather than the source so e.g.

:doc:`generated/gallery/plot_cropping`

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Got it!

Comment thread docs/conf.py Outdated
Comment on lines +75 to +77
# Set automodapi to generate files inside the generated directory
automodapi_toctreedirnm = "generated/api"

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.

What's the reason behind this change?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

My bad, i think i missed to remove this duplicate while working on the conflict,

Comment thread examples/plot_cropping.py
start_time = Time("2017-09-02T15:30:00")
end_time = Time("2017-09-02T17:00:00")

time_cropped = spec.crop((start_time, None), (end_time, None))

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.

I think should show the equivalent using crop_by_values

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I have added small comments in the code to show the equivalent, let me know how it looks

Comment thread examples/plot_cropping.py
low_freq = 200 * u.kHz
high_freq = 600 * u.kHz

freq_cropped = spec.crop_by_values((None, low_freq), (None, high_freq))

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.

kinda same here show the equivalent using crop

Comment thread examples/plot_cropping.py
low_freq_coord = SpectralCoord(200 * u.kHz)
high_freq_coord = SpectralCoord(600 * u.kHz)

both_cropped = spec.crop((start_time, low_freq_coord), (end_time, high_freq_coord))

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.

again maybe show the equivalent using crop_by_values

@Amityush-lgtm

Copy link
Copy Markdown
Contributor Author

Im looking into figure tests failure and it seems that the hashes have changed, i dont know why maybe because of ndcube, so i will update them and also check for the right version of mpl_animators

Comment thread examples/plot_cropping.py Outdated
end_time = Time("2017-09-02T17:00:00")

time_cropped = spec.crop((start_time, None), (end_time, None))
# Or equivalently using crop_by_values (which uses the WCS lookup table units of seconds):

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.

you need to be very strict with line length on the examples or it will vanish out of the code cell.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ill reduce it a bit

@Hermanlrx Hermanlrx left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just add colour bars when you find time! Otherwise lgtm

@samaloney
samaloney merged commit 9c9324a into sunpy:ndcube-refactor Aug 20, 2026
46 of 47 checks passed
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.

5 participants