def get_log_times()

in neural/utils_mous.py [0:0]


def get_log_times(log, events, sfreq):
    '''
    Adds time of occurence of events to the log_files table.
    Input:
        log (pandas table): log_files table
        events (array): cf https://mne.tools/dev/auto_tutorials/raw/plot_20_event_arrays.html
        sfreq (int): sampling frequency
    Output:
        log (pandas table): log_files table enriched with time of events information
    '''
    sel = np.sort(np.r_[np.where(events[:, 2] == 20)[0],  # fixation
                        np.where(events[:, 2] == 10)[0]  # context
                        ])
    common_megs = events[sel]
    common_logs = log.query('(new_context == True) or condition=="fix"')

    last_log = common_logs.time[0]
    last_meg = common_megs[0, 0]
    last_idx = 0
    assert len(common_megs) == len(common_logs)
    for common_meg, (idx, common_log) in zip(common_megs,
                                             common_logs.iterrows()):

        if common_meg[2] == 20:
            assert common_log.condition == 'fix'
        else:
            assert common_log.condition in ('sentence', 'word_list')

        log.loc[idx, 'meg_time'] = common_meg[0] / sfreq

        sel = slice(last_idx + 1, idx)
        times = log.loc[sel, 'time'] - last_log + last_meg / sfreq
        assert np.all(np.isfinite(times))
        log.loc[sel, 'meg_time'] = times

        last_log = common_log.time
        last_meg = common_meg[0]
        last_idx = idx

        assert np.isfinite(last_log) * np.isfinite(last_meg)

    # last block
    sel = slice(last_idx + 1, None)
    times = log.loc[sel, 'time'] - last_log + last_meg / sfreq
    log.loc[sel, 'meg_time'] = times
    log['meg_sample'] = np.array(log.meg_time.values * sfreq, int)
    return log