Skip to content

Commit

Permalink
Merge pull request #32 from alexpron/fix-dup-underscore
Browse files Browse the repository at this point in the history
Fix dup underscore
  • Loading branch information
quentinduche authored Dec 11, 2024
2 parents 659a4a0 + 54a7044 commit 5deb17b
Showing 1 changed file with 37 additions and 8 deletions.
45 changes: 37 additions & 8 deletions shanoir2bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand All @@ -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]
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 5deb17b

Please sign in to comment.