def testEstimate(self): #Lets set up a simple model based on normal dist abcParams = ABCParameters() epsilonArray = numpy.array([0.5, 0.2, 0.1]) posteriorSampleSize = 20 #Lets get an empirical estimate of Sprime model = NormalModel(abcMetrics) model.setMu(theta[0]) model.setSigma(theta[1]) Sprime = abcMetrics.summary(model.simulate()) logging.debug(("Real summary statistic: " + str(Sprime))) thetaDir = PathDefaults.getTempDir() abcSMC = ABCSMC(epsilonArray, createNormalModel, abcParams, thetaDir) abcSMC.maxRuns = 100000 abcSMC.setPosteriorSampleSize(posteriorSampleSize) thetasArray = abcSMC.run() thetasArray = numpy.array(thetasArray) meanTheta = numpy.mean(thetasArray, 0) logging.debug((thetasArray.shape)) logging.debug(thetasArray) logging.debug(meanTheta) print(thetasArray.shape[0], posteriorSampleSize) #Note only mean needs to be similar self.assertTrue(thetasArray.shape[0] >= posteriorSampleSize) self.assertEquals(thetasArray.shape[1], 2) self.assertTrue(numpy.linalg.norm(theta[0] - meanTheta[0]) < 0.2)
def testLoadParams(self): try: lmbda = 0.01 alterRegressor = PrimalRidgeRegression(lmbda) egoRegressor = PrimalRidgeRegression(lmbda) predictor = EgoEdgeLabelPredictor(alterRegressor, egoRegressor) params = [0.1, 0.2] paramFuncs = [egoRegressor.setLambda, alterRegressor.setLambda] fileName = PathDefaults.getTempDir() + "tempParams.pkl" predictor.saveParams(params, paramFuncs, fileName) params2 = predictor.loadParams(fileName) self.assertTrue(params2[0][0] == "apgl.predictors.PrimalRidgeRegression") self.assertTrue(params2[0][1] == "setLambda") self.assertTrue(params2[0][2] == 0.1) self.assertTrue(params2[1][0] == "apgl.predictors.PrimalRidgeRegression") self.assertTrue(params2[1][1] == "setLambda") self.assertTrue(params2[1][2] == 0.2) except IOError as e: logging.warn(e)
def testReadFromMatFile(self): numExamples = 10 dir = PathDefaults.getTempDir() fileName = dir + "examplesList1" X = rand(numExamples, 10) ml = ExamplesList(numExamples) ml.addDataField("X", X) ml.writeToMatFile(fileName) ml2 = ExamplesList.readFromMatFile(fileName) self.assertTrue(ml == ml2) Y = rand(numExamples, 20) ml.addDataField("Y", Y) ml.writeToMatFile(fileName) ml2 = ExamplesList.readFromMatFile(fileName) self.assertTrue(ml == ml2) Z = rand(numExamples, 50) ml.addDataField("Z", Z) ml.writeToMatFile(fileName) ml2 = ExamplesList.readFromMatFile(fileName) self.assertTrue(ml == ml2)
def testSaveParams(self): try: lmbda = 0.01 alterRegressor = PrimalRidgeRegression(lmbda) egoRegressor = PrimalRidgeRegression(lmbda) predictor = EgoEdgeLabelPredictor(alterRegressor, egoRegressor) params = [0.1, 0.2] paramFuncs = [egoRegressor.setLambda, alterRegressor.setLambda] fileName = PathDefaults.getTempDir() + "tempParams.pkl" predictor.saveParams(params, paramFuncs, fileName) except IOError as e: logging.warn(e)
def testSaveLoad(self): try: vList = VertexList(self.numVertices, self.numFeatures) vList.setVertex(0, numpy.array([1, 2, 3])) vList.setVertex(1, numpy.array([4, 5, 6])) vList.setVertex(2, numpy.array([7, 8, 9])) tempDir = PathDefaults.getTempDir() fileName = tempDir + "vList" vList.save(fileName) vList2 = VertexList.load(fileName) self.assertTrue((vList.getVertices() == vList2.getVertices()).all()) except IOError as e: logging.warn(e) pass
def testWriteToMatFile(self): numExamples = 100 X = rand(numExamples, 10) ml = ExamplesList(numExamples) dir = PathDefaults.getTempDir() fileName = dir + "examplesList1" #Try writing with no examples self.assertRaises(ValueError, ml.writeToMatFile, fileName) #Now, add a data field and try writing ml.addDataField("X", X) ml.writeToMatFile(fileName) ml.addDataField("Y", X) ml.writeToMatFile(fileName)
def testSaveLoad(self): try: vList = VertexList(self.numVertices, self.numFeatures) vList.setVertex(0, numpy.array([1, 2, 3])) vList.setVertex(1, numpy.array([4, 5, 6])) vList.setVertex(2, numpy.array([7, 8, 9])) tempDir = PathDefaults.getTempDir() fileName = tempDir + "vList" vList.save(fileName) vList2 = VertexList.load(fileName) self.assertTrue( (vList.getVertices() == vList2.getVertices()).all()) except IOError as e: logging.warn(e) pass
def testSaveLoad(self): try: vList = GeneralVertexList(self.numVertices) vList.setVertex(0, "abc") vList.setVertex(1, 12) vList.setVertex(2, "num") tempDir = PathDefaults.getTempDir() fileName = tempDir + "vList" vList.save(fileName) vList2 = GeneralVertexList.load(fileName) for i in range(self.numVertices): self.assertEquals(vList.getVertex(i), vList2.getVertex(i)) except IOError as e: logging.warn(e) pass
def profile(command, globalVars, localVars, numStats=30): """ Just profile the given command with the global and local variables and print out the cumulative and function times. """ try: import pstats import cProfile except ImportError: raise ImportError("profile() requires pstats and cProfile") tempDirectory = PathDefaults.getTempDir() profileFileName = tempDirectory + "profile.cprof" logging.info("Starting to profile ...") cProfile.runctx(command, globalVars, localVars, profileFileName) logging.info("Done") stats = pstats.Stats(profileFileName) stats.strip_dirs().sort_stats("cumulative").print_stats(numStats) stats.strip_dirs().sort_stats("time").print_stats(numStats)
def setUp(self): tempDir = PathDefaults.getTempDir() self.fileName = tempDir + "abc"