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 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
|
#! /usr/bin/env python
import openturns as ot
import openturns.testing
import os
import math as m
ot.TESTPREAMBLE()
try:
fileName = "myStudySaveLoad.xml"
# Create a Study Object by name
myStudy = ot.Study(fileName)
point = ot.Point(2, 1.0)
myStudy.add("point", point)
myStudy.save()
myStudy2 = ot.Study(fileName)
myStudy2.load()
point2 = ot.Point()
myStudy2.fillObject("point", point2)
# cleanup
os.remove(fileName)
# Create a Study Object with compression
myStudy = ot.Study()
compressionLevel = 5
myStudy.setStorageManager(ot.XMLStorageManager(fileName + ".gz", compressionLevel))
point = ot.Point(2, 1.0)
myStudy.add("point", point)
myStudy.save()
myStudy2 = ot.Study(fileName + ".gz")
myStudy2.load()
point2 = ot.Point()
myStudy2.fillObject("point", point2)
# cleanup
os.remove(fileName + ".gz")
# Create a Study Object with compression, direct way
compressionLevel = 5
myStudy = ot.Study(fileName, compressionLevel)
point = ot.Point(2, 1.0)
myStudy.add("point", point)
myStudy.save()
myStudy2 = ot.Study(fileName)
myStudy2.load()
point2 = ot.Point()
myStudy2.fillObject("point", point2)
# cleanup
os.remove(fileName)
# Create a Study Object
myStudy = ot.Study()
myStudy.setStorageManager(ot.XMLStorageManager(fileName))
# Add a PersistentObject to the Study (here a Point)
p1 = ot.Point(3, 0.0)
p1.setName("Good")
p1[0] = 10.0
p1[1] = 11.0
p1[2] = 12.0
myStudy.add(p1)
# Add another PersistentObject to the Study (here a Sample)
s1 = ot.Sample(3, 2)
s1.setName("mySample")
p2 = ot.Point(2, 0.0)
p2.setName("One")
p2[0] = 100.0
p2[1] = 200.0
s1[0] = p2
p3 = ot.Point(2, 0.0)
p3.setName("Two")
p3[0] = 101.0
p3[1] = 201.0
s1[1] = p3
p4 = ot.Point(2, 0.0)
p4.setName("Three")
p4[0] = 102.0
p4[1] = 202.0
s1[2] = p4
myStudy.add("mySample", s1)
# Add a point with a description
pDesc = ot.PointWithDescription(p1)
desc = pDesc.getDescription()
desc[0] = "x"
desc[1] = "y"
desc[2] = "z"
pDesc.setDescription(desc)
myStudy.add(pDesc)
# Add a matrix
matrix = ot.Matrix(2, 3)
matrix[0, 0] = 0
matrix[0, 1] = 1
matrix[0, 2] = 2
matrix[1, 0] = 3
matrix[1, 1] = 4
matrix[1, 2] = 5
myStudy.add("m", matrix)
# Create a Point that we will try to reinstaciate after reloading
point = ot.Point(2, 1000.0)
point.setName("point")
myStudy.add("point", point)
# Create a Simulation::Result
simulationResult = ot.ProbabilitySimulationResult(
ot.ThresholdEvent(), 0.5, 0.01, 150, 4
)
myStudy.add("simulationResult", simulationResult)
cNameList = [
"DirectionalSampling",
"SimulationSensitivityAnalysis",
"ProbabilitySimulationAlgorithm",
]
for cName in cNameList:
otClass = getattr(ot, cName)
instance = otClass()
print("--", cName, instance)
myStudy.add(cName, instance)
# Create a Beta distribution
beta = ot.Beta(3.0, 2.0, -1.0, 4.0)
myStudy.add("beta", beta)
# Create an analytical Function
input = ot.Description(3)
input[0] = "a"
input[1] = "b"
input[2] = "c"
formulas = ot.Description(3)
formulas[0] = "a+b+c"
formulas[1] = "a-b*c"
formulas[2] = "(a+2*b^2+3*c^3)/6"
analytical = ot.SymbolicFunction(input, formulas)
analytical.setName("analytical")
analytical.setOutputDescription(["z1", "z2", "z3"])
myStudy.add("analytical", analytical)
# Create a TaylorExpansionMoments algorithm
antecedent = ot.RandomVector(ot.IndependentCopula(analytical.getInputDimension()))
antecedent.setName("antecedent")
composite = ot.CompositeRandomVector(analytical, antecedent)
composite.setName("composite")
taylorExpansionsMoments = ot.TaylorExpansionMoments(composite)
taylorExpansionsMoments.setName("taylorExpansionsMoments")
taylorExpansionsMoments.getMeanFirstOrder()
taylorExpansionsMoments.getMeanSecondOrder()
taylorExpansionsMoments.getCovariance()
myStudy.add("taylorExpansionsMoments", taylorExpansionsMoments)
# Create a FORMResult
input2 = ot.Description(2)
input2[0] = "x"
input2[1] = "y"
formula2 = ot.Description(1)
formula2[0] = "y^2-x"
model = ot.SymbolicFunction(input2, formula2)
model.setName("sum")
input3 = ot.RandomVector(ot.Normal(2))
input3.setName("input")
output3 = ot.CompositeRandomVector(model, input3)
output3.setName("output")
event = ot.ThresholdEvent(output3, ot.Greater(), 1.0)
event.setName("failureEvent")
designPoint = ot.Point(2, 0.0)
designPoint[0] = 1.0
formResult = ot.FORMResult(ot.Point(2, 1.0), event, False)
formResult.setName("formResult")
formResult.getImportanceFactors()
formResult.getEventProbabilitySensitivity()
myStudy.add("formResult", formResult)
# Create a SORMResult
sormResult = ot.SORMResult([1.0] * 2, event, False)
sormResult.setName("sormResult")
sormResult.getEventProbabilityBreitung()
sormResult.getEventProbabilityHohenbichler()
sormResult.getEventProbabilityTvedt()
sormResult.getGeneralisedReliabilityIndexBreitung()
sormResult.getGeneralisedReliabilityIndexHohenbichler()
sormResult.getGeneralisedReliabilityIndexTvedt()
myStudy.add("sormResult", sormResult)
# Create a RandomGeneratorState
ot.RandomGenerator.SetSeed(0)
randomGeneratorState = ot.RandomGeneratorState(ot.RandomGenerator.GetState())
myStudy.add("randomGeneratorState", randomGeneratorState)
# Create a GeneralLinearModelResult
generalizedLinearModelResult = ot.GeneralLinearModelResult()
generalizedLinearModelResult.setName("generalizedLinearModelResult")
myStudy.add("generalizedLinearModelResult", generalizedLinearModelResult)
# KDTree
sample = ot.Normal(3).getSample(10)
kDTree = ot.KDTree(sample)
myStudy.add("kDTree", kDTree)
# Distribution parameters
# ArcsineMuSigma parameter ave
ams_parameters = ot.ArcsineMuSigma(8.4, 2.25)
myStudy.add("ams_parameters", ams_parameters)
# BetaMuSigma parameter save
bms_parameters = ot.BetaMuSigma(0.2, 0.6, -1, 2)
myStudy.add("bms_parameters", bms_parameters)
# GammaMuSigma parameter save
gmms_parameters = ot.GammaMuSigma(1.5, 2.5, -0.5)
myStudy.add("gmms_parameters", gmms_parameters)
# GumbelMuSigma parameter save
gms_parameters = ot.GumbelMuSigma(1.5, 1.3)
myStudy.add("gms_parameters", gms_parameters)
# LogNormalMuSigma parameter save
lnms_parameters = ot.LogNormalMuSigma(30000.0, 9000.0, 15000)
myStudy.add("lnms_parameters", lnms_parameters)
# LogNormalMuSigmaOverMu parameter save
lnmsm_parameters = ot.LogNormalMuSigmaOverMu(0.63, 5.24, -0.5)
myStudy.add("lnmsm_parameters", lnmsm_parameters)
# WeibullMinMuSigma parameter save
wms_parameters = ot.WeibullMinMuSigma(1.3, 1.23, -0.5)
myStudy.add("wms_parameters", wms_parameters)
# MemoizeFunction
f = ot.SymbolicFunction(["x1", "x2"], ["x1*x2"])
memoize = ot.MemoizeFunction(f)
memoize([5, 6])
myStudy.add("memoize", memoize)
# print ('Study = ' , myStudy)
myStudy.save()
# Create a new Study Object
myStudy = ot.Study()
myStudy.setStorageManager(ot.XMLStorageManager(fileName))
myStudy.load()
# print 'loaded Study = ' , myStudy
# MemoizeFunction
memoize = ot.MemoizeFunction()
myStudy.fillObject("memoize", memoize)
print("memoize = ", repr(memoize))
memoize([5, 6])
print("memoize.getCacheHits()=", memoize.getCacheHits())
# Create a Point from the one stored in the Study
point = ot.Point()
myStudy.fillObject("point", point)
print("point = ", repr(point))
# Create a Sample from the one stored in the Study
sample = ot.Sample()
myStudy.fillObject("mySample", sample)
print("sample = ", repr(sample))
# Create a Matrix from the one stored in the Study
matrix = ot.Matrix()
myStudy.fillObject("m", matrix)
print("matrix = ", repr(matrix))
# Create a Simulation::Result from the one stored in the Study
simulationResult = ot.ProbabilitySimulationResult()
myStudy.fillObject("simulationResult", simulationResult)
print("simulation result = ", simulationResult)
for cName in cNameList:
otClass = getattr(ot, cName)
instance = otClass()
saved = repr(instance)
myStudy.fillObject(cName, instance)
print("--", cName, instance)
loaded = repr(instance)
if saved != loaded:
print("saved=", saved)
print("loaded=", loaded)
# Create a Beta distribution from the one stored in the Study
beta = ot.Beta()
myStudy.fillObject("beta", beta)
print("beta = ", beta)
randomGeneratorState = ot.RandomGeneratorState()
myStudy.fillObject("randomGeneratorState", randomGeneratorState)
print("randomGeneratorState = ", randomGeneratorState)
# Create an analytical Function from the one stored in the
# Study
analytical = ot.Function()
myStudy.fillObject("analytical", analytical)
print("analytical = ", analytical)
print("analytical.outputDescription=", analytical.getOutputDescription())
# Create a GeneralLinearModelResult from the one stored in the Study
generalizedLinearModelResult = ot.GeneralLinearModelResult()
myStudy.fillObject("generalizedLinearModelResult", generalizedLinearModelResult)
print("generalizedLinearModelResult = ", generalizedLinearModelResult)
# KDTree
kDTree = ot.KDTree()
myStudy.fillObject("kDTree", kDTree)
# ArcsineMuSigma parameter loading
ams_parameters = ot.ArcsineMuSigma()
myStudy.fillObject("ams_parameters", ams_parameters)
# BetaMuSigma parameter loading
bms_parameters = ot.BetaMuSigma()
myStudy.fillObject("bms_parameters", bms_parameters)
# GammaMuSigma parameter loading
gmms_parameters = ot.GammaMuSigma()
myStudy.fillObject("gmms_parameters", gmms_parameters)
# GumbelMuSigma parameter loading
gms_parameters = ot.GumbelMuSigma()
myStudy.fillObject("gms_parameters", gms_parameters)
# LogNormalMuSigma parameter loading
lnms_parameters = ot.LogNormalMuSigma()
myStudy.fillObject("lnms_parameters", lnms_parameters)
# LogNormalMuSigmaOverMu parameter loading
lnmsm_parameters = ot.LogNormalMuSigmaOverMu()
myStudy.fillObject("lnmsm_parameters", lnmsm_parameters)
# WeibullMinMuSigma parameter loading
wms_parameters = ot.WeibullMinMuSigma()
myStudy.fillObject("wms_parameters", wms_parameters)
# cleanup
os.remove(fileName)
# test nan/inf
myStudy = ot.Study(fileName)
point = ot.Point([float(x) for x in ["nan", "inf", "-inf"]])
myStudy.add("point", point)
myStudy.save()
myStudy2 = ot.Study(fileName)
myStudy2.load()
point2 = ot.Point()
myStudy2.fillObject("point", point2)
for j in range(len(point2)):
print(
"j=",
j,
"isnormal=",
m.isfinite(point2[j]),
"isnan=",
m.isnan(point2[j]),
"isinf=",
m.isinf(point2[j]),
)
# cleanup
os.remove(fileName)
except Exception:
import os
import traceback
traceback.print_exc()
os._exit(1)
|