diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 29d848496..7dd894716 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -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 diff --git a/ndcube/ndcube.py b/ndcube/ndcube.py index 1cdebd2f6..8f668e96b 100644 --- a/ndcube/ndcube.py +++ b/ndcube/ndcube.py @@ -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) diff --git a/ndcube/tests/test_ndcube.py b/ndcube/tests/test_ndcube.py index 93ee12407..ed00f740e 100644 --- a/ndcube/tests/test_ndcube.py +++ b/ndcube/tests/test_ndcube.py @@ -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)