Skip to content

Commit

Permalink
Add test to assert that original data does not change
Browse files Browse the repository at this point in the history
  • Loading branch information
fepegar committed Aug 3, 2020
1 parent eced292 commit 3e928bb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
16 changes: 14 additions & 2 deletions tests/transforms/test_transforms.py
@@ -1,5 +1,4 @@
#!/usr/bin/env python

import copy
import torch
import numpy as np
import torchio
Expand Down Expand Up @@ -102,3 +101,16 @@ def test_transform_noop(self):
tensor = torch.rand(2, 4, 5, 8).numpy()
transformed = transform(tensor)
self.assertIs(transformed, tensor)

def test_original_unchanged(self):
sample = copy.deepcopy(self.sample)
composed = self.get_transform(channels=('t1', 't2'), is_3d=True)
sample = self.flip_affine_x(sample)
for transform in composed.transform.transforms:
original_data = copy.deepcopy(sample.t1.data)
transformed = transform(sample)
self.assertTensorEqual(
sample.t1.data,
original_data,
f'Changes after {transform.name}'
)
6 changes: 6 additions & 0 deletions tests/utils.py
Expand Up @@ -145,3 +145,9 @@ def assertTensorNotEqual(self, a, b, message=None):
assert not torch.all(torch.eq(a, b))
else:
assert not torch.all(torch.eq(a, b)), message

def assertTensorEqual(self, a, b, message=None):
if message is None:
assert torch.all(torch.eq(a, b))
else:
assert torch.all(torch.eq(a, b)), message

0 comments on commit 3e928bb

Please sign in to comment.