From 174d0ace64decac39340c7b160ffdaf37676a6d2 Mon Sep 17 00:00:00 2001 From: Edoardo Pasca Date: Tue, 19 Mar 2019 13:31:12 +0000 Subject: added and updated unittests --- Wrappers/Python/test/test_BlockDataContainer.py | 39 +++++++++++++++++++++++-- Wrappers/Python/test/test_BlockOperator.py | 23 +++++++++++++++ Wrappers/Python/test/test_DataContainer.py | 11 +++---- Wrappers/Python/test/test_functions.py | 13 ++++++--- 4 files changed, 75 insertions(+), 11 deletions(-) (limited to 'Wrappers') diff --git a/Wrappers/Python/test/test_BlockDataContainer.py b/Wrappers/Python/test/test_BlockDataContainer.py index ec69225..6c0bede 100755 --- a/Wrappers/Python/test/test_BlockDataContainer.py +++ b/Wrappers/Python/test/test_BlockDataContainer.py @@ -18,6 +18,8 @@ from ccpi.framework import BlockDataContainer #from ccpi.optimisation.Algorithms import CGLS import functools +from ccpi.optimisation.operators import Gradient, Identity, BlockOperator + class TestBlockDataContainer(unittest.TestCase): def skiptest_BlockDataContainerShape(self): print ("test block data container") @@ -327,6 +329,39 @@ class TestBlockDataContainer(unittest.TestCase): numpy.testing.assert_almost_equal(nbdc2.get_item(1).get_item(0).as_array()[0][0][0] , 1. , decimal=5) numpy.testing.assert_almost_equal(nbdc2.get_item(1).get_item(1).as_array()[0][0][0] , 3./2 , decimal=5) - + c5 = nbdc.get_item(0).power(2).sum() + c5a = nbdc.power(2).sum() + print ("sum", c5a, c5) + print ("test_Nested_BlockDataContainer OK") - + def stest_NestedBlockDataContainer2(self): + M, N = 2, 3 + ig = ImageGeometry(voxel_num_x = M, voxel_num_y = N) + ag = ig + u = ig.allocate(1) + op1 = Gradient(ig) + op2 = Identity(ig, ag) + + operator = BlockOperator(op1, op2, shape=(2,1)) + + d1 = op1.direct(u) + d2 = op2.direct(u) + + d = operator.direct(u) + + dd = operator.domain_geometry() + ww = operator.range_geometry() + + print(d.get_item(0).get_item(0).as_array()) + print(d.get_item(0).get_item(1).as_array()) + print(d.get_item(1).as_array()) + + c1 = d + d + + c2 = 2*d + + c3 = d / (d+0.0001) + + + c5 = d.get_item(0).power(2).sum() + diff --git a/Wrappers/Python/test/test_BlockOperator.py b/Wrappers/Python/test/test_BlockOperator.py index 8bd673b..951aa0a 100644 --- a/Wrappers/Python/test/test_BlockOperator.py +++ b/Wrappers/Python/test/test_BlockOperator.py @@ -4,6 +4,7 @@ from ccpi.framework import BlockDataContainer from ccpi.optimisation.ops import TomoIdentity from ccpi.framework import ImageGeometry, ImageData import numpy +from ccpi.optimisation.operators import FiniteDiff class TestBlockOperator(unittest.TestCase): @@ -102,6 +103,7 @@ class TestBlockOperator(unittest.TestCase): def test_TomoIdentity(self): ig = ImageGeometry(10,20,30) img = ig.allocate() + print (img.shape, ig.shape) self.assertTrue(img.shape == (30,20,10)) self.assertEqual(img.sum(), 0) Id = TomoIdentity(ig) @@ -288,3 +290,24 @@ class TestBlockOperator(unittest.TestCase): plt.imshow(cgsmall.get_output().get_item(0,0).subset(vertical=0).as_array()) plt.title('Composite CGLS\nsmall lambda') plt.show() + + def test_FiniteDiffOperator(self): + N, M = 200, 300 + + + ig = ImageGeometry(voxel_num_x = M, voxel_num_y = N) + u = ig.allocate('random_int') + G = FiniteDiff(ig, direction=0, bnd_cond = 'Neumann') + print(type(u), u.as_array()) + print(G.direct(u).as_array()) + + # Gradient Operator norm, for one direction should be close to 2 + numpy.testing.assert_allclose(G.norm(), numpy.sqrt(4), atol=0.1) + + M1, N1, K1 = 200, 300, 2 + ig1 = ImageGeometry(voxel_num_x = M1, voxel_num_y = N1, channels = K1) + u1 = ig1.allocate('random_int') + G1 = FiniteDiff(ig1, direction=2, bnd_cond = 'Periodic') + print(ig1.shape==u1.shape) + print (G1.norm()) + numpy.testing.assert_allclose(G1.norm(), numpy.sqrt(4), atol=0.1) \ No newline at end of file diff --git a/Wrappers/Python/test/test_DataContainer.py b/Wrappers/Python/test/test_DataContainer.py index 47feb95..7a7e6a0 100755 --- a/Wrappers/Python/test/test_DataContainer.py +++ b/Wrappers/Python/test/test_DataContainer.py @@ -495,9 +495,10 @@ class TestDataContainer(unittest.TestCase): self.assertEqual(order[1], image.dimension_labels[1]) self.assertEqual(order[2], image.dimension_labels[2]) def test_AcquisitionGeometry_allocate(self): - ageometry = AcquisitionGeometry(dimension=2, angles=numpy.linspace(0, 180, num=10), - geom_type='parallel', pixel_num_v=3, - pixel_num_h=5, channels=2) + ageometry = AcquisitionGeometry(dimension=2, + angles=numpy.linspace(0, 180, num=10), + geom_type='parallel', pixel_num_v=3, + pixel_num_h=5, channels=2) sino = ageometry.allocate() shape = sino.shape print ("shape", shape) @@ -509,8 +510,8 @@ class TestDataContainer(unittest.TestCase): self.assertEqual(1,sino.as_array()[shape[0]-1][shape[1]-1][shape[2]-1][shape[3]-1]) print (sino.dimension_labels, sino.shape, ageometry) - default_order = ['channel' , ' angle' , - 'vertical' , 'horizontal'] + default_order = ['channel' , 'angle' , + 'vertical' , 'horizontal'] self.assertEqual(default_order[0], sino.dimension_labels[0]) self.assertEqual(default_order[1], sino.dimension_labels[1]) self.assertEqual(default_order[2], sino.dimension_labels[2]) diff --git a/Wrappers/Python/test/test_functions.py b/Wrappers/Python/test/test_functions.py index 554d400..6a44641 100644 --- a/Wrappers/Python/test/test_functions.py +++ b/Wrappers/Python/test/test_functions.py @@ -49,11 +49,12 @@ class TestFunction(unittest.TestCase): noisy_data = ImageData(np.random.randint(10, size=ag)) d = ImageData(np.random.randint(10, size=ag)) - - g = L2NormSq(alpha=0.5, b=noisy_data) + alpha = 0.5 + # scaled function + g = alpha * L2NormSq(b=noisy_data) # Compare call of g - a2 = g.alpha*(d - noisy_data).power(2).sum() + a2 = alpha*(d - noisy_data).power(2).sum() #print(a2, g(d)) self.assertEqual(a2, g(d)) @@ -63,7 +64,11 @@ class TestFunction(unittest.TestCase): #print( a3, g.convex_conjugate(d)) - + def stest_ScaledFunctin(self): + ig = (N,N) + ag = ig + op1 = Gradient(ig) + op2 = Identity(ig, ag) # -- cgit v1.2.3