Example #1
0
def test_zero1():
    z = ZeroOperator()
    o = Operator(shapein=3, shapeout=6)
    zo = z(o)
    assert_is_instance(zo, ZeroOperator)
    assert_equal(zo.shapein, o.shapein)
    assert_is_none(zo.shapeout)
Example #2
0
def test_zero2():
    z = ZeroOperator(shapein=3, shapeout=6)
    o = Operator()
    zo = z(o)
    assert_is_instance(zo, ZeroOperator)
    assert_is_none(zo.shapein, 'in')
    assert_equal(zo.shapeout, z.shapeout, 'out')
Example #3
0
 def func(cls, none):
     with rule_manager(none=none):
         op = cls([op1, op2])
         if none:
             assert_is_instance(op, cls)
         else:
             assert_is_instance(op, DiagonalOperator)
Example #4
0
def test_zero3():
    z = ZeroOperator(shapein=3, shapeout=6)
    o = Operator(flags='square')
    zo = z*o
    assert_is_instance(zo, ZeroOperator)
    assert_equal(zo.shapein, z.shapein, 'in')
    assert_equal(zo.shapeout, z.shapeout, 'out')
Example #5
0
def assert_quantity(q, m, u):
    assert_is_instance(q, Quantity)
    assert_array_equal(q.magnitude, m)
    if isinstance(u, dict):
        assert_equal(q._unit, u)
    else:
        assert_equal(q.unit, u)
Example #6
0
def test_equ2gal():
    equ2gal = CartesianEquatorial2GalacticOperator()
    gal2equ = CartesianGalactic2EquatorialOperator()

    assert equ2gal.I is equ2gal.T
    assert gal2equ.I is gal2equ.T
    assert_same(equ2gal.todense(shapein=3), equ2gal.data)
    assert_same(gal2equ.todense(shapein=3), equ2gal.data.T)

    shapes = (3, (2, 3), (4, 2, 3))

    def func(op, shape):
        vec = np.arange(product(shape)).reshape(shape)
        vec_ = vec.reshape((-1, 3))
        expected = np.empty(shape)
        expected_ = expected.reshape((-1, 3))
        for i in range(expected.size // 3):
            expected_[i] = op(vec_[i])
        actual = op(vec)
        assert_same(actual, expected)

    for op in (equ2gal, gal2equ):
        for shape in shapes:
            yield func, op, shape

    assert_is_instance(equ2gal(gal2equ), IdentityOperator)
    assert_is_instance(gal2equ(equ2gal), IdentityOperator)
Example #7
0
def test_equ2gal():
    equ2gal = CartesianEquatorial2GalacticOperator()
    gal2equ = CartesianGalactic2EquatorialOperator()

    assert equ2gal.I is equ2gal.T
    assert gal2equ.I is gal2equ.T
    assert_same(equ2gal.todense(shapein=3), equ2gal.data)
    assert_same(gal2equ.todense(shapein=3), equ2gal.data.T)

    shapes = (3, (2, 3), (4, 2, 3))

    def func(op, shape):
        vec = np.arange(product(shape)).reshape(shape)
        vec_ = vec.reshape((-1, 3))
        expected = np.empty(shape)
        expected_ = expected.reshape((-1, 3))
        for i in range(expected.size // 3):
            expected_[i] = op(vec_[i])
        actual = op(vec)
        assert_same(actual, expected)

    for op in (equ2gal, gal2equ):
        for shape in shapes:
            yield func, op, shape

    assert_is_instance(equ2gal(gal2equ), IdentityOperator)
    assert_is_instance(gal2equ(equ2gal), IdentityOperator)
Example #8
0
 def func(s1, s2, s3, o1, o2, ref):
         rule = BinaryRule(s1 + ',' + s2, s3)
         result = rule(o1, o2)
         assert_is_not_none(result)
         assert_is_instance(result, Operator)
         if s3 == '1':
             assert_is_instance(result, IdentityOperator)
             return
         o3 = eval('ref.'+s3) if s3 != '.' else ref
         assert_is(result, o3)
Example #9
0
 def func(k1, k2):
     c1 = ConvolutionOperator(k1, shape)
     c2 = ConvolutionOperator(k2, shape)
     c = c1 * c2
     if k1.dtype.kind == 'f' and k2.dtype.kind == 'f':
         assert_is_instance(c, _FFTWRealConvolutionOperator)
     else:
         assert_is_instance(c, CompositionOperator)
         assert_eq(len(c.operands), 3)
     assert np.allclose(c(image.real), ref)
Example #10
0
    def func(opout, opin, idin):
        if opin is not None and idin is not None and opin != idin:
            return
        p = Op(shapeout=opout, shapein=opin) * IdentityOperator(shapein=idin)

        if idin is None:
            idin = opin
        assert_is_instance(p, Op)
        assert_eq(p.shapein, idin)
        assert_eq(p.shapeout, opout)
Example #11
0
 def func(s1, s2, sm1, sm2):
     shapein = broadcast_shapes(s1 + sm2[1:], s2 + sm2[1:])
     data1 = np.arange(product(s1 + sm1)).reshape(s1 + sm1)
     data2 = np.arange(product(s2 + sm2)).reshape(s2 + sm2)
     op1 = DenseBlockDiagonalOperator(data1)
     op2 = DenseBlockDiagonalOperator(data2)
     comp1 = op1 * op2
     assert_is_instance(comp1, DenseBlockDiagonalOperator)
     with rule_manager(none=True):
         comp2 = op1 * op2
     assert_equal(comp1.todense(shapein), comp2.todense(shapein))
Example #12
0
 def func(op1, op2, pin1, pout2, cls):
     op = op1 * op2
     assert_is_instance(op, cls)
     if not isinstance(op, BlockOperator):
         return
     pout = None if isinstance(op, BlockRowOperator) else \
            merge_none(pin1, pout2)
     pin = None if isinstance(op, BlockColumnOperator) else \
           merge_none(pin1, pout2)
     assert pout == op.partitionout
     assert pin == op.partitionin
Example #13
0
def test_zero5():
    z = ZeroOperator()
    o = Operator(shapein=3, shapeout=6, flags='linear')
    zo = z*o
    oz = o*z
    assert_is_instance(zo, ZeroOperator, 'zo')
    assert_equal(zo.shapein, o.shapein, 'zo in')
    assert_is_none(zo.shapeout, 'zo out')
    assert_is_instance(oz, ZeroOperator, 'oz')
    assert_is_none(oz.shapein, 'oz, in')
    assert_equal(oz.shapeout, o.shapeout, 'oz, out')
Example #14
0
 def func(op1, op2, pin1, pout2, cls):
     op = op1 * op2
     assert_is_instance(op, cls)
     if not isinstance(op, BlockOperator):
         return
     pout = None if isinstance(op, BlockRowOperator) else \
            merge_none(pin1, pout2)
     pin = None if isinstance(op, BlockColumnOperator) else \
           merge_none(pin1, pout2)
     assert pout == op.partitionout
     assert pin == op.partitionin
Example #15
0
def test_diagonal_numexpr2():
    d1 = DiagonalNumexprOperator([1, 2, 3], '(data+1)*3',
                                 broadcast='rightward')
    d2 = DiagonalNumexprOperator([3, 2, 1], '(data+2)*2')
    d = d1 * d2
    assert_is_instance(d, DiagonalOperator)
    assert_eq(d.broadcast, 'disabled')
    assert_eq(d.data, [60, 72, 72])
    c = BlockColumnOperator(3*[IdentityOutplaceOperator()], new_axisout=0)
    v = [1, 2]
    assert_inplace_outplace(d1*c, v, d1(c(v)))
Example #16
0
def test_diagonal_numexpr2():
    d1 = DiagonalNumexprOperator([1, 2, 3],
                                 '(data+1)*3',
                                 broadcast='rightward')
    d2 = DiagonalNumexprOperator([3, 2, 1], '(data+2)*2')
    d = d1 * d2
    assert_is_instance(d, DiagonalOperator)
    assert_eq(d.broadcast, 'disabled')
    assert_eq(d.data, [60, 72, 72])
    c = BlockColumnOperator(3 * [IdentityOutplaceOperator()], new_axisout=0)
    v = [1, 2]
    assert_inplace_outplace(d1 * c, v, d1(c(v)))
Example #17
0
 def func(a, b, operation, apply_rule):
     p = operation([a, b])
     if not apply_rule:
         if isinstance(a, IdentityOperator) or \
            isinstance(b, IdentityOperator):
             return
         assert not isinstance(p, BlockDiagonalOperator)
         return
     assert_is_instance(p, BlockDiagonalOperator)
     with rule_manager(none=True):
         q = operation([a, b])
     assert_equal(p.todense(), q.todense())
Example #18
0
def test_hard_thresholding():
    x = [-1., -0.2, -0.1, 0, 0.2, 0.1, 2, 3]
    lbda = 0.2
    H = HardThresholdingOperator(lbda)
    expected = [-1, 0, 0, 0, 0, 0, 2, 3]
    assert_equal(H(x), expected)
    x = np.array(x)
    H(x, x)
    assert_equal(x, expected)
    lbda2 = [0.3, 0.1, 2]
    shape = np.asarray(lbda2).shape
    G = HardThresholdingOperator(lbda2)
    assert_equal(G.shapein, shape)
    K = G(H)
    assert_is_instance(K, HardThresholdingOperator)
    assert_equal(K.a, np.maximum(lbda, lbda2))
    assert_equal(K.shapein, shape)
    K = H(G)
    assert_is_instance(K, HardThresholdingOperator)
    assert_equal(K.a, np.maximum(lbda, lbda2))
    assert_equal(K.shapein, shape)

    H = HardThresholdingOperator([0, 0])
    assert_is_instance(H, IdentityOperator)
    assert_equal(H.shapein, (2, ))

    H = HardThresholdingOperator(0)
    assert_is_instance(H, IdentityOperator)
    assert H.flags.square
    assert_equal(H.flags.shape_input, 'implicit')
    assert_equal(H.flags.shape_output, 'implicit')
Example #19
0
def test_hard_thresholding():
    x = [-1., -0.2, -0.1, 0, 0.2, 0.1, 2, 3]
    lbda = 0.2
    H = HardThresholdingOperator(lbda)
    expected = [-1, 0, 0, 0, 0, 0, 2, 3]
    assert_equal(H(x), expected)
    x = np.array(x)
    H(x, x)
    assert_equal(x, expected)
    lbda2 = [0.3, 0.1, 2]
    shape = np.asarray(lbda2).shape
    G = HardThresholdingOperator(lbda2)
    assert_equal(G.shapein, shape)
    K = G(H)
    assert_is_instance(K, HardThresholdingOperator)
    assert_equal(K.a, np.maximum(lbda, lbda2))
    assert_equal(K.shapein, shape)
    K = H(G)
    assert_is_instance(K, HardThresholdingOperator)
    assert_equal(K.a, np.maximum(lbda, lbda2))
    assert_equal(K.shapein, shape)

    H = HardThresholdingOperator([0, 0])
    assert_is_instance(H, IdentityOperator)
    assert_equal(H.shapein, (2,))

    H = HardThresholdingOperator(0)
    assert_is_instance(H, IdentityOperator)
    assert H.flags.square
    assert_equal(H.flags.shape_input, 'implicit')
    assert_equal(H.flags.shape_output, 'implicit')
Example #20
0
 def func(op1, op2):
     op = op1(op2)
     assert_is_instance(op, ZeroOperator)
     attr = {}
     attr.update(op2.attrout)
     attr.update(op1.attrout)
     assert_equal(op.attrout, attr)
     x = np.ones(3)
     y = ndarraywrap(4)
     op(x, y)
     y2_tmp = np.empty(4)
     y2 = np.empty(4)
     op2(x, y2_tmp)
     op1(y2_tmp, y2)
     assert_equal(y, y2)
     assert_is_instance(y, op1.classout)
Example #21
0
 def func(c1, t1, c2, t2):
     op2 = ConstantOperator(c2, broadcast=t2)
     op = op1 + op2
     if set((op1.broadcast, op2.broadcast)) != \
        set(('rightward', 'leftward')):
         assert_is_instance(op, ConstantOperator)
     v = np.zeros((2, 3))
     op(np.nan, v)
     z = np.zeros((2, 3))
     if t1 == 'rightward':
         z.T[...] += c1.T
     else:
         z[...] += c1
     if t2 == 'rightward':
         z.T[...] += c2.T
     else:
         z[...] += c2
     assert_eq(v, z)
Example #22
0
 def func(op, op1, op2, op_ref):
     assert_is_instance(op, type(op_ref))
     attr = {}
     attr.update(op2.attrout)
     attr.update(op1.attrout)
     assert_equal(op.attrout, attr)
     x = np.ones(op.shapein if op.shapein is not None else 3)
     y = ndarraywrap(4)
     op(x, y)
     if op1.flags.shape_output == 'unconstrained' or \
        op2.flags.shape_output == 'unconstrained':
         y2_tmp = np.empty(3 if isinstance(op2, IdentityOperator) else 4)
         y2 = np.empty(4)
         op2(x, y2_tmp)
         op1(y2_tmp, y2)
     else:
         y2 = op1(op2(x))
     assert_equal(y, y2)
     assert_is_instance(y, op1.classout)
Example #23
0
 def func(s, l):
     layout = LayoutGridSquares(shape, s, selection=selection, origin=l)
     if (not isinstance(s, Quantity) or s.unit == '') and \
        isinstance(l, Quantity) and l.unit == 'm':
         expected = np.array((1e-3, 1e-3))
     else:
         expected = np.array((1, 1))
     actual = np.mean(layout.center, axis=0).view(np.ndarray)
     assert_same(actual, expected, rtol=1000)
     vals = (layout.center, layout.all.center, layout.vertex,
             layout.all.vertex)
     unit = getattr(s, 'unit', '') or getattr(l, 'unit', '')
     if unit:
         for val in vals:
             assert_is_instance(val, Quantity)
             assert_equal(val.unit, unit)
     else:
         for val in vals:
             assert_is_type(val, np.ndarray)
Example #24
0
 def func(s, l):
     layout = LayoutGridSquares(shape, s, selection=selection,
                                origin=l)
     if (not isinstance(s, Quantity) or s.unit == '') and \
        isinstance(l, Quantity) and l.unit == 'm':
         expected = np.array((1e-3, 1e-3))
     else:
         expected = np.array((1, 1))
     actual = np.mean(layout.center, axis=0).view(np.ndarray)
     assert_same(actual, expected, rtol=1000)
     vals = (layout.center, layout.all.center, layout.vertex,
             layout.all.vertex)
     unit = getattr(s, 'unit', '') or getattr(l, 'unit', '')
     if unit:
         for val in vals:
             assert_is_instance(val, Quantity)
             assert_equal(val.unit, unit)
     else:
         for val in vals:
             assert_is_type(val, np.ndarray)
def test_packing():

    p = PackOperator([False, True, True, False])
    assert all_eq(p([1,2,3,4]), [1,4])
    assert all_eq(p.T([1,4]), [1,0,0,4])

    u = UnpackOperator([False, True, True, False])
    assert all_eq(u([1,4]), [1,0,0,4])
    assert all_eq(u.T([1,2,3,4]), [1,4])

    pdense = p.todense()
    udense = u.todense()
    assert all_eq(pdense, p.todense(inplace=True))
    assert all_eq(udense, u.todense(inplace=True))
    assert all_eq(pdense, udense.T)

    assert_is_instance(p*u, IdentityOperator)
    assert_is_instance(u*p, MaskOperator)
    m = u * p
    assert all_eq(np.dot(udense, pdense), m.todense())
Example #26
0
def test_soft_thresholding():
    x = [-1., -0.2, -0.1, 0, 0.1, 0.2, 2, 3]
    lbda = np.array(0.2)
    S = SoftThresholdingOperator(lbda)
    expected = [-1, 0, 0, 0, 0, 0, 2, 3] - lbda * [-1, 0, 0, 0, 0, 0, 1, 1]
    assert_equal(S(x), expected)
    x = np.array(x)
    S(x, x)
    assert_equal(x, expected)
    lbda2 = [0.3, 0.1, 2]
    shape = np.asarray(lbda2).shape
    T = SoftThresholdingOperator(lbda2)
    assert_equal(T.shapein, shape)

    S = SoftThresholdingOperator([0, 0])
    assert_is_instance(S, IdentityOperator)
    assert_equal(S.shapein, (2, ))

    S = SoftThresholdingOperator(0)
    assert_is_instance(S, IdentityOperator)
    assert S.flags.square
    assert_equal(S.flags.shape_input, 'implicit')
    assert_equal(S.flags.shape_output, 'implicit')
Example #27
0
def test_soft_thresholding():
    x = [-1., -0.2, -0.1, 0, 0.1, 0.2, 2, 3]
    lbda = np.array(0.2)
    S = SoftThresholdingOperator(lbda)
    expected = [-1, 0, 0, 0, 0, 0, 2, 3] - lbda * [-1, 0, 0, 0, 0, 0, 1, 1]
    assert_equal(S(x), expected)
    x = np.array(x)
    S(x, x)
    assert_equal(x, expected)
    lbda2 = [0.3, 0.1, 2]
    shape = np.asarray(lbda2).shape
    T = SoftThresholdingOperator(lbda2)
    assert_equal(T.shapein, shape)

    S = SoftThresholdingOperator([0, 0])
    assert_is_instance(S, IdentityOperator)
    assert_equal(S.shapein, (2,))

    S = SoftThresholdingOperator(0)
    assert_is_instance(S, IdentityOperator)
    assert S.flags.square
    assert_equal(S.flags.shape_input, 'implicit')
    assert_equal(S.flags.shape_output, 'implicit')
Example #28
0
 def func(s, r):
     layout = LayoutGrid(shape, s, radius=r, selection=selection)
     if (not isinstance(s, Quantity) or s.unit == '') and \
        isinstance(r, Quantity) and r.unit == 'm':
         expectedval = 0.4e-3
     else:
         expectedval = 0.4
     expected = np.empty(shape)
     expected[...] = expectedval
     expected[0, 1] = np.nan
     assert_same(layout.radius, expectedval, broadcasting=True)
     assert_same(layout.all.radius, expected)
     vals = (layout.radius, layout.all.radius, layout.center,
             layout.all.center)
     unit = getattr(s, 'unit', '') or getattr(r, 'unit', '')
     if unit:
         for val in vals:
             assert_is_instance(val, Quantity)
             assert_equal(val.unit, unit)
     else:
         r = np.asanyarray(r)
         expecteds = (type(r), type(r), np.ndarray, np.ndarray)
         for val, e in zip(vals, expecteds):
             assert_is_type(val, e)
Example #29
0
def test_homothety_operator():
    s = HomothetyOperator(1)
    assert s.C is s.T is s.H is s.I is s

    s = HomothetyOperator(-1)
    assert s.C is s.T is s.H is s.I is s

    s = HomothetyOperator(2.)
    assert s.C is s.T is s.H is s
    assert_is_not(s.I, s)

    def func(o):
        assert_is_instance(o, HomothetyOperator)
    for o in (s.I, s.I.C, s.I.T, s.I.H, s.I.I):
        yield func, o

    s = HomothetyOperator(complex(1, 1))
    assert_is(s.T, s)
    assert_is(s.H, s.C)
    assert_not_in(s.I, (s, s.C))
    assert_not_in(s.I.C, (s, s.C))
    assert_is_instance(s.C, HomothetyOperator)
    for o in (s.I, s.I.C, s.I.T, s.I.H, s.I.I):
        yield func, o
Example #30
0
 def func(s, r):
     layout = LayoutGrid(shape, s, radius=r, selection=selection)
     if (not isinstance(s, Quantity) or s.unit == '') and \
        isinstance(r, Quantity) and r.unit == 'm':
         expectedval = 0.4e-3
     else:
         expectedval = 0.4
     expected = np.empty(shape)
     expected[...] = expectedval
     expected[0, 1] = np.nan
     assert_same(layout.radius, expectedval, broadcasting=True)
     assert_same(layout.all.radius, expected)
     vals = (layout.radius, layout.all.radius, layout.center,
             layout.all.center)
     unit = getattr(s, 'unit', '') or getattr(r, 'unit', '')
     if unit:
         for val in vals:
             assert_is_instance(val, Quantity)
             assert_equal(val.unit, unit)
     else:
         r = np.asanyarray(r)
         expecteds = (type(r), type(r), np.ndarray, np.ndarray)
         for val, e in zip(vals, expecteds):
             assert_is_type(val, e)
Example #31
0
 def func2(cls):
     op = cls([2, PowerLawOperator(1, nu, nu0)])
     assert_is_instance(op, PowerLawOperator)
Example #32
0
 def func(c1, c2):
     c = c1 + c2
     assert_is_instance(c, _FFTWRealConvolutionOperator)
     assert_same(c1.todense() + c2.todense(), c.todense(), atol=5)
Example #33
0
def test_zero4():
    z = ZeroOperator()
    o = Operator(flags='linear')
    assert_is_instance(z*o, ZeroOperator)
    assert_is_instance(o*z, ZeroOperator)
Example #34
0
 def func(s, p):
     r = UnaryRule(s, p)
     if p == '.':
         assert_is(r(op1), op1)
     else:
         assert_is_instance(r(op1), IdentityOperator)
Example #35
0
 def func(o):
     assert_is_instance(o, HomothetyOperator)
Example #36
0
 def func2(cls):
     op = cls([2, PowerLawOperator(1, nu, nu0)])
     assert_is_instance(op, PowerLawOperator)
Example #37
0
 def func(n, firstrow):
     s = SymmetricBandToeplitzOperator(n, firstrow)
     if firstrow == [1] or firstrow == [[2], [1]]:
         assert_is_instance(s, DiagonalOperator)
     assert_same(s.todense(), totoeplitz(n, firstrow).todense(), atol=1)
Example #38
0
 def func(n, firstrow):
     s = SymmetricBandToeplitzOperator(n, firstrow)
     if firstrow == [1] or firstrow == [[2], [1]]:
         assert_is_instance(s, DiagonalOperator)
     assert_same(s.todense(), totoeplitz(n, firstrow).todense(), atol=1)