################################################################################ ################################################################################ ############################################################################# #---- Making the Graph --------------------------------------------- # This initializes the graph, just basic things (its in the SparseGraph Ex) numFeatures=0 numVertices=(rowLength-1)*(colLength-1) vList=GeneralVertexList(numVertices) weightMatrix = scipy.sparse.lil_matrix((numVertices, numVertices)) graph = SparseGraph(vList,True, W=weightMatrix) # HERE IS YOUR GRAPH! #-- This assigns to each vertex an array as a value. In the array, the first value is the x location, and then y (row), and 1 implies the vertex is unexplored. 0 will mean it is closed. The fourth value determines whether the vertex is in the fringe (1) or not (0) #-- 5th value-g(A*), h(A*), f(A*), g(Theta*), h(Theta*), f(Theta*) row=1 for i in range (graph.getNumVertices()): if(i%(colLength-1)==0 and i!=0): row=row+1 graph.setVertex(i, [(i%(colLength-1))+1,row, 1, 0,100000000000.0,0.0, 100000000000.0,100000000000.0, 0.0, 100000000000.0]) """ check the vertex values for i in range (graph.getNumVertices()): print "vertexVal: "+str(i)+ " " + str(graph.getVertex(i)) """ #----- Connect vertices # This just runs through and connects the vertices # This is pretty complicated, and you don't need to worry about it. The code here should be right becuase the red edges are draen correctly edgeCheck=[] for row in range((rowLength-1)): for column in range(colLength-1):
############################################################################## ############################################################################## ############################################################################## #---- Making the Graph --------------------------------------------- # This initializes the graph, just basic things (its in the SparseGraph Ex) numFeatures=0 numVertices=(rowLength-1)*(colLength-1) vList=GeneralVertexList(numVertices) weightMatrix = scipy.sparse.lil_matrix((numVertices, numVertices)) graph = SparseGraph(vList,True, W=weightMatrix) # HERE IS YOUR GRAPH! #-- This assigns to each vertex an array as a value. In the array, the first value is the x location, and then y (row), and 1 implies the vertex is unexplored. 0 will mean it is closed. The fourth value determines whether the vertex is in the fringe (1) or not (0) #-- 5th value-g(A*), h(A*), f(A*), g(Theta*), h(Theta*), f(Theta*) row=1 for i in range (graph.getNumVertices()): if(i%(colLength-1)==0 and i!=0): row=row+1 graph.setVertex(i, [(i%(colLength-1))+1,row, 1, 0,100000000000.0,0.0, 100000000000.0,100000000000.0, 0.0, 100000000000.0]) #----- Connect vertices # This just runs through and connects the vertices # This is pretty complicated, and you don't need to worry about it. The code here should be right becuase the red edges are draen correctly edgeCheck=[] for row in range((rowLength-1)): for column in range(colLength-1): x=column+1 y=row+1 vertex1=(((colLength-1)*row)+column)
def match(self, graph1, graph2): """ Take two graphs are match them. The two graphs must be AbstractMatrixGraphs with VertexLists representing the vertices. :param graph1: A graph object :param graph2: The second graph object to match :return permutation: A vector of indices representing the matching of elements of graph1 to graph2 :return distance: The graph distance list [graphDistance, fDistance, fDistanceExact] """ # Deal with case where at least one graph is emty if graph1.size == 0 and graph2.size == 0: permutation = numpy.array([], numpy.int) distanceVector = [0, 0, 0] time = 0 return permutation, distanceVector, time elif graph1.size == 0 or graph2.size == 0: if graph1.size == 0: graph1 = SparseGraph(VertexList(graph2.size, graph2.getVertexList().getNumFeatures())) else: graph2 = SparseGraph(VertexList(graph1.size, graph1.getVertexList().getNumFeatures())) numTempFiles = 5 tempFileNameList = [] for i in range(numTempFiles): fileObj = tempfile.NamedTemporaryFile(delete=False) tempFileNameList.append(fileObj.name) fileObj.close() configFileName = tempFileNameList[0] graph1FileName = tempFileNameList[1] graph2FileName = tempFileNameList[2] similaritiesFileName = tempFileNameList[3] outputFileName = tempFileNameList[4] if self.useWeightM: W1 = graph1.getWeightMatrix() W2 = graph2.getWeightMatrix() else: W1 = graph1.adjacencyMatrix() W2 = graph2.adjacencyMatrix() numpy.savetxt(graph1FileName, W1, fmt="%.5f") numpy.savetxt(graph2FileName, W2, fmt="%.5f") # Compute matrix similarities C = self.vertexSimilarities(graph1, graph2) numpy.savetxt(similaritiesFileName, C, fmt="%.5f") # Write config file configFile = open(configFileName, "w") configStr = "graph_1=" + graph1FileName + " s\n" configStr += "graph_2=" + graph2FileName + " s\n" configStr += "C_matrix=" + similaritiesFileName + " s\n" configStr += "algo=" + self.algorithm + " s\n" configStr += "algo_init_sol=" + self.init + " s\n" configStr += "alpha_ldh=" + str(self.alpha) + " d\n" configStr += "cdesc_matrix=A c\n" configStr += "cscore_matrix=A c\n" configStr += "hungarian_max=10000 d\n" configStr += "algo_fw_xeps=0.01 d\n" configStr += "algo_fw_feps=0.01 d\n" configStr += "dummy_nodes=0 i\n" configStr += "dummy_nodes_fill=" + str(self.rho) + " d\n" configStr += "dummy_nodes_c_coef=" + str(self.gamma) + " d\n" configStr += "qcvqcc_lambda_M=" + str(self.lambdaM) + " d\n" configStr += "qcvqcc_lambda_min=1e-3 d\n" configStr += "blast_match=0 i\n" configStr += "blast_match_proj=0 i\n" configStr += "exp_out_file=" + outputFileName + " s\n" configStr += "exp_out_format=Compact Permutation s\n" configStr += "verbose_mode=0 i\n" configStr += "verbose_file=cout s\n" configFile.write(configStr) configFile.close() fnull = open(os.devnull, "w") home = expanduser("~") argList = [home + "/.local/bin/graphm", configFileName] subprocess.call(argList, stdout=fnull, stderr=fnull) fnull.close() # Next: parse input files outputFile = open(outputFileName, "r") line = outputFile.readline() line = outputFile.readline() line = outputFile.readline() line = outputFile.readline() graphDistance = float(outputFile.readline().split()[2]) fDistance = float(outputFile.readline().split()[2]) fDistanceExact = float(outputFile.readline().split()[2]) time = float(outputFile.readline().split()[1]) line = outputFile.readline() line = outputFile.readline() permutation = numpy.zeros(max(graph1.getNumVertices(), graph2.getNumVertices()), numpy.int) i = 0 for line in outputFile: permutation[i] = int(line.strip()) - 1 i += 1 # Delete files os.remove(graph1FileName) os.remove(graph2FileName) os.remove(similaritiesFileName) os.remove(configFileName) os.remove(outputFileName) distanceVector = [graphDistance, fDistance, fDistanceExact] return permutation, distanceVector, time
def match(self, graph1, graph2): """ Take two graphs are match them. The two graphs must be AbstractMatrixGraphs with VertexLists representing the vertices. :param graph1: A graph object :param graph2: The second graph object to match :return permutation: A vector of indices representing the matching of elements of graph1 to graph2 :return distance: The graph distance list [graphDistance, fDistance, fDistanceExact] """ #Deal with case where at least one graph is emty if graph1.size == 0 and graph2.size == 0: permutation = numpy.array([], numpy.int) distanceVector = [0, 0, 0] time = 0 return permutation, distanceVector, time elif graph1.size == 0 or graph2.size == 0: if graph1.size == 0: graph1 = SparseGraph( VertexList(graph2.size, graph2.getVertexList().getNumFeatures())) else: graph2 = SparseGraph( VertexList(graph1.size, graph1.getVertexList().getNumFeatures())) numTempFiles = 5 tempFileNameList = [] for i in range(numTempFiles): fileObj = tempfile.NamedTemporaryFile(delete=False) tempFileNameList.append(fileObj.name) fileObj.close() configFileName = tempFileNameList[0] graph1FileName = tempFileNameList[1] graph2FileName = tempFileNameList[2] similaritiesFileName = tempFileNameList[3] outputFileName = tempFileNameList[4] if self.useWeightM: W1 = graph1.getWeightMatrix() W2 = graph2.getWeightMatrix() else: W1 = graph1.adjacencyMatrix() W2 = graph2.adjacencyMatrix() numpy.savetxt(graph1FileName, W1, fmt='%.5f') numpy.savetxt(graph2FileName, W2, fmt='%.5f') #Compute matrix similarities C = self.vertexSimilarities(graph1, graph2) numpy.savetxt(similaritiesFileName, C, fmt='%.5f') #Write config file configFile = open(configFileName, 'w') configStr = "graph_1=" + graph1FileName + " s\n" configStr += "graph_2=" + graph2FileName + " s\n" configStr += "C_matrix=" + similaritiesFileName + " s\n" configStr += "algo=" + self.algorithm + " s\n" configStr += "algo_init_sol=" + self.init + " s\n" configStr += "alpha_ldh=" + str(self.alpha) + " d\n" configStr += "cdesc_matrix=A c\n" configStr += "cscore_matrix=A c\n" configStr += "hungarian_max=10000 d\n" configStr += "algo_fw_xeps=0.01 d\n" configStr += "algo_fw_feps=0.01 d\n" configStr += "dummy_nodes=0 i\n" configStr += "dummy_nodes_fill=" + str(self.rho) + " d\n" configStr += "dummy_nodes_c_coef=" + str(self.gamma) + " d\n" configStr += "qcvqcc_lambda_M=" + str(self.lambdaM) + " d\n" configStr += "qcvqcc_lambda_min=1e-3 d\n" configStr += "blast_match=0 i\n" configStr += "blast_match_proj=0 i\n" configStr += "exp_out_file=" + outputFileName + " s\n" configStr += "exp_out_format=Compact Permutation s\n" configStr += "verbose_mode=0 i\n" configStr += "verbose_file=cout s\n" configFile.write(configStr) configFile.close() fnull = open(os.devnull, 'w') home = expanduser("~") argList = [home + "/.local/bin/graphm", configFileName] subprocess.call(argList, stdout=fnull, stderr=fnull) fnull.close() #Next: parse input files outputFile = open(outputFileName, 'r') line = outputFile.readline() line = outputFile.readline() line = outputFile.readline() line = outputFile.readline() graphDistance = float(outputFile.readline().split()[2]) fDistance = float(outputFile.readline().split()[2]) fDistanceExact = float(outputFile.readline().split()[2]) time = float(outputFile.readline().split()[1]) line = outputFile.readline() line = outputFile.readline() permutation = numpy.zeros( max(graph1.getNumVertices(), graph2.getNumVertices()), numpy.int) i = 0 for line in outputFile: permutation[i] = int(line.strip()) - 1 i += 1 #Delete files os.remove(graph1FileName) os.remove(graph2FileName) os.remove(similaritiesFileName) os.remove(configFileName) os.remove(outputFileName) distanceVector = [graphDistance, fDistance, fDistanceExact] return permutation, distanceVector, time