Skip to content

load_learners

Loads the learners from a specified directory.

Parameters:

Name Type Description Default
path Union[str, Path]

Path to the directory where models are stored.

required
verbose bool

Prints loaded models. Defaults to False.

False

Returns:

Name Type Description
dict dict

Dictionary containing loaded learners.

Raises:

Type Description
FileNotFoundError

If the specified directory does not exist.

Source code in periomod/wrapper/_wrapper.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def load_learners(path: Union[str, Path], verbose: bool = False) -> dict:
    """Loads the learners from a specified directory.

    Args:
        path (Union[str, Path]): Path to the directory where models are stored.
        verbose (bool): Prints loaded models. Defaults to False.

    Returns:
        dict: Dictionary containing loaded learners.

    Raises:
        FileNotFoundError: If the specified directory does not exist.
    """
    path = Path(path)
    if not path.is_absolute():
        path = Path.cwd() / path

    if not path.exists():
        raise FileNotFoundError(f"The directory {path} does not exist.")

    learners_dict = {}
    for model_file in path.glob("*.pkl"):
        model = joblib.load(model_file)
        learners_dict[model_file.stem] = model
        if verbose:
            print(f"Loaded model {model_file.stem} from {model_file}")

    return learners_dict