SG++-Doxygen-Documentation
plot_2d_sparse_grids.py

plots anisotropic full grids that form part of the combination technique

1 from argparse import ArgumentParser
2 import numpy as np
3 import matplotlib.pyplot as plt
4 from matplotlib.patches import Rectangle
5 
6 import pysgpp
7 from pysgpp.pysgpp_swig import DataVector, CombigridOperation
8 from pysgpp.extensions.datadriven.uq.dists import J, Beta, Uniform, Normal
9 from pysgpp.extensions.datadriven.uq.plot.colors import load_color, savefig,\
10  load_font_properties
11 from pysgpp.extensions.datadriven.uq.plot.plot2d import plotDensity2d
12 
13 
14 def g(x):
15  return np.prod([4 * xi * (1 - xi) for xi in x.array()])
16 
17 
18 # We have to wrap f in a pysgpp.MultiFunction object.
19 func = pysgpp.multiFunc(g)
20 numDims = 2
21 
22 if __name__ == "__main__":
23  # parse the input arguments
24  parser = ArgumentParser(description='Get a program and run it with input')
25  parser.add_argument('--version', action='version', version='%(prog)s 1.0')
26  parser.add_argument('--level', default=2, type=int, help="minimum level of regular grids")
27  parser.add_argument('--marginalType', default="beta", type=str, help="marginals")
28  args = parser.parse_args()
29 
30  if args.marginalType == "uniform":
31  marginal = Uniform(0, 1)
32  elif args.marginalType == "beta":
33  marginal = Beta(5, 10)
34  else:
35  marginal = Normal(0.5, 0.1, 0, 1)
36 
37  # plot pdf
38  dist = J([marginal] * numDims)
39  fig = plt.figure()
40  plotDensity2d(dist)
41  savefig(fig, "/tmp/%s" % (args.marginalType,))
42  plt.close(fig)
43 
44  w = pysgpp.singleFunc(marginal.pdf)
45 
46  grids = pysgpp.AbstractPointHierarchyVector()
47  grids.push_back(pysgpp.CombiHierarchies.linearLeja(w))
48  grids.push_back(pysgpp.CombiHierarchies.linearLeja(w))
49 
50  evaluators = pysgpp.FloatScalarAbstractLinearEvaluatorVector()
51  evaluators.push_back(pysgpp.CombiEvaluators.polynomialInterpolation())
52  evaluators.push_back(pysgpp.CombiEvaluators.polynomialInterpolation())
53 
54  # To create a CombigridOperation object with our own configuration, we have to provide a
55  # LevelManager as well:
56  levelManager = pysgpp.WeightedRatioLevelManager()
57  operation = pysgpp.CombigridOperation(grids, evaluators, levelManager, func)
58 
59  # We can add regular levels like before:
60  levelManager.addRegularLevels(args.level)
61 
62  # We can also fetch the used grid points and plot the grid:
63  grid = levelManager.getGridPointMatrix()
64  gridList = [[grid.get(r, c) for c in range(grid.getNcols())] for r in range(grid.getNrows())]
65 
66  fig = plt.figure()
67  plt.plot(gridList[0], gridList[1], " ",
68  color=load_color(0),
69  marker='o', markersize=10)
70  plt.axis('off')
71  currentAxis = plt.gca()
72  currentAxis.add_patch(Rectangle((0, 0), 1, 1, fill=None, alpha=1, linewidth=2))
73  plt.xlim(0, 1)
74  plt.ylim(0, 1)
75  plt.title(r"Sparse Grid $\ell=%i$" % args.level,
76  fontproperties=load_font_properties())
77  savefig(fig, "/tmp/sparse_grid_l%i_%s" % (args.level, args.marginalType))