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

Broke crop_by_extra_coord API to be similar to crop_by_coords. #142

Merged
merged 5 commits into from
Dec 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
1.1 (Unreleased)
================

API-Breaking Changes
--------------------
- `~ndcube.NDCubeBase.crop_by_extra_coord` API has been broken and
replaced.
Old version:
``crop_by_extra_coord(min_coord_value, interval_width, coord_name)``.
New version:
``crop_by_extra_coord(coord_name, min_coord_value, max_coord_value)``.
[#142]

New Features
------------
- Created a new `~ndcube.NDCubeBase` which has all the functionality
Expand Down
20 changes: 11 additions & 9 deletions ndcube/ndcube.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,36 +481,38 @@ def crop_by_coords(self, lower_corner, interval_widths=None, upper_corner=None,
for axis_pixels in all_pix_corners])
return self[item]

def crop_by_extra_coord(self, min_coord_value, interval_width, coord_name):
def crop_by_extra_coord(self, coord_name, min_coord_value, max_coord_value):
"""
Crops an NDCube given a minimum value and interval width along an extra coord.

Parameters
----------
coord_name: `str`
Name of extra coordinate by which to crop.

min_coord_value: Single value `astropy.units.Quantity`
The minimum desired value of the extra coord after cropping.
Unit must be consistent with the extra coord on which cropping is based.

interval_width: Single value `astropy.units.Quantity`
The width of the interval along the extra coord axis in physical units
consistent with the extra coord. Unit must be consistent with the extra
coord on which cropping is based.

extra_coord: `str`
Name of extra coordinate.
min_coord_value: Single value `astropy.units.Quantity`
The maximum desired value of the extra coord after cropping.
Unit must be consistent with the extra coord on which cropping is based.

Returns
-------
result: `ndcube.NDCube`

"""
if not isinstance(coord_name, str):
raise TypeError("The API for this function has changed. "
"Please give coord_name, min_coord_value, max_coord_value")
extra_coord_dict = self.extra_coords[coord_name]
if isinstance(extra_coord_dict["value"], u.Quantity):
extra_coord_values = extra_coord_dict["value"]
else:
extra_coord_values = np.asarray(extra_coord_dict["value"])
w = np.logical_and(extra_coord_values >= min_coord_value,
extra_coord_values < min_coord_value + interval_width)
extra_coord_values < max_coord_value)
w = np.arange(len(extra_coord_values))[w]
item = [slice(None)]*len(self.dimensions)
item[extra_coord_dict["axis"]] = slice(w[0], w[1]+1)
Expand Down
6 changes: 3 additions & 3 deletions ndcube/tests/test_ndcube.py
Original file line number Diff line number Diff line change
Expand Up @@ -865,9 +865,9 @@ def test_crop_by_coords_error(test_input):

@pytest.mark.parametrize(
"test_input,expected",
[((cubem, 0*u.pix, 1.5*u.pix, "time"), cubem[0:2]),
((cube, 0*u.pix, 1.5*u.pix, "bye"), cube[:, :, 0:2]),
((cubet, datetime.datetime(2000, 1, 1), datetime.timedelta(minutes=2), "time"), cubet[:2])])
[((cubem, "time", 0*u.pix, 1.5*u.pix, ), cubem[0:2]),
((cube, "bye", 0*u.pix, 1.5*u.pix, ), cube[:, :, 0:2]),
((cubet, "time", datetime.datetime(2000, 1, 1), datetime.datetime(2000, 1, 3)), cubet[:2])])
def test_crop_by_extra_coord(test_input, expected):
helpers.assert_cubes_equal(
test_input[0].crop_by_extra_coord(*tuple(test_input[1:])), expected)
Expand Down