Skip to content

Commit

Permalink
Assert that all images in subject list have different names
Browse files Browse the repository at this point in the history
Resolves #35
  • Loading branch information
fepegar committed Jan 9, 2020
1 parent 06054ad commit d3b43b1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
7 changes: 7 additions & 0 deletions tests/test_images_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ def test_wrong_subjects_list(self):
self.iterate_dataset([[Image('t1', 'nopath', INTENSITY)]])
with self.assertRaises(TypeError):
self.iterate_dataset([[Image('t1', 5, INTENSITY)]])
with self.assertRaises(KeyError):
with tempfile.NamedTemporaryFile() as f:
images = [
Image('t1', f.name, INTENSITY),
Image('t1', f.name, INTENSITY),
]
self.iterate_dataset([images])
with self.assertRaises(ValueError):
path = self.dir / 'test.txt'
path.touch()
Expand Down
16 changes: 16 additions & 0 deletions torchio/dataset/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,41 @@ def __getitem__(self, index):

@staticmethod
def parse_subjects_list(subjects_list):
# Check that it's list or tuple
if not isinstance(subjects_list, Sequence):
raise TypeError(
f'Subject list must be a sequence, not {type(subjects_list)}')

# Check that it's not empty
if not subjects_list:
raise ValueError('Subjects list is empty')

# Check each element
for subject_images in subjects_list:
# Check that each element is a list
if not isinstance(subject_images, Sequence):
message = (
'Subject images list must be a sequence'
f', not {type(subject_images)}'
)
raise TypeError(message)

# Check that there are only instances of Image
# and all images have different names
names = []
for image in subject_images:
if not isinstance(image, Image):
message = (
'Subject list elements must be instances of'
f' torchio.Image, not {type(image)}'
)
raise TypeError(message)
if image.name in names:
message = (
f'Two images with name "{image.name}" found in list'
)
raise KeyError(message)
names.append(image.name)

@staticmethod
def save_sample(sample, output_paths_dict):
Expand Down

0 comments on commit d3b43b1

Please sign in to comment.