Example #1
0
 def test_new_identity(self):
     # Valid dimensions and types
     c = Covariance(2, ctypes.c_double)
     print('constructed matrix:\n', c.to_matrix())
     c = Covariance(3, ctypes.c_double)
     print('constructed matrix:\n', c.to_matrix())
     c = Covariance(2, ctypes.c_float)
     print('constructed matrix:\n', c.to_matrix())
     c = Covariance(3, ctypes.c_float)
     print('constructed matrix:\n', c.to_matrix())
Example #2
0
 def test_new_identity(self):
     # Valid dimensions and types
     c = Covariance(2, ctypes.c_double)
     print 'constructed matrix:\n', c.to_matrix()
     c = Covariance(3, ctypes.c_double)
     print 'constructed matrix:\n', c.to_matrix()
     c = Covariance(2, ctypes.c_float)
     print 'constructed matrix:\n', c.to_matrix()
     c = Covariance(3, ctypes.c_float)
     print 'constructed matrix:\n', c.to_matrix()
Example #3
0
 def covariance(self, c):
     """
     Set new landmark covariance
     :type c: vital.types.Covariance
     """
     expected_covar_type = Covariance.c_ptr_type(3, self._datatype)
     # Convert covariance to correct type if necessary
     if c.C_TYPE_PTR != expected_covar_type:
         c = Covariance(3, self._datatype, c.to_matrix())
     self._call_cfunc('vital_landmark_{}_set_covar'.format(self._tchar),
                      [self.C_TYPE_PTR, expected_covar_type], [self, c])
Example #4
0
    def test_new_matrix(self):
        m = EigenArray(2, 2, dtype=numpy.double)
        m[:] = 1.
        c = Covariance(2, ctypes.c_double, 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
        m = EigenArray(2, 2, dtype=numpy.float32)
        m[:] = 1.
        c = Covariance(2, ctypes.c_double, 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(2, ctypes.c_float, init_scalar_or_matrix=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(3, init_scalar_or_matrix=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 #5
0
    def test_new_matrix(self):
        m = EigenArray(2, 2, dtype=numpy.double)
        m[:] = 1.
        c = Covariance(2, ctypes.c_double, 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
        m = EigenArray(2, 2, dtype=numpy.float32)
        m[:] = 1.
        c = Covariance(2, ctypes.c_double, 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(2, ctypes.c_float, init_scalar_or_matrix=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(3, init_scalar_or_matrix=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_from_cptr(self):
        # Create a new covariance from C function and create new python instance
        # from that pointer
        c_new_func = VitalObject.VITAL_LIB['vital_covariance_3d_new']
        c_new_func.argtypes = [VitalErrorHandle.C_TYPE_PTR]
        c_new_func.restype = Covariance.c_ptr_type(3, ctypes.c_double)
        with VitalErrorHandle() as eh:
            c_ptr = c_new_func(eh)

        c = Covariance(N=3, c_type=ctypes.c_double, from_cptr=c_ptr)
        nose.tools.assert_is(c.C_TYPE_PTR, Covariance.c_ptr_type(3, ctypes.c_double))
        numpy.testing.assert_array_equal(c.to_matrix(), numpy.eye(3))


        c_new_func = VitalObject.VITAL_LIB['vital_covariance_3f_new']
        c_new_func.argtypes = [VitalErrorHandle.C_TYPE_PTR]
        c_new_func.restype = Covariance.c_ptr_type(3, ctypes.c_float)
        with VitalErrorHandle() as eh:
            c_ptr = c_new_func(eh)

        c = Covariance(N=3, c_type=ctypes.c_float, from_cptr=c_ptr)
        nose.tools.assert_is(c.C_TYPE_PTR,Covariance.c_ptr_type(3, ctypes.c_float))
        numpy.testing.assert_array_equal(c.to_matrix(), numpy.eye(3))
Example #7
0
    def test_from_cptr(self):
        # Create a new covariance from C function and create new python instance
        # from that pointer
        c_new_func = VitalObject.VITAL_LIB['vital_covariance_3d_new']
        c_new_func.argtypes = [VitalErrorHandle.C_TYPE_PTR]
        c_new_func.restype = Covariance.c_ptr_type(3, ctypes.c_double)
        with VitalErrorHandle() as eh:
            c_ptr = c_new_func(eh)

        c = Covariance(N=3, c_type=ctypes.c_double, from_cptr=c_ptr)
        nose.tools.assert_is(c.C_TYPE_PTR,
                             Covariance.c_ptr_type(3, ctypes.c_double))
        numpy.testing.assert_array_equal(c.to_matrix(), numpy.eye(3))

        c_new_func = VitalObject.VITAL_LIB['vital_covariance_3f_new']
        c_new_func.argtypes = [VitalErrorHandle.C_TYPE_PTR]
        c_new_func.restype = Covariance.c_ptr_type(3, ctypes.c_float)
        with VitalErrorHandle() as eh:
            c_ptr = c_new_func(eh)

        c = Covariance(N=3, c_type=ctypes.c_float, from_cptr=c_ptr)
        nose.tools.assert_is(c.C_TYPE_PTR,
                             Covariance.c_ptr_type(3, ctypes.c_float))
        numpy.testing.assert_array_equal(c.to_matrix(), numpy.eye(3))
Example #8
0
    def test_new_scalar(self):
        c = Covariance(2, ctypes.c_double, 2.)
        print 'constructed matrix:\n', c.to_matrix()
        c = Covariance(3, ctypes.c_double, 2.)
        print 'constructed matrix:\n', c.to_matrix()
        c = Covariance(2, ctypes.c_float, 2.)
        print 'constructed matrix:\n', c.to_matrix()
        c = Covariance(3, ctypes.c_float, 2.)
        print 'constructed matrix:\n', c.to_matrix()

        c = Covariance(2, ctypes.c_double, 14.675)
        print 'constructed matrix:\n', c.to_matrix()
        c = Covariance(3, ctypes.c_double, 14.675)
        print 'constructed matrix:\n', c.to_matrix()
        c = Covariance(2, ctypes.c_float, 14.675)
        print 'constructed matrix:\n', c.to_matrix()
        c = Covariance(3, ctypes.c_float, 14.675)
        print 'constructed matrix:\n', c.to_matrix()
Example #9
0
    def test_new_scalar(self):
        c = Covariance(2, ctypes.c_double, 2.)
        print('constructed matrix:\n', c.to_matrix())
        c = Covariance(3, ctypes.c_double, 2.)
        print('constructed matrix:\n', c.to_matrix())
        c = Covariance(2, ctypes.c_float, 2.)
        print('constructed matrix:\n', c.to_matrix())
        c = Covariance(3, ctypes.c_float, 2.)
        print('constructed matrix:\n', c.to_matrix())

        c = Covariance(2, ctypes.c_double, 14.675)
        print('constructed matrix:\n', c.to_matrix())
        c = Covariance(3, ctypes.c_double, 14.675)
        print('constructed matrix:\n', c.to_matrix())
        c = Covariance(2, ctypes.c_float, 14.675)
        print('constructed matrix:\n', c.to_matrix())
        c = Covariance(3, ctypes.c_float, 14.675)
        print('constructed matrix:\n', c.to_matrix())