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
Spatial filter seems to match positions outside region #65
Comments
@grahambell - Thanks for the bug report. I see the same output with Python 3.5, Astropy 1.2, Numpy 1.11.2, Cython 0.24.1. @grahambell - By any chance, do you already have a test case for this that can be run quickly? E.g. same example, but just with one or a few RA / DEC values where the result is incorrect? For now I've put a v1.3 milestone on this. |
I've started debugging this ... here's some notes. Script to print the RA / DEC values where the pyregion result is incorrect: First incorrect result is for:
Script to show what's going on for that case: Output: Looks like the issue is
However, this seems to work:
I'll continue looking at this after lunch. @leejjoon or anyone -- any idea what the issue or fix is? |
Changing milestone for this issue to 1.2. |
I've narrowed this down to a bug (I think) in Here's a small self-contained test case: from astropy.wcs import WCS
wcs = WCS(naxis=2)
wcs.wcs.radesys = 'ICRS'
wcs.wcs.ctype = ['RA---TAN', 'DEC--TAN']
wcs.wcs.cunit = ['deg', 'deg']
wcs.wcs.crpix = [1, 1]
wcs.wcs.cdelt = [-0.0003, 0.0003]
wcs.wcs.crval = [-141, 15]
header = wcs.to_header()
import pyregion
shape_list = pyregion.parse('fk5\ncircle(0:00:00.000,00:00:00.00,36000")')
# import IPython; IPython.embed()
shape_list2 = shape_list.as_imagecoord(header=header)
print(repr(header))
print('shape_list: ', shape_list)
print('shape_list2:', shape_list2) Output:
So for this header and shape_list, ShapeList.as_imagecoord doesn't convert to image coordinates or raise an error, but just return the shape in sky coordinates again. I've looked at bit at the I'll set the milestone back to 1.3 ... probably that bug has been there for years and IMO this shouldn't hold up the 1.2 release, which is otherwise ready to go. @leejjoon or anyone else: do you have time to look into this issue? |
Here's the full list of pixels (RA, DEC) in the first two columns where |
I try to track this problem, and the script below reproduces the source of the problem. from astropy.wcs import WCS
from astropy.coordinates import SkyCoord
wcs = WCS(naxis=2)
wcs.wcs.radesys = 'ICRS'
wcs.wcs.ctype = ['RA---TAN', 'DEC--TAN']
wcs.wcs.cunit = ['deg', 'deg']
wcs.wcs.crpix = [1, 1]
wcs.wcs.cdelt = [-0.0003, 0.0003]
wcs.wcs.crval = [-141, 15]
header = wcs.to_header()
new_wcs = WCS(header)
c1, c2 = 0, 0
coord_format = "fk5"
old_coordinate = SkyCoord(c1, c2,
frame=coord_format, unit='degree',
obstime='J2000')
print(old_coordinate.to_pixel(new_wcs, origin=1)) The resulting coordinates are NaNs. As far as I can see, this is because the coordinate given ([0, 0] in this example) is either not defined in the given projection or it overflows. I note that region filters that pyregion generates are based on their image coordinate. And things like radius is converted to a radius in the image plane simply using the plate scale. So, you should not use pyregion's filter if the sky curvature is not negligible. For pyregion, I guess the question is what should be its behavior when some of the input points are not defined in the image coordinate. My inclination is that we just raise an exception indicating that the filter is ill-defined. Thought? |
Maybe @nden or @astrofrog or someone that knows |
Is this a blocker for a pyregion 2.0 release or can it be moved to 2.1? @astrofrog or @keflavich - Could you please have a look at the example by @leejjoon above and comment whether this is expected behaviour for this WCS or a bug?
I think for operations that act on arrays the common idiom is to return >>> import numpy as np
>>> np.sqrt([3, -2])
RuntimeWarning: invalid value encountered in sqrt
array([ 1.73205081, nan]) which in my experience are more often annoying and have to be suppressed instead of helpful, but emitting warnings is certainly also a valid way to deal with this. |
If the projection diverges, e.g., if you try to use a tangent projection at one of the poles, I think it would be nice to raise a helpful exception. This is a case where an answer is theoretically possible, but it is definitely wrong, whereas the case @cdeil highlighted is where there is no theoretical answer; I'm happy with a NaN return in that situation. However, looking at the specific case @leejjoon posted and the original bug report, I don't know what's going on, since the requested coordinates that evaluate to NaN are generally nowhere near a pole. Or, does the TAN projection use the local coordinate as the origin? That would make sense... I'd have to do some more reading and/or testing to understand really what's going on. |
Then, how about changing the behavior of the filter so that it raises/warns if the input regions has NaN. |
Yes. |
Sorry, closed accidentally. Reopening it. |
I also think that returning As far as I can tell though, the spatial filter results for the TAN examples given above indicate a bug, i.e. simply aren't correct for unknown reasons (see image posted above). Is that true? Is there anyone that has the time and expertise to have a look and resolve this issue? |
The last stable pyregion 1.2 release is broken, so I'll go ahead and make a new release v2.0 now (see #94 ) |
The spatial filter seems to match positions which are not part of the specified region. In the following (very slow) test script, I test points over the whole sky using a 1 degree grid. The output has, as well as the two expected circles, lines at RA = +/- 90 degrees and a few other scattered points. This was with Astropy 1.0.6 and the current master version of pyregion from this repository.
The text was updated successfully, but these errors were encountered: