Example #1
0
def test_collection_reset():
    counter = memory._gc_nbytes_counter
    op = HomothetyOperator(2)
    op.delete()
    assert memory._gc_nbytes_counter - counter == 8
    memory.garbage_collect()
    assert memory._gc_nbytes_counter == 0
Example #2
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 #3
0
def test_partition4():
    o1 = HomothetyOperator(1, shapein=1)
    o2 = HomothetyOperator(2, shapein=2)
    o3 = HomothetyOperator(3, shapein=3)

    @flags.separable
    class Op(Operator):
        pass

    op = Op()
    p = BlockDiagonalOperator([o1, o2, o3], axisin=0)
    r = (op + p + op) * p
    assert isinstance(r, BlockDiagonalOperator)
Example #4
0
def test_partition1():
    o1 = HomothetyOperator(1, shapein=1)
    o2 = HomothetyOperator(2, shapein=2)
    o3 = HomothetyOperator(3, shapein=3)
    r = DiagonalOperator([1, 2, 2, 3, 3, 3]).todense()

    def func(ops, p):
        op = BlockDiagonalOperator(ops, partitionin=p, axisin=0)
        assert_eq(op.todense(6), r, str(op))

    for ops, p in zip(
        ((o1, o2, o3), (I, o2, o3), (o1, 2 * I, o3), (o1, o2, 3 * I)),
        (None, (1, 2, 3), (1, 2, 3), (1, 2, 3))):
        yield func, ops, p
Example #5
0
    def get_polarizer_operator(self, sampling, scene):
        """
        Return operator for the polarizer grid.
        When the polarizer is not present a transmission of 1 is assumed
        for the detectors on the first focal plane and of 0 for the other.
        Otherwise, the signal is split onto the focal planes.

        """
        nd = len(self)
        nt = len(sampling)
        grid = self.detector.quadrant // 4

        if scene.kind == 'I':
            if self.optics.polarizer:
                return HomothetyOperator(1 / 2)
            # 1 for the first detector grid and 0 for the second one
            return DiagonalOperator(1 - grid,
                                    shapein=(nd, nt),
                                    broadcast='rightward')

        if not self.optics.polarizer:
            raise NotImplementedError(
                'Polarized input is not handled without the polarizer grid.')

        z = np.zeros(nd)
        data = np.array([z + 0.5, 0.5 - grid, z]).T[:, None, None, :]
        return ReshapeOperator((nd, nt, 1), (nd, nt)) * \
            DenseBlockDiagonalOperator(data, shapein=(nd, nt, 3))
Example #6
0
    def get_aperture_integration_operator(self):
        """
        Integrate flux density in the telescope aperture.
        Convert signal from W / m^2 / Hz into W / Hz.

        """
        nhorns = np.sum(self.horn.open)
        return HomothetyOperator(nhorns * np.pi * self.horn.radius**2)
Example #7
0
    def get_filter_operator(self):
        """
        Return the filter operator.
        Convert units from W/Hz to W.

        """
        if self.filter.bandwidth == 0:
            return IdentityOperator()
        return HomothetyOperator(self.filter.bandwidth)
Example #8
0
def test_block1():
    ops = [HomothetyOperator(i, shapein=(2, 2)) for i in range(1, 4)]

    def func(axis, s):
        op = BlockDiagonalOperator(ops, new_axisin=axis)
        assert_eq(op.shapein, s)
        assert_eq(op.shapeout, s)

    for axis, s in zip(range(-3, 3), ((3, 2, 2), (2, 3, 2), (2, 2, 3),
                                      (3, 2, 2), (2, 3, 2), (2, 2, 3))):
        yield func, axis, s
Example #9
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 #10
0
def test_diagonal2():
    ops = (DiagonalOperator([1., 2], broadcast='rightward'),
           DiagonalOperator([[2., 3, 4], [5, 6, 7]], broadcast='rightward'),
           DiagonalOperator([1., 2, 3, 4, 5], broadcast='leftward'),
           DiagonalOperator(np.arange(20).reshape(4, 5), broadcast='leftward'),
           DiagonalOperator(np.arange(120.).reshape(2, 3, 4, 5)),
           HomothetyOperator(7.),
           IdentityOperator())

    x = np.arange(120.).reshape(2, 3, 4, 5) / 2

    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)))
    for op in (AdditionOperator, CompositionOperator):#, MultiplicationOperator):
        for d1, d2 in itertools.combinations(ops, 2):
            if set((d1.broadcast, d2.broadcast)) == \
               set(('leftward', 'rightward')):
                continue
            yield func, op, d1, d2
Example #11
0
def test_absorb_scalar():
    h = HomothetyOperator(2)

    @linear
    class AbsorbRightOperator(Operator):
        def __init__(self, value=3., **keywords):
            self.value = np.array(value)
            Operator.__init__(self, **keywords)
            self.set_rule(('.', HomothetyOperator), lambda s, o:
                          AbsorbRightOperator(s.value * o.data),
                          CompositionOperator)

    @linear
    class AbsorbLeftOperator(Operator):
        def __init__(self, value=3., **keywords):
            self.value = np.array(value)
            Operator.__init__(self, **keywords)
            self.set_rule((HomothetyOperator, '.'), lambda o, s:
                          AbsorbLeftOperator(s.value * o.data),
                          CompositionOperator)
    nl = NonLinearOperator()
    l = LinearOperator()
    ar = AbsorbRightOperator()
    al = AbsorbLeftOperator()
    ops = [[h, nl, h, ar, nl, h, al, nl, h],
           [h, nl, ar, h, nl, al, h, nl, h],
           [h, ar, nl, h, al],
           [ar, h, nl, al, h],
           [h, ar, l, h, al],
           [ar, h, l, al, h],
           [h, l, ar],
           [l, ar, h],
           [h, l, al],
           [l, al, h]]
    expected_types = [
        [HomothetyOperator, NonLinearOperator, AbsorbRightOperator,
         NonLinearOperator, AbsorbLeftOperator, NonLinearOperator,
         HomothetyOperator],
        [HomothetyOperator, NonLinearOperator, AbsorbRightOperator,
         NonLinearOperator, AbsorbLeftOperator, NonLinearOperator,
         HomothetyOperator],
        [AbsorbRightOperator, NonLinearOperator, AbsorbLeftOperator],
        [AbsorbRightOperator, NonLinearOperator, AbsorbLeftOperator],
        [AbsorbRightOperator, LinearOperator, AbsorbLeftOperator],
        [AbsorbRightOperator, LinearOperator, AbsorbLeftOperator],
        [LinearOperator, AbsorbRightOperator],
        [LinearOperator, AbsorbRightOperator],
        [LinearOperator, AbsorbLeftOperator],
        [LinearOperator, AbsorbLeftOperator]]
    expected_values = [[2, 0, 6, 0, 6, 0, 2],
                       [2, 0, 6, 0, 6, 0, 2],
                       [6, 0, 6],
                       [6, 0, 6],
                       [12, 0, 3],
                       [12, 0, 3],
                       [0, 6],
                       [0, 6],
                       [0, 6],
                       [0, 6]]

    def get_val(op):
        if isinstance(op, (NonLinearOperator, LinearOperator)):
            return 0
        if isinstance(op, HomothetyOperator):
            return op.data
        return op.value

    def func(ops, expected_types, expected_values):
        op = CompositionOperator(ops)
        assert_eq([type(o) for o in op.operands], expected_types)
        assert_eq([get_val(o) for o in op.operands], expected_values)
    for op, expected_type, expected_value in zip(ops, expected_types,
                                                 expected_values):
        yield func, op, expected_type, expected_value
Example #12
0
def test_binaryrule4():
    rule = BinaryRule(('.', HomothetyOperator), p1)
    yield assert_is_none, rule(op1, op2)
    s = HomothetyOperator(2)
    yield assert_equal, rule(op1, s), (s, op1)