Mentions légales du service

Skip to content
Snippets Groups Projects
Commit 73e77c40 authored by hhakim's avatar hhakim
Browse files

Add TestPoly and inside a unit test for pyfaut.poly.basis.

parent 3f425f25
No related branches found
No related tags found
No related merge requests found
......@@ -9,9 +9,6 @@ import tempfile
import os
import random
dev = 'cpu'
field = 'real'
class TestFaust(unittest.TestCase):
......@@ -20,16 +17,27 @@ class TestFaust(unittest.TestCase):
MAX_DIM_SIZE = 256
MIN_DIM_SIZE = 3
def __init__(self, methodName='runTest', dev='cpu', field='real'):
super(TestFaust, self).__init__(methodName)
self.dev = dev
self.field = field
def setUp(self):
"""
"""
# self.dev = 'cpu'
# self.field = 'real'
# if 'dev' in os.environ:
# self.dev = os.environ['dev']
# if 'field' in os.environ:
# self.field = os.environ['field']
nrows = randint(TestFaust.MIN_DIM_SIZE,
TestFaust.MAX_DIM_SIZE+1)
ncols = randint(TestFaust.MIN_DIM_SIZE,
TestFaust.MAX_DIM_SIZE+1)
nfacts = randint(TestFaust.MIN_NUM_FACTORS,
TestFaust.MAX_NUM_FACTORS+1)
self.F = frand(nrows, ncols, num_factors=nfacts, dev=dev, field=field)
self.F = frand(nrows, ncols, num_factors=nfacts, dev=self.dev, field=self.field)
self.nrows = nrows
self.ncols = ncols
self.nfacts = nfacts
......@@ -85,7 +93,7 @@ class TestFaust(unittest.TestCase):
def test_device(self):
print("Faust.device")
self.assertEqual(self.F.device, dev)
self.assertEqual(self.F.device, self.dev)
def test_transpose(self):
print("Faust.transpose")
......@@ -115,7 +123,7 @@ class TestFaust(unittest.TestCase):
def test_add(self):
print("Faust.__add__, __radd__")
G = frand(self.nrows, self.ncols, dev=dev)
G = frand(self.nrows, self.ncols, dev=self.dev)
self.assertTrue(np.allclose((self.F+G).toarray(),
self.F.toarray()+G.toarray()))
self.assertTrue(np.allclose((self.F+G.toarray()).toarray(),
......@@ -123,7 +131,7 @@ class TestFaust(unittest.TestCase):
def test_sub(self):
print("Faust.__sub__, __rsub__")
G = frand(self.nrows, self.ncols, dev=dev)
G = frand(self.nrows, self.ncols, dev=self.dev)
self.assertTrue(np.allclose((self.F-G).toarray(),
self.F.toarray()-G.toarray()))
self.assertTrue(np.allclose((self.F-G.toarray()).toarray(),
......@@ -135,7 +143,7 @@ class TestFaust(unittest.TestCase):
def test_matmul(self):
print("Faust.__matmul__, dot, __rmatmul__")
G = frand(self.ncols, self.nrows, dev=dev)
G = frand(self.ncols, self.nrows, dev=self.dev)
self.assertTrue(np.allclose((self.F@G).toarray(),
self.F.toarray()@G.toarray()))
self.assertTrue(np.allclose((self.F@G.toarray()),
......@@ -150,7 +158,7 @@ class TestFaust(unittest.TestCase):
def test_concatenate(self):
print("Faust.concatenate, pyfaust.vstack, pyfaust.hstack")
G = frand(self.nrows, self.ncols, dev=dev)
G = frand(self.nrows, self.ncols, dev=self.dev)
self.assertTrue(np.allclose((self.F.concatenate(G)).toarray(),
np.concatenate((self.F.toarray(),
G.toarray()))))
......@@ -324,13 +332,13 @@ class TestFaust(unittest.TestCase):
print("Faust.optimize_time")
# test only if CPU and no gpu_mod enabled
# anyway the method is not yet implemented for GPU
if dev == 'cpu' and not is_gpu_mod_enabled():
if self.dev == 'cpu' and not is_gpu_mod_enabled():
oF = self.F.optimize_time()
self._assertAlmostEqual(oF, self.F)
def test_clone(self):
print("Faust.clone")
if dev =='cpu':
if self.dev =='cpu':
Fc = self.F.clone()
elif dev == 'gpu':
Fc = self.F.clone()
......
import unittest
from pyfaust.poly import basis
from numpy.random import randint
import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse.linalg import aslinearoperator
import tempfile
import os
import random
dev = 'cpu'
field = 'real'
class TestPoly(unittest.TestCase):
def __init__(self, methodName='runTest', dev='cpu', field='real'):
super(TestPoly, self).__init__(methodName)
self.dev = dev
self.field = field
def setUp(self):
pass
def test_basis(self):
print("Test basis()")
from scipy.sparse import random
d = 50
if self.field == 'complex':
dtype = 'complex'
else:
dtype = 'double'
L = random(d, d, .02, format='csr', dtype=dtype)
L = L@L.T
K = 5
F = basis(L, K, 'chebyshev', dev=self.dev)
# assert the dimensions are consistent to L
self.assertEqual(F.shape[0], (K+1)*L.shape[0])
self.assertEqual(F.shape[1], L.shape[0])
# assert the 0-degree polynomial matrix is the identity
last_fac = F.factors(F.numfactors()-1).toarray()
Id = np.eye(d)
self.assertTrue(np.allclose(Id, last_fac))
if K >= 1:
# assert the 1-degree polynomial matrix is in the form [Id ; L]
deg1_fac = F.factors(F.numfactors()-2).toarray()
self.assertTrue(np.allclose(deg1_fac[:d, :], Id))
self.assertTrue(np.allclose(deg1_fac[d:2*d, :], L.toarray()))
if K >= 2:
# assert the 2-degree polynomial matrix is in the form [Id ; [-Id, L]]
I2d = np.eye(2*d)
deg2_fac = F.factors(F.numfactors()-3).toarray()
self.assertTrue(np.allclose(deg2_fac[:2*d, :], I2d))
self.assertTrue(np.allclose(deg2_fac[2*d:, :d], -Id))
self.assertTrue(np.allclose(deg2_fac[2*d:, d:], 2*L.toarray()))
if K >= 3:
# assert the n-degree polynomial matrix is in the form
# [I_nd ; [0 , -Id, 2L]]
for n in range(3, K):
Ind = np.eye(n*d)
degn_fac = F.factors(F.numfactors()-n-1).toarray()
self.assertTrue(np.allclose(degn_fac[:n*d, :], Ind))
self.assertTrue(np.allclose(degn_fac[n*d:, -2*d:-d], -Id))
self.assertTrue(np.allclose(degn_fac[n*d:, -d:],
2*L.toarray()))
zero_part = degn_fac[n*d:, :-2*d]
self.assertTrue(np.linalg.norm(zero_part) == 0)
import unittest
from pyfaust.tests.TestFaust import TestFaust
from pyfaust.tests.TestPoly import TestPoly
dev = 'cpu'
field = 'real'
def run_tests(_dev, _field):
global dev, field
dev = _dev
field = _field
suite = unittest.makeSuite(TestFaust, 'test')
def run_tests(dev, field):
"""
Runs all available tests using device dev ('cpu' or 'gpu') and scalar type
field ('real' or 'complex') when it applies.
"""
runner = unittest.TextTestRunner()
suite = unittest.TestSuite()
for class_name in ['TestFaust', 'TestPoly']:
testloader = unittest.TestLoader()
test_names = eval("testloader.getTestCaseNames("+class_name+")")
for meth_name in test_names:
test = eval(""+class_name+"('"+meth_name+"', dev=dev, field=field)")
suite.addTest(test)
runner.run(suite)
import unittest
from pyfaust.tests.TestFaust import TestFaust
from pyfaust.tests.TestPoly import TestPoly
import sys
dev = 'cpu'
field = 'real'
if __name__ == "__main__":
nargs = len(sys.argv)
if(nargs > 1):
......@@ -14,10 +18,20 @@ if __name__ == "__main__":
raise ValueError("field must be complex or float")
del sys.argv[2] # deleted to avoid interfering with unittest
del sys.argv[1]
if(len(sys.argv) > 1):
# ENOTE: test only a single test if name passed on command line
singleton = unittest.TestSuite()
singleton.addTest(TestFaust(sys.argv[1]))
unittest.TextTestRunner().run(singleton)
if(nargs > 1):
# it remains a method fully qualified method name to test
# e.g. TestFaust.test_transpose
class_name, meth_name = sys.argv[1].split('.')[:]
testloader = unittest.TestLoader()
test_names = eval("testloader.getTestCaseNames("+class_name+")")
if meth_name in test_names:
test = eval(""+class_name+"('"+meth_name+"', dev=dev, field=field)")
else:
raise ValueError(meth_name +" is not in "+class_name)
suite = unittest.TestSuite()
suite.addTest(test)
runner = unittest.TextTestRunner()
runner.run(suite)
else:
# run all tests
unittest.main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment