Coverage for tests/test_tableMultiMatch.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# LSST Data Management System
2# Copyright 2016 LSST Corporation.
3#
4# This product includes software developed by the
5# LSST Project (http://www.lsst.org/).
6#
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the LSST License Statement and
18# the GNU General Public License along with this program. If not,
19# see <http://www.lsstcorp.org/LegalNotices/>.
20#
21# The classes in this test are a little non-standard to reduce code
22# duplication and support automated unittest discovery.
23# A base class includes all the code that implements the testing and
24# itself inherits from unittest.TestCase. unittest automated discovery
25# will scan all classes that inherit from unittest.TestCase and invoke
26# any test methods found. To prevent this base class from being executed
27# the test methods are placed in a different class that does not inherit
28# from unittest.TestCase. The actual test classes then inherit from
29# both the testing class and the implementation class allowing test
30# discovery to only run tests found in the subclasses.
31"""Tests for lsst.afw.table.multiMatch."""
33import os
34import re
35import unittest
37import numpy as np
39import lsst.afw.table as afwTable
40import lsst.geom
41import lsst.pex.exceptions as pexExcept
42import lsst.utils
43import lsst.utils.tests
46try:
47 afwdataDir = lsst.utils.getPackageDir("afwdata")
48except pexExcept.NotFoundError:
49 afwdataDir = None
52class TestGroupView(lsst.utils.tests.TestCase):
53 """Test case for lsst.afw.table.multiMatch.GroupView."""
55 def setUp(self):
56 self.schema = afwTable.SourceTable.makeMinimalSchema()
57 self.schema.addField("flux_instFlux", type=np.float64)
58 self.schema.addField("flux_instFluxErr", type=np.float64)
59 self.schema.addField("flux_flag", type="Flag")
60 self.table = afwTable.SourceTable.make(self.schema)
61 self.table.definePsfFlux("flux")
63 band = 2 # SDSS r
65 # Read SDSS catalogue
66 with open(os.path.join(afwdataDir, "CFHT", "D2", "sdss.dat"), "r") as ifd:
67 sdss = afwTable.SourceCatalog(self.table)
69 PRIMARY = 1 # values of mode
71 for line in ifd.readlines():
72 if re.search(r"^\s*#", line):
73 continue
75 fields = line.split()
76 objId = int(fields[0])
77 fields[1]
78 mode = int(fields[2])
79 ra, dec = [float(f) for f in fields[3:5]]
80 psfMags = [float(f) for f in fields[5:]]
82 if mode == PRIMARY:
83 s = sdss.addNew()
85 s.setId(objId)
86 s.setRa(ra * lsst.geom.degrees)
87 s.setDec(dec * lsst.geom.degrees)
88 s.set(self.table.getPsfFluxSlot().getMeasKey(), psfMags[band])
90 # Read catalalogue built from the template image
91 # Read SDSS catalogue
92 with open(os.path.join(afwdataDir, "CFHT", "D2", "template.dat"), "r") as ifd:
93 template = afwTable.SourceCatalog(self.table)
95 for line in ifd.readlines():
96 if re.search(r"^\s*#", line):
97 continue
99 fields = line.split()
100 id_, flags = [int(f) for f in fields[0:2]]
101 ra, dec = [float(f) for f in fields[2:4]]
102 flux = [float(f) for f in fields[4:]]
104 if flags & 0x1: # EDGE
105 continue
107 s = template.addNew()
108 s.setId(id_)
109 s.set(afwTable.SourceTable.getCoordKey().getRa(),
110 ra * lsst.geom.degrees)
111 s.set(afwTable.SourceTable.getCoordKey().getDec(),
112 dec * lsst.geom.degrees)
113 s.set(self.table.getPsfFluxSlot().getMeasKey(), flux[0])
115 m = afwTable.MultiMatch(self.schema,
116 dict(visit=np.int64),
117 RecordClass=afwTable.SimpleRecord)
118 m.add(sdss, {'visit': 1})
119 m.add(template, {'visit': 2})
121 self.matchedCatalog = m.finish()
123 def tearDown(self):
124 del self.table
125 del self.schema
126 del self.matchedCatalog
128 @unittest.skipIf(afwdataDir is None, "afwdata not setup")
129 def testGroupViewBuild(self):
130 """Simple test of building a GroupView from a MultiMatch. See DM-8557.
132 Table creation is copied from testSourceMatch.py's
133 SourceMatchTestCase.testPhotometricCalib().
134 """
135 allMatches = afwTable.GroupView.build(self.matchedCatalog)
136 self.assertTrue(len(allMatches) > 0)
137 testIds = [el for el in allMatches]
138 self.assertEqual(testIds, list(allMatches.ids))
141class MemoryTester(lsst.utils.tests.MemoryTestCase):
142 pass
145def setup_module(module):
146 lsst.utils.tests.init()
149if __name__ == "__main__": 149 ↛ 150line 149 didn't jump to line 150, because the condition on line 149 was never true
150 lsst.utils.tests.init()
151 unittest.main()