Skip to content

Commit

Permalink
[MRG] Fix calculation of samples to avoid incorrect file (mne-tools#4962
Browse files Browse the repository at this point in the history
)

* Fix calculation of samples to avoid incorrect warning (and resulting file)

* Determine subtype from extension

* Better explanation why we skip the reserved header field
  • Loading branch information
cbrnr authored and agramfort committed Feb 27, 2018
1 parent d2850c8 commit e22d010
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions mne/io/edf/edf.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ def find_edf_events(self):
def _read_ch(fid, subtype, samp, dtype_byte, dtype=None):
"""Read a number of samples for a single channel."""
# BDF
if subtype in ('24BIT', 'bdf'):
if subtype == 'bdf':
ch_data = np.fromfile(fid, dtype=dtype, count=samp * dtype_byte)
ch_data = ch_data.reshape(-1, 3).astype(np.int32)
ch_data = ((ch_data[:, 0]) +
Expand Down Expand Up @@ -594,9 +594,13 @@ def _read_edf_header(fname, annot, annotmap, exclude):

header_nbytes = int(fid.read(8).decode())

subtype = fid.read(44).strip().decode()[:5]
if len(subtype) == 0:
subtype = os.path.splitext(fname)[1][1:].lower()
# The following 44 bytes sometimes identify the file type, but this is
# not guaranteed. Therefore, we skip this field and use the file
# extension to determine the subtype (EDF or BDF, which differ in the
# number of bytes they use for the data records; EDF uses 2 bytes
# whereas BDF uses 3 bytes).
fid.read(44)
subtype = os.path.splitext(fname)[1][1:].lower()

n_records = int(fid.read(8).decode())
record_length = np.array([float(fid.read(8)), 1.]) # in seconds
Expand Down Expand Up @@ -660,7 +664,7 @@ def _read_edf_header(fname, annot, annotmap, exclude):
fid.seek(0, 2)
n_bytes = fid.tell()
n_data_bytes = n_bytes - header_nbytes
total_samps = (n_data_bytes // 3 if subtype == '24BIT'
total_samps = (n_data_bytes // 3 if subtype == 'bdf'
else n_data_bytes // 2)
read_records = total_samps // np.sum(n_samps)
if n_records != read_records:
Expand All @@ -669,7 +673,7 @@ def _read_edf_header(fname, annot, annotmap, exclude):
' Inferring from the file size.')
edf_info['n_records'] = n_records = read_records

if subtype in ('24BIT', 'bdf'):
if subtype == 'bdf':
edf_info['dtype_byte'] = 3 # 24-bit (3 byte) integers
edf_info['dtype_np'] = np.uint8
else:
Expand Down

0 comments on commit e22d010

Please sign in to comment.