Skip to content

Files in and out

dspk.io.write_raw writes a raw binary with a JSON .meta beside it (any stored type: here 16-bit integers with a gain, as acquisition systems store them); dspk.io.write_zarr writes a compressed Zarr store. dspk.io.open reads either (and SpikeGLX, NWB) and returns values in their unit, whatever the stored type.

from pathlib import Path
import tempfile
import numpy as np
import dspk
from plating import signals
s = signals.synthetic()
work = Path(tempfile.mkdtemp(prefix="dspk-playground-"))
# int16 with a gain of 0.195 µV per step (Intan's), and float32 Zarr
GAIN = 0.195
dspk.io.write_raw(s.data, work / "recording.bin", s.sample_rate, format="int16", gain=GAIN, unit="µV")
dspk.io.write_zarr(s.data, work / "recording.zarr", s.sample_rate, unit="µV")
raw, zarr = dspk.io.open(work / "recording.bin"), dspk.io.open(work / "recording.zarr")
print(raw)
print(zarr)

What comes back: Zarr exactly, int16 within half a step

from_raw, from_zarr = raw.read(), zarr.read()
print("zarr max difference:", np.max(np.abs(from_zarr - s.data)))
print("int16 max difference:", np.max(np.abs(from_raw - s.data)), f"(half a step: {GAIN / 2:.4f} µV)")
assert np.array_equal(from_zarr, s.data) and np.max(np.abs(from_raw - s.data)) <= GAIN / 2 + 1e-4

Files in and out

The whole script is playground/01_basics/03_files.py. Run it with uv run python playground/01_basics/03_files.py.