test_representation.py 6.92 KB
 Reimar H Leike committed Jan 23, 2019 1 2 3 4 5 6 7 8 9 10 11 12 13 ``````# 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 . # `````` Martin Reinecke committed Mar 20, 2020 14 ``````# Copyright(C) 2013-2020 Max-Planck-Society `````` Reimar H Leike committed Jan 23, 2019 15 16 17 18 19 20 ``````# # NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik. import numpy as np import pytest `````` Martin Reinecke committed Nov 26, 2019 21 ``````import nifty6 as ift `````` Reimar H Leike committed Jan 23, 2019 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 `````` from ..common import list2fixture _h_RG_spaces = [ ift.RGSpace(7, distances=0.2, harmonic=True), ift.RGSpace((12, 46), distances=(.2, .3), harmonic=True) ] _h_spaces = _h_RG_spaces + [ift.LMSpace(17)] _p_RG_spaces = [ ift.RGSpace(19, distances=0.7), ift.RGSpace((1, 2, 3, 6), distances=(0.2, 0.25, 0.34, .8)) ] _p_spaces = _p_RG_spaces + [ift.HPSpace(17), ift.GLSpace(8, 13)] _pow_spaces = [ift.PowerSpace(ift.RGSpace((17, 38), harmonic=True))] pmp = pytest.mark.parametrize dtype = list2fixture([np.float64, np.complex128]) `````` Martin Reinecke committed Jan 24, 2019 41 ``````def _check_repr(op): `````` Philipp Arras committed Jan 24, 2019 42 `````` op.__repr__() `````` Martin Reinecke committed Jan 24, 2019 43 44 `````` `````` Reimar H Leike committed Jan 23, 2019 45 46 ``````@pmp('sp', _p_RG_spaces) def testLOSResponse(sp, dtype): `````` Martin Reinecke committed Mar 20, 2020 47 48 49 50 `````` starts = ift.random.current_rng().standard_normal((len(sp.shape), 10)) ends = ift.random.current_rng().standard_normal((len(sp.shape), 10)) sigma_low = 1e-4*ift.random.current_rng().standard_normal(10) sigma_ups = 1e-5*ift.random.current_rng().standard_normal(10) `````` Martin Reinecke committed Jan 24, 2019 51 `````` _check_repr(ift.LOSResponse(sp, starts, ends, sigma_low, sigma_ups)) `````` Reimar H Leike committed Jan 23, 2019 52 53 54 55 56 57 `````` @pmp('sp', _h_spaces + _p_spaces + _pow_spaces) def testOperatorCombinations(sp, dtype): a = ift.DiagonalOperator(ift.Field.from_random("normal", sp, dtype=dtype)) b = ift.DiagonalOperator(ift.Field.from_random("normal", sp, dtype=dtype)) `````` Martin Reinecke committed Jan 24, 2019 58 59 `````` _check_repr(ift.SandwichOperator.make(a, b)) _check_repr(a(b)) `````` Philipp Arras committed Aug 01, 2019 60 61 `````` _check_repr(a+b) _check_repr(a-b) `````` Martin Reinecke committed Jan 24, 2019 62 63 `````` _check_repr(a*b) _check_repr(a**2) `````` Reimar H Leike committed Jan 23, 2019 64 65 66 67 `````` def testLinearInterpolator(): sp = ift.RGSpace((10, 8), distances=(0.1, 3.5)) `````` Martin Reinecke committed Mar 20, 2020 68 `````` pos = ift.random.current_rng().random((2, 23)) `````` Reimar H Leike committed Jan 23, 2019 69 70 `````` pos[0, :] *= 0.9 pos[1, :] *= 7*3.5 `````` Martin Reinecke committed Jan 24, 2019 71 `````` _check_repr(ift.LinearInterpolator(sp, pos)) `````` Reimar H Leike committed Jan 23, 2019 72 73 74 75 76 `````` @pmp('sp', _h_spaces + _p_spaces + _pow_spaces) def testOperatorAdaptor(sp, dtype): op = ift.DiagonalOperator(ift.Field.from_random("normal", sp, dtype=dtype)) `````` Martin Reinecke committed Jan 24, 2019 77 78 79 `````` _check_repr(op.adjoint) _check_repr(op.inverse) _check_repr(op.inverse.adjoint) `````` Reimar H Leike committed Jan 23, 2019 80 81 82 83 84 85 86 87 88 `````` @pmp('sp1', _h_spaces + _p_spaces + _pow_spaces) @pmp('sp2', _h_spaces + _p_spaces + _pow_spaces) def testNullOperator(sp1, sp2, dtype): op = ift.NullOperator(sp1, sp2) ift.extra.consistency_check(op, dtype, dtype) mdom1 = ift.MultiDomain.make({'a': sp1}) mdom2 = ift.MultiDomain.make({'b': sp2}) `````` Martin Reinecke committed Jan 24, 2019 89 90 91 `````` _check_repr(ift.NullOperator(mdom1, mdom2)) _check_repr(ift.NullOperator(sp1, mdom2)) _check_repr(ift.NullOperator(mdom1, sp2)) `````` Reimar H Leike committed Jan 23, 2019 92 93 94 95 `````` @pmp('sp', _p_RG_spaces) def testHarmonicSmoothingOperator(sp, dtype): `````` Martin Reinecke committed Jan 24, 2019 96 `````` _check_repr(ift.HarmonicSmoothingOperator(sp, 0.1)) `````` Reimar H Leike committed Jan 23, 2019 97 98 99 100 101 102 103 104 `````` @pmp('sp', _h_spaces + _p_spaces + _pow_spaces) def testDOFDistributor(sp, dtype): # TODO: Test for DomainTuple if sp.size < 4: return dofdex = np.arange(sp.size).reshape(sp.shape) % 3 `````` Martin Reinecke committed Dec 05, 2019 105 `````` dofdex = ift.Field.from_raw(sp, dofdex) `````` Martin Reinecke committed Jan 24, 2019 106 `````` _check_repr(ift.DOFDistributor(dofdex)) `````` Reimar H Leike committed Jan 23, 2019 107 108 109 110 `````` @pmp('sp', _h_spaces) def testPPO(sp, dtype): `````` Martin Reinecke committed Jan 24, 2019 111 `````` _check_repr(ift.PowerDistributor(target=sp)) `````` Reimar H Leike committed Jan 23, 2019 112 113 `````` ps = ift.PowerSpace( sp, ift.PowerSpace.useful_binbounds(sp, logarithmic=False, nbin=3)) `````` Martin Reinecke committed Jan 24, 2019 114 `````` _check_repr(ift.PowerDistributor(target=sp, power_space=ps)) `````` Reimar H Leike committed Jan 23, 2019 115 116 `````` ps = ift.PowerSpace( sp, ift.PowerSpace.useful_binbounds(sp, logarithmic=True, nbin=3)) `````` Martin Reinecke committed Jan 24, 2019 117 `````` _check_repr(ift.PowerDistributor(target=sp, power_space=ps)) `````` Reimar H Leike committed Jan 23, 2019 118 119 120 121 `````` @pmp('sp', _h_RG_spaces + _p_RG_spaces) def testFFT(sp, dtype): `````` Martin Reinecke committed Jan 24, 2019 122 123 `````` _check_repr(ift.FFTOperator(sp)) _check_repr(ift.FFTOperator(sp.get_default_codomain())) `````` Reimar H Leike committed Jan 23, 2019 124 125 126 127 `````` @pmp('sp', _h_RG_spaces + _p_RG_spaces) def testHartley(sp, dtype): `````` Martin Reinecke committed Jan 24, 2019 128 129 `````` _check_repr(ift.HartleyOperator(sp)) _check_repr(ift.HartleyOperator(sp.get_default_codomain())) `````` Reimar H Leike committed Jan 23, 2019 130 131 132 133 `````` @pmp('sp', _h_spaces) def testHarmonic(sp, dtype): `````` Martin Reinecke committed Jan 24, 2019 134 `````` _check_repr(ift.HarmonicTransformOperator(sp)) `````` Reimar H Leike committed Jan 23, 2019 135 136 137 138 139 `````` @pmp('sp', _p_spaces) def testMask(sp, dtype): # Create mask `````` Martin Reinecke committed Dec 04, 2019 140 `````` f = ift.from_random('normal', sp).val `````` Reimar H Leike committed Jan 23, 2019 141 142 `````` mask = np.zeros_like(f) mask[f > 0] = 1 `````` Martin Reinecke committed Dec 05, 2019 143 `````` mask = ift.Field.from_raw(sp, mask) `````` Reimar H Leike committed Jan 23, 2019 144 `````` # Test MaskOperator `````` Martin Reinecke committed Jan 24, 2019 145 `````` _check_repr(ift.MaskOperator(mask)) `````` Reimar H Leike committed Jan 23, 2019 146 147 148 149 150 `````` @pmp('sp', _h_spaces + _p_spaces) def testDiagonal(sp, dtype): op = ift.DiagonalOperator(ift.Field.from_random("normal", sp, dtype=dtype)) `````` Martin Reinecke committed Jan 24, 2019 151 `````` _check_repr(op) `````` Reimar H Leike committed Jan 23, 2019 152 153 154 155 `````` @pmp('sp', _h_spaces + _p_spaces + _pow_spaces) def testGeometryRemover(sp, dtype): `````` Martin Reinecke committed Jan 24, 2019 156 `````` _check_repr(ift.GeometryRemover(sp)) `````` Reimar H Leike committed Jan 23, 2019 157 158 159 160 161 162 `````` @pmp('spaces', [0, 1, 2, 3, (0, 1), (0, 2), (0, 1, 2), (0, 2, 3), (1, 3)]) @pmp('wgt', [0, 1, 2, -1]) def testContractionOperator(spaces, wgt, dtype): dom = (ift.RGSpace(10), ift.RGSpace(13), ift.GLSpace(5), ift.HPSpace(4)) `````` Martin Reinecke committed Jan 24, 2019 163 `````` _check_repr(ift.ContractionOperator(dom, spaces, wgt)) `````` Reimar H Leike committed Jan 23, 2019 164 165 166 `````` def testDomainTupleFieldInserter(): `````` Martin Reinecke committed Jan 24, 2019 167 168 `````` target = ift.DomainTuple.make((ift.UnstructuredDomain([3, 2]), ift.UnstructuredDomain(7), `````` Reimar H Leike committed Jan 23, 2019 169 `````` ift.RGSpace([4, 22]))) `````` Martin Reinecke committed Jan 24, 2019 170 `````` _check_repr(ift.DomainTupleFieldInserter(target, 1, (5,))) `````` Reimar H Leike committed Jan 23, 2019 171 172 173 174 175 176 177 178 179 `````` @pmp('space', [0, 2]) @pmp('factor', [1, 2, 2.7]) @pmp('central', [False, True]) def testZeroPadder(space, factor, dtype, central): dom = (ift.RGSpace(10), ift.UnstructuredDomain(13), ift.RGSpace(7, 12), ift.HPSpace(4)) newshape = [int(factor*l) for l in dom[space].shape] `````` Martin Reinecke committed Jan 24, 2019 180 `````` _check_repr(ift.FieldZeroPadder(dom, newshape, space, central)) `````` Reimar H Leike committed Jan 23, 2019 181 182 183 184 185 186 187 188 `````` @pmp('args', [[ift.RGSpace( (13, 52, 40)), (4, 6, 25), None], [ift.RGSpace( (128, 128)), (45, 48), 0], [ift.RGSpace(13), (7,), None], [ (ift.HPSpace(3), ift.RGSpace((12, 24), distances=0.3)), (12, 12), 1 ]]) def testRegridding(args): `````` Martin Reinecke committed Jan 24, 2019 189 `````` _check_repr(ift.RegriddingOperator(*args)) `````` Reimar H Leike committed Jan 23, 2019 190 191 `````` `````` Philipp Arras committed Jul 31, 2019 192 193 194 195 196 ``````@pmp('fdomain', [ ift.DomainTuple.make((ift.RGSpace( (3, 5, 4)), ift.RGSpace((16,), distances=(7.,))),), ift.DomainTuple.make(ift.HPSpace(12),) ]) `````` Reimar H Leike committed Jan 23, 2019 197 198 199 200 201 202 ``````@pmp('domain', [ ift.DomainTuple.make((ift.RGSpace((2,)), ift.GLSpace(10)),), ift.DomainTuple.make(ift.RGSpace((10, 12), distances=(0.1, 1.)),) ]) def testOuter(fdomain, domain): f = ift.from_random('normal', fdomain) `````` Martin Reinecke committed Jan 24, 2019 203 `````` _check_repr(ift.OuterProduct(f, domain)) `````` Reimar H Leike committed Jan 23, 2019 204 205 206 207 208 `````` @pmp('sp', _h_spaces + _p_spaces + _pow_spaces) @pmp('seed', [12, 3]) def testValueInserter(sp, seed): `````` Martin Reinecke committed Mar 20, 2020 209 `````` ift.random.push_sseq_from_seed(seed) `````` Reimar H Leike committed Jan 23, 2019 210 211 212 213 214 `````` ind = [] for ss in sp.shape: if ss == 1: ind.append(0) else: `````` Martin Reinecke committed Mar 20, 2020 215 `````` ind.append(int(ift.random.current_rng().integers(0, ss - 1))) `````` Martin Reinecke committed Jan 24, 2019 216 `````` _check_repr(ift.ValueInserter(sp, ind)) `````` Martin Reinecke committed Mar 20, 2020 217 `` ift.random.pop_sseq()``