mne_bids/write.py
def _participants_tsv(raw, subject_id, fname, overwrite=False):
    """Create a participants.tsv file and save it.

    This will append any new participant data to the current list if it
    exists. Otherwise a new file will be created with the provided information.

    Parameters
    ----------
    raw : mne.io.Raw
        The data as MNE-Python Raw object.
    subject_id : str
        The subject name in BIDS compatible format ('01', '02', etc.)
    fname : str | mne_bids.BIDSPath
        Filename to save the participants.tsv to.
    overwrite : bool
        Whether to overwrite the existing file.
        Defaults to False.
        If there is already data for the given `subject_id` and overwrite is
        False, an error will be raised.

    """
    subject_id = 'sub-' + subject_id
    data = OrderedDict(participant_id=[subject_id])

    subject_age = "n/a"
    sex = "n/a"
    hand = 'n/a'
    subject_info = raw.info.get('subject_info', None)
    if subject_info is not None:
        # add sex
        sex = _map_options(what='sex', key=subject_info.get('sex', 0),
                           fro='mne', to='bids')

        # add handedness
        hand = _map_options(what='hand', key=subject_info.get('hand', 0),
                            fro='mne', to='bids')

        # determine the age of the participant
        age = subject_info.get('birthday', None)
        meas_date = raw.info.get('meas_date', None)
        if isinstance(meas_date, (tuple, list, np.ndarray)):
            meas_date = meas_date[0]

        if meas_date is not None and age is not None:
            bday = datetime(age[0], age[1], age[2], tzinfo=timezone.utc)
            if isinstance(meas_date, datetime):
                meas_datetime = meas_date
            else:
                meas_datetime = datetime.fromtimestamp(meas_date,
                                                       tz=timezone.utc)
            subject_age = _age_on_date(bday, meas_datetime)
        else:
            subject_age = "n/a"

    data.update({'age': [subject_age], 'sex': [sex], 'hand': [hand]})

    if os.path.exists(fname):
        orig_data = _from_tsv(fname)
        # whether the new data exists identically in the previous data
        exact_included = _contains_row(orig_data,
                                       {'participant_id': subject_id,
                                        'age': subject_age,
                                        'sex': sex,
                                        'hand': hand})
        # whether the subject id is in the previous data
        sid_included = subject_id in orig_data['participant_id']
        # if the subject data provided is different to the currently existing
        # data and overwrite is not True raise an error
        if (sid_included and not exact_included) and not overwrite:
            raise FileExistsError(f'"{subject_id}" already exists in '  # noqa: E501 F821
                                  f'the participant list. Please set '
                                  f'overwrite to True.')

        # Append any columns the original data did not have
        # that mne-bids is trying to write. This handles
        # the edge case where users write participants data for
        # a subset of `hand`, `age` and `sex`.
        for key in data.keys():
            if key in orig_data:
                continue

            # add 'n/a' if any missing columns
            orig_data[key] = ['n/a'] * len(next(iter(data.values())))

        # Append any additional columns that original data had.
        # Keep the original order of the data by looping over
        # the original OrderedDict keys
        col_name = 'participant_id'
        for key in orig_data.keys():
            if key in data:
                continue

            # add original value for any user-appended columns
            # that were not handled by mne-bids
            p_id = data[col_name][0]
            if p_id in orig_data[col_name]:
                row_idx = orig_data[col_name].index(p_id)
                data[key] = [orig_data[key][row_idx]]

        # otherwise add the new data as new row
        data = _combine_rows(orig_data, data, 'participant_id')

    # overwrite is forced to True as all issues with overwrite == False have
    # been handled by this point
    _write_tsv(fname, data, True)
Create a participants.tsv file and save it.

    This will append any new participant data to the current list if it
    exists. Otherwise a new file will be created with the provided information.

    Parameters
    ----------
    raw : mne.io.Raw
        The data as MNE-Python Raw object.
    subject_id : str
        The subject name in BIDS compatible format ('01', '02', etc.)
    fname : str | mne_bids.BIDSPath
        Filename to save the participants.tsv to.
    overwrite : bool
        Whether to overwrite the existing file.
        Defaults to False.
        If there is already data for the given `subject_id` and overwrite is
        False, an error will be raised.

    
Create a participants.tsv file and save it.
This will append any new participant data to the current list if it
exists. Otherwise a new file will be created with the provided information.

Parameters

raw : mne.io.Raw
The data as MNE-Python Raw object.
subject_id : str
The subject name in BIDS compatible format ('01', '02', etc.)
fname : str | mne_bids.BIDSPath
Filename to save the participants.tsv to.
overwrite : bool
Whether to overwrite the existing file.
Defaults to False.
If there is already data for the given `subject_id` and overwrite is
False, an error will be raised.
Create a participants . tsv file and save it . This will append any new participant data to the current list if it exists . Otherwise a new file will be created with the provided information . Parameters raw : mne . io . Raw The data as MNE - Python Raw object . subject_id : str The subject name in BIDS compatible format ( ' 01 ' ' 02 ' etc . ) fname : str | mne_bids . BIDSPath Filename to save the participants . tsv to . overwrite : bool Whether to overwrite the existing file . Defaults to False . If there is already data for the given ` subject_id ` and overwrite is False an error will be raised .
def _participants_tsv(raw, subject_id, fname, overwrite=False):
    subject_id = 'sub-' + subject_id
    data = OrderedDict(participant_id=[subject_id])
    subject_age = "n/a"
    sex = "n/a"
    hand = 'n/a'
    subject_info = raw.info.get('subject_info', None)
    if subject_info is not None:
        sex = _map_options(what='sex', key=subject_info.get('sex', 0),
                           fro='mne', to='bids')
        hand = _map_options(what='hand', key=subject_info.get('hand', 0),
                            fro='mne', to='bids')
        age = subject_info.get('birthday', None)
        meas_date = raw.info.get('meas_date', None)
        if isinstance(meas_date, (tuple, list, np.ndarray)):
            meas_date = meas_date[0]
        if meas_date is not None and age is not None:
            bday = datetime(age[0], age[1], age[2], tzinfo=timezone.utc)
            if isinstance(meas_date, datetime):
                meas_datetime = meas_date
            else:
                meas_datetime = datetime.fromtimestamp(meas_date,
                                                       tz=timezone.utc)
            subject_age = _age_on_date(bday, meas_datetime)
        else:
            subject_age = "n/a"
    data.update({'age': [subject_age], 'sex': [sex], 'hand': [hand]})
    if os.path.exists(fname):
        orig_data = _from_tsv(fname)
        exact_included = _contains_row(orig_data,
                                       {'participant_id': subject_id,
                                        'age': subject_age,
                                        'sex': sex,
                                        'hand': hand})
        sid_included = subject_id in orig_data['participant_id']
        if (sid_included and not exact_included) and not overwrite:
            raise FileExistsError(f'"{subject_id}" already exists in '  
                                  f'the participant list. Please set '
                                  f'overwrite to True.')
        for key in data.keys():
            if key in orig_data:
                continue
            orig_data[key] = ['n/a'] * len(next(iter(data.values())))
        col_name = 'participant_id'
        for key in orig_data.keys():
            if key in data:
                continue
            p_id = data[col_name][0]
            if p_id in orig_data[col_name]:
                row_idx = orig_data[col_name].index(p_id)
                data[key] = [orig_data[key][row_idx]]
        data = _combine_rows(orig_data, data, 'participant_id')
    _write_tsv(fname, data, True)
def _participants_tsv ( raw , subject_id , fname , overwrite = False ) : subject_id = 'sub-' + subject_id data = OrderedDict ( participant_id = [ subject_id ] ) subject_age = "n/a" sex = "n/a" hand = 'n/a' subject_info = raw . info . get ( 'subject_info' , None ) if subject_info is not None : sex = _map_options ( what = 'sex' , key = subject_info . get ( 'sex' , 0 ) , fro = 'mne' , to = 'bids' ) hand = _map_options ( what = 'hand' , key = subject_info . get ( 'hand' , 0 ) , fro = 'mne' , to = 'bids' ) age = subject_info . get ( 'birthday' , None ) meas_date = raw . info . get ( 'meas_date' , None ) if isinstance ( meas_date , ( tuple , list , np . ndarray ) ) : meas_date = meas_date [ 0 ] if meas_date is not None and age is not None : bday = datetime ( age [ 0 ] , age [ 1 ] , age [ 2 ] , tzinfo = timezone . utc ) if isinstance ( meas_date , datetime ) : meas_datetime = meas_date else : meas_datetime = datetime . fromtimestamp ( meas_date , tz = timezone . utc ) subject_age = _age_on_date ( bday , meas_datetime ) else : subject_age = "n/a" data . update ( { 'age' : [ subject_age ] , 'sex' : [ sex ] , 'hand' : [ hand ] } ) if os . path . exists ( fname ) : orig_data = _from_tsv ( fname ) exact_included = _contains_row ( orig_data , { 'participant_id' : subject_id , 'age' : subject_age , 'sex' : sex , 'hand' : hand } ) sid_included = subject_id in orig_data [ 'participant_id' ] if ( sid_included and not exact_included ) and not overwrite : raise FileExistsError ( f'"{subject_id}" already exists in ' f'the participant list. Please set ' f'overwrite to True.' ) for key in data . keys ( ) : if key in orig_data : continue orig_data [ key ] = [ 'n/a' ] * len ( next ( iter ( data . values ( ) ) ) ) col_name = 'participant_id' for key in orig_data . keys ( ) : if key in data : continue p_id = data [ col_name ] [ 0 ] if p_id in orig_data [ col_name ] : row_idx = orig_data [ col_name ] . index ( p_id ) data [ key ] = [ orig_data [ key ] [ row_idx ] ] data = _combine_rows ( orig_data , data , 'participant_id' ) _write_tsv ( fname , data , True )
Create a participants.tsv file and save it.
Create a participants . tsv file and save it .
"""Create a participants.tsv file and save it.

    This will append any new participant data to the current list if it
    exists. Otherwise a new file will be created with the provided information.

    Parameters
    ----------
    raw : mne.io.Raw
        The data as MNE-Python Raw object.
    subject_id : str
        The subject name in BIDS compatible format ('01', '02', etc.)
    fname : str | mne_bids.BIDSPath
        Filename to save the participants.tsv to.
    overwrite : bool
        Whether to overwrite the existing file.
        Defaults to False.
        If there is already data for the given `subject_id` and overwrite is
        False, an error will be raised.

    """ # add sex # add handedness # determine the age of the participant # whether the new data exists identically in the previous data # whether the subject id is in the previous data # if the subject data provided is different to the currently existing # data and overwrite is not True raise an error # noqa: E501 F821 # Append any columns the original data did not have # that mne-bids is trying to write. This handles # the edge case where users write participants data for # a subset of `hand`, `age` and `sex`. # add 'n/a' if any missing columns # Append any additional columns that original data had. # Keep the original order of the data by looping over # the original OrderedDict keys # add original value for any user-appended columns # that were not handled by mne-bids # otherwise add the new data as new row # overwrite is forced to True as all issues with overwrite == False have # been handled by this point
raw
subject_id
fname
overwrite