openturns / openturns

Uncertainty treatment library
http://openturns.github.io/openturns/latest/index.html
Other
230 stars 93 forks source link

There is no example of OpenTURNSPythonFunction #1780

Open mbaudin47 opened 3 years ago

mbaudin47 commented 3 years ago

There should be a practical example of the OpenTURNSPythonFunction. This example should have a state and show how to use this state within the function evaluation. For example, it may show to to create the Ishigami model and allow to customize the parameters a and b.

This example is crucial e.g. for those of us who wrap complex models, which includes file management, parallel computing, etc...

Here is a template of class:

import openturns as ot

class IshigamiFunction(OpenTURNSPythonFunction):
    def __init__(self, a = 7.0, b = 0.1):
        super(IshigamiFunction, self).__init__(3, 1)
        # TODO write the code

    def _exec(self, X):
        # TODO write the code to evaluate the output
        return Y

ishigami_function = IshigamiFunction()
myFunc = Function(ishigami_function)

@josephmure : don't you agree?

mbaudin47 commented 3 years ago

@efekhari27 : May you point to the otwrapy example?

josephmure commented 3 years ago

I agree.

efekhari27 commented 3 years ago

Hi all, Thanks Michaël, I agree it would be interesting. Here is the otwrapy example I mentioned earlier today otwrapy example. Best, Elias

mbaudin47 commented 3 years ago

Thanks! A simplified version of the otwrapy example, without file management and with more input arguments in the constructor (e.g. some parameters of the distribution such as the means and standard variations of Gaussian inputs, etc...), should do the job of documenting the feature. The comment in the example should, however, mention that the feature is especially appropriate when wrapping a code, e.g. with coupling tools . This should allow the user to customize the two examples to their needs, while allowing the example to be as focused as possible.

mbaudin47 commented 3 years ago

There is another example there: https://github.com/mbaudin47/hpcuqtraining/blob/9ed990fb4873c19bd9f028d56a2ad9182c1fe9d6/2019/Scripts/wrapper.py#L41

josephmure commented 2 years ago

Hi @mbaudin47! In practice, I wonder whether examples without file management could not be handled with a simple PythonFunction (possibly used with ParametricFunction).

And if file management is required, then the example should perhaps rather be added to the otwrapy doc.

josephmure commented 2 years ago

To produce a non-file-management example that does something difficult to achieve with PythonFunction + ParametricFunction, we would need to come up with an _exec method that relies on some non-numeric attribute that would provide a service. I have something like this in mind:

import openturns as ot

class Quantile(ot.OpenTURNSPythonFunction):
    def __init__(self, distribution):
        super().__init__(1, 1)
        self._distribution = distribution

    def _exec(self, point):
        return self._distribution.computeQuantile(point[0])

    def _exec_sample(self, X):
        sample = ot.Sample(X) # X is a Buffer object, not a Sample, so it does not have the asPoint method
        return self._distribution.computeQuantile(sample.asPoint())

# Create a WeibullMax quantile function
weibullmax_quantile = Quantile(ot.WeibullMax(1.3, 2.7))
weibullmax_quantile_function = ot.Function(weibullmax_quantile)

# Apply the Function
print(weibullmax_quantile_function([0.05]))

Output: [-1.95176]

Of course, this Function is by itself pretty useless but it could be composed with something. What do you think @efekhari27 ?

mbaudin47 commented 2 years ago

To get your sample input work, convert it into a Sample, otherwise it is a MemoryView. Your script is interesting, but does not correspond to my purpose. Another example would be an ODE solver, with attributes such as the name of the algorithm, the initial condition and the parameters. Some of these parameters could be booleans or integers. This would better match the interface of a complex computer model, say Code_Saturne for example.