-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PR #17: add unittests for alignment, correlation, and affine libraries
- Loading branch information
Showing
7 changed files
with
139 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import pytest | ||
import torch | ||
|
||
from tttsa.affine import affine_transform_2d, affine_transform_3d, stretch_image | ||
from tttsa.transformations import R_2d, Rz | ||
|
||
|
||
def test_stretch_image(): | ||
a = torch.zeros((5, 5)) | ||
b = stretch_image(a, 1.1, -85) | ||
assert a.shape == b.shape | ||
|
||
|
||
def test_affine_transform_2d(): | ||
m1 = R_2d(torch.tensor(45.0)) | ||
m2 = R_2d(torch.randn(3)) | ||
|
||
# with a single image | ||
a = torch.zeros((4, 5)) | ||
b = affine_transform_2d(a, m1) | ||
assert a.shape == b.shape | ||
b = affine_transform_2d(a, m1, (5, 4)) | ||
assert b.shape == (5, 4) | ||
b = affine_transform_2d(a, m2) | ||
assert b.shape == (3, 4, 5) | ||
b = affine_transform_2d(a, m2, (5, 4)) | ||
assert b.shape == (3, 5, 4) | ||
|
||
# with a batch of images | ||
a = torch.zeros((3, 4, 5)) | ||
b = affine_transform_2d(a, m2) | ||
assert a.shape == b.shape | ||
b = affine_transform_2d(a, m2, (5, 4)) | ||
assert b.shape == (3, 5, 4) | ||
a = torch.zeros((2, 4, 5)) | ||
with pytest.raises(ValueError): | ||
affine_transform_2d(a, m1) | ||
with pytest.raises(ValueError): | ||
affine_transform_2d(a, m2) | ||
|
||
|
||
def test_affine_transform_3d(): | ||
m1 = Rz(torch.tensor(45.0)) | ||
m2 = Rz(torch.randn(3)) | ||
|
||
# with a single image | ||
a = torch.zeros((3, 4, 5)) | ||
b = affine_transform_3d(a, m1) | ||
assert a.shape == b.shape | ||
b = affine_transform_3d(a, m1, (5, 4, 3)) | ||
assert b.shape == (5, 4, 3) | ||
b = affine_transform_3d(a, m2) | ||
assert b.shape == (3, 3, 4, 5) | ||
b = affine_transform_3d(a, m2, (5, 4, 3)) | ||
assert b.shape == (3, 5, 4, 3) | ||
|
||
# with a batch of images | ||
a = torch.zeros((3, 3, 4, 5)) | ||
b = affine_transform_3d(a, m2) | ||
assert a.shape == b.shape | ||
b = affine_transform_3d(a, m2, (5, 4, 3)) | ||
assert b.shape == (3, 5, 4, 3) | ||
a = torch.zeros((2, 3, 4, 5)) | ||
with pytest.raises(ValueError): | ||
affine_transform_3d(a, m1) | ||
with pytest.raises(ValueError): | ||
affine_transform_3d(a, m2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import pytest | ||
import torch | ||
|
||
from tttsa.alignment import find_image_shift | ||
|
||
|
||
def test_find_image_shift(): | ||
a = torch.zeros((4, 4)) | ||
a[0, 0] = 1 | ||
b = torch.zeros((4, 4)) | ||
b[2, 2] = 0.7 | ||
b[2, 3] = 0.3 | ||
shift = find_image_shift(a, b) | ||
print(shift) | ||
assert shift.dtype == torch.float32 | ||
assert torch.all(shift == -2.0), ( | ||
"Interpolating a shift too close to a border is " | ||
"not possible, so an integer shift should be " | ||
"returned." | ||
) | ||
a = torch.zeros((8, 8)) | ||
a[3, 3] = 1 | ||
b = torch.zeros((8, 8)) | ||
b[4, 4] = 0.7 | ||
b[4, 5] = 0.3 | ||
shift = find_image_shift(a, b) | ||
# values should interpolated with floating point precision | ||
assert shift.dtype == torch.float32 | ||
assert shift[0] == pytest.approx(-1.1, 0.1) | ||
assert shift[1] == pytest.approx(-1.2, 0.1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters