Skip to content

Commit

Permalink
Merge pull request #44 from WillForan/master
Browse files Browse the repository at this point in the history
error earlier: check tsv columns; create directory
  • Loading branch information
yarikoptic authored Apr 23, 2024
2 parents 91bd117 + 429ab7e commit f08c2d8
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions bids2nda/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,11 @@ def run(args):
"sec": "Seconds",
"msec": "Milliseconds"}

participants_df = pd.read_csv(os.path.join(args.bids_directory, "participants.tsv"), header=0, sep="\t")
participants_file = os.path.join(args.bids_directory, "participants.tsv")
participants_df = pd.read_csv(participants_file, header=0, sep="\t")
if 'age' not in participants_df.columns or 'sex' not in participants_df.columns:
raise Exception(f"{participants_file} must have columns 'age' and 'sex' for nda columns 'interview_age' and 'sex'")


image03_dict = OrderedDict()
for file in glob(os.path.join(args.bids_directory, "sub-*", "*", "sub-*.nii.gz")) + \
Expand All @@ -185,6 +189,10 @@ def run(args):
else:
print("%s file not found - information about scan date required by NDA could not be found." % scans_file)
sys.exit(-1)

if 'filename' not in scans_df.columns or 'acq_time' not in scans_df.columns:
raise Exception(f"{scans_file} must have columns 'filename' and 'acq_time' (YYYY-MM-DD) to create 'interview_date' nda column'")

for (_, row) in scans_df.iterrows():
if file.endswith(row["filename"].replace("/", os.sep)):
date = row.acq_time
Expand All @@ -194,10 +202,14 @@ def run(args):
ndar_date = sdate[1] + "/" + sdate[2].split("T")[0] + "/" + sdate[0]
dict_append(image03_dict, 'interview_date', ndar_date)

interview_age = int(round(list(participants_df[participants_df.participant_id == "sub-" + sub].age)[0]*12, 0))
this_subj = participants_df[participants_df.participant_id == "sub-" + sub]
if this_subj.shape[0] == 0:
raise Exception(f"{participants_file} must have row with particiapnt_id = 'sub-{sub}'")

interview_age = int(round(list(this_subj.age)[0]*12, 0))
dict_append(image03_dict, 'interview_age', interview_age)

sex = list(participants_df[participants_df.participant_id == "sub-" + sub].sex)[0]
sex = list(this_subj.sex)[0]
dict_append(image03_dict, 'gender', sex)

dict_append(image03_dict, 'image_file', file)
Expand Down Expand Up @@ -301,6 +313,9 @@ def run(args):
if len(metadata) > 0 or suffix in ['bold', 'dwi']:
_, fname = os.path.split(file)
zip_name = fname.split(".")[0] + ".metadata.zip"

os.makedirs(args.output_directory, exist_ok=True)

with zipfile.ZipFile(os.path.join(args.output_directory, zip_name), 'w', zipfile.ZIP_DEFLATED) as zipf:

zipf.writestr(fname.replace(".nii.gz", ".json"), json.dumps(metadata, indent=4, sort_keys=True))
Expand Down

0 comments on commit f08c2d8

Please sign in to comment.