File: t_ParametricFunction_std.py

package info (click to toggle)
openturns 1.26-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 67,708 kB
  • sloc: cpp: 261,605; python: 67,030; ansic: 4,378; javascript: 406; sh: 185; xml: 164; makefile: 101
file content (126 lines) | stat: -rwxr-xr-x 3,658 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python

import openturns as ot
import openturns.testing as ott

f = ot.SymbolicFunction(
    ot.Description.BuildDefault(5, "x"),
    [
        "sin(x0) + x1 * cos(x2) / exp(x3)",
        "-2.0 * x0 + x1 * x2^2 + cos(x3)",
        "x0 / (abs(x1) * x2^2 + x3 + 1.0)",
    ],
)
pset = [3, 1]  # x3, x1
parametersSet = True
x = [1.0, 2.0, 0.0]  # x0, x2, x4

# with reference point
referencePoint = [0.85] * 2  # x3, x1
g2 = ot.ParametricFunction(f, pset, referencePoint, parametersSet)
print("g2=", g2)
print("g2(x)=", g2(x))

# check output description
outputDesc = ot.Description.BuildDefault(3, "g2_")
g2.setOutputDescription(outputDesc)
X = ot.Normal(3).getSample(10)
Y = g2(X)
assert Y.getDescription() == outputDesc, "wrong output description"

# with complementary parameter set
referencePoint = [1.0, 2.0, 0.0]  # x0, x2, x4
x = [0.85] * 2  # x3, x1
parametersSet = False
g3 = ot.ParametricFunction(f, pset, referencePoint, parametersSet)
print("g3=", g3)
print("g3(x)=", g3(x))

# From ticket #1092
modelFunc = ot.SymbolicFunction(["s", "r", "c"], ["r + c"])
s = [1.0]
r = 1e9
c = 1e8
theta = ot.Point([r, c])
calibratedIndices = [1, 2]
mycf = ot.ParametricFunction(modelFunc, calibratedIndices, theta)

print(modelFunc.gradient([s[0], r, c]).transpose())
print(mycf.parameterGradient(s).transpose())

# Check if parametric functions and memoize functions work well together

n_calls = 0


def py_f(X):
    global n_calls
    n_calls += 1
    return X


ot_f = ot.MemoizeFunction(ot.PythonFunction(3, 3, py_f))
param_f = ot.ParametricFunction(ot_f, [0, 1], [1.0, 2.0])

x = [3.0]
y = [1.5]
n_calls_0 = ot_f.getEvaluationCallsNumber()
par_grad = param_f.parameterGradient(x)
n_calls_1 = ot_f.getEvaluationCallsNumber()
assert (
    n_calls_1 - n_calls_0 == 4
), "Expected n_calls_1 - n_calls_0 == 4, here n_calls_1 - n_calls_0 == " + str(
    n_calls_1 - n_calls_0
)
assert n_calls == 4, "Expected n_calls == 4, here n_calls == " + str(n_calls)

n_calls = 0
n_calls_0 = ot_f.getEvaluationCallsNumber()
f_grad = param_f.gradient(y)
n_calls_1 = ot_f.getEvaluationCallsNumber()
assert (
    n_calls_1 - n_calls_0 == 2
), "Expected n_calls_1 - n_calls_0 == 2, here n_calls_1 - n_calls_0 == " + str(
    n_calls_1 - n_calls_0
)
assert n_calls == 2, "Expected n_calls == 2, here n_calls == " + str(n_calls)

eps = 1e-7
gr_f = ot.NonCenteredFiniteDifferenceGradient(eps, ot_f.getEvaluation())
ot_f.setGradient(gr_f)
n_calls = 0
param_f = ot.ParametricFunction(ot_f, [0, 1], [1.0, 2.0])

x = [3.5]
y = [2.5]
n_calls_0 = ot_f.getEvaluationCallsNumber()
par_grad = param_f.parameterGradient(x)
n_calls_1 = ot_f.getEvaluationCallsNumber()
assert (
    n_calls_1 - n_calls_0 == 3
), "Expected n_calls_1 - n_calls_0 == 3, here n_calls_1 - n_calls_0 == " + str(
    n_calls_1 - n_calls_0
)
assert n_calls == 3, "Expected n_calls == 3, here n_calls == " + str(n_calls)

n_calls = 0
n_calls_0 = ot_f.getEvaluationCallsNumber()
f_grad = param_f.gradient(y)
n_calls_1 = ot_f.getEvaluationCallsNumber()
assert (
    n_calls_1 - n_calls_0 == 2
), "Expected n_calls_1 - n_calls_0 == 2, here n_calls_1 - n_calls_0 == " + str(
    n_calls_1 - n_calls_0
)
assert n_calls == 2, "Expected n_calls == 2, here n_calls == " + str(n_calls)

# check parameters name are shown
html_str = g2._repr_html_()
assert "[x3 : 0.85, x1 : 0.85]" in html_str, "no params"

# check parameters order
f1 = ot.SymbolicFunction(["x1", "x2", "x3"], ["2*x1+3*x2+5*x3"])
f1.setGradient(ot.NonCenteredFiniteDifferenceGradient([1e-5] * 3, f1.getEvaluation()))
f2 = ot.ParametricFunction(f1, [1, 0], [4.0, 5.0])
pgrad = f2.parameterGradient([7.0])
ott.assert_almost_equal(pgrad[0, 0], 3.0)