manager.trainer.get_last_model

manager.trainer.get_last_model(model_name, model_dir=None)

Get the latest trained model from the cache.

Parameters

Name Type Description Default
model_name str Name of the model (e.g., ‘lgbm’, ‘xgb’). required
model_dir Optional[Union[str, Path]] Directory where models are stored. If None, defaults to the library’s cache home. None

Returns

Name Type Description
int A tuple (iteration, model_instance). If no model is found on disk
Any (cache directory missing, no matching files, or no parseable
tuple[int, Any] iteration numbers), returns (-1, None) — a legitimate
tuple[int, Any] “fresh install, no model yet” state.

Raises

Name Type Description
OSError If the latest model file exists on disk but cannot be deserialised (corrupt joblib, version mismatch, etc.).

Examples

import tempfile
from spotforecast2_safe.manager.trainer import get_last_model

# Empty directory — no model files yet.
with tempfile.TemporaryDirectory() as tmpdir:
    iteration, model = get_last_model("lgbm", model_dir=tmpdir)
    print(iteration, model)
    assert iteration == -1
    assert model is None
-1 None