.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/dataset/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_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-36 .. code-block:: default 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 Out: .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 37-43 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 43-48 .. 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 49-53 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 53-55 .. code-block:: default dataset = cache.export_to_dataset("toy_cache") print(dataset) .. rst-class:: sphx-glr-script-out 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 56-57 or by considering all features as default parameters: .. GENERATED FROM PYTHON SOURCE LINES 57-60 .. code-block:: default dataset = cache.export_to_dataset("toy_cache", categorize=False) print(dataset) .. rst-class:: sphx-glr-script-out 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 61-63 Access properties ----------------- .. GENERATED FROM PYTHON SOURCE LINES 63-64 .. code-block:: default dataset = cache.export_to_dataset("toy_cache") .. GENERATED FROM PYTHON SOURCE LINES 65-68 Variables names ~~~~~~~~~~~~~~~ We can access the variables names: .. GENERATED FROM PYTHON SOURCE LINES 68-70 .. code-block:: default print(dataset.variables) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none ['x', 'y'] .. GENERATED FROM PYTHON SOURCE LINES 71-74 Variables sizes ~~~~~~~~~~~~~~~ We can access the variables sizes: .. GENERATED FROM PYTHON SOURCE LINES 74-76 .. code-block:: default print(dataset.sizes) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none {'x': 1, 'y': 2} .. GENERATED FROM PYTHON SOURCE LINES 77-80 Variables groups ~~~~~~~~~~~~~~~~ We can access the variables groups: .. GENERATED FROM PYTHON SOURCE LINES 80-82 .. code-block:: default print(dataset.groups) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none ['inputs', 'outputs'] .. GENERATED FROM PYTHON SOURCE LINES 83-88 Access data ----------- Access by group ~~~~~~~~~~~~~~~ We can get the data by group, either as an array (default option): .. GENERATED FROM PYTHON SOURCE LINES 88-89 .. code-block:: default print(dataset.get_data_by_group("inputs")) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [[1.] [4.]] .. GENERATED FROM PYTHON SOURCE LINES 90-91 or as a dictionary indexed by the variables names: .. GENERATED FROM PYTHON SOURCE LINES 91-93 .. code-block:: default print(dataset.get_data_by_group("inputs", True)) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none {'x': array([[1.], [4.]])} .. GENERATED FROM PYTHON SOURCE LINES 94-98 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 98-99 .. code-block:: default print(dataset.get_data_by_names(["x"])) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none {'x': array([[1.], [4.]])} .. GENERATED FROM PYTHON SOURCE LINES 100-101 or as an array: .. GENERATED FROM PYTHON SOURCE LINES 101-103 .. code-block:: default print(dataset.get_data_by_names(["x", "y"], False)) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [[1. 2. 3.] [4. 5. 6.]] .. GENERATED FROM PYTHON SOURCE LINES 104-107 Access all data ~~~~~~~~~~~~~~~ We can get all the data, either as a large array: .. GENERATED FROM PYTHON SOURCE LINES 107-108 .. code-block:: default print(dataset.get_all_data()) .. rst-class:: sphx-glr-script-out 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 109-110 or as a dictionary indexed by variables names: .. GENERATED FROM PYTHON SOURCE LINES 110-111 .. code-block:: default print(dataset.get_all_data(as_dict=True)) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none {'inputs': {'x': array([[1.], [4.]])}, 'outputs': {'y': array([[2., 3.], [5., 6.]])}} .. GENERATED FROM PYTHON SOURCE LINES 112-114 We can get these data sorted by category, either with a large array for each category: .. GENERATED FROM PYTHON SOURCE LINES 114-115 .. code-block:: default print(dataset.get_all_data(by_group=False)) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none (array([[1., 2., 3.], [4., 5., 6.]]), ['x', 'y'], {'x': 1, 'y': 2}) .. GENERATED FROM PYTHON SOURCE LINES 116-117 or with a dictionary of variables names: .. GENERATED FROM PYTHON SOURCE LINES 117-118 .. code-block:: default print(dataset.get_all_data(by_group=False, as_dict=True)) .. rst-class:: sphx-glr-script-out 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.048 seconds) .. _sphx_glr_download_examples_dataset_plot_dataset_from_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_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 `_