Skip to content

Add stratified sampling across block ids and shared block-boundary logic - #85

Open
ahmedmohammed107 wants to merge 1 commit into
mainfrom
72-stratified-sampling
Open

Add stratified sampling across block ids and shared block-boundary logic#85
ahmedmohammed107 wants to merge 1 commit into
mainfrom
72-stratified-sampling

Conversation

@ahmedmohammed107

Copy link
Copy Markdown
Collaborator

Fixes #<ISSUE_NUMBER>.

Adds a block_id-aware notion of "distinct collection run" to agent_inferences, and uses it to (a) guarantee sampled training windows never span a gap or context change, and (b) make sample_batch() sample in a stratified, balanced way across blocks within the lookback window instead of treating it as one undifferentiated pool. The boundary rule itself (is_new_block()) is centralized in smocs/utils/block_boundary.py and shared verbatim between DBManager (on write) and the autoencoder inference thread's streaming buffer (in memory), so the two can never drift apart.

Also fixes an undocumented bug where eval_model() raced training_loop()'s post-training count bump and silently skipped evaluation on roughly half of all cycles — factored out via a new _fetch_and_preprocess_batch() helper that eval_model() now calls directly instead of get_training_data().

See the linked issue for full details. New unit test coverage added in test_db_api.py.

…k-boundary logic

- Within sampling_lookback, sample_batch now performs balanced
  stratified sampling across every unique block_id present, rather
  than sampling the lookback window as one undifferentiated pool.
  Remainders are allocated to the most recent (largest) block ids
  first, so the newest data is favored when the requested batch size
  doesn't divide evenly across blocks.
- agent_inferences gains 'block_id' and 'context' columns (context is
  a single, generic BLOB shared by every agent type - see init.sql).
  DBManager tracks the most recently written row (host, context,
  block_id) via refresh_latest_row_cache()/_load_latest_row() and
  advances block_id through _compute_block_id() whenever the gap since
  the previous sample exceeds the configured max_gap_seconds, or the
  context value changes.
- Added smocs/utils/block_boundary.py: is_new_block(), the shared rule
  for what counts as a block boundary, now used identically by both
  DBManager (when assigning block_id on write) and the inference
  thread (when deciding whether a candidate window may be considered
  at all) - a window is never allowed to cross a block_id boundary in
  either training or inference. Both the ingest thread's DBManager
  (which must seed its block-tracking cache from history on startup)
  and the inference thread (which reads max_gap_seconds to know when a
  candidate window has crossed a boundary) are wired up to this here.

This builds on the already-merged rename-window-terminology and
77-mysql-db-naming-convention branches; the window/flattened_window
terminology throughout is inherited from the former rather than
introduced here.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add stratified sampling across block ids and shared block-boundary logic

1 participant