Skip to content

Commit

Permalink
Merge pull request #157 from gnshafreak/missing_axes
Browse files Browse the repository at this point in the history
changed all instances of "missing_axis" to "missing_axes"
  • Loading branch information
DanRyanIrish authored May 14, 2019
2 parents 3267feb + f46f6a4 commit 95b60b1
Show file tree
Hide file tree
Showing 14 changed files with 145 additions and 136 deletions.
1 change: 1 addition & 0 deletions changelog/157.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Changed all instances of "missing_axis" to "missing_axes"
10 changes: 5 additions & 5 deletions docs/ndcube.rst
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ the WCS object would cause the translations to break.

To deal with this scenario, `~ndcube.NDCube` supports "missing" WCS
axes. An additional attribute is added to the WCS object
(`NDCube.wcs.missing_axis`) which is a list of `bool` type indicating
(`NDCube.wcs.missing_axes`) which is a list of `bool` type indicating
which WCS axes do not have a corresponding data axis. This allows
translation information on coupled axes to persist even if the data
axes do not. This feature also makes it possible for `~ndcube.NDCube`
Expand All @@ -175,7 +175,7 @@ Extra Coordinates
-----------------

In the case of some datasets, there may be additional translations
between the array elements and real world coordinates that are
between the array elements and real world coordinates that are
not included in the WCS. Consider a 3-D data cube from a rastering
slit-spectrograph instrument. The first axis corresponds to the
x-position of the slit as it steps across a region of interest in a
Expand All @@ -195,7 +195,7 @@ iterable of tuples of the form (`str`, `int`,
entry gives the name of the coordinate, the 1st entry gives the data
axis to which the extra coordinate corresponds, and the 2nd entry
gives the value of that coordinate at each pixel along the axis. So
to add timestamps along the 0th axis of ``my_cube`` we do::
to add timestamps along the 0th axis of ``my_cube`` we do::

>>> from datetime import datetime, timedelta
>>> # Define our timestamps. Must be same length as data axis.
Expand Down Expand Up @@ -296,7 +296,7 @@ Coordinate Transformations
--------------------------

The fundamental point the WCS system is the ability to easily
translate between pixel and real world coordinates. For this purpose,
translate between pixel and real world coordinates. For this purpose,
`~ndcube.NDCube` provides convenience wrappers for the better known
astropy functions, `astropy.wcs.WCS.all_pix2world` and
`astropy.wcs.WCS.all_world2pix`. These are
Expand Down Expand Up @@ -426,7 +426,7 @@ is in fact the default.::
(<Quantity [[0.60002173, 0.59999127, 0.5999608 , 0.59993033],
[1. , 1. , 1. , 1. ],
[1.39997827, 1.40000873, 1.4000392 , 1.40006967]] deg>,
<Quantity [[1.26915033e-05, 4.99987815e-01, 9.99962939e-01,
<Quantity [[1.26915033e-05, 4.99987815e-01, 9.99962939e-01,
1.49986193e+00],
[1.26918126e-05, 5.00000000e-01, 9.99987308e-01,
1.49989848e+00],
Expand Down
16 changes: 8 additions & 8 deletions ndcube/mixins/ndslicing.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,25 @@ def _slice(self, item):
`astropy.nddata.mixins.NDSlicingMixin.__getitem__`.
This method extends the `~astropy.nddata.mixins.NDSlicingMixin` method
to add support for ``missing_axis`` and ``extra_coords`` and overwrites
to add support for ``missing_axes`` and ``extra_coords`` and overwrites
the astropy handling of wcs slicing.
"""
kwargs = super()._slice(item)

wcs, missing_axis = self._slice_wcs_missing_axis(item)
wcs, missing_axes = self._slice_wcs_missing_axes(item)
kwargs['wcs'] = wcs
kwargs['missing_axis'] = missing_axis
kwargs['extra_coords'] = self._slice_extra_coords(item, missing_axis)
kwargs['missing_axes'] = missing_axes
kwargs['extra_coords'] = self._slice_extra_coords(item, missing_axes)

return kwargs

def _slice_wcs_missing_axis(self, item):
def _slice_wcs_missing_axes(self, item):
# here missing axis is reversed as the item comes already in the reverse order
# of the input
return utils.wcs._wcs_slicer(
self.wcs, copy.deepcopy(self.missing_axis[::-1]), item)
self.wcs, copy.deepcopy(self.missing_axes[::-1]), item)

def _slice_extra_coords(self, item, missing_axis):
def _slice_extra_coords(self, item, missing_axes):
if self.extra_coords is None:
new_extra_coords_dict = None
else:
Expand All @@ -77,5 +77,5 @@ def _slice_extra_coords(self, item, missing_axis):
except TypeError:
pass
new_extra_coords_dict = utils.cube.convert_extra_coords_dict_to_input_format(
new_extra_coords, missing_axis)
new_extra_coords, missing_axes)
return new_extra_coords_dict
8 changes: 4 additions & 4 deletions ndcube/mixins/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def _plot_2D_cube(self, axes=None, plot_axis_indices=None, axes_coordinates=None
if self.wcs.naxis is not 2:
slice_list = []
index = 0
for i, bool_ in enumerate(self.missing_axis):
for i, bool_ in enumerate(self.missing_axes):
if not bool_:
slice_list.append(axis_data[index])
index += 1
Expand All @@ -220,12 +220,12 @@ def _plot_2D_cube(self, axes=None, plot_axis_indices=None, axes_coordinates=None
ax = wcsaxes_compat.gca_wcs(self.wcs, slices=slice_list)
# Set axis labels
x_wcs_axis = utils.cube.data_axis_to_wcs_axis(plot_axis_indices[0],
self.missing_axis)
self.missing_axes)
ax.set_xlabel("{0} [{1}]".format(
self.world_axis_physical_types[plot_axis_indices[0]],
self.wcs.wcs.cunit[x_wcs_axis]))
y_wcs_axis = utils.cube.data_axis_to_wcs_axis(plot_axis_indices[1],
self.missing_axis)
self.missing_axes)
ax.set_ylabel("{0} [{1}]".format(
self.world_axis_physical_types[plot_axis_indices[1]],
self.wcs.wcs.cunit[y_wcs_axis]))
Expand Down Expand Up @@ -313,7 +313,7 @@ def _plot_3D_cube(self, plot_axis_indices=None, axes_coordinates=None,
# If there are missing axes in WCS object, add corresponding dummy axes to data.
if data.ndim < self.wcs.naxis:
new_shape = list(data.shape)
for i in np.arange(self.wcs.naxis)[self.missing_axis[::-1]]:
for i in np.arange(self.wcs.naxis)[self.missing_axes[::-1]]:
new_shape.insert(i, 1)
# Also insert dummy coordinates and units.
axes_coordinates.insert(i, None)
Expand Down
22 changes: 11 additions & 11 deletions ndcube/mixins/sequence_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ def _plot_2D_sequence_as_1Dline(self, axes_coordinates=None,
if unit_x_axis is None:
print('b', unit_x_axis)
unit_x_axis = np.asarray(self[0].wcs.wcs.cunit)[
np.invert(self[0].missing_axis)][0]
np.invert(self[0].missing_axes)][0]
print('c', unit_x_axis)
xdata = u.Quantity(np.concatenate([cube.axis_world_coords().to(unit_x_axis).value
for cube in self.data]), unit=unit_x_axis)
Expand Down Expand Up @@ -452,7 +452,7 @@ def _plot_2D_sequence(self, plot_axis_indices=None, axes_coordinates=None,
if axes_coordinates[cube_axis_index] is None:
if cube_axis_unit is None:
cube_axis_unit = np.array(self[0].wcs.wcs.cunit)[
np.invert(self[0].missing_axis)][0]
np.invert(self[0].missing_axes)][0]
cube_axis_coords = self[0].axis_world_coords().to(cube_axis_unit).value
cube_axis_name = self.world_axis_physical_types[1]
else:
Expand Down Expand Up @@ -564,7 +564,7 @@ def _plot_3D_sequence_as_2Dimage(self, axes=None, plot_axis_indices=None,
if axes_coordinates[cube_axis_index] is None:
if cube_axis_unit is None:
cube_axis_unit = np.array(self[0].wcs.wcs.cunit)[
np.invert(self[0].missing_axis)][0]
np.invert(self[0].missing_axes)][0]
cube_axis_coords = \
self[0].axis_world_coords()[cube_axis_index].to(cube_axis_unit).value
cube_axis_name = self.cube_like_world_axis_physical_types[1]
Expand Down Expand Up @@ -594,7 +594,7 @@ def _plot_3D_sequence_as_2Dimage(self, axes=None, plot_axis_indices=None,
# Concatenate values along common axis for each cube.
if common_axis_unit is None:
wcs_common_axis_index = utils.cube.data_axis_to_wcs_axis(
common_axis_index, self[0].missing_axis)
common_axis_index, self[0].missing_axes)
common_axis_unit = np.array(self[0].wcs.wcs.cunit)[wcs_common_axis_index]
common_axis_coords = u.Quantity(np.concatenate(
[cube.axis_world_coords()[common_axis_index].to(common_axis_unit).value
Expand Down Expand Up @@ -727,7 +727,7 @@ def __init__(self, seq, wcs=None, axes=None, plot_axis_indices=None,
# shape for an missing axes.
if seq[0].wcs.naxis != len(seq.dimensions) - 1:
new_shape = list(data_stack.shape)
for i in np.arange(seq[0].wcs.naxis)[seq[0].missing_axis[::-1]]:
for i in np.arange(seq[0].wcs.naxis)[seq[0].missing_axes[::-1]]:
new_shape.insert(i+1, 1)
# Also insert dummy coordinates and units.
axes_coordinates.insert(i+1, None)
Expand Down Expand Up @@ -827,7 +827,7 @@ def __init__(self, seq, wcs=None, axes=None, plot_axis_indices=None,
# shape for an missing axes.
if seq[0].wcs.naxis != len(seq._dimensions) - 1:
new_shape = list(data_concat.shape)
for i in np.arange(seq[0].wcs.naxis)[seq[0].missing_axis[::-1]]:
for i in np.arange(seq[0].wcs.naxis)[seq[0].missing_axes[::-1]]:
new_shape.insert(i, 1)
# Also insert dummy coordinates and units.
axes_coordinates.insert(i, None)
Expand Down Expand Up @@ -962,7 +962,7 @@ def __init__(self, seq, plot_axis_index=None, axis_ranges=None, unit_x_axis=None
# Define unit of x-axis if not supplied by user.
if unit_x_axis is None:
wcs_plot_axis_index = utils.cube.data_axis_to_wcs_axis(
cube_plot_axis_index, seq[0].missing_axis)
cube_plot_axis_index, seq[0].missing_axes)
unit_x_axis = np.asarray(seq[0].wcs.wcs.cunit)[wcs_plot_axis_index]
# Get x-axis values from each cube and combine into a single
# array for axis_ranges kwargs.
Expand Down Expand Up @@ -1193,16 +1193,16 @@ def __init__(self, seq, plot_axis_index=None, axis_ranges=None, unit_x_axis=None
# Define unit of x-axis if not supplied by user.
if unit_x_axis is None:
wcs_plot_axis_index = utils.cube.data_axis_to_wcs_axis(
plot_axis_index, seq[0].missing_axis)
plot_axis_index, seq[0].missing_axes)
unit_x_axis = np.asarray(
seq[0].wcs.wcs.cunit)[np.invert(seq[0].missing_axis)][wcs_plot_axis_index]
seq[0].wcs.wcs.cunit)[np.invert(seq[0].missing_axes)][wcs_plot_axis_index]
if plot_axis_index == seq._common_axis:
# Determine whether common axis is dependent.
x_axis_cube_coords = np.concatenate(
[cube.axis_world_coords(plot_axis_index).to(unit_x_axis).value
for cube in seq.data], axis=plot_axis_index)
dependent_axes = utils.wcs.get_dependent_data_axes(
seq[0].wcs, plot_axis_index, seq[0].missing_axis)
seq[0].wcs, plot_axis_index, seq[0].missing_axes)
if len(dependent_axes) > 1:
independent_axes = list(range(data_concat.ndim))
for i in list(dependent_axes)[::-1]:
Expand Down Expand Up @@ -1252,7 +1252,7 @@ def _get_non_common_axis_x_axis_coords(seq_data, plot_axis_index, unit_x_axis):
if x_axis_cube_coords.shape != cube.data.shape:
# Get sequence axes dependent and independent of plot_axis_index.
dependent_axes = utils.wcs.get_dependent_data_axes(
cube.wcs, plot_axis_index, cube.missing_axis)
cube.wcs, plot_axis_index, cube.missing_axes)
independent_axes = list(range(len(cube.dimensions)))
for i in list(dependent_axes)[::-1]:
independent_axes.pop(i)
Expand Down
44 changes: 26 additions & 18 deletions ndcube/ndcube.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class NDCubeBase(NDCubeSlicingMixin, NDCubeABC):
Note however that it is not always possible to save the input as reference.
Default is False.
missing_axis : `list` of `bool`
missing_axes : `list` of `bool`
Designates which axes in wcs object do not have a corresponding axis is the data.
True means axis is "missing", False means axis corresponds to a data axis.
Ordering corresponds to the axis ordering in the WCS object, i.e. reverse of data.
Expand All @@ -181,14 +181,14 @@ class NDCubeBase(NDCubeSlicingMixin, NDCubeABC):
"""

def __init__(self, data, wcs, uncertainty=None, mask=None, meta=None,
unit=None, extra_coords=None, copy=False, missing_axis=None, **kwargs):
if missing_axis is None:
self.missing_axis = [False]*wcs.naxis
unit=None, extra_coords=None, copy=False, missing_axes=None, **kwargs):
if missing_axes is None:
self.missing_axes = [False]*wcs.naxis
else:
self.missing_axis = missing_axis
self.missing_axes = missing_axes
if data.ndim is not wcs.naxis:
count = 0
for bool_ in self.missing_axis:
for bool_ in self.missing_axes:
if not bool_:
count += 1
if count is not data.ndim:
Expand All @@ -198,7 +198,7 @@ def __init__(self, data, wcs, uncertainty=None, mask=None, meta=None,
if extra_coords:
self._extra_coords_wcs_axis = \
utils.cube._format_input_extra_coords_to_extra_coords_wcs_axis(
extra_coords, self.missing_axis, data.shape)
extra_coords, self.missing_axes, data.shape)
else:
self._extra_coords_wcs_axis = None
# Initialize NDCube.
Expand Down Expand Up @@ -228,7 +228,7 @@ def world_axis_physical_types(self):
"""
ctype = list(self.wcs.wcs.ctype)
axes_ctype = []
for i, axis in enumerate(self.missing_axis):
for i, axis in enumerate(self.missing_axes):
if not axis:
# Find keys in wcs_ivoa_mapping dict that represent start of CTYPE.
# Ensure CTYPE is capitalized.
Expand All @@ -253,6 +253,14 @@ def world_axis_physical_types(self):
axes_ctype.append(axis_name)
return tuple(axes_ctype[::-1])

@property
def missing_axis(self):
warnings.warn(
"The missing_axis list has been deprecated by missing_axes and will no longer be supported after ndcube 2.0.",
DeprecationWarning
)
return self.missing_axes

def pixel_to_world(self, *quantity_axis_list):
# The docstring is defined in NDDataBase

Expand All @@ -261,10 +269,10 @@ def pixel_to_world(self, *quantity_axis_list):
indexed_not_as_one = []
result = []
quantity_index = 0
for i in range(len(self.missing_axis)):
for i in range(len(self.missing_axes)):
wcs_index = self.wcs.naxis-1-i
# the cases where the wcs dimension was made 1 and the missing_axis is True
if self.missing_axis[wcs_index]:
# the cases where the wcs dimension was made 1 and the missing_axes is True
if self.missing_axes[wcs_index]:
list_arg.append(self.wcs.wcs.crpix[wcs_index]-1+origin)
else:
# else it is not the case where the dimension of wcs is 1.
Expand All @@ -287,10 +295,10 @@ def world_to_pixel(self, *quantity_axis_list):
indexed_not_as_one = []
result = []
quantity_index = 0
for i in range(len(self.missing_axis)):
for i in range(len(self.missing_axes)):
wcs_index = self.wcs.naxis-1-i
# the cases where the wcs dimension was made 1 and the missing_axis is True
if self.missing_axis[wcs_index]:
# the cases where the wcs dimension was made 1 and the missing_axes is True
if self.missing_axes[wcs_index]:
list_arg.append(self.wcs.wcs.crval[wcs_index]+1-origin)
else:
# else it is not the case where the dimension of wcs is 1.
Expand Down Expand Up @@ -363,7 +371,7 @@ def axis_world_coords(self, *axes):
axes_translated = np.zeros_like(int_axes, dtype=bool)
# Determine which axes are dependent on others.
# Ensure the axes are in numerical order.
dependent_axes = [list(utils.wcs.get_dependent_data_axes(self.wcs, axis, self.missing_axis))
dependent_axes = [list(utils.wcs.get_dependent_data_axes(self.wcs, axis, self.missing_axes))
for axis in int_axes]
n_dependent_axes = [len(da) for da in dependent_axes]
# Iterate through each axis and perform WCS translation.
Expand Down Expand Up @@ -433,7 +441,7 @@ def extra_coords(self):
result[key] = {
"axis": utils.cube.wcs_axis_to_data_axis(
self._extra_coords_wcs_axis[key]["wcs axis"],
self.missing_axis),
self.missing_axes),
"value": self._extra_coords_wcs_axis[key]["value"]}
return result

Expand Down Expand Up @@ -625,7 +633,7 @@ class NDCubeOrdered(NDCube):
"""

def __init__(self, data, wcs, uncertainty=None, mask=None, meta=None,
unit=None, extra_coords=None, copy=False, missing_axis=None, **kwargs):
unit=None, extra_coords=None, copy=False, missing_axes=None, **kwargs):
axtypes = list(wcs.wcs.ctype)[::-1]
array_order = utils.cube.select_order(axtypes)
result_data = data.transpose(array_order)
Expand All @@ -649,4 +657,4 @@ def __init__(self, data, wcs, uncertainty=None, mask=None, meta=None,
super().__init__(result_data, result_wcs, uncertainty=result_uncertainty,
mask=result_mask, meta=meta, unit=unit,
extra_coords=reordered_extra_coords,
copy=copy, missing_axis=missing_axis, **kwargs)
copy=copy, missing_axes=missing_axes, **kwargs)
2 changes: 1 addition & 1 deletion ndcube/tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def assert_cubes_equal(test_input, expected_cube):
assert type(test_input) == type(expected_cube)
assert np.all(test_input.mask == expected_cube.mask)
assert_wcs_are_equal(test_input.wcs, expected_cube.wcs)
assert test_input.missing_axis == expected_cube.missing_axis
assert test_input.missing_axes == expected_cube.missing_axes
assert test_input.uncertainty.array.shape == expected_cube.uncertainty.array.shape
assert test_input.world_axis_physical_types == expected_cube.world_axis_physical_types
assert all(test_input.dimensions.value == expected_cube.dimensions.value)
Expand Down
Loading

0 comments on commit 95b60b1

Please sign in to comment.