.. 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 :ref:`Go to the end ` 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 27-37 .. code-block:: Python from __future__ import annotations from numpy import array from gemseo import configure_logger from gemseo.caches.simple_cache import SimpleCache configure_logger() .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 38-42 Import ------ In the following lines, we import the `array` and the :class:`.SimpleCache` classes. .. GENERATED FROM PYTHON SOURCE LINES 45-49 Create ------ We can create an instance of the :class:`.SimpleCache` class with the following line: .. GENERATED FROM PYTHON SOURCE LINES 49-51 .. code-block:: Python cache = SimpleCache() .. GENERATED FROM PYTHON SOURCE LINES 52-53 The cache information can be displayed easily: .. GENERATED FROM PYTHON SOURCE LINES 53-55 .. code-block:: Python cache .. raw:: html
Name: SimpleCache
  • Type: SimpleCache
  • Tolerance: 0.0
  • Input names: []
  • Output names: []
  • Length: 0


.. GENERATED FROM PYTHON SOURCE LINES 56-60 Cache ----- It is possible to manually add some data into the cache by using the following lines: .. GENERATED FROM PYTHON SOURCE LINES 60-62 .. code-block:: Python cache[{"x": array([1.0])}] = ({"y": array([2.0])}, None) .. GENERATED FROM PYTHON SOURCE LINES 63-66 We can add another entry to the cache, and we can then see that its length is still one. Indeed, as previously mentioned, the :class:`.SimpleCache` only enable to store one evaluation. .. GENERATED FROM PYTHON SOURCE LINES 66-69 .. code-block:: Python cache[{"x": array([2.0])}] = ({"y": array([3.0])}, None) cache .. raw:: html
Name: SimpleCache
  • Type: SimpleCache
  • Tolerance: 0.0
  • Input names: ['x']
  • Output names: ['y']
  • Length: 1


.. GENERATED FROM PYTHON SOURCE LINES 70-73 Get all data ------------ We can display the length and the data contained in the cache: .. GENERATED FROM PYTHON SOURCE LINES 73-75 .. code-block:: Python len(cache) .. rst-class:: sphx-glr-script-out .. code-block:: none 1 .. GENERATED FROM PYTHON SOURCE LINES 76-78 As mentioned before, we can see that only the last inputs and outputs cached are available: .. GENERATED FROM PYTHON SOURCE LINES 78-80 .. code-block:: Python list(cache) .. rst-class:: sphx-glr-script-out .. code-block:: none [CacheEntry(inputs={'x': array([2.])}, outputs={'y': array([3.])}, jacobian={})] .. GENERATED FROM PYTHON SOURCE LINES 81-85 Get last cached data -------------------- We can also print the last cached input and output data. For this cache, the last cached inputs and outputs are also the only ones cached. .. GENERATED FROM PYTHON SOURCE LINES 85-89 .. 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 90-93 Clear ----- It is also possible to clear the cache, by using the following lines: .. GENERATED FROM PYTHON SOURCE LINES 93-95 .. code-block:: Python cache.clear() cache .. raw:: html
Name: SimpleCache
  • Type: SimpleCache
  • Tolerance: 0.0
  • Input names: []
  • Output names: []
  • Length: 0


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