Пример #1
0
def _save_bads(*, bads, descriptions, bids_path):
    """Update the set of channels marked as bad.

    Parameters
    ----------
    bads : list
        The complete list of bad channels.
    descriptions : list
        The values to be written to the `status_description` column.
    """
    # We first make all channels not passed as bad here to be marked as good.
    mark_channels(bids_path=bids_path, ch_names=[], status='good')
    mark_channels(bids_path=bids_path,
                  ch_names=bads,
                  status='bad',
                  descriptions=descriptions)
Пример #2
0
def run():
    """Run the mark_channels command."""
    from mne.commands.utils import get_optparser

    parser = get_optparser(__file__,
                           usage="usage: %prog options args",
                           prog_prefix='mne_bids',
                           version=mne_bids.__version__)

    parser.add_option('--ch_name',
                      dest='ch_names',
                      action='append',
                      default=[],
                      help='The names of the bad channels. If multiple '
                      'channels are bad, pass the --ch_name parameter '
                      'multiple times.')
    parser.add_option('--status',
                      default='bad',
                      help='Status of the channels (Either "good", or "bad").')
    parser.add_option('--description',
                      dest='descriptions',
                      action='append',
                      default=[],
                      help='Descriptions as to why the channels are bad. '
                      'Must match the number of bad channels provided. '
                      'Pass multiple times to supply more than one '
                      'value in that case.')
    parser.add_option('--bids_root',
                      dest='bids_root',
                      help='The path of the folder containing the BIDS '
                      'dataset')
    parser.add_option('--subject_id', dest='subject', help=('Subject name'))
    parser.add_option('--session_id', dest='session', help='Session name')
    parser.add_option('--task', dest='task', help='Task name')
    parser.add_option('--acq',
                      dest='acquisition',
                      help='Acquisition parameter')
    parser.add_option('--run', dest='run', help='Run number')
    parser.add_option('--proc', dest='processing', help='Processing label.')
    parser.add_option('--rec', dest='recording', help='Recording name')
    parser.add_option('--type',
                      dest='datatype',
                      help='Recording data type, e.g. meg, ieeg or eeg')
    parser.add_option('--suffix',
                      dest='suffix',
                      help='The filename suffix, i.e. the last part before '
                      'the extension')
    parser.add_option('--ext',
                      dest='extension',
                      help='The filename extension, including the leading '
                      'period, e.g. .fif')
    parser.add_option('--verbose',
                      dest='verbose',
                      action='store_true',
                      help='Whether do generate additional diagnostic output')

    opt, args = parser.parse_args()
    if args:
        parser.print_help()
        parser.error(f'Please do not specify arguments without flags. '
                     f'Got: {args}.\n')

    if opt.bids_root is None:
        parser.print_help()
        parser.error('You must specify bids_root')
    if opt.ch_names is None:
        parser.print_help()
        parser.error('You must specify some --ch_name parameters.')

    status = opt.status
    ch_names = [] if opt.ch_names == [''] else opt.ch_names
    bids_path = BIDSPath(subject=opt.subject,
                         session=opt.session,
                         task=opt.task,
                         acquisition=opt.acquisition,
                         run=opt.run,
                         processing=opt.processing,
                         recording=opt.recording,
                         datatype=opt.datatype,
                         suffix=opt.suffix,
                         extension=opt.extension,
                         root=opt.bids_root)

    bids_paths = bids_path.match()
    # Only keep data we can actually read & write.
    allowed_extensions = list(reader.keys())
    bids_paths = [p for p in bids_paths if p.extension in allowed_extensions]

    if not bids_paths:
        logger.info('No matching files found. Please consider using a less '
                    'restrictive set of entities to broaden the search.')
        return  # XXX should be return with an error code?

    logger.info(f'Marking channels {", ".join(ch_names)} as bad in '
                f'{len(bids_paths)} recording(s) …')
    for bids_path in bids_paths:
        logger.info(f'Processing: {bids_path.basename}')
        mark_channels(bids_path=bids_path,
                      ch_names=ch_names,
                      status=status,
                      descriptions=opt.descriptions,
                      verbose=opt.verbose)
Пример #3
0
# Read the (now BIDS-formatted) data and print a list of channels currently
# marked as bad.

raw = read_raw_bids(bids_path=bids_path, verbose=False)
print(f'The following channels are currently marked as bad:\n'
      f'    {", ".join(raw.info["bads"])}\n')

# %%
# So currently, two channels are marked as bad: ``EEG 053`` and ``MEG 2443``.
# Let's assume that through visual data inspection, we found that two more
# MEG channels are problematic, and we would like to mark them as bad as well.
# To do that, we simply add them to a list, which we then pass to
# :func:`mne_bids.mark_channels`:

bads = ['MEG 0112', 'MEG 0131']
mark_channels(bids_path=bids_path, ch_names=bads, status='bad',
              verbose=False)

# %%
# That's it! Let's verify the result.

raw = read_raw_bids(bids_path=bids_path, verbose=False)
print(f'After marking MEG 0112 and MEG 0131 as bad, the following channels '
      f'are now marked as bad:\n    {", ".join(raw.info["bads"])}\n')

# %%
# As you can see, now a total of **four** channels is marked as bad: the ones
# that were already bad when we started – ``EEG 053`` and ``MEG 2443`` – and
# the two channels we passed to :func:`mne_bids.mark_channels` –
# ``MEG 0112`` and ``MEG 0131``. This shows that marking bad channels via
# :func:`mne_bids.mark_channels`, by default, is an **additive** procedure,
# which allows you to mark additional channels as bad while retaining the