22 from __future__
import absolute_import
23 from builtins
import zip
24 from builtins
import range
28 from .tableLib
import (BaseCatalog, SimpleCatalog, SourceCatalog, SimpleTable, SourceTable,
29 Schema, SchemaMapper, ReferenceMatch)
32 __all__ = [
"makeMergedSchema",
"copyIntoCatalog",
"matchesToCatalog",
"matchesFromCatalog"]
34 def makeMapper(sourceSchema, targetSchema, sourcePrefix=None, targetPrefix=None):
35 """Create a SchemaMapper between the input source and target schemas
37 \param[in] sourceSchema input source schema that fields will be mapped from
38 \param[in] targetSchema target schema that fields will be mapped to
39 \param[in] sourcePrefix if set, only those keys with that prefix will be mapped
40 \param[in] targetPrefix if set, prepend it to the mapped (target) key name
42 \return SchemaMapper between source and target schemas
45 for key, field
in sourceSchema:
46 keyName = field.getName()
47 if sourcePrefix
is not None:
48 if not keyName.startswith(sourcePrefix):
51 keyName = field.getName().replace(sourcePrefix,
"", 1)
52 m.addMapping(key, (targetPrefix
or "") + keyName)
55 def makeMergedSchema(sourceSchema, targetSchema, sourcePrefix=None, targetPrefix=None):
56 """Return a schema that is a deep copy of a mapping between source and target schemas
57 \param[in] sourceSchema input source schema that fields will be mapped from
58 \param[in] targetSchema target schema that fields will be mapped to
59 \param[in] sourcePrefix if set, only those keys with that prefix will be mapped
60 \param[in] targetPrefix if set, prepend it to the mapped (target) key name
62 \return schema schema that is the result of the mapping between source and target schemas
64 return makeMapper(sourceSchema, targetSchema, sourcePrefix, targetPrefix).getOutputSchema()
66 def copyIntoCatalog(catalog, target, sourceSchema=None, sourcePrefix=None, targetPrefix=None):
67 """Copy entries from one Catalog into another
69 \param[in] catalog source catalog to be copied from
70 \param[in/out] target target catalog to be copied to (edited in place)
71 \param[in] souceSchema schema of source catalog (optional)
72 \param[in] sourcePrefix if set, only those keys with that prefix will be copied
73 \param[in] targetPrefix if set, prepend it to the copied (target) key name
75 if sourceSchema
is None:
76 sourceSchema = catalog.schema
78 targetSchema = target.schema
79 target.reserve(len(catalog))
80 for i
in range(len(target), len(catalog)):
83 if len(catalog) != len(target):
84 raise RuntimeError(
"Length mismatch: %d vs %d" % (len(catalog), len(target)))
86 m =
makeMapper(sourceSchema, targetSchema, sourcePrefix, targetPrefix)
87 for rFrom, rTo
in zip(catalog, target):
91 """Denormalise matches into a Catalog of "unpacked matches"
93 \param[in] matches unpacked matches, i.e. a std::vector of Match objects whose schema
94 has "first" and "second" attributes which, resepectively, contain the
95 reference and source catalog entries, and a "distance" field (the
96 measured distance between the reference and source objects)
97 \param[in] matchMeta metadata for matches (must have .add attribute)
99 \return lsst.afw.table.BaseCatalog of matches (with ref_ and src_ prefix identifiers
100 for referece and source entries, respectively)
102 if len(matches) == 0:
103 raise RuntimeError(
"No matches provided.")
105 refSchema = matches[0].first.getSchema()
106 srcSchema = matches[0].second.getSchema()
109 mergedSchema =
makeMergedSchema(srcSchema, mergedSchema, targetPrefix=
"src_")
110 distKey = mergedSchema.addField(
"distance", type=float, doc=
"Distance between ref and src")
113 copyIntoCatalog([m.first
for m
in matches], mergedCatalog, sourceSchema=refSchema, targetPrefix=
"ref_")
114 copyIntoCatalog([m.second
for m
in matches], mergedCatalog, sourceSchema=srcSchema, targetPrefix=
"src_")
115 for m, r
in zip(matches, mergedCatalog):
116 r.set(distKey, m.distance)
120 catalogName = os.path.basename(
getPackageDir(
"astrometry_net_data"))
122 catalogName =
"NOT_SET"
123 matchMeta.add(
"REFCAT", catalogName)
124 mergedCatalog.getTable().setMetadata(matchMeta)
129 """Generate a list of ReferenceMatches from a Catalog of "unpacked matches"
131 \param[in] catalog catalog of matches. Must have schema where reference entries are
132 prefixed with "ref_" and source entries are prefixed with "src_"
133 \param[in] sourceSlotConfig an lsst.meas.base.baseMeasurement.SourceSlotConfig configuration
134 for source slots (optional)
136 \returns lsst.afw.table.ReferenceMatch of matches
138 refSchema =
makeMergedSchema(catalog.schema, SimpleTable.makeMinimalSchema(), sourcePrefix=
"ref_")
142 srcSchema =
makeMergedSchema(catalog.schema, SourceTable.makeMinimalSchema(), sourcePrefix=
"src_")
146 if sourceSlotConfig
is not None:
147 sourceSlotConfig.setupSchema(srcCatalog.schema)
150 distKey = catalog.schema.find(
"distance").key
151 for ref, src, cat
in zip(refCatalog, srcCatalog, catalog):
Defines the fields and offsets for a table.
A custom container class for records, based on std::vector.
A mapping between the keys of two Schemas, used to copy data between them.
std::string getPackageDir(std::string const &packageName)
return the root directory of a setup package
Custom catalog class for record/table subclasses that are guaranteed to have an ID, and should generally be sorted by that ID.
Lightweight representation of a geometric match between two records.