From 1a5dac88d145dcf3e5647fb9da886e861679bdc3 Mon Sep 17 00:00:00 2001 From: DanRyanIrish Date: Tue, 11 Dec 2018 11:23:01 -0500 Subject: [PATCH 1/4] Broke crop_by_extra_coord API to be similar to crop_by_coords. --- CHANGELOG.rst | 10 ++++++++++ ndcube/ndcube.py | 18 ++++++++++-------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 65687cfd1..d9619c3f6 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)``. + [#140] + New Features ------------ - Created a new `~ndcube.NDCubeBase` which has all the functionality diff --git a/ndcube/ndcube.py b/ndcube/ndcube.py index 2edb6107a..7b833cf53 100644 --- a/ndcube/ndcube.py +++ b/ndcube/ndcube.py @@ -481,29 +481,31 @@ 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 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"] From ab822b555af9ab97be3610d9f41c246f0abd40b1 Mon Sep 17 00:00:00 2001 From: DanRyanIrish Date: Tue, 11 Dec 2018 11:37:53 -0500 Subject: [PATCH 2/4] Updated tests for API change to crop_by_extra_coord made in previous commit. --- ndcube/tests/test_ndcube.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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) From ad5e31d6211e2c91b0b93ee0e61f2e560b7d5eb1 Mon Sep 17 00:00:00 2001 From: DanRyanIrish Date: Tue, 11 Dec 2018 11:54:57 -0500 Subject: [PATCH 3/4] Fix bugs in implementing new API for crop_by_extra_coord. --- ndcube/ndcube.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ndcube/ndcube.py b/ndcube/ndcube.py index f698634ba..8f668e96b 100644 --- a/ndcube/ndcube.py +++ b/ndcube/ndcube.py @@ -503,7 +503,7 @@ def crop_by_extra_coord(self, coord_name, min_coord_value, max_coord_value): result: `ndcube.NDCube` """ - if isinstance(coord_name, str): + 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] @@ -512,7 +512,7 @@ def crop_by_extra_coord(self, coord_name, min_coord_value, max_coord_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) From 5dfe4ff6ab8a445c632e0f8a05e0dec12a5930ce Mon Sep 17 00:00:00 2001 From: DanRyanIrish Date: Tue, 11 Dec 2018 12:08:20 -0500 Subject: [PATCH 4/4] Update changelog. --- CHANGELOG.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index f3a766d9f..7dd894716 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,7 +1,7 @@ 1.1 (Unreleased) ================ -API Breaking Changes +API-Breaking Changes -------------------- - `~ndcube.NDCubeBase.crop_by_extra_coord` API has been broken and replaced. @@ -9,7 +9,7 @@ API Breaking Changes ``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)``. - [#140] + [#142] New Features ------------