"""Define tests for confidence calculation.""" import os # noqa import ancillary_data as anc import numpy as np import pytest import xarray as xr from mvcm import conf @pytest.fixture def fixturepath(): """Define path.""" return os.path.join(os.path.dirname(__file__), "fixtures") @pytest.fixture def rad(): """Define dummy radiance array.""" return np.arange(260, 282) @pytest.fixture def single_threshold(): """Define dummy single thresholds.""" return [267, 270, 273, 1] @pytest.fixture def double_threshold(): """Define dummy double thresholds.""" return [264, 267, 270, 273, 276, 279, 1] @pytest.fixture def reference_data(fixturepath): """Define reference file name.""" return os.path.join(fixturepath, "ref_conf.npz") def test_single_threshold(rad, single_threshold, reference_data): """Test single threshold compuputation.""" ref_confidence = np.load(reference_data)["ref_sgl"] ref_confidence_flipped = np.load(reference_data)["ref_sgl_flipped"] c = conf.conf_test_new(rad, single_threshold) assert np.all(c == ref_confidence) single_threshold[0:-1] = single_threshold[-2::-1] c = conf.conf_test_new(rad, single_threshold) assert np.all(c == ref_confidence_flipped) def test_double_threshold(rad, double_threshold, reference_data): """Test double threshold computation.""" ref_confidence = np.load(reference_data)["ref_dbl"] ref_confidence_flipped = np.load(reference_data)["ref_dbl_flipped"] c = conf.conf_test_dble(rad, double_threshold) assert np.all(c == ref_confidence) double_threshold[0:-1] = double_threshold[-2::-1] c = conf.conf_test_dble(rad, double_threshold) assert np.all(c == ref_confidence_flipped) def test_c_single_threshold(rad, single_threshold, reference_data): """Test C version of single threshold computation.""" ref_confidence = np.load(reference_data)["ref_sgl"] ref_confidence_flipped = np.load(reference_data)["ref_sgl_flipped"] locut = np.full(np.shape(rad), single_threshold[0], dtype=np.float32) midpt = np.full(np.shape(rad), single_threshold[1], dtype=np.float32) hicut = np.full(np.shape(rad), single_threshold[2], dtype=np.float32) power = np.full(np.shape(rad), single_threshold[3], dtype=np.float32) c = anc.py_conf_test(np.array(rad, dtype=np.float32), locut, hicut, power, midpt) # NOTE: I have to use allclose() because the ref_confidence has been computed with doouble # precision and the C version returns single precision, which has some rounding differences assert np.allclose(c, ref_confidence) single_threshold[0:-1] = single_threshold[-2::-1] locut = np.full(np.shape(rad), single_threshold[0], dtype=np.float32) midpt = np.full(np.shape(rad), single_threshold[1], dtype=np.float32) hicut = np.full(np.shape(rad), single_threshold[2], dtype=np.float32) power = np.full(np.shape(rad), single_threshold[3], dtype=np.float32) c = anc.py_conf_test(np.array(rad, dtype=np.float32), locut, hicut, power, midpt) assert np.allclose(c, ref_confidence_flipped) def test_xr_single_threshold(rad, single_threshold, reference_data): """Test xarray version of single threshold computation.""" ref_confidence = np.load(reference_data)["ref_sgl"] ref_confidence_flipped = np.load(reference_data)["ref_sgl_flipped"] c = conf.asymm_confidence(xr.DataArray(rad), np.array(single_threshold)) assert np.allclose(c.values, ref_confidence) single_threshold[0:-1] = single_threshold[-2::-1] c = conf.asymm_confidence(xr.DataArray(rad), np.array(single_threshold)) assert np.allclose(c.values, ref_confidence_flipped)