6 How to save and load a study ?

The objective of this UC is to describe how to save some results obtained within a study and how to load a study performed previously, with some results.

The mechanism of OpenTURNS is detailed through the two following use cases. We give an example on a NumericalPoint and a NumericalMathFunction but it can be used for most objects.

The principle is the following one : all along the study, we create a list of objects we want to save, thanks to the command .add(). Then, at the end of the study, we save the list with the command .save(). Only at that time, all the study is saved.

6.1 UC : How to save a study ?

The objective of this Use Case is to explicitate how to save the structures created within a study in order to be able to load them in a future study.

Requirements  

none

 
Results  

an object containing all the objects saved : myStudy

type:

Study

a file .XML containing all the objects saved : myXMLFile.XML

type:

file .XML

 

Python script for this UseCase :

               # Create the name of the file .XML which will be created at the adress /tmp                # if the adress is not precised, the file .XML is created in the current repertory                fileName = "/tmp/myXMLFile"                  # Create a Study Object which will contain all the objects saved                myStudy = Study()                  # Associate it to the file .XML just created                myStudy.setStorageManager(XMLStorageManager(fileName))                  # Perform here the study                # for example, a NumericalPoint is created we want to save                p1 = NumericalPointWithDescription( ( ("x",10.), ("y",11.), ("z",12.) ) )                  # Add the NumericalPoint to the list of the objects saved                myStudy.add("point", p1)                  # Create an analytical NumericalMathFunction                analytical = NumericalMathFunction( ("a", "b"), ("sum", "prod", "mean"), ("a+b", "a*b", "(a+b)/2") )                  # Add the NumericalMathFunction to the list of the objects saved                myStudy.add("analytical", analytical)                  # Check the list of objects that will be saved                print "Study = " , myStudy                  # Remove the NumericalMathFunction to the list of the objects saved                myStudy.remove("analytical")                  # CARE!! At this point, no object has been saved : only the list have been written!                # SAVE NOW the objects listed                myStudy.save()


6.2 UC : How to load a study ?

The objective of this Use Case is to explicitate how to save the structures created within a study in order to be able to load them in a future study.

The principle is the following one. In order to be able to manipulate the objects contained in myStudy, it is necessary to :

Each object is identified whether with :

In this use case, we load the file saved in the previous use case.

Requirements  

a file .XML containing all the objects saved previously: myXMLFile.XML

type:

file .XML

 
Results  

all the objects of the file myXMLFile.XML loaded in the new study

type:

-

 

Python script for this UseCase :

               # Give the name and the adress of the file .XML that will be loaded                fileName = "/tmp/myXMLFile"                  # Create a Study Object                myStudy = Study()                  # Associate it to the file myXMLFile.XML                myStudy.setStorageManager(XMLStorageManager(fileName))                  # Load the file and all its objects                myStudy.load()                  # Check the content of the myStudy                print "Study = " , myStudy                  # In order to be able to manipulate the objects contained in myStudy, it is necessary to :                # 1. create the same empty structure in the new study                # 2. fill this new empty structure with the content of the loaded structure                  # Create a NumericalPoint from the one stored in myStudy                pointLoaded = NumericalPointWithDescription()                  # Fill the new structure point with the content of the NumericalPoint saved                # this NumericalPoint is identified with its name 'point'                myStudy.fillObject("point", pointLoaded)                  # Check if it worked : the NumericalPoint 'pointLoaded' has been loaded                # and we can manipulate it                print "pointLoaded = " , pointLoaded

Stochastic process  
Table of contents
Index