Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate participants.tsv if it doesn't exist or update it if subject is missing in the file #244

Merged
merged 22 commits into from
Jun 17, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions phys2bids/cli/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ def _get_parser():
type=str,
help='full path to store channels plot ',
default='')
optional.add_argument('-yml', '--participant-yml',
smoia marked this conversation as resolved.
Show resolved Hide resolved
dest='yml',
type=str,
help='file with info needed to generate participant.tsv file ',
default='participants.yml')
eurunuela marked this conversation as resolved.
Show resolved Hide resolved
optional.add_argument('-debug', '--debug',
dest='debug',
action='store_true',
Expand Down
5 changes: 5 additions & 0 deletions phys2bids/heuristics/participant.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
participant:
participant_id: # Required
age: n/a
sex: n/a
handedness: n/a
9 changes: 8 additions & 1 deletion phys2bids/phys2bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ def print_json(outfile, samp_freq, time_offset, ch_name):

def phys2bids(filename, info=False, indir='.', outdir='.', heur_file=None,
sub=None, ses=None, chtrig=0, chsel=None, num_timepoints_expected=0,
tr=1, thr=None, ch_name=[], chplot='', debug=False, quiet=False):
tr=1, thr=None, ch_name=[], chplot='', debug=False, quiet=False,
yml='participants.yml'):
"""
Main workflow of phys2bids.

Expand Down Expand Up @@ -296,6 +297,12 @@ def phys2bids(filename, info=False, indir='.', outdir='.', heur_file=None,
phys_in.num_timepoints_found, uniq_freq,
phys_out[uniq_freq].start_time, outfile)

# Generate participants.tsv file if it doesn't exist already.
# Update the file if the subject is not in the file.
# Do not update if the subject is already in the file.
if heur_file:
utils.participants_file(indir, outdir, yml, sub)


def _main(argv=None):
options = _get_parser().parse_args(argv)
Expand Down
53 changes: 53 additions & 0 deletions phys2bids/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# -*- coding: utf-8 -*-

from csv import reader, writer
import json
import logging
import os
import sys
from pathlib import Path
import yaml

LGR = logging.getLogger(__name__)

Expand Down Expand Up @@ -280,3 +282,54 @@ def load_heuristic(heuristic):
except Exception as exc:
raise ImportError(f'Failed to import heuristic {heuristic}: {exc}')
return mod


def append_list_as_row(file_name, list_of_elem):
# Open file in append mode
with open(file_name, 'a+', newline='') as write_obj:
# Create a writer object from csv module
csv_writer = writer(write_obj, delimiter='\t')
# Add contents of list as last row in the csv file
csv_writer.writerow(list_of_elem)


def participants_file(indir, outdir, yml, sub):
eurunuela marked this conversation as resolved.
Show resolved Hide resolved

participants_file = os.path.join(outdir, 'participants.tsv')
if not os.path.exists(participants_file):
# Read yaml info if file exists
if os.path.exists(os.path.join(indir, yml)):
with open(os.path.join(indir, yml)) as f:
yaml_data = yaml.load(f, Loader=yaml.FullLoader)
p_id = yaml_data['participant']['participant_id']
p_age = yaml_data['participant']['age']
p_sex = yaml_data['participant']['sex']
p_handedness = yaml_data['participant']['handedness']
else:
# Fill in with data from phys2bids
p_id = sub
p_age = 'n/a'
p_sex = 'n/a'
p_handedness = 'n/a'

header = ['participant_id', 'age', 'sex', 'handedness']
append_list_as_row(participants_file, header)

participants_data = [p_id, p_age, p_sex, p_handedness]
append_list_as_row(participants_file, participants_data)

else:
pf = open(participants_file, 'r')
header = pf.readline().split("\t")
pf.close()
p_id_idx = header.index('participant_id')
sub_exists = False
with open(participants_file) as pf:
tsvreader = reader(pf, delimiter="\t")
for line in tsvreader:
if sub in line[p_id_idx]:
sub_exists = True
break
if not sub_exists:
participants_data = [sub, 'n/a', 'n/a', 'n/a']
append_list_as_row(participants_file, participants_data)
5 changes: 4 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ test =
pytest-cov
interfaces =
%(acq)s
all =
extra =
yaml
%(interfaces)s
all =
%(doc)s
%(extra)s
%(style)s
%(test)s

Expand Down