.. 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 Click :ref:`here ` 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-33 .. code-block:: default from gemseo.api import configure_logger from gemseo.caches.hdf5_cache import HDF5Cache from numpy import array configure_logger() .. rst-class:: sphx-glr-script-out Out: .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 34-37 Import ------ Let's first import the :class:`array` and the :class:`.HDF5Cache` classes. .. GENERATED FROM PYTHON SOURCE LINES 41-51 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. .. GENERATED FROM PYTHON SOURCE LINES 51-54 .. code-block:: default cache = HDF5Cache("my_cache.hdf5", "node1") .. GENERATED FROM PYTHON SOURCE LINES 55-57 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 57-59 .. code-block:: default print(cache) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 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 60-68 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 68-73 .. code-block:: default cache[{"x": array([1.0])}] = ({"y": array([2.0])}, None) cache[{"x": array([2.0])}] = ({"y": array([3.0])}, None) print(cache) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Name: node1 Type: HDF5Cache Tolerance: 0.0 Input names: ['x'] Output names: ['y'] Length: 2 HDF file path: my_cache.hdf5 HDF node path: node1 .. GENERATED FROM PYTHON SOURCE LINES 74-78 Get all data ------------ We can now print some information from the cache, such as its length. We can also display all the cached data so far. .. GENERATED FROM PYTHON SOURCE LINES 78-83 .. code-block:: default print(len(cache)) for data in cache: print(data) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 2 CacheEntry(inputs={'x': array([1.])}, outputs={'y': array([2.])}, jacobian=None) CacheEntry(inputs={'x': array([2.])}, outputs={'y': array([3.])}, jacobian=None) .. GENERATED FROM PYTHON SOURCE LINES 84-88 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 88-93 .. code-block:: default last_entry = cache.last_entry print(last_entry.inputs) print(last_entry.outputs) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none {'x': array([2.])} {'y': array([3.])} .. GENERATED FROM PYTHON SOURCE LINES 94-98 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 98-101 .. code-block:: default cache.clear() print(cache) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 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.047 seconds) .. _sphx_glr_download_examples_cache_plot_hdf5cache.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_hdf5cache.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_hdf5cache.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_