Coverage for python/lsst/afw/table/_match.py : 27%

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# This file is part of afw.
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__all__ = ["SimpleMatch", "ReferenceMatch", "SourceMatch", "clone", "matchCls", "packMatches"]
24import numpy as np
26from lsst.utils.deprecated import deprecate_pybind11
28from ._base import BaseCatalog
29from ._schema import Schema
30from ._table import SimpleMatch, ReferenceMatch, SourceMatch, matchXy, matchRaDec
33def __repr__(self): # noqa: N807
34 return "Match(%s,\n %s,\n %g)" % \
35 (repr(self.first), repr(self.second), self.distance)
38def __str__(self): # noqa: N807
39 def sourceRaDec(s):
40 if hasattr(s, "getRa") and hasattr(s, "getDec"):
41 return " RA,Dec=(%g,%g) deg" % (s.getRa().asDegrees(), s.getDec().asDegrees())
42 return ""
44 def sourceXy(s):
45 if hasattr(s, "getX") and hasattr(s, "getY"):
46 return " x,y=(%g,%g)" % (s.getX(), s.getY())
47 return ""
49 def sourceStr(s):
50 return s.__class__.__name__ + ("(id %d" % s.getId()) + sourceRaDec(s) + sourceXy(s) + ")"
52 return "Match(%s, %s, dist %g)" % (sourceStr(self.first), sourceStr(self.second), self.distance,)
55def __getitem__(self, i): # noqa: N807
56 """Treat a Match as a tuple of length 3: (first, second, distance)"""
57 if i > 2 or i < -3:
58 raise IndexError(i)
59 if i < 0:
60 i += 3
61 if i == 0:
62 return self.first
63 elif i == 1:
64 return self.second
65 else:
66 return self.distance
69def __setitem__(self, i, val): # noqa: N807
70 """Treat a Match as a tuple of length 3: (first, second, distance)"""
71 if i > 2 or i < -3:
72 raise IndexError(i)
73 if i < 0:
74 i += 3
75 if i == 0:
76 self.first = val
77 elif i == 1:
78 self.second = val
79 else:
80 self.distance = val
83def __len__(self): # noqa: N807
84 return 3
87def clone(self):
88 return self.__class__(self.first, self.second, self.distance)
91# Pickling support disabled for this type (see testSourceMatch comment for reasoning)
92# def __getstate__(self):
93# return self.first, self.second, self.distance
94#
95#
96# def __setstate__(self, state):
97# self.__init__(*state)
100for matchCls in (SimpleMatch, ReferenceMatch, SourceMatch):
101 matchCls.__repr__ = __repr__
102 matchCls.__str__ = __str__
103 matchCls.__getitem__ = __getitem__
104 matchCls.__setitem__ = __setitem__
105 matchCls.__len__ = __len__
106 matchCls.clone = clone
107# matchCls.__getstate__ = __getstate__
108# matchCls.__setstate__ = __setstate__
111def packMatches(matches):
112 """Make a catalog of matches from a sequence of matches.
114 The catalog contains three fields:
115 - first: the ID of the first source record in each match
116 - second: the ID of the second source record in each match
117 - distance: the distance of each match
119 Parameters
120 ----------
121 matches :
122 Sequence of matches, typically of type SimpleMatch,
123 ReferenceMatch or SourceMatch. Each element must support:
124 `.first.getId()`->int, `.second.getId()->int` and
125 `.distance->float`.
127 Returns
128 -------
129 result :
130 The catalog of matches.
132 Notes
133 -----
134 This pure python implementation exists as a historical artifact
135 related to SWIG limitations. It might be practical to wrap the
136 overloaded C++ functions with pybind11, but there didn't seem much
137 point.
138 """
139 schema = Schema()
140 outKey1 = schema.addField("first", type=np.int64,
141 doc="ID for first source record in match.")
142 outKey2 = schema.addField("second", type=np.int64,
143 doc="ID for second source record in match.")
144 keyD = schema.addField("distance", type=np.float64,
145 doc="Distance between matches sources.")
146 result = BaseCatalog(schema)
147 result.table.preallocate(len(matches))
148 for match in matches:
149 record = result.addNew()
150 record.set(outKey1, match.first.getId())
151 record.set(outKey2, match.second.getId())
152 record.set(keyD, match.distance)
153 return result
156matchXy = deprecate_pybind11(
157 matchXy,
158 reason="Overloads that don't use `MatchControl` are deprecated. To be removed after 20.0.0.")
159matchRaDec = deprecate_pybind11(
160 matchRaDec,
161 reason="Overloads that don't use `MatchControl` are deprecated. To be removed after 20.0.0.")