.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/cache/plot_memory_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_memory_cache.py: Memory full cache ================= This example shows how to manipulate an :class:`.MemoryFullCache` object. .. GENERATED FROM PYTHON SOURCE LINES 26-36 .. code-block:: Python from __future__ import annotations from numpy import array from gemseo import configure_logger from gemseo.caches.memory_full_cache import MemoryFullCache configure_logger() .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 37-41 Import ------ First, we import the `array` and the :class:`MemoryError` classes. .. GENERATED FROM PYTHON SOURCE LINES 44-55 Create ------ We can create an instance of the :class:`.MemoryFullCache` class. We can then print it, and we can see it is empty. .. warning:: The :class:`.MemoryFullCache` relies on some multiprocessing features. When working on Windows, the execution of scripts containing instances of :class:`.MemoryFullCache` must be protected by an ``if __name__ == '__main__':`` statement. .. GENERATED FROM PYTHON SOURCE LINES 55-59 .. code-block:: Python cache = MemoryFullCache() cache .. raw:: html
Name: MemoryFullCache
  • Type: MemoryFullCache
  • Tolerance: 0.0
  • Input names: []
  • Output names: []
  • Length: 0


.. GENERATED FROM PYTHON SOURCE LINES 60-66 Cache ----- We can manually add data into the cache. However, it has to be noted that most of the time a cache is attached to an :class:`.MDODiscipline`. Then, the cache feeding has not to be performed explicitly by the user. .. GENERATED FROM PYTHON SOURCE LINES 66-71 .. code-block:: Python cache[{"x": array([1.0])}] = ({"y": array([2.0])}, None) cache[{"x": array([2.0])}] = ({"y": array([3.0])}, None) cache .. raw:: html
Name: MemoryFullCache
  • Type: MemoryFullCache
  • Tolerance: 0.0
  • Input names: ['x']
  • Output names: ['y']
  • Length: 2


.. GENERATED FROM PYTHON SOURCE LINES 72-76 Get all data ------------ We can now print some information from the cache, such as its length: .. GENERATED FROM PYTHON SOURCE LINES 76-78 .. code-block:: Python len(cache) .. rst-class:: sphx-glr-script-out .. code-block:: none 2 .. GENERATED FROM PYTHON SOURCE LINES 79-81 We can also display all the cached data so far. .. GENERATED FROM PYTHON SOURCE LINES 81-83 .. 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 84-89 Get last cached data -------------------- The user can access the last entry (inputs or outputs) which have been entered in the cache. .. GENERATED FROM PYTHON SOURCE LINES 89-93 .. 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 94-98 Clear ----- The user can clear a cache of all its entries by using the :meth:`.MemoryFullCache.clear` method: .. GENERATED FROM PYTHON SOURCE LINES 98-100 .. code-block:: Python cache.clear() cache .. raw:: html
Name: MemoryFullCache
  • Type: MemoryFullCache
  • Tolerance: 0.0
  • Input names: []
  • Output names: []
  • Length: 0


.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.013 seconds) .. _sphx_glr_download_examples_cache_plot_memory_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_memory_cache.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_memory_cache.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_