22 __all__ = [
"makeMergedSchema",
"copyIntoCatalog",
23 "matchesToCatalog",
"matchesFromCatalog",
"copyAliasMapWithPrefix"]
30 from .schema
import Schema
31 from .schemaMapper
import SchemaMapper
32 from .base
import BaseCatalog
33 from .simple
import SimpleCatalog, SimpleTable
34 from .source
import SourceCatalog, SourceTable
35 from .match
import ReferenceMatch
40 def makeMapper(sourceSchema, targetSchema, sourcePrefix=None, targetPrefix=None):
41 """Create a SchemaMapper between the input source and target schemas 43 @param[in] sourceSchema input source schema that fields will be mapped from 44 @param[in] targetSchema target schema that fields will be mapped to 45 @param[in] sourcePrefix if set, only those keys with that prefix will be mapped 46 @param[in] targetPrefix if set, prepend it to the mapped (target) key name 48 @return SchemaMapper between source and target schemas 51 for key, field
in sourceSchema:
52 keyName = field.getName()
53 if sourcePrefix
is not None:
54 if not keyName.startswith(sourcePrefix):
57 keyName = field.getName().replace(sourcePrefix,
"", 1)
58 m.addMapping(key, (targetPrefix
or "") + keyName)
62 def makeMergedSchema(sourceSchema, targetSchema, sourcePrefix=None, targetPrefix=None):
63 """Return a schema that is a deep copy of a mapping between source and target schemas 64 @param[in] sourceSchema input source schema that fields will be mapped from 65 @param[in] targetSchema target schema that fields will be mapped to 66 @param[in] sourcePrefix if set, only those keys with that prefix will be mapped 67 @param[in] targetPrefix if set, prepend it to the mapped (target) key name 69 @return schema schema that is the result of the mapping between source and target schemas 71 return makeMapper(sourceSchema, targetSchema, sourcePrefix, targetPrefix).getOutputSchema()
74 def copyIntoCatalog(catalog, target, sourceSchema=None, sourcePrefix=None, targetPrefix=None):
75 """Copy entries from one Catalog into another 77 @param[in] catalog source catalog to be copied from 78 @param[in/out] target target catalog to be copied to (edited in place) 79 @param[in] souceSchema schema of source catalog (optional) 80 @param[in] sourcePrefix if set, only those keys with that prefix will be copied 81 @param[in] targetPrefix if set, prepend it to the copied (target) key name 83 if sourceSchema
is None:
84 sourceSchema = catalog.schema
86 targetSchema = target.schema
87 target.reserve(len(catalog))
88 for i
in range(len(target), len(catalog)):
91 if len(catalog) != len(target):
92 raise RuntimeError(
"Length mismatch: %d vs %d" %
93 (len(catalog), len(target)))
95 m =
makeMapper(sourceSchema, targetSchema, sourcePrefix, targetPrefix)
96 for rFrom, rTo
in zip(catalog, target):
101 """Denormalise matches into a Catalog of "unpacked matches" 103 @param[in] matches unpacked matches, i.e. a list of Match objects whose schema 104 has "first" and "second" attributes which, resepectively, contain the 105 reference and source catalog entries, and a "distance" field (the 106 measured distance between the reference and source objects) 107 @param[in] matchMeta metadata for matches (must have .add attribute) 109 @return lsst.afw.table.BaseCatalog of matches (with ref_ and src_ prefix identifiers 110 for referece and source entries, respectively, including alias maps 111 from reference and source catalogs) 113 if len(matches) == 0:
114 raise RuntimeError(
"No matches provided.")
116 refSchema = matches[0].first.getSchema()
117 srcSchema = matches[0].second.getSchema()
121 srcSchema, mergedSchema, targetPrefix=
"src_")
126 distKey = mergedSchema.addField(
127 "distance", type=np.float64, doc=
"Distance between ref and src")
131 sourceSchema=refSchema, targetPrefix=
"ref_")
133 sourceSchema=srcSchema, targetPrefix=
"src_")
134 for m, r
in zip(matches, mergedCatalog):
135 r.set(distKey, m.distance)
139 catalogName = os.path.basename(
getPackageDir(
"astrometry_net_data"))
141 catalogName =
"NOT_SET" 142 matchMeta.add(
"REFCAT", catalogName)
143 mergedCatalog.getTable().setMetadata(matchMeta)
149 """Generate a list of ReferenceMatches from a Catalog of "unpacked matches" 151 @param[in] catalog catalog of matches. Must have schema where reference entries are 152 prefixed with "ref_" and source entries are prefixed with "src_" 153 @param[in] sourceSlotConfig an lsst.meas.base.baseMeasurement.SourceSlotConfig configuration 154 for source slots (optional) 156 @returns lsst.afw.table.ReferenceMatch of matches 159 catalog.schema, SimpleTable.makeMinimalSchema(), sourcePrefix=
"ref_")
164 catalog.schema, SourceTable.makeMinimalSchema(), sourcePrefix=
"src_")
168 if sourceSlotConfig
is not None:
169 sourceSlotConfig.setupSchema(srcCatalog.schema)
172 distKey = catalog.schema.find(
"distance").key
173 for ref, src, cat
in zip(refCatalog, srcCatalog, catalog):
180 """Copy an alias map from one schema into another 182 This copies the alias map of one schema into another, optionally 183 prepending a prefix to both the "from" and "to" names of the alias 184 (the example use case here is for the "match" catalog created by 185 `lsst.meas.astrom.denormalizeMatches` where prefixes "src_" and 186 "ref_" are added to the source and reference field entries, 191 inSchema : `lsst.afw.table.Schema` 192 The input schema whose `lsst.afw.table.AliasMap` is to be 193 copied to ``outSchema``. 194 outSchema : `lsst.afw.table.Schema` 195 The output schema into which the `lsst.afw.table.AliasMap` 196 from ``inSchema`` is to be copied (modified in place). 197 prefix : `str`, optional 198 An optional prefix to add to both the "from" and "to" names 199 of the alias (default is an empty string). 203 outSchema : `lsst.afw.table.Schema` 204 The output schema with the alias mappings from `inSchema` 207 for k, v
in inSchema.getAliasMap().
items():
208 outSchema.getAliasMap().
set(prefix + k, prefix + v)
CatalogT< BaseRecord > BaseCatalog
def matchesFromCatalog(catalog, sourceSlotConfig=None)
def makeMergedSchema(sourceSchema, targetSchema, sourcePrefix=None, targetPrefix=None)
def matchesToCatalog(matches, matchMeta)
daf::base::PropertySet * set
std::string getPackageDir(std::string const &packageName)
return the root directory of a setup package
def makeMapper(sourceSchema, targetSchema, sourcePrefix=None, targetPrefix=None)
Reports attempts to access elements using an invalid key.
def copyIntoCatalog(catalog, target, sourceSchema=None, sourcePrefix=None, targetPrefix=None)
Match< SimpleRecord, SourceRecord > ReferenceMatch
def copyAliasMapWithPrefix(inSchema, outSchema, prefix="")
std::vector< SchemaItem< Flag > > * items
SortedCatalogT< SimpleRecord > SimpleCatalog