test_smoothing_operator.py 5.4 KB
 Martin Reinecke committed May 12, 2017 1 2 3 4 5 6 7 8 9 10 11 12 ``````# This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . `````` Theo Steininger committed May 24, 2017 13 14 15 16 17 ``````# # Copyright(C) 2013-2017 Max-Planck-Society # # NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik # and financially supported by the Studienstiftung des deutschen Volkes. `````` Martin Reinecke committed May 12, 2017 18 19 20 `````` import unittest import numpy as np `````` Martin Reinecke committed May 15, 2017 21 ``````from numpy.testing import assert_equal, assert_approx_equal,\ `````` Martin Reinecke committed May 12, 2017 22 `````` assert_allclose `````` Pumpe, Daniel (dpumpe) committed May 14, 2017 23 `````` `````` Martin Reinecke committed May 12, 2017 24 25 26 27 ``````from nifty import Field,\ RGSpace,\ PowerSpace,\ SmoothingOperator `````` Pumpe, Daniel (dpumpe) committed May 14, 2017 28 `````` `````` Martin Reinecke committed May 12, 2017 29 30 31 32 33 34 35 36 37 38 ``````from itertools import product from test.common import expand def _get_rtol(tp): if (tp == np.float64) or (tp == np.complex128): return 1e-10 else: return 1e-5 `````` Pumpe, Daniel (dpumpe) committed May 14, 2017 39 ``````class SmoothingOperator_Tests(unittest.TestCase): `````` 40 `````` spaces = [RGSpace(128)] `````` Pumpe, Daniel (dpumpe) committed May 14, 2017 41 42 43 `````` @expand(product(spaces, [0., .5, 5.], [True, False])) def test_property(self, space, sigma, log_distances): `````` Martin Reinecke committed Jun 30, 2017 44 `````` op = SmoothingOperator.make(space, sigma=sigma, `````` Pumpe, Daniel (dpumpe) committed May 14, 2017 45 46 47 48 49 50 51 52 53 54 55 56 57 58 `````` log_distances=log_distances) if op.domain[0] != space: raise TypeError if op.unitary != False: raise ValueError if op.self_adjoint != True: raise ValueError if op.sigma != sigma: raise ValueError if op.log_distances != log_distances: raise ValueError @expand(product(spaces, [0., .5, 5.], [True, False])) def test_adjoint_times(self, space, sigma, log_distances): `````` Martin Reinecke committed Jun 30, 2017 59 `````` op = SmoothingOperator.make(space, sigma=sigma, `````` Pumpe, Daniel (dpumpe) committed May 14, 2017 60 61 62 `````` log_distances=log_distances) rand1 = Field.from_random('normal', domain=space) rand2 = Field.from_random('normal', domain=space) `````` Martin Reinecke committed Jun 20, 2017 63 64 `````` tt1 = rand1.vdot(op.times(rand2)) tt2 = rand2.vdot(op.adjoint_times(rand1)) `````` Pumpe, Daniel (dpumpe) committed May 14, 2017 65 66 `````` assert_approx_equal(tt1, tt2) `````` Pumpe, Daniel (dpumpe) committed May 14, 2017 67 `````` @expand(product(spaces, [0., .5, 5.], [False])) `````` Pumpe, Daniel (dpumpe) committed May 14, 2017 68 `````` def test_times(self, space, sigma, log_distances): `````` Martin Reinecke committed Jun 30, 2017 69 `````` op = SmoothingOperator.make(space, sigma=sigma, `````` Pumpe, Daniel (dpumpe) committed May 14, 2017 70 71 72 73 74 `````` log_distances=log_distances) rand1 = Field(space, val=0.) rand1.val[0] = 1. tt1 = op.times(rand1) assert_approx_equal(1, tt1.sum()) `````` Martin Reinecke committed May 12, 2017 75 `````` `````` Pumpe, Daniel (dpumpe) committed May 14, 2017 76 77 `````` @expand(product(spaces, [0., .5, 5.], [True, False])) def test_inverse_adjoint_times(self, space, sigma, log_distances): `````` Martin Reinecke committed Jun 30, 2017 78 `````` op = SmoothingOperator.make(space, sigma=sigma, `````` Pumpe, Daniel (dpumpe) committed May 14, 2017 79 80 81 `````` log_distances=log_distances) rand1 = Field.from_random('normal', domain=space) rand2 = Field.from_random('normal', domain=space) `````` Martin Reinecke committed Jun 20, 2017 82 83 `````` tt1 = rand1.vdot(op.inverse_times(rand2)) tt2 = rand2.vdot(op.inverse_adjoint_times(rand1)) `````` Pumpe, Daniel (dpumpe) committed May 14, 2017 84 `````` assert_approx_equal(tt1, tt2) `````` Martin Reinecke committed May 12, 2017 85 `````` `````` 86 `````` @expand(product([128, 256], [1, 0.4], [0., 1., 3.7], `````` Martin Reinecke committed May 12, 2017 87 88 89 90 `````` [np.float64, np.complex128])) def test_smooth_regular1(self, sz, d, sigma, tp): tol = _get_rtol(tp) sp = RGSpace(sz, harmonic=True, distances=d) `````` Martin Reinecke committed Jun 30, 2017 91 `````` smo = SmoothingOperator.make(sp, sigma=sigma) `````` Martin Reinecke committed May 12, 2017 92 93 94 95 96 97 98 99 100 101 102 `````` inp = Field.from_random(domain=sp, random_type='normal', std=1, mean=4, dtype=tp) out = smo(inp) inp = inp.val.get_full_data() assert_allclose(inp.sum(), out.sum(), rtol=tol, atol=tol) @expand(product([10, 15], [7, 10], [1, 0.4], [2, 0.3], [0., 1., 3.7], [np.float64, np.complex128])) def test_smooth_regular2(self, sz1, sz2, d1, d2, sigma, tp): tol = _get_rtol(tp) sp = RGSpace([sz1, sz2], distances=[d1, d2], harmonic=True) `````` Martin Reinecke committed Jun 30, 2017 103 `````` smo = SmoothingOperator.make(sp, sigma=sigma) `````` Martin Reinecke committed May 12, 2017 104 105 106 107 108 109 110 111 112 113 114 115 `````` inp = Field.from_random(domain=sp, random_type='normal', std=1, mean=4, dtype=tp) out = smo(inp) inp = inp.val.get_full_data() assert_allclose(inp.sum(), out.sum(), rtol=tol, atol=tol) @expand(product([100, 200], [False, True], [0., 1., 3.7], [np.float64, np.complex128])) def test_smooth_irregular1(self, sz, log, sigma, tp): tol = _get_rtol(tp) sp = RGSpace(sz, harmonic=True) ps = PowerSpace(sp, nbin=sz, logarithmic=log) `````` Martin Reinecke committed Jun 30, 2017 116 `````` smo = SmoothingOperator.make(ps, sigma=sigma) `````` Martin Reinecke committed May 12, 2017 117 118 119 120 121 122 123 124 125 126 127 128 `````` inp = Field.from_random(domain=ps, random_type='normal', std=1, mean=4, dtype=tp) out = smo(inp) inp = inp.val.get_full_data() assert_allclose(inp.sum(), out.sum(), rtol=tol, atol=tol) @expand(product([10, 15], [7, 10], [False, True], [0., 1., 3.7], [np.float64, np.complex128])) def test_smooth_irregular2(self, sz1, sz2, log, sigma, tp): tol = _get_rtol(tp) sp = RGSpace([sz1, sz2], harmonic=True) ps = PowerSpace(sp, logarithmic=log) `````` Martin Reinecke committed Jun 30, 2017 129 `````` smo = SmoothingOperator.make(ps, sigma=sigma) `````` Martin Reinecke committed May 12, 2017 130 131 132 133 134 `````` inp = Field.from_random(domain=ps, random_type='normal', std=1, mean=4, dtype=tp) out = smo(inp) inp = inp.val.get_full_data() assert_allclose(inp.sum(), out.sum(), rtol=tol, atol=tol)``````