当前位置: 首页>>代码示例>>Python>>正文


Python DictGraph.neighbours方法代码示例

本文整理汇总了Python中apgl.graph.DictGraph.DictGraph.neighbours方法的典型用法代码示例。如果您正苦于以下问题:Python DictGraph.neighbours方法的具体用法?Python DictGraph.neighbours怎么用?Python DictGraph.neighbours使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在apgl.graph.DictGraph.DictGraph的用法示例。


在下文中一共展示了DictGraph.neighbours方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: testGetNeighbours

# 需要导入模块: from apgl.graph.DictGraph import DictGraph [as 别名]
# 或者: from apgl.graph.DictGraph.DictGraph import neighbours [as 别名]
    def testGetNeighbours(self):
        dictGraph = DictGraph(True)
        dictGraph.addEdge(1, 2, 12)
        dictGraph.addEdge(1, 3, 18)
        dictGraph.addEdge(1, 4, 1)
        dictGraph.addEdge(3, 4, 1)
        dictGraph.addEdge(2, 2, 1)
        dictGraph.setVertex(5, 12)

        self.assertEquals(dictGraph.neighbours(1), [2, 3, 4])
        self.assertEquals(dictGraph.neighbours(3), [1, 4])
        self.assertEquals(dictGraph.neighbours(2), [1, 2])
        self.assertEquals(dictGraph.neighbours(5), [])

        #Directed graphs
        dictGraph = DictGraph(False)
        dictGraph.addEdge(1, 2, 12)
        dictGraph.addEdge(1, 3, 18)
        dictGraph.addEdge(1, 4, 1)
        dictGraph.addEdge(3, 4, 1)
        dictGraph.addEdge(2, 2, 1)
        dictGraph.setVertex(5, 12)

        self.assertEquals(dictGraph.neighbours(1), [2,3,4])
        self.assertEquals(dictGraph.neighbours(3), [4])
        self.assertEquals(dictGraph.neighbours(2), [2])
        self.assertEquals(dictGraph.neighbours(5), [])
开发者ID:charanpald,项目名称:APGL,代码行数:29,代码来源:DictGraphTest.py

示例2: testNeighbourOf

# 需要导入模块: from apgl.graph.DictGraph import DictGraph [as 别名]
# 或者: from apgl.graph.DictGraph.DictGraph import neighbours [as 别名]
    def testNeighbourOf(self):
        graph = DictGraph(True)

        graph.addEdge(0, 1)
        graph.addEdge(0, 2)
        graph.addEdge(0, 3)
        graph.addEdge(1, 2)
        graph.addEdge(2, 3)

        for i in range(4):
            self.assertEquals(graph.neighbours(i), graph.neighbourOf(i))

        #Now test directed graph
        graph = DictGraph(False)

        graph.addEdge(0, 1)
        graph.addEdge(0, 2)
        graph.addEdge(0, 3)
        graph.addEdge(1, 2)
        graph.addEdge(2, 3)

        self.assertEquals(graph.neighbourOf(0), [])
        self.assertEquals(graph.neighbourOf(1), [0])
        self.assertEquals(graph.neighbourOf(2), [0,1])
        self.assertEquals(graph.neighbourOf(3), [0, 2])
开发者ID:charanpald,项目名称:APGL,代码行数:27,代码来源:DictGraphTest.py

示例3: __init__

# 需要导入模块: from apgl.graph.DictGraph import DictGraph [as 别名]
# 或者: from apgl.graph.DictGraph.DictGraph import neighbours [as 别名]
    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 
开发者ID:charanpald,项目名称:wallhack,代码行数:96,代码来源:CitationIterGenerator.py

示例4: testGetIterator

# 需要导入模块: from apgl.graph.DictGraph import DictGraph [as 别名]
# 或者: from apgl.graph.DictGraph.DictGraph import neighbours [as 别名]
    def testGetIterator(self):
        generator = CitationIterGenerator()
        iterator = generator.getIterator()

        lastW = iterator.next()

        for W in iterator:
            self.assertTrue((W-W.T).getnnz() == 0)
            self.assertTrue((lastW - W[0:lastW.shape[0], 0:lastW.shape[0]]).getnnz() ==0  )
            lastW = W

        numVertices = W.shape[0]

        #Now compute the vertexIds manually:
        dataDir = PathDefaults.getDataDir() + "cluster/"
        edgesFilename = dataDir + "Cit-HepTh.txt"
        dateFilename = dataDir + "Cit-HepTh-dates.txt"

        #We can't load in numbers using numpy since some may start with zero 
        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([int("1" + vertex1), int("1" + vertex2)])

        edges = numpy.array(edges, numpy.int)

        #Check file read correctly
        self.assertTrue((edges[0, :] == numpy.array([11001, 19304045])).all())
        self.assertTrue((edges[1, :] == numpy.array([11001, 19308122])).all())
        self.assertTrue((edges[9, :] == numpy.array([11001, 19503124])).all())
        vertexIds1 = numpy.unique(edges)
        logging.info("Number of graph vertices: " + str(vertexIds1.shape[0]))

        file = open(dateFilename, 'r')
        file.readline()
        vertexIds2 = []

        for line in file:
            (id, sep, date) = line.partition("\t")
            id = id.strip()
            date = date.strip()
            vertexIds2.append(int("1" + id))

        #Check file read correctly 
        vertexIds2 = numpy.array(vertexIds2, numpy.int)
        self.assertTrue((vertexIds2[0:10] == numpy.array([19203201, 19203202, 19203203, 19203204, 19203205, 19203206, 19203207, 19203208, 19203209, 19203210], numpy.int)).all())
        vertexIds2 = numpy.unique(numpy.array(vertexIds2, numpy.int))

        graph = DictGraph(False)
        graph.addEdges(edges)

        #Find the set of vertices with known citation
        vertices = []
        vertexId2Set = set(vertexIds2.tolist())
        for i in graph.getAllVertexIds():
            Util.printIteration(i, 50000, edges.shape[0])
            if i in vertexId2Set:
                vertices.append(i)
                vertices.extend(graph.neighbours(i))

        logging.debug("Number of final vertices: " + str(numVertices))
        numVertices2 = numpy.unique(numpy.array(vertices)).shape[0]
        self.assertEquals(numVertices, numVertices2)

        #Now compare the weight matrices using the undirected graph
        #Note the order of vertices is different from the iterator 
        graph = DictGraph()
        graph.addEdges(edges)
        subgraph = graph.subgraph(numpy.unique(numpy.array(vertices)))
        W2 = subgraph.getSparseWeightMatrix()

        self.assertEquals(W.getnnz(), W2.getnnz())
开发者ID:malcolmreynolds,项目名称:APGL,代码行数:82,代码来源:CitationIterGeneratorTest.py

示例5: DatedPurchasesGraphListIterator

# 需要导入模块: from apgl.graph.DictGraph import DictGraph [as 别名]
# 或者: from apgl.graph.DictGraph.DictGraph import neighbours [as 别名]
class DatedPurchasesGraphListIterator(object):
    def __init__(self, purchasesByWeek, nb_purchases_per_it=None):
        """
        The background graph is a bi-partite graph of purchases. Purchases are
        grouped by date (week by week), and we consider the graph of purchases
        between first week and $i$-th week.
        The returned graph considers only users and counts the number of common
        purchases between two users.
        Purchases are given in a list of [user, prod, week, year] with increasing
        date.
        nb_purchases_per_it is the maximum number of purchases to put in each
        week (if there is more, randomly split the week). None corresponds to
        no-limit case.
        """
        # args
        self.group_by_iterator = DatedPurchasesGroupByIterator(purchasesByWeek, nb_purchases_per_it)

        # init variables
        self.dictUser = MyDictionary()
        self.dictProd = MyDictionary()
        for user, prod, week, year in purchasesByWeek:
            self.dictUser.index(user)
            self.dictProd.index(prod)
        self.backgroundGraph = DictGraph(False) # directed
        self.W = scipy.sparse.csr_matrix((len(self.dictUser), len(self.dictUser)), dtype='int16')
        self.usefullEdges = numpy.array([])

    def __iter__(self):
         return self

    def __next__(self):
        # next group of purchases (StopIteration is raised here)
        purchases_sublist = next(self.group_by_iterator)
        #logging.debug(" nb purchases: " + str(len(purchases_sublist)))

        # to check that the group really induces new edges
        W_has_changed = False

        # update graphs adding current-week purchases
        for user, prod, week, year in purchases_sublist:
            # update only if this purchase is seen for the first time
            try:
                newEdge = not self.backgroundGraph.getEdge(prod, user)
            except ValueError:
                newEdge = True
            if newEdge:
                self.backgroundGraph.addEdge(prod, user)
                newCommonPurchases = self.backgroundGraph.neighbours(prod)
#                print prod, newCommonPurchases
                for neighbour in filter(lambda neighbour: neighbour != user, newCommonPurchases):
                    W_has_changed = True
                    self.W[neighbour, user] += 1
                    self.W[user, neighbour] += 1
        
        # the returned graph will be restricted to usefull edges
        currentUsefullEdges = numpy.array(self.W.sum(1)).ravel().nonzero()[0]
        newUsefullEdges = numpy.setdiff1d(currentUsefullEdges, self.usefullEdges)
        self.usefullEdges = numpy.r_[self.usefullEdges, newUsefullEdges]
        
        if W_has_changed:
          return self.W[self.usefullEdges,:][:,self.usefullEdges]
        else:
          return next(self)
    next = __next__ 
开发者ID:charanpald,项目名称:sandbox,代码行数:66,代码来源:GraphIterators.py


注:本文中的apgl.graph.DictGraph.DictGraph.neighbours方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。