Skip to content
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

Fix pixel region index retrieval #1001

Merged
merged 11 commits into from
Jan 12, 2023
Merged

Conversation

duytnguyendtn
Copy link
Contributor

@duytnguyendtn duytnguyendtn commented Dec 22, 2022

When a u.pix defined region attempted to extract_region on a Spectrum1D defined with a spectral axis defined with u.pix, the region index bound detection algorithm failed to resolve the correct indices, instead returning the actual value of the pixel quantity of the spectral region. This resulted in an empty list being returned due to the slicing indices being incorrect.

Quick test snippet:

import astropy.units as u
flux_unit = u.dimensionless_unscaled
spec_unit = u.pix

import numpy as np
from specutils import Spectrum1D
flux = np.random.randn(200)*flux_unit
wavelength = np.arange(5100, 5300)*spec_unit
spec1d = Spectrum1D(spectral_axis=wavelength, flux=flux)

from specutils import SpectralRegion
from specutils.manipulation import extract_region
region = SpectralRegion.from_center(center=5200.5*spec_unit, width=100*spec_unit)
extract_region(spec1d, region)

should return:

Spectrum1D (length=101)
flux:             [ -0.14648, ..., 0.0035672 ],  mean=0.036819
spectral axis:    [ 5150.0 pix, ..., 5250.0 pix ],  mean=5200.0 pix

whereas on main:

Spectrum1D (length=0)
flux:             [ ],  mean= n/a
spectral axis:    [ ],  mean= n/a

tox test failures appear to be unrelated, possibly related to the same issues we've seen in spacetelescope/jdaviz#1901. Related tests are all testing locally

@pllim
Copy link
Member

pllim commented Dec 23, 2022

As discussed, this could use more tests to cover edge cases.

What if region is completely out of bounds? Only 1 pix overlap? Within bounds but somehow falls between the gaps of sampled points?

@pllim
Copy link
Member

pllim commented Dec 23, 2022

If you rebase after #1002 is merged, the CI should be green again unless this PR broke something.

@duytnguyendtn duytnguyendtn force-pushed the pixelregionextract branch 2 times, most recently from a6cd989 to cfb8526 Compare December 23, 2022 20:50
@codecov
Copy link

codecov bot commented Dec 23, 2022

Codecov Report

Attention: Patch coverage is 91.66667% with 1 line in your changes missing coverage. Please review.

Project coverage is 69.98%. Comparing base (c9633e6) to head (9aa0dca).
Report is 151 commits behind head on main.

Files with missing lines Patch % Lines
specutils/manipulation/extract_spectral_region.py 90.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1001      +/-   ##
==========================================
+ Coverage   69.93%   69.98%   +0.04%     
==========================================
  Files          64       64              
  Lines        4321     4331      +10     
==========================================
+ Hits         3022     3031       +9     
- Misses       1299     1300       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@rosteen rosteen left a comment

Choose a reason for hiding this comment

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

Mostly looks good, left a couple comments. Mainly I think the check on ascending pixel axis should go in SpectralAxis.__new__ rather than appearing twice here.

specutils/manipulation/extract_spectral_region.py Outdated Show resolved Hide resolved
specutils/manipulation/extract_spectral_region.py Outdated Show resolved Hide resolved
duytnguyendtn and others added 3 commits January 9, 2023 16:13
Co-authored-by: Ricky O'Steen <39831871+rosteen@users.noreply.github.com>
Copy link
Contributor

@rosteen rosteen left a comment

Choose a reason for hiding this comment

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

LGTM now, thanks!

@rosteen rosteen merged commit 34b5e84 into astropy:main Jan 12, 2023
@duytnguyendtn
Copy link
Contributor Author

Thanks Ricky and Pey-Lian!

@duytnguyendtn duytnguyendtn deleted the pixelregionextract branch January 12, 2023 19:02
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.

3 participants