Skip to content

Commit

Permalink
More refactoring (#460)
Browse files Browse the repository at this point in the history
  • Loading branch information
nden authored Jul 5, 2023
2 parents 8cca37e + d0a9cff commit 0029940
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 36 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ other

- Fixed a call to ``astropy.coordinates`` in ``wcstools.wcs_from_points``. [#448]

- Code and docstrings clean up. [#460]

0.18.3 (2022-12-23)
-------------------
Bug Fixes
Expand Down
13 changes: 4 additions & 9 deletions gwcs/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,8 @@ def array_index_to_world_values(self, *index_arrays):
``i`` is the row and ``j`` is the column (i.e. the opposite order to
`~BaseLowLevelWCS.pixel_to_world_values`).
"""
index_arrays = self._add_units_input(index_arrays[::-1], self.forward_transform, self.input_frame)

result = self(*index_arrays, with_units=False)

return self._remove_quantity_output(result, self.output_frame)
pixel_arrays = index_arrays[::-1]
return self.pixel_to_world_values(*pixel_arrays)

def world_to_pixel_values(self, *world_arrays):
"""
Expand Down Expand Up @@ -150,12 +147,10 @@ def world_to_array_index_values(self, *world_arrays):
`~BaseLowLevelWCS.pixel_to_world_values`). The indices should be
returned as rounded integers.
"""
world_arrays = self._add_units_input(world_arrays, self.backward_transform, self.output_frame)
result = self.invert(*world_arrays, with_units=False)
result = self.world_to_pixel_values(*world_arrays)
if self.pixel_n_dim != 1:
result = result[::-1]

return self._remove_quantity_output(result, self.input_frame)
return result

@property
def array_shape(self):
Expand Down
39 changes: 12 additions & 27 deletions gwcs/wcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,23 +202,15 @@ def get_transform(self, from_frame, to_frame):
"""
if not self._pipeline:
return None
try:
from_ind = self._get_frame_index(from_frame)
except ValueError:
raise CoordinateFrameError("Frame {0} is not in the available "
"frames".format(from_frame))
try:
to_ind = self._get_frame_index(to_frame)
except ValueError:
raise CoordinateFrameError("Frame {0} is not in the available frames".format(to_frame))

from_ind = self._get_frame_index(from_frame)
to_ind = self._get_frame_index(to_frame)
if to_ind < from_ind:
#transforms = np.array(self._pipeline[to_ind: from_ind], dtype="object")[:, 1].tolist()
transforms = [step.transform for step in self._pipeline[to_ind: from_ind]]
transforms = [tr.inverse for tr in transforms[::-1]]
elif to_ind == from_ind:
return None
else:
#transforms = np.array(self._pipeline[from_ind: to_ind], dtype="object")[:, 1].copy()
transforms = [step.transform for step in self._pipeline[from_ind: to_ind]]
return functools.reduce(lambda x, y: x | y, transforms)

Expand Down Expand Up @@ -297,9 +289,11 @@ def _get_frame_index(self, frame):
"""
if isinstance(frame, cf.CoordinateFrame):
frame = frame.name
#frame_names = [getattr(item[0], "name", item[0]) for item in self._pipeline]
frame_names = [step.frame if isinstance(step.frame, str) else step.frame.name for step in self._pipeline]
return frame_names.index(frame)
try:
return frame_names.index(frame)
except ValueError as e:
raise CoordinateFrameError(f"Frame {frame} is not in the available frames") from e

def _get_frame_name(self, frame):
"""
Expand Down Expand Up @@ -503,7 +497,9 @@ def invert(self, *args, **kwargs):
else:
return result

def numerical_inverse(self, *args, **kwargs):
def numerical_inverse(self, *args, tolerance=1e-5, maxiter=50, adaptive=True,
detect_divergence=True, quiet=True, with_bounding_box=True,
fill_value=np.nan, with_units=False, **kwargs):
"""
Invert coordinates from output frame to input frame using numerical
inverse.
Expand Down Expand Up @@ -555,8 +551,6 @@ def numerical_inverse(self, *args, **kwargs):
iterations set by ``maxiter`` parameter. Instead,
simply return the found solution. Default is `True`.
Other Parameters
----------------
adaptive : bool, optional
Specifies whether to adaptively select only points that
did not converge to a solution within the required
Expand Down Expand Up @@ -740,15 +734,6 @@ def numerical_inverse(self, *args, **kwargs):
[2.76552923e-05 1.14789013e-05]]
"""
tolerance = kwargs.get('tolerance', 1e-5)
maxiter = kwargs.get('maxiter', 50)
adaptive = kwargs.get('adaptive', True)
detect_divergence = kwargs.get('detect_divergence', True)
quiet = kwargs.get('quiet', True)
with_bounding_box = kwargs.get('with_bounding_box', True)
fill_value = kwargs.get('fill_value', np.nan)
with_units = kwargs.pop('with_units', False)

if not utils.isnumerical(args[0]):
args = self.output_frame.coordinate_to_quantity(*args)
if self.output_frame.naxes == 1:
Expand Down Expand Up @@ -1204,14 +1189,14 @@ def insert_frame(self, input_frame, transform, output_frame):
output_name, output_frame_obj = self._get_frame_name(output_frame)
try:
input_index = self._get_frame_index(input_frame)
except ValueError:
except CoordinateFrameError:
input_index = None
if input_frame_obj is None:
raise ValueError(f"New coordinate frame {input_name} must "
"be defined")
try:
output_index = self._get_frame_index(output_frame)
except ValueError:
except CoordinateFrameError:
output_index = None
if output_frame_obj is None:
raise ValueError(f"New coordinate frame {output_name} must "
Expand Down

0 comments on commit 0029940

Please sign in to comment.