Coverage for python/lsst/obs/base/tests.py: 28%
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# This file is part of obs_base.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <https://www.gnu.org/licenses/>.
22"""
23Test utilities for obs_base and concrete obs* packages.
24"""
26__all__ = (
27 "ObsTests",
28 "make_ramp_array",
29 "make_ramp_exposure_trimmed",
30 "make_ramp_exposure_untrimmed",
31)
33import logging
34import numpy as np
36from lsst.afw.image import Exposure
37from lsst.afw.cameraGeom.utils import calcRawCcdBBox
39from . import butler_tests
40from . import mapper_tests
41from . import camera_tests
44class ObsTests(butler_tests.ButlerGetTests, mapper_tests.MapperTests,
45 camera_tests.CameraTests):
46 """Aggregator class for all of the obs_* test classes.
48 Inherit from this class, then lsst.utils.tests.TestCase, in that order.
50 Example subclass:
52 .. code-block:: python
54 class TestObs(lsst.obs.base.tests.ObsTests, lsst.utils.tests.TestCase):
55 def setUp(self):
56 self.setUp_tests(...)
57 self.setUp_butler_get(...)
58 self.setUp_mapper(...)
59 self.setUp_camera(...)
61 Notes
62 -----
63 The intention is for each obs package to have a single test class that
64 inherits from this collector class, thus "automatically" getting all new
65 tests. If those tests require setup that isn't defined in a given obs
66 package, that obs package will be broken until updated. This is
67 intentional, as a way to prevent obs packages from falling behind out of
68 neglect.
69 """
71 def setUp_tests(self, butler, mapper, dataIds):
72 """Set up the necessary shared variables used by multiple tests.
74 Parameters
75 ----------
76 butler: lsst.daf.persistence.Butler
77 A butler object, instantiated on the testdata repository for the
78 obs package being tested.
79 mapper: lsst.obs.CameraMapper
80 A CameraMapper object for your camera, instantiated on the testdata
81 repository the obs package being tested.
82 dataIds: dict
83 dictionary of (exposure name): (dataId of that exposure in the
84 testdata repository), with unittest.SkipTest as the value for any
85 exposures you do not have/do not want to test. It must contain a
86 valid 'raw' dataId, in addition to 'bias','flat','dark', which may
87 be set to SkipTest. For example::
89 self.dataIds = {'raw': {'visit': 1, 'filter': 'g'},
90 'bias': {'visit': 1},
91 'flat': {'visit': 1},
92 'dark': unittest.SkipTest
93 }
94 """
95 self.butler = butler
96 self.mapper = mapper
97 self.dataIds = dataIds
98 self.log = logging.getLogger(__name__)
100 def tearDown(self):
101 del self.butler
102 del self.mapper
103 super(ObsTests, self).tearDown()
106def make_ramp_array(bbox, pedestal):
107 """Make a 2-d ramp array.
109 Parameters
110 ----------
111 bbox : `lsst.geom.Box2I`
112 Bounding box for the array.
113 pedestal : `int`
114 Minimum value for the ramp.
116 Returns
117 -------
118 ramp : `np.ndarray`
119 A 2-d array with shape ``(bbox.getHeight(), bbox.getWidth())``.
120 end : `int`
121 One past the maximum value in the ramp (for use as the
122 pedestal for another box).
123 """
124 end = pedestal + bbox.getArea()
125 return np.arange(pedestal, end).reshape(bbox.getHeight(), bbox.getWidth()), end
128def make_ramp_exposure_untrimmed(detector, dtype=None):
129 """Create an untrimmed, assembled exposure with different ramps for
130 each sub-amplifier region.
132 Parameters
133 ----------
134 detector : `lsst.afw.cameraGeom.Detector`
135 Detector object that the new exposure should match. Must have all amp
136 flips and offsets set to False/zero (i.e. represent an already-
137 assembled image).
138 dtype : `np.dtype`, optional
139 Type of the new exposure. Defaults to ``int32``.
141 Returns
142 -------
143 exposure : `lsst.afw.image.Exposure`
144 New exposure with the given detector attached.
145 """
146 if dtype is None:
147 dtype = np.dtype(np.int32)
148 ramp_exposure = Exposure(calcRawCcdBBox(detector), dtype=np.dtype(dtype))
149 ramp_exposure.setDetector(detector)
150 pedestal = 0
151 for amp in detector:
152 for name in ("HorizontalOverscan", "VerticalOverscan", "Prescan", "Data"):
153 bbox = getattr(amp, f"getRaw{name}BBox")()
154 ramp, pedestal = make_ramp_array(bbox, pedestal)
155 ramp_exposure.image[bbox].array[:, :] = ramp
156 return ramp_exposure
159def make_ramp_exposure_trimmed(detector, dtype=None):
160 """Create a trimmed, assembled exposure with different ramps for
161 each amplifier region.
163 Parameters
164 ----------
165 detector : `lsst.afw.cameraGeom.Detector`
166 Detector object that the new exposure should match.
167 dtype : `np.dtype`, optional
168 Type of the new exposure. Defaults to ``int32``.
170 Returns
171 -------
172 exposure : `lsst.afw.image.Exposure`
173 New exposure with the given detector attached.
174 """
175 if dtype is None:
176 dtype = np.dtype(np.int32)
177 ramp_exposure = Exposure(detector.getBBox(), dtype=np.dtype(dtype))
178 ramp_exposure.setDetector(detector)
179 pedestal = 0
180 for amp in detector:
181 ramp, pedestal = make_ramp_array(amp.getBBox(), pedestal)
182 ramp_exposure.image[amp.getBBox()].array[:, :] = ramp
183 return ramp_exposure