Skip to content

Commit 011fe40

Browse files
committed
Check for sub event more carefully
1 parent 4e82261 commit 011fe40

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

mne/io/nihon/nihon.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -276,22 +276,26 @@ def _read_nihon_header(fname):
276276
return header
277277

278278

279-
def _read_event_log_block(fid, t_block, device_type):
279+
def _read_event_log_block(fid, t_block, version):
280280
fid.seek(0x92 + t_block * 20)
281-
t_blk_address = np.fromfile(fid, np.uint32, 1)[0]
282-
if t_blk_address == 0:
281+
data = np.fromfile(fid, np.uint32, 1)
282+
if data.size == 0 or data[0] == 0:
283283
return
284+
t_blk_address = data[0]
284285

285286
fid.seek(t_blk_address + 0x1)
286-
data_name = np.fromfile(fid, "|S16", 1).astype("U16")[0]
287-
if data_name != device_type:
287+
data = np.fromfile(fid, "|S16", 1).astype("U16")
288+
if data.size == 0 or data[0] != version:
288289
return
289290

290291
fid.seek(t_blk_address + 0x12)
291-
n_logs = np.fromfile(fid, np.uint8, 1)[0]
292+
data = np.fromfile(fid, np.uint8, 1)
293+
if data.size == 0:
294+
return
295+
n_logs = data[0]
296+
292297
fid.seek(t_blk_address + 0x14)
293-
t_logs = np.fromfile(fid, "|S45", n_logs)
294-
return t_logs
298+
return np.fromfile(fid, "|S45", n_logs)
295299

296300

297301
def _parse_event_log(event_log):
@@ -333,11 +337,10 @@ def _read_nihon_annotations(fname):
333337
t_sub_logs = None
334338
if may_have_sub_blocks:
335339
t_sub_logs = _read_event_log_block(fid, t_block + 22, version)
336-
assert t_sub_logs is None or len(t_logs) == len(t_sub_logs)
337340

338341
for i, t_log in enumerate(t_logs):
339342
t_desc, t_onset = _parse_event_log(t_log)
340-
if t_sub_logs is not None:
343+
if t_sub_logs is not None and t_logs.size == t_sub_logs.size:
341344
t_sub_desc, t_sub_onset = _parse_sub_event_log(t_sub_logs[i])
342345
t_desc += t_sub_desc
343346
t_onset += t_sub_onset
@@ -351,7 +354,7 @@ def _read_nihon_annotations(fname):
351354
else:
352355
break
353356
else:
354-
warn(f"Could not decode log {t_desc} as one of {_encodings}")
357+
warn(f"Could not decode log as one of {_encodings}")
355358
continue
356359

357360
all_onsets.append(t_onset)

0 commit comments

Comments
 (0)