Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 133 additions & 19 deletions TESTS/unitTests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import matplotlib.pyplot as plt

import pyrtools as pt
from pyrtools.pyramids.pyramid import Pyramid

import scipy.io
import os
Expand Down Expand Up @@ -213,9 +212,9 @@ class pointOpTests(unittest.TestCase):
def test1(self):
matImg = scipy.io.loadmat(op.join(matfiles_path, 'pointOp1.mat'))
img = pt.synthetic_images.ramp((200,200))
filt = np.asarray([0.2, 0.5, 1.0, 0.4, 0.1]);
filt = np.asarray([0.2, 0.5, 1.0, 0.4, 0.1])
#foo = pointOp(200, 200, img, 5, filt, 0, 1, 0);
foo = pt.pointOp(img, filt, 0, 1);
foo = pt.pointOp(img, filt, 0, 1)
foo = np.reshape(foo,(200,200))
np.testing.assert_allclose(matImg['foo'], foo)

Expand Down Expand Up @@ -688,55 +687,55 @@ def test21(self):
matPyr = scipy.io.loadmat(op.join(matfiles_path, 'buildSpyr21.mat'))
texture = scipy.io.loadmat(op.join(matfiles_path, 'im04-1.mat'))['res'][0:256,0:256]
pyPyr = pt.pyramids.SteerablePyramidSpace(texture, height=3, order=0)
recon = pyPyr.recon_pyr(0);
recon = pyPyr.recon_pyr(0)
self.assertTrue(pt.compareRecon(matPyr['recon'], recon))
def test22(self):
matPyr = scipy.io.loadmat(op.join(matfiles_path, 'buildSpyr22.mat'))
texture = scipy.io.loadmat(op.join(matfiles_path, 'im04-1.mat'))['res'][0:256,0:256]
pyPyr = pt.pyramids.SteerablePyramidSpace(texture, height=3, order=1)
recon = pyPyr.recon_pyr(1);
recon = pyPyr.recon_pyr(1)
self.assertTrue(pt.compareRecon(matPyr['recon'], recon))
def test23(self):
matPyr = scipy.io.loadmat(op.join(matfiles_path, 'buildSpyr23.mat'))
texture = scipy.io.loadmat(op.join(matfiles_path, 'im04-1.mat'))['res'][0:256,0:256]
pyPyr = pt.pyramids.SteerablePyramidSpace(texture, height=3, order=3)
recon = pyPyr.recon_pyr(3);
recon = pyPyr.recon_pyr(3)
self.assertTrue(pt.compareRecon(matPyr['recon'], recon))
def test24(self):
matPyr = scipy.io.loadmat(op.join(matfiles_path, 'buildSpyr24.mat'))
texture = scipy.io.loadmat(op.join(matfiles_path, 'im04-1.mat'))['res'][0:256,0:256]
pyPyr = pt.pyramids.SteerablePyramidSpace(texture, height=3, order=5)
recon = pyPyr.recon_pyr(5);
recon = pyPyr.recon_pyr(5)
self.assertTrue(pt.compareRecon(matPyr['recon'], recon))
def test25(self):
matPyr = scipy.io.loadmat(op.join(matfiles_path, 'buildSpyr25.mat'))
texture = scipy.io.loadmat(op.join(matfiles_path, 'im04-1.mat'))['res'][0:256,0:256]
pyPyr = pt.pyramids.SteerablePyramidSpace(texture, height=3, order=5)
recon = pyPyr.recon_pyr(5,'reflect1', ['residual_highpass', 0, 1], [0]);
recon = pyPyr.recon_pyr(5,'reflect1', ['residual_highpass', 0, 1], [0])
self.assertTrue(pt.compareRecon(matPyr['recon'], recon))
def test26(self):
matPyr = scipy.io.loadmat(op.join(matfiles_path, 'buildSpyr26.mat'))
texture = scipy.io.loadmat(op.join(matfiles_path, 'im04-1.mat'))['res'][0:128,0:256]
pyPyr = pt.pyramids.SteerablePyramidSpace(texture, height=3, order=0)
recon = pyPyr.recon_pyr(0,'reflect1', ['residual_highpass', 0, 1], [0]);
recon = pyPyr.recon_pyr(0,'reflect1', ['residual_highpass', 0, 1], [0])
self.assertTrue(pt.compareRecon(matPyr['recon'], recon))
def test27(self):
matPyr = scipy.io.loadmat(op.join(matfiles_path, 'buildSpyr27.mat'))
texture = scipy.io.loadmat(op.join(matfiles_path, 'im04-1.mat'))['res'][0:128,0:256]
pyPyr = pt.pyramids.SteerablePyramidSpace(texture, height=3, order=1)
recon = pyPyr.recon_pyr(1,'reflect1', ['residual_highpass', 0, 1], [0]);
recon = pyPyr.recon_pyr(1,'reflect1', ['residual_highpass', 0, 1], [0])
self.assertTrue(pt.compareRecon(matPyr['recon'], recon))
def test28(self):
matPyr = scipy.io.loadmat(op.join(matfiles_path, 'buildSpyr28.mat'))
texture = scipy.io.loadmat(op.join(matfiles_path, 'im04-1.mat'))['res'][0:128,0:256]
pyPyr = pt.pyramids.SteerablePyramidSpace(texture, height=3, order=3)
recon = pyPyr.recon_pyr(3,'reflect1', ['residual_highpass', 0, 1], [0]);
recon = pyPyr.recon_pyr(3,'reflect1', ['residual_highpass', 0, 1], [0])
self.assertTrue(pt.compareRecon(matPyr['recon'], recon))
def test29(self):
matPyr = scipy.io.loadmat(op.join(matfiles_path, 'buildSpyr29.mat'))
texture = scipy.io.loadmat(op.join(matfiles_path, 'im04-1.mat'))['res'][0:128,0:256]
pyPyr = pt.pyramids.SteerablePyramidSpace(texture, height=3, order=5)
recon = pyPyr.recon_pyr(5,'reflect1', ['residual_highpass', 0, 1], [0]);
recon = pyPyr.recon_pyr(5,'reflect1', ['residual_highpass', 0, 1], [0])
self.assertTrue(pt.compareRecon(matPyr['recon'], recon))

class SteerablePyramidFreqpyrTests(unittest.TestCase):
Expand Down Expand Up @@ -1191,7 +1190,7 @@ class ProjectPolarTests(unittest.TestCase):
def test0(self):
# check that it runs, even though here the output will be nonsensical
img = pt.synthetic_images.disk(256)
proj_img = pt.project_polar_to_cartesian(img)
pt.project_polar_to_cartesian(img)
def test1(self):
# currently only works for square images
img = pt.synthetic_images.disk((256, 512))
Expand Down Expand Up @@ -1277,6 +1276,13 @@ def test1(self):
# res = pt.zconv2(ramp, disc, 3)
# self.assertTrue(pt.compareRecon(mres, res))

def _check_ranges(fig, ranges):
"""Helper function to check ranges in figure titles."""
titles = [ax.get_title() for ax in fig.axes]
assert len(titles) == len(ranges)
for t, r in zip(titles, ranges):
assert t.split("\n")[1].strip() == f"range: [{r[0]:.1e}, {r[1]:.1e}]"

class TestImshow(unittest.TestCase):

def test_imshow0(self):
Expand All @@ -1288,7 +1294,7 @@ def test_imshow0_fail(self):
im = np.random.rand(3, 10, 10)
with self.assertRaises(Exception):
# because we now need to pass a list of arrays for multiple images
fig = pt.imshow(im)
pt.imshow(im)

def test_imshow1(self):
im = np.random.rand(3, 10, 10, 4)
Expand All @@ -1299,7 +1305,7 @@ def test_imshow1_fails0(self):
im = np.random.rand(3, 10, 10, 4)
with self.assertRaises(Exception):
# because we now need to pass a list of arrays for multiple images
fig = pt.imshow(im)
pt.imshow(im)

def test_imshow2(self):
im = np.random.rand(3, 10, 10, 4)
Expand Down Expand Up @@ -1359,7 +1365,7 @@ def test_imshow12(self):
im = np.random.rand(3, 10, 10, 4)
with self.assertRaises(Exception):
# no longer support 4d arrays
fig = pt.imshow(im)
pt.imshow(im)

def test_imshow13(self):
im = np.random.rand(10, 10, 3)
Expand All @@ -1377,13 +1383,13 @@ def test_imshow15(self):
# don't support 1d arrays
im = np.random.rand(10)
with self.assertRaises(Exception):
fig = pt.imshow(im)
pt.imshow(im)

def test_imshow16(self):
# must be an array or list of them
im = 10
with self.assertRaises(TypeError):
fig = pt.imshow(im)
pt.imshow(im)

def test_imshow17(self):
im = np.random.rand(256, 256) + 1j * np.random.rand(256, 256)
Expand All @@ -1403,6 +1409,60 @@ def test_imshow19(self):
vrange = rng + str(i)
pt.imshow(list(im), vrange=vrange)

def test_imshow20(self):
im = np.random.randn(32, 32, 3)
fig = pt.imshow(im)
ranges = [(0, 1)]
_check_ranges(fig, ranges)
with self.assertWarns(UserWarning, msg="RGB images cannot have their vrange set"):
fig = pt.imshow(im, vrange="auto1")
_check_ranges(fig, ranges)

def test_imshow21(self):
im = 255 * np.random.randn(32, 32, 3).astype(int)
fig = pt.imshow(im)
ranges = [(0, 255)]
_check_ranges(fig, ranges)
with self.assertWarns(UserWarning, msg="RGB images cannot have their vrange set"):
fig = pt.imshow(im, vrange="auto1")
_check_ranges(fig, ranges)

def test_imshow22(self):
im = [np.random.randn(32, 32, 3), np.random.randn(32, 32, 3)]
fig = pt.imshow(im)
ranges = [(0, 1), (0, 1)]
_check_ranges(fig, ranges)
with self.assertWarns(UserWarning, msg="RGB images cannot have their vrange set"):
fig = pt.imshow(im, vrange="auto1")
_check_ranges(fig, ranges)

def test_imshow23(self):
im = [np.random.randn(32, 32), np.random.randn(32, 32, 3)]
fig = pt.imshow(im)
ranges = [(im[0].min(), im[0].max()), (0, 1)]
_check_ranges(fig, ranges)
with self.assertWarns(UserWarning, msg="RGB images cannot have their vrange set"):
fig = pt.imshow(im, vrange="auto1")
_check_ranges(fig, ranges)

def test_imshow24(self):
im = [np.random.randn(32, 32), np.random.randn(32, 32, 3), 255 * np.random.randn(32, 32, 3).astype(int)]
fig = pt.imshow(im)
ranges = [(im[0].min(), im[0].max()), (0, 1), (0, 255)]
_check_ranges(fig, ranges)
with self.assertWarns(UserWarning, msg="RGB images cannot have their vrange set"):
fig = pt.imshow(im, vrange="auto1")
_check_ranges(fig, ranges)

def test_imshow25(self):
im = [np.random.randn(32, 32, 3), 255 * np.random.randn(32, 32, 3).astype(int)]
fig = pt.imshow(im)
ranges = [(0, 1), (0, 255)]
_check_ranges(fig, ranges)
with self.assertWarns(UserWarning, msg="RGB images cannot have their vrange set"):
fig = pt.imshow(im, vrange="auto1")
_check_ranges(fig, ranges)

class TestAnimshow(unittest.TestCase):

def test_animshow0(self):
Expand Down Expand Up @@ -1458,7 +1518,61 @@ def test_animshow_fail_n_frames(self):
vid1 = np.random.rand(10, 10, 10)
vid2 = np.random.rand(5, 10, 10)
with self.assertRaises(Exception):
fig = pt.animshow([vid1, vid2], as_html5=False)._fig
pt.animshow([vid1, vid2], as_html5=False)._fig

def test_animshow8(self):
vid = np.random.randn(3, 32, 32, 3)
fig = pt.animshow(vid, as_html5=False)._fig
ranges = [(0, 1)]
_check_ranges(fig, ranges)
with self.assertWarns(UserWarning, msg="RGB images cannot have their vrange set"):
fig = pt.animshow(vid, vrange="auto1", as_html5=False)._fig
_check_ranges(fig, ranges)

def test_animshow9(self):
vid = 255 * np.random.randn(3, 32, 32, 3).astype(int)
fig = pt.animshow(vid, as_html5=False)._fig
ranges = [(0, 255)]
_check_ranges(fig, ranges)
with self.assertWarns(UserWarning, msg="RGB images cannot have their vrange set"):
fig = pt.animshow(vid, vrange="auto1", as_html5=False)._fig
_check_ranges(fig, ranges)

def test_animshow10(self):
vid = [np.random.randn(3, 32, 32, 3), np.random.randn(3, 32, 32, 3)]
fig = pt.animshow(vid, as_html5=False)._fig
ranges = [(0, 1), (0, 1)]
_check_ranges(fig, ranges)
with self.assertWarns(UserWarning, msg="RGB images cannot have their vrange set"):
fig = pt.animshow(vid, vrange="auto1", as_html5=False)._fig
_check_ranges(fig, ranges)

def test_animshow11(self):
vid = [np.random.randn(3, 32, 32), np.random.randn(3, 32, 32, 3)]
fig = pt.animshow(vid, as_html5=False)._fig
ranges = [(vid[0].min(), vid[0].max()), (0, 1)]
_check_ranges(fig, ranges)
with self.assertWarns(UserWarning, msg="RGB images cannot have their vrange set"):
fig = pt.animshow(vid, vrange="auto1", as_html5=False)._fig
_check_ranges(fig, ranges)

def test_animshow12(self):
vid = [np.random.randn(3, 32, 32), np.random.randn(3, 32, 32, 3), 255 * np.random.randn(3, 32, 32, 3).astype(int)]
fig = pt.animshow(vid, as_html5=False)._fig
ranges = [(vid[0].min(), vid[0].max()), (0, 1), (0, 255)]
_check_ranges(fig, ranges)
with self.assertWarns(UserWarning, msg="RGB images cannot have their vrange set"):
fig = pt.animshow(vid, vrange="auto1", as_html5=False)._fig
_check_ranges(fig, ranges)

def test_animshow13(self):
vid = [np.random.randn(3, 32, 32, 3), 255 * np.random.randn(3, 32, 32, 3).astype(int)]
fig = pt.animshow(vid, as_html5=False)._fig
ranges = [(0, 1), (0, 255)]
_check_ranges(fig, ranges)
with self.assertWarns(UserWarning, msg="RGB images cannot have their vrange set"):
fig = pt.animshow(vid, vrange="auto1", as_html5=False)._fig
_check_ranges(fig, ranges)


class TestPyrshow(unittest.TestCase):
Expand Down
Loading
Loading