.. 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 Click :ref:`here ` 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 a :class:`.MemoryFullCache` object. .. GENERATED FROM PYTHON SOURCE LINES 28-38 .. 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.memory_full_cache import MemoryFullCache configure_logger() .. rst-class:: sphx-glr-script-out Out: .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 39-43 Import ------ First, we import the `array` and the :class:`MemoryError` classes. .. GENERATED FROM PYTHON SOURCE LINES 43-47 .. code-block:: default standard_library.install_aliases() .. GENERATED FROM PYTHON SOURCE LINES 48-53 Create ------ We can create an instance of the :class:`.MemoryFullCache` class. We can then print it, and we can see it is empty. .. GENERATED FROM PYTHON SOURCE LINES 53-57 .. code-block:: default cache = MemoryFullCache() print(cache) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Name: MemoryFullCache Type: MemoryFullCache Tolerance: 0.0 Input names: None Output names: None Length: 0 .. GENERATED FROM PYTHON SOURCE LINES 58-64 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 a :class:`.MDODiscipline`. Then, the cache feeding has not to be performed explicitly by the user. .. GENERATED FROM PYTHON SOURCE LINES 64-71 .. code-block:: default data = {"x": array([1.0]), "y": array([2.0])} cache.cache_outputs(data, ["x"], data, ["y"]) 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: MemoryFullCache Type: MemoryFullCache Tolerance: 0.0 Input names: x Output names: y Length: 2 .. GENERATED FROM PYTHON SOURCE LINES 72-77 Get all data ------------ Once the cache has been filled, the user can get the length of the cache. The user can also print all the data contained inside the cache. .. GENERATED FROM PYTHON SOURCE LINES 77-80 .. code-block:: default print(cache.get_length()) print(cache.get_all_data()) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 2 {1: {'inputs': {'x': array([1.])}, 'outputs': {'y': array([2.])}, 'jacobian': None}, 2: {'inputs': {'x': array([2.])}, 'outputs': {'y': array([3.])}, 'jacobian': None}} .. GENERATED FROM PYTHON SOURCE LINES 81-86 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 86-90 .. 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 91-95 Clear ----- The user can clear an cache of all its entries by using the :meth:`.MemoryFullCache.clear` method: .. 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: MemoryFullCache Type: MemoryFullCache 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.068 seconds) .. _sphx_glr_download_examples_cache_plot_memory_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_memory_cache.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_memory_cache.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_