rheon.polars.ModelBank

class rheon.polars.ModelBank(specs: Iterable[dict[str, Any]], *, threads: int | None = None)[source]

Bases: object

A set of models fitted together over the same rows, fed one chunk at a time.

Use a model bank when you feed the rows yourself, from frames you hold or a query you hand it to read in chunks, and when you want to read, save or serve what it has learned between chunks. To run the same bank inside a Polars query, use lf.rheon.fit_predict(specs), and to run it outside Python, from a file to a file, use the rheon command line.

Parameters

specs

A list of the specs the rheon.spec builders make, at least one, with distinct names. The bank keeps one model for each spec and each group of the spec’s group column, and adds one output column per spec to each chunk it learns.

threads

How many threads the bank’s work runs on. None, the default, is the process’s shared threads, whose number RHEON_MAX_THREADS sets when they are first used (unset or 0: one thread per core). An integer n is n threads, which every bank given the same n shares and which end with the last of those banks, unless the process’s shared threads already number n, in which case the bank runs on those. The output and the saved state are the same to the bit at any count (see Notes). The count is not saved with the state, so load() takes its own. A spec’s gram_threads caps the threads it uses to learn each row into its Gram, and marginal’s shards splits its pair work across the bank’s threads. Neither adds a thread.

Raises

ValueError

When:

  • specs is empty, two specs share a name, or an item is not a spec (the message names the field);

  • a spec’s settings are refused, as its builder would refuse them;

  • threads is below 1 (None, not 0, gives the shared threads: one per core unless RHEON_MAX_THREADS is set), or above 1024 (or the machine’s core count, where that is larger), refused before any thread starts;

  • from the bank’s first call that runs, with threads=None: RHEON_MAX_THREADS is not a whole number, or is past the same bound.

WrongTypeError

specs is one spec, a string or bytes, in place of a list of specs. It is both a TypeError and a ValueError.

TypeError

specs is not iterable, or threads is not an integer, a bool included.

RuntimeError

From any method: the bank is in use on another thread (see Notes).

Examples

This code reads df from Example data:

spec = rp.spec.ewridge("ridge", targets=["y"], features=["x0", "x1"], half_life=100.0)
bank = rp.ModelBank([spec])
for chunk in df.iter_slices(100):          # chunks in stream order
    out = bank.fit_predict(chunk)          # the chunk, with the output column "ridge" added
coefs = bank.coef()                        # what it has learned, read without the rows
bank.save("bank.state")                    # a state file, to resume from on any OS
print(bank)
ModelBank(['ridge'], groups=1, rows_fed=400)

See also

Notes

A bank feeds each chunk to its models in stream order. Every prediction is made before the model learns the row it is for. Memory holds the models’ state and one chunk, however long the stream, and the numbers do not depend on how the stream is cut into chunks.

The output and the state do not depend on threads: each piece of work is the same arithmetic whichever thread runs it, and the work is divided by the data, not by the number of threads.

A bank is one ordered stream, so two threads cannot use it at once. A method that finds the bank in use on another thread raises RuntimeError, so that two calls never interleave their rows. Without the check they could, since fit_predict() releases Python’s global interpreter lock while it works. predict() learns nothing, so any number of predict calls may run at once. Only a call that learns, in flight, refuses them, and they refuse it.

Methods and properties

specs

The specs this bank runs, with every field filled in.

threads

The threads count the bank was made or loaded with.

rows_fed

The rows fed to the bank so far, over every chunk and group.

groups([spec])

The groups the bank holds a model for, one row for each spec and group.

drop_groups(keys[, spec])

Remove the models of these groups, in every spec or in one.

skip_learned(...)

The rows of a frame or query that the bank has not learned.

fit_predict(df)

Learn a frame's rows, and return them with each spec's output.

predict(df)

Predict a frame's rows from the bank as it stands, learning nothing.

fit_predict_arrow(df)

Learn a frame's rows, and return each spec's output as an Arrow array.

predict_arrow(df)

Predict a frame's rows without learning, and return each spec's output as an Arrow array.

fit_predict_batches(batches, *[, ...])

Learn a stream one chunk at a time, and yield each chunk with each spec's output.

fit(batches, *[, closed_groups, chunk_size])

Learn every row of a query, a frame or an iterator of frames, and keep only the state.

coef([spec, group])

Return the coefficients of every fit the bank holds, one row per coefficient.

last_row([spec, group])

Return each model's output on the last row it did not skip, one row per spec and group.

summary([spec, group])

Return what each model has been fed, one row per spec and group.

describe([spec, group])

Return statistics of each input column each model was fed, one row per column.

gram(spec[, group, dtype, layout])

Return the Gram behind a spec's fit, per group and instance, as numpy arrays.

marginal(spec[, group])

Return the feature and target pairs a marginal spec keeps, one row per pair.

audit([spec, group, table, pooled])

Return what an audit spec counted, one row per column, pair of columns or clock.

closed_groups([spec, drop])

Return the groups that have closed and not yet been read, oldest first, as one frame.

check([spec, group])

Return findings about the data each model was fed, one row per problem, the worst first.

solve_failures([spec, group])

Return how many of each model's solves needed help, one row per spec and group.

output_fields

Each spec's name, mapped to the names of its output column's fields, in order.

save(file)

Write the bank's state to a file that load() reads on any supported OS.

save_bytes()

The bytes save() writes, for a store that is not a file.

write_json(-> str)

Write the bank's state as JSON, which can be read without this library.

load(source[, specs, threads])

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

load_bytes(data[, specs, threads])

Load a bank from the bytes save_bytes() gave.