.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_numerical_methods/general_methods/plot_study_save_load.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_numerical_methods_general_methods_plot_study_save_load.py: Save/load a study ================= The objective of this example is to demonstrate how to save the structures created within a script session to disk in order to be able to load them in a future session. There are several possible ways to achieve this: - with the standard pickle module - with openturns's Study .. GENERATED FROM PYTHON SOURCE LINES 14-18 .. code-block:: Python import openturns as ot import pickle .. GENERATED FROM PYTHON SOURCE LINES 19-20 create objects to save .. GENERATED FROM PYTHON SOURCE LINES 20-23 .. code-block:: Python distribution = ot.Normal(4.0, 3.0) function = ot.SymbolicFunction(["x1", "x2"], ["x1 + x2"]) .. GENERATED FROM PYTHON SOURCE LINES 24-27 **With the pickle module** The objects are retrieved in the same order they are stored. .. GENERATED FROM PYTHON SOURCE LINES 29-30 save objects .. GENERATED FROM PYTHON SOURCE LINES 30-34 .. code-block:: Python with open("study.pkl", "wb") as f: pickle.dump(distribution, f) pickle.dump(function, f) .. GENERATED FROM PYTHON SOURCE LINES 35-36 load saved objects .. GENERATED FROM PYTHON SOURCE LINES 36-41 .. code-block:: Python with open("study.pkl", "rb") as f: loaded_distribution = pickle.load(f) loaded_function = pickle.load(f) str(loaded_distribution), str(loaded_function) .. rst-class:: sphx-glr-script-out .. code-block:: none ('Normal(mu = 4, sigma = 3)', '[x1,x2]->[x1 + x2]') .. GENERATED FROM PYTHON SOURCE LINES 42-58 **With OpenTURNS' Study** In order to be able to manipulate the objects contained in a Study, it is necessary to: - create the same empty structure in the new study, - fill this new empty structure with the content of the loaded structure, identified with its name or its id. Each object is identified whether with: - its name: it is useful to give names to the objects we want to save. If no name has been given by the user, we can use the default name. The name of each saved object can be checked in the output XML file or with the python `print` command (applied to the `Study` object). - its id number: this id number is unique to each object. It distinguishes objects with identical type and name (like the default name "Unnamed"). This id number may be checked by printing the study **after** it has been loaded in the python interface (with the `print` command). It can differ from the id number indicated in the XML file the study was loaded from. - for HDF5 storage (see below): the id serves both as xml id and hdf5 dataset name. Id uniqueness forbids any misleading in reading/writing hdf5 datasets. .. GENERATED FROM PYTHON SOURCE LINES 60-61 Create a Study Object .. GENERATED FROM PYTHON SOURCE LINES 61-63 .. code-block:: Python study = ot.Study() .. GENERATED FROM PYTHON SOURCE LINES 64-65 Associate it to an XML file .. GENERATED FROM PYTHON SOURCE LINES 65-68 .. code-block:: Python fileName = "study.xml" study.setStorageManager(ot.XMLStorageManager(fileName)) .. GENERATED FROM PYTHON SOURCE LINES 69-71 Alternatively, large amounts of data can be stored in binary HDF5 file. An XML file (`study_h5.xml`) serves as header for binary data, which are stored in the automatically created `study_h5.h5` file. .. GENERATED FROM PYTHON SOURCE LINES 71-75 .. code-block:: Python study_h5 = ot.Study() fileName_h5 = "study_h5.xml" study_h5.setStorageManager(ot.XMLH5StorageManager(fileName_h5)) .. GENERATED FROM PYTHON SOURCE LINES 76-77 Add an object to the study; at this point it is not written to disk yet .. GENERATED FROM PYTHON SOURCE LINES 77-80 .. code-block:: Python study.add("distribution", distribution) study.add("function", function) .. GENERATED FROM PYTHON SOURCE LINES 81-82 Save the study; this writes into the file .. GENERATED FROM PYTHON SOURCE LINES 82-84 .. code-block:: Python study.save() .. GENERATED FROM PYTHON SOURCE LINES 85-86 Create a new study associated to the same file .. GENERATED FROM PYTHON SOURCE LINES 86-89 .. code-block:: Python study = ot.Study() study.setStorageManager(ot.XMLStorageManager(fileName)) .. GENERATED FROM PYTHON SOURCE LINES 90-91 Load the file and all its objects .. GENERATED FROM PYTHON SOURCE LINES 91-93 .. code-block:: Python study.load() .. GENERATED FROM PYTHON SOURCE LINES 94-95 Check the content of the myStudy .. GENERATED FROM PYTHON SOURCE LINES 95-97 .. code-block:: Python print("Study = ", study) .. rst-class:: sphx-glr-script-out .. code-block:: none Study = 57 => FunctionImplementation [x1,x2]->[x1 + x2] 58 => Normal Normal(mu = 4, sigma = 3) 'distribution' is aliased to 58 'function' is aliased to 57 .. GENERATED FROM PYTHON SOURCE LINES 98-99 List names of stored objects .. GENERATED FROM PYTHON SOURCE LINES 99-101 .. code-block:: Python study.getLabels() .. rst-class:: sphx-glr-script-out .. code-block:: none ('distribution', 'function') .. GENERATED FROM PYTHON SOURCE LINES 102-103 Check our 'distribution' labelled object was loaded .. GENERATED FROM PYTHON SOURCE LINES 103-105 .. code-block:: Python study.hasObject("distribution") .. rst-class:: sphx-glr-script-out .. code-block:: none True .. GENERATED FROM PYTHON SOURCE LINES 106-107 Load the objects; we must create a void object of the desired type (or parent type) .. GENERATED FROM PYTHON SOURCE LINES 107-112 .. code-block:: Python distribution2 = ot.Normal() function2 = ot.Function() study.fillObject("distribution", distribution2) study.fillObject("function", function2) str(distribution2), str(function2) .. rst-class:: sphx-glr-script-out .. code-block:: none ('Normal(mu = 4, sigma = 3)', '[x1,x2]->[x1 + x2]') .. _sphx_glr_download_auto_numerical_methods_general_methods_plot_study_save_load.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_study_save_load.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_study_save_load.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_study_save_load.zip `