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)
Beispiel #2
0
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 ---")
    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())