Skip to content

fix: fall back to frame-alignment check when meta lacks fileSizeBytes - #4

Merged
tabedzki merged 1 commit into
mainfrom
tabedzki/fix/spikeglx-missing-filesizebytes
Aug 11, 2026
Merged

fix: fall back to frame-alignment check when meta lacks fileSizeBytes#4
tabedzki merged 1 commit into
mainfrom
tabedzki/fix/spikeglx-missing-filesizebytes

Conversation

@tabedzki

Copy link
Copy Markdown

🤖 Human guided, AI assisted PR (using this skill). AI text below. 🤖

Summary

Automated ingestion failed with:

KeyError: 'fileSizeBytes'

raised from SpikeGLX.validate_file, on two separate recordings from the same rig/mouse (emdia_nicky, 2026-03-30 and 2026-04-10, ~2 weeks apart), with normal successful recordings from the same setup on either side of both dates.

Investigation

Comparing the failing .ap.meta against a known-good one from the same probe/mouse (diff <(sort ...)) showed the failing file is missing exactly four fields relative to the good one:

fileSHA1=...
fileSizeBytes=...
fileTimeSecs=...
imErrFlags0_IS_CT_SR_LK_PP_SY_MS=...

Per SpikeGLX's metadata behavior, these fields are computed and written only as the last step of its shutdown sequence — after acquisition data is fully flushed to disk, and after computing a SHA1 checksum of the entire .ap.bin file (which can take real wall-clock time on a large file). Everything that comes before these fields in the .meta file (channel maps, IMRO table, geometry map, etc.) was present and well-formed in both failing cases.

Directly checked the .ap.bin file size against the expected per-sample-frame size (nSavedChans * 2 bytes for int16 data): it divided evenly with no remainder, and the implied duration (~57 min) was a plausible full recording — i.e. the actual recorded data is complete and not truncated. This is consistent with the SpikeGLX app being closed, crashed, or killed sometime after finishing the data write but before finishing its own finalization/checksum step, rather than any data corruption or transfer issue.

This is happening more than once on the same rig, so a hard crash in ingestion for every occurrence is worth avoiding — the data itself is fine.

Change

SpikeGLX.validate_file: when fileSizeBytes is absent from the meta file, fall back to checking that the actual .bin file size divides evenly into whole sample frames instead of raising immediately. A genuinely truncated/corrupted file (mid-frame cutoff) will still fail this check and still raise IOError. Logs a warning when this fallback path is used, so these recordings stay visible for follow-up rather than silently passing.

Test plan

  • Re-run ingestion for the two affected recordings (20260330_g0, 20260410_g0) and confirm EphysRecording populates successfully with a duration computed from the actual .bin file
  • Confirm a recording with a normal, complete .meta file (with fileSizeBytes present) still validates via the original exact-match path
  • Confirm a deliberately truncated/corrupted .bin file (not frame-aligned) still raises IOError

Assisted-by: ClaudeCode:claude-sonnet-5

SpikeGLX writes fileSizeBytes, fileTimeSecs, fileSHA1, and the
hardware error-flag summary only in the final step of its shutdown
sequence, after acquisition data is fully flushed to disk. If the
app is closed or crashes during that finalization step (e.g. while
still computing the SHA1 checksum of a large .bin file), the
resulting .meta file is missing these trailing fields even though
the recorded data itself is complete.

This previously caused a hard KeyError in validate_file, crashing
EphysRecording ingestion for otherwise-usable recordings (confirmed
on two live cases: the .ap.bin files were exact multiples of the
per-sample-frame size, with no evidence of truncation).

When fileSizeBytes is absent, fall back to checking that the file
size divides evenly into whole sample frames (nSavedChans * 2
bytes) instead of failing outright. A genuinely truncated/corrupted
file will not be frame-aligned and will still raise. Logs a warning
so these recordings remain visible for follow-up.

Assisted-by: ClaudeCode:claude-sonnet-5
@tabedzki
tabedzki merged commit 63c16d9 into main Aug 11, 2026
2 of 14 checks passed
tabedzki added a commit that referenced this pull request Aug 17, 2026
Reverts the frame-alignment fallback added in #4. The root cause
(SpikeGLX crashing before finishing its shutdown sequence, so
fileSizeBytes/fileTimeSecs/fileSHA1 were never written) has been
fixed upstream in SpikeGLX itself. These fields are relied on
downstream for values we need, so validate_file should go back to
treating their absence as an error rather than silently tolerating
it. Previously-affected files will be corrected by a separate
one-off script.

This reverts commit 0f2e909.

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