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, passclosed_groups=tofit_predict_batches(). For the sums of a group still open, usegram().Parameters
specThe spec whose closed groups to read, by name or by position. The default,
None, reads every spec’s.dropTrue, the default, removes what it returns from the queue of closed groups not yet read, as a loop that reads after each chunk wants.Falsereads the queue and leaves it as it is.
Returns
A
DataFramewith 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: undertarget_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,sessionWhich group closed, and its instance with the instance’s decay, as
coef()gives it.sessionis the value of the session that ended undergroup_close = "session", and null under"monotone".weight_sum,n_kishAs
gram()reports them, at the moment the group closed.rows_fed,rows_learned,clock_min,clock_maxThe span’s own
summary()counts,Int64as there, and its clock range, in the clock column’s own dtype as there.coefOn 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_targetandcross_centeredgram()’s arrays, packed:comomentsis the upper triangle with the diagonal, row by row (k(k+1)/2numbers), and the per-target arrays are row-major(n_targets, k).rheon.gram.from_row()expands them, and hands back exactly whatgram()would have returned for that group, so the exact solve on a closed group isrp.gram.solve(rp.gram.from_row(row))an
ew_covwithlagslagsandlag_comomentsgram()’s, the co-moments at each lag flattened lag by lag, row-major:L*k*knumbers forLlagsan
ew_covwithpcaeig_valsandeig_vecsthe 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 choicean
rcovrcov,rcorr,rcov_n,rcov_kind,rcov_bandwidth_used,rcov_omega2,rcov_iv_sparse,rcov_iqandrcov_psd_repairedrheon.spec.rcov()explains eacha
marginalpair_*marginal()’s frame turned on its side:pair_featureandpair_targetname the pairs, and every othermarginalcolumn becomespair_<column>, with one entry per pair in the same orderA
marginalcolumn that is a list per pair there (thelag_corr_*columns withlags, andbin_edges,bin_n,bin_mean_yandbin_var_ywithbins) is a list of lists here, present when any closingmarginalasked 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
KeyErrorspecis a name the bank does not have.IndexErrorspecis a position the bank does not have.ValueErrorspecisNoneand 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 forgroups().
Examples
This code reads
dffrom 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
fit_predict_batches(), whoseclosed_groups=reads the queue after each chunk.rheon.gram.from_row(), a closed row’s sums asgram()gives them.Close groups and read each one’s row, a worked example.
Notes
A spec with
group_closegives 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. Withoutgroup_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.