pyhs3.functions.InterpolationFunction¶
- class pyhs3.functions.InterpolationFunction(**data)[source]¶
Piecewise interpolation function implementation.
Implements ROOT’s PiecewiseInterpolation logic to morph between nominal and variation distributions based on nuisance parameter values. Supports multiple interpolation codes (0-6) for different mathematical approaches.
HS3 Reference
Note: Interpolation functions are not explicitly defined in the current HS3 specification.
- Mathematical Formulations:
For additive interpolation modes (codes 0, 2, 3, 4):
\[\text{result} = \text{nominal} + \sum_i I_i(\theta_i; \text{low}_i, \text{nominal}, \text{high}_i)\]For multiplicative interpolation modes (codes 1, 5, 6):
\[\text{result} = \text{nominal} \times \prod_i [1 + I_i(\theta_i; \text{low}_i/\text{nominal}, 1, \text{high}_i/\text{nominal})]\]
- Parameters:
name – Name of the function
high – High variation parameter names
low – Low variation parameter names
nom – Nominal parameter name
interpolationCodes – Interpolation method codes (0-6)
positiveDefinite – Whether function should be positive definite
vars – Variable names this function depends on (nuisance parameters)
data (
Any)
Fields¶ Field
Type
Required
Default
Yes
list[InterpolationCode]Yes
Yes
Yes
Yes
Yes
Literal[‘interpolation’]No
'interpolation'Yes
- __init__(**data)¶
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- Parameters:
data (
Any)
Methods
batch_expressions(members, context, functions)Evaluate many interpolation functions as one vectorised computation.
expression(context)Evaluate and return a named PyTensor expression.
get_parameter_list(context, param_key)Reconstruct a parameter list from flattened indexed keys.
model_post_init(context, /)This function is meant to behave like a BaseModel method to initialise private attributes.
process_parameter(param_key)Process a single parameter that can be either a string reference or numeric value.
process_parameter_list(param_key)Process a list parameter containing mixed string references and numeric values.
Attributes
Dictionary of PyTensor constants generated from numeric field values.
Set of parameter names this component depends on.
- classmethod InterpolationFunction.batch_expressions(members, context, functions)[source]¶
Evaluate many interpolation functions as one vectorised computation.
Every entry of every member becomes one element of a flat entry axis of length
E, and the members one row of a(F, E_max)table:Each entry’s parameter is gathered from one stacked vector: a free parameter times a constant (a
ProductFunctionof one free parameter and constants, or a parameter itself), or else the entry’s own expression with factor 1.Entries are sorted by interpolation code, so each code’s contributions are one contiguous slice, evaluated once with
_flexible_interp_single, and the slices are concatenated.Contributions are padded into
(F, E_max)with a sentinel (factor 1 for multiplicative, term 0 for additive members) and reduced over the entry axis.The
(F,)result is split into one scalar per member with a singleSplit, so the gradient is oneJoinrather than a sum ofFscatteredIncSubtensorupdates.
Members whose inputs are not all scalars, or whose entries mix additive (0, 2, 3, 4) and multiplicative (1, 5, 6) codes, are built with their own
expression()and logged at debug level.- Parameters:
- Returns:
Scalar expression per member name.
- Return type:
- InterpolationFunction.expression(context)¶
Evaluate and return a named PyTensor expression.
This is a template method that calls
_expression()to get the result, then names the result before returning._expression()may return a tensor it does not own — for example a single-summandSumFunctionor a single-factorProductFunctionreturnscontext[param]directly, which is the same tensor object stored inmodel.parameters. Renaming such a shared tensor in place would silently corrupt the parameter it borrows (e.g.model.parameters['mu']would be renamed to the function’s name). To avoid this, a result that already carries a different name is wrapped in a fresh identity copy before being renamed; freshly built (unnamed) expressions are named in place as before so that compilation andgraph_summarykeep working.
- InterpolationFunction.get_parameter_list(context, param_key)¶
Reconstruct a parameter list from flattened indexed keys.
Used to recover the original list structure from the indexed parameter mapping created by process_parameter_list().
- Parameters:
- Return type:
- Returns:
List of parameter values in original order.
Example
>>> from typing import Literal >>> class TestEvaluable(Evaluable): ... type: Literal["test"] = "test" ... factors: list[str | float] ... def _expression(self, _: Context) -> TensorVar: ... return None >>> >>> eval1 = TestEvaluable(name="test", factors=["a", 1.0, "b"]) >>> context = { ... "a": "tensor_a", ... "constant_test_factors[1]": "tensor_1", ... "b": "tensor_b" ... } >>> eval1.get_parameter_list(context, "factors") ['tensor_a', 'tensor_1', 'tensor_b']
- InterpolationFunction.model_post_init(context, /)¶
This function is meant to behave like a BaseModel method to initialise private attributes.
It takes context as an argument since that’s what pydantic-core passes when calling it.
- InterpolationFunction.process_parameter(param_key)¶
Process a single parameter that can be either a string reference or numeric value.
For numeric values, generates a unique constant name. For string values, returns the value as-is.
- Parameters:
param_key (
str) – The parameter field name to process (e.g., “mean”, “sigma”).- Return type:
- Returns:
(processed_name, numeric_value).processed_nameis either the original string value or a generated constant name;numeric_valueis the numeric value if the input was numeric,Noneotherwise.
Example
>>> from typing import Literal >>> class TestEvaluable(Evaluable): ... type: Literal["test"] = "test" ... some_param: str | float ... def _expression(self, _: Context) -> TensorVar: ... return None >>> >>> # String parameter >>> eval1 = TestEvaluable(name="test1", some_param="alpha") >>> eval1.process_parameter("some_param") ('alpha', None)
>>> # Numeric parameter >>> eval2 = TestEvaluable(name="test2", some_param=1.5) >>> eval2.process_parameter("some_param") ('constant_test2_some_param', 1.5)
- InterpolationFunction.process_parameter_list(param_key)¶
Process a list parameter containing mixed string references and numeric values.
For numeric values, generates indexed unique names and stores the values. For string values, returns the values as-is. Also updates internal parameter mapping with indexed keys.
- Parameters:
param_key (
str) – The parameter field name to process (e.g., “factors”, “coefficients”).- Return type:
- Returns:
(processed_names, numeric_values).processed_nameslists the parameter names (original strings or generated constant names);numeric_valueslists the numeric values (Nonefor string entries).
Example
>>> from typing import Literal >>> class TestEvaluable(Evaluable): ... type: Literal["test"] = "test" ... factors: list[str | float] ... def _expression(self, _: Context) -> TensorVar: ... return None >>> >>> eval1 = TestEvaluable(name="test", factors=["param1", 2.0, "param2"]) >>> names, values = eval1.process_parameter_list("factors") >>> names ['param1', 'constant_test_factors[1]', 'param2'] >>> values [None, 2.0, None]
- InterpolationFunction.constants¶
Dictionary of PyTensor constants generated from numeric field values.
- Returns:
Mapping from generated constant names to PyTensor constant tensors. Empty if all fields are string references.
- InterpolationFunction.parameters¶
Set of parameter names this component depends on.
- Returns:
Set of parameter names, including both string references and generated constant names for numeric values.
- InterpolationFunction.type¶
- InterpolationFunction.high¶
- InterpolationFunction.low¶
- InterpolationFunction.nom¶
- InterpolationFunction.interpolationCodes¶
- InterpolationFunction.positiveDefinite¶
- InterpolationFunction.vars¶
- InterpolationFunction.name¶