Resolvers
Native resolvers¶
hya.resolvers ¶
Implement some resolvers using features from standard libraries.
hya.resolvers.add_resolver ¶
add_resolver(first: Any, *others: Any) -> Any
Return the addition of several objects.
The arguments are not modified: first is never extended in place,
so interpolating a list of the configuration does not change it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
first
|
Any
|
The first value to add. |
required |
*others
|
Any
|
The other values to add. |
()
|
Returns:
| Type | Description |
|---|---|
Any
|
|
Example
>>> import hya
>>> from omegaconf import OmegaConf
>>> conf = OmegaConf.create({"key": "${hya.add:1,2}"})
>>> conf.key
3
>>> conf = OmegaConf.create({"key": "${hya.add:1,2,3,4}"})
>>> conf.key
10
>>> conf = OmegaConf.create({"x": [1, 2], "key": "${hya.add:${x},[3]}"})
>>> conf.key
[1, 2, 3]
>>> conf.x
[1, 2]
hya.resolvers.ceildiv_resolver ¶
ceildiv_resolver(dividend: float, divisor: float) -> float
Compute the ceiling division of two numbers.
Ceiling division rounds the result up, which is useful for
calculating the minimum number of chunks needed to accommodate a
given total. The type of the result follows //: an int for
two integers (exact, whatever their size) and a float if one of
them is a float (11.0, 4 gives 3.0).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dividend
|
float
|
The dividend (numerator). |
required |
divisor
|
float
|
The divisor (denominator). |
required |
Returns:
| Type | Description |
|---|---|
float
|
The ceiling of dividend / divisor, computed as |
float
|
|
Example
>>> import hya
>>> from omegaconf import OmegaConf
>>> conf = OmegaConf.create({"key": "${hya.ceildiv:11,4}"})
>>> conf.key
3
>>> # Useful for calculating number of batches
>>> conf = OmegaConf.create({"batches": "${hya.ceildiv:100,32}"})
>>> conf.batches
4
>>> # The type of the result follows ``//``
>>> OmegaConf.create({"key": "${hya.ceildiv:11.0,4}"}).key
3.0
hya.resolvers.iter_join_resolver ¶
iter_join_resolver(
iterable: Iterable[Any], separator: str
) -> str
Convert all items in an iterable to strings and join them.
This resolver takes an iterable (e.g., list, tuple) and concatenates all elements into a single string, separated by the specified separator. Each element is converted to a string using str().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iterable
|
Iterable[Any]
|
Any iterable object where all the returned values are strings or can be converted to string. Common examples include lists, tuples, or OmegaConf ListConfig objects. |
required |
separator
|
str
|
The separator string to use between the items. Can be any string including empty string, space, comma, etc. |
required |
Returns:
| Type | Description |
|---|---|
str
|
A single string with all iterable elements joined by the separator. |
Example
>>> import hya
>>> from omegaconf import OmegaConf
>>> conf = OmegaConf.create({"key": "${hya.iter_join:[abc,2,def],-}"})
>>> conf.key
abc-2-def
>>> conf = OmegaConf.create({"path": "${hya.iter_join:[data,models,v1],/}"})
>>> conf.path
data/models/v1
hya.resolvers.mul_resolver ¶
mul_resolver(first: Any, *others: Any) -> Any
Return the multiplication of several objects.
The arguments are not modified. The values are meant to be numbers: the
repetition of a list of the configuration ([1, 2] * 2) is not
supported.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
first
|
Any
|
The first value to multiply. |
required |
*others
|
Any
|
The other values to multiply. |
()
|
Returns:
| Type | Description |
|---|---|
Any
|
|
Example
>>> import hya
>>> from omegaconf import OmegaConf
>>> conf = OmegaConf.create({"key": "${hya.mul:1,2}"})
>>> conf.key
2
>>> conf = OmegaConf.create({"key": "${hya.mul:1,2,3}"})
>>> conf.key
6
hya.resolvers.path_resolver ¶
path_resolver(path: str | PathLike[str]) -> Path
Return a resolved path object from a path or a file URL.
This resolver converts a string, or any path-like object (for example
a pathlib.Path stored in the configuration), to a pathlib.Path
object: it expands the user home directory (~) and resolves the
path to an absolute path. A string that starts with file: (the
scheme is case-insensitive) is read as a file URL: the path of the URL
is used, with its percent-encoding (for example %20 for a space)
decoded. Any other string is a plain path, written as is: ? and
% are not interpreted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | PathLike[str]
|
The target path as a string or a path-like object, or a
|
required |
Returns:
| Type | Description |
|---|---|
Path
|
A fully resolved |
Path
|
directory and converted to absolute path. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a |
Note
Within an OmegaConf interpolation, a path with a tilde (~)
must be quoted (${hya.path:"~/data"}); unquoted, the
interpolation is a grammar error.
Example
>>> from pathlib import Path
>>> import hya
>>> from omegaconf import OmegaConf
>>> conf = OmegaConf.create({"key": "${hya.path:/my/path}"})
>>> conf.key
PosixPath('/my/path')
>>> conf = OmegaConf.create({"key": "${hya.path:file:///my/path%20to}"})
>>> conf.key
PosixPath('/my/path to')
>>> conf = OmegaConf.create({"key": '${hya.path:"~/my/path"}'})
>>> conf.key == Path("~/my/path").expanduser().resolve()
True
>>> conf = OmegaConf.create({"key": "${hya.path:${hya.path:/my/path}}"})
>>> conf.key
PosixPath('/my/path')
hya.resolvers.pi_resolver ¶
pi_resolver() -> float
Return the value PI.
Returns:
| Type | Description |
|---|---|
float
|
The value of PI. |
Example
>>> import hya
>>> from omegaconf import OmegaConf
>>> conf = OmegaConf.create({"key": "${hya.pi:}"})
>>> conf.key
3.14159...
hya.resolvers.sha256_resolver ¶
sha256_resolver(obj: Any) -> str
Compute the SHA-256 hash of an input object.
This resolver converts the object to a string representation and computes its SHA-256 hash. Useful for generating consistent identifiers or cache keys from configuration values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
The object to compute the SHA-256 hash. The object will be converted to a string using str() before hashing. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The SHA-256 hash as a hexadecimal string (64 characters). |
Example
>>> import hya
>>> from omegaconf import OmegaConf
>>> conf = OmegaConf.create({"key": "${hya.sha256:mystring}"})
>>> conf.key
bd3ff47540b31e62d4ca6b07794e5a886b0f655fc322730f26ecd65cc7dd5c90
>>> # Useful for generating consistent IDs
>>> conf = OmegaConf.create({"id": "${hya.sha256:experiment_v1}"})
Optional resolvers¶
hya.braceexpand ¶
Implement a braceexpand resolver for brace expansion patterns.
This module provides OmegaConf resolvers that use the braceexpand
library to expand string patterns with brace notation (similar to bash
brace expansion). The resolver is always registered; the braceexpand
package is needed only when it is called, and a missing package raises
an error that says how to install it.
hya.braceexpand.braceexpand_resolver ¶
braceexpand_resolver(pattern: str) -> list[str]
Return the list of the strings of a brace expansion of pattern.
This resolver expands a string with brace patterns similar to bash brace expansion, generating multiple strings from a single pattern. Useful for generating lists of similar configuration values.
Please check https://github.com/trendels/braceexpand for more information about the syntax.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
str
|
Specifies the pattern of the brace expansion. Supports numeric ranges (e.g., "file{1..3}.txt"), character sequences (e.g., "{a,b,c}"), and nested patterns. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
The list of the strings resulting from the brace expansion of |
list[str]
|
the pattern. It is a list (and not an iterator), so it has a |
list[str]
|
length, can be indexed and can be read several times. The |
list[str]
|
whole expansion is built at once. |
Note
When using brace expansion patterns in OmegaConf, you need
to quote the pattern string (${hya.braceexpand:"a{1,2}"})
or to escape the braces to avoid conflicts with OmegaConf's
own syntax for curly braces.
Example
>>> import hya
>>> from omegaconf import OmegaConf
>>> conf = OmegaConf.create({"key": '${hya.braceexpand:"file_{1..3}.txt"}'})
>>> list(conf.key)
['file_1.txt', 'file_2.txt', 'file_3.txt']
>>> len(conf.key)
3
hya.numpy ¶
Implement NumPy resolvers for array creation and manipulation.
This module provides OmegaConf resolvers that use NumPy for array
operations, allowing users to create NumPy arrays directly from
configuration files. The numpy package is needed only when the
resolver is called.
hya.numpy.to_array_resolver ¶
to_array_resolver(
data: ArrayLike, dtype: str | None = None
) -> ndarray
Implement a resolver to transform the input to a
numpy.ndarray.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
ArrayLike
|
Specifies the data to transform in |
required |
dtype
|
str | None
|
The data type of the array, as a string that
|
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
The input in a |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Example
>>> import hya
>>> from omegaconf import OmegaConf
>>> conf = OmegaConf.create({"key": "${hya.np.array:[1, 2, 3]}"})
>>> conf.key
array([1, 2, 3])
>>> conf = OmegaConf.create({"key": "${hya.np.array:[1, 2, 3],float32}"})
>>> conf.key
array([1., 2., 3.], dtype=float32)
hya.torch ¶
Implement PyTorch resolvers for tensor creation and dtype handling.
This module provides OmegaConf resolvers that use PyTorch for tensor
operations and data type specifications. The resolvers are always
registered; the torch package is needed only when one of them is
called, and a missing package raises an error that says how to install
it. Users can create PyTorch tensors and specify dtypes directly from
configuration files.
hya.torch.to_tensor_resolver ¶
to_tensor_resolver(
data: Any, dtype: dtype | None = None
) -> Tensor
Implement a resolver to transform the input to a
torch.Tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
Specifies the data to transform in |
required |
dtype
|
dtype | None
|
The data type of the tensor, for example the output of
the |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
The input in a |
Example
>>> import hya
>>> from omegaconf import OmegaConf
>>> conf = OmegaConf.create({"key": "${hya.torch.tensor:[1,2,3,4,5]}"})
>>> conf.key
tensor([1, 2, 3, 4, 5])
>>> conf = OmegaConf.create({"key": "${hya.torch.tensor:[1,2,3],${hya.torch.dtype:float32}}"})
>>> conf.key
tensor([1., 2., 3.])
hya.torch.torch_dtype_resolver ¶
torch_dtype_resolver(target: str) -> dtype
Create a torch.dtype from its string representation.
This resolver converts a string name to the corresponding PyTorch data type object. The string must match an attribute name in the torch module that represents a dtype (e.g., "float32", "int64").
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
str
|
The target data type as a string. Must be a valid torch dtype attribute name such as "float", "float32", "float64", "int32", "int64", "bool", etc. The string should match the attribute name in torch (e.g., use "float32" not "torch.float32"). |
required |
Returns:
| Type | Description |
|---|---|
dtype
|
The corresponding torch.dtype object. |
Raises:
| Type | Description |
|---|---|
InterpolationResolutionError
|
If the target string doesn't correspond to a valid torch dtype. The message lists the available dtype names, sorted. |
Example
>>> import hya
>>> from omegaconf import OmegaConf
>>> conf = OmegaConf.create({"key": "${hya.torch.dtype:float}"})
>>> conf.key
torch.float32
>>> conf = OmegaConf.create({"key": "${hya.torch.dtype:int64}"})
>>> conf.key
torch.int64