Skip to content

Commit bab5524

Browse files
committed
Fix bug where uncertainties were not being checked in arithmetic add and mul with nddata.
ALso add new arithmetic add with nddata test, which more concisely covers test cases.
1 parent cdd5b6d commit bab5524

2 files changed

Lines changed: 76 additions & 32 deletions

File tree

ndcube/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -814,8 +814,8 @@ def ndcube_2d_ln_lt_unit_unc_mask(wcs_2d_lt_ln):
814814
shape = (2, 3)
815815
data_cube = data_nd(shape).astype(float)
816816
mask = np.ones(shape, dtype=bool)
817-
mask[0:1, 0] = False
818-
uncertainty=StdDevUncertainty(np.ones((2, 3)) * 0.05)
817+
mask[:, 0] = False
818+
uncertainty=StdDevUncertainty(data_cube * 0.05)
819819
return NDCube(data_cube, wcs=wcs_2d_lt_ln, mask=mask, uncertainty=uncertainty, unit=u.ct)
820820

821821

ndcube/tests/test_ndcube_arithmetic.py

Lines changed: 74 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import pytest
33

44
import astropy.units as u
5-
65
import astropy.wcs
76
from astropy.nddata import NDData, StdDevUncertainty
7+
88
from ndcube import NDCube
99
from ndcube.tests.helpers import assert_cubes_equal
1010
from ndcube.utils.exceptions import NDCubeUserWarning
@@ -343,43 +343,87 @@ def test_cube_arithmetic_multiply(ndcube_2d_ln_lt_units, value):
343343

344344

345345
@pytest.mark.parametrize(("ndc", "value", "expected_kwargs"),
346-
[
347-
("ndcube_2d_ln_lt_no_unit_no_unc_no_mask_2", NDData(np.ones((2, 3)),
348-
wcs=None),
349-
{"data": np.array([[0, 1, 2], [3, 4, 5]])} # neither of the two has uncertainty or mask or unit.
350-
),
351-
("ndcube_2d_ln_lt_no_unit_no_unc_no_mask_2", NDData(np.ones((2, 3)),
352-
wcs=None,
353-
uncertainty=StdDevUncertainty(np.ones((2, 3))*0.1),
354-
mask=np.ones((2, 3), dtype=bool),
355-
unit=u.ct),
356-
{"data": np.array([[0, 1, 2], [3, 4, 5]]),
357-
"uncertainty": astropy.nddata.StdDevUncertainty(np.ones((2, 3))*0.1),
358-
"mask": np.ones((2, 3), dtype=bool),
359-
"unit": u.ct
360-
}# ndc has no mask no uncertainty no unit, but nddata has all.
361-
),
362-
("ndcube_2d_ln_lt_unit_unc_mask", NDData(np.ones((2, 3)),
363-
wcs=None,
364-
uncertainty=StdDevUncertainty(np.ones((2, 3))*0.1),
365-
mask=np.ones((2, 3), dtype=bool),
366-
unit=u.ct),
367-
{"unit": u.ct**2,
368-
"data": np.array([[0, 1, 2], [3, 4, 5]]),
369-
"uncertainty": astropy.nddata.StdDevUncertainty(np.array([[0.1118034, 0.1118034, 0.1118034],
370-
[0.1118034, 0.1118034, 0.1118034]])),
371-
"mask": np.ones((2, 3), dtype=bool)} # both of them have uncertainty and mask and unit.
372-
)
346+
[(
347+
"ndcube_2d_ln_lt_no_unit_no_unc_no_mask_2",
348+
NDData(np.ones((2, 3)) + 1, wcs=None),
349+
{"data": np.array([[0, 2, 4], [6, 8, 10]])}
350+
),
351+
(
352+
"ndcube_2d_ln_lt_no_unit_no_unc_no_mask_2",
353+
NDData(np.ones((2, 3)) + 1,
354+
wcs=None,
355+
uncertainty=StdDevUncertainty((np.ones((2, 3)) + 1) * 0.1),
356+
mask=np.array([[True, False, False], [False, True, False]]),
357+
unit=u.ct),
358+
{"data": np.array([[0, 2, 4], [6, 8, 10]]),
359+
"uncertainty": astropy.nddata.StdDevUncertainty((np.ones((2, 3)) + 1) * 0.1),
360+
"mask": np.array([[True, False, False], [False, True, False]]),
361+
"unit": u.ct} # ndc has no mask no uncertainty no unit, but nddata has all.
362+
),
363+
(
364+
"ndcube_2d_ln_lt_unit_unc_mask",
365+
NDData(np.ones((2, 3)) + 1,
366+
wcs=None,
367+
uncertainty=StdDevUncertainty((np.ones((2, 3)) + 1) * 0.1),
368+
mask=np.array([[True, False, False], [False, True, False]]),
369+
unit=u.ct),
370+
{"unit": u.ct**2,
371+
"data": np.array([[0, 2, 4], [6, 8, 10]]),
372+
"uncertainty": astropy.nddata.StdDevUncertainty(np.array([[0. , 0.2236068 , 0.4472136 ],
373+
[0.67082039, 0.89442719, 1.11803399]])),
374+
"mask": np.array([[True, True, True], [False, True, True]])}
375+
) # both of them have uncertainty and mask and unit.
373376
],
374377
indirect=("ndc",))
375-
def test_cube_arithmetic_multiply_ndcube_nddata(ndc, value, expected_kwargs, wcs_2d_lt_ln):
378+
def test_cube_arithmetic_multiply_nddata(ndc, value, expected_kwargs, wcs_2d_lt_ln):
376379
output_cube = ndc * value # perform the multiplication
377380

378381
expected_kwargs["wcs"] = wcs_2d_lt_ln
379382
expected_cube = NDCube(**expected_kwargs)
380383

381384
# Assert output cube is same as expected cube
382-
assert_cubes_equal(output_cube, expected_cube)
385+
assert_cubes_equal(output_cube, expected_cube, check_uncertainty_values=True)
386+
387+
388+
@pytest.mark.parametrize(("ndc", "value", "expected_kwargs"),
389+
[(
390+
"ndcube_2d_ln_lt_no_unit_no_unc_no_mask_2",
391+
NDData(np.ones((2, 3)), wcs=None),
392+
{"data": np.array([[1, 2, 3], [4, 5, 6]])}
393+
),
394+
(
395+
"ndcube_2d_ln_lt_no_unit_no_unc_no_mask_2",
396+
NDData(np.ones((2, 3)),
397+
wcs=None,
398+
uncertainty=StdDevUncertainty(np.ones((2, 3))*0.1),
399+
mask=np.array([[True, False, False], [False, True, False]])),
400+
{"data": np.array([[1, 2, 3], [4, 5, 6]]),
401+
"uncertainty": astropy.nddata.StdDevUncertainty(np.ones((2, 3))*0.1),
402+
"mask": np.array([[True, False, False], [False, True, False]])}
403+
), # ndc has no mask no uncertainty no unit, but nddata has all.
404+
(
405+
"ndcube_2d_ln_lt_unit_unc_mask",
406+
NDData(np.ones((2, 3)),
407+
wcs=None,
408+
uncertainty=StdDevUncertainty(np.ones((2, 3))*0.1),
409+
mask=np.array([[True, False, False], [False, True, False]]),
410+
unit=u.ct),
411+
{"unit": u.ct,
412+
"data": np.array([[1, 2, 3], [4, 5, 6]]),
413+
"uncertainty": astropy.nddata.StdDevUncertainty(np.array([[0.1 , 0.1118034 , 0.14142136],
414+
[0.18027756, 0.2236068 , 0.26925824]])),
415+
"mask": np.array([[True, True, True], [False, True, True]])}
416+
) # both of them have uncertainty and mask and unit.
417+
],
418+
indirect=("ndc",))
419+
def test_cube_arithmetic_add_nddata(ndc, value, expected_kwargs, wcs_2d_lt_ln):
420+
output_cube = ndc + value # perform the multiplication
421+
422+
expected_kwargs["wcs"] = wcs_2d_lt_ln
423+
expected_cube = NDCube(**expected_kwargs)
424+
425+
# Assert output cube is same as expected cube
426+
assert_cubes_equal(output_cube, expected_cube, check_uncertainty_values=True)
383427

384428

385429
@pytest.mark.parametrize('value', [

0 commit comments

Comments
 (0)