Skip to content

Commit

Permalink
ProjectionStrategy: pretty-print
Browse files Browse the repository at this point in the history
  • Loading branch information
mbaudin47 committed Aug 24, 2023
1 parent 1d861ea commit df95c19
Show file tree
Hide file tree
Showing 18 changed files with 549 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ Point ProjectionStrategy::getCoefficients() const
return getImplementation()->getCoefficients();
}

/* Design proxy accessor */
DesignProxy ProjectionStrategy::getDesignProxy() const
{
return getImplementation()->getDesignProxy();
}

/* Compute the components alpha_k_p_ by projecting the model on the partial L2 basis */
void ProjectionStrategy::computeCoefficients(const Function & function,
Expand All @@ -153,9 +158,9 @@ String ProjectionStrategy::__repr__() const


/* String converter */
String ProjectionStrategy::__str__(const String & ) const
String ProjectionStrategy::__str__(const String & offset) const
{
return __repr__();
return OSS() << getImplementation()->__str__(offset);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "openturns/FixedExperiment.hxx"
#include "openturns/UserDefined.hxx"
#include "openturns/Exception.hxx"
#include "openturns/Os.hxx"

BEGIN_NAMESPACE_OPENTURNS

Expand Down Expand Up @@ -130,10 +131,42 @@ ProjectionStrategyImplementation * ProjectionStrategyImplementation::clone() con
/* String converter */
String ProjectionStrategyImplementation::__repr__() const
{
return OSS() << "class=" << GetClassName()
<< " measure=" << measure_;
OSS oss(false);
oss << "class=" << GetClassName()
<< " alpha_k_p=" << alpha_k_p_
<< " residual=" << residual_p_
<< " relativeError=" << relativeError_p_
<< " measure=" << measure_
<< " weightedExperiment=" << weightedExperiment_
<< " inputSample_=" << inputSample_
<< " outputSample=" << outputSample_
<< " weights_=" << weights_
<< " proxy=" << proxy_;
return oss;
}

/* String converter */
String ProjectionStrategyImplementation::__str__(const String &) const
{
return __repr_markdown__();
}

/* String converter */
String ProjectionStrategyImplementation::__repr_markdown__() const
{
OSS oss(false);
oss << GetClassName() << Os::GetEndOfLine()
<< "- coefficients: " << alpha_k_p_.getDimension() << Os::GetEndOfLine()
<< "- residual: " << residual_p_ << Os::GetEndOfLine()
<< "- relative error: " << relativeError_p_ << Os::GetEndOfLine()
<< "- measure: " << measure_.getClassName() << Os::GetEndOfLine()
<< "- weighted experiment: " << weightedExperiment_.getClassName() << Os::GetEndOfLine()
<< "- input sample: size= " << inputSample_.getSize() <<" x dimension= " << inputSample_.getDimension() << Os::GetEndOfLine()
<< "- output sample: size= " << outputSample_.getSize() <<" x dimension= " << outputSample_.getDimension() << Os::GetEndOfLine()
<< "- weights: dimension= " << weights_.getDimension() << Os::GetEndOfLine()
<< "- design: size= " << proxy_.getSampleSize() << Os::GetEndOfLine();
return oss;
}

/* Measure accessor */
void ProjectionStrategyImplementation::setMeasure(const Distribution & measure)
Expand Down Expand Up @@ -219,6 +252,12 @@ Point ProjectionStrategyImplementation::getCoefficients() const
return alpha_k_p_;
}

/* Design proxy accessor */
DesignProxy ProjectionStrategyImplementation::getDesignProxy() const
{
return proxy_;
}

/* Compute the components alpha_k_p_ by projecting the model on the partial L2 basis */
void ProjectionStrategyImplementation::computeCoefficients(const Function &,
const FunctionCollection &,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ public:
virtual void setExperiment(const WeightedExperiment & weightedExperiment);
virtual WeightedExperiment getExperiment() const;

/** Design proxy accessor */
virtual DesignProxy getDesignProxy() const;

/** String converter */
String __repr__() const override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ public:

/** String converter */
String __repr__() const override;
String __str__(const String & offset = "") const override;
String __repr_markdown__() const;

/** Measure accessor */
virtual void setMeasure(const Distribution & measure);
Expand All @@ -102,13 +104,15 @@ public:
virtual Scalar getRelativeError() const;

/** Relative error accessor */
// virtual void setCoefficients(const Point & alpha_k);
virtual Point getCoefficients() const;

/** Experiment accessors */
virtual void setExperiment(const WeightedExperiment & weightedExperiment);
virtual WeightedExperiment getExperiment() const;

/** Design proxy accessor */
virtual DesignProxy getDesignProxy() const;

/** Method save() stores the object through the StorageManager */
void save(Advocate & adv) const override;

Expand Down
1 change: 1 addition & 0 deletions lib/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,7 @@ ot_check_test (FunctionalChaos_gsobol_sparse)
ot_check_test (FunctionalChaos_ishigami_sparse)
ot_check_test (FunctionalChaos_ishigami_database)
ot_check_test (FunctionalChaos_nd)
ot_check_test (FunctionalChaos_2d IGNOREOUT)
ot_check_test (LeastSquaresExpansion_std IGNOREOUT)
ot_check_test (IntegrationExpansion_std IGNOREOUT)
ot_check_test (KrigingAlgorithm_std)
Expand Down
94 changes: 94 additions & 0 deletions lib/test/t_FunctionalChaos_2d.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// -*- C++ -*-
/**
* @brief Test of a FunctionalChaosAlgorithm with 2 outputs
*
* Copyright 2005-2023 Airbus-EDF-IMACS-ONERA-Phimeca
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "openturns/OT.hxx"
#include "openturns/OTtestcode.hxx"

using namespace OT;
using namespace OT::Test;

int main(int, char *[])
{
TESTPREAMBLE;
OStream fullprint(std::cout);
setRandomGenerator();

try
{

// Problem parameters
const UnsignedInteger inputDimension = 2;
const UnsignedInteger outputDimension = 2;

// Create the function
Description inputVariables(inputDimension);
inputVariables[0] = "x1";
inputVariables[1] = "x2";
Description formula(outputDimension);
formula[0] = "cos(x1 + x2)";
formula[1] = "(x2 + 1) * exp(x1 - 2 * x2)";
const SymbolicFunction model(inputVariables, formula);

// Create the input distribution
Collection<Distribution> marginals(inputDimension);
for (UnsignedInteger i = 0; i < inputDimension; ++i) marginals[i] = Normal(0.0, 1.0);
const ComposedDistribution distribution(marginals);

// Create the orthogonal basis
Collection<OrthogonalUniVariatePolynomialFamily> polynomialCollection(inputDimension);
for (UnsignedInteger i = 0; i < inputDimension; ++i)
{
polynomialCollection[i] = HermiteFactory();
}
const LinearEnumerateFunction enumerateFunction(inputDimension);
const OrthogonalProductPolynomialFactory productBasis(polynomialCollection, enumerateFunction);

const UnsignedInteger degree = 4;
const UnsignedInteger basisSize = enumerateFunction.getBasisSizeFromTotalDegree(degree);
const AdaptiveStrategy adaptiveStrategy(FixedStrategy(productBasis, basisSize));


// Create the projection strategy
UnsignedInteger samplingSize = 100;
const MonteCarloExperiment experiment(distribution, samplingSize);
RandomGenerator::SetSeed(0);
const Sample X(experiment.generate());
const Sample Y(model(X));
FunctionalChaosAlgorithm algo(X, Y, distribution, adaptiveStrategy, LeastSquaresStrategy());
algo.run();

// Examine the results
const FunctionalChaosResult result(algo.getResult());
const ProjectionStrategy projectionStrategy(algo.getProjectionStrategy());
fullprint << "ProjectionStrategy (repr)= " << std::endl;
fullprint << projectionStrategy << std::endl;
std::cout << "ProjectionStrategy (str)= " << std::endl;
std::cout << projectionStrategy << std::endl;

} // try
catch (TestFailed & ex)
{
std::cerr << ex << std::endl;
return ExitCode::Error;
}


return ExitCode::Success;
}
9 changes: 9 additions & 0 deletions python/src/IntegrationStrategy.i
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,12 @@

%include openturns/IntegrationStrategy.hxx
namespace OT{ %extend IntegrationStrategy { IntegrationStrategy(const IntegrationStrategy & other) { return new OT::IntegrationStrategy(other); } } }

%pythoncode %{
def __IntegrationStrategy_repr_html(self):
"""Get HTML representation."""
html = openturns.ProjectionStrategy(self)._repr_html_()
return html

IntegrationStrategy._repr_html_ = __IntegrationStrategy_repr_html
%}
10 changes: 10 additions & 0 deletions python/src/LeastSquaresStrategy.i
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,13 @@

%include openturns/LeastSquaresStrategy.hxx
namespace OT{ %extend LeastSquaresStrategy { LeastSquaresStrategy(const LeastSquaresStrategy & other) { return new OT::LeastSquaresStrategy(other); } } }

%pythoncode %{
def __LeastSquaresStrategy_repr_html(self):
"""Get HTML representation."""
html = openturns.ProjectionStrategy(self)._repr_html_()
return html

LeastSquaresStrategy._repr_html_ = __LeastSquaresStrategy_repr_html
%}

22 changes: 22 additions & 0 deletions python/src/ProjectionStrategy.i
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,25 @@ OTTypedInterfaceObjectHelper(ProjectionStrategy)

%include openturns/ProjectionStrategy.hxx
namespace OT{ %extend ProjectionStrategy { ProjectionStrategy(const ProjectionStrategy & other) { return new OT::ProjectionStrategy(other); } } }

%pythoncode %{
def __ProjectionStrategy_repr_html(self):
"""Get HTML representation."""
html = ""
html += "<ul>\n"
html += f" <li>coefficients: dimension= {self.getCoefficients().getDimension()}</li>\n"
html += f" <li>residual: {self.getResidual()}</li>\n"
html += (
f" <li>relative error: {self.getRelativeError()}</li>\n"
)
html += f" <li>measure: {self.getMeasure().getImplementation().getClassName()}</li>\n"
html += f" <li>experiment: {self.getExperiment().getClassName()}</li>\n"
html += f" <li>input sample: size= {self.getInputSample().getSize()} x dimension= {self.getInputSample().getDimension()}</li>\n"
html += f" <li>output sample: size= {self.getOutputSample().getSize()} x dimension= {self.getOutputSample().getDimension()}</li>\n"
html += f" <li>weights: dimension= {self.getWeights().getDimension()}</li>\n"
html += f" <li>design: size= {self.getDesignProxy().getSampleSize()}</li>\n"
html += "</ul>\n"
return html

ProjectionStrategy._repr_html_ = __ProjectionStrategy_repr_html
%}
13 changes: 13 additions & 0 deletions python/src/ProjectionStrategyImplementation_doc.i.in
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,16 @@ m : Distribution
%enddef
%feature("docstring") OT::ProjectionStrategyImplementation::setMeasure
OT_ProjectionStrategy_setMeasure_doc

// ---------------------------------------------------------------------

%define OT_ProjectionStrategy_getDesignProxy_doc
"Accessor to the design proxy.

Parameters
----------
designProxy : DesignProxy
The design matrix."
%enddef
%feature("docstring") OT::ProjectionStrategyImplementation::getDesignProxy
OT_ProjectionStrategy_getDesignProxy_doc
2 changes: 2 additions & 0 deletions python/src/ProjectionStrategy_doc.i.in
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ OT_ProjectionStrategy_getWeights_doc
OT_ProjectionStrategy_setExperiment_doc
%feature("docstring") OT::ProjectionStrategy::setMeasure
OT_ProjectionStrategy_setMeasure_doc
%feature("docstring") OT::ProjectionStrategy::getDesignProxy
OT_ProjectionStrategy_getDesignProxy_doc
1 change: 1 addition & 0 deletions python/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,7 @@ ot_pyinstallcheck_test (LinearTaylor_std)
ot_pyinstallcheck_test (LinearLeastSquares_std)
ot_pyinstallcheck_test (QuadraticLeastSquares_std)
ot_pyinstallcheck_test (QuadraticTaylor_std)
ot_pyinstallcheck_test (ProjectionStrategy_std IGNOREOUT)
ot_pyinstallcheck_test (FunctionalChaos_ishigami)
ot_pyinstallcheck_test (FunctionalChaos_ishigami_sparse)
ot_pyinstallcheck_test (FunctionalChaos_ishigami_database)
Expand Down
Loading

0 comments on commit df95c19

Please sign in to comment.