From cb479f5458ee1565ba47bd08d0cb095aa8ed2b29 Mon Sep 17 00:00:00 2001 From: Erdal Karaca Date: Fri, 10 Jan 2025 08:43:12 +0100 Subject: [PATCH] #86: (failing) unit test for creating intermediate derivative files --- ancpbids/torch.py | 3 +-- tests/auto/test_writing.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/ancpbids/torch.py b/ancpbids/torch.py index 05c6f85..8aecd31 100644 --- a/ancpbids/torch.py +++ b/ancpbids/torch.py @@ -37,5 +37,4 @@ def split(self, *args: float): if not math.isclose(sum_ratios, 1.0): raise ValueError("sum of split ratios should be 1.0") - size_dataset = len(self) - return random_split(self, [round(size_dataset * ratio) for ratio in args]) + return random_split(self, args) diff --git a/tests/auto/test_writing.py b/tests/auto/test_writing.py index 12d8c6a..fd50b5a 100644 --- a/tests/auto/test_writing.py +++ b/tests/auto/test_writing.py @@ -107,6 +107,35 @@ def test_create_new_dataset(self): # TODO reload dataset and add assertions + def test_write_intermediate_results(self): + from ancpbids import model_latest as schema + dataset = schema.create_dataset(name='my-test-ds') + dataset.dataset_description.Name = 'a programmatically created dataset' + dataset.dataset_description.BIDSVersion = schema.VERSION + + for i in range(1, 10): + subject = dataset.create_folder(name=f"sub-{i}", type_=schema.Subject) + func_folder = subject.create_folder(name='func', type_=schema.DatatypeFolder) + img_file = func_folder.create_artifact() + img_file.suffix = 'bold' + img_file.extension = '.nii.gz' + img_file.add_entity('task', 'programming') + # touch() makes sure the artifact has a proper BIDS name within its parent directory + file_path: str = img_file.touch() + # we only care about the path beginning with the derivative folder + expected_relative_file_path = os.path.normpath( + f"my-test-ds/sub-{i}/func/sub-{i}_task-programming_bold.nii.gz") + self.assertTrue(file_path.endswith(expected_relative_file_path)) + + # we can also get its absolute path name + file_path_abs = img_file.get_absolute_path() + self.assertEqual(file_path_abs, file_path, "Expected touch to return the absolute path of the file") + + self.assertFalse(os.path.exists(file_path), f"File should not have been created yet: {file_path}") + # just create an empty file (this is where users would call some third party lib to create file specific contents) + open(file_path, 'w').close() + self.assertTrue(os.path.exists(file_path), f"Missing expected file {file_path}") + if __name__ == '__main__': unittest.main()