Convert a cache to a dataset

In this example, we will see how to convert a cache to a Dataset.

from __future__ import annotations

from gemseo import configure_logger
from gemseo.caches.memory_full_cache import MemoryFullCache
from numpy import array


configure_logger()
<RootLogger root (INFO)>

Let us consider a MemoryFullCache storing two parameters:

  • x with dimension 1 which is a cache input,

  • y with dimension 2 which is a cache output.

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)

This cache can be converted to an IODataset using its method to_dataset():

dataset = cache.to_dataset("toy_cache")
print(dataset)
GROUP     inputs outputs
VARIABLE       x       y
COMPONENT      0       0    1
0            1.0     2.0  3.0
1            4.0     5.0  6.0

The input variables belong to the input group and the output variables to the output group. We can avoid this categorization and simply build a Dataset:

dataset = cache.to_dataset("toy_cache", categorize=False)
print(dataset)
GROUP     parameters
VARIABLE           x    y
COMPONENT          0    0    1
0                1.0  2.0  3.0
1                4.0  5.0  6.0

Total running time of the script: ( 0 minutes 0.034 seconds)

Gallery generated by Sphinx-Gallery