Coverage for tests/test_tableMultiMatch.py: 24%

77 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-08-01 01:19 -0700

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.""" 

32 

33import os 

34import re 

35import unittest 

36 

37import numpy as np 

38 

39import lsst.afw.table as afwTable 

40import lsst.geom 

41import lsst.utils 

42import lsst.utils.tests 

43 

44 

45try: 

46 afwdataDir = lsst.utils.getPackageDir("afwdata") 

47except LookupError: 

48 afwdataDir = None 

49 

50 

51class TestGroupView(lsst.utils.tests.TestCase): 

52 """Test case for lsst.afw.table.multiMatch.GroupView.""" 

53 

54 def setUp(self): 

55 self.schema = afwTable.SourceTable.makeMinimalSchema() 

56 self.schema.addField("flux_instFlux", type=np.float64) 

57 self.schema.addField("flux_instFluxErr", type=np.float64) 

58 self.schema.addField("flux_flag", type="Flag") 

59 self.table = afwTable.SourceTable.make(self.schema) 

60 self.table.definePsfFlux("flux") 

61 

62 band = 2 # SDSS r 

63 

64 # Read SDSS catalogue 

65 with open(os.path.join(afwdataDir, "CFHT", "D2", "sdss.dat"), "r") as ifd: 

66 sdss = afwTable.SourceCatalog(self.table) 

67 

68 PRIMARY = 1 # values of mode 

69 

70 for line in ifd.readlines(): 

71 if re.search(r"^\s*#", line): 

72 continue 

73 

74 fields = line.split() 

75 objId = int(fields[0]) 

76 fields[1] 

77 mode = int(fields[2]) 

78 ra, dec = [float(f) for f in fields[3:5]] 

79 psfMags = [float(f) for f in fields[5:]] 

80 

81 if mode == PRIMARY: 

82 s = sdss.addNew() 

83 

84 s.setId(objId) 

85 s.setRa(ra * lsst.geom.degrees) 

86 s.setDec(dec * lsst.geom.degrees) 

87 s.set(self.table.getPsfFluxSlot().getMeasKey(), psfMags[band]) 

88 

89 # Read catalalogue built from the template image 

90 # Read SDSS catalogue 

91 with open(os.path.join(afwdataDir, "CFHT", "D2", "template.dat"), "r") as ifd: 

92 template = afwTable.SourceCatalog(self.table) 

93 

94 for line in ifd.readlines(): 

95 if re.search(r"^\s*#", line): 

96 continue 

97 

98 fields = line.split() 

99 id_, flags = [int(f) for f in fields[0:2]] 

100 ra, dec = [float(f) for f in fields[2:4]] 

101 flux = [float(f) for f in fields[4:]] 

102 

103 if flags & 0x1: # EDGE 

104 continue 

105 

106 s = template.addNew() 

107 s.setId(id_) 

108 s.set(afwTable.SourceTable.getCoordKey().getRa(), 

109 ra * lsst.geom.degrees) 

110 s.set(afwTable.SourceTable.getCoordKey().getDec(), 

111 dec * lsst.geom.degrees) 

112 s.set(self.table.getPsfFluxSlot().getMeasKey(), flux[0]) 

113 

114 m = afwTable.MultiMatch(self.schema, 

115 dict(visit=np.int64), 

116 RecordClass=afwTable.SimpleRecord) 

117 m.add(sdss, {'visit': 1}) 

118 m.add(template, {'visit': 2}) 

119 

120 self.matchedCatalog = m.finish() 

121 

122 def tearDown(self): 

123 del self.table 

124 del self.schema 

125 del self.matchedCatalog 

126 

127 @unittest.skipIf(afwdataDir is None, "afwdata not setup") 

128 def testGroupViewBuild(self): 

129 """Simple test of building a GroupView from a MultiMatch. See DM-8557. 

130 

131 Table creation is copied from testSourceMatch.py's 

132 SourceMatchTestCase.testPhotometricCalib(). 

133 """ 

134 allMatches = afwTable.GroupView.build(self.matchedCatalog) 

135 self.assertTrue(len(allMatches) > 0) 

136 testIds = [el for el in allMatches] 

137 self.assertEqual(testIds, list(allMatches.ids)) 

138 

139 

140class MemoryTester(lsst.utils.tests.MemoryTestCase): 

141 pass 

142 

143 

144def setup_module(module): 

145 lsst.utils.tests.init() 

146 

147 

148if __name__ == "__main__": 148 ↛ 149line 148 didn't jump to line 149, because the condition on line 148 was never true

149 lsst.utils.tests.init() 

150 unittest.main()