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(3, c_type=ctypes.c_double, init_scalar_or_matrix=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(3, c_type=ctypes.c_float, init_scalar_or_matrix=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])
def test_get_oob(self): # 2x2 covariance mat c = Covariance(c_type=ctypes.c_double) _ = c[0, 0] # Valid access nose.tools.assert_raises(IndexError, c.__getitem__, (0, 2)) c = Covariance(c_type=ctypes.c_float) _ = c[0, 0] # Valid access nose.tools.assert_raises(IndexError, c.__getitem__, (0, 2))
def test_set_oob(self): # 2x2 covariance mat c = Covariance(c_type=ctypes.c_float) c[0, 0] = 1 # Valid set nose.tools.assert_raises(IndexError, c.__setitem__, (0, 2), 1) c = Covariance(c_type=ctypes.c_double) c[0, 0] = 1 # Valid set nose.tools.assert_raises(IndexError, c.__setitem__, (0, 2), 1)
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())
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(3, c_type=ctypes.c_double, init_scalar_or_matrix=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(3, c_type=ctypes.c_float, init_scalar_or_matrix=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)
def test_covariance(self): for ct in self.C_TYPES: print(ct) l = Landmark(c_type=ct) # check default nose.tools.assert_equal(l.covariance, Covariance(3)) # set type-aligned covariance c = Covariance(3, ct, 7) l.covariance = c nose.tools.assert_equal(l.covariance, c) # set no-necessarily-aligned covariance c = Covariance(3, init_scalar_or_matrix=7) l.covariance = c nose.tools.assert_equal(l.covariance, c)
def covariance(self): cptr = self._call_cfunc( "vital_feature_covar", [self.C_TYPE_PTR], [self], Covariance.c_ptr_type(2, ctypes.c_double), ) return Covariance(2, ctypes.c_double, from_cptr=cptr)
def covariance(self): """ :return: a copy of this camera's center covariance :rtype: Covariance """ cam_covar = self.VITAL_LIB['vital_camera_center_covar'] cam_covar.argtypes = [self.c_ptr_type(), VitalErrorHandle.c_ptr_type()] cam_covar.restype = Covariance.c_ptr_type(3) with VitalErrorHandle() as eh: c_ptr = cam_covar(self, eh) return Covariance(3, from_cptr=c_ptr)
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])
def covariance(self, covar): if not isinstance(covar, Covariance): # Try an make a covariance out of whatever was provided covar = Covariance(2, self._datatype, covar) print "Setting covar:", covar self._call_cfunc( "vital_feature_{}_set_covar".format(self._tchar), [self.C_TYPE_PTR, Covariance.c_ptr_type(2, self._datatype)], [self, covar], )
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)
def test_set_covar(self): f = Feature(ctype=ctypes.c_double) nose.tools.assert_equal(f.covariance, Covariance()) expected = [[1, 2], [3, 4]] c = Covariance(2, ctypes.c_double, 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=ctypes.c_float) nose.tools.assert_equal(f.covariance, Covariance()) expected = [[1, 2], [3, 4]] c = Covariance(2, ctypes.c_float, 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)
def covariance(self, covar): if self._datatype is None: raise VitalNoTypeInfoException( "Type info required but not present") if not isinstance(covar, Covariance): # Try an make a covariance out of whatever was provided covar = Covariance(2, self._datatype, covar) print "Setting covar:", covar self._call_cfunc( "vital_feature_{}_set_covar".format(self._tchar), [self.C_TYPE_PTR, Covariance.c_ptr_type(2, self._datatype)], [self, covar], )
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))
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())
def test_equal(self): for ct in self.C_TYPES: print(ct) l = Landmark(c_type=ct) l2 = Landmark(c_type=ct) nose.tools.assert_equal(l, l2) l = Landmark([1, 1, 1], 42.2, c_type=ct) l2 = Landmark([1, 1, 1], 42.2, c_type=ct) nose.tools.assert_equal(l, l2) norm = [0, 0.5, 0.5] covar = Covariance(3, init_scalar_or_matrix=3) color = RGBColor(2, 1, 5) obs = 43 l.normal = l2.normal = norm l.covariance = l2.covariance = covar l.color = l2.color = color l.observations = l2.observations = obs nose.tools.assert_equal(l, l2)
def test_get_covar(self): dflt_covar = Covariance(2) f = Feature() nose.tools.assert_equal(f.covariance, dflt_covar)
def covariance(self): cptr = self._call_cfunc('vital_landmark_covariance', [self.C_TYPE_PTR], [self], Covariance.c_ptr_type(3)) return Covariance(3, from_cptr=cptr)