From 4b30e9a4320b9dbf64b788f375f3101a8ad21f53 Mon Sep 17 00:00:00 2001 From: Will Foran Date: Tue, 18 Jul 2023 18:28:50 -0400 Subject: [PATCH 1/2] main.py: check input tsv columns, raise exceptions --- bids2nda/main.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/bids2nda/main.py b/bids2nda/main.py index 4697525..3181019 100755 --- a/bids2nda/main.py +++ b/bids2nda/main.py @@ -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")) + \ @@ -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' to create 'interview_date' nda column'") + for (_, row) in scans_df.iterrows(): if file.endswith(row["filename"].replace("/", os.sep)): date = row.acq_time @@ -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) From 429ab7e0df3649ec99fce9afe1e6d521f30746d1 Mon Sep 17 00:00:00 2001 From: Will Foran Date: Tue, 18 Jul 2023 18:39:29 -0400 Subject: [PATCH 2/2] create output directory with metadata.zip when does not already exist --- bids2nda/main.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/bids2nda/main.py b/bids2nda/main.py index 3181019..f2d94bd 100755 --- a/bids2nda/main.py +++ b/bids2nda/main.py @@ -191,7 +191,7 @@ def run(args): 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' to create 'interview_date' nda column'") + 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)): @@ -313,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))