def test_append_list_as_row(tmpdir):
    file_name = tmpdir.join('test_row.tsv')
    list_of_elem = ["01", "32", 'some_info', "132.98", 'M']
    utils.append_list_as_row(file_name, list_of_elem)
    with open(file_name, mode='r') as tsv:
        tsv_read = reader(tsv, delimiter="\t")
        for row in tsv_read:
            assert row == list_of_elem
Example #2
0
def participants_file(outdir, yml, sub):
    """
    Create participants.tsv file if it does not exist.

    If it exists and the subject is missing, then add it.
    Otherwise, do nothing.

    Parameters
    ----------
    outdir: path
        Full path to the output directory.
    yml: path
        Full path to the yaml file.
    sub: str
        Subject ID.

    """
    LGR.info('Updating participants.tsv ...')
    file_path = os.path.join(outdir, 'participants.tsv')
    if not os.path.exists(file_path):
        LGR.warning('phys2bids could not find participants.tsv')
        # Read yaml info if file exists
        if '.yml' in yml and os.path.exists(yml):
            LGR.info('Using yaml data to populate participants.tsv')
            with open(yml) as f:
                yaml_data = yaml.load(f, Loader=yaml.FullLoader)
            p_id = f'sub-{sub}'
            p_age = yaml_data['participant']['age']
            p_sex = yaml_data['participant']['sex']
            p_handedness = yaml_data['participant']['handedness']
        else:
            LGR.info('No yaml file was provided. Using phys2bids data to '
                     'populate participants.tsv')
            # Fill in with data from phys2bids
            p_id = f'sub-{sub}'
            p_age = 'n/a'
            p_sex = 'n/a'
            p_handedness = 'n/a'

        # Write to participants.tsv file
        header = ['participant_id', 'age', 'sex', 'handedness']
        utils.append_list_as_row(file_path, header)

        participants_data = [p_id, p_age, p_sex, p_handedness]
        utils.append_list_as_row(file_path, participants_data)

    else:  # If participants.tsv exists only update when subject is not there
        LGR.info('phys2bids found participants.tsv. Updating if needed...')
        # Find participant_id column in header
        pf = open(file_path, 'r')
        header = pf.readline().split("\t")
        header_length = len(header)
        pf.close()
        p_id_idx = header.index('participant_id')

        # Check if subject is already in the file
        sub_exists = False
        with open(file_path) as pf:
            tsvreader = reader(pf, delimiter="\t")
            for line in tsvreader:
                if sub in line[p_id_idx]:
                    sub_exists = True
                    break
        # Only append to file if subject is not in the file
        if not sub_exists:
            LGR.info(f'Appending subjet sub-{sub} to participants.tsv ...')
            participants_data = ['n/a'] * header_length
            participants_data[p_id_idx] = f'sub-{sub}'
            utils.append_list_as_row(file_path, participants_data)