Skip to content

Write all channels of a stream with one HDF5 write (writeContinuousDataBatch) - #1

Merged
oruebel merged 1 commit into
add-aqnwb-v0.4from
batch-writes
Nov 20, 2026
Merged

oruebel merged 1 commit into
add-aqnwb-v0.4from
batch-writes

Conversation

@oruebel

@oruebel oruebel commented Oct 27, 2026 •

Copy link
Copy Markdown

Context

@jsiegle I had prioritized several needed updates in AqNWB but am now starting to look at updating the nwb-format plugin for OpenEphys to use AqNWB and bring the plugin up-to-date with the latest version of NWB. This PR is currently in draft mode because it depends on several changes/fixes in AqNWB that are in pending PRs, so we should hold off with merging until after I have made an AqNWB release with the required changes. However, I figured it would be useful to start the review of this in parallel so we can test and refine the changes. AqNWB with the necessary changes for this PR was released on 11/03/2026 so the PR now points to the corresponding 0.5.0 release of AqNWB.

Because this PR updates to the latest version of NWB, I also created follow-up PRs:

Both are in draft mode until this PR is merged, in case there are further changes here.

While working on this PR, I found four issues in the Open Ephys GUI (see GUI issues below). They are fixed on development (v1.2.0), and this PR uses the fixes when they are available. Update (2026-11-20): this PR now also includes three follow-ups that were reviewed on the fork to support: 1) batch writes, 2) session and subject metadata in the Record Node's options, and 3) a faster int16 conversion.

Summary

This PR replaces the NWB writer of the plugin (NWBFormat.h/.cpp) with AqNWB 0.5 and rewrites the NWB File file source on top of AqNWB's read API. It supersedes #4 (it builds on the commits of #4) and closes open-ephys-plugins#7.

The files are valid NWB 2.11 and pass the NWB Inspector at BEST_PRACTICE_VIOLATION (except check_empty_table, see below). The goal was to stay schema compliant without custom extensions or workarounds, and to update the readers (python-tools, NWB File Source) as necessary to correctly work with the updated format.

Main changes:

GUI versions

GUI Works Notes
v1.2.0 (development; recommended) ✅ All features, including batch writes, the NWB2 options and draining the record buffer at stop.
v1.1.0 ✅ The NWB2 options are not applied (open-ephys/plugin-GUI#710), so metadata must come from a JSON file. Channels are written one at a time (open-ephys/plugin-GUI#711), which is slower but still keeps up with 3072 channels at 30 kHz in Release builds. Samples still in the record buffer at stop are discarded (open-ephys/plugin-GUI#713), and the File Reader loop bug (open-ephys/plugin-GUI#712) affects recordings of looped files.

The plugin requires plugin API version 10 (GUI v1.x).

File layout

As before, each experiment (start/stop of acquisition) is written to a separate file experiment<N>.nwb, and the recordings within an experiment are appended to the same datasets. Existing files are never changed: if the data directory is changed between recordings, the next recording starts a new file in the new directory. That file is named experiment<N>_recording<M>.nwb if the directory already contains experiment<N>.nwb (python-tools support in open-ephys/open-ephys-python-tools#51).

Data Location Change from the previous plugin
Continuous data (one per stream) /acquisition/<source>-<id>.<stream> (ElectricalSeries)
  • Sample numbers moved from sync to sync/sample_numbers, to improve format compliance.
  • The scaling is stored only in channel_conversion. The previous plugin stored the factor in both data.conversion and channel_conversion, so NWB readers scaled the data twice.
Spikes (one per electrode) /acquisition/<source>-<id>.<stream>.<electrode> (SpikeEventSeries)
  • Sample numbers in sync/sample_numbers.
  • Scaling in data.conversion (one factor per waveform).
  • Chunked by whole waveforms.
TTL events (one per event channel) /events/<processor>-<id>.<stream>.TTL (TTL_1, TTL_2, ... for further channels; named like the Binary event folders) EventsTable (new in NWB 2.11) with line, state, full_word, sample_number, instead of a .TTL TimeSeries in /acquisition
Text messages /events/messages EventsTable with annotation, sample_number, instead of a TimeSeries of strings
Sync messages (recording start) /events/sync_messages EventsTable
Start/stop of each recording /intervals/epochs New, stored in a TimeIntervals table
Electrodes /general/extracellular_ephys/electrodes New channel_type column
Session and subject metadata /general, /general/subject New (see Session and subject metadata)

Note: sync/sample_numbers is not yet part of the NWB schema. TimeSeries/sync is a group without a specification for its contents, so validation doesn't flag the dataset. We are discussing a standard location for sample numbers in NeurodataWithoutBorders/nwb-schema#712. For now, I think the TimeSeries/sync group is a good place. Previously, the plugin used a dataset TimeSeries/sync instead, which was not schema compliant.

Robustness and performance

  • Crash safety: the file is written in HDF5 SWMR mode.
    • It is flushed when a recording stops and every 10 s during a recording, and sync messages are written immediately.
    • I killed the GUI (SIGKILL) during recording; the data up to the last flush was readable.
  • Batch writes: with GUI v1.2.0, the GUI passes all channels of a stream for each block to writeContinuousDataBatch. The plugin converts them to int16, interleaves them in 64 x 64 tiles, and writes the block with one ElectricalSeries::writeAllChannels call. With GUI v1.1.0, the GUI calls writeContinuousData per channel, and the plugin writes one column per call.
  • Chunk cache: for the per-channel path, the HDF5 chunk cache is sized to the number of recorded channels, and never set below the HDF5 default. With the default 1 MiB cache, a 384-channel recording already couldn't keep up.
  • int16 conversion: one loop that the compiler vectorizes, with the same arithmetic and rounding as JUCE's convertFloatToInt16LE, so the written values are unchanged. In a micro-benchmark it takes 0.25 ns per sample, compared with 0.41 ns for JUCE.

Performance tests

Setup:

  • Apple M4 (10 cores, 32 GB RAM), internal SSD, macOS 15.7.2;
  • Release builds of the GUI and the plugin, HDF5 1.14.3;
  • a File Reader plays back a synthetic Binary dataset (int16, 30 kHz, noise plus a 7 Hz sine, looped) into a Record Node, which records for 30 s.

The scripts and full results are on the fork's perf-tests branch.

The Record Thread's time spent writing continuous data is the share of real time; 100% would mean the engine can't keep up:

Channels Data rate NWB2, one write per channel (GUI v1.1.0) NWB2, block writes + faster conversion Binary
384 23 MB/s 8.9% 2.3% 1.1%
1536 92 MB/s 34.5% 7.9% 5.8%
3072 184 MB/s 70.5% 16.2% 13.2%

The middle column was measured with a prototype on perf-tests. It writes the blocks the same way and uses an earlier version of the conversion (0.32 instead of 0.25 ns per sample), so this PR should be at least as fast. With the batch API and the JUCE conversion, the share was 3.1%, 12.9% and 23.8%.

Results:

Timestamp handling

Timestamps are the GUI's synchronized timestamps, as in the Binary format: seconds since timestamp 0 of the GUI (sample 0 of the main stream, normally the start of acquisition). timestamps_reference_time and session_start_time are set to the time of timestamp 0: the system time when the first recording of the file starts (the "Software Time" sync message) minus the first recorded timestamp. The previous plugin used the time the file was created, so all times were offset by the time between the start of acquisition and the start of recording. Streams that aren't synchronized have timestamps of -1, as in the Binary format; the epochs ignore them.

Session and subject metadata

The metadata is applied when an NWB file is created, at the first recording of each experiment. There are three ways to set it; the README has the details.

  1. The Record Node's NWB2 options (gear button next to the recording format; GUI v1.2.0):

    • session description, experimenters, institution, lab;
    • subject ID, species, sex, age, date of birth, genotype, strain, weight and description;
    • the path of a metadata file.

    They are saved with the signal chain and can be set through the HTTP API as engine_NWB2_<id>.

  2. A JSON metadata file with the same structure as NeuroConv metadata, with more NWBFile and Subject fields than the options. The plugin reads the file set in the options, or else nwb_metadata.json next to the recording. Options that are set override the values in the file; empty options don't.

  3. After recording, with PyNWB or NWB GUIDE.

If fields that DANDI requires for the subject are missing, the plugin logs a warning. It logs an error for a date of birth that isn't a full ISO 8601 date and time with time zone, which AqNWB can't write. was_generated_by records the GUI and plugin versions.

NWB File Source

The NWB File file source now reads with AqNWB, with lazy reads instead of loading whole datasets. It plays back:

  • files written by this plugin;
  • files written by the previous plugin (tested with a GUI v0.6.7 file);
  • files written by other tools such as PyNWB, including TTL events and messages.

Playback is bit-exact, compared with a Binary recording of the played-back data. It handles the File Reader's event ranges of both GUI v1.1.0 and v1.2.0 (open-ephys/plugin-GUI#712).

Known limitations

Dependencies

Build and CI

  • AqNWB is included as a git submodule (sparse checkout) and compiled with the plugin; the CI workflows check out the submodule.
  • HDF5: the plugin no longer links the OpenEphysHDF5 common library. The HDF5 1.14.3 libraries are in libs/<platform>.
    • The install target installs their runtime libraries into the GUI's shared directory (shared-api10 on macOS).
    • The deploy steps package them in shared/ next to the plugin, signed on macOS, as zmq-interface does.
    • The deploy steps only run on main, so the packaging is only exercised after this PR is merged.
  • Workflows:
    • CI builds against the main branch of the Open Ephys GUI on all platforms.
    • Windows uses open-ephys-lib-v1.1.0.zip (which has RecordEngine::writeContinuousDataBatch) instead of building the GUI, and runs on windows-2022, as the GUI does.
  • Version: the plugin version is bumped to 2.0.0, since the file layout changed. The version is also stored in /general/was_generated_by. It stays a literal in OpenEphysLib.cpp, since the deploy steps read it from the info->libVersion line.

Testing

On macOS, with GUI v1.2.0 (development, d90de30) and v1.1.0:

GUI issues

Found while working on this PR; all are fixed on development (v1.2.0) and retested:

AI usage declaration

🤖 Generated with the help of Claude Code. All changes have been reviewed by @oruebel.

@oruebel
oruebel marked this pull request as ready for review October 27, 2026 07:04
Copilot AI balanced review requested due to automatic review settings October 27, 2026 07:04

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

…taBatch)

Implement RecordEngine::writeContinuousDataBatch: the GUI passes all
recorded channels of a data stream (one ElectricalSeries) for a block;
convert them to int16, interleave them into the [sample][channel] layout
of the dataset (in 64 x 64 tiles), and write the data and timestamps with
a single ElectricalSeries::writeAllChannels call instead of one HDF5
write per channel. Any other combination of channels is written channel
by channel. The sample numbers, the time range of the recording
(dataWritten) and the stream start times are shared with
writeContinuousData (writeBlockSampleNumbers, finishContinuousWrite).

GUI development (open-ephys/plugin-GUI#711, 5a16ccf) calls
writeContinuousDataBatch for the NWB2 engine if the plugin's library
version is 2.0 or newer; GUI v1.1.0 keeps calling writeContinuousData.

Tested on GUI development: data identical to the source for one and two
streams (Merger), with spikes, events and messages, several recordings,
and recordings across the File Reader loop; complete, gap-free sample
numbers for 20 s recordings with 3072 and 4608 channels. Record Thread time spent writing at
384/1536/3072 channels (Release): 3.1/12.9/23.8% (one write per channel:
8.6/35.0/71.8%).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@oruebel
oruebel merged commit dd9f1e2 into add-aqnwb-v0.4 Nov 20, 2026
3 checks passed
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.

2 participants