.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/cache/plot_hdf5cache.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_examples_cache_plot_hdf5cache.py: HDF5 cache ========== In this example, we will see how to use :class:`.HDF5Cache`. .. GENERATED FROM PYTHON SOURCE LINES 27-37 .. code-block:: Python from __future__ import annotations from numpy import array from gemseo import configure_logger from gemseo.caches.hdf5_cache import HDF5Cache configure_logger() .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 38-41 Import ------ Let's first import the :class:`array` and the :class:`.HDF5Cache` classes. .. GENERATED FROM PYTHON SOURCE LINES 45-59 Create ------ An instance of :class:`.HDF5Cache` can be instantiated with the following statement. The user has to provide the file path of the HDF5 file, as well as the node name, which usually is a discipline name. .. warning:: The :class:`.HDF5Cache` relies on some multiprocessing features. When working on Windows, the execution of scripts containing instances of :class:`.HDF5Cache` must be protected by an ``if __name__ == '__main__':`` statement. Currently, the use of an HDF5Cache is not supported in parallel on Windows platforms. This is due to the way subprocesses are forked in this architecture. The method :meth:`.DOEScenario.set_optimization_history_backup` is recommended as an alternative. .. GENERATED FROM PYTHON SOURCE LINES 59-62 .. code-block:: Python cache = HDF5Cache(hdf_file_path="my_cache.hdf5", hdf_node_path="node1") .. GENERATED FROM PYTHON SOURCE LINES 63-65 It is possible to see the principal attributes of the cache by printing it, either using a print statement or using the logger: .. GENERATED FROM PYTHON SOURCE LINES 65-67 .. code-block:: Python cache .. raw:: html
Name: node1
  • Type: HDF5Cache
  • Tolerance: 0.0
  • Input names: []
  • Output names: []
  • Length: 0
  • HDF file path: my_cache.hdf5
  • HDF node path: node1


.. GENERATED FROM PYTHON SOURCE LINES 68-76 Cache ----- In this example, we manually add data in the cache from the data dictionary to illustrate its use. Yet, it has to be noted that a cache can be attached to an :class:`.MDODiscipline` instance, and the user does not have to feed the cache manually. Here, we provide to the cache the data dictionary, and we set `x` as input and `y` as output. .. GENERATED FROM PYTHON SOURCE LINES 76-80 .. code-block:: Python cache[{"x": array([1.0])}] = ({"y": array([2.0])}, None) cache[{"x": array([2.0])}] = ({"y": array([3.0])}, None) cache .. GENERATED FROM PYTHON SOURCE LINES 81-84 Get all data ------------ We can now print some information from the cache, such as its length: .. GENERATED FROM PYTHON SOURCE LINES 84-86 .. code-block:: Python len(cache) .. rst-class:: sphx-glr-script-out .. code-block:: none 2 .. GENERATED FROM PYTHON SOURCE LINES 87-89 We can also display all the cached data so far. .. GENERATED FROM PYTHON SOURCE LINES 89-91 .. code-block:: Python list(cache) .. rst-class:: sphx-glr-script-out .. code-block:: none [CacheEntry(inputs={'x': array([1.])}, outputs={'y': array([2.])}, jacobian={}), CacheEntry(inputs={'x': array([2.])}, outputs={'y': array([3.])}, jacobian={})] .. GENERATED FROM PYTHON SOURCE LINES 92-96 Get last cached data -------------------- It is also possible to display the last entry cached, for the inputs and the outputs. .. GENERATED FROM PYTHON SOURCE LINES 96-100 .. code-block:: Python last_entry = cache.last_entry last_entry.inputs, last_entry.outputs .. rst-class:: sphx-glr-script-out .. code-block:: none ({'x': array([2.])}, {'y': array([3.])}) .. GENERATED FROM PYTHON SOURCE LINES 101-105 Clear the cache --------------- It is also possible to clear the cache, which removes all the data which has been stored so far in the HDF5 file. .. GENERATED FROM PYTHON SOURCE LINES 105-108 .. code-block:: Python cache.clear() cache .. raw:: html
Name: node1
  • Type: HDF5Cache
  • Tolerance: 0.0
  • Input names: []
  • Output names: []
  • Length: 0
  • HDF file path: my_cache.hdf5
  • HDF node path: node1


.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.051 seconds) .. _sphx_glr_download_examples_cache_plot_hdf5cache.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_hdf5cache.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_hdf5cache.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_