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

PR for getting pixel_edges along with pixel_values in axis_world_coords #174

Merged
merged 15 commits into from
May 23, 2019
Merged
49 changes: 13 additions & 36 deletions ndcube/ndcube.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from ndcube import utils
from ndcube.ndcube_sequence import NDCubeSequence
from ndcube.utils.wcs import wcs_ivoa_mapping
from ndcube.utils.cube import _pixel_centers_or_edges, _get_dimension_for_pixel
from ndcube.mixins import NDCubeSlicingMixin, NDCubePlotMixin


Expand Down Expand Up @@ -314,7 +315,7 @@ def world_to_pixel(self, *quantity_axis_list):
result.append(u.Quantity(world_to_pixel[index], unit=u.pix))
return result[::-1]

def axis_world_coords(self, *axes, **kwargs):
def axis_world_coords(self, *axes, edges=False):
"""
Returns WCS coordinate values of all pixels for all axes.

Expand All @@ -325,6 +326,7 @@ def axis_world_coords(self, *axes, **kwargs):
`~ndcube.NDCube.world_axis_physical_types`
of axes for which real world coordinates are desired.
axes=None implies all axes will be returned.
yashrsharma44 marked this conversation as resolved.
Show resolved Hide resolved

edges: `bool`
The edges argument helps in returning `pixel_edges`
instead of `pixel_values`. Default value is False,
Expand All @@ -345,11 +347,6 @@ def axis_world_coords(self, *axes, **kwargs):
cube_dimensions = np.array(self.dimensions.value, dtype=int)
n_dimensions = cube_dimensions.size
world_axis_types = self.world_axis_physical_types
# Extract the kwarg `edges` if present.
# True signifies pixel_edges
# False signifies pixel_values
# Default value is False
edges = kwargs.get("edges",False)

# Determine axis numbers of user supplied axes.
if axes == ():
Expand Down Expand Up @@ -393,44 +390,24 @@ def axis_world_coords(self, *axes, **kwargs):
# other dimensions all have 0 pixel value.
# Replace array in quantity list corresponding to current axis with
# np.arange array.

# Check for the edges arguments
if edges:
# Setting start and stop ranges for `pixel_edges`
start = -0.5
stop = cube_dimensions[axis] + 0.5
quantity_list = [
u.Quantity(np.zeros(cube_dimensions[dependent_axes[i]]+1),
unit=u.pix)] * n_dimensions
quantity_list[axis] = u.Quantity(np.arange(start,stop), unit=u.pix)
else:
quantity_list = [
u.Quantity(np.zeros(cube_dimensions[dependent_axes[i]]),
u.Quantity(np.zeros(_get_dimension_for_pixel(cube_dimensions[dependent_axes[i]], edges)),
yashrsharma44 marked this conversation as resolved.
Show resolved Hide resolved
unit=u.pix)] * n_dimensions
quantity_list[axis] = u.Quantity(np.arange(cube_dimensions[axis]), unit=u.pix)
quantity_list[axis] = u.Quantity(_pixel_centers_or_edges(cube_dimensions[axis], edges), unit=u.pix)
else:
# If the axis is dependent on another, perform
# translations on all dependent axes.
# Construct pixel quantities in each dimension letting
# other dimensions all have 0 pixel value.
# Construct orthogonal pixel index arrays for dependent axes.
# Check for edges arguments
if edges:
quantity_list = [u.Quantity(np.zeros(tuple(
[cube_dimensions[k]+1 for k in dependent_axes[i]])),
unit=u.pix)] * n_dimensions

dependent_pixel_quantities = np.meshgrid(
*[np.arange(-0.5, cube_dimensions[k] + 0.5) * u.pix
for k in dependent_axes[i]], indexing="ij")
else:
quantity_list = [u.Quantity(np.zeros(tuple(
[cube_dimensions[k] for k in dependent_axes[i]])),
unit=u.pix)] * n_dimensions

dependent_pixel_quantities = np.meshgrid(
*[np.arange(cube_dimensions[k]) * u.pix
for k in dependent_axes[i]], indexing="ij")
quantity_list = [u.Quantity(np.zeros(tuple(
[_get_dimension_for_pixel(cube_dimensions[k], edges) for k in dependent_axes[i]])),
unit=u.pix)] * n_dimensions
yashrsharma44 marked this conversation as resolved.
Show resolved Hide resolved

dependent_pixel_quantities = np.meshgrid(
*[_pixel_centers_or_edges(cube_dimensions[k], edges) * u.pix
for k in dependent_axes[i]], indexing="ij")

for k, axis in enumerate(dependent_axes[i]):
quantity_list[axis] = dependent_pixel_quantities[k]
# Perform wcs translation
Expand Down
37 changes: 37 additions & 0 deletions ndcube/utils/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,40 @@ def get_axis_number_from_axis_name(axis_name, world_axis_physical_types):
"a physical axis type. {0} not in any of {1}".format(
axis_name, world_axis_physical_types))
return axis_index[0]

def _pixel_centers_or_edges(axis_length, edges):
"""
Returns a range of pixel_values or pixel_edges

Parameters
----------
axis_length: `int`
The length of the axis

edges: `bool`
Boolean to signify whether pixel_edge or pixel_value requested
False stands for pixel_value, while True stands for pixel_edge
yashrsharma44 marked this conversation as resolved.
Show resolved Hide resolved
Returns
-------
`np.ndarray`
The axis_values for the given input
"""
if edges is False:
axis_values = np.arange(axis_length)
else:
axis_values = np.arange(-0.5, axis_length+0.5)
return axis_values

def _get_dimension_for_pixel(axis_length, edges):
"""
Returns the dimensions for the given edges.

Parameters
----------
axis_length : `int`
The length of the axis
edges : `bool`
Boolean to signify whether pixel_edge or pixel_value requested
False stands for pixel_value, while True stands for pixel_edge
"""
return axis_length+1 if edges else axis_length