Commit 2466ba66 authored by Philipp  Trunschke's avatar Philipp Trunschke

add python test scripts

parent ef4e46c4
Pipeline #1043 passed with stages
in 7 minutes and 53 seconds
......@@ -7,7 +7,8 @@ help:
@printf "Possible make targets are:\n \
\t\tshared \t\t -- Build xerus as a shared library.\n \
\t\tstatic \t\t -- Build xerus as a static library.\n \
\t\tpython \t\t -- Build the xerus python wrappers.\n \
\t\tpython2 \t\t -- Build the xerus python2 wrappers.\n \
\t\tpython3 \t\t -- Build the xerus python3 wrappers.\n \
\t\tdoc \t\t -- Build the html documentation for the xerus library.\n \
\t\tinstall \t -- Install the shared library and header files (may require root).\n \
\t\ttest \t\t -- Build and run the xerus unit tests.\n \
......@@ -153,7 +154,8 @@ build/libxerus.so: $(MINIMAL_DEPS) $(XERUS_SOURCES) build/libxerus_misc.so
$(CXX) -shared -fPIC -Wl,-soname,libxerus.so $(FLAGS) -I include $(XERUS_SOURCES) -L ./build/ -Wl,--as-needed -lxerus_misc $(SUITESPARSE) $(LAPACK_LIBRARIES) $(BLAS_LIBRARIES) -o build/libxerus.so
python: build/python2/xerus.so build/python3/xerus.so
python2: build/python2/xerus.so
python3: build/python3/xerus.so
build/python2/xerus.so: $(MINIMAL_DEPS) $(PYTHON_SOURCES) build/libxerus.so
mkdir -p $(dir $@)
......@@ -231,8 +233,11 @@ test: $(TEST_NAME)
./$(TEST_NAME) all
test_python: # build/libxerus.so build/python3/xerus.so
@export PYTHONPATH=build/python3:${PYTHONPATH}; export LD_LIBRARY_PATH=build:${LD_LIBRARY_PATH}; pytest src/pyTests
test_python2: # build/libxerus.so build/python2/xerus.so
@PYTHONPATH=build/python2:${PYTHONPATH} LD_LIBRARY_PATH=build:${LD_LIBRARY_PATH} $(PYTEST2) src/pyTests
test_python3: # build/libxerus.so build/python3/xerus.so
@PYTHONPATH=build/python3:${PYTHONPATH} LD_LIBRARY_PATH=build:${LD_LIBRARY_PATH} $(PYTEST3) src/pyTests
fullTest: $(TUTORIALS) $(TEST_NAME)
......
......@@ -3,10 +3,13 @@
#=================================================================================================
# Xerus can be compiled either with G++ or the Clang++ frontend of the LLVM.
# Set the CXX variable to the one you want to use.
# If you want to use OpenMP, make sure that your compiler supports OpenMP 4.0.
CXX = g++
# CXX = clang++
PYTHON2_CONFIG = python2-config
PYTHON3_CONFIG = python3-config
PYTHON2_TEST = pytest2
PYTHON3_TEST = pytest3
#=================================================================================================
# C++ Version
......
# np.random.seed(1337)
# def build_TestNumpyInterface(seed, num, ext):
# rsg = np.random.RandomState(seed)
# mnkls = rsg.randint(1, 20, (num,4))
# def make_test_to_ndarray(mn, kl):
# def test(self):
# ls = rsg.randn(ext, *mn)
# rs = rsg.randn(ext, *kl)
# for l,r in zip(ls,rs):
# lr = np.kron(l,r)
# L,R = project(lr, mn, kl)
# diff = norm(lr - np.kron(L,R))
# self.assertLessEqual(diff, 1e-12)
# return test
# def make_test_from_ndarray(mn, kl):
# def test(self):
# ls = rsg.randn(ext, *mn)
# rs = rsg.randn(ext, *kl)
# for l,r in zip(ls,rs):
# lr = np.kron(l,r)
# L,R = project(lr, mn, kl)
# diff = norm(lr - np.kron(L,R))
# self.assertLessEqual(diff, 1e-12)
# return test
# odir = dict()
# for mnkl in mnkls:
# name = "test_random_{}x{}_{}x{}".format(*mnkl)
# test = make_test(mnkl[:2], mnkl[2:])
# odir[name] = test
# return type("TestRandomKroneckerSplitting", (unittest.TestCase,), odir)
# TestRandomKroneckerSplitting = build_TestNumpyInterface(0, 100, 4)
from __future__ import division
import unittest
import itertools as _iter
import xerus as xe
import numpy as np
np.random.seed(1337)
ranges = lambda *args: _iter.product(*[range(arg) for arg in args])
class TestExtendedTT(unittest.TestCase):
def setUp(self):
self.dimension = [4, 6, 8]
self.ranks = [1, 8, 5, 1]
def test_from_function_set_component(self):
tt = xe.TTTensor(self.dimension)
arrs = []
for i in range(len(self.dimension)):
shape = [self.ranks[i], self.dimension[i], self.ranks[i+1]]
arr = np.random.randn(*shape)
x = xe.Tensor.from_function(shape, lambda x: arr[tuple(x)])
tt.set_component(i, x)
arrs.append(arr)
for i in range(len(self.dimension)):
self.assertTrue(np.all(arrs[i] == tt.get_component(i).to_ndarray()))
def test_eval_hermite(self):
from numpy.polynomial.hermite_e import hermeval
basis = xe.PolynomBasis.Hermite
tt = xe.TTTensor(self.dimension)
arrs = []
for i in range(len(self.dimension)):
shape = [self.ranks[i], self.dimension[i], self.ranks[i+1]]
arr = np.random.randn(*shape)
x = xe.Tensor.from_function(shape, lambda x: arr[tuple(x)])
tt.set_component(i, x)
arrs.append(arr)
for (i, j, k) in ranges(*self.dimension):
x = xe.uq_tt_evaluate(tt, [j, k], basis)
x = x[[i]]
# loc_extt = extt([i, j, k])
# self.assertLessEqual(np.abs((loc_extt - loc_xett)/loc_extt), 1e-10)
import unittest
import xerus as xe
import numpy as np
class TestReconstruction(unittest.TestCase):
def test_small_reconstruction(self):
# the function to approximate
def fnc(x, y):
return np.sin(2*np.pi*x)*(y[0] + 0.1*y[1]**2) + np.cos(2*np.pi*x)*y[1]
x_dim = 100
y_dim = 2
n_samples = 10000
n_test_samples = 100
x = np.linspace(0, 1, x_dim)
def discretized_fnc(y):
return fnc(x, y)
nodes = 2*np.random.rand(n_samples, y_dim)-1
measurements = xe.UQMeasurementSet()
for y in nodes:
u = discretized_fnc(y)
measurements.add(y, xe.Tensor.from_ndarray(u))
basis = xe.PolynomBasis.Legendre
dimension = [x_dim] + [3]*y_dim
reco = xe.uq_ra_adf(measurements, basis, dimension, targeteps=1e-8, maxitr=70)
test_nodes = 2*np.random.rand(n_test_samples, y_dim)-1
error = 0
for y in test_nodes:
res = xe.uq_tt_evaluate(reco, y, basis).to_ndarray()
ref = discretized_fnc(y)
error += np.linalg.norm(res - ref) / np.linalg.norm(ref)
error /= n_test_samples
self.assertLessEqual(error, 1e-3)
# def test_large_reconstruction(self):
# data = np.load('samples.npz')
# measurements = xe.UQMeasurementSet()
# for y,u in zip(data['nodes'], data['values']):
# measurements.add(y, xe.Tensor.from_ndarray(u))
# basis = xe.PolynomBasis.Legendre
# dimension = [data['values'].shape[1]] + [8]*data['nodes'].shape[1]
# reco = xe.uq_ra_adf(measurements, basis, dimension, targeteps=1e-8, maxitr=1000)
# #TODO: just assert that the residuum is below 1e-2
# ref = xe.load("reconstruction.xrs")
# self.assertLessEqual(xe.frob_norm(reco - ref), 1e-8)
if __name__ == '__main__':
unittest.main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment