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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
#! /usr/bin/env python
from openturns.viewer import View
import openturns as ot
# Curve
graph = ot.Normal().drawCDF()
view = View(graph, pixelsize=(800, 600), plot_kw={"color": "blue"})
# view.save('curve1.png')
view.show()
# Contour
graph = ot.Normal([1, 2], [3, 5], ot.CorrelationMatrix(2)).drawPDF()
view = View(graph)
# view.save('curve2.png')
view.show()
# Histogram tests
normal = ot.Normal(1)
size = 100
sample = normal.getSample(size)
graph = ot.HistogramFactory().build(sample, 10).drawPDF()
view = View(graph)
# view.save('curve3.png')
view.show()
# QQPlot tests
size = 100
normal = ot.Normal(1)
sample = normal.getSample(size)
sample2 = ot.Gamma(3.0, 4.0, 0.0).getSample(size)
graph = ot.VisualTest.DrawQQplot(sample, sample2, 100)
view = View(graph)
# view.save('curve4.png')
view.ShowAll(block=True)
# Clouds tests
dimension = 2
R = ot.CorrelationMatrix(dimension)
R[0, 1] = 0.8
distribution = ot.Normal(ot.Point(dimension, 3.0), ot.Point(dimension, 2.0), R)
size = 100
sample1 = ot.Normal([3.0] * dimension, [2.0] * dimension, R).getSample(size)
sample2 = ot.Normal([2.0] * dimension, [3.0] * dimension, R).getSample(size // 2)
cloud1 = ot.Cloud(sample1, "blue", "fsquare", "Sample1 Cloud")
cloud2 = ot.Cloud(sample2, "red", "fcircle", "Sample2 Cloud")
graph = ot.Graph("two samples clouds", "x1", "x2", True, "upper right")
graph.add(cloud1)
graph.add(cloud2)
view = View(graph)
# view.save('curve5.png')
view.show()
# Cloud, empty legend
graph = ot.Graph("two samples clouds", "x1", "x2", True, "upper right")
cloud1 = ot.Cloud(sample1)
graph.add(cloud1)
view = View(graph)
view.show()
# Text
graph = ot.Graph("Annotated cloud", "x", "y", True, "")
distribution = ot.Normal(2)
size = 30
sample2D = distribution.getSample(size)
cloud = ot.Cloud(sample2D, "red", "fsquare", "Sample2D Cloud")
graph.add(cloud)
# Display extrema indices
x1 = [x[0] for x in sample2D[:, 0]]
x2 = [x[0] for x in sample2D[:, 1]]
idx = [0] * 4
idx[0] = x1.index(min(x1))
idx[1] = x1.index(max(x1))
idx[2] = x2.index(min(x2))
idx[3] = x2.index(max(x2))
labels = ot.Description(sample2D.getSize())
for i in range(4):
labels[idx[i]] = str(idx[i])
position = ot.Description(sample2D.getSize(), "top")
position[idx[0]] = "right"
position[idx[1]] = "left"
position[idx[2]] = "top"
position[idx[3]] = "bottom"
text = ot.Text(sample2D, labels)
text.setColor("red")
text.setTextPositions(position)
graph.add(text)
view = View(graph)
view.show()
# CobWeb tests
size = 100
dim = 6
inputSample = ot.Normal(dim).getSample(size)
inputVar = list(["X" + str(i) for i in range(dim)])
expression = ""
for i in range(dim):
if i > 0:
expression += "+"
expression += "cos(" + str(i + 1) + "*" + inputVar[i] + ")"
model = ot.SymbolicFunction(inputVar, [expression])
outputSample = model(inputSample)
graph = ot.VisualTest.DrawParallelCoordinates(
inputSample, outputSample, 2.5, 3.0, "red", False
)
view = View(graph, legend_kw={"loc": "lower center"})
# view.save('curve6.png')
view.show()
# Staircase
distribution = ot.Poisson(10.0)
graph = distribution.drawCDF()
view = View(graph)
# view.save('curve7.png')
view.ShowAll(block=True)
# Pie
graph = ot.SobolIndicesAlgorithm.DrawImportanceFactors(
[0.4, 0.3, 0.2, 0.1], ["a0", "a1", "a2", "a3"], "Zou"
)
view = View(graph)
# view.save('curve8.png')
view.show()
# Convergence graph curve
aCollection = []
aCollection.append(
ot.LogNormalFactory().build(ot.LogNormalMuSigma()([300.0, 30.0, 0.0]))
)
aCollection.append(ot.Normal(75e3, 5e3))
myDistribution = ot.JointDistribution(aCollection)
vect = ot.RandomVector(myDistribution)
LimitState = ot.SymbolicFunction(("R", "F"), ("R-F/(pi_*100.0)",))
G = ot.CompositeRandomVector(LimitState, vect)
myEvent = ot.ThresholdEvent(G, ot.Less(), 0.0)
experiment = ot.MonteCarloExperiment()
myAlgo = ot.ProbabilitySimulationAlgorithm(myEvent, experiment)
myAlgo.setMaximumCoefficientOfVariation(0.05)
myAlgo.setMaximumOuterSampling(int(1e5))
myAlgo.run()
graph = myAlgo.drawProbabilityConvergence()
view = View(graph)
# view.save('curve10.png')
view.show()
# Polygon
size = 50
cursor = [0.0] * 2
data1 = ot.Sample(size, 2) # polygon y = 2x for x in [-25]
data2 = ot.Sample(size, 2) # polygon y = x*x for x in [-11]
for i in range(size):
tmp = 7.0 * i / size + 2
cursor[0] = tmp
cursor[1] = 2 * tmp
data1[i] = cursor
tmp = 9.0 * i / size + 1
cursor[0] = tmp
cursor[1] = tmp * tmp
data2[i] = cursor
graph = ot.Graph("Some polygons", "x1", "x2", True, "upper right")
myPolygon1 = ot.Polygon(data1)
myPolygon1.setColor("blue")
myPolygon1.setLegend("polygon 1")
graph.add(myPolygon1)
myPolygon2 = ot.Polygon(data2)
myPolygon2.setColor("red")
myPolygon2.setLegend("polygon 2")
graph.add(myPolygon2)
view = View(graph)
# view.save('curve11.png')
view.ShowAll(block=True)
# PolygonArray
generator = ot.Normal(2)
size = 5
array = [None] * size
palette = ot.Drawable.BuildDefaultPalette(size)
palette[0] = "blue"
for i in range(size):
vertices = generator.getSample(3)
array[i] = ot.Polygon(vertices, palette[i], palette[size - i - 1])
graph = ot.Graph("An array of polygons", "x", "y", True, "upper right")
parray = ot.PolygonArray(array)
parray.setLegend("array of polys")
graph.add(parray)
view = View(graph)
# view.save('curve12.png')
# BipartiteGraph
graph = ot.BipartiteGraph([[0, 1, 2], [0, 1, 2]]).draw()
view = View(graph)
# BarPlot: FORM reliability index marginal parameter sensitivity
f = ot.SymbolicFunction(["E", "F", "L", "I"], ["-F*L^3/(3*E*I)"])
dim = f.getInputDimension()
mean = [50.0, 1.0, 10.0, 5.0]
sigma = ot.Point(dim, 1.0)
R = ot.IdentityMatrix(dim)
distribution = ot.Normal(mean, sigma, R)
vect = ot.RandomVector(distribution)
output = ot.CompositeRandomVector(f, vect)
event = ot.ThresholdEvent(output, ot.Less(), -3.0)
solver = ot.Cobyla()
solver.setMaximumCallsNumber(400)
solver.setMaximumAbsoluteError(1.0e-10)
solver.setMaximumRelativeError(1.0e-10)
solver.setMaximumResidualError(1.0e-10)
solver.setMaximumConstraintError(1.0e-10)
algo = ot.FORM(solver, event, mean)
algo.run()
result = algo.getResult()
graph, _ = result.drawHasoferReliabilityIndexSensitivity()
view = View(graph)
# Optimization error history
opt_result = result.getOptimizationResult()
graph = opt_result.drawErrorHistory()
view = View(graph)
view.ShowAll(block=True)
# GridLayout
grid = ot.GridLayout(2, 3)
palette = ot.Drawable.BuildDefaultPalette(10)
for j in range(grid.getNbColumns()):
alpha = 1.0 + j
pdf_curve = ot.WeibullMin(1.0, alpha, 0.0).drawPDF()
cdf_curve = ot.WeibullMin(1.0, alpha, 0.0).drawCDF()
pdf_curve.setColors([palette[j]])
cdf_curve.setColors([palette[j]])
legends = [f"alpha={alpha}"]
pdf_curve.setLegends(legends)
cdf_curve.setLegends(legends)
grid.setGraph(0, j, pdf_curve)
grid.setGraph(1, j, cdf_curve)
view = View(grid)
# Square axes
graph = ot.ClaytonCopula(5.0).drawPDF()
view = View(graph, square_axes=True)
# Show axes as prescribed by getAxes()
graph = ot.Normal().drawPDF()
graph.setAxes(False)
view = View(graph)
# test _repr_png_
png = graph._repr_png_()
assert b"PNG" in png[:10]
# BuildDefaultPalette, BuildTableauPalette
ncurves = 5
graph = ot.Graph("BuildPalette", "X", "Y", True, "upper right")
n = 20
x = ot.Sample([[i] for i in range(n)])
for i in range(ncurves):
y = ot.Normal().getSample(n)
curve = ot.Curve(x, y)
curve.setLegend("Curve #%d" % (i))
graph.add(curve)
palette = ot.Drawable.BuildDefaultPalette(ncurves)
graph.setColors(palette)
view = View(graph)
palette = ot.Drawable.BuildTableauPalette(ncurves)
graph.setColors(palette)
view = View(graph)
# mixed legend
f = ot.SymbolicFunction(["x", "y"], ["sin(x)*sin(y)"])
# use different numbers of points for x and y to check
# these numbers are handled properly by the viewer
graph = f.draw([-4.0] * 2, [4.0] * 2, [64, 32])
curve = ot.Curve([-4.0, 4.0], [1.0, 1.0], "curve")
curve.setColor("black")
curve.setLineStyle("dashed")
graph.add(curve)
cloud = ot.Cloud(ot.Normal(2).getSample(100), "cloud")
graph.add(cloud)
view = View(graph)
# contour bug
outputSample = ot.Uniform().getSample(49)
x = [[-3.58], [-2.38667], [-1.19333], [0], [1.19333], [2.38667], [3.58]]
y = [[-3.58], [-2.38667], [-1.19333], [0], [1.19333], [2.38667], [3.58]]
contour = ot.Contour(x, y, outputSample)
contour.setLevels([0.0])
contour.setLabels(["0.0"])
graph = ot.Graph("", "", "", True)
graph.add(contour)
view = View(graph)
view.ShowAll(block=True)
|