Skip to content

Commit

Permalink
Merge pull request #142 from DanRyanIrish/harmonize_crop_api
Browse files Browse the repository at this point in the history
Broke crop_by_extra_coord API to be similar to crop_by_coords.
  • Loading branch information
DanRyanIrish authored Dec 11, 2018
2 parents 6de114a + 5dfe4ff commit d1d5e56
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
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

0 comments on commit d1d5e56

Please sign in to comment.