Blog: Two and half implementations of a scilab wrapper without any compilation: scilab_with_OpenTURNSPythonFunction_and_decorator.py

File scilab_with_OpenTURNSPythonFunction_and_decorator.py, 846 bytes (added by regis.lebrun@…, 5 years ago)

Scilab wrapping using the OpenTURNSPythonFunction and a decorator

Line 
1from openturns import *
2import os
3
4def FunctionDecorator(n, p):
5  class otfunc(OpenTURNSPythonFunction):
6     
7    def __init__(self, func):
8      OpenTURNSPythonFunction.__init__(self, n, p)
9      self.__class__.__name__ = func.__name__
10      self._exec = func
11       
12  return otfunc
13
14@FunctionDecorator(2, 1)
15def SIMPLEFUNC(X):
16    f = file("input.data", "w")
17    f.write("x1 = " + str(X[0]) + "\n")
18    f.write("x2 = " + str(X[1]) + "\n")
19    f.close()
20    rc = os.system("scilab -nb -nwni -f code.sce")
21    f = file("result.data", "r")
22    Y = [float(f.readline().split(" ")[2])]
23    f.close()
24    return Y
25
26model = NumericalMathFunction( SIMPLEFUNC )
27inP = [1.0, 2.0]
28outP = model(inP)
29print "model(", inP, ")=", outP
30inS = Normal(2).getNumericalSample(100)
31Log.SetColor(Log.INFO, TTY.CYANFG)
32outS = model(inS)
33print outS.computeMean()