.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/dataset/creation/plot_dataset_from_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_dataset_creation_plot_dataset_from_cache.py: Dataset from a cache ==================== In this example, we will see how to build a :class:`.Dataset` from objects of an :class:`.AbstractFullCache`. For that, we need to import this :class:`.Dataset` class: .. GENERATED FROM PYTHON SOURCE LINES 29-38 .. code-block:: default from __future__ import annotations from gemseo.api import configure_logger from gemseo.caches.memory_full_cache import MemoryFullCache from numpy import array configure_logger() .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 39-45 Synthetic data -------------- Let us consider a :class:`.MemoryFullCache` storing two parameters: - x with dimension 1 which is a cache input, - y with dimension 2 which is a cache output. .. GENERATED FROM PYTHON SOURCE LINES 45-50 .. code-block:: default cache = MemoryFullCache() cache[{"x": array([1.0])}] = ({"y": array([2.0, 3.0])}, None) cache[{"x": array([4.0])}] = ({"y": array([5.0, 6.0])}, None) .. GENERATED FROM PYTHON SOURCE LINES 51-55 Create a dataset ---------------- We can easily build a dataset from this :class:`.MemoryFullCache`, either by separating the inputs from the outputs (default option): .. GENERATED FROM PYTHON SOURCE LINES 55-57 .. code-block:: default dataset = cache.export_to_dataset("toy_cache") print(dataset) .. rst-class:: sphx-glr-script-out .. code-block:: none toy_cache Number of samples: 2 Number of variables: 2 Variables names and sizes by group: inputs: x (1) outputs: y (2) Number of dimensions (total = 3) by group: inputs: 1 outputs: 2 .. GENERATED FROM PYTHON SOURCE LINES 58-59 or by considering all features as default parameters: .. GENERATED FROM PYTHON SOURCE LINES 59-62 .. code-block:: default dataset = cache.export_to_dataset("toy_cache", categorize=False) print(dataset) .. rst-class:: sphx-glr-script-out .. code-block:: none toy_cache Number of samples: 2 Number of variables: 2 Variables names and sizes by group: parameters: x (1), y (2) Number of dimensions (total = 3) by group: parameters: 3 .. GENERATED FROM PYTHON SOURCE LINES 63-65 Access properties ----------------- .. GENERATED FROM PYTHON SOURCE LINES 65-66 .. code-block:: default dataset = cache.export_to_dataset("toy_cache") .. GENERATED FROM PYTHON SOURCE LINES 67-70 Variables names ~~~~~~~~~~~~~~~ We can access the variables names: .. GENERATED FROM PYTHON SOURCE LINES 70-72 .. code-block:: default print(dataset.variables) .. rst-class:: sphx-glr-script-out .. code-block:: none ['x', 'y'] .. GENERATED FROM PYTHON SOURCE LINES 73-76 Variables sizes ~~~~~~~~~~~~~~~ We can access the variables sizes: .. GENERATED FROM PYTHON SOURCE LINES 76-78 .. code-block:: default print(dataset.sizes) .. rst-class:: sphx-glr-script-out .. code-block:: none {'x': 1, 'y': 2} .. GENERATED FROM PYTHON SOURCE LINES 79-82 Variables groups ~~~~~~~~~~~~~~~~ We can access the variables groups: .. GENERATED FROM PYTHON SOURCE LINES 82-84 .. code-block:: default print(dataset.groups) .. rst-class:: sphx-glr-script-out .. code-block:: none ['inputs', 'outputs'] .. GENERATED FROM PYTHON SOURCE LINES 85-90 Access data ----------- Access by group ~~~~~~~~~~~~~~~ We can get the data by group, either as an array (default option): .. GENERATED FROM PYTHON SOURCE LINES 90-91 .. code-block:: default print(dataset.get_data_by_group("inputs")) .. rst-class:: sphx-glr-script-out .. code-block:: none [[1.] [4.]] .. GENERATED FROM PYTHON SOURCE LINES 92-93 or as a dictionary indexed by the variables names: .. GENERATED FROM PYTHON SOURCE LINES 93-95 .. code-block:: default print(dataset.get_data_by_group("inputs", True)) .. rst-class:: sphx-glr-script-out .. code-block:: none {'x': array([[1.], [4.]])} .. GENERATED FROM PYTHON SOURCE LINES 96-100 Access by variable name ~~~~~~~~~~~~~~~~~~~~~~~ We can get the data by variables names, either as a dictionary indexed by the variables names (default option): .. GENERATED FROM PYTHON SOURCE LINES 100-101 .. code-block:: default print(dataset.get_data_by_names(["x"])) .. rst-class:: sphx-glr-script-out .. code-block:: none {'x': array([[1.], [4.]])} .. GENERATED FROM PYTHON SOURCE LINES 102-103 or as an array: .. GENERATED FROM PYTHON SOURCE LINES 103-105 .. code-block:: default print(dataset.get_data_by_names(["x", "y"], False)) .. rst-class:: sphx-glr-script-out .. code-block:: none [[1. 2. 3.] [4. 5. 6.]] .. GENERATED FROM PYTHON SOURCE LINES 106-109 Access all data ~~~~~~~~~~~~~~~ We can get all the data, either as a large array: .. GENERATED FROM PYTHON SOURCE LINES 109-110 .. code-block:: default print(dataset.get_all_data()) .. rst-class:: sphx-glr-script-out .. code-block:: none ({'inputs': array([[1.], [4.]]), 'outputs': array([[2., 3.], [5., 6.]])}, {'inputs': ['x'], 'outputs': ['y']}, {'x': 1, 'y': 2}) .. GENERATED FROM PYTHON SOURCE LINES 111-112 or as a dictionary indexed by variables names: .. GENERATED FROM PYTHON SOURCE LINES 112-113 .. code-block:: default print(dataset.get_all_data(as_dict=True)) .. rst-class:: sphx-glr-script-out .. code-block:: none {'inputs': {'x': array([[1.], [4.]])}, 'outputs': {'y': array([[2., 3.], [5., 6.]])}} .. GENERATED FROM PYTHON SOURCE LINES 114-116 We can get these data sorted by category, either with a large array for each category: .. GENERATED FROM PYTHON SOURCE LINES 116-117 .. code-block:: default print(dataset.get_all_data(by_group=False)) .. rst-class:: sphx-glr-script-out .. code-block:: none (array([[1., 2., 3.], [4., 5., 6.]]), ['x', 'y'], {'x': 1, 'y': 2}) .. GENERATED FROM PYTHON SOURCE LINES 118-119 or with a dictionary of variables names: .. GENERATED FROM PYTHON SOURCE LINES 119-120 .. code-block:: default print(dataset.get_all_data(by_group=False, as_dict=True)) .. rst-class:: sphx-glr-script-out .. code-block:: none {'x': array([[1.], [4.]]), 'y': array([[2., 3.], [5., 6.]])} .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.023 seconds) .. _sphx_glr_download_examples_dataset_creation_plot_dataset_from_cache.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_dataset_from_cache.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_dataset_from_cache.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_