.. 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 31-41 .. code-block:: default from __future__ import division, unicode_literals 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 42-48 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 48-55 .. code-block:: default cache = MemoryFullCache() data = {"x": array([1.0]), "y": array([2.0, 3.0])} cache.cache_outputs(data, ["x"], data, ["y"]) data = {"x": array([4.0]), "y": array([5.0, 6.0])} cache.cache_outputs(data, ["x"], data, ["y"]) .. GENERATED FROM PYTHON SOURCE LINES 56-60 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 60-62 .. 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 63-64 or by considering all features as default parameters: .. GENERATED FROM PYTHON SOURCE LINES 64-67 .. 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 68-70 Access properties ----------------- .. GENERATED FROM PYTHON SOURCE LINES 70-71 .. code-block:: default dataset = cache.export_to_dataset("toy_cache") .. GENERATED FROM PYTHON SOURCE LINES 72-75 Variables names ~~~~~~~~~~~~~~~ We can access the variables names: .. GENERATED FROM PYTHON SOURCE LINES 75-77 .. code-block:: default print(dataset.variables) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none ['x', 'y'] .. GENERATED FROM PYTHON SOURCE LINES 78-81 Variables sizes ~~~~~~~~~~~~~~~ We can access the variables sizes: .. GENERATED FROM PYTHON SOURCE LINES 81-83 .. 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 84-87 Variables groups ~~~~~~~~~~~~~~~~ We can access the variables groups: .. GENERATED FROM PYTHON SOURCE LINES 87-89 .. code-block:: default print(dataset.groups) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none ['inputs', 'outputs'] .. GENERATED FROM PYTHON SOURCE LINES 90-95 Access data ----------- Access by group ~~~~~~~~~~~~~~~ We can get the data by group, either as an array (default option): .. GENERATED FROM PYTHON SOURCE LINES 95-96 .. 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 97-98 or as a dictionary indexed by the variables names: .. GENERATED FROM PYTHON SOURCE LINES 98-100 .. 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 101-105 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 105-106 .. 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 107-108 or as an array: .. GENERATED FROM PYTHON SOURCE LINES 108-110 .. 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 111-114 Access all data ~~~~~~~~~~~~~~~ We can get all the data, either as a large array: .. GENERATED FROM PYTHON SOURCE LINES 114-115 .. 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 116-117 or as a dictionary indexed by variables names: .. GENERATED FROM PYTHON SOURCE LINES 117-118 .. 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 119-121 We can get these data sorted by category, either with a large array for each category: .. GENERATED FROM PYTHON SOURCE LINES 121-122 .. 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 123-124 or with a dictionary of variables names: .. GENERATED FROM PYTHON SOURCE LINES 124-125 .. 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.052 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 `_