Skip to content

parameters

parameters

Abstract base class for parameter collections with serialization helpers.

Parameters is the common superclass for every configuration group in config/ (DataParameters, TrainingHyperparams, GenerateParameters, etc.). It extends pydantic.BaseModel with:

  • Recursive iteration over nested parameter groups.
  • Name-based lookup across the full parameter tree (get(), has()).
  • YAML / JSON round-trip serialization (from_yaml(), to_yaml(), from_json()).

Classes:

Name Description
Parameters

Abstract base for parameter collections used throughout the config layer.

Parameters pydantic-model

Bases: BaseModel

Abstract base for parameter collections used throughout the config layer.

Subclasses define typed fields (e.g. int, Literal["auto"] | float) and inherit recursive iteration, name-based lookup, and YAML / JSON serialization from this class.

Config:

  • default: pydantic_model_config

get(name, default=None)

Look up a parameter or sub-group by name across the full tree.

Checks direct attributes first, then walks nested groups recursively.

Parameters:

Name Type Description Default
name str

Field name to search for.

required
default Any

Value returned when name is not found.

None

Returns:

Type Description
DataT | Any | None

The parameter value or sub-group if found, otherwise default.

Source code in src/nemo_safe_synthesizer/configurator/parameters.py
def get(self, name: str, default: Any = None) -> DataT | Any | None:
    """Look up a parameter or sub-group by name across the full tree.

    Checks direct attributes first, then walks nested groups recursively.

    Args:
        name: Field name to search for.
        default: Value returned when ``name`` is not found.

    Returns:
        The parameter value or sub-group if found, otherwise ``default``.
    """
    if (group := getattr(self, name, None)) is not None:
        return group
    for param in self._iter_parameters(recursive=True):
        if name in param:
            return param.get(name)
    return default

has(name)

Check whether name exists anywhere in the parameter tree.

Unlike get(), this does not conflate falsy values (0, "", False, None) with absence.

Parameters:

Name Type Description Default
name str

Field name to search for.

required

Returns:

Type Description
bool

True if the parameter or sub-group exists.

Source code in src/nemo_safe_synthesizer/configurator/parameters.py
def has(self, name: str) -> bool:
    """Check whether ``name`` exists anywhere in the parameter tree.

    Unlike ``get()``, this does not conflate falsy values (``0``, ``""``,
    ``False``, ``None``) with absence.

    Args:
        name: Field name to search for.

    Returns:
        ``True`` if the parameter or sub-group exists.
    """
    if getattr(self, name, None) is not None:
        return True
    for param in self._iter_parameters(recursive=True):
        if name in param:
            return True
    return False

from_yaml_str(raw) classmethod

Construct an instance from a YAML-formatted string.

Parameters:

Name Type Description Default
raw str

YAML content as a string.

required

Returns:

Type Description
Self

A validated Parameters instance.

Source code in src/nemo_safe_synthesizer/configurator/parameters.py
@classmethod
def from_yaml_str(cls, raw: str) -> Self:
    """Construct an instance from a YAML-formatted string.

    Args:
        raw: YAML content as a string.

    Returns:
        A validated ``Parameters`` instance.
    """
    data = yaml.safe_load(raw)
    return cls.model_validate(data)

from_json(path, overrides=None) classmethod

Load from a JSON file, optionally applying field overrides.

Parameters:

Name Type Description Default
path PathT

Path to the JSON file.

required
overrides dict | None

Field-level overrides applied via model_copy(update=...).

None

Returns:

Type Description
Self

A validated Parameters instance.

Source code in src/nemo_safe_synthesizer/configurator/parameters.py
@classmethod
def from_json(cls, path: PathT, overrides: dict | None = None) -> Self:
    """Load from a JSON file, optionally applying field overrides.

    Args:
        path: Path to the JSON file.
        overrides: Field-level overrides applied via ``model_copy(update=...)``.

    Returns:
        A validated ``Parameters`` instance.
    """
    with open(path, "r") as f:
        data = json.load(f)
    params = cls.model_validate(data)
    if overrides:
        params = params.model_copy(update=overrides)
    return params

from_yaml(path, overrides=None) classmethod

Load from a YAML file, optionally applying field overrides.

Parameters:

Name Type Description Default
path PathT

Path to the YAML file.

required
overrides dict | None

Field-level overrides applied via model_copy(update=...).

None

Returns:

Type Description
Self

A validated Parameters instance.

Raises:

Type Description
FileNotFoundError

If path does not exist.

Source code in src/nemo_safe_synthesizer/configurator/parameters.py
@classmethod
def from_yaml(cls, path: PathT, overrides: dict | None = None) -> Self:
    """Load from a YAML file, optionally applying field overrides.

    Args:
        path: Path to the YAML file.
        overrides: Field-level overrides applied via ``model_copy(update=...)``.

    Returns:
        A validated ``Parameters`` instance.

    Raises:
        FileNotFoundError: If ``path`` does not exist.
    """
    pth = Path(path)
    if not pth.exists():
        raise FileNotFoundError(f"File {pth} does not exist")
    with pth.open("r") as f:
        data = yaml.safe_load(f)
    params = cls.model_validate(data)
    if overrides:
        params = params.model_copy(update=overrides)
    return params

to_yaml(path, exclude_unset=True)

Serialize this instance to a YAML file.

Parameters:

Name Type Description Default
path PathT

Destination file path.

required
exclude_unset bool

If True, omit fields that were never explicitly set.

True
Source code in src/nemo_safe_synthesizer/configurator/parameters.py
def to_yaml(self, path: PathT, exclude_unset: bool = True) -> None:
    """Serialize this instance to a YAML file.

    Args:
        path: Destination file path.
        exclude_unset: If ``True``, omit fields that were never explicitly set.
    """
    with open(path, "w") as f:
        j = json.loads(self.model_dump_json(exclude_unset=exclude_unset))
        yaml.safe_dump(j, f)

from_params(**kwargs) classmethod

Construct a Parameters instance from keyword arguments.

Parameters:

Name Type Description Default
**kwargs

Parameter values passed to model_validate.

{}

Returns:

Type Description
Self

A validated Parameters instance.

Source code in src/nemo_safe_synthesizer/configurator/parameters.py
@classmethod
def from_params(cls, **kwargs) -> Self:
    """Construct a ``Parameters`` instance from keyword arguments.

    Args:
        **kwargs: Parameter values passed to ``model_validate``.

    Returns:
        A validated ``Parameters`` instance.
    """
    return cls.model_validate(kwargs)

get_auto_params()

Yield parameters whose current value is the "auto" sentinel.

Source code in src/nemo_safe_synthesizer/configurator/parameters.py
def get_auto_params(self) -> Iterable[Any]:
    """Yield parameters whose current value is the ``"auto"`` sentinel."""
    for param in self:
        if param == "auto":
            yield param