From afa3778eb635c7d966c4c87d42a35255fee4cc83 Mon Sep 17 00:00:00 2001 From: pbbaba Date: Thu, 1 Aug 2024 10:42:18 -0400 Subject: [PATCH 01/10] added script to prepare json and yaml files --- json_yaml_files/prepare_files.py | 178 +++++++++++++++++++++++++++++++ 1 file changed, 178 insertions(+) create mode 100644 json_yaml_files/prepare_files.py diff --git a/json_yaml_files/prepare_files.py b/json_yaml_files/prepare_files.py new file mode 100644 index 0000000..32002d0 --- /dev/null +++ b/json_yaml_files/prepare_files.py @@ -0,0 +1,178 @@ +from bids import BIDSLayout +from collections import defaultdict +import re +import json +import sys +import yaml +from pathlib import Path + + +class BIDStoNDAConfigGenerator: + def __init__(self): + self.path = sys.argv[1] + self.layout = BIDSLayout(self.path, derivatives=True) + self.A = 'image03' + self.pattern = r'^(?P[^_]+)_(?P[^.]+)\.(?P[^.]+)\.(?P[^.]+)\..+$' + # self.top_level_files = ['CHANGES', + # 'README', 'dataset_description.json'] + self.scan_type = { + 'anat': { + 'mprage': 'MR structural (MPRAGE)', + 't1w': 'MR structural (T1)', + 'pd': 'MR structural (PD)', + 'fspgr': 'MR structural (FSPGR)', + 'fsip': 'MR structural (FISP)', + 't2w': 'MR structural (T2)', + 'pd_t2': 'MR structural (PD, T2)', + 'b0_map': 'MR structural (B0 map)', + 'b1_map': 'MR structural (B1 map)', + 'flash': 'MR structural (FLASH)', + 'mp2rage': 'MR structural (MP2RAGE)', + 'tse': 'MR structural (TSE)', + 't1w_t2w': 'MR structural (T1, T2)', + 'mpnrage': 'MR structural (MPnRAGE)' + }, + 'pet': { + 'pet': 'PET' + } + } + self.image_modality = { + 'pet': 'PET', + 'anat': 'MRI', + 'func': 'MRI', + 'dwi': 'MRI', + 'fmap': 'MRI' + } + + def extract_file_name_components(self, file_name): + match = re.match(self.pattern, file_name) + if match: + A = match.group('A') + X = match.group('X') + Y = match.group('Y') + Z = match.group('Z') + return A, X, Y, Z + else: + return None + + def prepare_json_contents(self, file_name): + # json_contents = {file: file for file in self.top_level_files} + json_contents = {} + _, X, Y, _ = self.extract_file_name_components(file_name) + files = self.layout.get( + scope=X if X != 'inputs' else 'raw', datatype=Y, return_type='file') + for file_path in files: + sub_index = file_path.find('derivatives') + if sub_index == -1: + sub_index = file_path.find('sub-') + if sub_index != -1: + sub_path = file_path[sub_index:] + entities = self.layout.parse_file_entities(sub_path) + subject = entities.get('subject') + session = entities.get('session') + if subject: + sub_path = sub_path.replace(subject, '{SUBJECT}') + if session: + sub_path = sub_path.replace(session, '{SESSION}') + sub_path = re.sub(r'-\d', '{#}', sub_path) + json_contents[sub_path] = sub_path + finalPath = '/'.join(self.path.split('/')[:-2]) + '/prepared_jsons' + print(finalPath) + Path(finalPath).mkdir(parents=True, exist_ok=True) + with open(f'{finalPath}/'+file_name, 'w') as f: + json.dump(json_contents, f) + + def fetch_scan_type(self, Y, Z): + scan_type = '' + if Z.lower() in self.scan_type[Y]: + scan_type = self.scan_type[Y][Z.lower()] + elif '_' in Z: + first = Z.split('_')[0] + if first.lower() in self.scan_type[Y]: + scan_type = self.scan_type[Y][first.lower()] + if Y in self.scan_type[Y]: + scan_type = self.scan_type[Y][Y] + return scan_type + + def prepare_yaml_contents(self, file_name): + _, _, Y, Z = self.extract_file_name_components(file_name) + + yaml_contents = { + "scan_type": self.fetch_scan_type(Y, Z), + "scan_object": 'LIVE', + "image_file_format": 'NIFTI', + "image_modality": self.image_modality[Y], + "transformation_performed": 'Yes' + } + finalPath = '/'.join(self.path.split('/')[:-2]) + '/prepared_yamls' + Path(finalPath).mkdir(parents=True, exist_ok=True) + with open(f'{finalPath}/'+file_name, 'w') as f: + yaml.dump(yaml_contents, f) + + def fetch_Z_value(self, files): + Z = set() + ignore_list = ['desc', 'subject', 'session', + 'extension', 'suffix', 'datatype'] + for file in files: + entities = self.layout.parse_file_entities(file) + # print(entities) + filtered_entities = {k: v for k, + v in entities.items() if k not in ignore_list} + # print(filtered_entities) + for key, value in filtered_entities.items(): + if key != 'space': + z = key + '-' + value + else: + z = value + if ({'suffix', 'datatype'} <= entities.keys() and entities['suffix'] != entities['datatype']) or \ + ('suffix' in entities and 'datatype' not in entities): + if z: + z = z + '_' + z = z + entities['suffix'] + if z: + Z.add(z) + if not Z and entities['suffix'] != entities['datatype']: + Z.add(entities['suffix']) + return list(Z) + + def prepare_file_names(self, X, Y, Z): + file_names = defaultdict(set) + filename = f"{self.A}_{X}" + file_base = f"{filename}.{Y}." + for z in Z: + file_names['json'].add(file_base + z + '.json') + file_names['yaml'].add(file_base + z + '.yaml') + file_names['json'] = list(file_names['json']) + file_names['yaml'] = list(file_names['yaml']) + return file_names + + def run(self): + X = [] + Xi = self.layout.get(scope='raw') # inputs + # Xd = self.layout.get(scope='derivatives') # derivatives + Xs = self.layout.get(scope='sourcedata') # sourcedata + if Xi: + X.append('inputs') + # if Xd: + # X.append('derivatives') + if Xs: + X.append('sourcedata') + + Y_types = self.layout.get_datatypes() + + for x in X: + for y in Y_types: + files = self.layout.get( + scope=x if x != 'inputs' else 'raw', datatype=y, return_type='file') + Z = self.fetch_Z_value(files) + file_names = self.prepare_file_names(X=x, Y=y, Z=Z) + for file in file_names['json']: + self.prepare_json_contents(file) + for file in file_names['yaml']: + self.prepare_yaml_contents(file) + + +# Example usage: +# path = '/Users/pbaba1/Downloads/NDA_Dataset/ds004733/' +generator = BIDStoNDAConfigGenerator() +generator.run() From 78eb0327c51cd9a8702ff085a337c3186e204759 Mon Sep 17 00:00:00 2001 From: pbbaba Date: Thu, 1 Aug 2024 17:15:48 -0400 Subject: [PATCH 02/10] changes as requested --- json_yaml_files/prepare_files.py | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/json_yaml_files/prepare_files.py b/json_yaml_files/prepare_files.py index 32002d0..3aa141e 100644 --- a/json_yaml_files/prepare_files.py +++ b/json_yaml_files/prepare_files.py @@ -4,17 +4,22 @@ import json import sys import yaml +import argparse from pathlib import Path class BIDStoNDAConfigGenerator: def __init__(self): - self.path = sys.argv[1] - self.layout = BIDSLayout(self.path, derivatives=True) + # fetch the + parser = argparse.ArgumentParser(description='Process a dataset path.') + parser.add_argument('dataset_path', type=str, help='The path to the dataset directory') + args = parser.parse_args() + self.path = args.dataset_path + + self.layout = BIDSLayout(self.path, derivatives=False) self.A = 'image03' self.pattern = r'^(?P[^_]+)_(?P[^.]+)\.(?P[^.]+)\.(?P[^.]+)\..+$' - # self.top_level_files = ['CHANGES', - # 'README', 'dataset_description.json'] + self.scan_type = { 'anat': { 'mprage': 'MR structural (MPRAGE)', @@ -56,15 +61,13 @@ def extract_file_name_components(self, file_name): return None def prepare_json_contents(self, file_name): - # json_contents = {file: file for file in self.top_level_files} json_contents = {} _, X, Y, _ = self.extract_file_name_components(file_name) files = self.layout.get( scope=X if X != 'inputs' else 'raw', datatype=Y, return_type='file') for file_path in files: sub_index = file_path.find('derivatives') - if sub_index == -1: - sub_index = file_path.find('sub-') + if sub_index != -1: sub_path = file_path[sub_index:] entities = self.layout.parse_file_entities(sub_path) @@ -74,8 +77,10 @@ def prepare_json_contents(self, file_name): sub_path = sub_path.replace(subject, '{SUBJECT}') if session: sub_path = sub_path.replace(session, '{SESSION}') - sub_path = re.sub(r'-\d', '{#}', sub_path) + sub_path = re.sub(r'-\d', '-{#}', sub_path) json_contents[sub_path] = sub_path + else: + sub_index = file_path.find('sub-') finalPath = '/'.join(self.path.split('/')[:-2]) + '/prepared_jsons' print(finalPath) Path(finalPath).mkdir(parents=True, exist_ok=True) @@ -99,7 +104,7 @@ def prepare_yaml_contents(self, file_name): yaml_contents = { "scan_type": self.fetch_scan_type(Y, Z), - "scan_object": 'LIVE', + "scan_object": 'Live', "image_file_format": 'NIFTI', "image_modality": self.image_modality[Y], "transformation_performed": 'Yes' @@ -111,14 +116,11 @@ def prepare_yaml_contents(self, file_name): def fetch_Z_value(self, files): Z = set() - ignore_list = ['desc', 'subject', 'session', - 'extension', 'suffix', 'datatype'] + ignore_list = ['desc', 'subject', 'session', 'extension', 'suffix', 'datatype'] for file in files: entities = self.layout.parse_file_entities(file) - # print(entities) filtered_entities = {k: v for k, v in entities.items() if k not in ignore_list} - # print(filtered_entities) for key, value in filtered_entities.items(): if key != 'space': z = key + '-' + value From 3269998ce0b6ca0fbceebcf10b2fb7ac4ed4afe6 Mon Sep 17 00:00:00 2001 From: Eric Earl Date: Fri, 2 Aug 2024 07:47:52 -0700 Subject: [PATCH 03/10] Update json_yaml_files/prepare_files.py Added a small additional description to the ArgumentParser. --- json_yaml_files/prepare_files.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/json_yaml_files/prepare_files.py b/json_yaml_files/prepare_files.py index 3aa141e..74b13e9 100644 --- a/json_yaml_files/prepare_files.py +++ b/json_yaml_files/prepare_files.py @@ -11,7 +11,7 @@ class BIDStoNDAConfigGenerator: def __init__(self): # fetch the - parser = argparse.ArgumentParser(description='Process a dataset path.') + parser = argparse.ArgumentParser(description='Prepare JSON and YAML files for a single dataset path.') parser.add_argument('dataset_path', type=str, help='The path to the dataset directory') args = parser.parse_args() self.path = args.dataset_path From 4e9b5364ac0b7cb0db16c1b87259b539e0d795db Mon Sep 17 00:00:00 2001 From: "Lucille A. Moore" Date: Tue, 13 Aug 2024 14:35:50 -0700 Subject: [PATCH 04/10] Update README.md --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 7767ce3..54f0b81 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,9 @@ + +## :mega: Repository No Longer Being Maintained :mega: + +**We have adopted a new process for storing and sharing ABCD data and will therefore no longer be maintaining this repository!** + + # Welcome to the DCAN Labs NDA BIDS Upload Repository This repository is for taking data as BIDS and uploading it to an NDA collection. From 5b7d0f018d4bae88db7dc6a1382a46b2686fddf6 Mon Sep 17 00:00:00 2001 From: pbbaba Date: Thu, 15 Aug 2024 15:10:24 -0400 Subject: [PATCH 05/10] Addec future work (TODO) to include in the arguments --- json_yaml_files/prepare_files.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/json_yaml_files/prepare_files.py b/json_yaml_files/prepare_files.py index 74b13e9..9d832a1 100644 --- a/json_yaml_files/prepare_files.py +++ b/json_yaml_files/prepare_files.py @@ -1,4 +1,5 @@ from bids import BIDSLayout +import bids from collections import defaultdict import re import json @@ -10,8 +11,12 @@ class BIDStoNDAConfigGenerator: def __init__(self): - # fetch the - parser = argparse.ArgumentParser(description='Prepare JSON and YAML files for a single dataset path.') + # TODO: + # add below in the command line args + # -d: destination folder where the yaml and json files need to be saved + # -s: source directory where the BIDS dataset is located + bids.config.set_option('extension_initial_dot', True) + parser = argparse.ArgumentParser(description='Process a dataset path.') parser.add_argument('dataset_path', type=str, help='The path to the dataset directory') args = parser.parse_args() self.path = args.dataset_path @@ -82,7 +87,6 @@ def prepare_json_contents(self, file_name): else: sub_index = file_path.find('sub-') finalPath = '/'.join(self.path.split('/')[:-2]) + '/prepared_jsons' - print(finalPath) Path(finalPath).mkdir(parents=True, exist_ok=True) with open(f'{finalPath}/'+file_name, 'w') as f: json.dump(json_contents, f) @@ -132,13 +136,20 @@ def fetch_Z_value(self, files): z = z + '_' z = z + entities['suffix'] if z: - Z.add(z) + Z.add(z) if not Z and entities['suffix'] != entities['datatype']: Z.add(entities['suffix']) return list(Z) def prepare_file_names(self, X, Y, Z): file_names = defaultdict(set) + ''' + file_names = { + json: [], + yaml: [] + } + ''' + filename = f"{self.A}_{X}" file_base = f"{filename}.{Y}." for z in Z: From 965edb73d3649723c4915621460c9fee595d31b2 Mon Sep 17 00:00:00 2001 From: pbbaba Date: Thu, 15 Aug 2024 15:12:38 -0400 Subject: [PATCH 06/10] added instructions on how to run the script --- json_yaml_files/prepare_files.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/json_yaml_files/prepare_files.py b/json_yaml_files/prepare_files.py index 9d832a1..3804923 100644 --- a/json_yaml_files/prepare_files.py +++ b/json_yaml_files/prepare_files.py @@ -185,7 +185,11 @@ def run(self): self.prepare_yaml_contents(file) -# Example usage: -# path = '/Users/pbaba1/Downloads/NDA_Dataset/ds004733/' +# Instructions on how to use: +# 1. Make sure you are in 'json_yaml_files' folder of the codebase +# 2. Use the below command to run the script +# python3 prepare_files.py /Users/pbaba1/Downloads/NDA_Dataset/ds004733/ +# where '/Users/pbaba1/Downloads/NDA_Dataset/ds004733/' is the path where the BIDS dataset is located + generator = BIDStoNDAConfigGenerator() generator.run() From 8250a97992cb9722c2c69c0824ebf5085b8a7397 Mon Sep 17 00:00:00 2001 From: pbbaba Date: Thu, 15 Aug 2024 15:21:20 -0400 Subject: [PATCH 07/10] printing concluding statements to indicate where the json and yaml files are created. --- json_yaml_files/prepare_files.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/json_yaml_files/prepare_files.py b/json_yaml_files/prepare_files.py index 3804923..1c2f8ab 100644 --- a/json_yaml_files/prepare_files.py +++ b/json_yaml_files/prepare_files.py @@ -183,7 +183,11 @@ def run(self): self.prepare_json_contents(file) for file in file_names['yaml']: self.prepare_yaml_contents(file) - + print() + print('Please check the following folders for the json and yaml files generated:') + print('JSON files: ' + '/'.join(self.path.split('/')[:-2]) + '/prepared_jsons') + print('YAML files: ' + '/'.join(self.path.split('/')[:-2]) + '/prepared_yamls') + print() # Instructions on how to use: # 1. Make sure you are in 'json_yaml_files' folder of the codebase From 64c74ace0a8b62d94d2afb4ee7b0ae797250daf1 Mon Sep 17 00:00:00 2001 From: "H. Jeremy Bockholt" Date: Tue, 17 Dec 2024 07:37:47 -0500 Subject: [PATCH 08/10] Update prepare_files.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Implemented functionality to generate both JSON and YAML files for derivatives in a BIDS dataset, ensuring compatibility with multiple data types. (Pooja's script was missing this) Fixed bugs in JSON file generation for input data, ensuring accurate file parsing and content preparation. (Pooja's script generated empty JSON files for inputs; I resolved this) Completed the addition of -s (source) and -d (destination) command-line arguments to allow users to specify custom input and output paths. (Pooja's TODO—completed by me) Refactored the script extensively to improve code readability and maintainability. How to Run: cd json_yaml_files python3 prepare_file.py -s -d --- json_yaml_files/prepare_files.py | 215 ++++++++++++++----------------- 1 file changed, 100 insertions(+), 115 deletions(-) diff --git a/json_yaml_files/prepare_files.py b/json_yaml_files/prepare_files.py index 1c2f8ab..6bcc7a0 100644 --- a/json_yaml_files/prepare_files.py +++ b/json_yaml_files/prepare_files.py @@ -11,20 +11,23 @@ class BIDStoNDAConfigGenerator: def __init__(self): - # TODO: - # add below in the command line args - # -d: destination folder where the yaml and json files need to be saved - # -s: source directory where the BIDS dataset is located + # Set BIDS configuration bids.config.set_option('extension_initial_dot', True) - parser = argparse.ArgumentParser(description='Process a dataset path.') - parser.add_argument('dataset_path', type=str, help='The path to the dataset directory') + + # Parse command-line arguments + parser = argparse.ArgumentParser(description='Generate YAML and JSON files for a BIDS dataset.') + parser.add_argument('-s', '--source', type=str, required=True, help='Path to the BIDS dataset directory') + parser.add_argument('-d', '--destination', type=str, required=True, help='Path to save the generated YAML and JSON files') args = parser.parse_args() - self.path = args.dataset_path + self.source_path = args.source + self.destination_path = Path(args.destination) + + # Initialize BIDS layout + self.layout = BIDSLayout(self.source_path, derivatives=True) - self.layout = BIDSLayout(self.path, derivatives=False) + # Define constants self.A = 'image03' self.pattern = r'^(?P[^_]+)_(?P[^.]+)\.(?P[^.]+)\.(?P[^.]+)\..+$' - self.scan_type = { 'anat': { 'mprage': 'MR structural (MPRAGE)', @@ -53,55 +56,47 @@ def __init__(self): 'dwi': 'MRI', 'fmap': 'MRI' } + + # Set output directories + self.json_dir = self.destination_path / 'prepared_jsons' + self.yaml_dir = self.destination_path / 'prepared_yamls' + self.json_dir.mkdir(parents=True, exist_ok=True) + self.yaml_dir.mkdir(parents=True, exist_ok=True) def extract_file_name_components(self, file_name): match = re.match(self.pattern, file_name) - if match: - A = match.group('A') - X = match.group('X') - Y = match.group('Y') - Z = match.group('Z') - return A, X, Y, Z - else: - return None - - def prepare_json_contents(self, file_name): + return match.groups() if match else (None, None, None, None) + + + def prepare_json_contents(self, file_name, files): json_contents = {} - _, X, Y, _ = self.extract_file_name_components(file_name) - files = self.layout.get( - scope=X if X != 'inputs' else 'raw', datatype=Y, return_type='file') + _, X, _, _ = self.extract_file_name_components(file_name) + + sub_path_prefix = { + 'derivatives': 'derivatives', + 'inputs': 'sub-' + } + for file_path in files: - sub_index = file_path.find('derivatives') - - if sub_index != -1: - sub_path = file_path[sub_index:] - entities = self.layout.parse_file_entities(sub_path) - subject = entities.get('subject') - session = entities.get('session') - if subject: - sub_path = sub_path.replace(subject, '{SUBJECT}') - if session: - sub_path = sub_path.replace(session, '{SESSION}') - sub_path = re.sub(r'-\d', '-{#}', sub_path) - json_contents[sub_path] = sub_path - else: - sub_index = file_path.find('sub-') - finalPath = '/'.join(self.path.split('/')[:-2]) + '/prepared_jsons' - Path(finalPath).mkdir(parents=True, exist_ok=True) - with open(f'{finalPath}/'+file_name, 'w') as f: - json.dump(json_contents, f) + sub_path = file_path[file_path.find(sub_path_prefix.get(X, '')):] if X in sub_path_prefix else file_path + entities = self.layout.parse_file_entities(sub_path) + subject, session = entities.get('subject'), entities.get('session') + sub_path = self._replace_placeholders(sub_path, subject, session) + json_contents[sub_path] = sub_path + + output_file = self.json_dir / file_name + self._write_to_file(output_file, json_contents) + def fetch_scan_type(self, Y, Z): - scan_type = '' - if Z.lower() in self.scan_type[Y]: - scan_type = self.scan_type[Y][Z.lower()] - elif '_' in Z: - first = Z.split('_')[0] - if first.lower() in self.scan_type[Y]: - scan_type = self.scan_type[Y][first.lower()] - if Y in self.scan_type[Y]: - scan_type = self.scan_type[Y][Y] - return scan_type + if Z.lower() in self.scan_type.get(Y, {}): + return self.scan_type[Y][Z.lower()] + if '_' in Z: + prefix = Z.split('_')[0] + if prefix.lower() in self.scan_type.get(Y, {}): + return self.scan_type[Y][prefix.lower()] + return self.scan_type.get(Y, {}).get(Y, '') + def prepare_yaml_contents(self, file_name): _, _, Y, Z = self.extract_file_name_components(file_name) @@ -111,89 +106,79 @@ def prepare_yaml_contents(self, file_name): "scan_object": 'Live', "image_file_format": 'NIFTI', "image_modality": self.image_modality[Y], - "transformation_performed": 'Yes' + "transformation_performed": 'No' } - finalPath = '/'.join(self.path.split('/')[:-2]) + '/prepared_yamls' - Path(finalPath).mkdir(parents=True, exist_ok=True) - with open(f'{finalPath}/'+file_name, 'w') as f: - yaml.dump(yaml_contents, f) + output_file = self.yaml_dir / file_name + self._write_to_file(output_file, yaml_contents, file_format='yaml') + def fetch_Z_value(self, files): Z = set() ignore_list = ['desc', 'subject', 'session', 'extension', 'suffix', 'datatype'] + for file in files: entities = self.layout.parse_file_entities(file) - filtered_entities = {k: v for k, - v in entities.items() if k not in ignore_list} + filtered_entities = {k: v for k, v in entities.items() if k not in ignore_list} + for key, value in filtered_entities.items(): - if key != 'space': - z = key + '-' + value - else: - z = value - if ({'suffix', 'datatype'} <= entities.keys() and entities['suffix'] != entities['datatype']) or \ - ('suffix' in entities and 'datatype' not in entities): - if z: - z = z + '_' - z = z + entities['suffix'] + z = f"{key}-{value}" if key != 'space' else value + if entities.get('suffix') and entities.get('suffix') != entities.get('datatype'): + z = f"{z}_{entities['suffix']}" if z else entities['suffix'] if z: Z.add(z) if not Z and entities['suffix'] != entities['datatype']: Z.add(entities['suffix']) + return list(Z) def prepare_file_names(self, X, Y, Z): - file_names = defaultdict(set) - ''' - file_names = { - json: [], - yaml: [] - } - ''' - - filename = f"{self.A}_{X}" - file_base = f"{filename}.{Y}." - for z in Z: - file_names['json'].add(file_base + z + '.json') - file_names['yaml'].add(file_base + z + '.yaml') - file_names['json'] = list(file_names['json']) - file_names['yaml'] = list(file_names['yaml']) - return file_names - - def run(self): - X = [] - Xi = self.layout.get(scope='raw') # inputs - # Xd = self.layout.get(scope='derivatives') # derivatives - Xs = self.layout.get(scope='sourcedata') # sourcedata - if Xi: - X.append('inputs') - # if Xd: - # X.append('derivatives') - if Xs: - X.append('sourcedata') - + file_base = f"{self.A}_{X}.{Y}." + return { + 'json': [f"{file_base}{z}.json" for z in Z], + 'yaml': [f"{file_base}{z}.yaml" for z in Z] + } + + + def _replace_placeholders(self, path, subject, session): + if subject: + path = path.replace(subject, '{SUBJECT}') + if session: + path = path.replace(session, '{SESSION}') + return re.sub(r'-\d', '-{#}', path) + + def _write_to_file(self, output_file, contents, file_format='json'): + with open(output_file, 'w') as f: + if file_format == 'yaml': + yaml.dump(contents, f) + else: + json.dump(contents, f, indent=2) + + + def run(self): + scopes = { + 'raw': 'inputs', + 'derivatives': 'derivatives', + 'sourcedata': 'sourcedata' + } + X = [name for scope, name in scopes.items() if self.layout.get(scope=scope)] Y_types = self.layout.get_datatypes() - + for x in X: for y in Y_types: files = self.layout.get( scope=x if x != 'inputs' else 'raw', datatype=y, return_type='file') + Z = self.fetch_Z_value(files) file_names = self.prepare_file_names(X=x, Y=y, Z=Z) - for file in file_names['json']: - self.prepare_json_contents(file) - for file in file_names['yaml']: - self.prepare_yaml_contents(file) - print() - print('Please check the following folders for the json and yaml files generated:') - print('JSON files: ' + '/'.join(self.path.split('/')[:-2]) + '/prepared_jsons') - print('YAML files: ' + '/'.join(self.path.split('/')[:-2]) + '/prepared_yamls') - print() - -# Instructions on how to use: -# 1. Make sure you are in 'json_yaml_files' folder of the codebase -# 2. Use the below command to run the script -# python3 prepare_files.py /Users/pbaba1/Downloads/NDA_Dataset/ds004733/ -# where '/Users/pbaba1/Downloads/NDA_Dataset/ds004733/' is the path where the BIDS dataset is located - -generator = BIDStoNDAConfigGenerator() -generator.run() + + for file_name in file_names['json']: + self.prepare_json_contents(file_name, files) + for file_name in file_names['yaml']: + self.prepare_yaml_contents(file_name) + + print(f"\nJSON files: {self.json_dir}") + print(f"YAML files: {self.yaml_dir}\n") + +if __name__ == "__main__": + generator = BIDStoNDAConfigGenerator() + generator.run() From 40450f2f54ca4ab4374c517c4d2bbf62eb86c170 Mon Sep 17 00:00:00 2001 From: Girish <79399756+Girish-Anadv-07@users.noreply.github.com> Date: Tue, 17 Dec 2024 10:27:40 -0500 Subject: [PATCH 09/10] Update README.md Co-authored-by: Eric Earl --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 54f0b81..0c2b212 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ -## :mega: Repository No Longer Being Maintained :mega: +## :mega: Repository No Longer Being Maintained by DCAN-Labs :mega: -**We have adopted a new process for storing and sharing ABCD data and will therefore no longer be maintaining this repository!** +**DCAN-Labs has adopted a new process for storing and sharing ABCD data and will therefore no longer be maintaining this repository!** # Welcome to the DCAN Labs NDA BIDS Upload Repository From 5833589c921b6dca1b20445a6f925c743d7ef3d6 Mon Sep 17 00:00:00 2001 From: Girish <79399756+Girish-Anadv-07@users.noreply.github.com> Date: Tue, 17 Dec 2024 10:27:55 -0500 Subject: [PATCH 10/10] Update README.md Co-authored-by: Eric Earl --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0c2b212..8502559 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ **DCAN-Labs has adopted a new process for storing and sharing ABCD data and will therefore no longer be maintaining this repository!** -# Welcome to the DCAN Labs NDA BIDS Upload Repository +# Welcome to the NDA BIDS Upload Repository This repository is for taking data as BIDS and uploading it to an NDA collection.