p = ps[i] k = ks[i] outputDirectory = PathDefaults.getOutputDir() baseFileName = outputDirectory + "InfoGraph" + graphType graph = simulator.generateRandomGraph(egoFileName, alterFileName, numVertices, infoProb, graphType, p, k) #Notice that the data is preprocessed in the same way as the survey data egoSimulator = EgoSimulator(graph, classifier, preprocessor) totalInfo = numpy.zeros(maxIterations+1) totalInfo[0] = EgoUtils.getTotalInformation(graph) logging.info("Total number of people with information: " + str(totalInfo[0])) logging.info("--- Simulation Started ---") for i in range(0, maxIterations): logging.info("--- Iteration " + str(i) + " ---") graph = egoSimulator.advanceGraph() totalInfo[i+1] = EgoUtils.getTotalInformation(graph) logging.info("Total number of people with information: " + str(totalInfo[i+1])) transmissionGraph = egoSimulator.getTransmissionGraph() pajekWriter.writeToFile(baseFileName, transmissionGraph) vertexWriter.writeToFile(baseFileName, transmissionGraph) simpleGraphWriter.writeToFile(baseFileName, transmissionGraph) logging.info("--- Simulation Finished ---")
def testAdvanceGraph2(self): #Create a simple graph and deterministic classifier numExamples = 10 numFeatures = 3 #Here, the first element is gender (say) with female = 0, male = 1 vList = VertexList(numExamples, numFeatures) vList.setVertex(0, numpy.array([0,0,1])) vList.setVertex(1, numpy.array([1,0,0])) vList.setVertex(2, numpy.array([1,0,0])) vList.setVertex(3, numpy.array([1,0,0])) vList.setVertex(4, numpy.array([0,0,1])) vList.setVertex(5, numpy.array([0,0,1])) vList.setVertex(6, numpy.array([0,0,0])) vList.setVertex(7, numpy.array([1,0,0])) vList.setVertex(8, numpy.array([0,0,1])) vList.setVertex(9, numpy.array([1,0,0])) sGraph = SparseGraph(vList) sGraph.addEdge(0, 1, 1) sGraph.addEdge(0, 2, 1) sGraph.addEdge(0, 3, 1) sGraph.addEdge(4, 5, 1) sGraph.addEdge(4, 6, 1) sGraph.addEdge(6, 7, 1) sGraph.addEdge(6, 8, 1) sGraph.addEdge(6, 9, 1) simulator = EgoSimulator(sGraph, self.dc) simulator.advanceGraph() self.assertEquals(simulator.getNumIterations(), 1) self.assertEquals(sGraph.getVertex(0)[numFeatures-1], 1) self.assertEquals(sGraph.getVertex(1)[numFeatures-1], 1) self.assertEquals(sGraph.getVertex(2)[numFeatures-1], 1) self.assertEquals(sGraph.getVertex(3)[numFeatures-1], 1) self.assertEquals(sGraph.getVertex(4)[numFeatures-1], 1) self.assertEquals(sGraph.getVertex(5)[numFeatures-1], 1) self.assertEquals(sGraph.getVertex(6)[numFeatures-1], 1) self.assertEquals(sGraph.getVertex(7)[numFeatures-1], 0) self.assertEquals(sGraph.getVertex(8)[numFeatures-1], 1) self.assertEquals(sGraph.getVertex(9)[numFeatures-1], 0) #Advance again and all egos have information simulator.advanceGraph() self.assertEquals(simulator.getNumIterations(), 2) self.assertEquals(sGraph.getVertex(0)[numFeatures-1], 1) self.assertEquals(sGraph.getVertex(1)[numFeatures-1], 1) self.assertEquals(sGraph.getVertex(2)[numFeatures-1], 1) self.assertEquals(sGraph.getVertex(3)[numFeatures-1], 1) self.assertEquals(sGraph.getVertex(4)[numFeatures-1], 1) self.assertEquals(sGraph.getVertex(5)[numFeatures-1], 1) self.assertEquals(sGraph.getVertex(6)[numFeatures-1], 1) self.assertEquals(sGraph.getVertex(7)[numFeatures-1], 1) self.assertEquals(sGraph.getVertex(8)[numFeatures-1], 1) self.assertEquals(sGraph.getVertex(9)[numFeatures-1], 1) #Should be no change simulator.advanceGraph() self.assertEquals(simulator.getNumIterations(), 3) #Check the correct alters are added at each step self.assertTrue((simulator.getAlters(0) == numpy.array([1,2,3,6])).all()) self.assertTrue((simulator.getAlters(1) == numpy.array([7,9])).all()) self.assertTrue((simulator.getAlters(2) == numpy.array([])).all()) #Check that the transmission graph is okay transGraph = simulator.getTransmissionGraph() self.assertEquals(transGraph.getNumVertices(), 9) self.assertEquals(transGraph.getNumEdges(), 7) self.assertEquals(transGraph.getAllVertexIds(), [0, 1, 2, 3, 4, 6, 7, 8, 9]) for i in transGraph.getAllVertexIds(): self.assertTrue((transGraph.getVertex(i) == sGraph.getVertex(i)).all())