1"""Python wrappers around C++ enum/name mappers.
3The C++ core exposes a family of mapper classes that convert between enum
4values, canonical names, and sometimes domain-specific identifiers. This module
5keeps those conversions available from Python while returning Python wrapper
6objects where appropriate, for example :class:`ObservableId` and
10from typing
import AnyStr, Optional, Sequence, Union
12from pyhyperiso.phyperiso.pyhyperiso.common
import (
13 ContributionTypeMapper
as _CppContributionTypeMapper,
15from pyhyperiso.phyperiso.pyhyperiso.common
import DecayMapper
as _CppDecayMapper
16from pyhyperiso.phyperiso.pyhyperiso.common
import CustomObservableSpec
as _CppCustomObservableSpec
17from pyhyperiso.phyperiso.pyhyperiso.common
import _CppDecayId
18from pyhyperiso.phyperiso.pyhyperiso.common
import GroupMapper
as _CppGroupMapper
19from pyhyperiso.phyperiso.pyhyperiso.common
import MassTypeMapper
as _CppMassTypeMapper
20from pyhyperiso.phyperiso.pyhyperiso.common
import ModelMapper
as _CppModelMapper
21from pyhyperiso.phyperiso.pyhyperiso.common
import ObservableMapper
as _CppObservableMapper
22from pyhyperiso.phyperiso.pyhyperiso.common
import OrderMapper
as _CppOrderMapper
23from pyhyperiso.phyperiso.pyhyperiso.common
import ParameterTypeMapper
as _CppParameterTypeMapper
24from pyhyperiso.phyperiso.pyhyperiso.common
import ScaleTypeMapper
as _CppScaleTypeMapper
25from pyhyperiso.phyperiso.pyhyperiso.common
import WCoefMapper
as _CppWCoefMapper
26from pyhyperiso.phyperiso.pyhyperiso.common
import WilsonBasisMapper
as _CppWilsonBasisMapper
55 """Python fallback wrapper around the bound C++ ``DecayId`` type.
57 Add the same class to ``pyhyperiso.core.Common.SymbolId`` if you prefer
58 keeping all symbol-id wrappers in one module.
61 def __init__(self, value: Union[str,
"_CppDecayId"]):
62 if isinstance(value, _CppDecayId):
65 self.
_cpp_obj = _CppDecayId(str(value))
69 """Return the canonical string stored by the C++ id."""
73 """Return the bound C++ object."""
80 return f
"DecayId({self.name!r})"
83 if isinstance(other, DecayId):
84 return self.
name.lower() == other.name.lower()
85 if isinstance(other, str):
86 return self.
name.lower() == other.lower()
90 return hash(self.
name.lower())
94 """Return the C++ ``LhaID`` object or ``None`` for optional mapper APIs."""
97 if isinstance(ext, LhaID):
103 """Wrap a bound C++ ``ObservableId`` as a Python ``ObservableId``."""
108 """Wrap a bound C++ ``DecayId`` as a Python ``DecayId``."""
113 """Wrap a bound C++ ``WGroupId`` as a Python ``WGroupId``."""
118 """Wrap a bound C++ ``WCoefId`` as a Python ``WCoefId``."""
123 """Return the value stored in a pybind optional, or ``None`` if empty."""
124 if cpp_optional
is None:
127 return _unwrap_optional(cpp_optional, label)
133 """Python value object describing a custom observable registration.
136 canonical: Canonical observable name to register.
137 aliases: Optional aliases.
138 ext: Optional FLHA id.
144 aliases: Optional[Sequence[str]] =
None,
145 ext: Optional[LhaID] =
None,
152 """Convert this spec to the bound C++ ``CustomObservableSpec``."""
153 return _CppCustomObservableSpec(
160 def from_any(cls, value: Union[
"CustomObservableSpec", dict, tuple]):
161 """Build a spec from a spec object, dict, or tuple.
163 Accepted tuple shapes are ``(canonical,)``, ``(canonical, aliases)``,
164 and ``(canonical, aliases, ext)``.
166 if isinstance(value, cls):
169 if isinstance(value, dict):
172 value.get(
"aliases"),
176 if isinstance(value, tuple):
180 return cls(value[0], value[1])
182 return cls(value[0], value[1], value[2])
185 "Custom observable spec must be CustomObservableSpec, dict, "
186 "or tuple(canonical[, aliases[, ext]])"
191 """Mapper for :class:`QCDOrder` values."""
196 def str(self, obs_id: QCDOrder):
197 """Return the canonical C++ string for a QCD order.
200 obs_id: Python ``QCDOrder`` enum value.
203 str: Canonical name stored by the C++ mapper.
205 return _CppOrderMapper.str(obs_id.value)
208 """Round-trip a QCD order through its canonical string.
211 obs_id: Python ``QCDOrder`` enum value.
214 Any: Bound C++ enum/id returned by the mapper.
216 return _CppOrderMapper.id_of(self.
str(obs_id))
219 """Return the C++ mapper's primary string table."""
220 return _CppOrderMapper.get_str()
223 """Return all string aliases known to the C++ mapper."""
224 return _CppOrderMapper.get_str_all()
227 """Return enum values known to the C++ mapper."""
228 return [x
for x
in _CppOrderMapper.get_enum()]
232 """Mapper for :class:`ParameterType` namespaces."""
237 def str(self, obs_id: ParameterType):
238 """Return the canonical string for a parameter namespace."""
239 return _CppParameterTypeMapper.str(obs_id.value)
241 def id_of(self, obs_id: ParameterType):
242 """Return the C++ id associated with a parameter namespace."""
243 return _CppParameterTypeMapper.id_of(self.
str(obs_id))
246 """Return the C++ mapper's primary string table."""
247 return _CppParameterTypeMapper.get_str()
250 """Return all string aliases known to the C++ mapper."""
251 return _CppParameterTypeMapper.get_str_all()
254 """Return enum values known to the C++ mapper."""
255 return [x
for x
in _CppParameterTypeMapper.get_enum()]
259 """Mapper for physics :class:`Model` values."""
264 def str(self, obs_id: Model):
265 """Return the canonical model name."""
266 return _CppModelMapper.str(obs_id.value)
269 """Return the C++ id associated with a model."""
270 return _CppModelMapper.id_of(self.
str(obs_id))
273 """Return the C++ mapper's primary string table."""
274 return _CppModelMapper.get_str()
277 """Return all string aliases known to the C++ mapper."""
278 return _CppModelMapper.get_str_all()
281 """Return enum values known to the C++ mapper."""
282 return [x
for x
in _CppModelMapper.get_enum()]
286 """Mapper for Wilson-coefficient basis conventions."""
291 def str(self, obs_id: WilsonBasis):
292 """Return the canonical string for a Wilson basis."""
293 return _CppWilsonBasisMapper.str(obs_id.value)
295 def id_of(self, obs_id: WilsonBasis):
296 """Return the C++ id associated with a Wilson basis."""
297 return _CppWilsonBasisMapper.id_of(self.
str(obs_id))
300 """Return the C++ mapper's primary string table."""
301 return _CppWilsonBasisMapper.get_str()
304 """Return all string aliases known to the C++ mapper."""
305 return _CppWilsonBasisMapper.get_str_all()
308 """Return enum values known to the C++ mapper."""
309 return [x
for x
in _CppWilsonBasisMapper.get_enum()]
313 """Mapper for Wilson-coefficient contribution components.
315 Contribution types typically distinguish SM-only, BSM-only, and total
316 coefficients in the Wilson pipeline.
322 def str(self, obs_id: ContributionType):
323 """Return the canonical contribution-type string."""
324 return _CppContributionTypeMapper.str(obs_id.value)
326 def id_of(self, obs_id: ContributionType):
327 """Return the C++ id associated with a contribution type."""
328 return _CppContributionTypeMapper.id_of(self.
str(obs_id))
331 """Return the C++ mapper's primary string table."""
332 return _CppContributionTypeMapper.get_str()
335 """Return all string aliases known to the C++ mapper."""
336 return _CppContributionTypeMapper.get_str_all()
339 """Return enum values known to the C++ mapper."""
340 return [x
for x
in _CppContributionTypeMapper.get_enum()]
344 """Mapper for QCD mass conventions."""
349 def str(self, obs_id: MassType):
350 """Return the canonical mass-type string."""
351 return _CppMassTypeMapper.str(obs_id.value)
354 """Return the C++ id associated with a mass convention."""
355 return _CppMassTypeMapper.id_of(self.
str(obs_id))
358 """Return the C++ mapper's primary string table."""
359 return _CppMassTypeMapper.get_str()
362 """Return all string aliases known to the C++ mapper."""
363 return _CppMassTypeMapper.get_str_all()
366 """Return enum values known to the C++ mapper."""
367 return [x
for x
in _CppMassTypeMapper.get_enum()]
371 """Mapper for scale categories, such as matching and hadronic scales."""
376 def str(self, obs_id: ScaleType):
377 """Return the canonical scale-type string."""
378 return _CppScaleTypeMapper.str(obs_id.value)
381 """Return the C++ id associated with a scale category."""
382 return _CppScaleTypeMapper.id_of(self.
str(obs_id))
385 """Return the C++ mapper's primary string table."""
386 return _CppScaleTypeMapper.get_str()
389 """Return all string aliases known to the C++ mapper."""
390 return _CppScaleTypeMapper.get_str_all()
393 """Return enum values known to the C++ mapper."""
394 return [x
for x
in _CppScaleTypeMapper.get_enum()]
398 """Mapper for Wilson-coefficient groups.
400 The wrapper accepts both legacy :class:`WGroup` enums and runtime strings.
401 String lookups return :class:`WGroupId`, which is the preferred API for
402 custom groups and config/CLI driven workflows.
410 """Return the canonical string for a Wilson group enum or dynamic id."""
411 if isinstance(group, WGroup):
412 return _CppGroupMapper.str(group.value)
413 if isinstance(group, WGroupId):
414 return _CppGroupMapper.canonical(group._to_cpp())
415 raise TypeError(
"str() expects WGroup or WGroupId")
419 """Resolve a group enum/name/alias to a dynamic :class:`WGroupId`."""
420 if isinstance(group, WGroupId):
422 if isinstance(group, WGroup):
427 def to_id(group: WGroup) -> WGroupId:
428 """Convert a builtin Wilson-group enum to :class:`WGroupId`."""
433 """Return the canonical name of a dynamic Wilson group id."""
434 return _CppGroupMapper.canonical(group_id._to_cpp())
437 def block_name(group, scale: ScaleType, basis: WilsonBasis = WilsonBasis.STANDARD) -> str:
438 """Return the scale/basis block name used by Wilson parameter blocks."""
439 if isinstance(group, WGroup):
440 return _CppGroupMapper.str(group.value, scale.value, basis.value)
442 return _CppGroupMapper.str_id(group_id._to_cpp(), scale.value, basis.value)
445 def register_custom(canonical: str, aliases=
None, external: Optional[str] =
None) -> bool:
446 """Register a custom Wilson group in the dynamic mapper.
449 canonical: Canonical group name.
450 aliases: Optional aliases accepted by :meth:`id_of`.
451 external: Optional external key used by C++ mapper extensions.
453 return _CppGroupMapper.register_custom(canonical, list(aliases
or []), external)
456 """Return the C++ mapper's primary string table."""
457 return _CppGroupMapper.get_str()
460 """Return all string aliases known to the C++ mapper."""
461 return _CppGroupMapper.get_str_all()
464 """Return builtin enum values known to the C++ mapper."""
465 return [
WGroup(x)
for x
in _CppGroupMapper.get_enum()]
469 """Mapper for Wilson-coefficient identifiers.
471 Runtime lookups return :class:`WCoefId`, so custom coefficients do not need
472 a corresponding static :class:`WCoeff` enum value.
480 """Return the canonical string for a Wilson coefficient."""
481 if isinstance(coef, WCoeff):
482 return _CppWCoefMapper.str(coef.value)
483 if isinstance(coef, WCoefId):
484 return _CppWCoefMapper.canonical(coef._to_cpp())
485 raise TypeError(
"str() expects WCoeff or WCoefId")
489 """Resolve a coefficient enum/name/alias to :class:`WCoefId`."""
490 if isinstance(coef, WCoefId):
492 if isinstance(coef, WCoeff):
498 """Convert a builtin coefficient enum to :class:`WCoefId`."""
503 """Return the canonical name of a dynamic coefficient id."""
504 return _CppWCoefMapper.canonical(coef_id._to_cpp())
508 """Register a custom Wilson coefficient.
511 canonical: Canonical coefficient name.
512 aliases: Optional aliases accepted by :meth:`id_of`.
513 flha: External FLHA pair used by Wilson input/output conventions.
515 return _CppWCoefMapper.register_custom(canonical, list(aliases
or []), tuple(flha))
519 """Return the external FLHA base pair for a coefficient enum or id."""
520 if isinstance(coef, WCoeff):
521 return tuple(_CppWCoefMapper.flha_base(coef.value))
523 return tuple(_CppWCoefMapper.flha_base(coef_id._to_cpp()))
526 def flha_full(coef, order: QCDOrder, contribution: ContributionType) -> LhaID:
527 """Return the full FLHA identifier for a Wilson coefficient term."""
528 if not isinstance(order, QCDOrder):
529 raise TypeError(
"order must be a QCDOrder")
530 if not isinstance(contribution, ContributionType):
531 raise TypeError(
"contribution must be a ContributionType")
532 if isinstance(coef, WCoeff):
533 cpp_id = _CppWCoefMapper.flha_full(coef.value, order.value, contribution.value)
536 cpp_id = _CppWCoefMapper.flha_full(coef_id._to_cpp(), order.value, contribution.value)
540 """Return the C++ mapper's primary string table."""
541 return _CppWCoefMapper.get_str()
544 """Return all string aliases known to the C++ mapper."""
545 return _CppWCoefMapper.get_str_all()
548 """Return builtin enum values known to the C++ mapper."""
549 return [
WCoeff(x)
for x
in _CppWCoefMapper.get_enum()]
553 """Mapper for builtin and custom observables.
555 Dynamic/user-facing code should use :class:`ObservableId`; the static
556 :class:`Observables` enum is kept for builtin legacy paths only.
563 def str(obs: Observables) -> str:
564 """Return the canonical C++ name for a builtin observable enum."""
565 return _CppObservableMapper.str(obs.value)
569 """Resolve a builtin observable name to the static enum.
571 Custom observables cannot be represented by :class:`Observables`; use
572 :meth:`id_of` for dynamic lookup.
574 return Observables(_CppObservableMapper.enum_elt_legacy(name))
577 def id_of(name: AnyStr) -> ObservableId:
578 """Resolve a canonical name or alias to an :class:`ObservableId`."""
582 def to_id(obs: Observables) -> ObservableId:
583 """Convert a builtin observable enum to an :class:`ObservableId`."""
588 """Return the canonical name of an internal observable id."""
589 return _CppObservableMapper.canonical(obs_id._to_cpp())
593 """Return builtin observable names."""
594 return _CppObservableMapper.get_str()
598 """Return builtin and custom observable names."""
599 return _CppObservableMapper.get_str_all()
603 """Return builtin observable enum values known to the mapper."""
604 return [
Observables(x)
for x
in _CppObservableMapper.get_enum()]
608 """Convert an FLHA code to an observable id.
611 KeyError: If no observable is associated with the FLHA code.
613 cpp_opt = _CppObservableMapper.from_flha(flha_id._cpp_obj)
614 cpp_id = _unwrap_optional(cpp_opt,
"ObservableId from FLHA")
619 """Return the FLHA code associated with an observable.
622 obs: Either a builtin :class:`Observables` or an
623 :class:`ObservableId`.
625 if isinstance(obs, Observables):
626 return LhaID(_CppObservableMapper.flha(obs.value))
627 if isinstance(obs, ObservableId):
628 return LhaID(_CppObservableMapper.flha(obs._to_cpp()))
629 raise TypeError(
"flha() expects Observables or ObservableId")
634 parent_decay: Union[str, Decays, DecayId],
635 aliases: Optional[Sequence[str]] =
None,
636 ext: Optional[LhaID] =
None,
638 """Register a custom observable and attach it to a parent decay.
641 canonical: Canonical observable name.
642 parent_decay: Parent decay as name/alias, :class:`Decays`, or
644 aliases: Optional observable aliases.
645 ext: Optional FLHA id.
648 bool: ``True`` if the C++ registry accepted the symbol.
650 aliases = list(aliases
or [])
653 if isinstance(parent_decay, Decays):
654 return _CppObservableMapper.register_custom_with_decay_enum(
661 if isinstance(parent_decay, DecayId):
662 return _CppObservableMapper.register_custom_with_decay_id(
664 parent_decay._to_cpp(),
669 return _CppObservableMapper.register_custom(
678 """Mapper for builtin and custom decays."""
684 def str(decay: Union[Decays, DecayId]) -> str:
685 """Return the canonical string for a builtin enum or dynamic id."""
686 if isinstance(decay, Decays):
687 return _CppDecayMapper.str(decay.value)
688 if isinstance(decay, DecayId):
689 return _CppDecayMapper.canonical(decay._to_cpp())
690 raise TypeError(
"str() expects Decays or DecayId")
694 """Resolve a builtin decay name to the static enum."""
695 return Decays(_CppDecayMapper.enum_elt_legacy(name))
698 def id_of(decay: Union[AnyStr, Decays]) -> DecayId:
699 """Resolve a decay name/alias or enum to :class:`DecayId`."""
700 if isinstance(decay, Decays):
705 def to_id(decay: Decays) -> DecayId:
706 """Convert a builtin decay enum to a dynamic :class:`DecayId`."""
711 """Return the canonical name of a dynamic decay id."""
712 return _CppDecayMapper.canonical(decay_id._to_cpp())
716 """Return builtin decay names."""
717 return _CppDecayMapper.get_str()
721 """Return builtin and custom decay names."""
722 return _CppDecayMapper.get_str_all()
726 """Return builtin decay enum values."""
727 return [
Decays(x)
for x
in _CppDecayMapper.get_enum()]
731 """Return observables attached to a decay.
734 list[Observables] for builtin :class:`Decays` input, and
735 list[ObservableId] for :class:`DecayId` or string input.
737 if isinstance(decay, Decays):
738 return [
Observables(o)
for o
in _CppDecayMapper.get_observables(decay.value)]
740 if isinstance(decay, DecayId):
749 """Return builtin decay observables converted to :class:`ObservableId`."""
754 """Return the builtin decay enum associated with a builtin observable."""
755 return Decays(_CppDecayMapper.get_decay(obs.value))
758 def get_decay_id(obs: Union[Observables, ObservableId]) -> Optional[DecayId]:
759 """Return the dynamic parent decay id of an observable, if known."""
760 if isinstance(obs, Observables):
761 cpp_opt = _CppDecayMapper.get_decay_id(obs.value)
762 elif isinstance(obs, ObservableId):
763 cpp_opt = _CppDecayMapper.get_decay_id(obs._to_cpp())
765 raise TypeError(
"get_decay_id() expects Observables or ObservableId")
774 """Return the dynamic parent decay id or raise from C++ if absent."""
775 return _wrap_decay_id(_CppDecayMapper.get_decay_id_or_throw(obs._to_cpp()))
779 """Return whether a decay has at least one observable."""
780 if isinstance(decay, Decays):
782 elif isinstance(decay, str):
784 if not isinstance(decay, DecayId):
785 raise TypeError(
"has_observables() expects Decays, DecayId, or str")
786 return _CppDecayMapper.has_observables(decay._to_cpp())
791 observables: Sequence[Union[CustomObservableSpec, dict, tuple]],
792 aliases: Optional[Sequence[str]] =
None,
794 """Register a custom decay together with at least one observable."""
795 cpp_specs = [CustomObservableSpec.from_any(o)._to_cpp()
for o
in observables]
796 return _CppDecayMapper.register_custom_with_observables(
805 "ParameterTypeMapper",
808 "ContributionTypeMapper",
817 "CustomObservableSpec",
static IdOf< DecayTag > to_id(Decays e)
Converts an enum value to an IdOf<Tag>.
static IdOf< WGroupTag > id_of(std::string_view s)
Resolves a string into an IdOf<Tag> via the registry.
id_of(self, ContributionType obs_id)
str(self, ContributionType obs_id)
from_any(cls, Union["CustomObservableSpec", dict, tuple] value)
__init__(self, str canonical, Optional[Sequence[str]] aliases=None, Optional[LhaID] ext=None)
__init__(self, Union[str, "_CppDecayId"] value)
str canonical(DecayId decay_id)
bool register_custom_with_observables(str canonical, Sequence[Union[CustomObservableSpec, dict, tuple]] observables, Optional[Sequence[str]] aliases=None)
str str(Union[Decays, DecayId] decay)
DecayId get_decay_id_or_throw(ObservableId obs)
bool has_observables(Union[Decays, DecayId, str] decay)
Decays enum_elt_legacy(AnyStr name)
Decays get_decay(Observables obs)
DecayId id_of(Union[AnyStr, Decays] decay)
get_observables(Union[Decays, DecayId, str] decay)
get_observable_ids(Decays decay)
Optional[DecayId] get_decay_id(Union[Observables, ObservableId] obs)
DecayId to_id(Decays decay)
str block_name(group, ScaleType scale, WilsonBasis basis=WilsonBasis.STANDARD)
str canonical(WGroupId group_id)
WGroupId to_id(WGroup group)
bool register_custom(str canonical, aliases=None, Optional[str] external=None)
str(self, MassType obs_id)
id_of(self, MassType obs_id)
id_of(self, Model obs_id)
str canonical(ObservableId obs_id)
ObservableId from_flha(LhaID flha_id)
ObservableId to_id(Observables obs)
bool register_custom(str canonical, Union[str, Decays, DecayId] parent_decay, Optional[Sequence[str]] aliases=None, Optional[LhaID] ext=None)
Observables enum_elt_legacy(AnyStr name)
ObservableId id_of(AnyStr name)
id_of(self, QCDOrder obs_id)
str(self, QCDOrder obs_id)
id_of(self, ParameterType obs_id)
str(self, ParameterType obs_id)
str(self, ScaleType obs_id)
id_of(self, ScaleType obs_id)
WCoefId to_id(WCoeff coef)
LhaID flha_full(coef, QCDOrder order, ContributionType contribution)
bool register_custom(str canonical, aliases=None, flha=(0, 0))
tuple[int, int] flha_base(coef)
str canonical(WCoefId coef_id)
id_of(self, WilsonBasis obs_id)
str(self, WilsonBasis obs_id)
_unwrap_optional_or_none(cpp_optional, str label)
WGroupId _wrap_wgroup_id(cpp_id)
_cpp_lhaid_or_none(Optional[LhaID] ext)
WCoefId _wrap_wcoef_id(cpp_id)
DecayId _wrap_decay_id(cpp_id)
ObservableId _wrap_observable_id(cpp_id)