.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/cache/plot_simple_cache.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_simple_cache.py: Simple cache ============ This example shows the manipulation of :class:`.SimpleCache` instances. This cache only stores the last inputs and outputs stored. .. GENERATED FROM PYTHON SOURCE LINES 29-39 .. code-block:: default from __future__ import absolute_import, division, print_function, unicode_literals from future import standard_library from numpy import array from gemseo.api import configure_logger from gemseo.caches.simple_cache import SimpleCache configure_logger() .. rst-class:: sphx-glr-script-out Out: .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 40-44 Import ------ In the following lines, we import the `array` and the :class:`.SimpleCache` classes. .. GENERATED FROM PYTHON SOURCE LINES 44-48 .. code-block:: default standard_library.install_aliases() .. GENERATED FROM PYTHON SOURCE LINES 49-53 Create ------ We can create an instance of the :class:`.SimpleCache` class with the following line: .. GENERATED FROM PYTHON SOURCE LINES 53-58 .. code-block:: default cache = SimpleCache() # The cache information can be displayed easily: print(cache) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Name: SimpleCache Type: SimpleCache Tolerance: 0.0 Input names: None Output names: None Length: 0 .. GENERATED FROM PYTHON SOURCE LINES 59-63 Cache ----- It is possible to manually add some data into the cache by using the following lines: .. GENERATED FROM PYTHON SOURCE LINES 63-66 .. code-block:: default data = {"x": array([1.0]), "y": array([2.0])} cache.cache_outputs(data, ["x"], data, ["y"]) .. GENERATED FROM PYTHON SOURCE LINES 67-70 We can add another entry to the cache, and we can then see that its length is still one. Indeed, as previously mentionned, the :class:`.SimpleCache` only enable to store one evaluation. .. GENERATED FROM PYTHON SOURCE LINES 70-74 .. code-block:: default data = {"x": array([2.0]), "y": array([3.0])} cache.cache_outputs(data, ["x"], data, ["y"]) print(cache) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Name: SimpleCache Type: SimpleCache Tolerance: 0.0 Input names: x Output names: y Length: 1 .. GENERATED FROM PYTHON SOURCE LINES 75-80 Get all data ------------ We can display the lenght and the data contained in the cache. As mentionned before, we can see that only the last inputs and outputs cached are available: .. GENERATED FROM PYTHON SOURCE LINES 80-83 .. code-block:: default print(cache.get_length()) print(cache.get_all_data()) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 1 {1: {'inputs': {'x': array([2.])}, 'outputs': {'y': array([3.])}, 'jacobian': None}} .. GENERATED FROM PYTHON SOURCE LINES 84-88 Get last cached data -------------------- We can also print the last cached input and output data. For this cache, the last cached inputs and ouputs are also the only ones cached. .. GENERATED FROM PYTHON SOURCE LINES 88-91 .. code-block:: default print(cache.get_last_cached_inputs()) print(cache.get_last_cached_outputs()) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none {'x': array([2.])} {'y': array([3.])} .. GENERATED FROM PYTHON SOURCE LINES 92-95 Clear ----- It is also possible to clear the cache, by using the following lines: .. GENERATED FROM PYTHON SOURCE LINES 95-97 .. code-block:: default cache.clear() print(cache) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Name: SimpleCache Type: SimpleCache Tolerance: 0.0 Input names: None Output names: None Length: 0 .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.004 seconds) .. _sphx_glr_download_examples_cache_plot_simple_cache.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_simple_cache.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_simple_cache.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_