Example #1
0
    def func(valid, x, shape, broadcast):
        p = PackOperator(valid, broadcast=broadcast)
        masking = MaskOperator(~valid, broadcast=broadcast)
        if broadcast == 'leftward':
            x_ = np.empty(shape + x.shape)
            x_[...] = x
            expected_ = np.empty(shape + (expected.size,))
            expected_[...] = expected
        else:
            x_ = np.empty(x.shape + shape)
            x_.reshape((x.size, -1))[...] = x.ravel()[..., None]
            expected_ = np.empty((expected.size,) + shape)
            expected_.reshape((expected.size, -1))[...] = expected[..., None]

        if broadcast == 'disabled' and shape != ():
            assert_raises(ValueError, p, x_)
            return
        assert_equal(p(x_), expected_)

        assert_is_type(p.T, UnpackOperator)
        assert_equal(p.T.broadcast, p.broadcast)
        assert_equal(p.T(expected_), masking(x_))

        u = UnpackOperator(valid, broadcast=broadcast)
        assert_is_type(u.T, PackOperator)
        assert_equal(u.T.broadcast, u.broadcast)
        assert_equal(u(expected_), masking(x_))
        assert_equal(u.T(x_), expected_)
Example #2
0
 def func(c1, c2):
     op1 = Cartesian2SphericalOperator(c1)
     op2 = Spherical2CartesianOperator(c2)
     op = op1(op2)
     if c1 == c2:
         assert_is_type(op, IdentityOperator)
     else:
         assert_is_type(op, CompositionOperator)
Example #3
0
 def func(c1, c2):
     op1 = Cartesian2SphericalOperator(c1)
     op2 = Spherical2CartesianOperator(c2)
     op = op1(op2)
     if c1 == c2:
         assert_is_type(op, IdentityOperator)
     else:
         assert_is_type(op, CompositionOperator)
Example #4
0
 def func(op, cls1, cls2, cls3):
     operation = CompositionOperator \
                 if op.flags.linear else MultiplicationOperator
     op1 = cls1(3*[op], axisin=0)
     op2 = cls2(3*[op], axisout=0)
     result = op1 * op2
     assert_is_type(result, cls3)
     assert_is_type(result.operands[0], operation)
Example #5
0
 def func(op, cls1, cls2, cls3):
     operation = CompositionOperator \
                 if op.flags.linear else MultiplicationOperator
     op1 = cls1(3 * [op], axisin=0)
     op2 = cls2(3 * [op], axisout=0)
     result = op1 * op2
     assert_is_type(result, cls3)
     assert_is_type(result.operands[0], operation)
Example #6
0
def test_dense_rule_homothety():
    m = np.array([[1, 2], [3, 4], [5, 6]])
    d = HomothetyOperator(2) * DenseOperator(m)
    assert_is_type(d, DenseOperator)
    assert_same(d.data, m * 2)
    d = HomothetyOperator(2j) * DenseOperator(m)
    assert_is_type(d, DenseOperator)
    assert_same(d.data, m * 2j)
    assert_equal(d.dtype, complex)
Example #7
0
 def func(c, o, r):
     op = MultiplicationOperator([c, o])
     assert_eq(op(v), c.data*o(v))
     assert_is_type(op, r[0])
     if type(op) is CompositionOperator:
         op = op.operands[0]
         r = r[1]
         assert_is_type(op, r[0])
     assert_eq, op.data, r[1]
Example #8
0
 def func(op_, id_):
     op = op_(id_)
     assert_is_type(op, type(op_))
     attr = {}
     assert_is(op.classout, op_.classout)
     attr.update(id_.attrout)
     attr.update(op_.attrout)
     assert_eq(op.attrout, attr)
     assert_eq(op.flags.linear, op_.flags.linear)
     assert_eq(op.flags.contiguous_input, op_.flags.contiguous_input)
Example #9
0
 def func2(itype, ftype):
     mat = get_mat(itype, ftype)
     op = SparseOperator(mat, shapein=4)
     todense = op.todense()
     pTp = op.T * op
     if (itype, ftype) not in ((np.int32, np.float32),
                               (np.int32, np.float64),
                               (np.int64, np.float32),
                               (np.int64, np.float64)):
         assert_is_type(pTp, CompositionOperator)
         return
     assert_is_type(pTp, DiagonalOperator)
     assert_same(pTp.todense(), np.dot(todense.T, todense))
Example #10
0
def test_beams():
    assert_is_type(q.primary_beam, BeamGaussian)
    assert_is_type(q.secondary_beam, BeamGaussian)
    a = QubicAcquisition(150, s, primary_beam=BeamUniformHalfSpace(),
                         secondary_beam=BeamUniformHalfSpace())
    assert_is_type(a.instrument.primary_beam, BeamUniformHalfSpace)
    assert_is_type(a.instrument.secondary_beam, BeamUniformHalfSpace)
Example #11
0
def test_healpix_spherical_rules():
    def func(op, nside, c, nest):
        op2 = Spherical2HealpixOperator(nside, c, nest=nest)
        if c == op.convention and nside == op.nside and nest == op.nest:
            assert_is_type(op(op2), IdentityOperator)
        else:
            assert_is_type(op(op2), CompositionOperator)
    for c in Healpix2SphericalOperator.CONVENTIONS:
        op = Healpix2SphericalOperator(NSIDE, c, nest=True)
        assert_is_type(op.I, Spherical2HealpixOperator)
        assert_equal(op.I.convention, op.convention)
        for nside in NSIDE, 2*NSIDE:
            for c2 in Healpix2SphericalOperator.CONVENTIONS:
                for nest in False, True:
                    yield func, op, nside, c2, nest
Example #12
0
 def func2(itype, ftype):
     array = np.recarray((6, 1), dtype=[('index', itype), ('r11', ftype),
                                        ('r22', ftype), ('r32', ftype)])
     dense = np.zeros((6*3, 4*3), dtype=ftype)
     fill(array, dense, index1, value1, angle1, 0)
     op = SparseOperator(FSRRotation3dMatrix(dense.shape, data=array))
     pTp = op.T * op
     if (itype, ftype) not in ((np.int32, np.float32),
                               (np.int32, np.float64),
                               (np.int64, np.float32),
                               (np.int64, np.float64)):
         assert_is_type(pTp, CompositionOperator)
         return
     assert_is_type(pTp, DiagonalOperator)
     assert_same(pTp.todense(), np.dot(dense.T, dense), atol=1)
Example #13
0
def test_cartesian_spherical_rules():
    def func(c1, c2):
        op1 = Cartesian2SphericalOperator(c1)
        op2 = Spherical2CartesianOperator(c2)
        op = op1(op2)
        if c1 == c2:
            assert_is_type(op, IdentityOperator)
        else:
            assert_is_type(op, CompositionOperator)
    for c1 in 'zenith,azimuth', 'azimuth,elevation':
        op = Cartesian2SphericalOperator(c1)
        assert_is_type(op.I, Spherical2CartesianOperator)
        assert_equal(op.convention, c1)
        for c2 in 'zenith,azimuth', 'azimuth,elevation':
            yield func, c1, c2
Example #14
0
def test_sparse_operator():
    assert_raises(TypeError, SparseOperator, np.zeros((10, 3)))
    sp = SparseOperator(scipy.sparse.csc_matrix((4, 6)))
    assert_is_type(sp, pyoperators.linear.SparseOperator)

    data = FSRMatrix((10, 3), ncolmax=1)
    assert_raises(ValueError, SparseOperator, data, shapein=4)
    assert_raises(ValueError, SparseOperator, data, shapeout=11)

    data = FSRRotation2dMatrix((12, 6), ncolmax=1)
    assert_raises(ValueError, SparseOperator, data, shapein=(5, 2))
    assert_raises(ValueError, SparseOperator, data, shapeout=(7,))

    data = FSRRotation3dMatrix((12, 6), ncolmax=1)
    assert_raises(ValueError, SparseOperator, data, shapein=(5, 3))
    assert_raises(ValueError, SparseOperator, data, shapeout=(7,))
Example #15
0
def test_cartesian_spherical_rules():
    def func(c1, c2):
        op1 = Cartesian2SphericalOperator(c1)
        op2 = Spherical2CartesianOperator(c2)
        op = op1(op2)
        if c1 == c2:
            assert_is_type(op, IdentityOperator)
        else:
            assert_is_type(op, CompositionOperator)

    for c1 in 'zenith,azimuth', 'azimuth,elevation':
        op = Cartesian2SphericalOperator(c1)
        assert_is_type(op.I, Spherical2CartesianOperator)
        assert_equal(op.convention, c1)
        for c2 in 'zenith,azimuth', 'azimuth,elevation':
            yield func, c1, c2
Example #16
0
def test_healpix_spherical_rules():
    def func(op, nside, c, nest):
        op2 = Spherical2HealpixOperator(nside, c, nest=nest)
        if c == op.convention and nside == op.nside and nest == op.nest:
            assert_is_type(op(op2), IdentityOperator)
        else:
            assert_is_type(op(op2), CompositionOperator)

    for c in Healpix2SphericalOperator.CONVENTIONS:
        op = Healpix2SphericalOperator(NSIDE, c, nest=True)
        assert_is_type(op.I, Spherical2HealpixOperator)
        assert_equal(op.I.convention, op.convention)
        for nside in NSIDE, 2 * NSIDE:
            for c2 in Healpix2SphericalOperator.CONVENTIONS:
                for nest in False, True:
                    yield func, op, nside, c2, nest
Example #17
0
def test_beams():
    assert_is_type(q.primary_beam, BeamGaussian)
    assert_is_type(q.secondary_beam, BeamGaussian)
    a = QubicAcquisition(150,
                         s,
                         primary_beam=BeamUniformHalfSpace(),
                         secondary_beam=BeamUniformHalfSpace())
    assert_is_type(a.instrument.primary_beam, BeamUniformHalfSpace)
    assert_is_type(a.instrument.secondary_beam, BeamUniformHalfSpace)
Example #18
0
 def func(cls, itype, ftype, inplace):
     proj_ref = _get_projection[cls](itype, ftype)
     proj_ = _get_projection[cls](itype, ftype)
     proj = proj_.restrict(restriction, inplace=inplace)
     if inplace:
         assert_is_type(proj_, DeletedOperator)
     if cls is not FSRMatrix:
         def pack(v):
             return v[restriction, :]
     else:
         pack = PackOperator(restriction)
     masking = MaskOperator(~restriction, broadcast='rightward')
     block_size = proj_ref.matrix.block_shape[1]
     shape = (5,) + ((block_size,) if block_size > 1 else ())
     x = np.arange(5*block_size).reshape(shape) + 1
     assert_equal(proj_ref(masking(x)), proj(pack(x)))
     pack = PackOperator(restriction)
     assert_equal(pack(kernel), proj.canonical_basis_in_kernel())
Example #19
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 #20
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 #21
0
    def func(op1, op2):
        op = op1(op2)
        attr = {}
        attr.update(op2.attrout)
        attr.update(op1.attrout)
        assert_equal(op.attrout, attr)
        assert_is(op.classout, op1.classout)
        if op1.flags.linear:
            assert_is_type(op, ZeroOperator)
            assert_same(op.todense(shapein=3, shapeout=4), np.zeros((4, 3)))
            return
        if op1.flags.shape_output == 'unconstrained' or \
           op1.flags.shape_input != 'explicit' and \
           op2.flags.shape_output != 'explicit':
            assert_is_type(op, CompositionOperator)
        else:
            assert_is_type(op, ConstantOperator)

        if op1.flags.shape_input == 'unconstrained' and \
           op2.flags.shape_output == 'unconstrained':
            return
        with rule_manager(none=True):
            op_ref = op1(op2)
        assert_same(op.todense(shapein=3, shapeout=4),
                    op_ref.todense(shapein=3, shapeout=4))
Example #22
0
def test_healpix_cartesian_rules():
    op = Healpix2CartesianOperator(NSIDE)
    assert_is_type(op.I, Cartesian2HealpixOperator)
    assert_equal(op.I.nside, op.nside)
    assert_is_type(Healpix2CartesianOperator(NSIDE)(
        Cartesian2HealpixOperator(2 * NSIDE)), CompositionOperator)
    assert_is_type(Healpix2CartesianOperator(NSIDE)(
        Cartesian2HealpixOperator(NSIDE)), IdentityOperator)
Example #23
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 #24
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 #25
0
def test_healpix_cartesian_rules():
    op = Healpix2CartesianOperator(NSIDE)
    assert_is_type(op.I, Cartesian2HealpixOperator)
    assert_equal(op.I.nside, op.nside)
    assert_is_type(
        Healpix2CartesianOperator(NSIDE)(Cartesian2HealpixOperator(2 * NSIDE)),
        CompositionOperator)
    assert_is_type(
        Healpix2CartesianOperator(NSIDE)(Cartesian2HealpixOperator(NSIDE)),
        IdentityOperator)
Example #26
0
    def func(cls, d1, d2):
        op = {AdditionOperator: operator.add,
              CompositionOperator: operator.mul,
              MultiplicationOperator: operator.mul}[cls]
        d = cls([d1, d2])
        if type(d1) is DiagonalOperator:
            assert_is_type(d, DiagonalOperator)
        elif type(d1) is HomothetyOperator:
            assert_is_type(d, HomothetyOperator)
        elif op is CompositionOperator:
            assert_is_type(d, IdentityOperator)
        else:
            assert_is_type(d, HomothetyOperator)

        data = op(d1.data.T, d2.data.T).T \
               if 'rightward' in (d1.broadcast, d2.broadcast) \
               else op(d1.data, d2.data)
        assert_same(d.data, data)
        if cls is CompositionOperator:
            assert_same(d(x), d1(d2(x)))
        else:
            assert_same(d(x), op(d1(x), d2(x)))
Example #27
0
def test_dio_morph():
    op = MPIDistributionIdentityOperator(MPI.COMM_SELF)
    assert_is_type(op, IdentityOperator)
Example #28
0
def test_dio_morph():
    op = MPIDistributionIdentityOperator(MPI.COMM_SELF)
    assert_is_type(op, IdentityOperator)
Example #29
0
def test_degrees_rules():
    d = DegreesOperator()
    assert_is_type(d.I, RadiansOperator)
Example #30
0
def test_power_rule_mul():
    ops = (ReciprocalOperator(), SqrtOperator(), SquareOperator(),
           PowerOperator(2.5))
    op = MultiplicationOperator(ops)
    assert_is_type(op, PowerOperator)
    assert_equal(op.n, 4)
Example #31
0
 def func(op, nside, c, nest):
     op2 = Spherical2HealpixOperator(nside, c, nest=nest)
     if c == op.convention and nside == op.nside and nest == op.nest:
         assert_is_type(op(op2), IdentityOperator)
     else:
         assert_is_type(op(op2), CompositionOperator)
Example #32
0
 def func1(cls):
     d = cls(3.)
     assert_is_type(d, HomothetyOperator)
Example #33
0
 def func(n, c):
     op = PowerOperator(n)
     assert_is_type(op, c)
     if isinstance(op, PowerOperator):
         assert_equal(op.n, n)
Example #34
0
def test_radians_rules():
    d = RadiansOperator()
    assert_is_type(d.I, DegreesOperator)
Example #35
0
def test_power_rule_comp():
    ops = (ReciprocalOperator(), SqrtOperator(), SquareOperator(),
           PowerOperator(2.5))
    op = CompositionOperator(ops)
    assert_is_type(op, PowerOperator)
    assert_equal(op.n, -2.5)
Example #36
0
 def func2(shape):
     d = DenseBlockDiagonalOperator(np.ones(shape))
     assert_is_type(d, DenseOperator)
Example #37
0
 def func(op, nside, c, nest):
     op2 = Spherical2HealpixOperator(nside, c, nest=nest)
     if c == op.convention and nside == op.nside and nest == op.nest:
         assert_is_type(op(op2), IdentityOperator)
     else:
         assert_is_type(op(op2), CompositionOperator)
Example #38
0
def test_zero8():
    class Op(Operator):
        pass
    o = Op()
    assert_is_type(o + O, Op)
Example #39
0
def test_healpix_convolution_morph():
    op = HealpixConvolutionGaussianOperator(fwhm=0)
    assert_is_type(op, IdentityOperator)
    op = HealpixConvolutionGaussianOperator(sigma=0)
    assert_is_type(op, IdentityOperator)
Example #40
0
def test_degrees_rules():
    d = DegreesOperator()
    assert_is_type(d.I, RadiansOperator)
Example #41
0
def test_power_rule_comp():
    ops = (ReciprocalOperator(), SqrtOperator(), SquareOperator(),
           PowerOperator(2.5))
    op = CompositionOperator(ops)
    assert_is_type(op, PowerOperator)
    assert_equal(op.n, -2.5)
Example #42
0
def test_radians_rules():
    d = RadiansOperator()
    assert_is_type(d.I, DegreesOperator)
Example #43
0
def test_power_rule_mul():
    ops = (ReciprocalOperator(), SqrtOperator(), SquareOperator(),
           PowerOperator(2.5))
    op = MultiplicationOperator(ops)
    assert_is_type(op, PowerOperator)
    assert_equal(op.n, 4)
Example #44
0
 def func2(tau):
     assert_is_type(ConvolutionTruncatedExponentialOperator(tau),
                    ConvolutionTruncatedExponentialOperator)
Example #45
0
 def func2(tau):
     assert_is_type(ConvolutionTruncatedExponentialOperator(tau),
                    ConvolutionTruncatedExponentialOperator)
Example #46
0
 def func(d, e):
     op = DiagonalOperator(d)
     if all(_ in (-1, 1) for _ in op.data.flat):
         assert op.flags.involutary
     assert_is_type(op, e)
Example #47
0
def test_healpix_convolution_morph():
    op = HealpixConvolutionGaussianOperator(fwhm=0)
    assert_is_type(op, IdentityOperator)
    op = HealpixConvolutionGaussianOperator(sigma=0)
    assert_is_type(op, IdentityOperator)