diff --git a/mne/io/cnt/_utils.py b/mne/io/cnt/_utils.py index 74984a403c2..83e18b12162 100644 --- a/mne/io/cnt/_utils.py +++ b/mne/io/cnt/_utils.py @@ -7,6 +7,7 @@ from math import modf from datetime import datetime import numpy as np +from os import SEEK_END from ...utils import warn @@ -96,7 +97,7 @@ def _session_date_2_meas_date(session_date, date_format): return (int_part, frac_part) -def _compute_robust_event_table_position(fid): +def _compute_robust_event_table_position(fid, data_format): """Compute `event_table_position`. When recording event_table_position is computed (as accomulation). If the @@ -112,40 +113,64 @@ def _compute_robust_event_table_position(fid): x_xxxxxxxx : xxxxxxxx xx Xxxxxxxxxxx Xxx xxxxxxxxxxx. """ - - def _infer_n_bytes_event_table_pos(readed_event_table_pos): - readed_event_table_pos_feature = np.binary_repr( - readed_event_table_pos).lstrip('-') - - for n_bytes in [2, 4]: - computed_event_table_pos = ( - 900 + 75 * int(n_channels) + - n_bytes * int(n_channels) * int(n_samples)) - - if ( - np.binary_repr(computed_event_table_pos) - .endswith(readed_event_table_pos_feature) - ): - return n_bytes, computed_event_table_pos - - raise Exception("event_table_dismatch") - SETUP_NCHANNELS_OFFSET = 370 SETUP_NSAMPLES_OFFSET = 864 SETUP_EVENTTABLEPOS_OFFSET = 886 + def get_most_possible_sol(fid, possible_n_bytes, n_samples, n_channels): + """Find the most possible solution + + Since both event table position and n_bytes has many possible values, + and n_samples might be not so accurate, distance between the possible + event table position and calculated event table position is used to + find the most possible combination of the event table position and + n_bytes. + + When the distance of the solution find is not equals to 0, there is + a mismatch between the n_samples and the event_table_pos. + """ + sol_table = [] + for event_table_pos in possible_event_table_pos(fid): + for n_bytes in possible_n_bytes: + calc_event_table_pos = 900 + 75 * n_channels + \ + n_bytes * n_channels * n_samples + distance = abs(calc_event_table_pos - event_table_pos) + + if distance == 0: + return event_table_pos, n_bytes, distance + sol_table.append((event_table_pos, n_bytes, distance)) + return sorted(sol_table, key=lambda x: x[2])[0] + + def possible_event_table_pos(fid): + """Yield all the possible event table position""" + fid.seek(SETUP_EVENTTABLEPOS_OFFSET) + event_table_pos = int(np.frombuffer(fid.read(4), dtype='