Skip to content

Commit

Permalink
make shared pointer available in python for DataMatrix, DataVector an…
Browse files Browse the repository at this point in the history
…d Grid; ignoring ambiguous declaration of doTransformation in python interface
  • Loading branch information
Fabian Franzelin committed Mar 13, 2016
1 parent 932ed12 commit 9514d92
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 53 deletions.
15 changes: 11 additions & 4 deletions base/build/pysgpp/base.i
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@

%include "base/src/sgpp/globaldef.hpp"

// -------------------------------------------------------
// shared pointer declarations
// this needs to be done before the declarations of the types themselves
%include <std_shared_ptr.i>
%shared_ptr(sgpp::base::Grid)
%shared_ptr(sgpp::base::DataVector)
%shared_ptr(sgpp::base::DataMatrix)
// -------------------------------------------------------

%apply (double* IN_ARRAY1, int DIM1) {(double* input, int size)}

namespace std {
%template(IntVector) vector<int>;
%template(IntIntVector) vector< vector<int> >;
%template(IntIntVector) vector< vector<int> >;
%template(BoolVector) vector<bool>;
%template(DoubleVector) vector<double>;
%template(FloatVector) vector<float>;
Expand All @@ -34,7 +43,6 @@ namespace std {
%include "GridFactory.i"
%include "OpFactory.i"


%ignore sgpp::base::DataVectorSP::DataVectorSP(std::vector<float> input);
%ignore sgpp::base::DataVectorSP::operator=;
%ignore sgpp::base::DataVectorSP::operator[];
Expand Down Expand Up @@ -163,12 +171,11 @@ namespace std {
%template(SPrewaveletBase) sgpp::base::PrewaveletBasis<unsigned int, unsigned int>;

%apply std::vector<std::pair<size_t, double> > *OUTPUT { std::vector<std::pair<size_t, double> >& result };
%apply std::vector<double> *INPUT { std::vector<double>& point };
%apply std::vector<double> *INPUT { std::vector<double>& point };

%template(SGetAffectedBasisFunctions) sgpp::base::GetAffectedBasisFunctions<sgpp::base::SLinearBase>;
%template(SAlgorithmEvaluation) sgpp::base::AlgorithmEvaluation<sgpp::base::SLinearBase>;
%template(SGetAffectedBasisFunctionsBoundaries) sgpp::base::GetAffectedBasisFunctions<sgpp::base::SLinearBoundaryBase>;
%template(SGetAffectedBasisFunctionsLinearStretchedBoundaries) sgpp::base::GetAffectedBasisFunctions<sgpp::base::SLinearStretchedBoundaryBase>;
%template(DimensionBoundaryVector) std::vector<sgpp::base::DimensionBoundary>;
%template(Stretching1DVector) std::vector<sgpp::base::Stretching1D>;

4 changes: 4 additions & 0 deletions datadriven/build/pysgpp/datadriven.i
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@

%include "datadriven/src/sgpp/datadriven/application/RegularizationConfiguration.hpp"

// --------------------------------------
// renaming ambiguous function declarations for python
%ignore base::datadriven::OperationRosenblattTransformation::doTransformation(base::DataVector* alpha, base::DataMatrix* points, base::DataMatrix* pointscdf, size_t dim_start);

//- namespace datadriven ------------------------------------------
namespace datadriven {
/*%nodefaultdtor sgpp::datadriven::OperationTest;
Expand Down
54 changes: 51 additions & 3 deletions datadriven/examples/uq/input_modeling_and_propagation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,52 @@
from pysgpp.extensions.datadriven.uq.dists import SGDEdist
from pysgpp import (RegularGridConfiguration, Linear, LinearBoundary,
AdpativityConfiguration,
SLESolverConfiguration,
RegularizationConfiguration, Laplace,
LearnerSGDEConfiguration,
DataVector, DataMatrix, Grid, LearnerSGDE)
from pysgpp.extensions.datadriven.uq.dists import SGDEdist, TNormal, J
from pysgpp import DataMatrix
import numpy as np

dist = SGDEdist.byLearnerSGDE(samples, gridConfig, adaptConfig, solverConfig,
regularizationConfig, learnerConfig)
# -------------------- prepare data
U = J([TNormal(0.5, 0.06, 0, 1)])
np.random.seed(12345)
samples = DataMatrix(U.rvs(1000))
# ---------- using SGDE from SG++ ------------------------
gridConfig = RegularGridConfiguration()
gridConfig.dim_ = U.getDim()
gridConfig.level_ = 6
gridConfig.type_ = LinearBoundary

adaptConfig = AdpativityConfiguration()
adaptConfig.noPoints_ = 10
adaptConfig.numRefinements_ = 0

solverConfig = SLESolverConfiguration()
solverConfig.maxIterations_ = 100
solverConfig.eps_ = 1e-10
solverConfig.threshold_ = 1e-10

regularizationConfig = RegularizationConfiguration()
regularizationConfig.regType_ = Laplace

learnerConfig = LearnerSGDEConfiguration()
learnerConfig.doCrossValidation_ = False
learnerConfig.kfold_ = 0
learnerConfig.lambdaStart_ = 1e-1
learnerConfig.lambdaEnd_ = 1e-10
learnerConfig.lambdaSteps_ = 0
learnerConfig.logScale_ = True
learnerConfig.shuffle_ = False
learnerConfig.seed_ = 1234567
learnerConfig.silent_ = False

learner = LearnerSGDE(gridConfig, adaptConfig, solverConfig,
regularizationConfig, learnerConfig)
learner.initialize(samples)
grid = learner.getGrid()
print learner.getDim(), learner.getNsamples()
print learner.getGrid()
print learner.getSurpluses()
# dist = SGDEdist.byLearnerSGDE(samples, gridConfig, adaptConfig, solverConfig,
# regularizationConfig, learnerConfig)
2 changes: 1 addition & 1 deletion datadriven/python/learner/LearnerBuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ def __getattr__(self, attr):
raise AttributeError, "Not all attributes assigned to create grid"
if self.__border != None:
if self.__border == BorderTypes.TRAPEZOIDBOUNDARY:
grid = Grid.createLinearBoundaryGrid(self.__dim)
grid = Grid.createLinearBoundaryGrid(self.__dim, 1)
elif self.__border == BorderTypes.COMPLETEBOUNDARY:
grid = Grid.createLinearBoundaryGrid(self.__dim, 0)
else:
Expand Down
7 changes: 4 additions & 3 deletions datadriven/python/uq/dists/SGDEdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ def __init__(self, grid, alpha, trainData=None,
# print "Vol: %g" % (self.scale - self.fmin)

@classmethod
def byLearnerSGDE(cls, samples, gridConfig, adaptConfig, solverConfig,
regularizationConfig, learnerConfig):
def byLearnerSGDE(cls, samples, gridConfig, adaptConfig,
solverConfig, regularizationConfig,
learnerConfig):
learner = LearnerSGDE(gridConfig, adaptConfig, solverConfig,
regularizationConfig, learnerConfig)
regularizationConfig, learnerConfig)
learner.initialize(samples)
return cls(learner.getGrid(), learner.getSurpluses(), trainData=samples.array())

Expand Down
31 changes: 20 additions & 11 deletions datadriven/python/uq/plot/plot2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,34 @@ def plotFunction2d(f, addContour=True, n=101,
xlim=[0, 1], ylim=[0, 1]):
x = np.linspace(xlim[0], xlim[1], n)
y = np.linspace(ylim[0], ylim[1], n)
X, Y = np.meshgrid(x, y)
# X, Y = np.meshgrid(x, y)
Z = np.ones(n * n).reshape(n, n)

for i in xrange(len(X)):
for j, (xi, yi) in enumerate(zip(X[i], Y[i])):
Z[i, j] = f(xi, 1 - yi)
xv, yv = np.meshgrid(x, y, sparse=False, indexing='xy')
for i in range(len(x)):
for j in range(len(y)):
Z[j, i] = f(xv[j, i], yv[j, i])

plt.imshow(Z, interpolation='bilinear', extent=(0, 1, 0, 1))
# for i in xrange(len(X)):
# for j, (xi, yi) in enumerate(zip(X[i], Y[i])):
# Z[i, j] = f(xi, 1 - yi)

plt.imshow(Z[::-1, :], interpolation='bilinear',
extent=(xlim[0], xlim[1], xlim[0], xlim[1]))

plt.jet()
plt.colorbar()

if addContour:
cs = plt.contour(X, 1 - Y, Z, colors='black')
cs = plt.contour(xv, yv, Z, colors='black')
# cs = plt.contour(X, 1 - Y, Z, colors='black')
plt.clabel(cs, inline=1, fontsize=18)

return


def plotSG2d(grid, alpha, addContour=True, n=100):
def plotSG2d(grid, alpha, addContour=True, n=100,
show_negative=False, show_grid_points=False):
gs = grid.getStorage()

gpxp = []
Expand All @@ -104,7 +112,7 @@ def plotSG2d(grid, alpha, addContour=True, n=100):
gpxn = []
gpyn = []

for i in xrange(gs.size()):
for i in xrange(gs.getSize()):
if alpha[i] > 0:
gpxp.append(gs.get(i).getCoord(0))
gpyp.append(gs.get(i).getCoord(1))
Expand Down Expand Up @@ -146,13 +154,14 @@ def plotSG2d(grid, alpha, addContour=True, n=100):

plt.imshow(Z, interpolation='bilinear', extent=(0, 1, 0, 1))

if len(neg_z) > 0:
if len(neg_z) > 0 and show_negative:
plt.plot(neg_x, neg_y, linestyle=' ', marker='o', color='red')
plt.title("[%g, %g]" % (min(neg_z), max(neg_z)))

# plot surpluses
plt.plot(gpxp, gpyp, "^ ", color="white")
plt.plot(gpxn, gpyn, "v ", color="red")
if show_grid_points:
plt.plot(gpxp, gpyp, "^ ", color="white")
plt.plot(gpxn, gpyn, "v ", color="red")

plt.jet()
plt.colorbar()
Expand Down
22 changes: 12 additions & 10 deletions datadriven/python/uq/plot/plot3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def plotDensity3d(U, n=50):
for j, (x, y) in enumerate(zip(X[i], Y[i])):
Z[i, j] = U.pdf([x, y])

surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1,
cmap=cm.coolwarm, linewidth=0, antialiased=False)

ax.set_xlim(xlim[0], xlim[1])
ax.set_ylim(ylim[0], ylim[1])
Expand All @@ -48,20 +48,21 @@ def plotSG3d(grid, alpha, n=50, f=lambda x: x):

# get grid points
gs = grid.getStorage()
gps = np.zeros([gs.size(), 2])
gps = np.zeros([gs.getSize(), 2])
p = DataVector(2)
for i in xrange(gs.size()):
for i in xrange(gs.getSize()):
gs.get(i).getCoords(p)
gps[i, :] = p.array()

surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
ax.scatter(gps[:, 0], gps[:, 1], np.zeros(gs.size()))
ax.plot_wireframe(X, Y, Z)
# surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm,
# linewidth=0, antialiased=False)
ax.scatter(gps[:, 0], gps[:, 1], np.zeros(gs.getSize()))
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
# ax.set_zlim(0, 2)

fig.colorbar(surf, shrink=0.5, aspect=5)
# fig.colorbar(surf, shrink=0.5, aspect=5)
return fig, ax, Z


Expand Down Expand Up @@ -91,10 +92,10 @@ def plotFunction3d(f, xlim=[0, 1], ylim=[0, 1], n=50):
def plotSGNodal3d(grid, alpha):
gs = grid.getStorage()

A = np.ndarray([gs.size(), 3])
A = np.ndarray([gs.getSize(), 3])

p = DataVector(2)
for i in xrange(gs.size()):
for i in xrange(gs.getSize()):
gs.get(i).getCoords(p)
A[i, 0] = p[0]
A[i, 1] = p[1]
Expand All @@ -106,6 +107,7 @@ def plotSGNodal3d(grid, alpha):
def plotNodal3d(A):
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax.scatter(A[:, 0], A[:, 1], A[:, 2])

return fig, ax
Expand Down
2 changes: 1 addition & 1 deletion datadriven/src/sgpp/datadriven/application/LearnerSGDE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ void LearnerSGDE::train(base::Grid& grid, base::DataVector& alpha, base::DataMat
}

double LearnerSGDE::computeResidual(base::Grid& grid, base::DataVector& alpha,
base::DataMatrix& test, double lambdaReg) {
base::DataMatrix& test, double lambdaReg) {
std::unique_ptr<base::OperationMatrix> C = computeRegularizationMatrix(grid);

base::DataVector rhs(grid.getSize());
Expand Down
4 changes: 2 additions & 2 deletions datadriven/src/sgpp/datadriven/application/LearnerSGDE.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class LearnerSGDE : public datadriven::DensityEstimator {
*
* @param samples DataMatrix (nrows = number of samples, ncols = dimensionality)
*/
void initialize(base::DataMatrix& samples);
virtual void initialize(base::DataMatrix& samples);

/**
* This methods evaluates the sparse grid density at a single point
Expand Down Expand Up @@ -162,7 +162,7 @@ class LearnerSGDE : public datadriven::DensityEstimator {
* @return
*/
double computeResidual(base::Grid& grid, base::DataVector& alpha, base::DataMatrix& test,
double lambdaReg);
double lambdaReg);

/**
* generates the regularization matrix
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,9 @@ void OperationInverseRosenblattTransformationLinear::doTransformation_in_next_di
return;
}

double OperationInverseRosenblattTransformationLinear::doTransformation1D(
base::Grid* grid1d, base::DataVector* alpha1d, double coord1d) {
double OperationInverseRosenblattTransformationLinear::doTransformation1D(base::Grid* grid1d,
base::DataVector* alpha1d,
double coord1d) {
/***************** STEP 1. Compute CDF ********************/

// compute PDF, sort by coordinates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ void OperationRosenblattTransformationLinear::doTransformation_in_next_dim(
}

double OperationRosenblattTransformationLinear::doTransformation1D(base::Grid* grid1d,
base::DataVector* alpha1d,
double coord1d) {
base::DataVector* alpha1d,
double coord1d) {
/***************** STEP 1. Compute CDF ********************/

// compute PDF, sort by coordinates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ class OperationRosenblattTransformationLinear : public OperationRosenblattTransf
void doTransformation_in_next_dim(base::Grid* g_in, base::DataVector* a_in, size_t dim_x,
base::DataVector* coords1d, base::DataVector* cdfs1d,
size_t& curr_dim);
virtual double doTransformation1D(base::Grid* grid1d, base::DataVector* alpha1d,
double coord1d);
virtual double doTransformation1D(base::Grid* grid1d, base::DataVector* alpha1d, double coord1d);
};

} // namespace datadriven
Expand Down

0 comments on commit 9514d92

Please sign in to comment.