Skip to content

Commit

Permalink
Merge pull request #32 from Cadair/ape14
Browse files Browse the repository at this point in the history
first pass at using the ape 14 methods on gWCS
  • Loading branch information
Cadair authored Jan 28, 2019
2 parents 9a73afd + 8dc7290 commit 2ff2f19
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 13 deletions.
1 change: 1 addition & 0 deletions changelog/32.trivial.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Migrate the `dkist.dataset.Dataset` class to use gWCS's APE 14 API
23 changes: 18 additions & 5 deletions dkist/dataset/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def __init__(self, data, uncertainty=None, mask=None, wcs=None,
super().__init__(data, uncertainty, mask, wcs, meta, unit, copy)

if self.wcs and missing_axis is None:
self.missing_axis = [False] * self.wcs.forward_transform.n_outputs
self.missing_axis = [False] * self.wcs.world_n_dim
else:
self.missing_axis = missing_axis

Expand Down Expand Up @@ -137,9 +137,22 @@ def world_axes_names(self):
else:
return ('',) * self.data.ndim # pragma: no cover # We should never hit this

@property
def world_axis_physical_types(self):
"""
Returns an iterable of strings describing the physical type for each world axis.
The strings conform to the International Virtual Observatory Alliance
standard, UCD1+ controlled Vocabulary. For a description of the standard and
definitions of the different strings and string components,
see http://www.ivoa.net/documents/latest/UCDlist.html.
"""
return self.wcs.world_axis_physical_types[::-1]

@property
def axis_units(self):
return self.wcs.output_frame.unit[::-1]
return tuple(map(u.Unit, self.wcs.world_axis_units[::-1]))

def __repr__(self):
"""
Expand Down Expand Up @@ -175,7 +188,7 @@ def pixel_to_world(self, *quantity_axis_list):
coord : `list`
A list of arrays containing the output coordinates.
"""
world = self.wcs(*quantity_axis_list[::-1], with_units=True)
world = self.wcs.pixel_to_world(*quantity_axis_list[::-1])

# Convert list to tuple as a more standard return type
if isinstance(world, list):
Expand Down Expand Up @@ -205,7 +218,7 @@ def world_to_pixel(self, *quantity_axis_list):
coord : `list`
A list of arrays containing the output coordinates.
"""
return tuple(self.wcs.invert(*quantity_axis_list[::-1], with_units=True))[::-1]
return tuple(self.wcs.world_to_pixel(*quantity_axis_list[::-1]))[::-1]

def world_axis_physical_types(self):
raise NotImplementedError() # pragma: no cover
Expand Down Expand Up @@ -267,7 +280,7 @@ def crop_by_coords(self, lower_corner, upper_corner, units=None):
all_world_corners = [all_world_corners_grid[i].flatten()*lower_corner[i].unit
for i in range(n_dim)]
# Convert to pixel coordinates
all_pix_corners = self.wcs(*all_world_corners, with_units=False)
all_pix_corners = self.wcs.world_to_pixel(*all_world_corners)
# Derive slicing item with which to slice NDCube.
# Be sure to round down min pixel and round up + 1 the max pixel.
item = tuple([slice(int(np.clip(axis_pixels.value.min(), 0, None)),
Expand Down
2 changes: 1 addition & 1 deletion dkist/dataset/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_repr(dataset, dataset_3d):
def test_wcs_roundtrip(dataset):
p = (10*u.pixel, 10*u.pixel)
w = dataset.pixel_to_world(*p)
p2 = dataset.world_to_pixel(w)
p2 = dataset.world_to_pixel(*w)
assert_quantity_allclose(p, p2)


Expand Down
12 changes: 6 additions & 6 deletions dkist/wcs/slicer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import numpy as np

import gwcs.coordinate_frames as cf
from astropy.modeling import Model, Parameter, separable
from astropy.modeling import Model, separable
from astropy.modeling.models import Shift, Identity

from dkist.utils.model_tools import re_model_trees, remove_input_frame
Expand All @@ -22,7 +22,8 @@ def __init__(self, input_specification):

@property
def inputs(self):
return tuple(f"n{i}" for i in range(len(self.input_specification)) if self.input_specification[i] is None)
return tuple(f"n{i}" for i in range(len(self.input_specification))
if self.input_specification[i] is None)

@property
def outputs(self):
Expand Down Expand Up @@ -168,7 +169,7 @@ def _new_output_frame(self, axes):
start = start[-1] # pragma: no cover # I can't work out how to hit this.
# Increment start for the next frame.
start += 1
frame._axes_order = tuple(range(start, start+frame.naxes))
frame._axes_order = tuple(range(start, start + frame.naxes))

if len(frames) == 1:
return frames[0]
Expand Down Expand Up @@ -263,11 +264,11 @@ def _convert_item_to_models(self, item, drop_all_non_separable):
elif not self.separable[i] and drop_all_non_separable:
axes_to_drop.append(i)
else:
inputs.append(ax*input_units[i])
inputs.append(ax * input_units[i])
prepend.append(Identity(1))
elif ax.start:
inputs.append(None)
prepend.append(Shift(ax.start*input_units[i]))
prepend.append(Shift(ax.start * input_units[i]))
else:
inputs.append(None)
prepend.append(Identity(1))
Expand Down Expand Up @@ -310,7 +311,6 @@ def __getitem__(self, item):
remove_coupled_trees=drop_all_non_separable)
model = re_model_trees(trees)


if not all([isinstance(a, Identity) for a in prepend]):
model = self._list_to_compound(prepend) | model

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ exclude = extern,sphinx,*parsetab.py

[flake8]
max-line-length = 100
ignore = I100,I102,I103,I104,I201
ignore = I100,I101,I102,I103,I104,I201

[yapf]
column_limit = 100
Expand Down

0 comments on commit 2ff2f19

Please sign in to comment.