From eae4b8139d39ab19da523a6c9d8aea64670ce072 Mon Sep 17 00:00:00 2001 From: hayesla Date: Mon, 22 Apr 2024 16:23:45 +0200 Subject: [PATCH 01/18] adding pow --- ndcube/ndcube.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/ndcube/ndcube.py b/ndcube/ndcube.py index 082d1c034..6fb82a1b1 100644 --- a/ndcube/ndcube.py +++ b/ndcube/ndcube.py @@ -940,6 +940,25 @@ def __rmul__(self, value): def __truediv__(self, value): return self.__mul__(1/value) + def __pow__(self, value): + new_data = self.data ** value + new_unit = self.unit ** value + new_uncertainty = self.uncertainty + + if self.uncertainty is not None: + try: + new_uncertainty = new_uncertainty.propagate(np.power, self, self.data ** value, correlation=1) + + except ValueError as e: + if "unsupported operation: power" in e.args[0]: + new_uncertainty = None + raise warnings.warn(f"{type(self.uncertainty)} does not support power propagation of uncertainties, setting uncertainties to None.", + UserWarning) + else: + raise e + + return self._new_instance_from_op(new_data, new_unit, new_uncertainty) + def to(self, new_unit, **kwargs): """Convert instance to another unit. From 788a2a581d502b721080276cba5265229fd53732 Mon Sep 17 00:00:00 2001 From: hayesla Date: Tue, 23 Apr 2024 11:54:22 +0200 Subject: [PATCH 02/18] make array type floats --- ndcube/conftest.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ndcube/conftest.py b/ndcube/conftest.py index 76f33a1b4..8dfa999c5 100644 --- a/ndcube/conftest.py +++ b/ndcube/conftest.py @@ -42,9 +42,9 @@ def skycoord_2d_lut(shape): return SkyCoord(*data, unit=u.deg) -def data_nd(shape): +def data_nd(shape, dtype=float): nelem = np.prod(shape) - return np.arange(nelem).reshape(shape) + return np.arange(nelem, dtype=dtype).reshape(shape) def time_extra_coords(shape, axis, base): @@ -329,7 +329,7 @@ def ndcube_4d_ln_l_t_lt(wcs_4d_lt_t_l_ln): def ndcube_4d_ln_lt_l_t(wcs_4d_t_l_lt_ln): shape = (5, 8, 10, 12) wcs_4d_t_l_lt_ln.array_shape = shape - data_cube = data_nd(shape) + data_cube = data_nd(shape, dtype=int) return NDCube(data_cube, wcs=wcs_4d_t_l_lt_ln) @@ -478,7 +478,7 @@ def ndcube_2d_ln_lt_uncert(wcs_2d_lt_ln): shape = (10, 12) data_cube = data_nd(shape) uncertainty = astropy.nddata.StdDevUncertainty(data_cube * 0.1) - cube = NDCube(data_cube, wcs=wcs_2d_lt_ln, uncertainty=uncertainty) + cube = NDCube(data_cube+1, wcs=wcs_2d_lt_ln, uncertainty=uncertainty) return cube @@ -512,7 +512,7 @@ def ndcube_2d_ln_lt_uncert_ec(wcs_2d_lt_ln): def ndcube_2d_ln_lt_units(wcs_2d_lt_ln): shape = (10, 12) data_cube = data_nd(shape).astype(float) - return NDCube(data_cube, wcs=wcs_2d_lt_ln, unit=u.ct) + return NDCube(data_cube + 1, wcs=wcs_2d_lt_ln, unit=u.ct) @pytest.fixture From 380680cf981acc1c6cc995c537eb5038ce219b83 Mon Sep 17 00:00:00 2001 From: hayesla Date: Tue, 23 Apr 2024 11:56:26 +0200 Subject: [PATCH 03/18] make array type floats --- ndcube/ndcube.py | 11 +++++++---- ndcube/tests/test_ndcube.py | 7 +++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/ndcube/ndcube.py b/ndcube/ndcube.py index 6fb82a1b1..2eb6e4402 100644 --- a/ndcube/ndcube.py +++ b/ndcube/ndcube.py @@ -942,7 +942,10 @@ def __truediv__(self, value): def __pow__(self, value): new_data = self.data ** value - new_unit = self.unit ** value + new_unit = self.unit + if self.unit is not None: + new_unit = self.unit ** value + new_uncertainty = self.uncertainty if self.uncertainty is not None: @@ -950,10 +953,10 @@ def __pow__(self, value): new_uncertainty = new_uncertainty.propagate(np.power, self, self.data ** value, correlation=1) except ValueError as e: - if "unsupported operation: power" in e.args[0]: + if "unsupported operation" in e.args[0]: new_uncertainty = None - raise warnings.warn(f"{type(self.uncertainty)} does not support power propagation of uncertainties, setting uncertainties to None.", - UserWarning) + warnings.warn(f"{type(self.uncertainty)} does not support power propagation of uncertainties, setting uncertainties to None.", + UserWarning, stacklevel=2) else: raise e diff --git a/ndcube/tests/test_ndcube.py b/ndcube/tests/test_ndcube.py index b8f1e1ada..114354146 100644 --- a/ndcube/tests/test_ndcube.py +++ b/ndcube/tests/test_ndcube.py @@ -1119,6 +1119,13 @@ def test_cube_arithmetic_multiply_notimplementederror(ndcube_2d_ln_lt_units): _ = ndcube_2d_ln_lt_units * ndcube_2d_ln_lt_units +@pytest.mark.parametrize('power', [2, -2, 10, 0.5]) +def test_cube_arithmetic_power(ndcube_2d_ln_lt_uncert, power): + cube_quantity = u.Quantity(ndcube_2d_ln_lt_uncert.data, ndcube_2d_ln_lt_uncert.unit) + new_cube = ndcube_2d_ln_lt_uncert ** power + check_arithmetic_value_and_units(new_cube, cube_quantity**power) + + @pytest.mark.parametrize('new_unit', [u.mJ, 'mJ']) def test_to(ndcube_1d_l, new_unit): cube = ndcube_1d_l From 1cbb7a05f138bb26e5409d1e1962ada80c8e0a34 Mon Sep 17 00:00:00 2001 From: hayesla Date: Tue, 23 Apr 2024 12:12:15 +0200 Subject: [PATCH 04/18] adding changelog --- changelog/678.feature.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/678.feature.rst diff --git a/changelog/678.feature.rst b/changelog/678.feature.rst new file mode 100644 index 000000000..7edb686d4 --- /dev/null +++ b/changelog/678.feature.rst @@ -0,0 +1 @@ +Add __pow__ to ndcube.NDCube. From d02d0949f140b0932f8aacc08629e808f44773a4 Mon Sep 17 00:00:00 2001 From: Laura Hayes Date: Tue, 23 Apr 2024 13:44:43 +0200 Subject: [PATCH 05/18] Update changelog/678.feature.rst Co-authored-by: DanRyanIrish --- changelog/678.feature.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/678.feature.rst b/changelog/678.feature.rst index 7edb686d4..e2f3e4031 100644 --- a/changelog/678.feature.rst +++ b/changelog/678.feature.rst @@ -1 +1 @@ -Add __pow__ to ndcube.NDCube. +Enable `~ndcube.NDCube` to be raised to a power. From 7db5880e87b1ef7dc0d1785f4e3ff559301e49eb Mon Sep 17 00:00:00 2001 From: Laura Hayes Date: Tue, 23 Apr 2024 13:44:57 +0200 Subject: [PATCH 06/18] Update ndcube/ndcube.py Co-authored-by: DanRyanIrish --- ndcube/ndcube.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/ndcube/ndcube.py b/ndcube/ndcube.py index 2eb6e4402..c840bd1d0 100644 --- a/ndcube/ndcube.py +++ b/ndcube/ndcube.py @@ -942,10 +942,7 @@ def __truediv__(self, value): def __pow__(self, value): new_data = self.data ** value - new_unit = self.unit - if self.unit is not None: - new_unit = self.unit ** value - + new_unit = self.unit if self.unit is None else self.unit ** value new_uncertainty = self.uncertainty if self.uncertainty is not None: From 06f2b421b13ffe650c727e641a37c766840f2c6d Mon Sep 17 00:00:00 2001 From: Laura Hayes Date: Tue, 23 Apr 2024 13:45:05 +0200 Subject: [PATCH 07/18] Update ndcube/ndcube.py Co-authored-by: DanRyanIrish --- ndcube/ndcube.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ndcube/ndcube.py b/ndcube/ndcube.py index c840bd1d0..25659a38b 100644 --- a/ndcube/ndcube.py +++ b/ndcube/ndcube.py @@ -952,7 +952,7 @@ def __pow__(self, value): except ValueError as e: if "unsupported operation" in e.args[0]: new_uncertainty = None - warnings.warn(f"{type(self.uncertainty)} does not support power propagation of uncertainties, setting uncertainties to None.", + warnings.warn(f"{type(self.uncertainty)} does not support propagation of uncertainties for power. Setting uncertainties to None.", UserWarning, stacklevel=2) else: raise e From 34c56678fb1d7824ae4cb471ac31e79d7e26c94a Mon Sep 17 00:00:00 2001 From: Laura Hayes Date: Tue, 23 Apr 2024 13:45:12 +0200 Subject: [PATCH 08/18] Update ndcube/ndcube.py Co-authored-by: DanRyanIrish --- ndcube/ndcube.py | 1 - 1 file changed, 1 deletion(-) diff --git a/ndcube/ndcube.py b/ndcube/ndcube.py index 25659a38b..083f5f681 100644 --- a/ndcube/ndcube.py +++ b/ndcube/ndcube.py @@ -948,7 +948,6 @@ def __pow__(self, value): if self.uncertainty is not None: try: new_uncertainty = new_uncertainty.propagate(np.power, self, self.data ** value, correlation=1) - except ValueError as e: if "unsupported operation" in e.args[0]: new_uncertainty = None From cc4228b5d5957e73e73437d6eb06aeb36559e567 Mon Sep 17 00:00:00 2001 From: Nabil Freij Date: Tue, 23 Apr 2024 14:46:03 +0200 Subject: [PATCH 09/18] improve message hopefully --- ndcube/ndcube.py | 7 ++++--- ndcube/tests/test_ndcube.py | 6 +++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/ndcube/ndcube.py b/ndcube/ndcube.py index 40af1ba61..9ddb4098b 100644 --- a/ndcube/ndcube.py +++ b/ndcube/ndcube.py @@ -1138,10 +1138,11 @@ def my_propagate(uncertainty, data, mask, **kwargs): if propagate_uncertainties: if self.uncertainty is None: warnings.warn("Uncertainties cannot be propagated as there are no uncertainties, " - "i.e. self.uncertainty is None.") + "i.e., the `uncertainty` keyword was never set on creation of this NDCube.") elif isinstance(self.uncertainty, astropy.nddata.UnknownUncertainty): - warnings.warn("Uncertainty is an UnknownUncertainty which does not " - "support uncertainty propagation.") + warnings.warn("The set uncertainty on this NDCube has no known way to propagate forward and so will be dropped. " + "To create an uncertainty that can propagate, please see " + "https://docs.astropy.org/en/stable/uncertainty/index.html") elif (not operation_ignores_mask and (self.mask is True or (self.mask is not None and not isinstance(self.mask, bool) diff --git a/ndcube/tests/test_ndcube.py b/ndcube/tests/test_ndcube.py index f7e2bd4d8..413324073 100644 --- a/ndcube/tests/test_ndcube.py +++ b/ndcube/tests/test_ndcube.py @@ -790,7 +790,7 @@ def test_wcs_type_after_init(ndcube_3d_ln_lt_l, wcs_3d_l_lt_ln): def test_rebin(ndcube_3d_l_ln_lt_ectime): cube = ndcube_3d_l_ln_lt_ectime[:, 1:] bin_shape = (10, 2, 1) - with pytest.warns(UserWarning, match="Uncertainty is an UnknownUncertainty which does not support uncertainty propagation."): + with pytest.warns(UserWarning, match="The set uncertainty on this NDCube has no known way to propagate forward"): output = cube.rebin(bin_shape, operation=np.sum, propagate_uncertainties=True) output_sc, output_spec = output.axis_world_coords(wcs=output.wcs) output_time, = output.axis_world_coords(wcs=output.extra_coords) @@ -845,7 +845,7 @@ def test_rebin_no_ec(ndcube_3d_l_ln_lt_ectime): cube = ndcube_3d_l_ln_lt_ectime[:, 1:] cube._extra_coords = ExtraCoords(cube) bin_shape = (10, 2, 1) - with pytest.warns(UserWarning, match="Uncertainty is an UnknownUncertainty which does not support uncertainty propagation."): + with pytest.warns(UserWarning, match="The set uncertainty on this NDCube has no known way to propagate forward"): output = cube.rebin(bin_shape, operation=np.mean, propagate_uncertainties=True) assert output.extra_coords.is_empty @@ -942,7 +942,7 @@ def test_rebin_no_propagate(ndcube_2d_ln_lt_mask_uncert): cube._mask = False cube._uncertainty = UnknownUncertainty(cube.data * 0.1) - with pytest.warns(UserWarning, match="Uncertainty is an UnknownUncertainty which does not support uncertainty propagation."): + with pytest.warns(UserWarning, match="The set uncertainty on this NDCube has no known way to propagate forward"): output = cube.rebin(bin_shape, operation=np.sum, propagate_uncertainties=True) assert output.uncertainty is None From b4f363b5d1748a7aeb272da387c73e6c3958bbb5 Mon Sep 17 00:00:00 2001 From: hayesla Date: Tue, 23 Apr 2024 14:55:44 +0200 Subject: [PATCH 10/18] fixes --- ndcube/ndcube.py | 4 ++++ ndcube/tests/test_ndcube.py | 11 ++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/ndcube/ndcube.py b/ndcube/ndcube.py index 083f5f681..d103e6bfe 100644 --- a/ndcube/ndcube.py +++ b/ndcube/ndcube.py @@ -953,6 +953,10 @@ def __pow__(self, value): new_uncertainty = None warnings.warn(f"{type(self.uncertainty)} does not support propagation of uncertainties for power. Setting uncertainties to None.", UserWarning, stacklevel=2) + elif "UnknownUncertainty does not support uncertainty propagation with correlation." in e.args[0]: + new_uncertainty = None + warnings.warn(f"{type(self.uncertainty)} does not support uncertainty propagation with correlation. Setting uncertainties to None.", + UserWarning, stacklevel=2) else: raise e diff --git a/ndcube/tests/test_ndcube.py b/ndcube/tests/test_ndcube.py index 114354146..af3f73600 100644 --- a/ndcube/tests/test_ndcube.py +++ b/ndcube/tests/test_ndcube.py @@ -1119,8 +1119,17 @@ def test_cube_arithmetic_multiply_notimplementederror(ndcube_2d_ln_lt_units): _ = ndcube_2d_ln_lt_units * ndcube_2d_ln_lt_units +@pytest.mark.filterwarnings('ignore::RuntimeWarning') @pytest.mark.parametrize('power', [2, -2, 10, 0.5]) -def test_cube_arithmetic_power(ndcube_2d_ln_lt_uncert, power): +def test_cube_arithmetic_power(ndcube_4d_unit_uncertainty, power): + cube_quantity = u.Quantity(ndcube_4d_unit_uncertainty.data, ndcube_4d_unit_uncertainty.unit) + new_cube = ndcube_4d_unit_uncertainty ** power + check_arithmetic_value_and_units(new_cube, cube_quantity**power) + + +@pytest.mark.filterwarnings('ignore::RuntimeWarning') +@pytest.mark.parametrize('power', [2, -2, 10, 0.5]) +def test_cube_arithmetic_power_uncertainty_nounit(ndcube_2d_ln_lt_uncert, power): cube_quantity = u.Quantity(ndcube_2d_ln_lt_uncert.data, ndcube_2d_ln_lt_uncert.unit) new_cube = ndcube_2d_ln_lt_uncert ** power check_arithmetic_value_and_units(new_cube, cube_quantity**power) From eac92902d0a32afb9c7281df7a76160bc335389e Mon Sep 17 00:00:00 2001 From: hayesla Date: Tue, 23 Apr 2024 15:19:29 +0200 Subject: [PATCH 11/18] updating with suggestions and tests --- ndcube/conftest.py | 2 +- ndcube/ndcube.py | 4 ++-- ndcube/tests/test_ndcube.py | 18 +++++++++++++----- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/ndcube/conftest.py b/ndcube/conftest.py index 6f427430e..5b5c512cf 100644 --- a/ndcube/conftest.py +++ b/ndcube/conftest.py @@ -479,7 +479,7 @@ def ndcube_2d_ln_lt_uncert(wcs_2d_lt_ln): shape = (10, 12) data_cube = data_nd(shape) uncertainty = astropy.nddata.StdDevUncertainty(data_cube * 0.1) - cube = NDCube(data_cube+1, wcs=wcs_2d_lt_ln, uncertainty=uncertainty) + cube = NDCube(data_cube, wcs=wcs_2d_lt_ln, uncertainty=uncertainty) return cube diff --git a/ndcube/ndcube.py b/ndcube/ndcube.py index 191d2ceee..bf3eb2161 100644 --- a/ndcube/ndcube.py +++ b/ndcube/ndcube.py @@ -961,9 +961,9 @@ def __pow__(self, value): new_uncertainty = None warnings.warn(f"{type(self.uncertainty)} does not support propagation of uncertainties for power. Setting uncertainties to None.", UserWarning, stacklevel=2) - elif "UnknownUncertainty does not support uncertainty propagation with correlation." in e.args[0]: + elif "does not support uncertainty propagation" in e.args[0]: new_uncertainty = None - warnings.warn(f"{type(self.uncertainty)} does not support uncertainty propagation with correlation. Setting uncertainties to None.", + warnings.warn(f"{e.args[0]} Setting uncertainties to None.", UserWarning, stacklevel=2) else: raise e diff --git a/ndcube/tests/test_ndcube.py b/ndcube/tests/test_ndcube.py index ac15f8ed0..1e12949b9 100644 --- a/ndcube/tests/test_ndcube.py +++ b/ndcube/tests/test_ndcube.py @@ -1126,17 +1126,25 @@ def test_cube_arithmetic_multiply_notimplementederror(ndcube_2d_ln_lt_units): @pytest.mark.filterwarnings('ignore::RuntimeWarning') @pytest.mark.parametrize('power', [2, -2, 10, 0.5]) -def test_cube_arithmetic_power(ndcube_4d_unit_uncertainty, power): - cube_quantity = u.Quantity(ndcube_4d_unit_uncertainty.data, ndcube_4d_unit_uncertainty.unit) - new_cube = ndcube_4d_unit_uncertainty ** power +def test_cube_arithmetic_power(ndcube_2d_ln_lt, power): + cube_quantity = u.Quantity(ndcube_2d_ln_lt.data, ndcube_2d_ln_lt.unit) + new_cube = ndcube_2d_ln_lt ** power check_arithmetic_value_and_units(new_cube, cube_quantity**power) +@pytest.mark.filterwarnings('ignore::RuntimeWarning') +@pytest.mark.parametrize('power', [2, -2, 10, 0.5]) +def test_cube_arithmetic_power_unknown_uncertainty(ndcube_4d_unit_uncertainty, power): + cube_quantity = u.Quantity(ndcube_4d_unit_uncertainty.data, ndcube_4d_unit_uncertainty.unit) + with pytest.warns(UserWarning, match="UnknownUncertainty does not support uncertainty propagation with correlation. Setting uncertainties to None."): + new_cube = ndcube_4d_unit_uncertainty ** power + check_arithmetic_value_and_units(new_cube, cube_quantity**power) @pytest.mark.filterwarnings('ignore::RuntimeWarning') @pytest.mark.parametrize('power', [2, -2, 10, 0.5]) -def test_cube_arithmetic_power_uncertainty_nounit(ndcube_2d_ln_lt_uncert, power): +def test_cube_arithmetic_power_uncertainty(ndcube_2d_ln_lt_uncert, power): cube_quantity = u.Quantity(ndcube_2d_ln_lt_uncert.data, ndcube_2d_ln_lt_uncert.unit) - new_cube = ndcube_2d_ln_lt_uncert ** power + with pytest.warns(UserWarning, match=r" does not support propagation of uncertainties for power. Setting uncertainties to None."): + new_cube = ndcube_2d_ln_lt_uncert ** power check_arithmetic_value_and_units(new_cube, cube_quantity**power) From 2652087c4b2f40b2bd9a36ab476d4e9c2aa452bf Mon Sep 17 00:00:00 2001 From: hayesla Date: Tue, 23 Apr 2024 15:21:55 +0200 Subject: [PATCH 12/18] removing +1 --- ndcube/conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ndcube/conftest.py b/ndcube/conftest.py index 5b5c512cf..797914e28 100644 --- a/ndcube/conftest.py +++ b/ndcube/conftest.py @@ -513,7 +513,7 @@ def ndcube_2d_ln_lt_uncert_ec(wcs_2d_lt_ln): def ndcube_2d_ln_lt_units(wcs_2d_lt_ln): shape = (10, 12) data_cube = data_nd(shape).astype(float) - return NDCube(data_cube + 1, wcs=wcs_2d_lt_ln, unit=u.ct) + return NDCube(data_cube, wcs=wcs_2d_lt_ln, unit=u.ct) @pytest.fixture From bab5238de5c3c9701967803b4d54bdbb1d4dd9bc Mon Sep 17 00:00:00 2001 From: DanRyanIrish Date: Tue, 23 Apr 2024 15:30:11 +0200 Subject: [PATCH 13/18] Update ndcube/tests/test_ndcube.py --- ndcube/tests/test_ndcube.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ndcube/tests/test_ndcube.py b/ndcube/tests/test_ndcube.py index 1e12949b9..b2e9cab55 100644 --- a/ndcube/tests/test_ndcube.py +++ b/ndcube/tests/test_ndcube.py @@ -1141,7 +1141,7 @@ def test_cube_arithmetic_power_unknown_uncertainty(ndcube_4d_unit_uncertainty, p @pytest.mark.filterwarnings('ignore::RuntimeWarning') @pytest.mark.parametrize('power', [2, -2, 10, 0.5]) -def test_cube_arithmetic_power_uncertainty(ndcube_2d_ln_lt_uncert, power): +def test_cube_arithmetic_power_std_uncertainty(ndcube_2d_ln_lt_uncert, power): cube_quantity = u.Quantity(ndcube_2d_ln_lt_uncert.data, ndcube_2d_ln_lt_uncert.unit) with pytest.warns(UserWarning, match=r" does not support propagation of uncertainties for power. Setting uncertainties to None."): new_cube = ndcube_2d_ln_lt_uncert ** power From fe8795a598a7f61410af5dc7026f42809d80521a Mon Sep 17 00:00:00 2001 From: Nabil Freij Date: Tue, 23 Apr 2024 15:39:39 +0200 Subject: [PATCH 14/18] Update ndcube/ndcube.py Co-authored-by: Stuart Mumford --- ndcube/ndcube.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ndcube/ndcube.py b/ndcube/ndcube.py index 9ddb4098b..e41fb0f29 100644 --- a/ndcube/ndcube.py +++ b/ndcube/ndcube.py @@ -1140,7 +1140,7 @@ def my_propagate(uncertainty, data, mask, **kwargs): warnings.warn("Uncertainties cannot be propagated as there are no uncertainties, " "i.e., the `uncertainty` keyword was never set on creation of this NDCube.") elif isinstance(self.uncertainty, astropy.nddata.UnknownUncertainty): - warnings.warn("The set uncertainty on this NDCube has no known way to propagate forward and so will be dropped. " + warnings.warn("The uncertainty on this NDCube has no known way to propagate forward and so will be dropped. " "To create an uncertainty that can propagate, please see " "https://docs.astropy.org/en/stable/uncertainty/index.html") elif (not operation_ignores_mask From c2efd9132456ddb94d93425e4b3bc9f835646f7c Mon Sep 17 00:00:00 2001 From: Nabil Freij Date: Tue, 23 Apr 2024 15:40:13 +0200 Subject: [PATCH 15/18] Apply suggestions from code review --- 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 413324073..85572d906 100644 --- a/ndcube/tests/test_ndcube.py +++ b/ndcube/tests/test_ndcube.py @@ -790,7 +790,7 @@ def test_wcs_type_after_init(ndcube_3d_ln_lt_l, wcs_3d_l_lt_ln): def test_rebin(ndcube_3d_l_ln_lt_ectime): cube = ndcube_3d_l_ln_lt_ectime[:, 1:] bin_shape = (10, 2, 1) - with pytest.warns(UserWarning, match="The set uncertainty on this NDCube has no known way to propagate forward"): + with pytest.warns(UserWarning, match="The uncertainty on this NDCube has no known way to propagate forward"): output = cube.rebin(bin_shape, operation=np.sum, propagate_uncertainties=True) output_sc, output_spec = output.axis_world_coords(wcs=output.wcs) output_time, = output.axis_world_coords(wcs=output.extra_coords) @@ -845,7 +845,7 @@ def test_rebin_no_ec(ndcube_3d_l_ln_lt_ectime): cube = ndcube_3d_l_ln_lt_ectime[:, 1:] cube._extra_coords = ExtraCoords(cube) bin_shape = (10, 2, 1) - with pytest.warns(UserWarning, match="The set uncertainty on this NDCube has no known way to propagate forward"): + with pytest.warns(UserWarning, match="The uncertainty on this NDCube has no known way to propagate forward"): output = cube.rebin(bin_shape, operation=np.mean, propagate_uncertainties=True) assert output.extra_coords.is_empty @@ -942,7 +942,7 @@ def test_rebin_no_propagate(ndcube_2d_ln_lt_mask_uncert): cube._mask = False cube._uncertainty = UnknownUncertainty(cube.data * 0.1) - with pytest.warns(UserWarning, match="The set uncertainty on this NDCube has no known way to propagate forward"): + with pytest.warns(UserWarning, match="The uncertainty on this NDCube has no known way to propagate forward"): output = cube.rebin(bin_shape, operation=np.sum, propagate_uncertainties=True) assert output.uncertainty is None From 03b66e07e9d4f86bd8297d207bcaa97f82edfc16 Mon Sep 17 00:00:00 2001 From: Laura Hayes Date: Tue, 23 Apr 2024 16:21:56 +0200 Subject: [PATCH 16/18] Update ndcube/tests/test_ndcube.py Co-authored-by: Stuart Mumford --- ndcube/tests/test_ndcube.py | 1 + 1 file changed, 1 insertion(+) diff --git a/ndcube/tests/test_ndcube.py b/ndcube/tests/test_ndcube.py index b2e9cab55..fda3038d4 100644 --- a/ndcube/tests/test_ndcube.py +++ b/ndcube/tests/test_ndcube.py @@ -1131,6 +1131,7 @@ def test_cube_arithmetic_power(ndcube_2d_ln_lt, power): new_cube = ndcube_2d_ln_lt ** power check_arithmetic_value_and_units(new_cube, cube_quantity**power) + @pytest.mark.filterwarnings('ignore::RuntimeWarning') @pytest.mark.parametrize('power', [2, -2, 10, 0.5]) def test_cube_arithmetic_power_unknown_uncertainty(ndcube_4d_unit_uncertainty, power): From a3aa228975dd63c41c5bb691f3ec805422b18bc2 Mon Sep 17 00:00:00 2001 From: Laura Hayes Date: Tue, 23 Apr 2024 16:22:03 +0200 Subject: [PATCH 17/18] Update ndcube/tests/test_ndcube.py Co-authored-by: Stuart Mumford --- ndcube/tests/test_ndcube.py | 1 + 1 file changed, 1 insertion(+) diff --git a/ndcube/tests/test_ndcube.py b/ndcube/tests/test_ndcube.py index fda3038d4..935149313 100644 --- a/ndcube/tests/test_ndcube.py +++ b/ndcube/tests/test_ndcube.py @@ -1140,6 +1140,7 @@ def test_cube_arithmetic_power_unknown_uncertainty(ndcube_4d_unit_uncertainty, p new_cube = ndcube_4d_unit_uncertainty ** power check_arithmetic_value_and_units(new_cube, cube_quantity**power) + @pytest.mark.filterwarnings('ignore::RuntimeWarning') @pytest.mark.parametrize('power', [2, -2, 10, 0.5]) def test_cube_arithmetic_power_std_uncertainty(ndcube_2d_ln_lt_uncert, power): From dc6fe12dcf30fb9481edb9385d8124eb35640b1f Mon Sep 17 00:00:00 2001 From: hayesla Date: Tue, 23 Apr 2024 16:28:41 +0200 Subject: [PATCH 18/18] adding np.errstate instead --- ndcube/tests/test_ndcube.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/ndcube/tests/test_ndcube.py b/ndcube/tests/test_ndcube.py index 935149313..f9708d3b7 100644 --- a/ndcube/tests/test_ndcube.py +++ b/ndcube/tests/test_ndcube.py @@ -1124,30 +1124,31 @@ def test_cube_arithmetic_multiply_notimplementederror(ndcube_2d_ln_lt_units): _ = ndcube_2d_ln_lt_units * ndcube_2d_ln_lt_units -@pytest.mark.filterwarnings('ignore::RuntimeWarning') + @pytest.mark.parametrize('power', [2, -2, 10, 0.5]) def test_cube_arithmetic_power(ndcube_2d_ln_lt, power): cube_quantity = u.Quantity(ndcube_2d_ln_lt.data, ndcube_2d_ln_lt.unit) - new_cube = ndcube_2d_ln_lt ** power - check_arithmetic_value_and_units(new_cube, cube_quantity**power) + with np.errstate(divide='ignore'): + new_cube = ndcube_2d_ln_lt ** power + check_arithmetic_value_and_units(new_cube, cube_quantity**power) -@pytest.mark.filterwarnings('ignore::RuntimeWarning') @pytest.mark.parametrize('power', [2, -2, 10, 0.5]) def test_cube_arithmetic_power_unknown_uncertainty(ndcube_4d_unit_uncertainty, power): cube_quantity = u.Quantity(ndcube_4d_unit_uncertainty.data, ndcube_4d_unit_uncertainty.unit) with pytest.warns(UserWarning, match="UnknownUncertainty does not support uncertainty propagation with correlation. Setting uncertainties to None."): - new_cube = ndcube_4d_unit_uncertainty ** power - check_arithmetic_value_and_units(new_cube, cube_quantity**power) + with np.errstate(divide='ignore'): + new_cube = ndcube_4d_unit_uncertainty ** power + check_arithmetic_value_and_units(new_cube, cube_quantity**power) -@pytest.mark.filterwarnings('ignore::RuntimeWarning') @pytest.mark.parametrize('power', [2, -2, 10, 0.5]) def test_cube_arithmetic_power_std_uncertainty(ndcube_2d_ln_lt_uncert, power): cube_quantity = u.Quantity(ndcube_2d_ln_lt_uncert.data, ndcube_2d_ln_lt_uncert.unit) with pytest.warns(UserWarning, match=r" does not support propagation of uncertainties for power. Setting uncertainties to None."): - new_cube = ndcube_2d_ln_lt_uncert ** power - check_arithmetic_value_and_units(new_cube, cube_quantity**power) + with np.errstate(divide='ignore'): + new_cube = ndcube_2d_ln_lt_uncert ** power + check_arithmetic_value_and_units(new_cube, cube_quantity**power) @pytest.mark.parametrize('new_unit', [u.mJ, 'mJ'])