Coverage for tests/test_calexpCutout.py : 23%

Hot-keys 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#
2# LSST Data Management System
3# Copyright 2008-2016 AURA/LSST.
4#
5# This product includes software developed by the
6# LSST Project (http://www.lsst.org/).
7#
8# This program is free software: you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation, either version 3 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the LSST License Statement and
19# the GNU General Public License along with this program. If not,
20# see <https://www.lsstcorp.org/LegalNotices/>.
21#
22"""Test configuration of CalexpCutoutTask and the run method.
24Run the task on one test image and perform various checks on the results
25"""
26import os
27import random
28import unittest
29from astropy.coordinates import SkyCoord
30from astropy.table import QTable
31import astropy.units as u
33import lsst.utils
34import lsst.utils.tests
35from lsst.afw.image import ExposureF
36from lsst.pipe.tasks.calexpCutout import CalexpCutoutTask
38random.seed(208241138)
40packageDir = lsst.utils.getPackageDir('pipe_tasks')
41datadir = os.path.join(packageDir, 'tests', "data")
44def make_data(bbox, wcs, border=100, ngood=13, nbad=7, nedge=3):
45 dx, dy = bbox.getDimensions()
46 data = {}
48 ident = []
49 pt = []
50 size = []
51 for i in range(ngood):
52 x = random.random()*(dx - 2*border) + border
53 y = random.random()*(dy - 2*border) + border
54 sphpt = wcs.pixelToSky(x, y)
55 pt.append(SkyCoord(sphpt.getRa().asDegrees(), sphpt.getDec().asDegrees(),
56 frame='icrs', unit=u.deg))
57 ident.append((i+1)*u.dimensionless_unscaled)
58 size.append(random.randint(13, 26)*u.dimensionless_unscaled)
59 data['good'] = QTable([ident, pt, size], names=['id', 'position', 'size'])
61 ident = []
62 pt = []
63 size = []
64 for i in range(nbad):
65 x = random.random()*(dx - 2*border) + border
66 y = random.random()*(dy - 2*border) + border
67 sphpt = wcs.pixelToSky(-x, -y)
68 pt.append(SkyCoord(sphpt.getRa().asDegrees(), sphpt.getDec().asDegrees(),
69 frame='icrs', unit=u.deg))
70 ident.append((i+1)*u.dimensionless_unscaled)
71 size.append(random.randint(13, 26)*u.dimensionless_unscaled)
72 data['bad'] = QTable([ident, pt, size], names=['id', 'position', 'size'])
74 ident = []
75 pt = []
76 size = []
77 for i in range(nedge):
78 x_or_y = random.randint(0, 1)
79 if x_or_y:
80 x = random.random()*dx
81 y = [0, dy][random.randint(0, 1)]
82 else:
83 x = [0, dx][random.randint(0, 1)]
84 y = random.random()*dy
86 sphpt = wcs.pixelToSky(x, y)
87 pt.append(SkyCoord(sphpt.getRa().asDegrees(), sphpt.getDec().asDegrees(),
88 frame='icrs', unit=u.deg))
89 ident.append((i+1)*u.dimensionless_unscaled)
90 size.append(random.randint(13, 26)*u.dimensionless_unscaled)
91 data['edge'] = QTable([ident, pt, size], names=['id', 'position', 'size'])
92 return data
95class CalexpCutoutTestCase(lsst.utils.tests.TestCase):
96 def setUp(self):
97 self.exp = ExposureF.readFits(os.path.join(datadir, 'v695833-e0-c000-a00.sci.fits'))
98 self.data = make_data(self.exp.getBBox(), self.exp.getWcs())
100 def tearDown(self):
101 del self.data
102 del self.exp
104 def testCalexpCutout(self):
105 config = CalexpCutoutTask.ConfigClass()
106 task = CalexpCutoutTask(config=config)
107 result = task.run(self.data['good'], self.exp)
108 self.assertEqual(len(result.cutouts), len(self.data['good']))
110 # Test configuration of the max number of cutouts
111 config.max_cutouts = 4
112 task = CalexpCutoutTask(config=config)
113 result = task.run(self.data['good'], self.exp)
114 self.assertEqual(len(result.cutouts), task.config.max_cutouts)
116 def testEdge(self):
117 # Currently edge cutouts are handled the same way
118 # as cutouts completely outside the image. That
119 # could change in the future
120 config = CalexpCutoutTask.ConfigClass()
121 task = CalexpCutoutTask(config=config)
122 result = task.run(self.data['edge'], self.exp)
123 # Cutouts on the edge should be skipped
124 self.assertEqual(len(result.cutouts), 0)
125 self.assertEqual(len(result.skipped_positions), len(self.data['edge']))
127 config.skip_bad = False
128 task = CalexpCutoutTask(config=config)
129 # Should now raise for cutouts on the edge
130 with self.assertRaises(ValueError):
131 result = task.run(self.data['edge'], self.exp)
133 def testBad(self):
134 config = CalexpCutoutTask.ConfigClass()
135 task = CalexpCutoutTask(config=config)
136 result = task.run(self.data['bad'], self.exp)
137 # Cutouts outside the image should be skipped
138 self.assertEqual(len(result.cutouts), 0)
139 self.assertEqual(len(result.skipped_positions), len(self.data['bad']))
141 config.skip_bad = False
142 task = CalexpCutoutTask(config=config)
143 # Should now raise for cutouts outside the image
144 with self.assertRaises(ValueError):
145 result = task.run(self.data['bad'], self.exp)
147 def testBadColumns(self):
148 config = CalexpCutoutTask.ConfigClass()
149 task = CalexpCutoutTask(config=config)
150 table = QTable([[], []], names=['one', 'two'])
151 with self.assertRaises(ValueError):
152 result = task.run(table, self.exp) # noqa
155class MemoryTestCase(lsst.utils.tests.MemoryTestCase):
156 pass
159if __name__ == "__main__": 159 ↛ 160line 159 didn't jump to line 160, because the condition on line 159 was never true
160 lsst.utils.tests.init()
161 unittest.main()