Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BF]: corrected bids naming and duplication keys #35

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[BF]: corrected bids naming and duplication keys
  • Loading branch information
alexpron committed Dec 16, 2024
commit 0f97451146e3f42135a5bbcf301a149196cbe746
75 changes: 53 additions & 22 deletions shanoir2bids.py
Original file line number Diff line number Diff line change
@@ -199,35 +199,66 @@ def generate_bids_heuristic_file(
outtype = '("dicom","nii.gz")'

heuristic = f"""from heudiconv.heuristics.reproin import create_key

def simplify_filename(input_file):

from heudiconv.bids import BIDSFile
# TODO use BIDS official schema instead
bids_entities = tuple(BIDSFile._known_entities) + ("chunk",)

# deduplication BIDS entities
if not "run-" in input_file:
# 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 input_file:
split_keyword = entity + "-" # insure in a bids entity key
break

split_filename = input_file.split(split_keyword)
if split_keyword == "_":
file_suffix = (
"_".join(split_filename[:-1])
+ "_"
+ r"run-{{item:02d}}"
+ split_keyword
+ split_filename[-1]
)
if file_suffix[0] == "_":
file_suffix = file_suffix[1:]
else:

def create_bids_key(dataset):
prefix = "".join(split_filename[:-1])
if prefix == "" or prefix[-1] == "_":
prefix = prefix[:-1]
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]
)
else:
file_suffix = input_file
return file_suffix

from heudiconv.bids import BIDSFile

def create_bids_key(dataset):

# 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']
file_suffix = simplify_filename(dataset['bidsName'])
template = create_key(subdir=dataset['bidsDir'],file_suffix=file_suffix,outtype={outtype})
return template