lsst.obs.base  13.0-40-g96cd6c9+3
 All Classes Namespaces Files Functions Variables
butler_tests.py
Go to the documentation of this file.
1 from __future__ import absolute_import, division, print_function
2 from future.utils import with_metaclass
3 #
4 # LSST Data Management System
5 # Copyright 2016 LSST Corporation.
6 #
7 # This product includes software developed by the
8 # LSST Project (http://www.lsst.org/).
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 LSST License Statement and
21 # the GNU General Public License along with this program. If not,
22 # see <http://www.lsstcorp.org/LegalNotices/>.
23 #
24 import abc
25 import inspect
26 import unittest
27 import collections
28 
29 
30 class ButlerGetTests(with_metaclass(abc.ABCMeta)):
31  """
32  Tests of obs_* Butler get() functionality.
33 
34  In the subclasses's setUp():
35  * Call setUp_butler_get() to fill in required parameters.
36  """
37 
38  def setUp_butler_get(self,
39  ccdExposureId_bits=None,
40  exposureIds=None,
41  filters=None,
42  exptimes=None,
43  detectorIds=None,
44  detector_names=None,
45  detector_serials=None,
46  dimensions=None,
47  sky_origin=None,
48  raw_subsets=None,
49  good_detectorIds=None,
50  bad_detectorIds=None,
51  linearizer_type=None
52  ):
53  """
54  Set up the necessary variables for butlerGet tests.
55 
56  All "exposure name" entries below should correspond to an entry in
57  self.dataIds.
58 
59  Parameters
60  ----------
61 
62  ccdExposureId_bits : `int`
63  expected value of ccdExposureId_bits
64  exposureIds : `dict`
65  dict of exposure name : ccdExposureId (the number as returned by the butler)
66  filters : `dict`
67  dict of exposure name : filter name
68  exptimes : `dict`
69  dict of exposure name : exposure time
70  detector_names : `dict`
71  dict of exposure name : detector name
72  detectorIds : `dict`
73  dict of exposure name : detectorId
74  detector_serials : `dict`
75  dict of exposure name : detector serial
76  dimensions : `dict`
77  dict of exposure name : dimensions (as a geom.Extent2I)
78  sky_origin : `tuple` of `float`
79  Longitude, Latitude of 'raw' exposure
80  raw_subsets : `tuple` of (kwargs, `int`)
81  keyword args and expected number of subsets for butler.subset('raw', **kwargs)
82  good_detectorIds : `list` of `int`
83  list of valid ccd numbers
84  bad_detectorIds : `list` of `int`
85  list of invalid ccd numbers
86  linearizer_type : `dict`
87  dict of detectorId (usually `int`): LinearizerType
88  (e.g. lsst.ip.isr.LinearizeLookupTable.LinearityType),
89  or unittest.SkipTest to skip all linearizer tests.
90  """
91 
92  fields = ['ccdExposureId_bits',
93  'exposureIds',
94  'filters',
95  'exptimes',
96  'detector_names',
97  'detectorIds',
98  'detector_serials',
99  'dimensions',
100  'sky_origin',
101  'raw_subsets',
102  'good_detectorIds',
103  'bad_detectorIds',
104  'linearizer_type'
105  ]
106  ButlerGet = collections.namedtuple("ButlerGetData", fields)
107 
108  self.butler_get_data = ButlerGet(ccdExposureId_bits=ccdExposureId_bits,
109  exposureIds=exposureIds,
110  filters=filters,
111  exptimes=exptimes,
112  detectorIds=detectorIds,
113  detector_names=detector_names,
114  detector_serials=detector_serials,
115  dimensions=dimensions,
116  sky_origin=sky_origin,
117  raw_subsets=raw_subsets,
118  good_detectorIds=good_detectorIds,
119  bad_detectorIds=bad_detectorIds,
120  linearizer_type=linearizer_type
121  )
122 
124  bits = self.butler.get('ccdExposureId_bits')
125  self.assertEqual(bits, self.butler_get_data.ccdExposureId_bits)
126 
127  def _test_exposure(self, name):
128  if self.dataIds[name] is unittest.SkipTest:
129  self.skipTest('Skipping %s as requested' % (inspect.currentframe().f_code.co_name))
130 
131  exp = self.butler.get(name, self.dataIds[name])
132  self.assertEqual(exp.getDimensions(), self.butler_get_data.dimensions[name])
133  self.assertEqual(exp.getDetector().getId(), self.butler_get_data.detectorIds[name])
134  self.assertEqual(exp.getDetector().getName(), self.butler_get_data.detector_names[name])
135  self.assertEqual(exp.getDetector().getSerial(), self.butler_get_data.detector_serials[name])
136  self.assertEqual(exp.getFilter().getName(), self.butler_get_data.filters[name])
137  exposureId = self.butler.get('ccdExposureId', dataId=self.dataIds[name])
138  self.assertEqual(exposureId, self.butler_get_data.exposureIds[name])
139  self.assertEqual(exp.getInfo().getVisitInfo().getExposureTime(), self.butler_get_data.exptimes[name])
140  return exp
141 
142  def test_raw(self):
143  exp = self._test_exposure('raw')
144  # We only test the existence of WCS in the raw files, since it's only well-defined
145  # for raw, and other exposure types could have or not have a WCS depending
146  # on various implementation details.
147  self.assertEqual(exp.hasWcs(), True)
148  origin = exp.getWcs().getSkyOrigin()
149  self.assertAlmostEqual(origin.getLongitude().asDegrees(), self.butler_get_data.sky_origin[0])
150  self.assertAlmostEqual(origin.getLatitude().asDegrees(), self.butler_get_data.sky_origin[1])
151 
152  def test_bias(self):
153  self._test_exposure('bias')
154 
155  def test_dark(self):
156  self._test_exposure('dark')
157 
158  def test_flat(self):
159  self._test_exposure('flat')
160 
161  @unittest.skip('Cannot test this, as there is a bug in the butler! DM-8097')
162  def test_raw_sub_bbox(self):
163  exp = self.butler.get('raw', self.dataIds['raw'], immediate=True)
164  bbox = exp.getBBox()
165  bbox.grow(-1)
166  sub = self.butler.get("raw_sub", self.dataIds['raw'], bbox=bbox, immediate=True)
167  self.assertEqual(sub.getImage().getBBox(), bbox)
168  self.assertImagesEqual(sub, exp.Factory(exp, bbox))
169 
170  def test_subset_raw(self):
171  for kwargs, expect in self.butler_get_data.raw_subsets:
172  subset = self.butler.subset("raw", **kwargs)
173  self.assertEqual(len(subset), expect, msg="Failed for kwargs: {}".format(kwargs))
174 
176  """Test that we can get a linearizer for good detectorIds."""
177  if self.butler_get_data.linearizer_type is unittest.SkipTest:
178  self.skipTest('Skipping %s as requested' % (inspect.currentframe().f_code.co_name))
179 
180  camera = self.butler.get("camera")
181  for detectorId in self.butler_get_data.good_detectorIds:
182  detector = camera[detectorId]
183  linearizer = self.butler.get("linearizer", dataId=dict(ccd=detectorId), immediate=True)
184  self.assertEqual(linearizer.LinearityType, self.butler_get_data.linearizer_type[detectorId])
185  linearizer.checkDetector(detector)
186 
188  """Do bad detectorIds raise?"""
189  if self.butler_get_data.linearizer_type is unittest.SkipTest:
190  self.skipTest('Skipping %s as requested' % (inspect.currentframe().f_code.co_name))
191 
192  for badccd in self.butler_get_data.bad_detectorIds:
193  with self.assertRaises(RuntimeError):
194  self.butler.get("linearizer", dataId=dict(ccd=badccd), immediate=True)