Skip to content

Commit

Permalink
use parse_file_entities for get_entity_value
Browse files Browse the repository at this point in the history
  • Loading branch information
singlesp committed Jan 24, 2025
1 parent bc5e37c commit 18707d3
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions cubids/cubids.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,9 +556,9 @@ def change_filename(self, filepath, entities):

suffix = entities["suffix"]

sub = get_entity_value(filepath, "sub")
sub = get_entity_value(filepath, "subject")
if self.is_longitudinal:
ses = get_entity_value(filepath, "ses")
ses = get_entity_value(filepath, "session")

# Add the scan path + new path to the lists of old, new filenames
self.old_filenames.append(filepath)
Expand Down Expand Up @@ -2131,20 +2131,26 @@ def get_entity_value(path, key):
Examples
--------
>>> print(get_entity_value('/path/to/sub-01/ses-02/func/sub-01_ses-02_task-rest_bold.nii.gz', 'sub'))
>>> print(get_entity_value('/path/to/sub-01/ses-02/func/sub-01_ses-02_task-rest_bold.nii.gz', 'subject'))
01
>>> print(get_entity_value('/path/to/sub-01/ses-02/func/sub-01_ses-02_task-rest_bold.nii.gz', 'ses'))
>>> print(get_entity_value('/path/to/sub-01/ses-02/func/sub-01_ses-02_task-rest_bold.nii.gz', 'session'))
02
>>> print(get_entity_value('/path/to/sub-01/ses-02/func/sub-01_ses-02_task-rest_bold.nii.gz', 'task'))
rest
>>> print(get_entity_value('/path/to/sub-01/ses-02/func/sub-01_ses-02_task-rest_bold.nii.gz', 'run'))
None
"""
filename = Path(path).name
pattern = rf"{key}-([a-zA-Z0-9]+)"
match = re.search(pattern, filename)
if match:
return match.group(1)
if key == "sub":
key = "subject"
if key == "ses":
key = "session"

entity_value = parse_file_entities(path).get(key)

if entity_value:
return entity_value
else:
return None


def build_path(filepath, entities, out_dir, is_longitudinal):
Expand Down

0 comments on commit 18707d3

Please sign in to comment.