rheon.polars.ModelBank.closed_groups

ModelBank.closed_groups(spec: str | int | None = None, *, drop: bool = True) → DataFrame[source]

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

Use it with a spec that sets group_close, to read each group’s sums and fit once no further row can join it, and to keep a bank over an unbounded number of keys bounded. To have each chunk’s closed groups read for you, pass closed_groups= to fit_predict_batches(). For the sums of a group still open, use gram().

Parameters

spec

The spec whose closed groups to read, by name or by position. The default, None, reads every spec’s.

drop

True, the default, removes what it returns from the queue of closed groups not yet read, as a loop that reads after each chunk wants. False reads the queue and leaves it as it is.

Returns

A DataFrame with one row per (group, instance), an instance being the part of the model that fits one half-life, and one per Gram where a spec reads several: under target_gaps = "own_rows" targets missing on different rows are fitted from Grams of their own. The columns every row has:

spec, group, instance, half_life, lam, session

Which group closed, and its instance with the instance’s decay, as coef() gives it. session is the value of the session that ended under group_close = "session", and null under "monotone".

weight_sum, n_kish

As gram() reports them, at the moment the group closed.

rows_fed, rows_learned, clock_min, clock_max

The span’s own summary() counts, Int64 as there, and its clock range, in the clock column’s own dtype as there.

coef

On a Gram’s row, the coefficients of that Gram’s targets, and on a kind that reports coefficients, its coefficients. Null on the rows of a kind that reports none.

Then, for each kind of model among the specs that close groups, a block of columns, null on the rows of the other kinds:

kind

columns

what they hold

a kind that keeps a Gram

columns, means, comoments, targets, target_means, target_vars, target_weights, target_n_kish, cross_moments, means_by_target and cross_centered

gram()’s arrays, packed: comoments is the upper triangle with the diagonal, row by row (k(k+1)/2 numbers), and the per-target arrays are row-major (n_targets, k). rheon.gram.from_row() expands them, and hands back exactly what gram() would have returned for that group, so the exact solve on a closed group is rp.gram.solve(rp.gram.from_row(row))

an ew_cov with lags

lags and lag_comoments

gram()’s, the co-moments at each lag flattened lag by lag, row-major: L*k*k numbers for L lags

an ew_cov with pca

eig_vals and eig_vecs

the vectors signed to continue those of the previous closed row of the same (spec, instance): the previous group under "monotone", and the same group’s previous close under "session", never the previous chunk. So a sign flip between two rows is a real rotation, not an eigensolver’s arbitrary choice

an rcov

rcov, rcorr, rcov_n, rcov_kind, rcov_bandwidth_used, rcov_omega2, rcov_iv_sparse, rcov_iq and rcov_psd_repaired

rheon.spec.rcov() explains each

a marginal

pair_*

marginal()’s frame turned on its side: pair_feature and pair_target name the pairs, and every other marginal column becomes pair_<column>, with one entry per pair in the same order

A marginal column that is a list per pair there (the lag_corr_* columns with lags, and bin_edges, bin_n, bin_mean_y and bin_var_y with bins) is a list of lists here, present when any closing marginal asked for it. A nested list has no CSV form, so write a closed frame with them to parquet, or keep it as a frame.

Raises

KeyError

spec is a name the bank does not have.

IndexError

spec is a position the bank does not have.

ValueError

spec is None and the specs that close groups read clocks of different dtypes, which one clock column cannot hold. The message names them, and nothing is removed from the queue. Read them one spec at a time, as for groups().

Examples

This code reads df from Example data:

blocks = rp.spec.ew_cov(
    "cov", features=["x0", "x1"], lam=1.0, group="block", group_close="monotone"
)
by_block = df.with_columns(block=pl.int_range(pl.len()) // 100)   # 4 blocks of 100 rows
bank = rp.ModelBank([blocks])
bank.fit(by_block)
closed = bank.closed_groups()               # blocks 0 to 2: block 3 is still open
print(closed.select("group", "rows_fed").rows())
g = rp.gram.from_row(closed.head(1))        # block 0's sums, as gram() gives them
print(g["comoments"].shape)
[('0', 100), ('1', 100), ('2', 100)]
(2, 2)

See also

Notes

A spec with group_close gives a group’s sums at the moment the bank can prove no further row will join it, and then drops its model. Under "monotone" that is at the end of the chunk in which a larger key arrives, and under "session" when the group’s session ends. Without group_close, every key ever seen stays in memory. A closed group’s model is freed when the group closes, whether or not the queue is read. Its rows (see Returns) stay in the queue until they are read, so read it between chunks. What is unread is saved with the state, so a loop that saves between chunks loses no row. predict() never closes a group.