Avoid loading all timestamps into memory using DataChunkIterator #136
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR implements memory optimization for timestamp handling in SpikeGadgets .rec file conversion to avoid loading all timestamps into memory at once, which becomes problematic for long recordings.
Problem
For 17-hour recordings at 30kHz sampling rate, the current implementation loads all timestamps into memory using
np.concatenate(), requiring approximately 14.7 GB of RAM just for timestamps:This causes memory exhaustion and makes conversion of long recordings impossible.
Solution
1. TimestampDataChunkIterator
Added a new
TimestampDataChunkIteratorclass that extendsGenericDataChunkIteratorto lazily load timestamps in chunks rather than loading the entire array into memory.2. Smart Timestamp Handling
Enhanced
RecFileDataChunkIteratorwith intelligent timestamp detection:rateparameter inElectricalSeries(most memory efficient)TimestampDataChunkIteratorwithH5DataIO3. Backward Compatibility
All existing APIs are preserved with new methods:
get_sampling_rate(): Returns sampling rate for regular timestampsget_timestamps_chunked(): Returns chunked timestamp iteratorget_timestamps(): Backward-compatible method that loads on demandMemory Impact
For a 17-hour recording at 30kHz:
Files Modified
convert_ephys.py: Core timestamp optimization implementationconvert_analog.py: Applied same optimization to analog dataconvert.py: Updated main conversion pipelineconvert_intervals.py: Updated to use new timestamp access methodtest_behavior_only_rec.py: Updated test for compatibilityTesting
The implementation includes comprehensive testing:
TimestampDataChunkIteratorfunctionalityElectricalSeriescreationFixes #47.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.