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
def genericSolverCheck(solverClass, params, gms, semiRings,checkPartial=False,checkMarginals=False,testPythonVisitor=True,testLpInterface=False): class PyCallback(object): def __init__(self): self.inBegin=False self.inEnd=False self.inVisit=False def begin(self,inference): self.inBegin=True def end(self,inference): self.inEnd=True def visit(self,inference): self.inVisit=True for operator, accumulator in semiRings: for gmGen in gms: gm = gmGen[operator] for param in params: # start inference solver = solverClass(gm=gm, accumulator=accumulator, parameter=param) solver2 = solverClass(gm=gm, accumulator=accumulator, parameter=param) if (testLpInterface==True): c=0 for vi in range(gm.numberOfVariables): nl = gm.numberOfLabels(vi) for l in range(nl): assert c==solver.lpNodeVariableIndex(vi,l) c+=1 cv=c for fi in range(gm.numberOfFactors): if gm[fi].numberOfVariables>1: s = gm[fi].size for l in range(nl): assert solver.lpFactorVariableIndex(fi,s)>0 or cv==0 sw = opengm.shapeWalker(gm[fi].shape) for c in sw: assert solver.lpFactorVariableIndex(fi,c)>0 or cv==0 solver2.addConstraint(lpVariableIndices=[0,1],coefficients=[1,1],lowerBound=0.0,upperBound=1.0) solver2.addConstraints(lpVariableIndices=[ [0,1],[0,2] ,[1,2]],coefficients=[ [1,1],[2,2],[1,2]],lowerBounds=[0,0,0],upperBounds=[1,1,1]) solver.infer() arg = solver.arg() # no used? value = solver.value() bound = solver.bound() if testPythonVisitor==True: solver = solverClass(gm=gm, accumulator=accumulator, parameter=param) callback=PyCallback() pvisitor=solver.pythonVisitor(callback,1) solver.infer(pvisitor) assert callback.inBegin == True assert callback.inEnd == True if checkPartial : pOptimal = solver.partialOptimality() assert len(pOptimal)==gm.numberOfVariables #assert len(numpy.where(pOptimal==True)[0]) == gm.numberOfVariables if checkMarginals : visRange=numpy.arange(gm.numberOfVariables) marginal = solver.marginals(visRange) assert marginal.shape[0]==gm.numberOfVariables assert marginal.shape[1]==gm.numberOfLabels(0) fis1 = gm.factorSubset(order=1).factorIndices fis2 = gm.factorSubset(order=2).factorIndices assert len(fis1)!=0 assert len(fis2)!=0 factorMarginal1 = solver.factorMarginals(fis1) assert factorMarginal1.ndim==2 assert factorMarginal1.shape[0]==len(fis1) assert factorMarginal1.shape[1]==gm.numberOfLabels(0) factorMarginal2 = solver.factorMarginals(fis2) assert factorMarginal2.ndim==3 assert factorMarginal2.shape[0]==len(fis2) assert factorMarginal2.shape[1]==gm.numberOfLabels(0) assert factorMarginal2.shape[2]==gm.numberOfLabels(0)
import opengm import numpy # some graphical model # -with 3 variables with 4 labels. # -with 2 random 2-order functions # -connected to 2 factors gm = opengm.gm([4] * 3) f = numpy.random.rand(4, 4).astype(numpy.float32) gm.addFactor(gm.addFunction(f), [0, 1]) f = numpy.random.rand(4, 4).astype(numpy.float32) gm.addFactor(gm.addFunction(f), [1, 2]) # iterate over all factors of the graphical model for factor in gm.factors(): # iterate over all labelings with a "shape walker" for coord in opengm.shapeWalker(f.shape): print " f[", coord, "]=", factor[coord]
def myFunc( labels): p=1.0 s=0 for l in labels: p*=l s+=l return p+s pf=opengm.PythonFunction(myFunc,[2,2]) print pf.shape print pf[0,0] print pf[1,1] for c in opengm.shapeWalker(pf.shape): print c," ",pf[c] unaries=numpy.random.rand(5 , 5,2) potts=opengm.PottsFunction([2,2],0.0,0.1) gm=opengm.grid2d2Order(unaries=unaries,regularizer=potts) viewFunction=opengm.modelViewFunction(gm[0]) for c in opengm.shapeWalker(viewFunction.shape): print c," ",viewFunction[c] viewFunction=opengm.modelViewFunction(gm[25]) print viewFunction.shape