Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
APIAdapter.py
Go to the documentation of this file.
1"""Python accessors for global Hyperiso API state.
2
3This module exposes a small Pythonic wrapper around the C++ ``APIAdapter``.
4The adapter is intended for inspection and diagnostics after the global
5Hyperiso runtime has been initialized with :class:`HyperisoMaster`.
6
7Typical uses include checking initialization flags, retrieving configured
8paths, listing available parameter blocks and reading block entries from a
9specific parameter namespace.
10"""
11
12from __future__ import annotations
13
14from enum import Enum
15from typing import Set
16
17from pyhyperiso.phyperiso.pyhyperiso.core import APIAdapter as _CppAPIAdapter
18from pyhyperiso.phyperiso.pyhyperiso.core import APIPath as _CppAPIPath
19from pyhyperiso.core.Common.BlockName import BlockName
20from pyhyperiso.core.Common.GeneralEnum import ParameterType
21from pyhyperiso.core.Common.LhaID import LhaID
22from pyhyperiso.core.Core.HyperisoMaster import ExternalFlag
23from pyhyperiso.core.Math.Scalar import Scalar
24
25
26class APIPath(Enum):
27 """Known path keys exposed by the C++ API adapter.
28
29 ``LHA_PATH`` is runtime state set by ``HyperisoMaster.init(...)`` or
30 ``switch_lha(...)``. All other entries are backed by the active C++
31 ``IPathsProvider`` and can be overridden before initialization with
32 ``HyperisoMaster.pre_init_set_paths(...)``.
33 """
34
35 LHA_PATH = _CppAPIPath.LHA_PATH
36 ASSETS_ROOT = _CppAPIPath.ASSETS_ROOT
37
38 DEFAULT_PARAM_VALUES = _CppAPIPath.DEFAULT_PARAM_VALUES
39 DEFAULT_OBS_VALUES = _CppAPIPath.DEFAULT_OBS_VALUES
40 DEFAULT_PARAM_CORR = _CppAPIPath.DEFAULT_PARAM_CORR
41 DEFAULT_OBS_CORR = _CppAPIPath.DEFAULT_OBS_CORR
42 DEFAULT_NUISANCES = _CppAPIPath.DEFAULT_NUISANCES
43
44 USER_SM_PARAMS = _CppAPIPath.USER_SM_PARAMS
45 USER_FLAVOR_PARAMS = _CppAPIPath.USER_FLAVOR_PARAMS
46 USER_DECAY_PARAMS = _CppAPIPath.USER_DECAY_PARAMS
47 USER_OBS_VALUES = _CppAPIPath.USER_OBS_VALUES
48 USER_PARAM_CORR = _CppAPIPath.USER_PARAM_CORR
49 USER_OBS_CORR = _CppAPIPath.USER_OBS_CORR
50 USER_NUISANCES = _CppAPIPath.USER_NUISANCES
51
52 PARAM_MAPPING_DIR = _CppAPIPath.PARAM_MAPPING_DIR
53 TEMPLATE_DIR = _CppAPIPath.TEMPLATE_DIR
54 SPECTRUM_DIR = _CppAPIPath.SPECTRUM_DIR
55 MARTY_TEMP_DIR = _CppAPIPath.MARTY_TEMP_DIR
56
57
59 """Inspection wrapper around the C++ ``APIAdapter``.
60
61 The adapter reads process-wide state managed by the C++ core. It should
62 normally be used after ``HyperisoMaster.init(...)`` has loaded a model and
63 an LHA input file.
64
65 Examples:
66 >>> from pyhyperiso.core.Core.HyperisoMaster import HyperisoMaster
67 >>> from pyhyperiso.core.Core.HyperisoConfig import HyperisoConfig
68 >>> hyp = HyperisoMaster()
69 >>> hyp.init("lha/si_input.flha", HyperisoConfig())
70 >>> api = APIAdapter()
71 >>> api.check_flag(ExternalFlag.IS_LHA_SPECTRUM)
72 False
73 >>> api.get_path(APIPath.USER_SM_PARAMS)
74 '/path/to/user_sm_params.yaml'
75 >>> "MASS" in {str(b) for b in api.get_blocks_list(ParameterType.SM)}
76 True
77 """
78
79 def __init__(self) -> None:
80 """Create an adapter bound to the current C++ runtime state."""
81 self._cpp_obj = _CppAPIAdapter()
82
83 def check_flag(self, ext_flag: ExternalFlag) -> bool:
84 """Return whether an external initialization flag is enabled.
85
86 Args:
87 ext_flag: Python wrapper for a C++ ``ExternalFlag`` value.
88
89 Returns:
90 ``True`` if the flag is active in the current Hyperiso session,
91 otherwise ``False``.
92 """
93 return bool(self._cpp_obj.check_flag(ext_flag.value))
94
95 def get_path(self, path_type: APIPath) -> str:
96 """Return a configured path from the C++ runtime.
97
98 Args:
99 path_type: Path key to query.
100
101 Returns:
102 Path string associated with ``path_type``. Provider-backed paths
103 reflect any valid pre-init override registered through
104 ``HyperisoMaster.pre_init_set_paths(...)``.
105 """
106 return str(self._cpp_obj.get_path(path_type.value))
107
108 def get_all_blocks(self) -> Set[BlockName]:
109 """Return all known block names across parameter namespaces.
110
111 Returns:
112 Set of Python ``BlockName`` wrappers.
113 """
114 raw_blocks = self._cpp_obj.get_all_blocks()
115 return {BlockName(x) for x in raw_blocks}
116
117 def get_blocks_list(self, param_type: ParameterType = ParameterType.SM) -> Set[BlockName]:
118 """Return the block names available in one parameter namespace.
119
120 Args:
121 param_type: Parameter namespace to inspect. For example,
122 ``ParameterType.SM`` reads Standard Model input blocks, while
123 ``ParameterType.WILSON`` reads Wilson-coefficient blocks.
124
125 Returns:
126 Set of available block names.
127 """
128 raw_blocks = self._cpp_obj.get_blocks_list(param_type.value)
129 return {BlockName(x) for x in raw_blocks}
130
132 self,
133 block_name: str,
134 param_type: ParameterType = ParameterType.SM,
135 ) -> dict[LhaID, Scalar]:
136 """Return all entries stored in a parameter block.
137
138 Args:
139 block_name: Name of the LHA-style block, such as ``"MASS"`` or
140 ``"SMINPUTS"``.
141 param_type: Parameter namespace containing the block.
142
143 Returns:
144 Mapping from LHA code to scalar value.
145 """
146 raw_block_infos = self._cpp_obj.get_block_infos(
147 BlockName(block_name)._cpp_obj,
148 param_type.value,
149 )
150 return {LhaID(x): Scalar.from_cpp(y) for x, y in raw_block_infos.items()}
151
152 def get_type_of_block(self, block_name: str) -> list[ParameterType]:
153 """Return namespaces in which a block name exists.
154
155 Args:
156 block_name: Name of the block to locate.
157
158 Returns:
159 List of ``ParameterType`` values containing ``block_name``.
160 """
161 cpp_result = self._cpp_obj.get_type_of_block(BlockName(block_name)._cpp_obj)
162 return [ParameterType(item) for item in cpp_result]
163
164
165__all__ = ["APIAdapter", "APIPath"]
list[ParameterType] get_type_of_block(self, str block_name)
dict[LhaID, Scalar] get_block_infos(self, str block_name, ParameterType param_type=ParameterType.SM)
Set[BlockName] get_blocks_list(self, ParameterType param_type=ParameterType.SM)
str get_path(self, APIPath path_type)
Definition APIAdapter.py:95
bool check_flag(self, ExternalFlag ext_flag)
Definition APIAdapter.py:83