File Formats
Four writer and reader pairs, and the run directory save_as writes.
This page is the layout of each, so a file can be read anywhere, and
what a file you made yourself has to look like for a g.Pype reader.
Season 4, Episode 5
is the walk-through.
What every reader produces
A reader hands the pipeline (time, channel) samples as float32,
a sampling rate, and where the file records them, labels, roles, units,
an absolute start time and markers. The rate comes from the first of:
g.Pype’s own metadata in the file, the container’s native metadata, a
recorded time axis, or your sampling_rate= argument; one you pass
where the file disagrees is refused. A rate derived from a time axis is
snapped to the nearest integer when within one part in ten thousand.
|
|
|
|
Note |
|
|---|---|---|---|---|---|
Precision on disk |
text, 17 digits |
float64, exact |
float64, exact |
16-bit, lossy |
every pipeline is |
Labels |
always |
when recorded |
when recorded |
always |
CSV and EDF cannot leave a column unnamed |
Roles |
no |
yes |
yes |
yes |
a CSV reads back as all |
Units |
no |
yes |
yes |
yes |
in |
Start time |
no |
yes |
yes |
no |
in |
Markers |
no |
yes |
yes |
yes |
EDF+ annotations, no channel |
Gaps, trust |
no |
yes |
yes |
no |
in |
CSV
# a comment line, dropped
Time, Fz, Cz, Pz
0,0.0,0.0,0.0
0.004,2.4868988990783691,1.1,3.1
UTF-8, comma-separated, a header row, and a first column named exactly
Time in seconds, ascending. Without a Time column every column is
a channel and sampling_rate= is required. Values are written with 17
significant digits, times with 15. pandas.read_csv(path, comment="#",
skipinitialspace=True) reads it anywhere.
Three ways a CSV is read wrong, silently: a header time in lower
case makes the time axis a data channel; a file without a header loses
its first row to the header; a column with an empty cell is refused,
nan is not.
MATLAB v7.3 and HDF5
One layout for both, since the writers agree on it. A dataset named
data (variable_name= for another) of shape
(1 + n_channels, n_samples) as HDF5 sees it: the time row first,
then one row per channel, float64. MATLAB reverses the dimension
order, so load() gives (n_samples, 1 + n_channels): data(:, 1)
is time and data(:, 2:end) the signal.
Beside it, gpype_meta: a JSON document with channel_labels,
channel_roles, channel_units, sampling_rate, start_time,
device_serial and the markers, gaps and trust state. HDF5Writer
stores it as a string dataset, MatWriter as a column of character
codes, MATLAB’s convention for text. A foreign HDF5 file can carry
sampling_rate, channel_labels and channel_roles as attributes
of the dataset instead, and a reader takes them.
import h5py, numpy as np
with h5py.File("rec.h5") as h:
block = h["data"][...] # (1 + channels, samples)
t, sig = block[0], block[1:].T # seconds, (samples, channels)
To write one g.Pype reads with everything in place:
with h5py.File("rec.h5", "w") as h:
d = h.create_dataset("data", data=np.column_stack([t, sig]).T)
d.attrs["sampling_rate"] = 250.0
d.attrs["channel_labels"] = np.array(["Fz", "Cz", "Pz"], dtype=object)
d.attrs["channel_roles"] = np.array(["signal"] * 3, dtype=object)
has_time_row is never guessed. A file of (channels, samples) with
no time row, read with the default, loses its first channel to the time
axis, and the transpose reads as a handful of samples of many channels;
neither raises. MatWriter also writes a sampling_rate variable
for a person opening the file in MATLAB; MatReader does not read it.
EDF+
The clinical interchange format, read by every EEG package. Samples are
16-bit integers scaled between physical_min and physical_max,
±10000 µV by default and settable on EDFWriter: a step of about
0.305 µV, and values outside the range are clipped. Labels come from the
signal headers, roles from the transducer field where g.Pype wrote them,
the device serial from the equipment field. Markers are EDF+
annotations, one per data record at most, text cut at 40 bytes.
EDF cannot express a sample count that is not a whole number of records,
so a file may end with up to one record of padding. EDFWriter records
the true count in an annotation, gpype:samples=676; the reader uses
it, and a foreign reader can crop to it. mne.io.read_raw_edf reads
the file with its annotations; the
MNE workflow shows it.
A recorded run
python script.py --save-as my_run writes a directory
my_run_<timestamp> with manifest.json and one .h5 file per
source, named after the node, eeg.h5 for a source named eeg, and
by class and ordinal for one without a name, BCICore_0.h5. Each
file holds data in the layout above, the time row first, gpype_meta,
and a table of the blocks as they arrived, which is what lets
--load-from my_run_<timestamp> replay every source at its recorded
instants, with no device present. The manifest lists each stream’s node,
class, rate, channel count, sample count and dropped blocks.
Record and Replay describes the mechanism.
A three-line check
Before trusting a file, read it once and look at the shape:
with gp.Pipeline() as p:
c = gp.Collector()
p.connect(gp.HDF5Reader(file_name=path, mode="batch"), c)
r = p.run()
print(r.data.shape, r.rate, r.labels, r.roles)
The shape is (samples, channels) and not the transpose, the channel
count is not one short, and the rate is the real one. Every silent
misreading on this page shows in that line.