def testWriteToFile3(self): """ We will test out writing out some random graphs to Pajek """ numVertices = 20 numFeatures = 0 vList = VertexList(numVertices, numFeatures) graph = SparseGraph(vList) p = 0.1 generator = ErdosRenyiGenerator(p) graph = generator.generate(graph) pw = PajekWriter() directory = PathDefaults.getOutputDir() + "test/" pw.writeToFile(directory + "erdosRenyi20", graph) #Now write a small world graph p = 0.2 k = 3 graph.removeAllEdges() generator = SmallWorldGenerator(p, k) graph = generator.generate(graph) pw.writeToFile(directory + "smallWorld20", graph)
def generateGraph(self, alpha, p, dim): Parameter.checkFloat(alpha, 0.0, float('inf')) Parameter.checkFloat(p, 0.0, 1.0) Parameter.checkInt(dim, 0, float('inf')) numVertices = self.graph.getNumVertices() self.X = numpy.random.rand(numVertices, dim) D = KernelUtils.computeDistanceMatrix(numpy.dot(self.X, self.X.T)) P = numpy.exp(-alpha * D) diagIndices = numpy.array(list(range(0, numVertices))) P[(diagIndices, diagIndices)] = numpy.zeros(numVertices) B = numpy.random.rand(numVertices, numVertices) <= P #Note that B is symmetric - could just go through e.g. upper triangle for i in range(numpy.nonzero(B)[0].shape[0]): v1 = numpy.nonzero(B)[0][i] v2 = numpy.nonzero(B)[1][i] self.graph.addEdge(v1, v2) erdosRenyiGenerator = ErdosRenyiGenerator(p) self.graph = erdosRenyiGenerator.generate(self.graph, False) return self.graph
def testGraphDisplay(self): try: import networkx import matplotlib except ImportError as error: logging.debug(error) return #Show numFeatures = 1 numVertices = 20 vList = VertexList(numVertices, numFeatures) graph = SparseGraph(vList) p = 0.2 generator = ErdosRenyiGenerator(p) graph = generator.generate(graph) logging.debug((graph.getNumEdges())) nxGraph = graph.toNetworkXGraph() nodePositions = networkx.spring_layout(nxGraph) nodesAndEdges = networkx.draw_networkx(nxGraph, pos=nodePositions) ax = matplotlib.pyplot.axes() ax.set_xticklabels([]) ax.set_yticklabels([])
def testMeanSeqScalarStats(self): numFeatures = 1 numVertices = 10 vList = VertexList(numVertices, numFeatures) p = 0.1 generator = ErdosRenyiGenerator(p) numGraphs = 50 graphList = [] subgraphIndices = [list(range(3)), list(range(6)), list(range(10))] for i in range(numGraphs): graph = generator.generate(SparseGraph(vList)) graphList.append((graph, subgraphIndices)) growthStatistics = GraphStatistics() meanStats, stdStats = growthStatistics.meanSeqScalarStats(graphList) #Check some of the stats for i in range(len(subgraphIndices)): self.assertEquals(meanStats[i, growthStatistics.numVerticesIndex], len(subgraphIndices[i])) self.assertEquals(stdStats[i, growthStatistics.numVerticesIndex], 0) self.assertAlmostEquals(meanStats[i, growthStatistics.numEdgesIndex], 0.5*(len(subgraphIndices[i])*(len(subgraphIndices[i])-1))*p, places=0)
def testGenerate2(self): numVertices = 20 graph = SparseGraph(GeneralVertexList(numVertices)) p = 0.2 generator = ErdosRenyiGenerator(p) graph = generator.generate(graph) self.assertTrue((graph.getNumEdges() - p*numVertices*numVertices/2) < 8)
def testLearnModel(self): numVertices = 100 numFeatures = 1 vList = VertexList(numVertices, numFeatures) graph = SparseGraph(vList) p = 0.2 generator = ErdosRenyiGenerator(p) graph = generator.generate(graph) vertexIndices = list(range(0, numVertices)) k = 2 learner = GrowthLearner(k) tol = 10**-1 #Lets test the values of alpha on a series of Erdos-Renyi graphs for i in range(1, 6): p = float(i)/10 graph.removeAllEdges() graph = generator.generate(graph) alpha = learner.learnModel(graph, vertexIndices) logging.debug((numpy.linalg.norm(alpha - numpy.array([p, 0])))) #self.assertTrue(numpy.linalg.norm(alpha - numpy.array([p, 0])) < tol) #Now test the learning on some preferencial attachment graphs ell = 10 m = 8 vertexIndices = list(range(ell, numVertices)) graph.removeAllEdges() generator = BarabasiAlbertGenerator(ell, m) graph = generator.generate(graph) alpha = learner.learnModel(graph, vertexIndices) logging.debug(alpha)
def __init__(self): numVertices = 1000 graph = SparseGraph(GeneralVertexList(numVertices)) p = 0.1 generator = ErdosRenyiGenerator(p) graph = generator.generate(graph) subgraphIndicesList = [] for i in range(100, numVertices, 10): subgraphIndicesList.append(range(i)) k1 = 5 k2 = 100 self.graph = graph self.subgraphIndicesList = subgraphIndicesList self.clusterer = IterativeSpectralClustering(k1, k2, T=10, alg="IASC")
class ErdoRenyiGeneratorTest(unittest.TestCase): def setUp(self): numpy.set_printoptions(suppress=True, linewidth=200, precision=5) self.numVertices = 10; self.numFeatures = 2; self.vList = VertexList(self.numVertices, self.numFeatures) self.graph = SparseGraph(self.vList) self.p = 0.1 self.erg = ErdosRenyiGenerator(self.p) def testGenerate(self): #undirected = True p = 0.0 self.graph.removeAllEdges() self.erg.setP(p) graph = self.erg.generate(self.graph) self.assertEquals(graph.getNumEdges(), 0) undirected = False self.graph = SparseGraph(self.vList, undirected) self.graph.removeAllEdges() self.erg = ErdosRenyiGenerator(p) graph = self.erg.generate(self.graph) self.assertEquals(graph.getNumEdges(), 0) p = 1.0 undirected = True self.graph = SparseGraph(self.vList, undirected) self.graph.removeAllEdges() self.erg = ErdosRenyiGenerator(p) graph = self.erg.generate(self.graph) self.assertEquals(graph.getNumEdges(), (self.numVertices*self.numVertices-self.numVertices)/2) p = 1.0 undirected = False self.graph = SparseGraph(self.vList, undirected) self.graph.removeAllEdges() self.erg = ErdosRenyiGenerator(p) graph = self.erg.generate(self.graph) self.assertEquals(graph.getNumEdges(), self.numVertices*self.numVertices-self.numVertices) self.assertEquals(graph.getEdge(1, 2), 1) self.assertEquals(graph.getEdge(1, 1), None) p = 0.5 numVertices = 1000 numFeatures = 0 vList = VertexList(numVertices, numFeatures) undirected = False self.graph = SparseGraph(vList, undirected) self.erg = ErdosRenyiGenerator(p) graph = self.erg.generate(self.graph) self.assertAlmostEquals(graph.getNumEdges()/float(numVertices**2 - numVertices), p, places=2) p = 0.1 self.graph = SparseGraph(vList, undirected) self.erg = ErdosRenyiGenerator(p) graph = self.erg.generate(self.graph) self.assertAlmostEquals(graph.getNumEdges()/float(numVertices**2 - numVertices), p, places=2) #Test the case in which we have a graph with edges p = 0.5 numVertices = 10 vList = VertexList(numVertices, numFeatures) graph = SparseGraph(vList, undirected) graph.addEdge(0, 1, 5) graph.addEdge(0, 2) graph.addEdge(0, 5, 0.7) graph.addEdge(1, 8) graph.addEdge(2, 9) numEdges = graph.getNumEdges() graph = self.erg.generate(graph, False) self.assertTrue(graph.getNumEdges() > numEdges) @apgl.skip("") def testGraphDisplay(self): try: import networkx import matplotlib except ImportError as error: logging.debug(error) return #Show numFeatures = 1 numVertices = 20 vList = VertexList(numVertices, numFeatures) graph = SparseGraph(vList) p = 0.2 generator = ErdosRenyiGenerator(p) graph = generator.generate(graph) logging.debug((graph.getNumEdges())) nxGraph = graph.toNetworkXGraph() nodePositions = networkx.spring_layout(nxGraph) nodesAndEdges = networkx.draw_networkx(nxGraph, pos=nodePositions) ax = matplotlib.pyplot.axes() ax.set_xticklabels([]) ax.set_yticklabels([]) #matplotlib.pyplot.show() def testGenerate2(self): numVertices = 20 graph = SparseGraph(GeneralVertexList(numVertices)) p = 0.2 generator = ErdosRenyiGenerator(p) graph = generator.generate(graph) self.assertTrue((graph.getNumEdges() - p*numVertices*numVertices/2) < 8) def testErdosRenyiGenerations(self): numVertices = 20 graph = DenseGraph(GeneralVertexList(numVertices)) p = 0.2 generator = ErdosRenyiGenerator(p) graph = generator.generate(graph)
def testErdosRenyiGenerations(self): numVertices = 20 graph = DenseGraph(GeneralVertexList(numVertices)) p = 0.2 generator = ErdosRenyiGenerator(p) graph = generator.generate(graph)
import numpy import scipy.sparse.linalg from scipy.sparse import csr_matrix from pyamg import smoothed_aggregation_solver from apgl.generator.ErdosRenyiGenerator import ErdosRenyiGenerator from apgl.graph.SparseGraph import SparseGraph from apgl.graph.GeneralVertexList import GeneralVertexList numpy.set_printoptions(suppress=True, precision=4, linewidth=100) numpy.random.seed(21) p = 0.001 numVertices = 10000 generator = ErdosRenyiGenerator(p) graph = SparseGraph(GeneralVertexList(numVertices)) graph = generator.generate(graph) print("Num vertices = " + str(graph.getNumVertices())) print("Num edges = " + str(graph.getNumEdges())) L = graph.normalisedLaplacianSym(sparse=True) # L = csr_matrix(L) print("Created Laplacian") # ml = smoothed_aggregation_solver(L) # M = ml.aspreconditioner() start = time.clock() w, V = scipy.sparse.linalg.eigsh(L, k=20) totalTime = time.clock() - start
import struct #from apgl.graph import * from apgl.graph.DenseGraph import DenseGraph from apgl.graph.VertexList import VertexList from apgl.generator.ErdosRenyiGenerator import ErdosRenyiGenerator import numpy if len(sys.argv) != 2: print('Usage {} <num-vertices>'.format(sys.argv[0])) sys.exit(1) # Generate a random graph p = 0.2 graph = DenseGraph(VertexList(int(sys.argv[1]), 1)) generator = ErdosRenyiGenerator(p) graph = generator.generate(graph) numVertices = graph.getNumVertices() numEdges = graph.getNumEdges() print("{} vertices {} edges".format(numVertices, numEdges)) # Write packed adjacency list to a binary file outfile = 'graph' f = open(outfile, "wb") try: f.write(struct.pack('i', numVertices)) f.write(struct.pack('i', numEdges)) index = 0 # Vertices for i in range(numVertices): f.write(struct.pack('i', numVertices + index))