def testSerializationLinearBoudaryWithLeaf(self): """Uses Linear grid for tests""" from pysgpp import Grid srcLeaf = [] factory = Grid.createLinearBoundaryGrid(2) self.failIfEqual(factory, None) gen = factory.createGridGenerator() gen.regular(3) for i in xrange(factory.getStorage().size()): srcLeaf.append(factory.getStorage().get(i).isLeaf()) str = factory.serialize() self.assert_(len(str) > 0) newfac = Grid.unserialize(str) self.failIfEqual(newfac, None) self.assertEqual(factory.getStorage().size(), newfac.getStorage().size()) for i in xrange(factory.getStorage().size()): self.failUnlessEqual(newfac.getStorage().get(i).isLeaf(), srcLeaf[i])
def createGrid(grid, dim, deg=1, addTruncatedBorder=False): # create new grid gridType = grid.getType() if gridType in [Poly, PolyBoundary]: deg = max(deg, grid.getDegree()) # print gridType, deg if deg > 1: if gridType in [LinearBoundary, PolyBoundary]: return Grid.createPolyBoundaryGrid(dim, deg) elif gridType == LinearL0Boundary: raise NotImplementedError("there is no full boundary polynomial grid") elif gridType in [Linear, Poly]: return Grid.createPolyGrid(dim, deg) else: raise Exception('unknown grid type %s' % gridType) else: if gridType == Linear: return Grid.createLinearGrid(dim) elif gridType == LinearBoundary: return Grid.createLinearBoundaryGrid(dim) elif gridType == LinearL0Boundary: return Grid.createLinearBoundaryGrid(dim, 0) else: raise Exception('unknown grid type %s' % gridType)
def testSerializationLinearBoudaryBoundingBox(self): """Uses Linear grid for tests""" from pysgpp import Grid factory = Grid.createLinearBoundaryGrid(2) self.failIfEqual(factory, None) gen = factory.createGridGenerator() gen.regular(3) boundingBox = factory.getBoundingBox() tempBound = boundingBox.getBoundary(0) tempBound.leftBoundary = 0.0 tempBound.rightBoundary = 100.0 tempBound.bDirichletLeft = False; tempBound.bDirichletRight = False; boundingBox.setBoundary(0, tempBound) str = factory.serialize() self.assert_(len(str) > 0) newfac = Grid.unserialize(str) self.failIfEqual(newfac, None) self.assertEqual(factory.getStorage().size(), newfac.getStorage().size()) boundingBox = newfac.getBoundingBox() tempBound = boundingBox.getBoundary(0) self.assertEqual(0.0, tempBound.leftBoundary) self.assertEqual(100.0, tempBound.rightBoundary) self.assertEqual(False, tempBound.bDirichletLeft) self.assertEqual(False, tempBound.bDirichletRight)
def testSerializationLinearBoudaryBoundingBox(self): """Uses Linear grid for tests""" from pysgpp import Grid factory = Grid.createLinearBoundaryGrid(2) self.failIfEqual(factory, None) gen = factory.createGridGenerator() gen.regular(3) boundingBox = factory.getBoundingBox() tempBound = boundingBox.getBoundary(0) tempBound.leftBoundary = 0.0 tempBound.rightBoundary = 100.0 tempBound.bDirichletLeft = False tempBound.bDirichletRight = False boundingBox.setBoundary(0, tempBound) str = factory.serialize() self.assert_(len(str) > 0) newfac = Grid.unserialize(str) self.failIfEqual(newfac, None) self.assertEqual(factory.getStorage().size(), newfac.getStorage().size()) boundingBox = newfac.getBoundingBox() tempBound = boundingBox.getBoundary(0) self.assertEqual(0.0, tempBound.leftBoundary) self.assertEqual(100.0, tempBound.rightBoundary) self.assertEqual(False, tempBound.bDirichletLeft) self.assertEqual(False, tempBound.bDirichletRight)
def createGrid(grid, dim, deg=1, addTruncatedBorder=False): # create new grid gridType = grid.getType() if gridType in [Poly, PolyBoundary]: deg = max(deg, grid.getDegree()) # print gridType, deg if deg > 1: if gridType in [LinearBoundary, PolyBoundary]: return Grid.createPolyBoundaryGrid(dim, deg) elif gridType == LinearL0Boundary: raise NotImplementedError( "there is no full boundary polynomial grid") elif gridType in [Linear, Poly]: return Grid.createPolyGrid(dim, deg) else: raise Exception('unknown grid type %s' % gridType) else: if gridType == Linear: return Grid.createLinearGrid(dim) elif gridType == LinearBoundary: return Grid.createLinearBoundaryGrid(dim) elif gridType == LinearL0Boundary: return Grid.createLinearBoundaryGrid(dim, 0) else: raise Exception('unknown grid type %s' % gridType)
def plot_1d_2d(level=2): grid1d = Grid.createLinearGrid(1) grid1d.getGenerator().regular(level) grid2d2 = extend_grid(grid1d, 1) plotGrid2d(grid2d2) grid2d = Grid.createLinearGrid(2) grid2d.getGenerator().full(level) plotGrid2d(grid2d) plt.show()
def run_atan_sg(inputspace, gridType, level, numGridPoints, boundaryLevel, fullGrid, refinement, out, plot): testSetting = AtanPeridynamicExample(inputspace) if refinement is not None: testSetting.run_adaptive_sparse_grid(Grid.stringToGridType(gridType), level, numGridPoints, refinement, boundaryLevel, fullGrid, out, plot) else: testSetting.run_regular_sparse_grid(Grid.stringToGridType(gridType), level, numGridPoints, boundaryLevel, fullGrid, out, plot)
def testRefinement2d_two(self): from pysgpp import Grid, DataVector, SurplusRefinementFunctor factory = Grid.createLinearBoundaryGrid(2) storage = factory.getStorage() gen = factory.createGridGenerator() gen.regular(0) alpha = DataVector(4) for i in xrange(len(alpha)): alpha[i] = 0.0 alpha[0] = 1.0 func = SurplusRefinementFunctor(alpha) gen.refine(func) alpha2 = DataVector(8) for i in xrange(len(alpha2)): alpha2[i] = 0.0 alpha2[4] = 1.0 func = SurplusRefinementFunctor(alpha2) gen.refine(func) self.failUnlessEqual(storage.size(), 13)
def testOperationB(self): from pysgpp import Grid, DataVector, DataMatrix factory = Grid.createLinearBoundaryGrid(1) gen = factory.createGridGenerator() gen.regular(2) alpha = DataVector(factory.getStorage().size()) p = DataMatrix(1, 1) beta = DataVector(1) alpha.setAll(0.0) p.set(0, 0, 0.25) beta[0] = 1.0 opb = factory.createOperationB() opb.mult(beta, p, alpha) self.failUnlessAlmostEqual(alpha[0], 0.75) self.failUnlessAlmostEqual(alpha[1], 0.25) self.failUnlessAlmostEqual(alpha[2], 0.5) self.failUnlessAlmostEqual(alpha[3], 1.0) self.failUnlessAlmostEqual(alpha[4], 0.0) alpha.setAll(0.0) alpha[2] = 1.0 p.set(0, 0, 0.25) beta[0] = 0.0 opb.multTranspose(alpha, p, beta) self.failUnlessAlmostEqual(beta[0], 0.5)
def testOperationTest_test(self): from pysgpp import Grid, DataVector, DataMatrix factory = Grid.createLinearBoundaryGrid(1) gen = factory.createGridGenerator() gen.regular(1) alpha = DataVector(factory.getStorage().size()) data = DataMatrix(1, 1) data.setAll(0.25) classes = DataVector(1) classes.setAll(1.0) testOP = factory.createOperationTest() alpha[0] = 0.0 alpha[1] = 0.0 alpha[2] = 1.0 c = testOP.test(alpha, data, classes) self.failUnless(c > 0.0) alpha[0] = 0.0 alpha[1] = 0.0 alpha[2] = -1.0 c = testOP.test(alpha, data, classes) self.failUnless(c == 0.0)
def eval_fullGrid(level, dim, border=True): if border: grid = Grid.createLinearBoundaryGrid(dim, 1) else: grid = Grid.createLinearGrid(dim) grid.getGenerator().full(level) gs = grid.getStorage() ans = np.ndarray((gs.getSize(), dim)) p = DataVector(dim) for i in range(gs.getSize()): gs.getCoordinates(gs.getPoint(i), p) ans[i, :] = p.array() return ans
def checkPositivity(grid, alpha): # define a full grid of maxlevel of the grid gs = grid.getStorage() fullGrid = Grid.createLinearGrid(gs.dim()) fullGrid.createGridGenerator().full(gs.getMaxLevel()) fullHashGridStorage = fullGrid.getStorage() A = DataMatrix(fullHashGridStorage.size(), fullHashGridStorage.dim()) p = DataVector(gs.dim()) for i in xrange(fullHashGridStorage.size()): fullHashGridStorage.get(i).getCoords(p) A.setRow(i, p) res = evalSGFunctionMulti(grid, alpha, A) ymin, ymax, cnt = 0, -1e10, 0 for i, yi in enumerate(res.array()): if yi < 0. and abs(yi) > 1e-13: cnt += 1 ymin = min(ymin, yi) ymax = max(ymax, yi) A.getRow(i, p) print " %s = %g" % (p, yi) if cnt > 0: print "warning: function is not positive" print "%i/%i: [%g, %g]" % (cnt, fullHashGridStorage.size(), ymin, ymax) return cnt == 0
def checkPositivity(grid, alpha): # define a full grid of maxlevel of the grid gs = grid.getStorage() fullGrid = Grid.createLinearGrid(gs.getDimension()) fullGrid.getGenerator().full(gs.getMaxLevel()) fullHashGridStorage = fullGrid.getStorage() A = np.ndarray( (fullHashGridStorage.getSize(), fullHashGridStorage.getDimension())) p = DataVector(gs.getDimension()) for i in range(fullHashGridStorage.getSize()): fullHashGridStorage.getCoordinates(fullHashGridStorage.getPoint(i), p) A[i, :] = p.array() negativeGridPoints = {} res = evalSGFunctionMulti(grid, alpha, A) ymin, ymax, cnt = 0, -1e10, 0 for i, yi in enumerate(res): # print( A[i, :], yi ) if yi < -1e-11: cnt += 1 negativeGridPoints[i] = yi, HashGridPoint( fullHashGridStorage.getPoint(i)) ymin = min(ymin, yi) ymax = max(ymax, yi) # print( " %s = %g" % (A[i, :], yi) ) if cnt > 0: print("warning: function is not positive") print("%i/%i: [%g, %g]" % (cnt, fullHashGridStorage.getSize(), ymin, ymax)) return negativeGridPoints
def eval_fullGrid(level, dim, border=True): if border: grid = Grid.createLinearBoundaryGrid(dim) else: grid = Grid.createLinearGrid(dim) grid.createGridGenerator().full(level) gs = grid.getStorage() ans = DataMatrix(gs.size(), dim) p = DataVector(dim) for i in xrange(gs.size()): gs.get(i).getCoords(p) ans.setRow(i, p) return ans
def testOperationB(self): from pysgpp import Grid, DataVector, DataMatrix factory = Grid.createLinearBoundaryGrid(1) gen = factory.createGridGenerator() gen.regular(2) alpha = DataVector(factory.getStorage().size()) p = DataMatrix(1,1) beta = DataVector(1) alpha.setAll(0.0) p.set(0,0,0.25) beta[0] = 1.0 opb = factory.createOperationB() opb.mult(beta, p, alpha) self.failUnlessAlmostEqual(alpha[0], 0.75) self.failUnlessAlmostEqual(alpha[1], 0.25) self.failUnlessAlmostEqual(alpha[2], 0.5) self.failUnlessAlmostEqual(alpha[3], 1.0) self.failUnlessAlmostEqual(alpha[4], 0.0) alpha.setAll(0.0) alpha[2] = 1.0 p.set(0,0, 0.25) beta[0] = 0.0 opb.multTranspose(alpha, p, beta) self.failUnlessAlmostEqual(beta[0], 0.5)
def run_sobol_g_function_sg(fullModel, gridType, level, numGridPoints, fullGrid, refinement, out): testSetting = SobolGFunctionSudret2008(fullModel) sobol_indices, N = testSetting.run_sparse_grids( Grid.stringToGridType(gridType), level, numGridPoints, fullGrid, refinement, out) return testSetting.sobol_indices, sobol_indices, N
def testOperationTest_test(self): from pysgpp import Grid, DataVector, DataMatrix factory = Grid.createLinearBoundaryGrid(1) gen = factory.createGridGenerator() gen.regular(1) alpha = DataVector(factory.getStorage().size()) data = DataMatrix(1,1) data.setAll(0.25) classes = DataVector(1) classes.setAll(1.0) testOP = factory.createOperationTest() alpha[0] = 0.0 alpha[1] = 0.0 alpha[2] = 1.0 c = testOP.test(alpha, data, classes) self.failUnless(c > 0.0) alpha[0] = 0.0 alpha[1] = 0.0 alpha[2] = -1.0 c = testOP.test(alpha, data, classes) self.failUnless(c == 0.0)
def testEval(self): grid = Grid.createLinearGrid(1) grid.getGenerator().regular(3) gs = grid.getStorage() # prepare surplus vector nodalValues = DataVector(gs.getSize()) nodalValues.setAll(0.0) # interpolation on nodal basis p = DataVector(gs.getDimension()) for i in range(gs.getSize()): gs.getCoordinates(gs.getPoint(i), p) nodalValues[i] = f(p[0]) # nodalValues[i] = f(p[0], p[1]) # hierarchization alpha = hierarchize(grid, nodalValues) # eval the sparse grid function x = np.linspace(0, 1, 1000) y = [f(xi) for xi in x] y1 = [evalSGFunction(grid, alpha, np.array([xi])) for xi in x] y2 = evalSGFunctionMulti(grid, alpha, np.array([x]).T) assert np.all(y1 - y2 <= 1e-13)
def test44(self): from pysgpp import Grid, DataVector, FullGrid, FullGridSet dim = 2 level = 8 function = buildParableBoundary(dim) grid = Grid.createSquareRootGrid(dim) testFG(self, grid, level, function)
def test34(self): from pysgpp import Grid, DataVector, FullGrid, FullGridSet dim = 2 level = 9 function = buildParableBoundary(dim) grid = Grid.createLinearBoundaryGrid(dim) testFG(self, grid, level, function)
def plot_2d_3d(level=2): grid2d = Grid.createLinearGrid(2) grid2d.getGenerator().regular(level) plotGrid2d(grid2d) grid3d = extend_grid(grid2d, 1) plotGrid3dSlices(grid3d) plt.show()
def testSerializationLinearBoudary(self): """Uses Linear grid for tests""" from pysgpp import Grid factory = Grid.createLinearBoundaryGrid(2) self.failIfEqual(factory, None) gen = factory.createGridGenerator() gen.regular(3) str = factory.serialize() self.assert_(len(str) > 0) newfac = Grid.unserialize(str) self.failIfEqual(newfac, None) self.assertEqual(factory.getStorage().size(), newfac.getStorage().size())
def testHierarchisationDBoundary(self): from pysgpp import Grid dim = 3 level = 5 function = buildParableBoundary(dim) grid = Grid.createLinearBoundaryGrid(dim) testHierarchisationDehierarchisation(self, grid, level, function)
def testHierarchisationDModLinear(self): from pysgpp import Grid dim = 3 level = 5 function = buildParable(dim) grid = Grid.createModLinearGrid(dim) testHierarchisationDehierarchisation(self, grid, level, function)
def test43(self): from pysgpp import Grid, DataVector, FullGrid, FullGridSet dim = 4 level = 4 function = buildParableBoundary(dim) grid = Grid.createSquareRootGrid(dim) testFG(self, grid, level, function)
def test13(self): from pysgpp import Grid, DataVector, FullGrid, FullGridSet dim = 4 level = 6 function = buildParableBoundary(dim) grid = Grid.createLinearBoundaryGrid(dim, 0) testFG(self, grid, level, function)
def test44(self): from pysgpp import Grid, DataVector, FullGrid, FullGridSet dim = 2 level = 8 function = buildParableBoundary(dim) grid = Grid.createLinearTruncatedBoundaryGrid(dim) testFG(self, grid, level, function)
def createGrid(grid, dim, deg=1, addTruncatedBorder=False): # create new grid gridType = grid.getType() deg = max(deg, grid.getDegree()) # print( gridType, deg ) if deg > 1 and gridType in [GridType_Linear]: return Grid.createPolyGrid(dim, deg) if deg > 1 and gridType in [ GridType_LinearBoundary, GridType_LinearL0Boundary ]: return Grid.createPolyBoundaryGrid(dim, deg) elif deg > 1 and gridType in [GridType_LinearClenshawCurtis]: return Grid.createPolyClenshawCurtisGrid(dim, deg) elif deg > 1 and gridType in [GridType_LinearClenshawCurtisBoundary]: return Grid.createPolyClenshawCurtisBoundaryGrid(dim, deg) elif deg > 1 and gridType in [GridType_ModLinear]: return Grid.createModPolyGrid(dim, deg) elif deg > 1 and gridType in [GridType_ModLinearClenshawCurtis]: return Grid.createModPolyClenshawCurtisGrid(dim, deg) else: gridConfig = RegularGridConfiguration() gridConfig.type_ = gridType gridConfig.dim_ = dim gridConfig.maxDegree_ = deg return Grid.createGrid(gridConfig)
def general_test(self, d, l, bb, xs): test_desc = "dim=%d, level=%d, len(x)=%s" % (d, l, len(xs)) print(test_desc) self.grid = Grid.createLinearGrid(d) self.grid_gen = self.grid.getGenerator() self.grid_gen.regular(l) alpha = DataVector( [self.get_random_alpha() for i in range(self.grid.getSize())]) bb_ = BoundingBox(d) for d_k in range(d): dimbb = BoundingBox1D() dimbb.leftBoundary = bb[d_k][0] dimbb.rightBoundary = bb[d_k][1] bb_.setBoundary(d_k, dimbb) # Calculate the expected value without the bounding box expected_normal = [ self.calc_exp_value_normal(x, d, bb, alpha) for x in xs ] #expected_transposed = [self.calc_exp_value_transposed(x, d, bb, alpha) for x in xs] # Now set the bounding box self.grid.getStorage().setBoundingBox(bb_) dm = DataMatrix(len(xs), d) for k, x in enumerate(xs): dv = DataVector(x) dm.setRow(k, dv) multEval = createOperationMultipleEval(self.grid, dm) actual_normal = DataVector(len(xs)) #actual_transposed = DataVector(len(xs)) multEval.mult(alpha, actual_normal) #multEval.mult(alpha, actual_transposed) actual_normal_list = [] for k in range(len(xs)): actual_normal_list.append(actual_normal.__getitem__(k)) #actual_transposed_list = [] #for k in xrange(len(xs)): # actual_transposed_list.append(actual_transposed.__getitem__(k)) self.assertAlmostEqual(actual_normal_list, expected_normal) #self.assertAlmostEqual(actual_tranposed_list, expected_tranposed) del self.grid
def __init__(self, grid): super(FullGridCandidates, self).__init__() # genreate new full grid gs = grid.getStorage() maxLevel = gs.getMaxLevel() self.numDims = gs.getDimension() self.fullGrid = Grid.createLinearGrid(self.numDims) self.fullGrid.getGenerator().full(maxLevel)
def testCreation(self): """Uses Linear grid for tests""" from pysgpp import Grid factory = Grid.createLinearGrid(2) self.failIfEqual(factory, None) storage = factory.getStorage() self.failIfEqual(storage, None)
def setUp(self): self.grid = Grid.createLinearGrid(2) # a simple 2D grid self.grid.createGridGenerator().regular(3) # max level 3 => 17 points self.HashGridStorage = self.grid.getStorage() alpha = DataVector(self.grid.getSize()) alpha.setAll(1.0) for i in [9, 10, 11, 12]: alpha[i] = 0.0 coarseningFunctor = SurplusCoarseningFunctor(alpha, 4, 0.5) self.grid.createGridGenerator().coarsen(coarseningFunctor, alpha)
def testHatRegular1D(self): from pysgpp import Grid factory = Grid.createModLinearGrid(1) m = generateLaplaceMatrix(factory, 5) m_ref = readReferenceMatrix(self, factory.getStorage(), 'data/C_laplace_phi_li_ausgeklappt_dim_1_nopsgrid_31_float.dat.gz') # compare compareStiffnessMatrices(self, m, m_ref)
def createGrid(dim, level, borderType, isFull=False): from pysgpp.extensions.datadriven.learner.Types import BorderTypes if borderType == BorderTypes.NONE: grid = Grid.createLinearGrid(dim) elif borderType == BorderTypes.TRAPEZOIDBOUNDARY: grid = Grid.createLinearTrapezoidBoundaryGrid(dim) elif borderType == BorderTypes.COMPLETEBOUNDARY: grid = Grid.createLinearBoundaryGrid(dim, 0) else: raise Exception('Unknown border type') # create regular grid of level accLevel gridGen = grid.createGridGenerator() if isFull: gridGen.full(level) else: gridGen.regular(level) return grid
def testHatRegular1D_two(self): from pysgpp import Grid factory = Grid.createLinearTrapezoidBoundaryGrid(1) m = generateLaplaceMatrix(factory, 5) m_ref = readReferenceMatrix(self, factory.getStorage(), 'data/C_laplace_phi_li_hut_trapezrand_dim_1_nopsgrid_33_float.dat.gz') # compare compareStiffnessMatrices(self, m, m_ref)
def createGrid(dim, level, borderType, isFull=False): from pysgpp.extensions.datadriven.learner.Types import BorderTypes if borderType == BorderTypes.NONE: grid = Grid.createLinearGrid(dim) elif borderType == BorderTypes.TRAPEZOIDBOUNDARY: grid = Grid.createLinearTrapezoidBoundaryGrid(dim) elif borderType == BorderTypes.COMPLETEBOUNDARY: grid = Grid.createLinearBoundaryGrid(dim, 0) else: raise Exception('Unknown border type') # create regular grid of level accLevel gridGen = grid.getGenerator() if isFull: gridGen.full(level) else: gridGen.regular(level) return grid
def testHatRegulardD_two(self): from pysgpp import Grid factory = Grid.createLinearBoundaryGrid(3) m = generateLaplaceMatrix(factory, 4) m_ref = readReferenceMatrix(self, factory.getStorage(), 'data/C_laplace_phi_li_hut_l0_rand_dim_3_nopsgrid_297_float.dat.gz') # compare compareStiffnessMatrices(self, m, m_ref)
def general_test(self, d, l, bb, xs): test_desc = "dim=%d, level=%d, len(x)=%s" % (d, l, len(xs)) print test_desc self.grid = Grid.createLinearGrid(d) self.grid_gen = self.grid.createGridGenerator() self.grid_gen.regular(l) alpha = DataVector([self.get_random_alpha() for i in xrange(self.grid.getSize())]) bb_ = BoundingBox(d) for d_k in xrange(d): dimbb = DimensionBoundary() dimbb.leftBoundary = bb[d_k][0] dimbb.rightBoundary = bb[d_k][1] bb_.setBoundary(d_k, dimbb) # Calculate the expected value without the bounding box expected_normal = [self.calc_exp_value_normal(x, d, bb, alpha) for x in xs] #expected_transposed = [self.calc_exp_value_transposed(x, d, bb, alpha) for x in xs] # Now set the bounding box self.grid.getStorage().setBoundingBox(bb_) dm = DataMatrix(len(xs), d) for k, x in enumerate(xs): dv = DataVector(x) dm.setRow(k, dv) multEval = createOperationMultipleEval(self.grid, dm) actual_normal = DataVector(len(xs)) #actual_transposed = DataVector(len(xs)) multEval.mult(alpha, actual_normal) #multEval.mult(alpha, actual_transposed) actual_normal_list = [] for k in xrange(len(xs)): actual_normal_list.append(actual_normal.__getitem__(k)) #actual_transposed_list = [] #for k in xrange(len(xs)): # actual_transposed_list.append(actual_transposed.__getitem__(k)) self.assertAlmostEqual(actual_normal_list, expected_normal) #self.assertAlmostEqual(actual_tranposed_list, expected_tranposed) del self.grid
def setUp(self): # # Grid # DIM = 2 LEVEL = 2 self.grid = Grid.createLinearGrid(DIM) self.grid_gen = self.grid.createGridGenerator() self.grid_gen.regular(LEVEL) # # trainData, classes, errors # xs = [] DELTA = 0.05 DELTA_RECI = int(1/DELTA) for i in xrange(DELTA_RECI): for j in xrange(DELTA_RECI): xs.append([DELTA*i, DELTA*j]) random.seed(1208813) ys = [ random.randint(-10, 10) for i in xrange(DELTA_RECI**2)] # print xs # print ys self.trainData = DataMatrix(xs) self.classes = DataVector(ys) self.alpha = DataVector([3, 6, 7, 9, -1]) self.errors = DataVector(DELTA_RECI**2) coord = DataVector(DIM) for i in xrange(self.trainData.getNrows()): self.trainData.getRow(i, coord) self.errors.__setitem__ (i, self.classes[i] - self.grid.eval(self.alpha, coord)) #print "Errors:" #print self.errors # # Functor # self.functor = WeightedErrorRefinementFunctor(self.alpha, self.grid) self.functor.setTrainDataset(self.trainData) self.functor.setClasses(self.classes) self.functor.setErrors(self.errors)
def setUp(self,): self.__gridFormatter = None self.filename = pathlocal + "/datasets/grid.gz" self.savefile = pathlocal + "/datasets/savetest.grid.gz" self.correct_str = "" self.grid = None self.__gridFormatter = GridFormatter() dim = 3 self.grid = Grid.createLinearGrid(dim) self.grid.createGridGenerator().regular(3) self.correct_str = self.grid.serialize()
def makePositive(grid, alpha): """ insert full grid points if they are negative and the father node is part of the sparse grid @param grid: @param alpha: """ # copy old sg function jgrid = copyGrid(grid) # evaluate the sparse grid function at all full grid points level = grid.getStorage().getMaxLevel() fg = Grid.createLinearGrid(grid.getStorage().dim()) fg.createGridGenerator().full(level) # copy the old grid and use it as reference jgs = jgrid.getStorage() fgs = fg.getStorage() # run over all results and check where the function value # is lower than zero cnt = 1 while True: print "run %i: full grid size = %i" % (cnt, fgs.size()) gps = [] # insert those fg points, which are not yet positive values = computeNodalValues(fg, grid, alpha) for i in xrange(len(values)): gp = fgs.get(i) if values[i] < 0 and not jgs.has_key(gp): gps += insertPoint(jgrid, gp) gps += insertHierarchicalAncestors(jgrid, gp) jgrid.getStorage().recalcLeafProperty() # 1. compute nodal values for new grid points jnodalValues = computeNodalValues(jgrid, grid, alpha) # 2. set the new ones to zero jgs = jgrid.getStorage() for gp in gps: jnodalValues[jgs.seq(gp)] = 0. # 3. hierarchize jalpha = hierarchize(jgrid, jnodalValues) # stop loop if no points have been added if len(gps) == 0: break # 4. reset values for next loop grid = copyGrid(jgrid) alpha = DataVector(jalpha) cnt += 1 return jgrid, jalpha
def fromJson(cls, jsonObject): """ Restores the ASGC object from the json object with its attributes. @param jsonObject: json object @return: the restored ASGC object """ knowledge = ASGCKnowledge() # restore iteration key = '_ASGCKnowledge__iteration' if key in jsonObject: knowledge.setIteration(int(jsonObject[key])) # restore surpluses: {iteration: {qoi: {dtype: {t: <Grid>}}}} key = '_ASGCKnowledge__grids' if key in jsonObject: grids = {} for iteration, v1 in jsonObject[key].items(): d1 = {} for qoi, gridString in v1.items(): # undo the hack that made it json compatible gridString = gridString.replace('__', '\n')\ .encode('utf8') # compatibility with older poly basis type gridString = gridString.replace('myPoly', 'poly') # deserialize ... grid = Grid.unserialize(gridString) # ... and store it d1[qoi] = grid grids[int(iteration)] = d1 knowledge.setGrids(grids) # restore surpluses: {iteration: {qoi: {dtype: {t: <list float>}}}} key = '_ASGCKnowledge__alphas' if key in jsonObject: alphas = {} for iteration, v1 in jsonObject[key].items(): d1 = {} for qoi, v2 in v1.items(): d2 = {} for dtype, v3 in v2.items(): d3 = {} for t, alpha in v3.items(): d3[float(t)] = DataVector(alpha) d2[int(dtype)] = d3 d1[qoi] = d2 alphas[int(iteration)] = d1 knowledge.setAlphas(alphas) return knowledge
def general_test(self, d, l, bb, x): test_desc = "dim=%d, level=%d, bb=%s, x=%s" % (d, l, bb, x) print test_desc self.grid = Grid.createLinearGrid(d) self.grid_gen = self.grid.createGridGenerator() self.grid_gen.regular(l) alpha = DataVector([1] * self.grid.getSize()) bb_ = BoundingBox(d) for d_k in xrange(d): dimbb = DimensionBoundary() dimbb.leftBoundary = bb[d_k][0] dimbb.rightBoundary = bb[d_k][1] bb_.setBoundary(d_k, dimbb) # Calculate the expected value without the bounding box expected = 0.0 inside = True x_trans = DataVector(d) for d_k in xrange(d): if not (bb[d_k][0] <= x[d_k] and x[d_k] <= bb[d_k][1]): inside = False break else: x_trans[d_k] = (x[d_k] - bb[d_k][0]) / (bb[d_k][1] - bb[d_k][0]) if inside: p = DataVector(x_trans) expected = self.grid.eval(alpha, p) else: expected = 0.0 # Now set the bounding box self.grid.getStorage().setBoundingBox(bb_) p = DataVector(x) actual = self.grid.eval(alpha, p) self.assertAlmostEqual(actual, expected) del self.grid
def testGeneration(self): from pysgpp import Grid, DataVector factory = Grid.createLinearBoundaryGrid(2) storage = factory.getStorage() gen = factory.createGridGenerator() self.failIfEqual(gen, None) self.failUnlessEqual(storage.size(), 0) gen.regular(3) self.failUnlessEqual(storage.size(), 37) #This should fail self.failUnlessRaises(Exception, gen.regular, 3)
def testHatRegulardD_two(self): from pysgpp import Grid factory = Grid.createLinearBoundaryGrid(3) training = buildTrainingVector(readDataVector('data/data_dim_3_nops_512_float.arff.gz')) level = 3 gen = factory.createGridGenerator() gen.regular(level) m = generateBBTMatrix(factory, training) m_ref = readReferenceMatrix(self, factory.getStorage(), 'data/BBT_phi_li_hut_trapezrand_dim_3_nopsgrid_225_float.dat.gz') # compare compareBBTMatrices(self, m, m_ref)
def testPrewaveletdD_two(self): from pysgpp import Grid factory = Grid.createPrewaveletGrid(3) training = buildTrainingVector(readDataVector('data/data_dim_3_nops_512_float.arff.gz')) level = 4 gen = factory.createGridGenerator() gen.regular(level) m = generateBBTMatrix(factory, training) m_ref = readReferenceMatrix(self, factory.getStorage(), 'data/BBT_prewavelet_dim_3_nopsgrid_111_float.dat.gz') # compare compareBBTMatrices(self, m, m_ref)
def testHatRegular1D_one(self): from pysgpp import Grid factory = Grid.createLinearGrid(1) training = buildTrainingVector(readDataVector('data/data_dim_1_nops_8_float.arff.gz')) level = 3 gen = factory.createGridGenerator() gen.regular(level) m = generateBBTMatrix(factory, training) m_ref = readReferenceMatrix(self, factory.getStorage(), 'data/BBT_phi_li_hut_dim_1_nopsgrid_7_float.dat.gz') # compare compareBBTMatrices(self, m, m_ref)
def setUp(self): # # Grid # DIM = 2 LEVEL = 2 self.grid = Grid.createLinearGrid(DIM) self.grid_gen = self.grid.createGridGenerator() self.grid_gen.regular(LEVEL) # # trainData, classes, errors # xs = [] DELTA = 0.05 DELTA_RECI = int(1/DELTA) for i in xrange(DELTA_RECI): for j in xrange(DELTA_RECI): xs.append([DELTA*i, DELTA*j]) random.seed(1208813) ys = [ random.randint(-10, 10) for i in xrange(DELTA_RECI**2)] self.trainData = DataMatrix(xs) self.classes = DataVector(ys) self.alpha = DataVector([3, 6, 7, 9, -1]) self.multEval = createOperationMultipleEval(self.grid, self.trainData) self.errors = DataVector(DELTA_RECI**2) coord = DataVector(DIM) for i in xrange(self.trainData.getNrows()): self.trainData.getRow(i, coord) self.errors.__setitem__ (i, abs(self.classes[i] - self.grid.eval(self.alpha, coord))) # # OnlinePredictiveRefinementDimension # hash_refinement = HashRefinement(); self.strategy = OnlinePredictiveRefinementDimension(hash_refinement) self.strategy.setTrainDataset(self.trainData) self.strategy.setClasses(self.classes) self.strategy.setErrors(self.errors)
def discretizeFunction(f, bounds, level=2, hasBorder=False, *args, **kws): # define linear transformation to the unit hyper cube T = JointTransformation() for xlim in bounds: T.add(LinearTransformation(xlim[0], xlim[1])) # create grid dim = len(bounds) # create adequate grid if hasBorder: grid = Grid.createLinearBoundaryGrid(dim) else: grid = Grid.createLinearGrid(dim) # init storage grid.createGridGenerator().regular(level) gs = grid.getStorage() # discretize on given level p = DataVector(dim) nodalValues = DataVector(gs.size()) for i in xrange(gs.size()): gs.get(i).getCoords(p) # transform to the right space q = T.unitToProbabilistic(p.array()) # apply the given function nodalValues[i] = float(f(q)) # hierarchize alpha = hierarchize(grid, nodalValues) # estimate the l2 error err = estimateDiscreteL2Error(grid, alpha, f) # TODO: adaptive refinement return grid, alpha, err