Skip to main content

nex5_nwb/
lib.rs

1//! Read and write NWB 2.x files as [`nex5file::FileData`].
2//!
3//! Spike trains map to the NWB `Units` table; events and continuous variables
4//! map to `acquisition/` [`TimeSeries`](consus_nwb::model::TimeSeries) groups.
5//! For spike sorting, PSTHs, and filtering, use the sibling [`nex5-analyze`] crate.
6//!
7//! [`nex5-analyze`]: ../nex5_analyze/index.html
8
9mod convert;
10mod error;
11mod metadata;
12mod options;
13
14pub use error::{NwbError, Result};
15pub use options::{NwbReadOptions, NwbWriteOptions};
16
17use std::fs;
18use std::path::Path;
19
20use convert::{file_data_to_nwb_bytes, nwb_bytes_to_file_data};
21use nex5file::FileData;
22
23/// Read an NWB file from disk into [`FileData`].
24pub fn read_nwb_file(path: impl AsRef<Path>, options: &NwbReadOptions) -> Result<FileData> {
25    let bytes = fs::read(path)?;
26    read_nwb_bytes(&bytes, options)
27}
28
29/// Read NWB HDF5 bytes into [`FileData`].
30pub fn read_nwb_bytes(bytes: &[u8], options: &NwbReadOptions) -> Result<FileData> {
31    nwb_bytes_to_file_data(bytes, options)
32}
33
34/// Write [`FileData`] to an NWB file on disk.
35pub fn write_nwb_file(
36    data: &FileData,
37    path: impl AsRef<Path>,
38    options: &NwbWriteOptions,
39) -> Result<()> {
40    let bytes = write_nwb_bytes(data, options)?;
41    fs::write(path, bytes)?;
42    Ok(())
43}
44
45/// Serialize [`FileData`] to NWB HDF5 bytes.
46pub fn write_nwb_bytes(data: &FileData, options: &NwbWriteOptions) -> Result<Vec<u8>> {
47    file_data_to_nwb_bytes(data, options)
48}