Exemple #1
0
def test_gauss(minimizer):
    space = ift.UnstructuredDomain((1,))
    starting_point = ift.Field.full(space, 3.)

    class ExpEnergy(ift.Energy):
        def __init__(self, position):
            super(ExpEnergy, self).__init__(position)

        @property
        def value(self):
            x = self.position.to_global_data()[0]
            return -np.exp(-(x**2))

        @property
        def gradient(self):
            x = self.position.to_global_data()[0]
            return ift.Field.full(self.position.domain, 2*x*np.exp(-(x**2)))

        def apply_metric(self, x):
            p = self.position.to_global_data()[0]
            v = (2 - 4*p*p)*np.exp(-p**2)
            return ift.DiagonalOperator(
                ift.Field.full(self.position.domain, v))(x)

    try:
        minimizer = eval(minimizer)
        energy = ExpEnergy(position=starting_point)

        (energy, convergence) = minimizer(energy)
    except NotImplementedError:
        raise SkipTest

    assert_equal(convergence, IC.CONVERGED)
    assert_allclose(energy.position.local_data, 0., atol=1e-3)
Exemple #2
0
    def __init__(self, target):
        self._target = ift.DomainTuple.make(target)
        self._domain = ift.DomainTuple.make(ift.UnstructuredDomain((2, )))
        self._capability = self.TIMES | self.ADJOINT_TIMES
        pos = self.target[0].get_k_length_array().val

        self._pos = pos
Exemple #3
0
    def __init__(self, domain, uv):
        from pynfft.nfft import NFFT
        npix = domain.shape[0]
        assert npix == domain.shape[1]
        assert len(domain.shape) == 2
        assert type(npix) == int, "npix must be integer"
        assert npix > 0 and (npix %
                             2) == 0, "npix must be an even, positive integer"
        assert isinstance(uv, np.ndarray), "uv must be a Numpy array"
        assert uv.dtype == np.float64, "uv must be an array of float64"
        assert uv.ndim == 2, "uv must be a 2D array"
        assert uv.shape[0] > 0, "at least one point needed"
        assert uv.shape[1] == 2, "the second dimension of uv must be 2"
        assert np.all(uv >= -0.5) and np.all(uv <= 0.5),\
            "all coordinates must lie between -0.5 and 0.5"

        self._domain = ift.DomainTuple.make(domain)
        self._target = ift.DomainTuple.make(ift.UnstructuredDomain(
            uv.shape[0]))
        self._capability = self.TIMES | self.ADJOINT_TIMES

        self.npt = uv.shape[0]
        self.plan = NFFT(self.domain.shape, self.npt, m=6)
        self.plan.x = uv
        self.plan.precompute()
Exemple #4
0
def test_rosenbrock(minimizer):
    try:
        from scipy.optimize import rosen, rosen_der, rosen_hess_prod
    except ImportError:
        raise SkipTest
    np.random.seed(42)
    space = ift.DomainTuple.make(ift.UnstructuredDomain((2,)))
    starting_point = ift.Field.from_random('normal', domain=space)*10

    class RBEnergy(ift.Energy):
        def __init__(self, position):
            super(RBEnergy, self).__init__(position)

        @property
        def value(self):
            return rosen(self._position.to_global_data_rw())

        @property
        def gradient(self):
            inp = self._position.to_global_data_rw()
            out = ift.Field.from_global_data(space, rosen_der(inp))
            return out

        @property
        def metric(self):
            class RBCurv(ift.EndomorphicOperator):
                def __init__(self, loc):
                    self._loc = loc.to_global_data_rw()
                    self._capability = self.TIMES
                    self._domain = space

                def apply(self, x, mode):
                    self._check_input(x, mode)
                    inp = x.to_global_data_rw()
                    out = ift.Field.from_global_data(
                        space, rosen_hess_prod(self._loc.copy(), inp))
                    return out

            t1 = ift.GradientNormController(
                tol_abs_gradnorm=1e-5, iteration_limit=1000)
            return ift.InversionEnabler(RBCurv(self._position), t1)

        def apply_metric(self, x):
            inp = x.to_global_data_rw()
            pos = self._position.to_global_data_rw()
            return ift.Field.from_global_data(space, rosen_hess_prod(pos, inp))

    try:
        minimizer = eval(minimizer)
        energy = RBEnergy(position=starting_point)

        (energy, convergence) = minimizer(energy)
    except NotImplementedError:
        raise SkipTest

    assert_equal(convergence, IC.CONVERGED)
    assert_allclose(energy.position.local_data, 1., rtol=1e-3, atol=1e-3)
def test_value_inserter(sp, seed):
    np.random.seed(seed)
    ind = tuple([np.random.randint(0, ss - 1) for ss in sp.shape])
    op = ift.ValueInserter(sp, ind)
    f = ift.from_random('normal', ift.UnstructuredDomain((1,)))
    inp = f.to_global_data()[0]
    ret = op(f).to_global_data()
    assert_(ret[ind] == inp)
    assert_(np.sum(ret) == inp)
Exemple #6
0
 def __init__(self, target):
     self._domain = ift.makeDomain(ift.UnstructuredDomain(1))
     self._target = ift.makeDomain(target)
     self._capability = self.TIMES | self.ADJOINT_TIMES
Exemple #7
0
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]
    _check_repr(ift.FieldZeroPadder(dom, newshape, space, central))
Exemple #8
0
def testSymmetrizingOperator(space, dtype):
    dom = (ift.LogRGSpace(10, [2.], [1.]), ift.UnstructuredDomain(13),
           ift.LogRGSpace((5, 27), [1., 2.7], [0., 4.]), ift.HPSpace(4))
    _check_repr(ift.SymmetrizingOperator(dom, space))
Exemple #9
0
def testDomainTupleFieldInserter():
    target = ift.DomainTuple.make(
        (ift.UnstructuredDomain([3, 2]), ift.UnstructuredDomain(7),
         ift.RGSpace([4, 22])))
    _check_repr(ift.DomainTupleFieldInserter(target, 1, (5, )))
Exemple #10
0
    _check_repr(ift.FieldZeroPadder(dom, newshape, space, central))


@pmp('args',
     [(ift.RGSpace(10, harmonic=True), 4, 0),
      (ift.RGSpace((24, 31), distances=(0.4, 2.34), harmonic=True), (4, 3), 0),
      ((ift.HPSpace(4), ift.RGSpace(27, distances=0.3, harmonic=True)),
       (10, ), 1),
      (ift.PowerSpace(ift.RGSpace(10, distances=0.3, harmonic=True)), 6, 0)])
def testExpTransform(args, dtype):
    _check_repr(ift.ExpTransform(args[0], args[1], args[2]))


@pmp('args',
     [(ift.LogRGSpace([10, 17], [2., 3.], [1., 0.]), 0),
      ((ift.LogRGSpace(10, [2.], [1.]), ift.UnstructuredDomain(13)), 0),
      ((ift.UnstructuredDomain(13), ift.LogRGSpace(17, [3.], [.7])), 1)])
def testQHTOperator(args):
    dtype = np.float64
    tgt = ift.DomainTuple.make(args[0])
    _check_repr(ift.QHTOperator(tgt, args[1]))


@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):
    _check_repr(ift.RegriddingOperator(*args))
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]
    op = ift.FieldZeroPadder(dom, newshape, space, central)
    ift.extra.consistency_check(op, dtype, dtype)
def testSymmetrizingOperator(space, dtype):
    dom = (ift.LogRGSpace(10, [2.], [1.]), ift.UnstructuredDomain(13),
           ift.LogRGSpace((5, 27), [1., 2.7], [0., 4.]), ift.HPSpace(4))
    op = ift.SymmetrizingOperator(dom, space)
    ift.extra.consistency_check(op, dtype, dtype)
def testDomainTupleFieldInserter():
    target = ift.DomainTuple.make(
        (ift.UnstructuredDomain([3, 2]), ift.UnstructuredDomain(7),
         ift.RGSpace([4, 22])))
    op = ift.DomainTupleFieldInserter(target, 1, (5, ))
    ift.extra.consistency_check(op)