|
| 1 | +"""UNIT TESTS FOR SIZE SUBPACKAGE. |
| 2 | +
|
| 3 | +This module contains unit tests for the size subpackage. |
| 4 | +
|
| 5 | +""" |
| 6 | + |
| 7 | +import numpy as np |
| 8 | +from numpy import testing as npt |
| 9 | + |
| 10 | +from unittest import TestCase |
| 11 | + |
| 12 | +from cs_util import size |
| 13 | + |
| 14 | + |
| 15 | +class SizeTestCase(TestCase): |
| 16 | + """Test case for the ``size`` module.""" |
| 17 | + |
| 18 | + def setUp(self): |
| 19 | + """Set test parameter values.""" |
| 20 | + # Unit-sigma Gaussian: T = 2, r50 = 1.17741, FWHM = 2.35482 |
| 21 | + self._sigma = 1.0 |
| 22 | + self._T = 2.0 |
| 23 | + self._r50 = 1.1774100226 |
| 24 | + self._fwhm = 2.3548200450 |
| 25 | + |
| 26 | + def tearDown(self): |
| 27 | + """Unset test parameter values.""" |
| 28 | + self._sigma = None |
| 29 | + self._T = None |
| 30 | + self._r50 = None |
| 31 | + self._fwhm = None |
| 32 | + |
| 33 | + def test_constants(self): |
| 34 | + """Test the module constants against their closed forms.""" |
| 35 | + npt.assert_almost_equal(size.SIGMA_TO_R50, np.sqrt(2 * np.log(2))) |
| 36 | + npt.assert_almost_equal(size.SIGMA_TO_FWHM, 2 * np.sqrt(2 * np.log(2))) |
| 37 | + npt.assert_almost_equal(size.SIGMA_TO_R50, 1.17741, decimal=5) |
| 38 | + npt.assert_almost_equal(size.SIGMA_TO_FWHM, 2.35482, decimal=5) |
| 39 | + |
| 40 | + def test_unit_sigma_values(self): |
| 41 | + """Test all conversions on the unit-sigma Gaussian.""" |
| 42 | + npt.assert_almost_equal(size.T_to_sigma(self._T), self._sigma) |
| 43 | + npt.assert_almost_equal(size.sigma_to_T(self._sigma), self._T) |
| 44 | + npt.assert_almost_equal(size.sigma_to_r50(self._sigma), self._r50) |
| 45 | + npt.assert_almost_equal(size.sigma_to_fwhm(self._sigma), self._fwhm) |
| 46 | + npt.assert_almost_equal(size.T_to_r50(self._T), self._r50) |
| 47 | + npt.assert_almost_equal(size.T_to_fwhm(self._T), self._fwhm) |
| 48 | + |
| 49 | + def test_T_to_r50_closed_form(self): |
| 50 | + """Test ``T_to_r50(T) == sqrt(ln 2 * T)``.""" |
| 51 | + T = np.array([0.05, 0.18, 2.0, 10.0]) |
| 52 | + npt.assert_allclose(size.T_to_r50(T), np.sqrt(np.log(2) * T)) |
| 53 | + |
| 54 | + def test_inverse_direct_values(self): |
| 55 | + """Test each inverse converter directly on the unit-sigma case.""" |
| 56 | + npt.assert_almost_equal(size.r50_to_sigma(self._r50), self._sigma) |
| 57 | + npt.assert_almost_equal(size.fwhm_to_sigma(self._fwhm), self._sigma) |
| 58 | + npt.assert_almost_equal(size.r50_to_T(self._r50), self._T) |
| 59 | + |
| 60 | + def test_nonpositive_T(self): |
| 61 | + """Test that T = 0 maps to size 0 and negative T to NaN.""" |
| 62 | + npt.assert_equal(size.T_to_sigma(0.0), 0.0) |
| 63 | + npt.assert_equal(size.T_to_r50(0.0), 0.0) |
| 64 | + npt.assert_equal(size.T_to_fwhm(0.0), 0.0) |
| 65 | + with np.errstate(invalid="ignore"): |
| 66 | + self.assertTrue(np.isnan(size.T_to_r50(-1.0))) |
| 67 | + |
| 68 | + def test_round_trips(self): |
| 69 | + """Test that inverse pairs compose to the identity.""" |
| 70 | + values = np.array([0.01, 0.1, 1.0, 7.5]) |
| 71 | + npt.assert_allclose(size.r50_to_T(size.T_to_r50(values)), values) |
| 72 | + npt.assert_allclose(size.T_to_r50(size.r50_to_T(values)), values) |
| 73 | + npt.assert_allclose( |
| 74 | + size.sigma_to_T(size.T_to_sigma(values)), values |
| 75 | + ) |
| 76 | + npt.assert_allclose( |
| 77 | + size.r50_to_sigma(size.sigma_to_r50(values)), values |
| 78 | + ) |
| 79 | + npt.assert_allclose( |
| 80 | + size.fwhm_to_sigma(size.sigma_to_fwhm(values)), values |
| 81 | + ) |
| 82 | + |
| 83 | + def test_fwhm_is_twice_r50(self): |
| 84 | + """Test ``FWHM = 2 r50`` for a Gaussian, via both paths.""" |
| 85 | + T = np.array([0.05, 0.18, 2.0]) |
| 86 | + npt.assert_allclose(size.T_to_fwhm(T), 2 * size.T_to_r50(T)) |
| 87 | + |
| 88 | + def test_array_input(self): |
| 89 | + """Test that array input returns arrays of the same shape.""" |
| 90 | + T = np.linspace(0.01, 5.0, 11) |
| 91 | + r50 = size.T_to_r50(T) |
| 92 | + self.assertEqual(r50.shape, T.shape) |
| 93 | + self.assertTrue(np.all(np.diff(r50) > 0)) |
0 commit comments