Skip to content

Commit

Permalink
#86: (failing) unit test for creating intermediate derivative files
Browse files Browse the repository at this point in the history
  • Loading branch information
erdalkaraca committed Jan 10, 2025
1 parent b23df51 commit cb479f5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
3 changes: 1 addition & 2 deletions ancpbids/torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
29 changes: 29 additions & 0 deletions tests/auto/test_writing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit cb479f5

Please sign in to comment.