Example #1
0
    def test_get_value(self):
        m = numpy.ndarray((3, 3))
        # [[ 0 1 2 ]               [[ 0 2 4 ]
        #  [ 3 4 5 ]  -> should ->  [ 2 4 6 ]
        #  [ 6 7 8 ]]               [ 4 6 8 ]]
        m.reshape((9,))[:] = list(range(9))

        c = Covariance.from_matrix(3, c_type='d', init=m)
        # Test matrix upper triangle locations
        nose.tools.assert_equal(c[0,0], 0)
        nose.tools.assert_equal(c[0,1], 2)
        nose.tools.assert_equal(c[0,2], 4)
        nose.tools.assert_equal(c[1,1], 4)
        nose.tools.assert_equal(c[1,2], 6)
        nose.tools.assert_equal(c[2,2], 8)
        nose.tools.assert_equal(c[0,1], c[1,0])
        nose.tools.assert_equal(c[0,2], c[2,0])
        nose.tools.assert_equal(c[1,2], c[2,1])

        c = Covariance.from_matrix(3, c_type='f', init=m)
        # Test matrix upper triangle locations
        nose.tools.assert_equal(c[0,0], 0)
        nose.tools.assert_equal(c[0,1], 2)
        nose.tools.assert_equal(c[0,2], 4)
        nose.tools.assert_equal(c[1,1], 4)
        nose.tools.assert_equal(c[1,2], 6)
        nose.tools.assert_equal(c[2,2], 8)
        nose.tools.assert_equal(c[0,1], c[1,0])
        nose.tools.assert_equal(c[0,2], c[2,0])
        nose.tools.assert_equal(c[1,2], c[2,1])
Example #2
0
    def test_get_value(self):
        m = numpy.ndarray((3, 3))
        # [[ 0 1 2 ]               [[ 0 2 4 ]
        #  [ 3 4 5 ]  -> should ->  [ 2 4 6 ]
        #  [ 6 7 8 ]]               [ 4 6 8 ]]
        m.reshape((9, ))[:] = list(range(9))

        c = Covariance.from_matrix(3, c_type='d', init=m)
        # Test matrix upper triangle locations
        nose.tools.assert_equal(c[0, 0], 0)
        nose.tools.assert_equal(c[0, 1], 2)
        nose.tools.assert_equal(c[0, 2], 4)
        nose.tools.assert_equal(c[1, 1], 4)
        nose.tools.assert_equal(c[1, 2], 6)
        nose.tools.assert_equal(c[2, 2], 8)
        nose.tools.assert_equal(c[0, 1], c[1, 0])
        nose.tools.assert_equal(c[0, 2], c[2, 0])
        nose.tools.assert_equal(c[1, 2], c[2, 1])

        c = Covariance.from_matrix(3, c_type='f', init=m)
        # Test matrix upper triangle locations
        nose.tools.assert_equal(c[0, 0], 0)
        nose.tools.assert_equal(c[0, 1], 2)
        nose.tools.assert_equal(c[0, 2], 4)
        nose.tools.assert_equal(c[1, 1], 4)
        nose.tools.assert_equal(c[1, 2], 6)
        nose.tools.assert_equal(c[2, 2], 8)
        nose.tools.assert_equal(c[0, 1], c[1, 0])
        nose.tools.assert_equal(c[0, 2], c[2, 0])
        nose.tools.assert_equal(c[1, 2], c[2, 1])
Example #3
0
    def test_set(self):
        m = numpy.ndarray((3, 3))
        # [[ 0 1 2 ]                      [[ 0 2 4 ]
        #  [ 3 4 5 ]  -> should become ->  [ 2 4 6 ]
        #  [ 6 7 8 ]]                      [ 4 6 8 ]]
        m.reshape((9,))[:] = list(range(9))
        c = Covariance.from_matrix(3, c_type='d', init=m)

        # modify some locations
        c[0,1] = 1
        c[2,2] = 3

        nose.tools.assert_equal(c[0,0], 0)
        nose.tools.assert_equal(c[0,1], 1)
        nose.tools.assert_equal(c[0,2], 4)
        nose.tools.assert_equal(c[1,1], 4)
        nose.tools.assert_equal(c[1,2], 6)
        nose.tools.assert_equal(c[2,2], 3)
        nose.tools.assert_equal(c[0,1], c[1,0])
        nose.tools.assert_equal(c[0,2], c[2,0])
        nose.tools.assert_equal(c[1,2], c[2,1])

        # Set in upper triangle and see it reflect in lower
        c[0, 2] = 10.1
        nose.tools.assert_equal(c[2, 0], 10.1)

        # Change something in lower triangle and see it reflected in upper
        c[2, 1] = 20.2
        nose.tools.assert_equal(c[1, 2], 20.2)

        # FLOAT
        c = Covariance.from_matrix(3, c_type='f', init=m)

        # modify some locations
        c[0,1] = 1
        c[2,2] = 3

        nose.tools.assert_equal(c[0,0], 0)
        nose.tools.assert_equal(c[0,1], 1)
        nose.tools.assert_equal(c[0,2], 4)
        nose.tools.assert_equal(c[1,1], 4)
        nose.tools.assert_equal(c[1,2], 6)
        nose.tools.assert_equal(c[2,2], 3)
        nose.tools.assert_equal(c[0,1], c[1,0])
        nose.tools.assert_equal(c[0,2], c[2,0])
        nose.tools.assert_equal(c[1,2], c[2,1])

        # Set in upper triangle and see it reflect in lower
        c[0, 2] = 10.1
        nose.tools.assert_almost_equal(c[2, 0], 10.1, 6)

        # Change something in lower triangle and see it reflected in upper
        c[2, 1] = 20.2
        nose.tools.assert_almost_equal(c[1, 2], 20.2, 5)
Example #4
0
    def test_set(self):
        m = numpy.ndarray((3, 3))
        # [[ 0 1 2 ]                      [[ 0 2 4 ]
        #  [ 3 4 5 ]  -> should become ->  [ 2 4 6 ]
        #  [ 6 7 8 ]]                      [ 4 6 8 ]]
        m.reshape((9, ))[:] = list(range(9))
        c = Covariance.from_matrix(3, c_type='d', init=m)

        # modify some locations
        c[0, 1] = 1
        c[2, 2] = 3

        nose.tools.assert_equal(c[0, 0], 0)
        nose.tools.assert_equal(c[0, 1], 1)
        nose.tools.assert_equal(c[0, 2], 4)
        nose.tools.assert_equal(c[1, 1], 4)
        nose.tools.assert_equal(c[1, 2], 6)
        nose.tools.assert_equal(c[2, 2], 3)
        nose.tools.assert_equal(c[0, 1], c[1, 0])
        nose.tools.assert_equal(c[0, 2], c[2, 0])
        nose.tools.assert_equal(c[1, 2], c[2, 1])

        # Set in upper triangle and see it reflect in lower
        c[0, 2] = 10.1
        nose.tools.assert_equal(c[2, 0], 10.1)

        # Change something in lower triangle and see it reflected in upper
        c[2, 1] = 20.2
        nose.tools.assert_equal(c[1, 2], 20.2)

        # FLOAT
        c = Covariance.from_matrix(3, c_type='f', init=m)

        # modify some locations
        c[0, 1] = 1
        c[2, 2] = 3

        nose.tools.assert_equal(c[0, 0], 0)
        nose.tools.assert_equal(c[0, 1], 1)
        nose.tools.assert_equal(c[0, 2], 4)
        nose.tools.assert_equal(c[1, 1], 4)
        nose.tools.assert_equal(c[1, 2], 6)
        nose.tools.assert_equal(c[2, 2], 3)
        nose.tools.assert_equal(c[0, 1], c[1, 0])
        nose.tools.assert_equal(c[0, 2], c[2, 0])
        nose.tools.assert_equal(c[1, 2], c[2, 1])

        # Set in upper triangle and see it reflect in lower
        c[0, 2] = 10.1
        nose.tools.assert_almost_equal(c[2, 0], 10.1, 6)

        # Change something in lower triangle and see it reflected in upper
        c[2, 1] = 20.2
        nose.tools.assert_almost_equal(c[1, 2], 20.2, 5)
Example #5
0
    def test_new_matrix(self):
        a = EigenArray(2, 2, type='d')
        m = a.get_matrix()
        m[:] = 1.
        c = Covariance.from_matrix(2, 'd', m)
        m_out = c.to_matrix()
        print('input matrix:\n', m)
        print('output matrix:\n', m_out)
        numpy.testing.assert_array_equal(m_out, m)

        # Type casting should be handled
        a = EigenArray(2, 2, type='f')
        m = a.get_matrix()
        m[:] = 1.
        c = Covariance.from_matrix(2, 'd', m)
        m_out = c.to_matrix()
        print('input matrix:\n', m)
        print('output matrix:\n', m_out)
        numpy.testing.assert_array_equal(m_out, m)

        # Any other numpy array of the correct shape should be acceptable
        m = numpy.ndarray((2, 2))
        m[:] = 3.
        c = Covariance.from_matrix(2, 'f', init=m)
        m_out = c.to_matrix()
        print('input matrix:\n', m)
        print('output matrix:\n', m_out)
        numpy.testing.assert_array_equal(m_out, m)

        # Diagonally congruent values should be averages when initializing with
        # matrix
        m = numpy.eye(3, dtype=numpy.double)
        m[0,2] = 2.
        m_expected = m.copy()
        m_expected[0,2] = 1.
        m_expected[2,0] = 1.
        c = Covariance.from_matrix(3, init=m)
        m_out = c.to_matrix()
        print('input matrix:\n', m)
        print('output matrix:\n', m_out)
        numpy.testing.assert_array_equal(m_out, m_expected)
Example #6
0
    def test_new_matrix(self):
        a = EigenArray(2, 2, type='d')
        m = a.get_matrix()
        m[:] = 1.
        c = Covariance.from_matrix(2, 'd', m)
        m_out = c.to_matrix()
        print('input matrix:\n', m)
        print('output matrix:\n', m_out)
        numpy.testing.assert_array_equal(m_out, m)

        # Type casting should be handled
        a = EigenArray(2, 2, type='f')
        m = a.get_matrix()
        m[:] = 1.
        c = Covariance.from_matrix(2, 'd', m)
        m_out = c.to_matrix()
        print('input matrix:\n', m)
        print('output matrix:\n', m_out)
        numpy.testing.assert_array_equal(m_out, m)

        # Any other numpy array of the correct shape should be acceptable
        m = numpy.ndarray((2, 2))
        m[:] = 3.
        c = Covariance.from_matrix(2, 'f', init=m)
        m_out = c.to_matrix()
        print('input matrix:\n', m)
        print('output matrix:\n', m_out)
        numpy.testing.assert_array_equal(m_out, m)

        # Diagonally congruent values should be averages when initializing with
        # matrix
        m = numpy.eye(3, dtype=numpy.double)
        m[0, 2] = 2.
        m_expected = m.copy()
        m_expected[0, 2] = 1.
        m_expected[2, 0] = 1.
        c = Covariance.from_matrix(3, init=m)
        m_out = c.to_matrix()
        print('input matrix:\n', m)
        print('output matrix:\n', m_out)
        numpy.testing.assert_array_equal(m_out, m_expected)
Example #7
0
    def test_set_covar(self):
        f = Feature(ctype='d')
        #nose.tools.assert_equal(f.covariance, Covariance.new_covar())

        expected = [[1, 2], [3, 4]]
        c = Covariance.from_matrix(2, 'd', expected)
        f.covariance = c
        #nose.tools.assert_equal(f.covariance, c)
        # Should also work if we just give it the raw iterable
        f.covariance = expected
        #nose.tools.assert_equal(f.covariance, c)

        # And for floats...
        f = Feature(ctype='f')
        #nose.tools.assert_equal(f.covariance, Covariance())

        expected = [[1, 2], [3, 4]]
        c = Covariance.from_matrix(2, 'f', expected)
        f.covariance = c
        #nose.tools.assert_equal(f.covariance, c)
        # Should also work if we just give it the raw iterable
        f.covariance = expected
Example #8
0
    def test_set_covar(self):
        f = Feature(ctype='d')
        #nose.tools.assert_equal(f.covariance, Covariance.new_covar())

        expected = [[1, 2],
                    [3, 4]]
        c = Covariance.from_matrix(2, 'd', expected)
        f.covariance = c
        #nose.tools.assert_equal(f.covariance, c)
        # Should also work if we just give it the raw iterable
        f.covariance = expected
        #nose.tools.assert_equal(f.covariance, c)

        # And for floats...
        f = Feature(ctype='f')
        #nose.tools.assert_equal(f.covariance, Covariance())

        expected = [[1, 2],
                    [3, 4]]
        c = Covariance.from_matrix(2, 'f', expected)
        f.covariance = c
        #nose.tools.assert_equal(f.covariance, c)
        # Should also work if we just give it the raw iterable
        f.covariance = expected