From 360bb4462905b743743c1b449fee171c416b3396 Mon Sep 17 00:00:00 2001 From: Luke Chang Date: Thu, 24 Feb 2022 19:47:03 -0500 Subject: [PATCH 1/8] Updated documentation for GUID mapping --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e9b180d..d79a10a 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ Extract NIHM Data Archive compatible metadata from Brain Imaging Data Structure GUID_MAPPING Path to a text file with participant_id to GUID mapping. You will need to use the GUID Tool (https://ndar.nih.gov/contribute.html) to generate GUIDs - for your participants. + for your participants. Formatted as - OUTPUT_DIRECTORY Directory where NDA files will be stored optional arguments: @@ -26,10 +26,12 @@ Extract NIHM Data Archive compatible metadata from Brain Imaging Data Structure ## GUID_MAPPING file format -The is the file format produced by the GUID Tool: one line per subject in the format +The is the file format produced by the GUID Tool: one line per subject in the format: ` - ` +If your ids are in the format of `sub-sid001420`, be sure to not include 'sub-' in your `` e.g., ` - ` + ## Example outputs See [/examples](/examples) From a3523f7844c4b48c955d000c2855c2a274b5b015 Mon Sep 17 00:00:00 2001 From: Luke Chang Date: Thu, 24 Feb 2022 19:49:50 -0500 Subject: [PATCH 2/8] updated cosine_to_orientation() function --- bids2nda/main.py | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/bids2nda/main.py b/bids2nda/main.py index 7017865..7e68a8b 100644 --- a/bids2nda/main.py +++ b/bids2nda/main.py @@ -98,6 +98,9 @@ def cosine_to_orientation(iop): express the direction you move, in the DPCS, as you move from row to row, and therefore as the row index changes. + Notes: + Modified Yarik's original solution from https://stackoverflow.com/a/45469577 to use the argmax for increaesd flexibility. + Parameters ---------- iop: list of float @@ -107,21 +110,9 @@ def cosine_to_orientation(iop): ------- {'Axial', 'Coronal', 'Sagittal'} """ - # Solution based on https://stackoverflow.com/a/45469577 - iop_round = np.round(iop) - plane = np.cross(iop_round[0:3], iop_round[3:6]) - plane = np.abs(plane) - if plane[0] == 1: - return "Sagittal" - elif plane[1] == 1: - return "Coronal" - elif plane[2] == 1: - return "Axial" - else: - raise RuntimeError( - "Could not deduce the image orientation of %r. 'plane' value is %r" - % (iop, plane) - ) + planes = ['Sagittal', 'Coronal', 'Axial'] + plane = np.abs(np.cross(iop_round[0:3], iop_round[3:6])) + return planes[np.argmax(plane)] def run(args): From 639403dd193fdbe9f95280f193e6b2640bdb7593 Mon Sep 17 00:00:00 2001 From: Luke Chang Date: Thu, 24 Feb 2022 20:02:20 -0500 Subject: [PATCH 3/8] Updated documentation to clarify GUID mapping --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e9b180d..34a0b25 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ Extract NIHM Data Archive compatible metadata from Brain Imaging Data Structure GUID_MAPPING Path to a text file with participant_id to GUID mapping. You will need to use the GUID Tool (https://ndar.nih.gov/contribute.html) to generate GUIDs - for your participants. + for your participants. Formatted as - OUTPUT_DIRECTORY Directory where NDA files will be stored optional arguments: @@ -30,6 +30,8 @@ The is the file format produced by the GUID Tool: one line per subject in the fo ` - ` +If your ids are in the format of `sub-sid001420`, be sure to not include 'sub-' in your `` e.g., ` - ` + ## Example outputs See [/examples](/examples) From 727d0054e160719fd99641765d29e71222bd2d6e Mon Sep 17 00:00:00 2001 From: Luke Chang Date: Thu, 24 Feb 2022 20:04:22 -0500 Subject: [PATCH 4/8] updated function to increase flexibility --- bids2nda/main.py | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/bids2nda/main.py b/bids2nda/main.py index 7017865..ea7a26e 100644 --- a/bids2nda/main.py +++ b/bids2nda/main.py @@ -98,6 +98,9 @@ def cosine_to_orientation(iop): express the direction you move, in the DPCS, as you move from row to row, and therefore as the row index changes. + Notes: + Modified Yarik's original solution from https://stackoverflow.com/a/45469577 to use the argmax for increaesd flexibility. + Parameters ---------- iop: list of float @@ -107,23 +110,11 @@ def cosine_to_orientation(iop): ------- {'Axial', 'Coronal', 'Sagittal'} """ - # Solution based on https://stackoverflow.com/a/45469577 - iop_round = np.round(iop) - plane = np.cross(iop_round[0:3], iop_round[3:6]) - plane = np.abs(plane) - if plane[0] == 1: - return "Sagittal" - elif plane[1] == 1: - return "Coronal" - elif plane[2] == 1: - return "Axial" - else: - raise RuntimeError( - "Could not deduce the image orientation of %r. 'plane' value is %r" - % (iop, plane) - ) - + planes = ['Sagittal', 'Coronal', 'Axial'] + plane = np.abs(np.cross(np.round(iop[0:3]), np.round(iop[3:6]))) + return planes[np.argmax(plane)] + def run(args): guid_mapping = dict([line.split(" - ") for line in open(args.guid_mapping).read().split("\n") if line != '']) From e1a3b8a5fa4aba507546346e008ba4a001671a17 Mon Sep 17 00:00:00 2001 From: Luke Chang Date: Thu, 24 Feb 2022 23:41:09 -0500 Subject: [PATCH 5/8] changed output to csv --- bids2nda/main.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bids2nda/main.py b/bids2nda/main.py index 32ca7d3..ad6dd76 100644 --- a/bids2nda/main.py +++ b/bids2nda/main.py @@ -377,9 +377,9 @@ def run(args): image03_df = pd.DataFrame(image03_dict) - with open(os.path.join(args.output_directory, "image03.txt"), "w") as out_fp: - out_fp.write('"image"\t"3"\n') - image03_df.to_csv(out_fp, sep="\t", index=False, quoting=csv.QUOTE_ALL) + with open(os.path.join(args.output_directory, "image03.csv"), "w") as out_fp: + out_fp.write('"image","3",') + image03_df.to_csv(out_fp, sep=",", index=False, quoting=csv.QUOTE_ALL) def main(): class MyParser(argparse.ArgumentParser): From 23a6a26d9fd401e80a9c7bd70c5d1eeb47bbcb6e Mon Sep 17 00:00:00 2001 From: Luke Chang Date: Thu, 24 Feb 2022 23:50:07 -0500 Subject: [PATCH 6/8] added experiment_id to arg.parser --- README.md | 1 + bids2nda/main.py | 9 +++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d79a10a..bad76b7 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ Extract NIHM Data Archive compatible metadata from Brain Imaging Data Structure (https://ndar.nih.gov/contribute.html) to generate GUIDs for your participants. Formatted as - OUTPUT_DIRECTORY Directory where NDA files will be stored + EXPERIMENT_ID experiment_id value assigned from NDA after setting the study up throught the NDA website (int) optional arguments: -h, --help show this help message and exit diff --git a/bids2nda/main.py b/bids2nda/main.py index ad6dd76..6f8a36b 100644 --- a/bids2nda/main.py +++ b/bids2nda/main.py @@ -195,10 +195,10 @@ def run(args): suffix = file.split("_")[-1].split(".")[0] if suffix == "bold": description = suffix + " " + metadata["TaskName"] - dict_append(image03_dict, 'experiment_id', metadata.get("ExperimentID", "")) + dict_append(image03_dict, 'experiment_id', args.experiment_id) else: description = suffix - dict_append(image03_dict, 'experiment_id', '') + dict_append(image03_dict, 'experiment_id', args.experiment_id) # Shortcut for the global.const section -- apparently might not be flattened fully metadata_const = metadata.get('global', {}).get('const', {}) dict_append(image03_dict, 'image_description', description) @@ -405,6 +405,11 @@ def error(self, message): "output_directory", help="Directory where NDA files will be stored", metavar="OUTPUT_DIRECTORY") + parser.add_argument( + "experiment_id", + help="Experiment ID assigned by NDA for collection. Requred for fMRI studies", + metavar='EXPERIMENT_ID') + args = parser.parse_args() run(args) From 72b66eb158c7e3f7225afac80b9459ce23e9f916 Mon Sep 17 00:00:00 2001 From: Luke Chang Date: Thu, 24 Feb 2022 23:58:17 -0500 Subject: [PATCH 7/8] fixed new line in output --- bids2nda/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bids2nda/main.py b/bids2nda/main.py index 6f8a36b..ffdc063 100644 --- a/bids2nda/main.py +++ b/bids2nda/main.py @@ -378,7 +378,7 @@ def run(args): image03_df = pd.DataFrame(image03_dict) with open(os.path.join(args.output_directory, "image03.csv"), "w") as out_fp: - out_fp.write('"image","3",') + out_fp.write('"image","3"') image03_df.to_csv(out_fp, sep=",", index=False, quoting=csv.QUOTE_ALL) def main(): From 34e301f4db7ac585f84252f6ce9ebf190d9b2079 Mon Sep 17 00:00:00 2001 From: Luke Chang Date: Fri, 25 Feb 2022 00:01:16 -0500 Subject: [PATCH 8/8] fixed new line for header in output csv file --- bids2nda/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bids2nda/main.py b/bids2nda/main.py index ffdc063..7ee1767 100644 --- a/bids2nda/main.py +++ b/bids2nda/main.py @@ -378,7 +378,7 @@ def run(args): image03_df = pd.DataFrame(image03_dict) with open(os.path.join(args.output_directory, "image03.csv"), "w") as out_fp: - out_fp.write('"image","3"') + out_fp.write('"image","3"\n') image03_df.to_csv(out_fp, sep=",", index=False, quoting=csv.QUOTE_ALL) def main():