From 78569df65c2dd5b9b6f9bd8092f3e383c5b5986b Mon Sep 17 00:00:00 2001 From: Alexandre Pron Date: Wed, 27 Nov 2024 10:41:55 +0100 Subject: [PATCH 1/6] [BF]: remove duplicated '_' character in BIDS filename --- shanoir2bids.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/shanoir2bids.py b/shanoir2bids.py index 644bba0..3fae7c1 100755 --- a/shanoir2bids.py +++ b/shanoir2bids.py @@ -200,7 +200,12 @@ def generate_bids_heuristic_file( heuristic = f"""from heudiconv.heuristics.reproin import create_key def create_bids_key(dataset): - + split_filename = dataset['bidsName'].split('_') + # insert additional run key to dissociate indentical scans + file_suffix = "_".join(split_filename[:-1]) + '_' + r"run-{{item:02d}}_" + split_filename[-1] + if len(split_filename) == 1: + # remove unwanted first "_" + file_suffix = file_suffix[1:] template = create_key(subdir=dataset['bidsDir'],file_suffix="_".join(dataset['bidsName'].split('_')[:-1]) + '_' + r"run-{{item:02d}}_" + dataset['bidsName'].split('_')[-1],outtype={outtype}) return template From d2228801b2ba56ab3ffb383df1e15f75e2a83b57 Mon Sep 17 00:00:00 2001 From: Alexandre Pron Date: Wed, 27 Nov 2024 10:41:55 +0100 Subject: [PATCH 2/6] [BF]: remove duplicated '_' character in BIDS filename --- shanoir2bids.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/shanoir2bids.py b/shanoir2bids.py index bdaf86d..36e2677 100755 --- a/shanoir2bids.py +++ b/shanoir2bids.py @@ -200,7 +200,12 @@ def generate_bids_heuristic_file( heuristic = f"""from heudiconv.heuristics.reproin import create_key def create_bids_key(dataset): - + split_filename = dataset['bidsName'].split('_') + # insert additional run key to dissociate indentical scans + file_suffix = "_".join(split_filename[:-1]) + '_' + r"run-{{item:02d}}_" + split_filename[-1] + if len(split_filename) == 1: + # remove unwanted first "_" + file_suffix = file_suffix[1:] template = create_key(subdir=dataset['bidsDir'],file_suffix="_".join(dataset['bidsName'].split('_')[:-1]) + '_' + r"run-{{item:02d}}_" + dataset['bidsName'].split('_')[-1],outtype={outtype}) return template From 3e78538c9263dc0b719102b01902d85bdfaa74d8 Mon Sep 17 00:00:00 2001 From: Alexandre Pron Date: Wed, 27 Nov 2024 11:16:42 +0100 Subject: [PATCH 3/6] [BF]: remove duplicated '_' character in BIDS filename --- shanoir2bids.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/shanoir2bids.py b/shanoir2bids.py index 36e2677..d6bab1a 100755 --- a/shanoir2bids.py +++ b/shanoir2bids.py @@ -206,7 +206,7 @@ def create_bids_key(dataset): if len(split_filename) == 1: # remove unwanted first "_" file_suffix = file_suffix[1:] - template = create_key(subdir=dataset['bidsDir'],file_suffix="_".join(dataset['bidsName'].split('_')[:-1]) + '_' + r"run-{{item:02d}}_" + dataset['bidsName'].split('_')[-1],outtype={outtype}) + template = create_key(subdir=dataset['bidsDir'],file_suffix=file_suffix,outtype={outtype}) return template def get_dataset_to_key_mapping(shanoir2bids): @@ -219,7 +219,9 @@ def get_dataset_to_key_mapping(shanoir2bids): def simplify_runs(info): info_final = dict() for key in info.keys(): + print(key) if len(info[key])==1: + print('Simplified key', key) new_template = key[0].replace('run-{{item:02d}}_','') new_key = (new_template, key[1], key[2]) info_final[new_key] = info[key] @@ -723,7 +725,7 @@ def escape_solr_special_characters(s): "overwrite": True, } - if self.longitudinal: + if self.longitudinal and bids_seq_session is not None: workflow_params["session"] = bids_seq_session try: workflow(**workflow_params) From 388ff55075e14aa3f6a3547d696a8f0fe8db7958 Mon Sep 17 00:00:00 2001 From: Alexandre Pron Date: Wed, 27 Nov 2024 14:25:52 +0100 Subject: [PATCH 4/6] [BF]: remove duplicated '_' character in BIDS filename --- shanoir2bids.py | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/shanoir2bids.py b/shanoir2bids.py index d6bab1a..27e3a62 100755 --- a/shanoir2bids.py +++ b/shanoir2bids.py @@ -24,6 +24,7 @@ import shanoir_downloader from dotenv import load_dotenv from heudiconv.main import workflow +from heudiconv.bids import sanitize_label import bids_validator @@ -199,13 +200,28 @@ def generate_bids_heuristic_file( heuristic = f"""from heudiconv.heuristics.reproin import create_key + def create_bids_key(dataset): - split_filename = dataset['bidsName'].split('_') - # insert additional run key to dissociate indentical scans - file_suffix = "_".join(split_filename[:-1]) + '_' + r"run-{{item:02d}}_" + split_filename[-1] - if len(split_filename) == 1: - # remove unwanted first "_" - file_suffix = file_suffix[1:] + + from heudiconv.bids import BIDSFile + + bids_entities = BIDSFile._known_entities + # check where to insert run key + split_keyword = '_' # default + for entity in bids_entities[bids_entities.index('run') + 1 :]: + if entity in dataset['bidsName']: + split_keyword = '_' + entity + break + split_filename = dataset['bidsName'].split(split_keyword) + if split_keyword != '_': + file_suffix = "".join(split_filename[:-1]) + '_' + r"run-{{item:02d}}" + split_keyword + split_filename[-1] + else: + # insert additional run key to dissociate identical scans + file_suffix = "_".join(split_filename[:-1]) + '_' + r"run-{{item:02d}}" + split_keyword + split_filename[-1] + if len(split_filename) == 1: + # remove unwanted first "_" + file_suffix = file_suffix[1:] + print(file_suffix) template = create_key(subdir=dataset['bidsDir'],file_suffix=file_suffix,outtype={outtype}) return template @@ -453,12 +469,12 @@ def is_mapping_bids(self): paths = ( "/" + "sub-" - + subject + + sanitize_label(subject) + "/" + map["bidsDir"] + "/" + "sub-" - + subject + + sanitize_label(subject) + "_" + map["bidsName"] + extension @@ -469,18 +485,18 @@ def is_mapping_bids(self): paths = ( "/" + "sub-" - + subject + + sanitize_label(subject) + "/" + "ses-" - + map["bidsSession"] + + sanitize_label(map["bidsSession"]) + "/" + map["bidsDir"] + "/" + "sub-" - + subject + + sanitize_label(subject) + "_" + "ses-" - + map["bidsSession"] + + sanitize_label(map["bidsSession"]) + "_" + map["bidsName"] + extension From 6c1b3f3dba3942b3505673a3e0d64e30729f6106 Mon Sep 17 00:00:00 2001 From: Alexandre Pron Date: Wed, 27 Nov 2024 14:31:03 +0100 Subject: [PATCH 5/6] [BF]: remove duplicated '_' character in BIDS filename --- shanoir2bids.py | 1 - 1 file changed, 1 deletion(-) diff --git a/shanoir2bids.py b/shanoir2bids.py index 27e3a62..78b9df9 100755 --- a/shanoir2bids.py +++ b/shanoir2bids.py @@ -221,7 +221,6 @@ def create_bids_key(dataset): if len(split_filename) == 1: # remove unwanted first "_" file_suffix = file_suffix[1:] - print(file_suffix) template = create_key(subdir=dataset['bidsDir'],file_suffix=file_suffix,outtype={outtype}) return template From 54a704442627c45bff78c7c340d5ebd46e185bce Mon Sep 17 00:00:00 2001 From: Alexandre Pron Date: Wed, 27 Nov 2024 15:00:52 +0100 Subject: [PATCH 6/6] [ENH]: support run keyword in bidsName --- shanoir2bids.py | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/shanoir2bids.py b/shanoir2bids.py index 78b9df9..448606b 100755 --- a/shanoir2bids.py +++ b/shanoir2bids.py @@ -205,22 +205,29 @@ def create_bids_key(dataset): from heudiconv.bids import BIDSFile - bids_entities = BIDSFile._known_entities - # check where to insert run key - split_keyword = '_' # default - for entity in bids_entities[bids_entities.index('run') + 1 :]: - if entity in dataset['bidsName']: - split_keyword = '_' + entity - break - split_filename = dataset['bidsName'].split(split_keyword) - if split_keyword != '_': - file_suffix = "".join(split_filename[:-1]) + '_' + r"run-{{item:02d}}" + split_keyword + split_filename[-1] + # check if run key is already used in filename + # (could be done in Pybids or using heudiconv utils?) + + if not '_run-' in dataset['bidsName']: + bids_entities = BIDSFile._known_entities + # insert additional run key to dissociate identical scans + # check where to insert run key + split_keyword = '_' # default + for entity in bids_entities[bids_entities.index('run') + 1 :]: + if entity in dataset['bidsName']: + split_keyword = '_' + entity + break + split_filename = dataset['bidsName'].split(split_keyword) + if split_keyword != '_': + file_suffix = "".join(split_filename[:-1]) + '_' + r"run-{{item:02d}}" + split_keyword + split_filename[-1] + else: + + file_suffix = "_".join(split_filename[:-1]) + '_' + r"run-{{item:02d}}" + split_keyword + split_filename[-1] + if len(split_filename) == 1: + # remove unwanted first "_" + file_suffix = file_suffix[1:] else: - # insert additional run key to dissociate identical scans - file_suffix = "_".join(split_filename[:-1]) + '_' + r"run-{{item:02d}}" + split_keyword + split_filename[-1] - if len(split_filename) == 1: - # remove unwanted first "_" - file_suffix = file_suffix[1:] + file_suffix = dataset['bidsName'] template = create_key(subdir=dataset['bidsDir'],file_suffix=file_suffix,outtype={outtype}) return template