mne_bids/read.py
def _handle_channels_reading(channels_fname, raw):
    """Read associated channels.tsv and populate raw.

    Updates status (bad) and types of channels.
    """
    logger.info('Reading channel info from {}.'.format(channels_fname))
    channels_dict = _from_tsv(channels_fname)
    ch_names_tsv = channels_dict['name']

    # Now we can do some work.
    # The "type" column is mandatory in BIDS. We can use it to set channel
    # types in the raw data using a mapping between channel types
    channel_type_dict = dict()

    # Get the best mapping we currently have from BIDS to MNE nomenclature
    bids_to_mne_ch_types = _get_ch_type_mapping(fro='bids', to='mne')
    ch_types_json = channels_dict['type']
    for ch_name, ch_type in zip(ch_names_tsv, ch_types_json):

        # Try to map from BIDS nomenclature to MNE, leave channel type
        # untouched if we are uncertain
        updated_ch_type = bids_to_mne_ch_types.get(ch_type, None)

        if updated_ch_type is None:
            # XXX Try again with uppercase spelling – this should be removed
            # XXX once https://github.com/bids-standard/bids-validator/issues/1018  # noqa:E501
            # XXX has been resolved.
            # XXX x-ref https://github.com/mne-tools/mne-bids/issues/481
            updated_ch_type = bids_to_mne_ch_types.get(ch_type.upper(), None)
            if updated_ch_type is not None:
                msg = ('The BIDS dataset contains channel types in lowercase '
                       'spelling. This violates the BIDS specification and '
                       'will raise an error in the future.')
                warn(msg)

        if updated_ch_type is not None:
            channel_type_dict[ch_name] = updated_ch_type

    # Special handling for (synthesized) stimulus channel
    synthesized_stim_ch_name = 'STI 014'
    if (synthesized_stim_ch_name in raw.ch_names and
            synthesized_stim_ch_name not in ch_names_tsv):
        logger.info(
            f'The stimulus channel "{synthesized_stim_ch_name}" is present in '
            f'the raw data, but not included in channels.tsv. Removing the '
            f'channel.')
        raw.drop_channels([synthesized_stim_ch_name])

    # Rename channels in loaded Raw to match those read from the BIDS sidecar
    if len(ch_names_tsv) != len(raw.ch_names):
        warn(f'The number of channels in the channels.tsv sidecar file '
             f'({len(ch_names_tsv)}) does not match the number of channels '
             f'in the raw data file ({len(raw.ch_names)}). Will not try to '
             f'set channel names.')
    else:
        for bids_ch_name, raw_ch_name in zip(ch_names_tsv,
                                             raw.ch_names.copy()):
            if bids_ch_name != raw_ch_name:
                raw.rename_channels({raw_ch_name: bids_ch_name})

    # Set the channel types in the raw data according to channels.tsv
    ch_type_map_avail = {
        ch_name: ch_type
        for ch_name, ch_type in channel_type_dict.items()
        if ch_name in raw.ch_names
    }
    ch_diff = set(channel_type_dict.keys()) - set(ch_type_map_avail.keys())
    if ch_diff:
        warn(f'Cannot set channel type for the following channels, as they '
             f'are missing in the raw data: {", ".join(sorted(ch_diff))}')
    raw.set_channel_types(ch_type_map_avail)

    # Set bad channels based on _channels.tsv sidecar
    if 'status' in channels_dict:
        bads_tsv = _get_bads_from_tsv_data(channels_dict)
        bads_avail = [ch_name for ch_name in bads_tsv
                      if ch_name in raw.ch_names]

        ch_diff = set(bads_tsv) - set(bads_avail)
        if ch_diff:
            warn(f'Cannot set "bad" status for the following channels, as '
                 f'they are missing in the raw data: '
                 f'{", ".join(sorted(ch_diff))}')

        raw.info['bads'] = bads_avail

    return raw
Read associated channels.tsv and populate raw.

    Updates status (bad) and types of channels.
    
Read associated channels.tsv and populate raw.
Updates status (bad) and types of channels.
Read associated channels . tsv and populate raw . Updates status ( bad ) and types of channels .
def _handle_channels_reading(channels_fname, raw):
    logger.info('Reading channel info from {}.'.format(channels_fname))
    channels_dict = _from_tsv(channels_fname)
    ch_names_tsv = channels_dict['name']
    channel_type_dict = dict()
    bids_to_mne_ch_types = _get_ch_type_mapping(fro='bids', to='mne')
    ch_types_json = channels_dict['type']
    for ch_name, ch_type in zip(ch_names_tsv, ch_types_json):
        updated_ch_type = bids_to_mne_ch_types.get(ch_type, None)
        if updated_ch_type is None:
            updated_ch_type = bids_to_mne_ch_types.get(ch_type.upper(), None)
            if updated_ch_type is not None:
                msg = ('The BIDS dataset contains channel types in lowercase '
                       'spelling. This violates the BIDS specification and '
                       'will raise an error in the future.')
                warn(msg)
        if updated_ch_type is not None:
            channel_type_dict[ch_name] = updated_ch_type
    synthesized_stim_ch_name = 'STI 014'
    if (synthesized_stim_ch_name in raw.ch_names and
            synthesized_stim_ch_name not in ch_names_tsv):
        logger.info(
            f'The stimulus channel "{synthesized_stim_ch_name}" is present in '
            f'the raw data, but not included in channels.tsv. Removing the '
            f'channel.')
        raw.drop_channels([synthesized_stim_ch_name])
    if len(ch_names_tsv) != len(raw.ch_names):
        warn(f'The number of channels in the channels.tsv sidecar file '
             f'({len(ch_names_tsv)}) does not match the number of channels '
             f'in the raw data file ({len(raw.ch_names)}). Will not try to '
             f'set channel names.')
    else:
        for bids_ch_name, raw_ch_name in zip(ch_names_tsv,
                                             raw.ch_names.copy()):
            if bids_ch_name != raw_ch_name:
                raw.rename_channels({raw_ch_name: bids_ch_name})
    ch_type_map_avail = {
        ch_name: ch_type
        for ch_name, ch_type in channel_type_dict.items()
        if ch_name in raw.ch_names
    }
    ch_diff = set(channel_type_dict.keys()) - set(ch_type_map_avail.keys())
    if ch_diff:
        warn(f'Cannot set channel type for the following channels, as they '
             f'are missing in the raw data: {", ".join(sorted(ch_diff))}')
    raw.set_channel_types(ch_type_map_avail)
    if 'status' in channels_dict:
        bads_tsv = _get_bads_from_tsv_data(channels_dict)
        bads_avail = [ch_name for ch_name in bads_tsv
                      if ch_name in raw.ch_names]
        ch_diff = set(bads_tsv) - set(bads_avail)
        if ch_diff:
            warn(f'Cannot set "bad" status for the following channels, as '
                 f'they are missing in the raw data: '
                 f'{", ".join(sorted(ch_diff))}')
        raw.info['bads'] = bads_avail
    return raw
def _handle_channels_reading ( channels_fname , raw ) : logger . info ( 'Reading channel info from {}.' . format ( channels_fname ) ) channels_dict = _from_tsv ( channels_fname ) ch_names_tsv = channels_dict [ 'name' ] channel_type_dict = dict ( ) bids_to_mne_ch_types = _get_ch_type_mapping ( fro = 'bids' , to = 'mne' ) ch_types_json = channels_dict [ 'type' ] for ch_name , ch_type in zip ( ch_names_tsv , ch_types_json ) : updated_ch_type = bids_to_mne_ch_types . get ( ch_type , None ) if updated_ch_type is None : updated_ch_type = bids_to_mne_ch_types . get ( ch_type . upper ( ) , None ) if updated_ch_type is not None : msg = ( 'The BIDS dataset contains channel types in lowercase ' 'spelling. This violates the BIDS specification and ' 'will raise an error in the future.' ) warn ( msg ) if updated_ch_type is not None : channel_type_dict [ ch_name ] = updated_ch_type synthesized_stim_ch_name = 'STI 014' if ( synthesized_stim_ch_name in raw . ch_names and synthesized_stim_ch_name not in ch_names_tsv ) : logger . info ( f'The stimulus channel "{synthesized_stim_ch_name}" is present in ' f'the raw data, but not included in channels.tsv. Removing the ' f'channel.' ) raw . drop_channels ( [ synthesized_stim_ch_name ] ) if len ( ch_names_tsv ) != len ( raw . ch_names ) : warn ( f'The number of channels in the channels.tsv sidecar file ' f'({len(ch_names_tsv)}) does not match the number of channels ' f'in the raw data file ({len(raw.ch_names)}). Will not try to ' f'set channel names.' ) else : for bids_ch_name , raw_ch_name in zip ( ch_names_tsv , raw . ch_names . copy ( ) ) : if bids_ch_name != raw_ch_name : raw . rename_channels ( { raw_ch_name : bids_ch_name } ) ch_type_map_avail = { ch_name : ch_type for ch_name , ch_type in channel_type_dict . items ( ) if ch_name in raw . ch_names } ch_diff = set ( channel_type_dict . keys ( ) ) - set ( ch_type_map_avail . keys ( ) ) if ch_diff : warn ( f'Cannot set channel type for the following channels, as they ' f'are missing in the raw data: {", ".join(sorted(ch_diff))}' ) raw . set_channel_types ( ch_type_map_avail ) if 'status' in channels_dict : bads_tsv = _get_bads_from_tsv_data ( channels_dict ) bads_avail = [ ch_name for ch_name in bads_tsv if ch_name in raw . ch_names ] ch_diff = set ( bads_tsv ) - set ( bads_avail ) if ch_diff : warn ( f'Cannot set "bad" status for the following channels, as ' f'they are missing in the raw data: ' f'{", ".join(sorted(ch_diff))}' ) raw . info [ 'bads' ] = bads_avail return raw
Read associated channels.tsv and populate raw.
Read associated channels . tsv and populate raw .
"""Read associated channels.tsv and populate raw.

    Updates status (bad) and types of channels.
    """ # Now we can do some work. # The "type" column is mandatory in BIDS. We can use it to set channel # types in the raw data using a mapping between channel types # Get the best mapping we currently have from BIDS to MNE nomenclature # Try to map from BIDS nomenclature to MNE, leave channel type # untouched if we are uncertain # XXX Try again with uppercase spelling – this should be removed # XXX once https://github.com/bids-standard/bids-validator/issues/1018  # noqa:E501 # XXX has been resolved. # XXX x-ref https://github.com/mne-tools/mne-bids/issues/481 # Special handling for (synthesized) stimulus channel # Rename channels in loaded Raw to match those read from the BIDS sidecar # Set the channel types in the raw data according to channels.tsv # Set bad channels based on _channels.tsv sidecar
channels_fname
raw