diff --git a/shanoir2bids.py b/shanoir2bids.py index f3bf813..3a39adc 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,9 +200,35 @@ def generate_bids_heuristic_file( heuristic = f"""from heudiconv.heuristics.reproin import create_key + def create_bids_key(dataset): - template = create_key(subdir=dataset['bidsDir'],file_suffix="_".join(dataset['bidsName'].split('_')[:-1]) + '_' + r"run-{{item:02d}}_" + dataset['bidsName'].split('_')[-1],outtype={outtype}) + from heudiconv.bids import BIDSFile + + # 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: + file_suffix = dataset['bidsName'] + template = create_key(subdir=dataset['bidsDir'],file_suffix=file_suffix,outtype={outtype}) return template def get_dataset_to_key_mapping(shanoir2bids): @@ -214,7 +241,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] @@ -449,12 +478,12 @@ def is_mapping_bids(self): paths = ( "/" + "sub-" - + subject + + sanitize_label(subject) + "/" + map["bidsDir"] + "/" + "sub-" - + subject + + sanitize_label(subject) + "_" + map["bidsName"] + extension @@ -465,18 +494,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 @@ -721,7 +750,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)