Common
neps.utils.common
#
Common utility functions used across the library.
MissingDependencyError
#
Bases: ImportError
Raise when a dependency is missing for an optional feature.
Source code in neps/utils/common.py
filter_instances
#
get_initial_directory
#
Find the initial directory based on its existence and the presence of the "previous_config.id" file.
PARAMETER | DESCRIPTION |
---|---|
pipeline_directory |
The current config directory. |
RETURNS | DESCRIPTION |
---|---|
Path
|
The initial directory. |
Source code in neps/utils/common.py
get_rnd_state
#
get_rnd_state() -> dict
Current state of the global random number generators in a devoctorized format.
Source code in neps/utils/common.py
get_searcher_data
#
Returns the data from the YAML file associated with the specified searcher.
PARAMETER | DESCRIPTION |
---|---|
searcher |
The name of the searcher.
TYPE:
|
searcher_path |
The path to the directory where the searcher defined YAML file is located. |
RETURNS | DESCRIPTION |
---|---|
dict[str, Any]
|
The content of the YAML file. |
Source code in neps/utils/common.py
get_value
#
Honestly, don't know why you would use this. Please try not to.
Source code in neps/utils/common.py
has_instance
#
instance_from_map
#
instance_from_map(
mapping: dict[str, Any],
request: str | list | tuple | type,
name: str = "mapping",
*,
allow_any: bool = True,
as_class: bool = False,
kwargs: dict | None = None
) -> Any
Get an instance of an class from a mapping.
PARAMETER | DESCRIPTION |
---|---|
mapping |
Mapping from string keys to classes or instances |
request |
A key from the mapping. If allow_any is True, could also be an object or a class, to use a custom object. |
name |
Name of the mapping used in error messages
TYPE:
|
allow_any |
If set to True, allows using custom classes/objects.
TYPE:
|
as_class |
If the class should be returned without beeing instanciated
TYPE:
|
kwargs |
Arguments used for the new instance, if created. Its purpose is to serve at default arguments if the user doesn't built the object.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
if the request is invalid (not a string if allow_any is False), or invalid key. |
Source code in neps/utils/common.py
is_partial_class
#
load_checkpoint
#
load_checkpoint(
directory: Path | str | None = None,
checkpoint_name: str = "checkpoint",
model: Module | None = None,
optimizer: Optimizer | None = None,
) -> dict | None
Load a checkpoint and return the model state_dict and checkpoint values.
PARAMETER | DESCRIPTION |
---|---|
directory |
Directory where the checkpoint is located. |
checkpoint_name |
The name of the checkpoint file.
TYPE:
|
model |
The PyTorch model to load.
TYPE:
|
optimizer |
The optimizer to load.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
dict | None
|
A dictionary containing the checkpoint values, or None if the checkpoint file does not exist hence no checkpointing was previously done. |
Source code in neps/utils/common.py
load_lightning_checkpoint
#
load_lightning_checkpoint(
checkpoint_dir: Path | str,
previous_pipeline_directory: Path | str | None = None,
) -> tuple[Path, dict] | tuple[None, None]
Load the latest checkpoint file from the specified directory.
This function searches for possible checkpoint files in the checkpoint_dir
and loads
the latest one if found. It returns a tuple with the checkpoint path and the loaded
checkpoint data.
PARAMETER | DESCRIPTION |
---|---|
previous_pipeline_directory |
The previous pipeline directory. |
checkpoint_dir |
The directory where checkpoint files are stored. |
RETURNS | DESCRIPTION |
---|---|
tuple[Path, dict] | tuple[None, None]
|
A tuple containing the checkpoint path (str) and the loaded checkpoint data (dict) or (None, None) if no checkpoint files are found in the directory. |
Source code in neps/utils/common.py
save_checkpoint
#
save_checkpoint(
directory: Path | str | None = None,
checkpoint_name: str = "checkpoint",
values_to_save: dict | None = None,
model: Module | None = None,
optimizer: Optimizer | None = None,
) -> None
Save a checkpoint including model state_dict and optimizer state_dict to a file.
PARAMETER | DESCRIPTION |
---|---|
directory |
Directory where the checkpoint will be saved. |
values_to_save |
Additional values to save in the checkpoint.
TYPE:
|
model |
The PyTorch model to save.
TYPE:
|
optimizer |
The optimizer to save.
TYPE:
|
checkpoint_name |
The name of the checkpoint file.
TYPE:
|
Source code in neps/utils/common.py
set_rnd_state
#
set_rnd_state(state: dict) -> None
Set the global random number generators to the given state.