Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
DependencyPruner.py
Go to the documentation of this file.
1"""High-level Python wrapper for dependency pruning operations.
2
3This module wraps the bound C++ ``DependencyPruner`` adapter. It provides a
4small Python-friendly façade for detaching and reattaching dependent blocks or
5individual dependent parameters from their upstream dependencies.
6
7The Hyperiso core must be initialized before using this wrapper, typically via
8``HyperisoMaster.init(...)``, because the underlying C++ adapter delegates to
9``Parameters::GetInstance(...)``.
10
11Example:
12 >>> pruner = DependencyPruner()
13 >>> pruner.detach_block(ParameterType.SM, "EW")
14 >>> pruner.reattach_block(ParameterType.SM, "EW")
15"""
16
17from typing import Any
18
19from pyhyperiso.phyperiso.pyhyperiso.core import DependencyPruner as _CppDependencyPruner
20from pyhyperiso.core.Common.GeneralEnum import ParameterType
21
22
24 """Detach and reattach dependency links in the Hyperiso parameter graph.
25
26 ``DependencyPruner`` is a Python façade over the C++ adapter with the same
27 name. It can operate at two levels:
28
29 - block level, through :meth:`detach_block` and :meth:`reattach_block`,
30 - parameter level, through :meth:`detach_parameter` and
31 :meth:`reattach_parameter`.
32
33 Args:
34 cpp_obj: Optional already-bound C++ ``DependencyPruner`` instance. When
35 omitted, a fresh C++ adapter is created.
36 """
37
38 def __init__(self, cpp_obj: _CppDependencyPruner | None = None):
39 """Create a Python wrapper around the C++ ``DependencyPruner``."""
40 self._cpp_obj = cpp_obj if cpp_obj is not None else _CppDependencyPruner()
41
42 @staticmethod
43 def _normalize_block_name(block_name: Any) -> str:
44 """Convert a Python block-name-like object to the string expected by pybind.
45
46 Args:
47 block_name: Block name, usually a ``str``. Objects implementing
48 ``__str__`` are also accepted.
49
50 Returns:
51 str: Block name passed to the C++ binding, where it is converted to
52 ``BlockName``.
53 """
54 return str(block_name)
55
56 @staticmethod
57 def _to_cpp_lha_id(id_: Any) -> Any:
58 """Return the bound C++ ``LhaID`` object for a Python LHA id wrapper.
59
60 Args:
61 id_: Either a bound C++ ``LhaID`` object or a Python wrapper exposing
62 ``to_cpp()``.
63
64 Returns:
65 Any: Object forwarded to the pybind layer.
66 """
67 return id_.to_cpp() if hasattr(id_, "to_cpp") else id_
68
69 def detach_block(self, parameter_type: ParameterType, block_name: Any) -> None:
70 """Detach a dependent block from its upstream source blocks.
71
72 After detachment, updates of upstream blocks no longer propagate through
73 this dependency link until the block is reattached.
74
75 Args:
76 parameter_type: Parameter namespace containing the block, e.g.
77 ``ParameterType.SM``.
78 block_name: Name of the dependent block to detach.
79 """
80 self._cpp_obj.detach_block(parameter_type.value, self._normalize_block_name(block_name))
81
82 def reattach_block(self, parameter_type: Any, block_name: Any) -> None:
83 """Reattach a previously detached dependent block to its sources.
84
85 Args:
86 parameter_type: Parameter namespace containing the block, e.g.
87 ``ParameterType.SM``.
88 block_name: Name of the dependent block to reattach.
89 """
90 self._cpp_obj.reattach_block(parameter_type.value, self._normalize_block_name(block_name))
91
92 def detach_parameter(self, parameter_type: ParameterType, block_name: Any, id_: Any) -> None:
93 """Detach a dependent parameter from its upstream source parameters.
94
95 Args:
96 parameter_type: Parameter namespace containing the parameter.
97 block_name: Name of the block containing the parameter.
98 id_: LHA identifier of the dependent parameter. This may be a bound
99 C++ ``LhaID`` or a Python wrapper exposing ``to_cpp()``.
100 """
102 parameter_type.value,
103 self._normalize_block_name(block_name),
104 self._to_cpp_lha_id(id_),
105 )
106
107 def reattach_parameter(self, parameter_type: ParameterType, block_name: Any, id_: Any) -> None:
108 """Reattach a previously detached dependent parameter to its sources.
109
110 Args:
111 parameter_type: Parameter namespace containing the parameter.
112 block_name: Name of the block containing the parameter.
113 id_: LHA identifier of the dependent parameter. This may be a bound
114 C++ ``LhaID`` or a Python wrapper exposing ``to_cpp()``.
115 """
117 parameter_type.value,
118 self._normalize_block_name(block_name),
119 self._to_cpp_lha_id(id_),
120 )
121
122 def detach_param(self, parameter_type: ParameterType, block_name: Any, id_: Any) -> None:
123 """Alias for :meth:`detach_parameter`.
124
125 Args:
126 parameter_type: Parameter namespace containing the parameter.
127 block_name: Name of the block containing the parameter.
128 id_: LHA identifier of the dependent parameter.
129 """
130 self.detach_parameter(parameter_type, block_name, id_)
131
132 def reattach_param(self, parameter_type: Any, block_name: Any, id_: Any) -> None:
133 """Alias for :meth:`reattach_parameter`.
134
135 Args:
136 parameter_type: Parameter namespace containing the parameter.
137 block_name: Name of the block containing the parameter.
138 id_: LHA identifier of the dependent parameter.
139 """
140 self.reattach_parameter(parameter_type, block_name, id_)
141
142 @property
143 def cpp_obj(self) -> _CppDependencyPruner:
144 """Bound C++ ``DependencyPruner`` instance used by this wrapper."""
145 return self._cpp_obj
146
147
148__all__ = ["DependencyPruner"]
None detach_param(self, ParameterType parameter_type, Any block_name, Any id_)
None reattach_param(self, Any parameter_type, Any block_name, Any id_)
__init__(self, _CppDependencyPruner|None cpp_obj=None)
None reattach_block(self, Any parameter_type, Any block_name)
None reattach_parameter(self, ParameterType parameter_type, Any block_name, Any id_)
None detach_block(self, ParameterType parameter_type, Any block_name)
None detach_parameter(self, ParameterType parameter_type, Any block_name, Any id_)