def testExpandIntArray(self): v = numpy.array([1, 3, 2, 4], numpy.int) w = Util.expandIntArray(v) self.assertTrue((w == numpy.array([0,1,1,1,2,2,3,3,3,3], numpy.int)).all()) v = numpy.array([], numpy.int) w = Util.expandIntArray(v) self.assertTrue((w == numpy.array([], numpy.int)).all())
def generate(self, graph, requireEmpty=True): ''' Create an Configuration Model graph. Note the the degree sequence(s) given in the constructor cannot be guarenteed. The algorithm randomly selects two free "spokes" and then tried to connect them. If two vertices are already connected the corresponding spokes are not used again. In the case that requireEmpty is False then a non-empty graph can be used and the given degree sequence(s) is(are) the difference(s) in degrees between the output graph and input one. :param graph: a graph to populate with edges :type graph: :class:`apgl.graph.AbstractMatrixGraph` :param requireEmpty: if this is set to true then we require an empty graph. :type requireEmpty: :class:`bool` :returns: The modified input graph. ''' Parameter.checkClass(graph, AbstractMatrixGraph) if requireEmpty and graph.getNumEdges() != 0: raise ValueError("Graph must have no edges") if graph.getNumVertices() != self.outDegSequence.shape[0]: raise ValueError( "Graph must have same number of vertices as degree sequence") if self.getInDegSequence() != None and graph.isUndirected(): raise ValueError( "In-degree sequence must be used in conjunction with directed graphs" ) if self.getInDegSequence() == None: expandedInds = Util.expandIntArray(self.outDegSequence) numpy.random.shuffle(expandedInds) for i in range(0, len(expandedInds), 2): if i != len(expandedInds) - 1: graph.addEdge(expandedInds[i], expandedInds[i + 1]) else: expandedOutInds = Util.expandIntArray(self.outDegSequence) expandedInInds = Util.expandIntArray(self.inDegSequence) numpy.random.shuffle(expandedOutInds) numpy.random.shuffle(expandedInInds) for i in range( numpy.min( numpy.array([ expandedOutInds.shape[0], expandedInInds.shape[0] ]))): graph.addEdge(expandedOutInds[i], expandedInInds[i]) return graph
def generate(self, graph, requireEmpty=True): """ Create an Configuration Model graph. Note the the degree sequence(s) given in the constructor cannot be guarenteed. The algorithm randomly selects two free "spokes" and then tried to connect them. If two vertices are already connected the corresponding spokes are not used again. In the case that requireEmpty is False then a non-empty graph can be used and the given degree sequence(s) is(are) the difference(s) in degrees between the output graph and input one. :param graph: a graph to populate with edges :type graph: :class:`apgl.graph.AbstractMatrixGraph` :param requireEmpty: if this is set to true then we require an empty graph. :type requireEmpty: :class:`bool` :returns: The modified input graph. """ Parameter.checkClass(graph, AbstractMatrixGraph) if requireEmpty and graph.getNumEdges() != 0: raise ValueError("Graph must have no edges") if graph.getNumVertices() != self.outDegSequence.shape[0]: raise ValueError("Graph must have same number of vertices as degree sequence") if self.getInDegSequence() != None and graph.isUndirected(): raise ValueError("In-degree sequence must be used in conjunction with directed graphs") if self.getInDegSequence() == None: expandedInds = Util.expandIntArray(self.outDegSequence) numpy.random.shuffle(expandedInds) for i in range(0, len(expandedInds), 2): if i != len(expandedInds) - 1: graph.addEdge(expandedInds[i], expandedInds[i + 1]) else: expandedOutInds = Util.expandIntArray(self.outDegSequence) expandedInInds = Util.expandIntArray(self.inDegSequence) numpy.random.shuffle(expandedOutInds) numpy.random.shuffle(expandedInInds) for i in range(numpy.min(numpy.array([expandedOutInds.shape[0], expandedInInds.shape[0]]))): graph.addEdge(expandedOutInds[i], expandedInInds[i]) return graph
We simulate a network of sexual contacts and see if we can approximate the degree using a subset of the edges. """ import numpy from apgl.viroscopy.model.HIVGraph import HIVGraph from apgl.util.Util import Util numpy.set_printoptions(linewidth=200, threshold=10000) numVertices = 500 graph = HIVGraph(numVertices) T = 100 t = 0 expandedDegreeSeq = Util.expandIntArray(graph.outDegreeSequence()) expandedDegreeSeq = numpy.arange(numVertices) contactRate = 0.01 tau = 1.0 infectedList = range(10) #infectedList = range(numVertices) while (t < T): contactRatesMatrix = numpy.zeros((len(infectedList), numVertices)) #Select a random contact based on the degree edsInds = numpy.random.randint(0, expandedDegreeSeq.shape[0], len(infectedList)) #Choose between a contact sampled from those with edges and one from random