Пример #1
0
    def __next__(self):
        
        if self.emit == False:
            # We need a "do while" loop here to manage situations where self.graphIterator is already exhausted
            while True:
                W = next(self.graphIterator)
                graph = SparseGraph(W.shape[0], W=W)
                numComponents = len(graph.findConnectedComponents())
                if __debug__:
                    logging.debug("graph size = " + str(graph.size) + " num components = " + str(numComponents))
                
                if numComponents < self.maxComponents: 
#                    self.emit = True 
                    break
        else:
            W = self.graphIterator.next()           
            
        return W
Пример #2
0
    def __next__(self):

        if self.emit == False:
            # We need a "do while" loop here to manage situations where self.graphIterator is already exhausted
            while True:
                W = next(self.graphIterator)
                graph = SparseGraph(W.shape[0], W=W)
                numComponents = len(graph.findConnectedComponents())
                if __debug__:
                    logging.debug("graph size = " + str(graph.size) +
                                  " num components = " + str(numComponents))

                if numComponents < self.maxComponents:
                    #                    self.emit = True
                    break
        else:
            W = self.graphIterator.next()

        return W
Пример #3
0
W = scipy.sparse.csr_matrix(createDataset(sigma=1.5))
nystromNs = numpy.arange(20, 151, 10) 
k = 2

errors = numpy.zeros((len(nystromNs), numMethods))  
innerProds = numpy.zeros((len(nystromNs), numMethods))  

L = GraphUtils.shiftLaplacian(W)
L2 = GraphUtils.normalisedLaplacianSym(W)

print(L2.todense())

#Find connected components 
graph = SparseGraph(GeneralVertexList(W.shape[0]))
graph.setWeightMatrix(W)
components = graph.findConnectedComponents()
print(len(components)) 

#Compute exact eigenvalues 
omega, Q = numpy.linalg.eigh(L.todense())
inds = numpy.flipud(numpy.argsort(omega)) 
omega, Q = omega[inds], Q[:, inds]
omegak, Qk = omega[0:k], Q[:, 0:k]    

print(omega)
print(Q)

omegaHat, Qhat = numpy.linalg.eigh(L2.todense())
inds = numpy.argsort(omegaHat)
omegaHat, Qhat = omegaHat[inds], Qhat[:, inds]
omegaHatk, Qhatk = omegaHat[0:k], Qhat[:, 0:k] 
Пример #4
0
class CitationIterGenerator(object):
    """
    A class to load the high energy physics data and generate an iterator. The 
    dataset is found in http://snap.stanford.edu/data/cit-HepTh.html
    """
    def __init__(self, minGraphSize=500, maxGraphSize=None, dayStep=30):
        
        dataDir = PathDefaults.getDataDir() + "cluster/"
        edgesFilename = dataDir + "Cit-HepTh.txt"
        dateFilename = dataDir + "Cit-HepTh-dates.txt"

        #Note the IDs are integers but can start with zero so we prefix "1" to each ID 
        edges = []
        file = open(edgesFilename, 'r')
        file.readline()
        file.readline()
        file.readline()
        file.readline()

        for line in file:
            (vertex1, sep, vertex2) = line.partition("\t")
            vertex1 = vertex1.strip()
            vertex2 = vertex2.strip()
            edges.append([vertex1, vertex2])
            
            #if vertex1 == vertex2: 
            #    print(vertex1)

        file.close()

        logging.info("Loaded edge file " + str(edgesFilename) + " with " + str(len(edges)) + " edges")

        #Keep an edge graph 
        graph = DictGraph(False)
        graph.addEdges(edges)
        logging.info("Created directed citation graph with " + str(graph.getNumEdges()) + " edges and " + str(graph.getNumVertices()) + " vertices")

        #Read in the dates articles appear in a dict which used the year and month
        #as the key and the value is a list of vertex ids. For each month we include
        #all papers uploaded that month and those directed cited by those uploads. 
        startDate = datetime.date(1990, 1, 1)

        file = open(dateFilename, 'r')
        file.readline()
        numLines = 0 
        subgraphIds = []

        for line in file:
            (id, sep, date) = line.partition("\t")
            id = id.strip()
            date = date.strip()
            

            inputDate = datetime.datetime.strptime(date.strip(), "%Y-%m-%d")
            inputDate = inputDate.date()

            if graph.vertexExists(id):
                tDelta = inputDate - startDate
                            
                graph.vertices[id] = tDelta.days 
                subgraphIds.append(id)
                
                #If a paper cites another, it must have been written before 
                #the citing paper - enforce this rule. 
                for neighbour in graph.neighbours(id): 
                    if graph.getVertex(neighbour) == None: 
                        graph.setVertex(neighbour, tDelta.days) 
                        subgraphIds.append(neighbour)
                    elif tDelta.days < graph.getVertex(neighbour): 
                        graph.setVertex(neighbour, tDelta.days) 
                        
            numLines += 1 
            
        file.close()
        
        subgraphIds = set(subgraphIds)
        graph = graph.subgraph(list(subgraphIds))
        logging.debug(graph)
        logging.info("Loaded date file " + str(dateFilename) + " with " + str(len(subgraphIds)) + " dates and " + str(numLines) + " lines")

        W = graph.getSparseWeightMatrix()
        W = W + W.T
        
        vList = VertexList(W.shape[0], 1)
        vList.setVertices(numpy.array([graph.getVertices(graph.getAllVertexIds())]).T)
        
        #Note: we have 16 self edges and some two-way citations so this graph has fewer edges than the directed one 
        self.graph = SparseGraph(vList, W=W)
        logging.debug(self.graph)
        
        #Now pick the max component 
        components = self.graph.findConnectedComponents()
        self.graph = self.graph.subgraph(components[0])
        
        logging.debug("Largest component graph: " + str(self.graph))
        
        self.minGraphSize = minGraphSize
        self.maxGraphSize = maxGraphSize 
        self.dayStep = dayStep 
        
    def getIterator(self):
        """
        Return an iterator which outputs the citation graph for each month. Note
        that the graphs are undirected but we make them directed.
        """
        vertexArray = self.graph.getVertexList().getVertices()
        dates = vertexArray[:, 0]
        firstVertex = numpy.argmin(dates)
        
        dayList = range(int(numpy.min(dates)), int(numpy.max(dates)), self.dayStep)
        dayList.append(numpy.max(dates))
        
        subgraphIndicesList = []

        #Generate subgraph indices list 
        for i in dayList:
            subgraphIndices = numpy.nonzero(dates <= i)[0]
            
            #Check subgraphIndices are sorted 
            subgraphIndices = numpy.sort(subgraphIndices)
            currentSubgraph = self.graph.subgraph(subgraphIndices)
            compIndices = currentSubgraph.depthFirstSearch(list(subgraphIndices).index(firstVertex))
            subgraphIndices =  subgraphIndices[compIndices]        
            
            if self.maxGraphSize != None and subgraphIndices.shape[0] > self.maxGraphSize: 
                break 
            
            print(subgraphIndices.shape[0])
            
            if subgraphIndices.shape[0] >= self.minGraphSize: 
                subgraphIndicesList.append(subgraphIndices)
                
        iterator = IncreasingSubgraphListIterator(self.graph, subgraphIndicesList)
        return iterator 
Пример #5
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)