milia.core.io#

Saving and loading fitted estimators.

Every milia estimator pickles cleanly, so persistence is joblib plus a small JSON manifest recording what was saved and with which milia version.

Functions

load(path)

Load an estimator saved with save().

save(estimator, path)

Save a fitted estimator to a directory.

milia.core.io.save(estimator, path)#

Save a fitted estimator to a directory.

Writes the estimator with joblib as model.joblib and a manifest.json recording its class, module, the milia version and the time it was saved.

Parameters:
  • estimator (Any) – Any picklable estimator.

  • path (str | Path) – Target directory, created if missing.

Return type:

Path

Returns:

The directory written to.

Examples

>>> import tempfile, numpy as np
>>> from milia import make
>>> model = make("detector", "ecod").fit(np.random.default_rng(0).normal(size=(50, 2)))
>>> folder = save(model, tempfile.mkdtemp())
>>> sorted(p.name for p in folder.iterdir())
['manifest.json', 'model.joblib']
milia.core.io.load(path)#

Load an estimator saved with save().

Only load directories you trust: joblib files can execute code when loaded.

Parameters:

path (str | Path) – Directory written by save().

Return type:

Any

Returns:

The fitted estimator.

Examples

>>> import tempfile, numpy as np
>>> from milia import make
>>> X = np.random.default_rng(0).normal(size=(50, 2))
>>> model = make("detector", "ecod").fit(X)
>>> again = load(save(model, tempfile.mkdtemp()))
>>> bool(np.allclose(again.score_samples(X), model.score_samples(X)))
True