lsst.afw  15.0-25-gacd707fca
catalogMatches.py
Go to the documentation of this file.
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 
23 __all__ = ["makeMergedSchema", "copyIntoCatalog",
24  "matchesToCatalog", "matchesFromCatalog"]
25 
26 import os.path
27 
28 import numpy as np
29 
30 import lsst.pex.exceptions as pexExcept
31 from .schema import Schema
32 from .schemaMapper import SchemaMapper
33 from .base import BaseCatalog
34 from .simple import SimpleCatalog, SimpleTable
35 from .source import SourceCatalog, SourceTable
36 from .match import ReferenceMatch
37 
38 from lsst.utils import getPackageDir
39 
40 
41 def makeMapper(sourceSchema, targetSchema, sourcePrefix=None, targetPrefix=None):
42  """Create a SchemaMapper between the input source and target schemas
43 
44  @param[in] sourceSchema input source schema that fields will be mapped from
45  @param[in] targetSchema target schema that fields will be mapped to
46  @param[in] sourcePrefix if set, only those keys with that prefix will be mapped
47  @param[in] targetPrefix if set, prepend it to the mapped (target) key name
48 
49  @return SchemaMapper between source and target schemas
50  """
51  m = SchemaMapper(sourceSchema, targetSchema)
52  for key, field in sourceSchema:
53  keyName = field.getName()
54  if sourcePrefix is not None:
55  if not keyName.startswith(sourcePrefix):
56  continue
57  else:
58  keyName = field.getName().replace(sourcePrefix, "", 1)
59  m.addMapping(key, (targetPrefix or "") + keyName)
60  return m
61 
62 
63 def makeMergedSchema(sourceSchema, targetSchema, sourcePrefix=None, targetPrefix=None):
64  """Return a schema that is a deep copy of a mapping between source and target schemas
65  @param[in] sourceSchema input source schema that fields will be mapped from
66  @param[in] targetSchema target schema that fields will be mapped to
67  @param[in] sourcePrefix if set, only those keys with that prefix will be mapped
68  @param[in] targetPrefix if set, prepend it to the mapped (target) key name
69 
70  @return schema schema that is the result of the mapping between source and target schemas
71  """
72  return makeMapper(sourceSchema, targetSchema, sourcePrefix, targetPrefix).getOutputSchema()
73 
74 
75 def copyIntoCatalog(catalog, target, sourceSchema=None, sourcePrefix=None, targetPrefix=None):
76  """Copy entries from one Catalog into another
77 
78  @param[in] catalog source catalog to be copied from
79  @param[in/out] target target catalog to be copied to (edited in place)
80  @param[in] souceSchema schema of source catalog (optional)
81  @param[in] sourcePrefix if set, only those keys with that prefix will be copied
82  @param[in] targetPrefix if set, prepend it to the copied (target) key name
83  """
84  if sourceSchema is None:
85  sourceSchema = catalog.schema
86 
87  targetSchema = target.schema
88  target.reserve(len(catalog))
89  for i in range(len(target), len(catalog)):
90  target.addNew()
91 
92  if len(catalog) != len(target):
93  raise RuntimeError("Length mismatch: %d vs %d" %
94  (len(catalog), len(target)))
95 
96  m = makeMapper(sourceSchema, targetSchema, sourcePrefix, targetPrefix)
97  for rFrom, rTo in zip(catalog, target):
98  rTo.assign(rFrom, m)
99 
100 
101 def matchesToCatalog(matches, matchMeta):
102  """Denormalise matches into a Catalog of "unpacked matches"
103 
104  @param[in] matches unpacked matches, i.e. a list of Match objects whose schema
105  has "first" and "second" attributes which, resepectively, contain the
106  reference and source catalog entries, and a "distance" field (the
107  measured distance between the reference and source objects)
108  @param[in] matchMeta metadata for matches (must have .add attribute)
109 
110  @return lsst.afw.table.BaseCatalog of matches (with ref_ and src_ prefix identifiers
111  for referece and source entries, respectively)
112  """
113  if len(matches) == 0:
114  raise RuntimeError("No matches provided.")
115 
116  refSchema = matches[0].first.getSchema()
117  srcSchema = matches[0].second.getSchema()
118 
119  mergedSchema = makeMergedSchema(refSchema, Schema(), targetPrefix="ref_")
120  mergedSchema = makeMergedSchema(
121  srcSchema, mergedSchema, targetPrefix="src_")
122  distKey = mergedSchema.addField(
123  "distance", type=np.float64, doc="Distance between ref and src")
124 
125  mergedCatalog = BaseCatalog(mergedSchema)
126  copyIntoCatalog([m.first for m in matches], mergedCatalog,
127  sourceSchema=refSchema, targetPrefix="ref_")
128  copyIntoCatalog([m.second for m in matches], mergedCatalog,
129  sourceSchema=srcSchema, targetPrefix="src_")
130  for m, r in zip(matches, mergedCatalog):
131  r.set(distKey, m.distance)
132 
133  # obtain reference catalog name if one is setup
134  try:
135  catalogName = os.path.basename(getPackageDir("astrometry_net_data"))
137  catalogName = "NOT_SET"
138  matchMeta.add("REFCAT", catalogName)
139  mergedCatalog.getTable().setMetadata(matchMeta)
140 
141  return mergedCatalog
142 
143 
144 def matchesFromCatalog(catalog, sourceSlotConfig=None):
145  """Generate a list of ReferenceMatches from a Catalog of "unpacked matches"
146 
147  @param[in] catalog catalog of matches. Must have schema where reference entries are
148  prefixed with "ref_" and source entries are prefixed with "src_"
149  @param[in] sourceSlotConfig an lsst.meas.base.baseMeasurement.SourceSlotConfig configuration
150  for source slots (optional)
151 
152  @returns lsst.afw.table.ReferenceMatch of matches
153  """
154  refSchema = makeMergedSchema(
155  catalog.schema, SimpleTable.makeMinimalSchema(), sourcePrefix="ref_")
156  refCatalog = SimpleCatalog(refSchema)
157  copyIntoCatalog(catalog, refCatalog, sourcePrefix="ref_")
158 
159  srcSchema = makeMergedSchema(
160  catalog.schema, SourceTable.makeMinimalSchema(), sourcePrefix="src_")
161  srcCatalog = SourceCatalog(srcSchema)
162  copyIntoCatalog(catalog, srcCatalog, sourcePrefix="src_")
163 
164  if sourceSlotConfig is not None:
165  sourceSlotConfig.setupSchema(srcCatalog.schema)
166 
167  matches = []
168  distKey = catalog.schema.find("distance").key
169  for ref, src, cat in zip(refCatalog, srcCatalog, catalog):
170  matches.append(ReferenceMatch(ref, src, cat[distKey]))
171 
172  return matches
CatalogT< BaseRecord > BaseCatalog
Definition: fwd.h:71
def matchesFromCatalog(catalog, sourceSlotConfig=None)
def makeMergedSchema(sourceSchema, targetSchema, sourcePrefix=None, targetPrefix=None)
def matchesToCatalog(matches, matchMeta)
def makeMapper(sourceSchema, targetSchema, sourcePrefix=None, targetPrefix=None)
def copyIntoCatalog(catalog, target, sourceSchema=None, sourcePrefix=None, targetPrefix=None)
Match< SimpleRecord, SourceRecord > ReferenceMatch
Definition: fwd.h:104
SortedCatalogT< SimpleRecord > SimpleCatalog
Definition: fwd.h:79