예제 #1
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)
예제 #2
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]])
예제 #3
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)