Пример #1
0
eCsvReader = EgoCsvReader()
egoQuestionIds = eCsvReader.getEgoQuestionIds()

pajekWriter = PajekWriter()
simpleGraphWriter = SimpleGraphWriter()
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]))
Пример #2
0
class SvmEgoSimulatorTest(unittest.TestCase):
    def setUp(self):
        logging.basicConfig(stream=sys.stdout, level=logging.INFO)
        dataDir = PathDefaults.getDataDir() + "infoDiffusion/"
        #dataDir = "/home/charanpal/Private/Postdoc/Code/APGL/data/"
        matFileName = dataDir + "EgoAlterTransmissions.mat"
        self.svmEgoSimulator = SvmEgoSimulator(matFileName)

    def testSampleExamples(self):
        self.svmEgoSimulator.sampleExamples(100)

        self.assertEquals(self.svmEgoSimulator.examplesList.getNumSampledExamples(), 100)

    def testModelSelection(self):
        Cs = [1.0, 2.0]
        kernel = "linear"
        kernelParams = [0.0]
        errorCosts = [0.1, 0.2]
        folds = 5
        sampleSize = 1000
        
        CVal, kernelParamVal, errorCost, error = self.svmEgoSimulator.modelSelection(Cs, kernel, kernelParams, errorCosts, folds, sampleSize)

        self.assertTrue(CVal in Cs)
        self.assertTrue(kernelParamVal in kernelParams)
        self.assertTrue(errorCost in errorCosts)
        self.assertTrue(error >= 0.0 and error < 1.0)

    def testEvaluateClassifier(self):
        CVal = 1.0
        kernel = "linear"
        kernelParamVal = 0.0
        errorCost = 0.5
        folds = 6
        sampleSize = 1000
        invert = False

        (means, vars) = self.svmEgoSimulator.evaluateClassifier(CVal, kernel, kernelParamVal, errorCost, folds, sampleSize, invert)


    def testTrainClassifier(self):
        CVal = 1.0
        kernel = "linear"
        kernelParamVal = 0.0
        errorCost = 0.5
        folds = 6
        sampleSize = 1000

        self.svmEgoSimulator.trainClassifier(CVal, kernel, kernelParamVal, errorCost, sampleSize)

    def testGenerateRandomGraph(self):
        egoFileName = PathDefaults.getDataDir() + "infoDiffusion/EgoData.csv"
        alterFileName = PathDefaults.getDataDir()  + "infoDiffusion/AlterData.csv"
        numVertices = 1000
        infoProb = 0.1

        
        p = 0.1
        neighbours = 10
        generator = SmallWorldGenerator(p, neighbours)
        graph = SparseGraph(VertexList(numVertices, 0))
        graph = generator.generate(graph)

        self.svmEgoSimulator.generateRandomGraph(egoFileName, alterFileName, infoProb, graph)

    def testRunSimulation(self):
        egoFileName = PathDefaults.getDataDir() + "infoDiffusion/EgoData.csv"
        alterFileName = PathDefaults.getDataDir()  + "infoDiffusion/AlterData.csv"
        numVertices = 1000
        infoProb = 0.1
        p = 0.1
        neighbours = 10

        generator = SmallWorldGenerator(p, neighbours)
        graph = SparseGraph(VertexList(numVertices, 0))
        graph = generator.generate(graph)
        
        CVal = 1.0
        kernel = "linear"
        kernelParamVal = 0.0
        errorCost = 0.5
        folds = 6
        sampleSize = 1000

        maxIterations = 5

        self.svmEgoSimulator.trainClassifier(CVal, kernel, kernelParamVal, errorCost, sampleSize)
        self.svmEgoSimulator.generateRandomGraph(egoFileName, alterFileName, infoProb, graph)
        self.svmEgoSimulator.runSimulation(maxIterations)
Пример #3
0
#List of the numbers we want to store at each simulation
quantitiesArray = numpy.zeros((simulationRepetitions, maxIterations+1))
genderDistributions = numpy.zeros((simulationRepetitions, maxIterations+3, numGenders))
ageDistributions = numpy.zeros((simulationRepetitions, maxIterations+3, numAges))
educationDistributions = numpy.zeros((simulationRepetitions, maxIterations+3, numEducations))
professionDistributions = numpy.zeros((simulationRepetitions, maxIterations+3, numProfessions))
foodRiskDistributions = numpy.zeros((simulationRepetitions, maxIterations+3, numFoodRisks))
experienceDistributions = numpy.zeros((simulationRepetitions, maxIterations+3, numExperiences))
internetFreqDistributions = numpy.zeros((simulationRepetitions, maxIterations+3, numInternets))

for i in range(0, simulationRepetitions):
    vList = VertexList(numVertices, 0)
    graph = SparseGraph(vList)
    graph = generator.generate(graph)
    simulator.generateRandomGraph(egoFileName, alterFileName, infoProb, graph)
    (totalInfo, transmissions) = simulator.runSimulation(maxIterations)

    alters = []
    egos = numpy.unique(transmissions[0][:, 0])
    quantitiesArray[i, 0] = egos.shape[0]

    for j in range(len(transmissions)):
        alters.append(numpy.unique(transmissions[j][:, 1]))
        quantitiesArray[i, 1+j] = alters[j].shape[0]

    """
    The commented features have very large ranges - we need to bin them appropriately.
    Either that, or ignore them.
    """