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

Respect the bounding_box in inverse transforms #498

Merged
merged 19 commits into from
Dec 13, 2024

Conversation

nden
Copy link
Collaborator

@nden nden commented May 14, 2024

Fixes #496

This fixes a bug in filtering out coordinates that are outside the footprint when evaluating the inverse transform. This affects also the in_image function. Previously the output of the backward transform was compared to the bounding_box and values outside it were replaced by NaNs. This PR adds an second step which is run before evaluating the backward transform and assigns output of NaN to inputs outside the footprint.

@nden nden requested a review from a team as a code owner May 14, 2024 16:33
Copy link

codecov bot commented May 14, 2024

Codecov Report

Attention: Patch coverage is 91.13924% with 7 lines in your changes missing coverage. Please review.

Project coverage is 87.50%. Comparing base (9cd8552) to head (08bafd0).
Report is 9 commits behind head on master.

Files with missing lines Patch % Lines
gwcs/wcs.py 92.85% 5 Missing ⚠️
gwcs/api.py 60.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #498      +/-   ##
==========================================
+ Coverage   87.42%   87.50%   +0.08%     
==========================================
  Files          22       22              
  Lines        3874     3931      +57     
==========================================
+ Hits         3387     3440      +53     
- Misses        487      491       +4     

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

@nden
Copy link
Collaborator Author

nden commented May 14, 2024

@bmorris3 Do you mind testing this PR with your code?
Since this changes the functionality, there are a number of failures in the jwst pipeline. I'll need to address them before this is merged or make this the non-default behavior.

@nden nden added this to the 0.22 milestone May 14, 2024
@nden nden requested a review from Cadair May 17, 2024 15:52
@nden
Copy link
Collaborator Author

nden commented May 17, 2024

@Cadair Can you take a look at this PR and see if it's OK for sun related code?
Note that enforcing the bounding_box on the inverse transform now works and this can change results. If reproducing the bug is necessary, a parameter should be passed with_bounding_box=False.

@nden nden marked this pull request as ready for review December 13, 2024 20:41
gwcs/api.py Outdated Show resolved Hide resolved
gwcs/tests/conftest.py Outdated Show resolved Hide resolved
gwcs/tests/test_coordinate_systems.py Outdated Show resolved Hide resolved
gwcs/tests/test_coordinate_systems.py Outdated Show resolved Hide resolved
gwcs/tests/test_coordinate_systems.py Outdated Show resolved Hide resolved
gwcs/tests/test_wcs.py Outdated Show resolved Hide resolved
gwcs/tests/test_wcs.py Outdated Show resolved Hide resolved
@nden nden merged commit 863b6e4 into spacetelescope:master Dec 13, 2024
23 of 28 checks passed
not_numerical = True
world_arrays = high_level_objects_to_values(*world_arrays, low_level_wcs=self)
for axtyp in axes_types:
ind = np.asarray((np.asarray(self.output_frame.axes_type) == axtyp))
Copy link
Member

Choose a reason for hiding this comment

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

I don't think outer np.asarray is needed.

@@ -501,7 +489,57 @@ def invert(self, *args, **kwargs):
else:
return result

def numerical_inverse(self, *args, tolerance=1e-5, maxiter=50, adaptive=True,
def outside_footprint(self, world_arrays):
Copy link
Member

Choose a reason for hiding this comment

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

Add docstrings

return world_arrays


def out_of_bounds(self, pixel_arrays, fill_value=np.nan):
Copy link
Member

Choose a reason for hiding this comment

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

missing docstrings for a public function. Actually, maybe make these functions private?

axis_range = footprint[:, idim]
else:
axis_range = footprint
range = [axis_range.min(), axis_range.max()]
Copy link
Member

Choose a reason for hiding this comment

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

This should be something like:

range = sorted([wrap_ra(axis_range.min(), wrapping_angle=180 or 360), wrap_ra(axis_range.max(), wrapping_angle=180 or 360)]

or _dec (instead of _ra). To do this correctly, look into the transforms in gwcs pipeline and find CartesianToSpherical. Then look at CartesianToSpherical.wrap_lon_at to find the actual wrapping angle. However, this only for lon (RA). For lat I think the range is [-90, 90]. So a different treatment should be for that coordinate.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe something like this:

                range = [axis_range.min(), axis_range.max()]
                if abs(range[1] - range[0]) >= 350:
                    # most likely this coordinate is wrapped at 360
                    range = [range[1] - 360, range[0]]
                elif abs(range[1] - range[0]) >= 170:
                    # most likely this coordinate is wrapped at 180
                    range = [range[1] - 180, range[0]]

could detect phase jumps and unwrap one of the range's limit. This may need some tweaking.

Copy link
Member

Choose a reason for hiding this comment

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

Also, range is a built-in function. I suggest changing the variable name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug in treatment of pixel_bounds
3 participants