Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force little-endian format when writing eeg datafile #80

Merged
merged 14 commits into from Sep 29, 2021
Merged
3 changes: 3 additions & 0 deletions CITATION.cff
Expand Up @@ -23,6 +23,9 @@ authors:
- given-names: "Phillip"
family-names: "Alday"
orcid: https://orcid.org/0000-0002-9984-5745
- given-names: "Aniket"
family-names: "Pradhan"
orcid: https://orcid.org/0000-0002-6705-5116
title: "pybv"
version: 0.5.0
date-released: 2021-01-03
Expand Down
7 changes: 6 additions & 1 deletion README.rst
Expand Up @@ -60,7 +60,12 @@ consisting of:
- comments marked as ``; comment``
- key-value pairs marked as ``key=value``

A documentation for core BrainVision file format is provided by Brain Products.
The binary ``.eeg`` data file is written in little-endian format without a Byte Order
Mark (BOM), in accordance with the specification by Brain Products.
This ensures that the data file is uniformly written irrespective of the
native system architecture.

A documentation for the BrainVision file format is provided by Brain Products.
You can `view the specification <https://www.brainproducts.com/productdetails.php?id=21&tab=5>`_
as hosted by Brain Products.

Expand Down
2 changes: 2 additions & 0 deletions docs/changelog.rst
Expand Up @@ -7,6 +7,7 @@ Authors
People who contributed to this software across releases (in **alphabetical order**):

- `Adam Li`_
- `Aniket Pradhan`_
- `Chris Holdgraf`_
- `Clemens Brunner`_
- `Phillip Alday`_
Expand All @@ -33,6 +34,7 @@ Changelog
~~~~~~~~~

- :func:`pybv.write_brainvision` gained a new parameter, ``ref_ch_names``, to specify the reference channels used during recording, by `Richard H枚chenberger`_ and `Stefan Appelhoff`_ (:gh:`75`)
- Fix bug where :func:`pybv.write_brainvision` would write the binary eeg file in big-endian on a big-endian system. This could lead to non-uniformity of data-files written by big-endian or little-endian machines, by `Aniket Pradhan`, `Clemens Brunner` and `Stefan Appelhoff`_ (:gh:`80`).
Aniket-Pradhan marked this conversation as resolved.
Show resolved Hide resolved

API
~~~
Expand Down
7 changes: 7 additions & 0 deletions pybv/io.py
Expand Up @@ -14,6 +14,7 @@
import datetime
import os
import shutil
import sys
from os import path as op
from warnings import warn

Expand Down Expand Up @@ -491,5 +492,11 @@ def _write_bveeg_file(eeg_fname, data, orientation, format, resolution, units):
raise ValueError(msg)
data = data.astype(dtype=dtype)

# Swap bytes if system architecure is big-endian and dtype is "native" or
sappelhoff marked this conversation as resolved.
Show resolved Hide resolved
# "big-endian"
byteorder = data.dtype.byteorder # pragma: no cover
if (sys.byteorder == "big" and byteorder == "=") or byteorder == ">": # pragma: no cover # noqa: E501
sappelhoff marked this conversation as resolved.
Show resolved Hide resolved
data = data.byteswap()

# Save to binary
data.ravel(order='F').tofile(eeg_fname)