From d6ec46e44a66195f9e85754eac141e7880179b52 Mon Sep 17 00:00:00 2001 From: 36000 Date: Wed, 13 Nov 2024 13:51:23 -0800 Subject: [PATCH 1/2] [FIX] smarter automatic scalar names --- AFQ/definitions/utils.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/AFQ/definitions/utils.py b/AFQ/definitions/utils.py index 1629821f..74bd0bda 100644 --- a/AFQ/definitions/utils.py +++ b/AFQ/definitions/utils.py @@ -72,9 +72,12 @@ def _arglist_to_string(args, get_attr=None): def name_from_path(path): file_name = op.basename(path) # get file name file_name = drop_extension(file_name) # remove extension - if "-" in file_name: - file_name = file_name.split("-")[-1] # get suffix if exists - return file_name + if "desc-" in file_name: # get desc if exists + return file_name.split("desc-")[-1].split("_")[0] + elif "_" in file_name: + return file_name.split("_")[-1] # get suffix if exists + else: + return file_name def _ff_helper(required, err_msg): From aa6470a9f4523b4ac72b8f13a5d03f70ff6ca0ba Mon Sep 17 00:00:00 2001 From: 36000 Date: Wed, 13 Nov 2024 14:44:21 -0800 Subject: [PATCH 2/2] add test for scalar name from file name --- AFQ/tests/test_definitions.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/AFQ/tests/test_definitions.py b/AFQ/tests/test_definitions.py index de1f774b..65c66aed 100644 --- a/AFQ/tests/test_definitions.py +++ b/AFQ/tests/test_definitions.py @@ -9,6 +9,7 @@ from AFQ.definitions.image import * # interprets images from eval from AFQ.definitions.mapping import * # interprets mappings from eval from AFQ.tests.test_api import create_dummy_bids_path +from AFQ.definitions.utils import name_from_path def test_str_instantiates_mixin(): @@ -23,6 +24,15 @@ def test_str_instantiates_mixin(): thresh_image_from_str.upper_bound) +def test_name_from_path(): + path = "/data/sub-01/ses-01/dwi/sub-01_ses-01_desc-icvf_dwi.nii.gz" + assert name_from_path(path) == "icvf" + path = "/data/sub-01/ses-01/dwi/sub-01_ses-01_desc-od_dwi.nii.gz" + assert name_from_path(path) == "od" + path = "/data/sub-01/ses-01/anat/sub-01_ses-01_T1w.nii.gz" + assert name_from_path(path) == "T1w" + + def test_resample_image(): image_data = np.zeros((2, 2, 2), dtype=bool) image_data[0] = True