Coverage for tests/test_dispersion.py: 96%
76 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-17 09:53 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-17 09:53 +0000
1#!/usr/bin/env python
2# This file is part of atmospec.
3#
4# Developed for the LSST Data Management System.
5# This product includes software developed by the LSST Project
6# (https://www.lsst.org).
7# See the COPYRIGHT file at the top-level directory of this distribution
8# for details of code ownership.
9#
10# This program is free software: you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation, either version 3 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program. If not, see <https://www.gnu.org/licenses/>.
22"""Test cases for atmospec dispersion relation."""
24import unittest
25import numpy as np
27import lsst.utils
28import lsst.utils.tests
29from lsst.atmospec.dispersion import DispersionRelation
32class DispersionRelationTestCase(lsst.utils.tests.TestCase):
33 """A test case for the DispersionRelation class."""
35 def testImport(self):
36 import lsst.atmospec.dispersion as dispersion # noqa: F401
38 def test_init_and_coefficients(self):
39 # A simple 1-to-1 mapping: pixel == wavelength
40 observedLines = [1.0, 2.0, 3.0]
41 spectralLines = [1.0, 2.0, 3.0]
42 dr = DispersionRelation(observedLines, spectralLines)
43 self.assertEqual(dr.observedLines, observedLines)
44 self.assertEqual(dr.spectralLines, spectralLines)
45 self.assertEqual(len(dr.pix2wlCoeffs), 2)
46 # slope ~ 1, intercept ~ 0
47 self.assertAlmostEqual(dr.pix2wlCoeffs[0], 1.0)
48 self.assertAlmostEqual(dr.pix2wlCoeffs[1], 0.0)
50 def test_init_nontrivial(self):
51 # wavelength = 2 * pixel + 5
52 observedLines = [0.0, 10.0, 20.0]
53 spectralLines = [5.0, 25.0, 45.0]
54 dr = DispersionRelation(observedLines, spectralLines)
55 self.assertAlmostEqual(dr.pix2wlCoeffs[0], 2.0)
56 self.assertAlmostEqual(dr.pix2wlCoeffs[1], 5.0)
58 def test_calcCoefficients_none_observed(self):
59 # observedLines is None -> warning branch, defaults to 1-to-1
60 dr = DispersionRelation(None, [1.0, 2.0])
61 self.assertEqual(dr.observedLines, [1, 2])
62 self.assertEqual(dr.spectralLines, [1, 2])
63 self.assertAlmostEqual(dr.pix2wlCoeffs[0], 1.0)
64 self.assertAlmostEqual(dr.pix2wlCoeffs[1], 0.0)
66 def test_calcCoefficients_none_spectral(self):
67 # spectralLines is None -> warning branch, defaults to 1-to-1
68 dr = DispersionRelation([1.0, 2.0], None)
69 self.assertEqual(dr.observedLines, [1, 2])
70 self.assertEqual(dr.spectralLines, [1, 2])
71 self.assertAlmostEqual(dr.pix2wlCoeffs[0], 1.0)
72 self.assertAlmostEqual(dr.pix2wlCoeffs[1], 0.0)
74 def test_calcCoefficients_both_none(self):
75 dr = DispersionRelation(None, None)
76 self.assertEqual(dr.observedLines, [1, 2])
77 self.assertEqual(dr.spectralLines, [1, 2])
79 def test_Pixel2Wavelength_scalar(self):
80 # wavelength = 2 * pixel + 5
81 dr = DispersionRelation([0.0, 10.0], [5.0, 25.0])
82 wl = dr.Pixel2Wavelength(3.0)
83 self.assertAlmostEqual(float(wl), 11.0)
85 def test_Pixel2Wavelength_array(self):
86 dr = DispersionRelation([0.0, 10.0], [5.0, 25.0])
87 pixels = np.array([0.0, 1.0, 2.0])
88 wl = dr.Pixel2Wavelength(pixels)
89 np.testing.assert_allclose(wl, [5.0, 7.0, 9.0])
91 def test_Wavelength2Pixel_scalar(self):
92 dr = DispersionRelation([0.0, 10.0], [5.0, 25.0])
93 pix = dr.Wavelength2Pixel(11.0)
94 self.assertAlmostEqual(float(pix), 3.0)
96 def test_Wavelength2Pixel_array(self):
97 dr = DispersionRelation([0.0, 10.0], [5.0, 25.0])
98 wavelengths = np.array([5.0, 7.0, 9.0])
99 pix = dr.Wavelength2Pixel(wavelengths)
100 np.testing.assert_allclose(pix, [0.0, 1.0, 2.0], atol=1e-9)
102 def test_roundTrip_scalar(self):
103 dr = DispersionRelation([0.0, 10.0, 20.0], [5.0, 25.0, 45.0])
104 pixel = 7.0
105 wl = dr.Pixel2Wavelength(pixel)
106 recovered = dr.Wavelength2Pixel(wl)
107 self.assertAlmostEqual(float(recovered), pixel)
109 def test_roundTrip_array(self):
110 dr = DispersionRelation([0.0, 10.0, 20.0], [5.0, 25.0, 45.0])
111 pixels = np.array([1.0, 5.0, 12.5, 100.0])
112 wl = dr.Pixel2Wavelength(pixels)
113 recovered = dr.Wavelength2Pixel(wl)
114 np.testing.assert_allclose(recovered, pixels)
117class TestMemory(lsst.utils.tests.MemoryTestCase):
118 pass
121def setup_module(module):
122 lsst.utils.tests.init()
125if __name__ == "__main__": 125 ↛ 126line 125 didn't jump to line 126 because the condition on line 125 was never true
126 lsst.utils.tests.init()
127 unittest.main()