mne_bids/read.py
def _handle_scans_reading(scans_fname, raw, bids_path):
    """Read associated scans.tsv and set meas_date."""
    scans_tsv = _from_tsv(scans_fname)
    fname = bids_path.fpath.name

    if fname.endswith('.pdf'):
        # for BTI files, the scan is an entire directory
        fname = fname.split('.')[0]

    # get the row corresponding to the file
    # use string concatenation instead of os.path
    # to work nicely with windows
    data_fname = bids_path.datatype + '/' + fname
    fnames = scans_tsv['filename']
    if 'acq_time' in scans_tsv:
        acq_times = scans_tsv['acq_time']
    else:
        acq_times = ['n/a'] * len(fnames)

    row_ind = fnames.index(data_fname)

    # check whether all split files have the same acq_time
    # and throw an error if they don't
    if '_split-' in fname:
        split_idx = fname.find('split-')
        pattern = re.compile(bids_path.datatype + '/' +
                             bids_path.basename[:split_idx] +
                             r'split-\d+_' + bids_path.datatype +
                             bids_path.fpath.suffix)
        split_fnames = list(filter(pattern.match, fnames))
        split_acq_times = []
        for split_f in split_fnames:
            split_acq_times.append(acq_times[fnames.index(split_f)])
        if len(set(split_acq_times)) != 1:
            raise ValueError("Split files must have the same acq_time.")

    # extract the acquisition time from scans file
    acq_time = acq_times[row_ind]
    if acq_time != 'n/a':
        # microseconds in the acquisition time is optional
        if '.' not in acq_time:
            # acquisition time ends with '.%fZ' microseconds string
            acq_time += '.0Z'
        acq_time = datetime.strptime(acq_time, '%Y-%m-%dT%H:%M:%S.%fZ')
        acq_time = acq_time.replace(tzinfo=timezone.utc)

        logger.debug(f'Loaded {scans_fname} scans file to set '
                     f'acq_time as {acq_time}.')
        # First set measurement date to None and then call call anonymize() to
        # remove any traces of the measurement date we wish
        # to replace – it might lurk out in more places than just
        # raw.info['meas_date'], e.g. in info['meas_id]['secs'] and in
        # info['file_id'], which are not affected by set_meas_date().
        # The combined use of set_meas_date(None) and anonymize() is suggested
        # by the MNE documentation, and in fact we cannot load e.g. OpenNeuro
        # ds003392 without this combination.
        raw.set_meas_date(None)
        with warnings.catch_warnings():
            # This is to silence a warning emitted by MNE-Python < 0.24. The
            # warnings filter can be safely removed once we drop support for
            # MNE-Python 0.23 and older.
            warnings.filterwarnings(
                action='ignore',
                message="Input info has 'meas_date' set to None",
                category=RuntimeWarning,
                module='mne'
            )
            raw.anonymize(daysback=None, keep_his=True)
        raw.set_meas_date(acq_time)

    return raw
Read associated scans.tsv and set meas_date.
Read associated scans.tsv and set meas_date.
Read associated scans . tsv and set meas_date .
def _handle_scans_reading(scans_fname, raw, bids_path):
    scans_tsv = _from_tsv(scans_fname)
    fname = bids_path.fpath.name
    if fname.endswith('.pdf'):
        fname = fname.split('.')[0]
    data_fname = bids_path.datatype + '/' + fname
    fnames = scans_tsv['filename']
    if 'acq_time' in scans_tsv:
        acq_times = scans_tsv['acq_time']
    else:
        acq_times = ['n/a'] * len(fnames)
    row_ind = fnames.index(data_fname)
    if '_split-' in fname:
        split_idx = fname.find('split-')
        pattern = re.compile(bids_path.datatype + '/' +
                             bids_path.basename[:split_idx] +
                             r'split-\d+_' + bids_path.datatype +
                             bids_path.fpath.suffix)
        split_fnames = list(filter(pattern.match, fnames))
        split_acq_times = []
        for split_f in split_fnames:
            split_acq_times.append(acq_times[fnames.index(split_f)])
        if len(set(split_acq_times)) != 1:
            raise ValueError("Split files must have the same acq_time.")
    acq_time = acq_times[row_ind]
    if acq_time != 'n/a':
        if '.' not in acq_time:
            acq_time += '.0Z'
        acq_time = datetime.strptime(acq_time, '%Y-%m-%dT%H:%M:%S.%fZ')
        acq_time = acq_time.replace(tzinfo=timezone.utc)
        logger.debug(f'Loaded {scans_fname} scans file to set '
                     f'acq_time as {acq_time}.')
        raw.set_meas_date(None)
        with warnings.catch_warnings():
            warnings.filterwarnings(
                action='ignore',
                message="Input info has 'meas_date' set to None",
                category=RuntimeWarning,
                module='mne'
            )
            raw.anonymize(daysback=None, keep_his=True)
        raw.set_meas_date(acq_time)
    return raw
def _handle_scans_reading ( scans_fname , raw , bids_path ) : scans_tsv = _from_tsv ( scans_fname ) fname = bids_path . fpath . name if fname . endswith ( '.pdf' ) : fname = fname . split ( '.' ) [ 0 ] data_fname = bids_path . datatype + '/' + fname fnames = scans_tsv [ 'filename' ] if 'acq_time' in scans_tsv : acq_times = scans_tsv [ 'acq_time' ] else : acq_times = [ 'n/a' ] * len ( fnames ) row_ind = fnames . index ( data_fname ) if '_split-' in fname : split_idx = fname . find ( 'split-' ) pattern = re . compile ( bids_path . datatype + '/' + bids_path . basename [ : split_idx ] + r'split-\d+_' + bids_path . datatype + bids_path . fpath . suffix ) split_fnames = list ( filter ( pattern . match , fnames ) ) split_acq_times = [ ] for split_f in split_fnames : split_acq_times . append ( acq_times [ fnames . index ( split_f ) ] ) if len ( set ( split_acq_times ) ) != 1 : raise ValueError ( "Split files must have the same acq_time." ) acq_time = acq_times [ row_ind ] if acq_time != 'n/a' : if '.' not in acq_time : acq_time += '.0Z' acq_time = datetime . strptime ( acq_time , '%Y-%m-%dT%H:%M:%S.%fZ' ) acq_time = acq_time . replace ( tzinfo = timezone . utc ) logger . debug ( f'Loaded {scans_fname} scans file to set ' f'acq_time as {acq_time}.' ) raw . set_meas_date ( None ) with warnings . catch_warnings ( ) : warnings . filterwarnings ( action = 'ignore' , message = "Input info has 'meas_date' set to None" , category = RuntimeWarning , module = 'mne' ) raw . anonymize ( daysback = None , keep_his = True ) raw . set_meas_date ( acq_time ) return raw
Read associated scans.tsv and set meas_date.
Read associated scans . tsv and set meas_date .
"""Read associated scans.tsv and set meas_date.""" # for BTI files, the scan is an entire directory # get the row corresponding to the file # use string concatenation instead of os.path # to work nicely with windows # check whether all split files have the same acq_time # and throw an error if they don't # extract the acquisition time from scans file # microseconds in the acquisition time is optional # acquisition time ends with '.%fZ' microseconds string # First set measurement date to None and then call call anonymize() to # remove any traces of the measurement date we wish # to replace – it might lurk out in more places than just # raw.info['meas_date'], e.g. in info['meas_id]['secs'] and in # info['file_id'], which are not affected by set_meas_date(). # The combined use of set_meas_date(None) and anonymize() is suggested # by the MNE documentation, and in fact we cannot load e.g. OpenNeuro # ds003392 without this combination. # This is to silence a warning emitted by MNE-Python < 0.24. The # warnings filter can be safely removed once we drop support for # MNE-Python 0.23 and older.
scans_fname
raw
bids_path