-
-
Notifications
You must be signed in to change notification settings - Fork 132
extract_region: preserve WCS for single regions, add velocity-region support #1263
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d3dad78
Region slicing now works with non-native units (issue 1261). extract_…
harry353 c4c3d39
Applied reviewer suggestions: narrow down exception, preserve_wcs=Fal…
harry353 21a5e85
Update specutils/manipulation/extract_spectral_region.py
rosteen f4cf603
Merge branch 'extract-region-test-branch' of https://github.com/harry…
rosteen 4cdb853
Fix off-by-one error in _subregion_to_edge_pixels upper bound handling
harry353 8cb515a
Merge branch 'extract-region-test-branch' of https://github.com/harry…
rosteen f43a4a1
codestyle
rosteen d982ded
Changelog
rosteen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import numpy as np | ||
| import astropy.units as u | ||
| from numpy.testing import assert_allclose | ||
| import pytest | ||
| from astropy.wcs import WCS | ||
|
|
||
| from specutils.spectra.spectrum import Spectrum | ||
| from specutils.spectra.spectral_region import SpectralRegion | ||
| from specutils.manipulation import extract_region | ||
|
|
||
| @pytest.fixture | ||
| def frequency_spectrum(): | ||
| # Create basic frequency WCS | ||
| w = WCS(naxis=1) | ||
| w.wcs.crval = [1.410e9] # starting frequency (Hz) | ||
| w.wcs.cdelt = [1.0e6] # 1 MHz per channel | ||
| w.wcs.crpix = [1] # reference pixel | ||
| w.wcs.cunit = ['Hz'] | ||
| w.wcs.restfrq = 1.420e9 # rest frequency in Hz | ||
|
|
||
| # Build spectral axis and flux | ||
| freqs = np.arange(1.410e9, 1.431e9, 1.0e6) * u.Hz | ||
| flux = np.arange(1, len(freqs) + 1, dtype=float) * u.Jy | ||
|
|
||
| return Spectrum(spectral_axis=freqs, flux=flux, wcs=w, velocity_convention='radio') | ||
|
|
||
|
|
||
| def test_extract_region_velocity_on_frequency_axis(frequency_spectrum): | ||
| spec = frequency_spectrum | ||
|
|
||
| # Define velocity range | ||
| region = SpectralRegion(-500 * u.km / u.s, 500 * u.km / u.s) | ||
|
|
||
| # Extract region with WCS preservation | ||
| sub = extract_region(spec, region, preserve_wcs=True) | ||
|
|
||
| # Determine expected frequency channels based on velocity condition | ||
| velocities = spec.velocity.to(u.km / u.s) | ||
| mask = (velocities >= -500 * u.km / u.s) & (velocities <= 500 * u.km / u.s) | ||
| expected_freqs = spec.spectral_axis[mask] | ||
| expected_flux = spec.flux[mask] | ||
|
|
||
| # Assertions | ||
| assert len(sub.spectral_axis) == len(expected_freqs) | ||
| assert_allclose(sub.spectral_axis.to_value(u.Hz), | ||
| expected_freqs.to_value(u.Hz), | ||
| rtol=0, atol=1e-12) | ||
|
|
||
| assert_allclose(sub.flux.to_value(u.Jy), | ||
| expected_flux.to_value(u.Jy), | ||
| rtol=0, atol=0) | ||
|
|
||
| assert np.isclose(sub.wcs.wcs.crval[0], expected_freqs[0].value) | ||
| assert np.isclose(sub.wcs.wcs.crpix[0], 1) | ||
| assert np.isclose(sub.wcs.wcs.cdelt[0], spec.wcs.wcs.cdelt[0]) | ||
| assert sub.wcs.wcs.restfrq == spec.wcs.wcs.restfrq | ||
|
|
||
| def test_extract_region_drops_wcs_when_disabled(frequency_spectrum): | ||
| spec = frequency_spectrum | ||
|
|
||
| # Define velocity range | ||
| region = SpectralRegion(-500 * u.km / u.s, 500 * u.km / u.s) | ||
|
|
||
| # Extract region without WCS preservation | ||
| sub = extract_region(spec, region, preserve_wcs=False) | ||
|
|
||
| # Basic content check | ||
| velocities = spec.velocity.to(u.km / u.s) | ||
| mask = (velocities >= -500 * u.km / u.s) & (velocities <= 500 * u.km / u.s) | ||
| expected_flux = spec.flux[mask] | ||
|
|
||
| assert_allclose(sub.flux.to_value(u.Jy), | ||
| expected_flux.to_value(u.Jy), | ||
| rtol=0, atol=0) | ||
|
|
||
| # Ensure WCS is removed | ||
| assert not hasattr(sub, "wcs") or sub.wcs is None |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current
Falsebehavior does keep a WCS, it just turns it into a lookuptable WCS - so it doesn't exactly drop the WCS, it changes it.