Mentions légales du service

Skip to content
Snippets Groups Projects
Commit 7ba6a066 authored by hhakim's avatar hhakim
Browse files

Add unit tests for pyfaust LazyLinearOp2.

parent 27aa848e
Branches
Tags
No related merge requests found
import unittest
import pyfaust as pf
from pyfaust.lazylinop import LazyLinearOp2, vstack, hstack, LazyLinearOp2
import numpy.linalg as LA
import numpy as np
class TestLazyLinearOpFaust(unittest.TestCase):
def setUp(self):
self.lop = LazyLinearOp2.create_from_op(pf.rand(10, 15))
self.lopA = self.lop.toarray()
self.lop2 = LazyLinearOp2.create_from_op(pf.rand(self.lop.shape[0], self.lop.shape[1]))
self.lop2A = self.lop2.toarray()
self.lop3 = LazyLinearOp2.create_from_op(pf.rand(self.lop.shape[1], self.lop.shape[0]))
self.lop3A = self.lop3.toarray()
def test_shape(self):
self.assertEqual(self.lop.shape, self.lopA.shape)
def test_ndim(self):
self.assertEqual(self.lop.ndim, 2)
def test_transp(self):
lopT = self.lop.T
self.assertAlmostEqual(LA.norm(lopT.toarray()-self.lopA.T), 0)
lopT = self.lop.transpose()
self.assertAlmostEqual(LA.norm(lopT.toarray()-self.lopA.T), 0)
self.assertEqual(self.lop.shape[0], lopT.shape[1])
self.assertEqual(self.lop.shape[1], lopT.shape[0])
def test_conj(self):
lopC = self.lop.conj()
self.assertAlmostEqual(LA.norm(lopC.toarray()-self.lopA.conj()), 0)
def test_adjoint(self):
lopH = self.lop.H
self.assertAlmostEqual(LA.norm(lopH.toarray()-self.lopA.conj().T), 0)
lopH = self.lop.getH()
self.assertAlmostEqual(LA.norm(lopH.toarray()-self.lopA.conj().T), 0)
self.assertEqual(self.lop.shape[0], lopH.shape[1])
self.assertEqual(self.lop.shape[1], lopH.shape[0])
def test_add(self):
ladd = self.lop + self.lop2
self.assertAlmostEqual(LA.norm(ladd.toarray()-(self.lopA+self.lop2A)),
0)
M = np.random.rand(*self.lop.shape)
ladd2 = self.lop + M
self.assertTrue(isinstance(ladd2, LazyLinearOp2))
self.assertAlmostEqual(LA.norm(ladd2.toarray()-(self.lopA + M)),
0)
def test_iadd(self):
self.assertRaises(NotImplementedError, self.lop.__iadd__, self.lop2)
def test_radd(self):
M = np.random.rand(*self.lop.shape)
ladd2 = M + self.lop
self.assertTrue(isinstance(ladd2, LazyLinearOp2))
self.assertAlmostEqual(LA.norm(ladd2.toarray()-(M + self.lopA)), 0)
def test_sub(self):
lsub = self.lop - self.lop2
self.assertAlmostEqual(LA.norm(lsub.toarray() - (self.lopA - self.lop2A)),
0)
M = np.random.rand(*self.lop.shape)
lsub2 = self.lop - M
self.assertTrue(isinstance(lsub2, LazyLinearOp2))
self.assertAlmostEqual(LA.norm(lsub2.toarray() - (self.lopA - M)), 0)
def test_rsub(self):
M = np.random.rand(*self.lop.shape)
lsub2 = M - self.lop
self.assertTrue(isinstance(lsub2, LazyLinearOp2))
self.assertAlmostEqual(LA.norm(lsub2.toarray()-(M - self.lopA)), 0)
def test_isub(self):
self.assertRaises(NotImplementedError, self.lop.__isub__, self.lop2)
def test_matmul_dot_matvec(self):
from scipy.sparse import csr_matrix, issparse
lmul = self.lop @ self.lop3
self.assertTrue(pf.lazylinop.LazyLinearOp2.isLazyLinearOp(lmul))
self.assertAlmostEqual(LA.norm(lmul.toarray() - (self.lopA @ self.lop3A)),
0)
lmul = self.lop.dot(self.lop3)
self.assertTrue(pf.lazylinop.LazyLinearOp2.isLazyLinearOp(lmul))
self.assertAlmostEqual(LA.norm(lmul.toarray() - (self.lopA @ self.lop3A)),
0)
M = np.random.rand(self.lop.shape[1], 15)
lmul2 = self.lop @ M
self.assertTrue(isinstance(lmul2, np.ndarray))
self.assertAlmostEqual(LA.norm(lmul2 - (self.lopA @ M)),
0)
if self.__class__ == TestLazyLinearOpFaust:
lmul2 = self.lop @ csr_matrix(M)
self.assertTrue(isinstance(lmul2, np.ndarray))
self.assertAlmostEqual(LA.norm(lmul2 - (self.lop @ M)),
0)
lmul2 = self.lop.matvec(M[:,0])
self.assertTrue(isinstance(lmul2, np.ndarray))
self.assertAlmostEqual(LA.norm(lmul2 - (self.lopA @ M[:,0])),
0)
S = csr_matrix(M)
lmul3 = pf.lazylinop.asLazyLinearOp(S) @ S.T
self.assertTrue(issparse(lmul3))
self.assertAlmostEqual(LA.norm(lmul3 - (M @ M.T)),
0)
def test_rmatmul(self):
M = np.random.rand(15, self.lop.shape[0])
lmul2 = M @ self.lop
self.assertTrue(isinstance(lmul2, np.ndarray))
self.assertAlmostEqual(LA.norm(lmul2 - (M @ self.lopA)),
0)
def test_imatmul(self):
self.assertRaises(NotImplementedError, self.lop.__imatmul__, self.lop2)
def test_mul(self):
v = np.random.rand(self.lop.shape[1])
lmul2 = self.lop * v
self.assertTrue(isinstance(lmul2, LazyLinearOp2))
self.assertAlmostEqual(LA.norm(lmul2.toarray() - (self.lopA * v)),
0)
v = np.random.rand(1, self.lop.shape[1])
lmul2 = self.lop * v
self.assertTrue(isinstance(lmul2, LazyLinearOp2))
self.assertAlmostEqual(LA.norm(lmul2.toarray() - (self.lopA * v)),
0)
s = np.random.rand(1, 1)[0, 0]
lmul2 = self.lop * s
self.assertTrue(isinstance(lmul2, LazyLinearOp2))
self.assertAlmostEqual(LA.norm(lmul2.toarray() - (self.lopA * s)),
0)
def test_rmul(self):
v = np.random.rand(self.lop.shape[1])
lmul2 = v * self.lop
self.assertTrue(isinstance(lmul2, LazyLinearOp2))
self.assertAlmostEqual(LA.norm(lmul2.toarray() - (v * self.lopA)),
0)
v = np.random.rand(1, self.lop.shape[1])
lmul2 = v * self.lop
self.assertTrue(isinstance(lmul2, LazyLinearOp2))
self.assertAlmostEqual(LA.norm(lmul2.toarray() - (v * self.lopA)),
0)
s = np.random.rand(1, 1)[0, 0]
lmul2 = s * self.lop
self.assertTrue(isinstance(lmul2, LazyLinearOp2))
self.assertAlmostEqual(LA.norm(lmul2.toarray() - (s * self.lopA)),
0)
def test_concatenate(self):
lcat = self.lop.concatenate(self.lop2, axis=0)
self.assertAlmostEqual(LA.norm(lcat.toarray() - np.vstack((self.lopA,
self.lop2A))),
0)
self.assertEqual(lcat.shape[0], self.lop.shape[0] + self.lop2.shape[0])
lcat = self.lop.concatenate(self.lop2, axis=1)
self.assertAlmostEqual(LA.norm(lcat.toarray() - np.hstack((self.lopA,
self.lop2A))),
0)
self.assertEqual(lcat.shape[1], self.lop.shape[1] + self.lop2.shape[1])
# auto concat
lcat = self.lop.concatenate(self.lop, axis=0)
self.assertAlmostEqual(LA.norm(lcat.toarray() - np.vstack((self.lopA,
self.lopA))),
0)
self.assertEqual(lcat.shape[0], self.lop.shape[0] + self.lop.shape[0])
# using hstack and vstack
#TODO: re-enable later (when LazyLinearOp2 will replace LazyLinearOp)
# lcat = vstack((self.lop, self.lop2, self.lop))
# self.assertAlmostEqual(LA.norm(lcat.toarray() - np.vstack((self.lopA,
# self.lop2A,
# self.lopA))),
# 0)
# self.assertEqual(lcat.shape[0], 2 * self.lop.shape[0] + self.lop2.shape[0])
# lcat = hstack((self.lop, self.lop2, self.lopA))
# self.assertAlmostEqual(LA.norm(lcat.toarray() - np.hstack((self.lopA,
# self.lop2A,
# self.lopA))),
# 0)
# self.assertEqual(lcat.shape[1], 2 * self.lop.shape[1] + self.lop2.shape[1])
def test_chain_ops(self):
lchain = self.lop + self.lop2
lchain = lchain @ self.lop3
lchain = 2 * lchain
v = np.random.rand(lchain.shape[1])
lchain = lchain * v
lchain = lchain.concatenate(self.lop3, axis=0)
mat_ref = np.vstack(((2 * (self.lopA + self.lop2A) @ self.lop3A) * v,
self.lop3A))
self.assertAlmostEqual(LA.norm(lchain.toarray() - mat_ref),
0)
def test_get_item(self):
n1 = self.lop.shape[0]//2
n2 = self.lop.shape[1]//2
lslice = self.lop[3:n1, 3:n2]
lsliceA = self.lopA[3:n1, 3:n2]
self.assertAlmostEqual(LA.norm(lslice.toarray()-lsliceA), 0)
def test_real(self):
cF = pf.rand(self.lop.shape[0], self.lop.shape[1], field='complex')
lcF = LazyLinearOp2.create_from_op(cF)
lcF = lcF.real
self.assertAlmostEqual(LA.norm(lcF.toarray()-cF.real.toarray()), 0)
def test_imag(self):
cF = pf.rand(self.lop.shape[0], self.lop.shape[1], field='complex')
lcF = LazyLinearOp2.create_from_op(cF)
lcF = lcF.imag
self.assertAlmostEqual(LA.norm(lcF.toarray()-cF.imag.toarray()), 0)
def test_aslazylinop(self):
from pyfaust.lazylinop import asLazyLinearOp
cF = pf.rand(self.lop.shape[0], self.lop.shape[1], field='complex')
#TODO: re-enable later (when LazyLinearOp2 will replace LazyLinearOp)
# lcF = asLazyLinearOp(cF)
# self.assertTrue(pf.lazylinop.LazyLinearOp2.isLazyLinearOp(lcF))
# self.assertEqual(cF.shape, lcF.shape)
class TestLazyLinearOpFFTFunc(TestLazyLinearOpFaust):
def setUp(self):
from scipy.fft import fft, ifft
# axis = 0 to be consistent with LazyLinearOp2.toarray() which applies
# fft on columns of the matrix, not on the rows (axis=1)
self.lop = LazyLinearOp2.create_from_funcs(lambda x: fft(x, axis=0),
lambda x: 8 * ifft(x, axis=0), (8, 8))
self.lopA = self.lop.toarray()
self.lop2 = LazyLinearOp2.create_from_op(pf.rand(self.lop.shape[0], self.lop.shape[1]))
self.lop2A = self.lop2.toarray()
self.lop3 = LazyLinearOp2.create_from_op(pf.rand(self.lop.shape[1], self.lop.shape[0]))
self.lop3A = self.lop3.toarray()
if '__main__' == __name__:
unittest.main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment