mne_bids/read.py
def _handle_events_reading(events_fname, raw):
    """Read associated events.tsv and populate raw.

    Handle onset, duration, and description of each event.
    """
    logger.info('Reading events from {}.'.format(events_fname))
    events_dict = _from_tsv(events_fname)

    # Get the descriptions of the events
    if 'trial_type' in events_dict:
        trial_type_col_name = 'trial_type'
    elif 'stim_type' in events_dict:  # Backward-compat with old datasets.
        trial_type_col_name = 'stim_type'
        warn(f'The events file, {events_fname}, contains a "stim_type" '
             f'column. This column should be renamed to "trial_type" for '
             f'BIDS compatibility.')
    else:
        trial_type_col_name = None

    if trial_type_col_name is not None:
        # Drop events unrelated to a trial type
        events_dict = _drop(events_dict, 'n/a', trial_type_col_name)

        if 'value' in events_dict:
            # Check whether the `trial_type` <> `value` mapping is unique.
            trial_types = events_dict[trial_type_col_name]
            values = np.asarray(events_dict['value'], dtype=str)
            for trial_type in np.unique(trial_types):
                idx = np.where(trial_type == np.atleast_1d(trial_types))[0]
                matching_values = values[idx]

                if len(np.unique(matching_values)) > 1:
                    # Event type descriptors are ambiguous; create hierarchical
                    # event descriptors.
                    logger.info(
                        f'The event "{trial_type}" refers to multiple event '
                        f'values. Creating hierarchical event names.')
                    for ii in idx:
                        new_name = f'{trial_type}/{values[ii]}'
                        logger.info(f'    Renaming event: {trial_type} -> '
                                    f'{new_name}')
                        trial_types[ii] = new_name
            descriptions = np.asarray(trial_types, dtype=str)
        else:
            descriptions = np.asarray(events_dict[trial_type_col_name],
                                      dtype=str)
    elif 'value' in events_dict:
        # If we don't have a proper description of the events, perhaps we have
        # at least an event value?
        # Drop events unrelated to value
        events_dict = _drop(events_dict, 'n/a', 'value')
        descriptions = np.asarray(events_dict['value'], dtype=str)

    # Worst case, we go with 'n/a' for all events
    else:
        descriptions = np.array(['n/a'] * len(events_dict['onset']), dtype=str)

    # Deal with "n/a" strings before converting to float
    onsets = np.array(
        [np.nan if on == 'n/a' else on for on in events_dict['onset']],
        dtype=float)
    durations = np.array(
        [0 if du == 'n/a' else du for du in events_dict['duration']],
        dtype=float)

    # Keep only events where onset is known
    good_events_idx = ~np.isnan(onsets)
    onsets = onsets[good_events_idx]
    durations = durations[good_events_idx]
    descriptions = descriptions[good_events_idx]
    del good_events_idx

    # Add events as Annotations, but keep essential Annotations present in
    # raw file
    annot_from_raw = raw.annotations.copy()

    annot_from_events = mne.Annotations(onset=onsets,
                                        duration=durations,
                                        description=descriptions)
    raw.set_annotations(annot_from_events)

    annot_idx_to_keep = [idx for idx, annot in enumerate(annot_from_raw)
                         if annot['description'] in ANNOTATIONS_TO_KEEP]
    annot_to_keep = annot_from_raw[annot_idx_to_keep]

    if len(annot_to_keep):
        raw.set_annotations(raw.annotations + annot_to_keep)

    return raw
Read associated events.tsv and populate raw.

    Handle onset, duration, and description of each event.
    
Read associated events.tsv and populate raw.
Handle onset, duration, and description of each event.
Read associated events . tsv and populate raw . Handle onset duration and description of each event .
def _handle_events_reading(events_fname, raw):
    logger.info('Reading events from {}.'.format(events_fname))
    events_dict = _from_tsv(events_fname)
    if 'trial_type' in events_dict:
        trial_type_col_name = 'trial_type'
    elif 'stim_type' in events_dict:  
        trial_type_col_name = 'stim_type'
        warn(f'The events file, {events_fname}, contains a "stim_type" '
             f'column. This column should be renamed to "trial_type" for '
             f'BIDS compatibility.')
    else:
        trial_type_col_name = None
    if trial_type_col_name is not None:
        events_dict = _drop(events_dict, 'n/a', trial_type_col_name)
        if 'value' in events_dict:
            trial_types = events_dict[trial_type_col_name]
            values = np.asarray(events_dict['value'], dtype=str)
            for trial_type in np.unique(trial_types):
                idx = np.where(trial_type == np.atleast_1d(trial_types))[0]
                matching_values = values[idx]
                if len(np.unique(matching_values)) > 1:
                    logger.info(
                        f'The event "{trial_type}" refers to multiple event '
                        f'values. Creating hierarchical event names.')
                    for ii in idx:
                        new_name = f'{trial_type}/{values[ii]}'
                        logger.info(f'    Renaming event: {trial_type} -> '
                                    f'{new_name}')
                        trial_types[ii] = new_name
            descriptions = np.asarray(trial_types, dtype=str)
        else:
            descriptions = np.asarray(events_dict[trial_type_col_name],
                                      dtype=str)
    elif 'value' in events_dict:
        events_dict = _drop(events_dict, 'n/a', 'value')
        descriptions = np.asarray(events_dict['value'], dtype=str)
    else:
        descriptions = np.array(['n/a'] * len(events_dict['onset']), dtype=str)
    onsets = np.array(
        [np.nan if on == 'n/a' else on for on in events_dict['onset']],
        dtype=float)
    durations = np.array(
        [0 if du == 'n/a' else du for du in events_dict['duration']],
        dtype=float)
    good_events_idx = ~np.isnan(onsets)
    onsets = onsets[good_events_idx]
    durations = durations[good_events_idx]
    descriptions = descriptions[good_events_idx]
    del good_events_idx
    annot_from_raw = raw.annotations.copy()
    annot_from_events = mne.Annotations(onset=onsets,
                                        duration=durations,
                                        description=descriptions)
    raw.set_annotations(annot_from_events)
    annot_idx_to_keep = [idx for idx, annot in enumerate(annot_from_raw)
                         if annot['description'] in ANNOTATIONS_TO_KEEP]
    annot_to_keep = annot_from_raw[annot_idx_to_keep]
    if len(annot_to_keep):
        raw.set_annotations(raw.annotations + annot_to_keep)
    return raw
def _handle_events_reading ( events_fname , raw ) : logger . info ( 'Reading events from {}.' . format ( events_fname ) ) events_dict = _from_tsv ( events_fname ) if 'trial_type' in events_dict : trial_type_col_name = 'trial_type' elif 'stim_type' in events_dict : trial_type_col_name = 'stim_type' warn ( f'The events file, {events_fname}, contains a "stim_type" ' f'column. This column should be renamed to "trial_type" for ' f'BIDS compatibility.' ) else : trial_type_col_name = None if trial_type_col_name is not None : events_dict = _drop ( events_dict , 'n/a' , trial_type_col_name ) if 'value' in events_dict : trial_types = events_dict [ trial_type_col_name ] values = np . asarray ( events_dict [ 'value' ] , dtype = str ) for trial_type in np . unique ( trial_types ) : idx = np . where ( trial_type == np . atleast_1d ( trial_types ) ) [ 0 ] matching_values = values [ idx ] if len ( np . unique ( matching_values ) ) > 1 : logger . info ( f'The event "{trial_type}" refers to multiple event ' f'values. Creating hierarchical event names.' ) for ii in idx : new_name = f'{trial_type}/{values[ii]}' logger . info ( f'    Renaming event: {trial_type} -> ' f'{new_name}' ) trial_types [ ii ] = new_name descriptions = np . asarray ( trial_types , dtype = str ) else : descriptions = np . asarray ( events_dict [ trial_type_col_name ] , dtype = str ) elif 'value' in events_dict : events_dict = _drop ( events_dict , 'n/a' , 'value' ) descriptions = np . asarray ( events_dict [ 'value' ] , dtype = str ) else : descriptions = np . array ( [ 'n/a' ] * len ( events_dict [ 'onset' ] ) , dtype = str ) onsets = np . array ( [ np . nan if on == 'n/a' else on for on in events_dict [ 'onset' ] ] , dtype = float ) durations = np . array ( [ 0 if du == 'n/a' else du for du in events_dict [ 'duration' ] ] , dtype = float ) good_events_idx = ~ np . isnan ( onsets ) onsets = onsets [ good_events_idx ] durations = durations [ good_events_idx ] descriptions = descriptions [ good_events_idx ] del good_events_idx annot_from_raw = raw . annotations . copy ( ) annot_from_events = mne . Annotations ( onset = onsets , duration = durations , description = descriptions ) raw . set_annotations ( annot_from_events ) annot_idx_to_keep = [ idx for idx , annot in enumerate ( annot_from_raw ) if annot [ 'description' ] in ANNOTATIONS_TO_KEEP ] annot_to_keep = annot_from_raw [ annot_idx_to_keep ] if len ( annot_to_keep ) : raw . set_annotations ( raw . annotations + annot_to_keep ) return raw
Read associated events.tsv and populate raw.
Read associated events . tsv and populate raw .
"""Read associated events.tsv and populate raw.

    Handle onset, duration, and description of each event.
    """ # Get the descriptions of the events # Backward-compat with old datasets. # Drop events unrelated to a trial type # Check whether the `trial_type` <> `value` mapping is unique. # Event type descriptors are ambiguous; create hierarchical # event descriptors. # If we don't have a proper description of the events, perhaps we have # at least an event value? # Drop events unrelated to value # Worst case, we go with 'n/a' for all events # Deal with "n/a" strings before converting to float # Keep only events where onset is known # Add events as Annotations, but keep essential Annotations present in # raw file
events_fname
raw