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
34 from .schema
import Schema
35 from .schemaMapper
import SchemaMapper
36 from .base
import BaseCatalog
37 from .simple
import SimpleCatalog, SimpleTable
38 from .source
import SourceCatalog, SourceTable
39 from .match
import ReferenceMatch
44 def makeMapper(sourceSchema, targetSchema, sourcePrefix=None, targetPrefix=None):
45 """Create a SchemaMapper between the input source and target schemas 47 \param[in] sourceSchema input source schema that fields will be mapped from 48 \param[in] targetSchema target schema that fields will be mapped to 49 \param[in] sourcePrefix if set, only those keys with that prefix will be mapped 50 \param[in] targetPrefix if set, prepend it to the mapped (target) key name 52 \return SchemaMapper between source and target schemas 55 for key, field
in sourceSchema:
56 keyName = field.getName()
57 if sourcePrefix
is not None:
58 if not keyName.startswith(sourcePrefix):
61 keyName = field.getName().replace(sourcePrefix,
"", 1)
62 m.addMapping(key, (targetPrefix
or "") + keyName)
66 def makeMergedSchema(sourceSchema, targetSchema, sourcePrefix=None, targetPrefix=None):
67 """Return a schema that is a deep copy of a mapping between source and target schemas 68 \param[in] sourceSchema input source schema that fields will be mapped from 69 \param[in] targetSchema target schema that fields will be mapped to 70 \param[in] sourcePrefix if set, only those keys with that prefix will be mapped 71 \param[in] targetPrefix if set, prepend it to the mapped (target) key name 73 \return schema schema that is the result of the mapping between source and target schemas 75 return makeMapper(sourceSchema, targetSchema, sourcePrefix, targetPrefix).getOutputSchema()
78 def copyIntoCatalog(catalog, target, sourceSchema=None, sourcePrefix=None, targetPrefix=None):
79 """Copy entries from one Catalog into another 81 \param[in] catalog source catalog to be copied from 82 \param[in/out] target target catalog to be copied to (edited in place) 83 \param[in] souceSchema schema of source catalog (optional) 84 \param[in] sourcePrefix if set, only those keys with that prefix will be copied 85 \param[in] targetPrefix if set, prepend it to the copied (target) key name 87 if sourceSchema
is None:
88 sourceSchema = catalog.schema
90 targetSchema = target.schema
91 target.reserve(len(catalog))
92 for i
in range(len(target), len(catalog)):
95 if len(catalog) != len(target):
96 raise RuntimeError(
"Length mismatch: %d vs %d" %
97 (len(catalog), len(target)))
99 m =
makeMapper(sourceSchema, targetSchema, sourcePrefix, targetPrefix)
100 for rFrom, rTo
in zip(catalog, target):
105 """Denormalise matches into a Catalog of "unpacked matches" 107 \param[in] matches unpacked matches, i.e. a list of Match objects whose schema 108 has "first" and "second" attributes which, resepectively, contain the 109 reference and source catalog entries, and a "distance" field (the 110 measured distance between the reference and source objects) 111 \param[in] matchMeta metadata for matches (must have .add attribute) 113 \return lsst.afw.table.BaseCatalog of matches (with ref_ and src_ prefix identifiers 114 for referece and source entries, respectively) 116 if len(matches) == 0:
117 raise RuntimeError(
"No matches provided.")
119 refSchema = matches[0].first.getSchema()
120 srcSchema = matches[0].second.getSchema()
124 srcSchema, mergedSchema, targetPrefix=
"src_")
125 distKey = mergedSchema.addField(
126 "distance", type=np.float64, doc=
"Distance between ref and src")
130 sourceSchema=refSchema, targetPrefix=
"ref_")
132 sourceSchema=srcSchema, targetPrefix=
"src_")
133 for m, r
in zip(matches, mergedCatalog):
134 r.set(distKey, m.distance)
138 catalogName = os.path.basename(getPackageDir(
"astrometry_net_data"))
139 except pexExcept.NotFoundError:
140 catalogName =
"NOT_SET" 141 matchMeta.add(
"REFCAT", catalogName)
142 mergedCatalog.getTable().setMetadata(matchMeta)
148 """Generate a list of ReferenceMatches from a Catalog of "unpacked matches" 150 \param[in] catalog catalog of matches. Must have schema where reference entries are 151 prefixed with "ref_" and source entries are prefixed with "src_" 152 \param[in] sourceSlotConfig an lsst.meas.base.baseMeasurement.SourceSlotConfig configuration 153 for source slots (optional) 155 \returns lsst.afw.table.ReferenceMatch of matches 158 catalog.schema, SimpleTable.makeMinimalSchema(), sourcePrefix=
"ref_")
163 catalog.schema, SourceTable.makeMinimalSchema(), sourcePrefix=
"src_")
167 if sourceSlotConfig
is not None:
168 sourceSlotConfig.setupSchema(srcCatalog.schema)
171 distKey = catalog.schema.find(
"distance").key
172 for ref, src, cat
in zip(refCatalog, srcCatalog, catalog):
CatalogT< BaseRecord > BaseCatalog
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
SortedCatalogT< SimpleRecord > SimpleCatalog