22 from __future__
import absolute_import, division, print_function
24 __all__ = [
"makeMergedSchema",
"copyIntoCatalog",
25 "matchesToCatalog",
"matchesFromCatalog"]
29 from builtins
import zip
30 from builtins
import range
33 from .schema
import Schema
34 from .schemaMapper
import SchemaMapper
35 from .base
import BaseCatalog
36 from .simple
import SimpleCatalog, SimpleTable
37 from .source
import SourceCatalog, SourceTable
38 from .match
import ReferenceMatch
40 from lsst.utils
import getPackageDir
43 def makeMapper(sourceSchema, targetSchema, sourcePrefix=None, targetPrefix=None):
44 """Create a SchemaMapper between the input source and target schemas
46 \param[in] sourceSchema input source schema that fields will be mapped from
47 \param[in] targetSchema target schema that fields will be mapped to
48 \param[in] sourcePrefix if set, only those keys with that prefix will be mapped
49 \param[in] targetPrefix if set, prepend it to the mapped (target) key name
51 \return SchemaMapper between source and target schemas
53 m = SchemaMapper(sourceSchema, targetSchema)
54 for key, field
in sourceSchema:
55 keyName = field.getName()
56 if sourcePrefix
is not None:
57 if not keyName.startswith(sourcePrefix):
60 keyName = field.getName().replace(sourcePrefix,
"", 1)
61 m.addMapping(key, (targetPrefix
or "") + keyName)
65 def makeMergedSchema(sourceSchema, targetSchema, sourcePrefix=None, targetPrefix=None):
66 """Return a schema that is a deep copy of a mapping between source and target schemas
67 \param[in] sourceSchema input source schema that fields will be mapped from
68 \param[in] targetSchema target schema that fields will be mapped to
69 \param[in] sourcePrefix if set, only those keys with that prefix will be mapped
70 \param[in] targetPrefix if set, prepend it to the mapped (target) key name
72 \return schema schema that is the result of the mapping between source and target schemas
74 return makeMapper(sourceSchema, targetSchema, sourcePrefix, targetPrefix).getOutputSchema()
77 def copyIntoCatalog(catalog, target, sourceSchema=None, sourcePrefix=None, targetPrefix=None):
78 """Copy entries from one Catalog into another
80 \param[in] catalog source catalog to be copied from
81 \param[in/out] target target catalog to be copied to (edited in place)
82 \param[in] souceSchema schema of source catalog (optional)
83 \param[in] sourcePrefix if set, only those keys with that prefix will be copied
84 \param[in] targetPrefix if set, prepend it to the copied (target) key name
86 if sourceSchema
is None:
87 sourceSchema = catalog.schema
89 targetSchema = target.schema
90 target.reserve(len(catalog))
91 for i
in range(len(target), len(catalog)):
94 if len(catalog) != len(target):
95 raise RuntimeError(
"Length mismatch: %d vs %d" %
96 (len(catalog), len(target)))
98 m =
makeMapper(sourceSchema, targetSchema, sourcePrefix, targetPrefix)
99 for rFrom, rTo
in zip(catalog, target):
104 """Denormalise matches into a Catalog of "unpacked matches"
106 \param[in] matches unpacked matches, i.e. a list of Match objects whose schema
107 has "first" and "second" attributes which, resepectively, contain the
108 reference and source catalog entries, and a "distance" field (the
109 measured distance between the reference and source objects)
110 \param[in] matchMeta metadata for matches (must have .add attribute)
112 \return lsst.afw.table.BaseCatalog of matches (with ref_ and src_ prefix identifiers
113 for referece and source entries, respectively)
115 if len(matches) == 0:
116 raise RuntimeError(
"No matches provided.")
118 refSchema = matches[0].first.getSchema()
119 srcSchema = matches[0].second.getSchema()
123 srcSchema, mergedSchema, targetPrefix=
"src_")
124 distKey = mergedSchema.addField(
125 "distance", type=np.float64, doc=
"Distance between ref and src")
129 sourceSchema=refSchema, targetPrefix=
"ref_")
131 sourceSchema=srcSchema, targetPrefix=
"src_")
132 for m, r
in zip(matches, mergedCatalog):
133 r.set(distKey, m.distance)
137 catalogName = os.path.basename(getPackageDir(
"astrometry_net_data"))
139 catalogName =
"NOT_SET"
140 matchMeta.add(
"REFCAT", catalogName)
141 mergedCatalog.getTable().setMetadata(matchMeta)
147 """Generate a list of ReferenceMatches from a Catalog of "unpacked matches"
149 \param[in] catalog catalog of matches. Must have schema where reference entries are
150 prefixed with "ref_" and source entries are prefixed with "src_"
151 \param[in] sourceSlotConfig an lsst.meas.base.baseMeasurement.SourceSlotConfig configuration
152 for source slots (optional)
154 \returns lsst.afw.table.ReferenceMatch of matches
157 catalog.schema, SimpleTable.makeMinimalSchema(), sourcePrefix=
"ref_")
162 catalog.schema, SourceTable.makeMinimalSchema(), sourcePrefix=
"src_")
166 if sourceSlotConfig
is not None:
167 sourceSlotConfig.setupSchema(srcCatalog.schema)
170 distKey = catalog.schema.find(
"distance").key
171 for ref, src, cat
in zip(refCatalog, srcCatalog, catalog):
CatalogT< BaseRecord > BaseCatalog
Match< SimpleRecord, SourceRecord > ReferenceMatch
SortedCatalogT< SourceRecord > SourceCatalog
SortedCatalogT< SimpleRecord > SimpleCatalog