Skip to content

Serialization

All model classes support save(filepath) / cls.load(filepath) round-trips. See the README for usage examples.

SerializationError

online_cp._serialization.SerializationError

Bases: Exception

Raised when a model cannot be serialized or deserialized.

Source code in src/online_cp/_serialization.py
class SerializationError(Exception):
    """Raised when a model cannot be serialized or deserialized."""

register_callable

online_cp._serialization.register_callable(name: str) -> Callable

Register a callable under name for serialization.

Use this decorator on module-level functions or classes that you want to pass as kernel, distance_func, or similar arguments to a model and then be able to save/load that model.

Parameters:

Name Type Description Default
name str

Unique registry key. Must be the same in the saving and loading process.

required

Examples:

>>> @register_callable("my_distance")
... def my_distance(X, y=None):
...     import numpy as np
...     from scipy.spatial.distance import cdist, pdist, squareform
...     if y is None:
...         return squareform(pdist(X))
...     return cdist(X, y)
Source code in src/online_cp/_serialization.py
def register_callable(name: str) -> Callable:
    """Register a callable under *name* for serialization.

    Use this decorator on module-level functions or classes that you want to
    pass as ``kernel``, ``distance_func``, or similar arguments to a model and
    then be able to save/load that model.

    Parameters
    ----------
    name : str
        Unique registry key.  Must be the same in the saving *and* loading
        process.

    Examples
    --------
    >>> @register_callable("my_distance")
    ... def my_distance(X, y=None):
    ...     import numpy as np
    ...     from scipy.spatial.distance import cdist, pdist, squareform
    ...     if y is None:
    ...         return squareform(pdist(X))
    ...     return cdist(X, y)
    """

    def decorator(fn: Callable) -> Callable:
        _CALLABLE_REGISTRY[name] = fn
        return fn

    return decorator