Skip to content

Commit

Permalink
Some testing
Browse files Browse the repository at this point in the history
  • Loading branch information
MargaretDuff committed Sep 18, 2023
1 parent 0723002 commit 0710893
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
6 changes: 3 additions & 3 deletions Wrappers/Python/test/test_BlockOperator.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from cil.framework import ImageGeometry, ImageData
import numpy
from cil.optimisation.operators import FiniteDifferenceOperator

from cil.optimisation.operators import LinearOperator
initialise_tests()

class TestBlockOperator(unittest.TestCase):
Expand Down Expand Up @@ -187,9 +187,9 @@ def test_FiniteDiffOperator(self):
G = FiniteDifferenceOperator(ig, direction=0, bnd_cond = 'Neumann')
logging.info("{} {}".format(type(u), str(u.as_array())))
logging.info(str(G.direct(u).as_array()))

LinearOperator.PowerMethod(G, range_is_domain=False)
# Gradient Operator norm, for one direction should be close to 2
numpy.testing.assert_allclose(G.norm(), numpy.sqrt(4), atol=0.1)
numpy.testing.assert_allclose(LinearOperator.PowerMethod(G, range_is_domain=False), numpy.sqrt(4), atol=0.1)

M1, N1, K1 = 200, 300, 2
ig1 = ImageGeometry(voxel_num_x = M1, voxel_num_y = N1, channels = K1)
Expand Down
31 changes: 19 additions & 12 deletions Wrappers/Python/test/test_Operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,25 +306,32 @@ def test_PowerMethod(self):
numpy.testing.assert_almost_equal(res1,3.1624439599276974, decimal=4)

# 2x2 non-diagonalisable nilpotent matrix
with self.assertRaises(ValueError):

M1=numpy.array([[0.,1.], [0.,0.]])
M1op = MatrixOperator(M1)
res1 = M1op.PowerMethod(M1op,5)

M1=numpy.array([[0.,1.], [0.,0.]])
M1op = MatrixOperator(M1)
res1 = M1op.PowerMethod(M1op,5)
numpy.testing.assert_almost_equal(res1,0, decimal=4)

# 2x2 non-diagonalisable nilpotent matrix where range_is_domain is False
M1=numpy.array([[0.,1.], [0.,0.]])
M1op = MatrixOperator(M1)
res1 = M1op.PowerMethod(M1op,5, range_is_domain=False)
numpy.testing.assert_almost_equal(res1,1, decimal=4)


# 2x2 matrix, max absolute eigenvalue is not unique and initial vector chosen for non-convergence
with self.assertWarns(Warning):
M1=numpy.array([[2.,1.], [0.,-2.]])
M1op = MatrixOperator(M1)
res1 = M1op.PowerMethod(M1op,100, initial=DataContainer(numpy.array([1.,1.])))

M1=numpy.array([[2.,1.], [0.,-2.]])
M1op = MatrixOperator(M1)
_,_,_,_,convergence = M1op.PowerMethod(M1op,100, initial=DataContainer(numpy.array([1.,1.])), return_all=True)
numpy.testing.assert_equal(convergence,False)

# 2x2 matrix, max absolute eigenvalue is not unique and initial vector chosen for convergence

M1=numpy.array([[2.,1.,0.],[0.,1.,1.], [0.,0.,1.]])
M1op = MatrixOperator(M1)
res1 = M1op.PowerMethod(M1op,100)
res1,_,_,_,convergence = M1op.PowerMethod(M1op,100, return_all=True)
numpy.testing.assert_almost_equal(res1,2., decimal=4)

numpy.testing.assert_equal(convergence,True)

# Gradient Operator (float)
ig = ImageGeometry(30,30)
Expand Down

0 comments on commit 0710893

Please sign in to comment.