def test_constructor(self): functions = [] functions.append(opengm.SparseFunction([2, 3, 4], 1)) functions.append(opengm.SparseFunction((2, 3, 4), 1)) for f in functions: assert(f.defaultValue == 1) assert(f.dimension == 3) assert(f.shape[0] == 2) assert(f.shape[1] == 3) assert(f.shape[2] == 4) assert(len(f.shape) == 3) assert(f.size == 2 * 3 * 4)
def test_key_to_coordinate(self): f = opengm.SparseFunction([2, 3, 4], 0) c = numpy.ones(3, dtype=numpy.uint64) for key, cTrue in enumerate(opengm.shapeWalker(f.shape)): f.keyToCoordinate(key, c) for ct, cOwn in zip(cTrue, c): assert ct == cOwn
def test_dense_assignment(self): f = opengm.SparseFunction() fDense = numpy.zeros([3, 4]) fDense[0, 1] = 1 fDense[0, 2] = 2 f.assignDense(fDense, 0) assert f.dimension == 2 assert f.shape[0] == 3 assert f.shape[1] == 4 assert f[[0, 0]] == 0 assert f[[0, 1]] == 1 assert f[[0, 2]] == 2 for c in opengm.shapeWalker(f.shape): assert f[c] == fDense[c[0], c[1]] assert len(f.container) == 2
#--------------------------------------------------------------- # Numpy Ndarray # (is stored in a different multi array function within opengm) #--------------------------------------------------------------- f = numpy.random.rand(2, 2, 3, 4) fid = gm.addFunction(f) gm.addFactor(fid, [0, 1, 2, 4]) print "\nexplicit function: \n", f #--------------------------------------------------------------- # Sparse Function #--------------------------------------------------------------- # fill sparse function "by hand" f = opengm.SparseFunction(shape=[3, 4, 4], defaultValue=1) # fill diagonale with zeros for d in xrange(4): f[[d, d, d]] = 0 print "\nsparse function: \n", f fid = gm.addFunction(f) functionIds.append(fid) gm.addFactor(fid, [3, 4, 5]) # fill sparse function from dense function f = opengm.SparseFunction() f.assignDense(numpy.identity(4), defaultValue=0) fid = gm.addFunction(f) functionIds.append(fid) gm.addFactor(fid, [4, 5]) print "\nsparse function: \n", f