def _add_stim_id()

in neural/utils_mous.py [0:0]


def _add_stim_id(log, verbose, stimuli):
    '''Auxiliary function to preprocess the log_files table.
    Matches stimulus information between the log_files and stimuli tables.
    Input:
        log (pandas table)
        verbose (bool): if True, prints commentary
        stimuli (pandas table)
    Output:
        log_completed (pandas table)
    '''
    # Find beginning of each sequence (word list or sentence)
    start = 0
    sequence_pos = -1
    for idx, row in log.query('condition == "fix"').iterrows():
        if sequence_pos >= 0:
            log.loc[start:idx, 'sequence_pos'] = sequence_pos
        sequence_pos += 1
        start = idx
    log.loc[start:, 'sequence_pos'] = sequence_pos

    # Find corresponding stimulus id
    stim_id = 0
    lower30 = lambda s: s[:30].lower()  # noqa
    stimuli['first_30_chars'] = stimuli.sequence.apply(lower30)
    sel = slice(0, 0)
    for pos, row in log.groupby('sequence_pos'):
        if pos == -1:
            continue

        # select words in this sequence
        sel = row.condition == "word"
        if not sum(sel):
            continue

        # match with stimuli
        first_30_chars = ' '.join(row.loc[sel, 'word'])[:30].lower()  # noqa
        stim_id = stimuli.query('first_30_chars == @first_30_chars').index
        assert len(stim_id) == 1
        stim_id = stim_id[0]

        n_words = len(stimuli.loc[stim_id, 'sequence'].split(' '))
        if (n_words != sum(sel)) and verbose:
            print('mistach of %i words in %s (stim %i)' %
                  (n_words - sum(sel), pos, stim_id))
            print('stim: %s' % stimuli.loc[stim_id, 'sequence'])
            print('log: %s' % ' '.join(row.loc[sel, 'word']))

        # Update
        log.loc[row.index, 'stim_id'] = stim_id
    return log