polars_online.ModelBank.load

classmethod ModelBank.load(source: str | PathLike[str] | bytes | IO[bytes], specs: Iterable[dict[str, Any]] | None = None, *, threads: int | None = None) → ModelBank[source]

Load a bank from a state file save() wrote, on this OS or any other.

Use it to resume a saved bank, or to serve predictions from it with predict(). The file carries the bank’s specs, so you need not give them. For bytes from a store that is not a file, this call takes them as well, as load_bytes() does.

Parameters

source

A path, the bytes save_bytes() gave, or a file object opened for binary reading, such as open(..., "rb") or an io.BytesIO, as Polars’ read_* functions take any of the three.

specs

The specs you expect the file to hold. Given, the load is refused unless they are the file’s, which is how a job that resumes checks that the state it found belongs to the bank it is about to run. None, the default, takes the file’s specs unchecked.

threads

The loaded bank’s threads, as ModelBank takes it. The file carries no count, so a bank saved on eight threads and loaded with none runs on the process’s shared threads, with the same numbers.

Returns

A ModelBank holding the saved state, which goes on from the row after the last one it learned before the save.

Raises

OSError

The file cannot be read, such as FileNotFoundError for a path that does not exist.

TypeError

source is not a path, bytes or a file object, or the file object gives text rather than bytes. threads is not an integer.

ValueError

The file can be read but is not a bank this build loads:

  • it is not a bank’s state file, or it is damaged in a way the checks find (see Notes);

  • a newer build wrote it, under a format or state version above this build’s;

  • a build of 0.13.0 or earlier wrote it, below the oldest state version this build loads, which is 1.0’s (polars_online.schema_version() gives the current one): fit the bank again from its input;

  • a state contradicts its own spec, such as an sgd state without the scaler its standardize needs;

  • specs are given and differ from the file’s;

  • threads is below 1, or above 1024 (or the machine’s core count, where that is larger).

UnstableWarning

Under POLARS_ONLINE_WARN_UNSTABLE=1, for a file holding a formula target, as save() says.

Examples

This code reads df and today from Example data:

spec = po.spec.ewridge("ridge", targets=["y"], features=["x0", "x1"], half_life=100.0)
saved = po.ModelBank([spec])
saved.fit_predict(df)
saved.save("bank.state")

bank = po.ModelBank.load("bank.state", specs=[spec])   # refused if the specs differ
bank.fit_predict(today)                                # and the stream goes on
print(bank.rows_fed)
800

See also

Notes

The file carries no checksum. The load checks the file’s envelope (its magic number, its format and state versions, its number of states) and each state’s shape against its spec, every vector at the length its spec gives it. A damaged file that still decodes and passes those checks loads: a flipped bit in a stored number is a slightly different state, consistent with itself, and the model goes on from it. Keep a state file where it cannot be damaged, or keep a checksum beside it.