コード例 #1
0
 def testAdvanceGraph(self):    
     totalInfo = EgoUtils.getTotalInformation(self.sGraph)
             
     self.sGraph = self.egoSimulator.advanceGraph()     
     totalInfo2 = EgoUtils.getTotalInformation(self.sGraph)
     
     #Test that the number of people who know information is the same or more 
     self.assertTrue(totalInfo2 >= totalInfo)
コード例 #2
0
ファイル: EgoUtilsTest.py プロジェクト: malcolmreynolds/APGL
 def testGraphFromMatFile(self):
     matFileName = PathDefaults.getDataDir() +  "infoDiffusion/EgoAlterTransmissions1000.mat"
     sGraph = EgoUtils.graphFromMatFile(matFileName)
     
     examplesList = ExamplesList.readFromMatFile(matFileName)
     numFeatures = examplesList.getDataFieldSize("X", 1)
     
     self.assertEquals(examplesList.getNumExamples(), sGraph.getNumEdges())
     self.assertEquals(examplesList.getNumExamples()*2, sGraph.getNumVertices())
     self.assertEquals(numFeatures/2+1, sGraph.getVertexList().getNumFeatures())
     
     #Every even vertex has information, odd does not 
     for i in range(0, sGraph.getNumVertices()): 
         vertex = sGraph.getVertex(i)
         
         if i%2 == 0: 
             self.assertEquals(vertex[sGraph.getVertexList().getNumFeatures()-1], 1)
         else: 
             self.assertEquals(vertex[sGraph.getVertexList().getNumFeatures()-1], 0)
             
     #Test the first few vertices are the same 
     for i in range(0, 10): 
         vertex1 = sGraph.getVertex(i*2)[0:numFeatures/2]
         vertex2 = sGraph.getVertex(i*2+1)[0:numFeatures/2]
         vertexEx1 = examplesList.getSubDataField("X", numpy.array([i])).ravel()[0:numFeatures/2]
         vertexEx2 = examplesList.getSubDataField("X", numpy.array([i])).ravel()[numFeatures/2:numFeatures]
         
         self.assertTrue((vertex1 == vertexEx1).all())
         self.assertTrue((vertex2 == vertexEx2).all())
コード例 #3
0
ファイル: EgoUtilsTest.py プロジェクト: malcolmreynolds/APGL
    def testAverageHopDistance(self):
        transmissions = [numpy.array([[0, 1], [3, 4]])]

        self.assertEquals(EgoUtils.averageHopDistance(transmissions), 1)

        transmissions = []
        transmissions.append(numpy.array([[0, 1], [3, 4]]))
        transmissions.append(numpy.array([[4, 5]]))

        self.assertEquals(EgoUtils.averageHopDistance(transmissions), 1.5)

        transmissions.append(numpy.array([[5, 6]]))

        self.assertEquals(EgoUtils.averageHopDistance(transmissions), 2)

        transmissions.append(numpy.array([[2, 8]]))

        self.assertEquals(EgoUtils.averageHopDistance(transmissions), 5.0/3)

        #Try an example with two people sending to one node
        transmissions = []
        transmissions.append(numpy.array([[0, 1], [2, 1]]))
        transmissions.append(numpy.array([[1, 5]]))

        self.assertEquals(EgoUtils.averageHopDistance(transmissions), 1.5)

        #Try a straight line
        transmissions = []
        transmissions.append(numpy.array([[0, 1]]))
        transmissions.append(numpy.array([[1, 2]]))
        transmissions.append(numpy.array([[2, 3]]))

        self.assertEquals(EgoUtils.averageHopDistance(transmissions), 3)

        #A branch
        transmissions.append(numpy.array([[2, 4]]))

        self.assertEquals(EgoUtils.averageHopDistance(transmissions), 4)

        #Try empty transmissions
        transmissions = []

        self.assertEquals(EgoUtils.averageHopDistance(transmissions), 0)
コード例 #4
0
ファイル: EgoUtilsTest.py プロジェクト: malcolmreynolds/APGL
    def testReceiversPerSender(self):
        #Try empty transmissions
        transmissions = []
        self.assertEquals(EgoUtils.receiversPerSender(transmissions), 0)

        transmissions = [numpy.array([[0, 1], [3, 4]])]
        self.assertEquals(EgoUtils.receiversPerSender(transmissions), 1)

        transmissions = [numpy.array([[0, 1], [0, 2]])]
        self.assertEquals(EgoUtils.receiversPerSender(transmissions), 2)

        transmissions = [numpy.array([[0, 1], [0, 2], [1, 3]])]
        self.assertEquals(EgoUtils.receiversPerSender(transmissions), 1.5)

        transmissions = [numpy.array([[0, 1], [0, 2], [1, 3]]), numpy.array([[0, 5]])]
        self.assertEquals(EgoUtils.receiversPerSender(transmissions), 2)

        transmissions = [numpy.array([[0, 2], [1, 2]])]
        self.assertEquals(EgoUtils.receiversPerSender(transmissions), 0.5)
コード例 #5
0
ファイル: WritePajek.py プロジェクト: malcolmreynolds/APGL
vertexWriter = CsvVertexWriter()

for i in range(len(graphTypes)):
    graphType = graphTypes[i]
    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 ---")
コード例 #6
0
    def testAdvanceGraph3(self):
        """ 
        This test will learn from a set of ego and alter pairs, then we will make predictions on 
        the pairs and see the results. The we test if the same results are present in a simulation.  
        """
        dataDir = PathDefaults.getDataDir() + "infoDiffusion/"
        matFileName = dataDir +  "EgoAlterTransmissions1000.mat"
        examplesList = ExamplesList.readFromMatFile(matFileName)
        examplesList.setDefaultExamplesName("X")
        examplesList.setLabelsName("y")
        
        logging.debug(("Number of y = +1: " + str(sum(examplesList.getSampledDataField("y") == 1))))
        logging.debug(("Number of y = -1: " + str(sum(examplesList.getSampledDataField("y") == -1))))
        
        #Standardise the examples 
        preprocessor = Standardiser()
        X = examplesList.getDataField(examplesList.getDefaultExamplesName())
        X = preprocessor.standardiseArray(X)
        examplesList.overwriteDataField(examplesList.getDefaultExamplesName(), X)
        
        classifier = MlpySVM(kernel='linear', kp=1, C=32.0)

        y = examplesList.getDataField("y")
        classifier.learnModel(X, y)
        predY = classifier.classify(X)
        logging.debug(("Number of y = +1: " + str(sum(examplesList.getSampledDataField("y") == 1))))
        logging.debug(("Number of y = -1: " + str(sum(examplesList.getSampledDataField("y") == -1))))

        sampledY = examplesList.getSampledDataField(examplesList.getLabelsName()).ravel()

        error = mlpy.err(sampledY, predY)
        sensitivity = mlpy.sens(sampledY, predY)
        specificity = mlpy.spec(sampledY, predY)
        errorP = mlpy.errp(sampledY, predY)
        errorN = mlpy.errn(sampledY, predY)
        
        logging.debug("--- Classification evaluation ---")
        logging.debug(("Error on " + str(examplesList.getNumExamples()) + " examples is " + str(error)))
        logging.debug(("Sensitivity (recall = TP/(TP+FN)): " + str(sensitivity)))
        logging.debug(("Specificity (TN/TN+FP): "  + str(specificity)))
        logging.debug(("Error on positives: "  + str(errorP)))
        logging.debug(("Error on negatives: "  + str(errorN)))
        
        sGraph = EgoUtils.graphFromMatFile(matFileName)

        #Notice that the data is preprocessed in the same way as the survey data 
        egoSimulator = EgoSimulator(sGraph, classifier, preprocessor)
        
        totalInfo = EgoUtils.getTotalInformation(sGraph)
        logging.debug(("Total number of people with information: " + str(totalInfo)))
        self.assertEquals(totalInfo, 1000)
        
        sGraph = egoSimulator.advanceGraph()
        
        totalInfo = EgoUtils.getTotalInformation(sGraph)
        logging.debug(("Total number of people with information: " + str(totalInfo)))
        self.assertEquals(totalInfo, 1000 + sum(predY == 1))
        
        altersList = egoSimulator.getAlters(0)
        predictedAlters = numpy.nonzero(predY == 1)[0]
        
        self.assertTrue((altersList == predictedAlters*2+1).all())