コード例 #1
0
ファイル: test_ipfp.py プロジェクト: Avasse/larray
    def test_ipfp_no_values(self):
        # 6, 12, 18
        along_a = ndtest([(3, 'b')], start=1) * 6
        # 6, 12, 18
        along_b = ndtest([(3, 'a')], start=1) * 6
        r = ipfp([along_a, along_b])
        assert_array_equal(r,
                           [[1.0, 2.0, 3.0], [2.0, 4.0, 6.0], [3.0, 6.0, 9.0]])

        along_a = LArray([2, 1], Axis(2, 'b'))
        along_b = LArray([1, 2], Axis(2, 'a'))
        r = ipfp([along_a, along_b])
        assert_array_equal(r, [[2 / 3, 1 / 3], [4 / 3, 2 / 3]])
コード例 #2
0
    def test_ipfp(self):
        a = Axis(2, 'a')
        b = Axis(2, 'b')
        initial = LArray([[2, 1], [1, 2]], [a, b])

        # array sums already match target sums
        # [3, 3], [3, 3]
        r = ipfp([initial.sum(a), initial.sum(b)], initial)
        assert_array_equal(r, [[2, 1], [1, 2]])

        # array sums do not match target sums (ie the usual case)
        along_a = LArray([2, 1], b)
        along_b = LArray([1, 2], a)
        r = ipfp([along_a, along_b], initial)
        assert_array_equal(r, [[0.8, 0.2], [1.0, 1.0]])

        # same as above but using a more precise threshold
        r = ipfp([along_a, along_b], initial, threshold=0.01)
        assert_array_equal(r, [[0.8450704225352113, 0.15492957746478875],
                               [1.1538461538461537, 0.8461538461538463]])

        # inverted target sums
        with self.assertRaisesRegexp(
                ValueError,
                "axes of target sum along axis 0 \(a\) do not match corresponding "
                "array axes: got {a\*} but expected {b\*}. Are the target sums in the "
                "correct order\?"):
            ipfp([along_b, along_a], initial, threshold=0.01)
コード例 #3
0
def test_create_model():
    # data = None --> empty array with shape (0,0) is generated
    model = ArrayModel()
    assert model.get_data_2D().shape == (0, 0)

    # data = scalar
    model = ArrayModel(LArray(5))
    assert model.get_data_2D().shape == (1, 1)

    # data = 1D array --> reshaped to 2D array with dim (1, len(data))
    model = ArrayModel(ndtest(5))
    assert model.get_data_2D().shape == (1, 5)

    # data = 3D array --> reshaped to 2D array with dim (axis1*axis2, axis3)
    model = ArrayModel(ndtest((5, 5, 5)))
    assert model.get_data_2D().shape == (5 * 5, 5)
コード例 #4
0
ファイル: test_ipfp.py プロジェクト: Avasse/larray
    def test_ipfp_no_name(self):
        initial = LArray([[2, 1], [1, 2]])

        # sums already correct
        # [3, 3], [3, 3]
        r = ipfp([initial.sum(axis=0), initial.sum(axis=1)], initial)
        assert_array_equal(r, [[2, 1], [1, 2]])

        # different sums (ie the usual case)
        along_a = LArray([2, 1])
        along_b = LArray([1, 2])
        r = ipfp([along_a, along_b], initial)
        assert_array_equal(r, [[0.8, 0.2], [1.0, 1.0]])
コード例 #5
0
ファイル: test_ipfp.py プロジェクト: Avasse/larray
    def test_ipfp(self):
        a = Axis('a=a0,a1')
        b = Axis('b=b0,b1')
        initial = LArray([[2, 1], [1, 2]], [a, b])

        # array sums already match target sums
        # [3, 3], [3, 3]
        r = ipfp([initial.sum(a), initial.sum(b)], initial)
        assert_array_equal(r, initial)

        # array sums do not match target sums (ie the usual case)
        along_a = LArray([2, 1], b)
        along_b = LArray([1, 2], a)
        r = ipfp([along_a, along_b], initial)
        assert_array_equal(r, [[0.8, 0.2], [1.0, 1.0]])

        # same as above but using a more precise threshold
        r = ipfp([along_a, along_b], initial, threshold=0.01)
        assert_array_equal(r, [[0.8450704225352113, 0.15492957746478875],
                               [1.1538461538461537, 0.8461538461538463]])

        # inverted target sums
        with self.assertRaisesRegexp(
                ValueError,
                "axes of target sum along a \(axis 0\) do not match corresponding "
                "array axes: got {a} but expected {b}. Are the target sums in the "
                "correct order\?"):
            ipfp([along_b, along_a], initial)

        # different target sums totals
        along_a = LArray([2, 1], b)
        along_b = LArray([1, 3], a)
        with self.assertRaisesRegexp(
                ValueError,
                "target sum along b \(axis 1\) is different than target sum along "
                "a \(axis 0\): 4 vs 3"):
            ipfp([along_a, along_b], initial)

        # all zero values
        initial = LArray([[0, 0], [1, 2]], [a, b])
        along_a = LArray([2, 1], b)
        along_b = LArray([1, 2], a)
        with self.assertRaisesRegexp(
                ValueError,
                "found all zero values sum along b \(axis 1\) but non zero target "
                "sum:\na0: 1"):
            ipfp([along_a, along_b], initial)

        # zero target sum
        initial = LArray([[2, 1], [1, 2]], [a, b])
        along_a = LArray([0, 1], b)
        along_b = LArray([1, 0], a)
        with self.assertRaisesRegexp(
                ValueError,
                "found Non Zero Values but Zero target Sum \(nzvzs\) along a "
                "\(axis 0\), use nzvzs='warn' or 'fix' to set them to zero "
                "automatically:\nb0: 3"):
            ipfp([along_a, along_b], initial)

        # negative initial values
        initial = LArray([[2, -1], [1, 2]], [a, b])
        with self.assertRaisesRegexp(ValueError,
                                     "negative value\(s\) found:\na0_b1: -1"):
            ipfp([along_a, along_b], initial)