22 __all__ = (
"Translator",
"KeyHandler",
"CopyKeyHandler",
"ConstantKeyHandler",
23 "makeCalibrationLabel")
26 from typing
import Optional, Any, Dict, Tuple, FrozenSet, Iterable, List
27 from abc
import ABCMeta, abstractmethod
33 filter: Optional[str] =
None) -> str:
34 """Make a Gen3 calibration_label string corresponding to a Gen2 data ID. 38 datasetTypeName : `str` 39 Name of the dataset type this calibration label identifies. 41 Date string used in the Gen2 template. 43 Detector ID used in the Gen2 template. 44 filter : `str`, optional 45 Filter used in the Gen2 template. 50 Calibration label string. 54 elements = [datasetTypeName, calibDate]
56 elements.append(f
"{ccd:03d}")
57 if filter
is not None:
58 elements.append(filter)
59 return "gen2/{}".
format(
"_".join(elements))
63 """Base class for Translator helpers that each handle just one Gen3 Data 69 Name of the Gen3 dimension (data ID key) populated by 70 this handler (e.g. "visit" or "abstract_filter"). 75 __slots__ = (
"dimension",)
78 skyMap: Optional[BaseSkyMap], skyMapName: Optional[str],
79 datasetTypeName: str):
80 """Update a Gen3 data ID dict with a single key-value pair from a Gen2 83 This method is implemented by the base class and is not expected to 84 be re-implemented by subclasses. 89 Gen2 data ID from which to draw key-value pairs from. 91 Gen3 data ID to update in-place. 92 skyMap: `BaseSkyMap`, optional 93 SkyMap that defines the tracts and patches used in the Gen2 data 96 Name of the Gen3 skymap dimension that defines the tracts and 97 patches used in the Gen3 data ID. 98 datasetTypeName: `str` 99 Name of the dataset type. 101 gen3id[self.
dimension] = self.
extract(gen2id, skyMap=skyMap, skyMapName=skyMapName,
102 datasetTypeName=datasetTypeName)
105 def extract(self, gen2id: dict, skyMap: Optional[BaseSkyMap], skyMapName: Optional[str],
106 datasetTypeName: str) -> Any:
107 """Extract a Gen3 data ID value from a Gen2 data ID. 112 Gen2 data ID from which to draw key-value pairs from. 113 skyMap: `BaseSkyMap`, optional 114 SkyMap that defines the tracts and patches used in the Gen2 data 117 Name of the Gen3 skymap dimension that defines the tracts and 118 patches used in the Gen3 data ID. 119 datasetTypeName: `str` 120 Name of the dataset type. 122 raise NotImplementedError()
126 """A KeyHandler that adds a constant key-value pair to the Gen3 data ID. 131 Name of the Gen3 dimension (data ID key) populated by 132 this handler (e.g. "visit" or "abstract_filter"). 140 __slots__ = (
"value",)
142 def extract(self, gen2id: dict, skyMap: Optional[BaseSkyMap], skyMapName: Optional[str],
143 datasetTypeName: str) -> Any:
149 """A KeyHandler that simply copies a value from a Gen3 data ID. 154 Name of the Gen3 dimension produced by this handler. 155 dtype : `type`, optional 156 If not `None`, the type that values for this key must be an 159 def __init__(self, dimension: str, gen2key: Optional[str] =
None,
160 dtype: Optional[type] =
None):
162 self.
gen2key = gen2key
if gen2key
is not None else dimension
165 __slots__ = (
"gen2key",
"dtype")
167 def extract(self, gen2id: dict, skyMap: Optional[BaseSkyMap], skyMapName: Optional[str],
168 datasetTypeName: str) -> Any:
171 if self.
dtype is not None:
174 except ValueError
as err:
176 f
"'{r}' is not a valid value for {self.dimension}; " 177 f
"expected {self.dtype.__name__}, got {type(r).__name__}." 183 """A KeyHandler for skymap patches. 190 def extract(self, gen2id: dict, skyMap: Optional[BaseSkyMap], skyMapName: Optional[str],
191 datasetTypeName: str) -> Any:
193 tract = gen2id[
"tract"]
194 tractInfo = skyMap[tract]
195 x, y = gen2id[
"patch"].split(
",")
196 patchInfo = tractInfo[int(x), int(y)]
197 return tractInfo.getSequentialPatchIndex(patchInfo)
201 """A KeyHandler for skymaps.""" 207 def extract(self, gen2id: dict, skyMap: Optional[BaseSkyMap], skyMapName: Optional[str],
208 datasetTypeName: str) -> Any:
214 """A KeyHandler for master calibration datasets. 218 super().
__init__(
"calibration_label")
222 def extract(self, gen2id: dict, skyMap: Optional[BaseSkyMap], skyMapName: Optional[str],
223 datasetTypeName: str) -> Any:
226 ccd=gen2id.get(
"ccd"), filter=gen2id.get(
"filter"))
230 """Callable object that translates Gen2 Data IDs to Gen3 Data IDs for a 231 particular DatasetType. 233 Translators should usually be constructed via the `makeMatching` method. 238 A list of KeyHandlers this Translator should use. 239 skyMap : `BaseSkyMap`, optional 240 SkyMap instance used to define any tract or patch Dimensions. 242 Gen3 SkyMap Dimension name to be associated with any tract or patch 244 datasetTypeName : `str` 245 Name of the dataset type whose data IDs this translator handles. 247 def __init__(self, handlers: List[KeyHandler], skyMap: Optional[BaseSkyMap], skyMapName: Optional[str],
248 datasetTypeName: str):
254 __slots__ = (
"handlers",
"skyMap",
"skyMapName",
"datasetTypeName")
264 Tuple[FrozenSet[str], KeyHandler, bool]
273 def addRule(cls, handler: KeyHandler, instrument: Optional[str] =
None,
274 datasetTypeName: Optional[str] =
None, gen2keys: Iterable[str] = (),
275 consume: bool =
True):
276 """Add a KeyHandler and an associated matching rule. 280 handler : `KeyHandler` 281 A KeyHandler instance to add to a Translator when this rule 284 Gen3 instrument name the Gen2 repository must be associated with 285 for this rule to match, or None to match any instrument. 286 datasetTypeName : `str` 287 Name of the DatasetType this rule matches, or None to match any 290 Sequence of Gen2 data ID keys that must all be present for this 292 consume : `bool` or `tuple` 293 If True (default), remove all entries in gen2keys from the set of 294 keys being matched to in order to prevent less-specific handlers 296 May also be a `tuple` listing only the keys to consume. 301 consume = frozenset(gen2keys)
303 consume = frozenset(consume)
305 consume = frozenset()
309 rulesForInstrument = cls._rules.setdefault(instrument, {
None: []})
310 rulesForInstrumentAndDatasetType = rulesForInstrument.setdefault(datasetTypeName, [])
311 rulesForInstrumentAndDatasetType.append((frozenset(gen2keys), handler, consume))
314 def makeMatching(cls, datasetTypeName: str, gen2keys: Dict[str, type], instrument: Optional[str] =
None,
315 skyMap: Optional[BaseSkyMap] =
None, skyMapName: Optional[str] =
None):
316 """Construct a Translator appropriate for instances of the given 321 datasetTypeName : `str` 322 Name of the dataset type. 324 Keys of a Gen2 data ID for this dataset. 325 instrument: `str`, optional 326 Name of the Gen3 instrument dimension for translated data IDs. 327 skyMap: `~lsst.skymap.BaseSkyMap`, optional 328 The skymap instance that defines any tract/patch data IDs. 329 `~lsst.skymap.BaseSkyMap` instances. 330 skyMapName : `str`, optional 331 Gen3 SkyMap Dimension name to be associated with any tract or patch 336 translator : `Translator` 337 A translator whose translate() method can be used to transform Gen2 338 data IDs to Gen3 dataIds. 340 if instrument
is not None:
341 rulesForInstrument = cls._rules.get(instrument, {
None: []})
343 rulesForInstrument = {
None: []}
344 rulesForAnyInstrument = cls._rules[
None]
345 candidateRules = itertools.chain(
346 rulesForInstrument.get(datasetTypeName, []),
347 rulesForInstrument[
None],
348 rulesForAnyInstrument.get(datasetTypeName, []),
349 rulesForAnyInstrument[
None],
352 targetKeys =
set(gen2keys)
353 for ruleKeys, ruleHandlers, consume
in candidateRules:
354 if ruleKeys.issubset(targetKeys):
355 matchedHandlers.append(ruleHandlers)
356 targetKeys -= consume
357 return Translator(matchedHandlers, skyMap=skyMap, skyMapName=skyMapName,
358 datasetTypeName=datasetTypeName)
361 """Return a Gen3 data ID that corresponds to the given Gen2 data ID. 365 handler.translate(gen2id, gen3id, skyMap=self.
skyMap, skyMapName=self.
skyMapName,
371 """The names of the dimensions populated by this Translator 374 return frozenset(h.dimension
for h
in self.
handlers)
381 for coaddName
in (
"deep",
"goodSeeing",
"psfMatched",
"dcr"):
388 Translator.addRule(
CopyKeyHandler(
"tract", dtype=int), gen2keys=(
"tract",))
392 for datasetTypeName
in (
"transmission_sensor",
"transmission_optics",
"transmission_filter"):
394 datasetTypeName=datasetTypeName)
399 Translator.addRule(
CopyKeyHandler(
"htm7", gen2key=
"pixel_id", dtype=int), gen2keys=(
"pixel_id",))
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
daf::base::PropertySet * set
def __call__(self, gen2id)