LSSTApplications  20.0.0
LSSTDataManagementBasePackage
_match.py
Go to the documentation of this file.
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/>.
21 
22 __all__ = ["SimpleMatch", "ReferenceMatch", "SourceMatch", "clone", "matchCls", "packMatches"]
23 
24 import numpy as np
25 
26 from lsst.utils.deprecated import deprecate_pybind11
27 
28 from ._base import BaseCatalog
29 from ._schema import Schema
30 from ._table import SimpleMatch, ReferenceMatch, SourceMatch, matchXy, matchRaDec
31 
32 
33 def __repr__(self): # noqa: N807
34  return "Match(%s,\n %s,\n %g)" % \
35  (repr(self.first), repr(self.second), self.distance)
36 
37 
38 def __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 ""
43 
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 ""
48 
49  def sourceStr(s):
50  return s.__class__.__name__ + ("(id %d" % s.getId()) + sourceRaDec(s) + sourceXy(s) + ")"
51 
52  return "Match(%s, %s, dist %g)" % (sourceStr(self.first), sourceStr(self.second), self.distance,)
53 
54 
55 def __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
67 
68 
69 def __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
81 
82 
83 def __len__(self): # noqa: N807
84  return 3
85 
86 
87 def clone(self):
88  return self.__class__(self.first, self.second, self.distance)
89 
90 
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)
98 
99 
100 for 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__
109 
110 
111 def packMatches(matches):
112  """Make a catalog of matches from a sequence of matches.
113 
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
118 
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`.
126 
127  Returns
128  -------
129  result :
130  The catalog of matches.
131 
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
154 
155 
157  matchXy,
158  reason="Overloads that don't use `MatchControl` are deprecated. To be removed after 20.0.0.")
159 matchRaDec = deprecate_pybind11(
160  matchRaDec,
161  reason="Overloads that don't use `MatchControl` are deprecated. To be removed after 20.0.0.")
lsst::afw::table._match.packMatches
def packMatches(matches)
Definition: _match.py:111
lsst::utils.deprecated
Definition: deprecated.py:1
lsst::afw::table::BaseCatalog
CatalogT< BaseRecord > BaseCatalog
Definition: fwd.h:71
lsst::afw::table._schema.Schema
Definition: _schema.py:91
lsst::afw::table._match.clone
clone
Definition: _match.py:106
lsst::utils.deprecated.deprecate_pybind11
def deprecate_pybind11(obj, reason, category=FutureWarning)
Definition: deprecated.py:32