diff --git a/ccdproc/tests/test_ccdproc.py b/ccdproc/tests/test_ccdproc.py index 1d27b1cc..ae7c7bce 100644 --- a/ccdproc/tests/test_ccdproc.py +++ b/ccdproc/tests/test_ccdproc.py @@ -172,8 +172,8 @@ def test_subtract_overscan(median, transpose, data_rectangle): # Since some array libraries do not support in-place operations, we # work on the science and overscan regions separately. - science_data = ccd_data.data[science_region].copy() - overscan_data = 0 * ccd_data.data[oscan_region].copy() + oscan + science_data = xp.asarray(ccd_data.data[science_region], copy=True) + overscan_data = 0 * xp.asarray(ccd_data.data[oscan_region], copy=True) + oscan # Add a fake sky background so the "science" part of the image has a # different average than the "overscan" part. @@ -201,7 +201,7 @@ def test_subtract_overscan(median, transpose, data_rectangle): ) ) # Is the overscan region zero? - assert (ccd_data_overscan.data[oscan_region] == 0).all() + assert xp.all(ccd_data_overscan.data[oscan_region] == 0) # Now do what should be the same subtraction, with the overscan specified # with the fits_section @@ -222,7 +222,7 @@ def test_subtract_overscan(median, transpose, data_rectangle): ) ) # Is the overscan region zero? - assert (ccd_data_fits_section.data[oscan_region] == 0).all() + assert xp.all(ccd_data_fits_section.data[oscan_region] == 0) # Do both ways of subtracting overscan give exactly the same result? assert xp.all( @@ -353,7 +353,7 @@ def test_subtract_overscan_fails(): subtract_overscan(xp.zeros((10, 10)), 3, median=False, model=None) # Do we get an error if we specify both overscan and fits_section? with pytest.raises(TypeError): - subtract_overscan(ccd_data, overscan=ccd_data[0:10], fits_section="[1:10]") + subtract_overscan(ccd_data, overscan=ccd_data[0:10, ...], fits_section="[1:10]") # Do we raise an error if we specify neither overscan nor fits_section? with pytest.raises(TypeError): subtract_overscan(ccd_data) @@ -402,7 +402,7 @@ def test_trim_with_wcs_alters_wcs(): ccd_data = ccd_data_func() # WCS construction example pulled form astropy.wcs docs wcs = WCS(naxis=2) - wcs.wcs.crpix = xp.asarray(ccd_data.shape) / 2 + wcs.wcs.crpix = xp.asarray(ccd_data.shape, dtype=xp.float64) / 2 wcs.wcs.cdelt = xp.asarray([-0.066667, 0.066667]) wcs.wcs.crval = [0, -90] wcs.wcs.ctype = ["RA---AIR", "DEC--AIR"] @@ -951,7 +951,7 @@ def test__overscan_schange(): ccd_data = ccd_data_func() old_data = ccd_data.copy() new_data = subtract_overscan(ccd_data, overscan=ccd_data[:, 1], overscan_axis=0) - assert not xp.allclose(old_data.data, new_data.data) + assert not xp.all(xpx.isclose(old_data.data, new_data.data)) assert xp.all(xpx.isclose(old_data.data, ccd_data.data)) diff --git a/ccdproc/tests/test_ccdproc_logging.py b/ccdproc/tests/test_ccdproc_logging.py index 6b903629..893648d0 100644 --- a/ccdproc/tests/test_ccdproc_logging.py +++ b/ccdproc/tests/test_ccdproc_logging.py @@ -5,6 +5,7 @@ from astropy.nddata import CCDData from ccdproc import Keyword, create_deviation, subtract_bias, trim_image +from ccdproc.conftest import testing_array_library as xp from ccdproc.core import _short_names from ccdproc.tests.pytest_fixtures import ccd_data as ccd_data_func @@ -72,7 +73,7 @@ def test_implicit_logging(): # should happen: # + A key named func.__name__ is created, with # + value that is the list of arguments the function was called with. - bias = CCDData(np.zeros_like(ccd_data.data), unit="adu") + bias = CCDData(xp.zeros_like(ccd_data.data), unit="adu") result = subtract_bias(ccd_data, bias) assert "subtract_bias" in result.header assert result.header["subtract_bias"] == ( diff --git a/ccdproc/tests/test_combiner.py b/ccdproc/tests/test_combiner.py index 9e8dcab7..8a8ff17c 100644 --- a/ccdproc/tests/test_combiner.py +++ b/ccdproc/tests/test_combiner.py @@ -358,7 +358,7 @@ def test_pixelwise_weights(): ] combo = Combiner(ccd_list) combo.weights = xp.ones_like(combo._data_arr) - combo.weights = xpx.at(combo.weights)[:, 5, 5].set(xp.asarray([1, 5, 10])) + combo.weights = xpx.at(combo.weights)[:, 5, 5].set(xp.asarray([1.0, 5.0, 10.0])) ccd = combo.average_combine() assert xp.all(xpx.isclose(ccd.data[5, 5], 312.5)) assert xp.all(xpx.isclose(ccd.data[0, 0], 0)) @@ -463,7 +463,7 @@ def test_combiner_minmax(): c = Combiner(ccd_list) c.minmax_clipping(min_clip=-500, max_clip=500) ccd = c.median_combine() - assert ccd.data.mean() == 0 + assert xp.mean(ccd.data) == 0 def test_combiner_minmax_max(): @@ -475,7 +475,7 @@ def test_combiner_minmax_max(): c = Combiner(ccd_list) c.minmax_clipping(min_clip=None, max_clip=500) - assert c._data_arr_mask[2].all() + assert xp.all(c._data_arr_mask[2, ...]) def test_combiner_minmax_min(): @@ -487,7 +487,7 @@ def test_combiner_minmax_min(): c = Combiner(ccd_list) c.minmax_clipping(min_clip=-500, max_clip=None) - assert c._data_arr_mask[1].all() + assert xp.all(c._data_arr_mask[1, ...]) def test_combiner_sigmaclip_high(): @@ -581,10 +581,10 @@ def test_combiner_sum(): # test weighted sum def test_combiner_sum_weighted(): - ccd_data = CCDData(data=xp.asarray([[0, 1], [2, 3]]), unit="adu") + ccd_data = CCDData(data=xp.asarray([[0.0, 1.0], [2.0, 3.0]]), unit="adu") ccd_list = [ccd_data, ccd_data, ccd_data] c = Combiner(ccd_list) - c.weights = xp.asarray([1, 2, 3]) + c.weights = xp.asarray([1.0, 2.0, 3.0]) ccd = c.sum_combine() expected_result = sum(w * d.data for w, d in zip(c.weights, ccd_list, strict=True)) assert xp.all(xpx.isclose(ccd.data, expected_result)) @@ -596,10 +596,10 @@ def test_combiner_sum_weighted_by_pixel(): ccd_list = [ccd_data, ccd_data, ccd_data] c = Combiner(ccd_list) # Weights below are chosen so that every entry in - weights_pixel = [[8, 4], [2, 1]] + weights_pixel = [[8.0, 4.0], [2.0, 1.0]] c.weights = xp.asarray([weights_pixel] * 3) ccd = c.sum_combine() - expected_result = xp.asarray([[24, 24], [24, 24]]) + expected_result = xp.asarray([[24.0, 24.0], [24.0, 24.0]]) assert xp.all(xpx.isclose(ccd.data, expected_result)) @@ -610,11 +610,11 @@ def test_combiner_sum_weighted_with_mask(): CCDData(xp.asarray([[10, 20]]), unit=u.adu), ] combiner = Combiner(ccd_list) - combiner.weights = xp.asarray([1, 3]) + combiner.weights = xp.asarray([1.0, 3.0]) combined = combiner.sum_combine() - expected = xp.asarray([[30, 62]]) + expected = xp.asarray([[30.0, 62.0]]) assert xp.all(xpx.isclose(combined.data, expected)) @@ -1107,7 +1107,7 @@ def test_combiner_uncertainty_average(): # Just the standard deviation of ccd data. ref_uncertainty = xp.ones((10, 10)) / 2 # Correction because we combined two images. - ref_uncertainty /= xp.sqrt(2) + ref_uncertainty /= xp.sqrt(xp.asarray(2.0)) assert xp.all(xpx.isclose(ccd.uncertainty.array, ref_uncertainty)) @@ -1124,11 +1124,11 @@ def test_combiner_uncertainty_average_mask(): c = Combiner(ccd_list) ccd = c.average_combine() # Just the standard deviation of ccd data. - ref_uncertainty = xp.ones((10, 10)) * xp.std(xp.asarray([1, 2, 3])) + ref_uncertainty = xp.ones((10, 10)) * xp.std(xp.asarray([1.0, 2.0, 3.0])) # Correction because we combined two images. - ref_uncertainty /= xp.sqrt(3) + ref_uncertainty /= xp.sqrt(xp.asarray(3.0)) ref_uncertainty = xpx.at(ref_uncertainty)[5, 5].set( - xp.std(xp.asarray([2, 3])) / xp.sqrt(2) + xp.std(xp.asarray([2.0, 3.0])) / xp.sqrt(xp.asarray(2.0)) ) assert xp.all(xpx.isclose(ccd.uncertainty.array, ref_uncertainty)) @@ -1147,14 +1147,14 @@ def test_combiner_uncertainty_median_mask(): c = Combiner(ccd_list) ccd = c.median_combine() # Just the standard deviation of ccd data. - ref_uncertainty = xp.ones((10, 10)) * mad_to_sigma * mad([1, 2, 3]) - # Correction because we combined two images. - ref_uncertainty /= xp.sqrt(3) # 0.855980789955 # It turns out that the expression below evaluates to a np.float64, which # introduces numpy into the array namespace, which raises an error # when arrat_api_compat tries to figure out the namespace. Casting # it to a regular float fixes that. - med_value = float(mad_to_sigma * mad([2, 3]) / xp.sqrt(2)) + ref_uncertainty = xp.ones((10, 10)) * float(mad_to_sigma * mad([1, 2, 3])) + # Correction because we combined two images. + ref_uncertainty /= xp.sqrt(xp.asarray(3.0)) # 0.855980789955 + med_value = float(mad_to_sigma * mad([2, 3])) / float(xp.sqrt(xp.asarray(2.0))) ref_uncertainty = xpx.at(ref_uncertainty)[5, 5].set(med_value) # 0.524179041254 assert xp.all(xpx.isclose(ccd.uncertainty.array, ref_uncertainty)) @@ -1172,10 +1172,10 @@ def test_combiner_uncertainty_sum_mask(): c = Combiner(ccd_list) ccd = c.sum_combine() # Just the standard deviation of ccd data. - ref_uncertainty = xp.ones((10, 10)) * xp.std(xp.asarray([1, 2, 3])) - ref_uncertainty *= xp.sqrt(3) + ref_uncertainty = xp.ones((10, 10)) * xp.std(xp.asarray([1.0, 2.0, 3.0])) + ref_uncertainty *= xp.sqrt(xp.asarray(3.0)) ref_uncertainty = xpx.at(ref_uncertainty)[5, 5].set( - xp.std(xp.asarray([2, 3])) * xp.sqrt(2) + xp.std(xp.asarray([2.0, 3.0])) * xp.sqrt(xp.asarray(2.0)) ) assert xp.all(xpx.isclose(ccd.uncertainty.array, ref_uncertainty)) @@ -1517,12 +1517,16 @@ def my_summer(data, mask, axis=None): xp = array_api_compat.array_namespace(data) new_data = [] for i in range(data.shape[0]): - if mask[i] is not None: - new_data.append(data[i] * ~mask[i]) + if mask[i, ...] is not None: + new_data.append( + xp.where( + mask[i, ...], xp.zeros_like(data[i, ...]), data[i, ...] + ) + ) else: - new_data.append(xp.zeros_like(data[i])) + new_data.append(xp.zeros_like(data[i, ...])) - new_data = xp.asarray(new_data) + new_data = xp.stack(new_data) def sum_func(_, axis=axis): return xp.sum(new_data, axis=axis) diff --git a/ccdproc/tests/test_rebin.py b/ccdproc/tests/test_rebin.py index 17bf0fb9..66aa41bb 100644 --- a/ccdproc/tests/test_rebin.py +++ b/ccdproc/tests/test_rebin.py @@ -7,6 +7,7 @@ from astropy.nddata import StdDevUncertainty from astropy.utils.exceptions import AstropyDeprecationWarning +from ccdproc.conftest import testing_array_library as xp from ccdproc.core import rebin from ccdproc.tests.pytest_fixtures import ccd_data as ccd_data_func @@ -67,7 +68,7 @@ def test_rebin_smaller(): def test_rebin_ccddata(mask_data, uncertainty): ccd_data = ccd_data_func(data_size=10) if mask_data: - ccd_data.mask = np.zeros_like(ccd_data) + ccd_data.mask = xp.zeros_like(ccd_data.data) if uncertainty: err = np.random.default_rng().normal(size=ccd_data.shape) ccd_data.uncertainty = StdDevUncertainty(err)