Example #1
0
    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
Example #2
0
    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
Example #3
0
"""
Name: Generate Graph:
Author: Jia_qiu Wang(王佳秋)
Data: December, 2016
function:
"""

from apgl.graph import GeneralVertexList, SparseGraph
import numpy

numVertices = 5  # 顶点个数
graph = SparseGraph(numVertices)  # 具有5个顶点个数的图数据结构
graph[0, 1] = 1
graph[0, 2] = 3
graph[1, 2] = 0.1
graph[3, 4] = 2
graph.setVertex(0, "abc")
graph.setVertex(1, 123)
print(graph.findConnectedComponents())  # 输出联通分量

print(graph.getWeightMatrix())  # 输出图的邻接矩阵

# print(graph.degreeDistribution())

print(graph.neighbours(0))

print(graph)