Example #1
0
import unittest
import numpy
import apgl


@apgl.skipIf(not apgl.checkImport('pysparse'), 'No module pysparse')
class PySparseUtilsTest(unittest.TestCase):
    def testSum(self):
        try:
            from pysparse import spmatrix
            from apgl.util.PySparseUtils import PySparseUtils
        except ImportError as error:
            return

        n = 10
        X = spmatrix.ll_mat(n, n)

        self.assertEquals(PySparseUtils.sum(X), 0.0)

        X[1, 1] = 5
        X[2, 4] = 6.1
        X[3, 1] = 2.5

        self.assertEquals(PySparseUtils.sum(X), 13.6)

    def testNonzero(self):
        try:
            from pysparse import spmatrix
            from apgl.util.PySparseUtils import PySparseUtils
        except ImportError as error:
            return
from apgl.util.PathDefaults import PathDefaults

import unittest
import logging
import sys
import apgl
from apgl.egograph.SvmEgoSimulator import SvmEgoSimulator
from apgl.generator import *
from apgl.graph import * 

@apgl.skipIf(not apgl.checkImport('svm'), 'No module svm')
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
Example #3
0
import apgl
import numpy 
import unittest
import pickle 
import numpy.testing as nptst 

from exp.viroscopy.model.HIVGraph import HIVGraph
from exp.viroscopy.model.HIVVertices import HIVVertices

@apgl.skipIf(not apgl.checkImport('sppy'), 'No module pysparse')
class  HIVGraphTest(unittest.TestCase):
    def setup(self):
        pass

    def testContructor(self):
        numVertices = 10
        graph = HIVGraph(numVertices)

        
        self.assertEquals(numVertices, graph.getNumVertices())
        self.assertEquals(8, graph.getVertexList().getNumFeatures())
        self.assertTrue(graph.isUndirected() == True)

    def testGetSusceptibleSet(self):
        numVertices = 10
        graph = HIVGraph(numVertices)

        self.assertTrue(graph.getSusceptibleSet() == set(range(numVertices)))

        for i in range(9):
import sys 
import apgl
import numpy 
import unittest
import logging
import pickle 
import scipy.integrate
from apgl.util.Util import Util 

from exp.viroscopy.model.HIVABCParameters import HIVABCParameters
from exp.viroscopy.model.HIVRates import HIVRates
from exp.viroscopy.model.HIVGraph import HIVGraph


@apgl.skipIf(not apgl.checkImport('sppy'), 'No module sppy')
class  HIVABCParametersTest(unittest.TestCase):
    def setUp(self):
        numpy.seterr(invalid='raise')
        logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
        numpy.set_printoptions(suppress=True, precision=4, linewidth=100)
        numpy.random.seed(21)

        M = 1000
        undirected = True

        graph = HIVGraph(M, undirected)
        alpha = 2
        zeroVal = 0.9
        p = Util.powerLawProbs(alpha, zeroVal)
        hiddenDegSeq = Util.randomChoice(p, graph.getNumVertices())
        rates = HIVRates(graph, hiddenDegSeq)
Example #5
0
import apgl
import unittest
import numpy
import logging
from apgl.predictors.RandomForest import RandomForest
from apgl.util.PathDefaults import PathDefaults
from apgl.util.Evaluator import Evaluator
from apgl.util.Sampling import Sampling

@apgl.skipIf(not apgl.checkImport('sklearn'), 'Module sklearn is required')
class  RandomForestTest(unittest.TestCase):
    def setUp(self):
        numpy.random.seed(21)
        numExamples = 200
        numFeatures = 5

        self.X = numpy.random.rand(numExamples, numFeatures)
        c = numpy.random.rand(numFeatures)
        self.y = numpy.sign(self.X.dot(c) < numpy.mean(self.X.dot(c)))

    def testLearnModel(self):
        randomForest = RandomForest()
        randomForest.learnModel(self.X, self.y)

        tree = randomForest.getClassifier()

    def testPredict(self):
        randomForest = RandomForest()
        randomForest.learnModel(self.X, self.y)
        predY = randomForest.predict(self.X)
Example #6
0
import apgl
import unittest
import numpy
import logging
from sandbox.predictors.RandomForest import RandomForest
from sandbox.util.PathDefaults import PathDefaults
from sandbox.util.Evaluator import Evaluator
from sandbox.util.Sampling import Sampling


@apgl.skipIf(not apgl.checkImport('sklearn'), 'Module sklearn is required')
class RandomForestTest(unittest.TestCase):
    def setUp(self):
        numpy.random.seed(21)
        numExamples = 200
        numFeatures = 5

        self.X = numpy.random.rand(numExamples, numFeatures)
        c = numpy.random.rand(numFeatures)
        self.y = numpy.sign(self.X.dot(c) < numpy.mean(self.X.dot(c)))

    def testLearnModel(self):
        randomForest = RandomForest()
        randomForest.learnModel(self.X, self.y)

        tree = randomForest.getClassifier()

    def testPredict(self):
        randomForest = RandomForest()
        randomForest.learnModel(self.X, self.y)
        predY = randomForest.predict(self.X)
Example #7
0

import unittest
import numpy
import apgl
import logging
from apgl.graph import *
try:
    from apgl.clustering.LPKMeans import LPKMeans
except ImportError:
    pass

@apgl.skipIf(not apgl.checkImport('cvxopt'), 'No module cvxopt')
class LPKMeansTest(unittest.TestCase):
    def setUp(self):
        numpy.set_printoptions(suppress=True, precision=3)
        numpy.random.seed(21)
        numpy.set_printoptions(threshold=numpy.nan, linewidth=100)

        pass

    def testCluster(self):
        numFeatures = 2

        X1 = numpy.random.randn(30, numFeatures) + numpy.array([1, 1])
        X2 = numpy.random.randn(30, numFeatures) + numpy.array([-1, -1])
        X = numpy.r_[X1, X2]

        logging.debug(X.shape)

        k = 2
Example #8
0
import apgl
import numpy
import unittest
import pickle
import numpy.testing as nptst

from wallhack.viroscopy.model.HIVGraph import HIVGraph
from wallhack.viroscopy.model.HIVVertices import HIVVertices


@apgl.skipIf(not apgl.checkImport("sppy"), "No module pysparse")
class HIVGraphTest(unittest.TestCase):
    def setup(self):
        pass

    def testContructor(self):
        numVertices = 10
        vertices = HIVVertices(numVertices)

        self.assertEquals(numVertices, vertices.getNumVertices())
        self.assertEquals(8, vertices.getNumFeatures())

    def testAddVertices(self):
        numVertices = 10
        vertices = HIVVertices(numVertices)

        v1 = vertices[4]

        vertices.addVertices(5)
        print(vertices.V.shape)
        self.assertEquals(numVertices + 5, vertices.getNumVertices())
Example #9
0
from apgl.generator.SmallWorldGenerator import SmallWorldGenerator
from apgl.graph.SparseGraph import SparseGraph
from apgl.graph.VertexList import VertexList
from apgl.util import * 
import numpy
import numpy.random as rand
import unittest
import apgl
import logging

try:
    import mlpy
except ImportError:
    pass

@apgl.skipIf(not apgl.checkImport('mlpy'), 'No module mlpy')
class EgoSimulatorTest(unittest.TestCase):
    def setUp(self):
        numVertices = 500
        numFeatures = 49 
        
        self.means = rand.randn(numFeatures)
        self.vars = rand.randn(numFeatures, numFeatures)
        self.vars = self.vars + self.vars.T #Make vars symmetric
        p1 = 0.1
        
        self.egoGenerator = EgoGenerator()
        vList = self.egoGenerator.generateIndicatorVertices(numVertices, self.means, self.vars, p1)
        sGraph = SparseGraph(vList)
        
        p2 = 0.1 
Example #10
0
class EvaluatorTestCase(unittest.TestCase):
    #def setUp(self):
    #    self.foo = Evaluator()
    #

    def testRootMeanSqError(self):
        y = numpy.array([1, 2, 3])
        predY = numpy.array([1, 2, 3])

        self.assertEquals(Evaluator.rootMeanSqError(y, predY), 0.0)

        y = numpy.array([1, 2, 3])
        predY = numpy.array([1, 2, 2])

        self.assertEquals(Evaluator.rootMeanSqError(y, predY),
                          float(1) / numpy.sqrt(3))

        predY = numpy.array([1, 2])
        self.assertRaises(ValueError, Evaluator.rootMeanSqError, y, predY)

    def testWeightedRootMeanSqError(self):

        y = numpy.array([0.1, 0.2, 0.3])
        predY = numpy.array([0.1, 0.2, 0.3])

        self.assertEquals(Evaluator.weightedRootMeanSqError(y, predY), 0.0)

        #Errors on larger ys are weighted more
        predY = numpy.array([0.0, 0.2, 0.3])
        predY2 = numpy.array([0.1, 0.2, 0.4])

        self.assertTrue(
            Evaluator.weightedRootMeanSqError(y, predY) <
            Evaluator.weightedRootMeanSqError(y, predY2))

    def testBinaryError(self):
        testY = numpy.array([1, 1, -1, 1])
        predY = numpy.array([-1, 1, -1, 1])
        predY2 = numpy.array([-1, -1, -1, 1])
        predY3 = numpy.array([-1, -1, 1, -1])

        self.assertTrue(Evaluator.binaryError(testY, predY) == 0.25)
        self.assertTrue(Evaluator.binaryError(testY, testY) == 0.0)
        self.assertTrue(Evaluator.binaryError(predY, predY) == 0.0)

        self.assertTrue(Evaluator.binaryError(testY, predY2) == 0.5)
        self.assertTrue(Evaluator.binaryError(testY, predY3) == 1.0)

    def testBinaryErrorP(self):
        testY = numpy.array([1, 1, -1, 1])
        predY = numpy.array([-1, 1, -1, 1])
        predY2 = numpy.array([-1, -1, -1, 1])
        predY3 = numpy.array([-1, -1, 1, -1])

        self.assertTrue(Evaluator.binaryErrorP(testY, predY) == 1.0 / 3.0)
        self.assertTrue(Evaluator.binaryErrorP(testY, testY) == 0.0)
        self.assertTrue(Evaluator.binaryErrorP(predY, predY) == 0.0)

        self.assertTrue(Evaluator.binaryErrorP(testY, predY2) == 2.0 / 3.0)
        self.assertTrue(Evaluator.binaryErrorP(testY, predY3) == 1.0)

        testY = numpy.array([-1, -1, -1, -1])
        predY = numpy.array([-1, 1, -1, 1])

        self.assertTrue(Evaluator.binaryErrorP(testY, predY) == 0.0)

    def testBinaryErrorN(self):
        testY = numpy.array([1, 1, -1, 1])
        predY = numpy.array([-1, 1, -1, 1])
        predY2 = numpy.array([-1, -1, -1, 1])
        predY3 = numpy.array([-1, -1, 1, -1])

        self.assertTrue(Evaluator.binaryErrorN(testY, predY) == 0.0)
        self.assertTrue(Evaluator.binaryErrorN(testY, testY) == 0.0)
        self.assertTrue(Evaluator.binaryErrorN(predY, predY) == 0.0)

        self.assertTrue(Evaluator.binaryErrorN(testY, predY2) == 0.0)
        self.assertTrue(Evaluator.binaryErrorN(testY, predY3) == 1.0)

        testY = numpy.array([-1, -1, -1, -1])
        predY = numpy.array([-1, 1, -1, 1])

        self.assertTrue(Evaluator.binaryErrorN(testY, predY) == 0.5)

        testY = numpy.array([1, 1, 1, 1])
        predY = numpy.array([-1, 1, -1, 1])

        self.assertTrue(Evaluator.binaryErrorN(testY, predY) == 0.0)

    @apgl.skipIf(not apgl.checkImport('sklearn'), 'No module scikits.learn')
    def testAuc(self):
        testY = numpy.array([-1, -1, 1, 1])
        predY = numpy.array([-1, 0, 1, 1])
        predY2 = numpy.array([0.1, 0.2, 0.3, 0.4])

        self.assertEquals(Evaluator.auc(predY, testY), 1.0)
        self.assertEquals(Evaluator.auc(predY2, testY), 1.0)
        self.assertEquals(Evaluator.auc(-predY, testY), 0.0)

        numExamples = 1000
        testY = numpy.array(numpy.random.rand(numExamples) > 0.5, numpy.int)
        predY = numpy.random.rand(numExamples) > 0.5

        #For a random score the AUC is approximately 0.5
        self.assertAlmostEquals(Evaluator.auc(predY, testY), 0.5, 1)

    @apgl.skipIf(not apgl.checkImport('sklearn'), 'No module scikits.learn')
    def testLocalAuc(self):
        testY = numpy.array([-1, -1, 1, 1, 1, 1, 1, -1, -1, 1])
        predY = numpy.array([
            0.987, 0.868, 0.512, 0.114, 0.755, 0.976, 0.05, 0.371, 0.629, 0.819
        ])

        self.assertEquals(Evaluator.localAuc(testY, predY, 1.0),
                          Evaluator.auc(predY, testY))
        self.assertEquals(Evaluator.localAuc(testY, predY, 0.0), 0)

        self.assertEquals(Evaluator.localAuc(testY, testY, 0.2), 1.0)

        #Ask stephan if correct - needs extra tests

    def testBinaryBootstrapError(self):

        testY = numpy.array([-1, -1, 1, 1, 1])
        predY = 1 - testY

        trainY = numpy.array([-1, -1, 1, 1, 1])
        predTrainY = 1 - trainY

        self.assertEquals(
            Evaluator.binaryBootstrapError(testY, testY, trainY, trainY, 0.5),
            0.0)

        self.assertEquals(
            Evaluator.binaryBootstrapError(testY, testY, trainY, predTrainY,
                                           0.5), 0.5)
        self.assertEquals(
            Evaluator.binaryBootstrapError(testY, testY, trainY, predTrainY,
                                           0.1), 0.9)

        self.assertEquals(
            Evaluator.binaryBootstrapError(testY, predY, trainY, trainY, 0.1),
            0.1)

    def testMeanAbsError(self):
        testY = numpy.array([1, 2, 1.5])
        predY = numpy.array([2, 1, 0.5])

        self.assertEquals(Evaluator.meanAbsError(testY, predY), 1.0)
        self.assertEquals(Evaluator.meanAbsError(testY, testY), 0.0)

        testY = numpy.random.rand(10)
        predY = numpy.random.rand(10)

        error = numpy.abs(testY - predY).mean()
        self.assertEquals(error, Evaluator.meanAbsError(testY, predY))

    @apgl.skipIf(not apgl.checkImport('sklearn'), 'No module scikits.learn')
    def testPrecisionFromIndLists(self):
        predList = [4, 2, 10]
        testList = [4, 2]

        self.assertEquals(Evaluator.precisionFromIndLists(testList, predList),
                          2.0 / 3)

        testList = [4, 2, 10]
        self.assertEquals(Evaluator.precisionFromIndLists(testList, predList),
                          1)

        predList = [10, 2, 4]
        self.assertEquals(Evaluator.precisionFromIndLists(testList, predList),
                          1)

        testList = [1, 9, 11]
        self.assertEquals(Evaluator.precisionFromIndLists(testList, predList),
                          0)

        predList = [1, 2, 3, 4, 5]
        testList = [1, 9, 11]

        self.assertEquals(Evaluator.precisionFromIndLists(testList, predList),
                          1.0 / 5)

    def testAveragePrecisionFromLists(self):
        predList = [4, 2, 10]
        testList = [4, 2, 15, 16]

        self.assertEquals(
            Evaluator.averagePrecisionFromLists(testList, predList), 0.5)

        predList = [0, 1, 2, 3, 4, 5]
        testList = [0, 3, 4, 5]
        self.assertAlmostEquals(
            Evaluator.averagePrecisionFromLists(testList, predList),
            0.691666666666)
Example #11
0
import apgl
from apgl.graph.VertexList import VertexList
from apgl.util import *
from apgl.graph.test.MatrixGraphTest import MatrixGraphTest
import unittest
import numpy

try:
    from apgl.graph.CsArrayGraph import CsArrayGraph
except ImportError:
    pass


@apgl.skipIf(not apgl.checkImport('sppy'), 'No module sppy')
class CsArrayGraphTest(unittest.TestCase, MatrixGraphTest):
    def setUp(self):
        self.GraphType = CsArrayGraph
        self.initialise()

    def testInit(self):
        numVertices = 0
        numFeatures = 1
        vList = VertexList(numVertices, numFeatures)

        graph = CsArrayGraph(vList)
        self.assertEquals(graph.weightMatrixDType(), numpy.float)

        graph = CsArrayGraph(vList, dtype=numpy.int16)
        self.assertEquals(graph.weightMatrixDType(), numpy.int16)

        #Test just specifying number of vertices