Skip to content

feat: backfill TowersBlock.reward_scale as float from ViRMEn logs - #186

Merged
tabedzki merged 5 commits into
masterfrom
claude/towerblocks-scalefactor-float-7a480f
Nov 25, 2026
Merged

tabedzki merged 5 commits into
masterfrom
claude/towerblocks-scalefactor-float-7a480f

Conversation

@tabedzki

@tabedzki tabedzki commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Summary

behavior.TowersBlock.reward_scale was tinyint, but ViRMEn moves the reward scale in 0.2 steps (processKeypress.m, autoAdjustReward.m), so 1.2 and 1.8 were stored as integers. Once the column is widened to float (done separately in the DB), this script restores the real values. It updates only that one column. It does not delete and repopulate, which would cascade into TowersBlock.Trial and its extstorage blobs.

backfill_reward_scale.py [--user ID] [--search-dir DIR] [--path-map CSV] [--commit]
  for each session in scope
    resolve_log: --path-map → recorded path → --search-dir
      off-path file must match log.animal.name + session.start (±2 min)
    for each block
      classify(db, file):
        ok        db == round/trunc(file) and first_trial agrees → update1
        mismatch  anything else                                 → report, never write
        fallback  db == 0 (catch branch in TowersBlock.m:89)    → only with --include-fallback
  --write-missing CSV: unresolved sessions, fill in path, pass back as --path-map

The checking logic is in u19_pipeline/utils/reward_scale_backfill.py, which doesn't need a DB, so it can be tested.

This PR also brings the Python mirrors in line with the MATLAB definitions, which Python ingestion will eventually need:

 acquisition.SessionStarted
+  new_remote_path_behavior_file
 acquisition.Session
+  stimulus_set; defaults num_trials=-1, is_bad_session=0, session_comments=""
+acquisition.SessionBlock
+acquisition.SessionBlockTrial
 behavior.TowersSession   blobs → =null
 behavior.TowersBlock
-  block : tinyint
+  -> acquisition.SessionBlock
+  main_level, trial_duration_median; sublevel=null; reward_scale: float
 behavior.TowersBlock.Trial
+  -> acquisition.SessionBlockTrial
-  trial_time/collision/position/velocity/sensor_dots : longblob
+  ... : blob@extstorage

Evidence

  • Before: pytest tests/utils → ImportError: cannot import name 'reward_scale_backfill'
    After: 71 passed; full suite on the merge with master: 695 passed, 6 skipped (skips are missing suite2p/sklearn). Covers rounding at .5 and negatives, 0 as a fallback vs. a valid truncation, None/NaN/inf/empty/multi-element scalars, single-block and single-trial logs squeezed to dicts, first_trial mismatches, two sessions on the same day separated by start time, missing identity fields, duplicate basenames, and the CSV round trip.

Not verified: there's no DB access from the dev host, so the script hasn't run against real data, and resolve_log has no tests (importing the script needs a DB config). First real run should be --user <id> --limit 20 as an audit.

Open question: Python's TowersBlock.Trial may resolve to a different MySQL table name than MATLAB's TowersBlockTrial. Check with SHOW TABLES FROM u19_behavior LIKE '%towers_block%'. The script doesn't touch Trial.

Merge Danger

Door: two-way

Merging writes nothing. The script is read-only unless you pass --commit. The definition changes only affect Python reads, because DataJoint 0.14 loads the heading for an existing table from the DB.

Blast Radius: low

Python code that inserts through these definitions now gets MATLAB's defaults. The Trial blobs would go to extstorage instead of MySQL.

reward_scale was declared tinyint, so ViRMEn's fractional reward scales
(0.2 steps) were collapsed to integers on insert. After widening the
column to float, scripts/backfill_reward_scale.py re-reads the behavior
logs and updates only that column, leaving TowersBlock.Trial untouched.

Each block is verified before writing: the stored value must be the
rounded or truncated file value and first_trial must agree. Logs found
outside the recorded path (via --search-dir or --path-map) must match the
session's subject and start time. Read-only unless --commit is passed.

Also syncs the Python mirrors of acquisition and behavior with the MATLAB
definitions: adds SessionBlock/SessionBlockTrial, new_remote_path_behavior_file,
missing Session/TowersBlock attributes, and extstorage blobs on
TowersBlock.Trial.
master moved to a stricter ruff.toml (line-length 88, more rule sets)
after this branch was cut. Reformat, and fix the new findings:

- Report searched log files that fail to load instead of silently
  skipping them, so an unresolved session isn't blamed on the identity
  check alone (S112).
- Mark the naive datetimes as intentional: the ViRMEn clock vector and
  the MySQL datetime it is compared against are both rig-local (DTZ001).
- Parenthesize an implicit string concatenation, use min() over sorted()[0].
uv audit fails on master's lock file:

- dnspython 2.8.0 -> 2.9.0 (GHSA-45c3-73f7-pv4m)
- msgpack 1.2.2 -> 1.2.4 (GHSA-j586-36cw-2gc2)
- tornado 6.5.10 -> 6.6 (GHSA-pw9p-v7r6-gx4p, GHSA-r4j5-9xpp-3985,
  GHSA-pcg2-g6cj-q92c)

Dependabot's #183 covers only the first two. tornado 6.6 (2026-10-21)
is now outside the 14-day exclude-newer window. No other locked
versions change.
…o claude/towerblocks-scalefactor-float-7a480f

# Conflicts:
#	uv.lock
@tabedzki
tabedzki merged commit 1b3512e into master Nov 25, 2026
5 checks passed
@tabedzki
tabedzki deleted the claude/towerblocks-scalefactor-float-7a480f branch November 25, 2026 15:44
tabedzki added a commit that referenced this pull request Nov 25, 2026
…Session

#186 added stimulus_set to acquisition.Session, but the live
u19_acquisition.session table has no such column and MATLAB never fills it,
so every insert or fetch of Session fails. behavior.TowersSession.stimulus_set
is a real column and stays. Add DB-free tests for both.

Assisted-by: ClaudeCode:claude-sonnet-5-5
tabedzki added a commit that referenced this pull request Nov 25, 2026
…Session

#186 added stimulus_set to acquisition.Session, but the live
u19_acquisition.session table has no such column and MATLAB never fills it.
Reads were unaffected, because DataJoint takes an existing table's heading
from the database; the definition was just wrong. behavior.TowersSession's
stimulus_set is a real column and stays. Add DB-free tests for both.

Assisted-by: ClaudeCode:claude-sonnet-5-5
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.

1 participant