Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
BlockProvider.py
Go to the documentation of this file.
1"""Helpers for inspecting and logging C++ parameter blocks.
2
3The C++ ``BlockProvider`` gives access to HyperISO parameter blocks. It can
4check whether a block exists, log blocks through the C++ logging system, and
5retrieve block names or block contents as Python objects.
6"""
7
8from __future__ import annotations
9
10from typing import Any, TypeAlias
11
12from pyhyperiso.phyperiso.pyhyperiso.core import BlockProvider as _CppBlockProvider
13from pyhyperiso.core.Common.GeneralEnum import ParameterType
14
15
16BlockKey: TypeAlias = Any
17BlockContent: TypeAlias = dict[BlockKey, float]
18
19
21 """Diagnostic and inspection wrapper around the C++ ``BlockProvider``.
22
23 This class exposes block-level utilities for HyperISO parameters. It keeps
24 the original C++ logging methods available while also providing Pythonic
25 accessors for block names and block contents.
26
27 Examples:
28 >>> logger = BlockLogger()
29 >>> logger.exists("SMINPUTS", ParameterType.SM)
30 True
31 >>> logger.get_all_blocks(ParameterType.SM)
32 {"SMINPUTS", "MASS", ...}
33 >>> logger.get_block(ParameterType.SM, "MASS")
34 {25: 125.1, ...}
35 """
36
37 def __init__(self) -> None:
38 """Create a block logger for the current C++ parameter storage."""
39 self._cpp_obj = _CppBlockProvider()
40
41 @staticmethod
42 def _to_cpp_param_type(param_type: ParameterType) -> int:
43 """Convert a Python ``ParameterType`` enum to its C++ value.
44
45 Args:
46 param_type: Python parameter namespace enum.
47
48 Returns:
49 Integer value expected by the C++ binding.
50
51 Raises:
52 TypeError: If ``param_type`` is not a ``ParameterType`` instance.
53 """
54 if not isinstance(param_type, ParameterType):
55 raise TypeError(
56 f"param_type must be an instance of ParameterType, got {type(param_type).__name__}."
57 )
58
59 return param_type.value
60
61 def exists(self, blockname: str, param_type: ParameterType) -> bool:
62 """Return whether a block exists in a parameter namespace.
63
64 Args:
65 blockname: LHA-style block name, for example ``"MASS"``.
66 param_type: Parameter namespace to inspect.
67
68 Returns:
69 ``True`` if the block exists, otherwise ``False``.
70 """
71 return bool(
72 self._cpp_obj.exists(
73 blockname,
74 self._to_cpp_param_type(param_type),
75 )
76 )
77
78 def log_all_blocks(self, param_type: ParameterType) -> None:
79 """Log all blocks from a parameter namespace through the C++ logger.
80
81 Args:
82 param_type: Parameter namespace to log.
83 """
84 self._cpp_obj.log_all_blocks(self._to_cpp_param_type(param_type))
85
86 def log_block(self, param_type: ParameterType, blockname: str) -> None:
87 """Log one block through the C++ logger.
88
89 Args:
90 param_type: Parameter namespace containing the block.
91 blockname: Name of the block to log.
92 """
94 self._to_cpp_param_type(param_type),
95 blockname,
96 )
97
98 def get_block(self, param_type: ParameterType, blockname: str) -> BlockContent:
99 """Return the content of one block as a Python dictionary.
100
101 Args:
102 param_type: Parameter namespace containing the block.
103 blockname: Name of the block to retrieve.
104
105 Returns:
106 Dictionary mapping LHA identifiers to scalar values.
107
108 Examples:
109 >>> logger = BlockLogger()
110 >>> logger.get_block(ParameterType.SM, "MASS")
111 {25: 125.1, ...}
112 """
113 return dict(
114 self._cpp_obj.get_block(
115 self._to_cpp_param_type(param_type),
116 blockname,
117 )
118 )
119
120 def get_all_blocks(self, param_type: ParameterType) -> set[str]:
121 """Return all block names available in a parameter namespace.
122
123 Args:
124 param_type: Parameter namespace to inspect.
125
126 Returns:
127 Set containing all available block names.
128
129 Examples:
130 >>> logger = BlockLogger()
131 >>> logger.get_all_blocks(ParameterType.SM)
132 {"SMINPUTS", "MASS", ...}
133 """
134 return set(
136 self._to_cpp_param_type(param_type),
137 )
138 )
139
140
141__all__ = ["BlockLogger", "BlockContent", "BlockKey"]
int _to_cpp_param_type(ParameterType param_type)
BlockContent get_block(self, ParameterType param_type, str blockname)
set[str] get_all_blocks(self, ParameterType param_type)
None log_all_blocks(self, ParameterType param_type)
None log_block(self, ParameterType param_type, str blockname)
bool exists(self, str blockname, ParameterType param_type)