rheon.polars.stream.save_checkpoint

rheon.polars.stream.save_checkpoint(path: str | PathLike[str], stages: Mapping[str, ModelBank | bytes | str | PathLike[str]]) → None[source]

Save the states of a run’s stages as one file, written in full or not at all.

A run that chains stateful stages, such as refresh_time(), then with_windows(), then a bank, keeps a state for each. Saved as separate files, a crash between two saves leaves stages that stopped at different rows, and a stream processor that commits its input’s offsets after the saves has no single point to resume from. A checkpoint holds every stage’s state in one file. The file is written beside path under another name, flushed to the disk, and renamed over path, so a reader finds the old checkpoint or the new one and never a part of either. Once the call returns, the new one survives a power loss. Commit the offsets after it returns.

Parameters

path

The file to write. Its folder must exist.

stages

Each stage’s state by a name of your choosing, in the order to keep them: a ModelBank, whose state is saved; the bytes of a state, such as bank.save_bytes(); or the path of a state file a stage wrote, such as with_windows(save_state=...). At least one, and no name twice.

Returns

None. load_checkpoint() reads the stages back.

Raises

ValueError

No stage is given, a name is empty or given twice, or a state is not one this version of rheon saves, is damaged, or is itself a checkpoint. The file at path is left as it was.

FileNotFoundError

The folder of path is not there, or a stage’s path is not.

IsADirectoryError

path is a folder.

OSError

The file cannot be written; the file at path is left as it was.

Examples

import tempfile, os
spec = rp.spec.ewridge("m", targets=["y"], features=["x"], half_life=50.0)
df = pl.DataFrame({"x": [0.0, 1.0, 2.0, 3.0], "y": [0.1, 1.9, 4.2, 5.8]})
bank = rp.ModelBank([spec])
bank.fit_predict(df)
path = os.path.join(tempfile.mkdtemp(), "run.checkpoint")
rp.stream.save_checkpoint(path, {"bank": bank})
stages = rp.stream.load_checkpoint(path)
print(list(stages))
print(rp.ModelBank.load_bytes(stages["bank"]).coef().height > 0)
['bank']
True