Note
Go to the end to download the full example code.
HDF5 cache#
In this example, we will see how to use HDF5Cache
.
from __future__ import annotations
from numpy import array
from gemseo import configure_logger
from gemseo.caches.hdf5_cache import HDF5Cache
configure_logger()
<RootLogger root (INFO)>
Import#
Let's first import the array
and the HDF5Cache
classes.
Create#
An instance of HDF5Cache
can be instantiated with the following
statement. The user has to provide the file path of the HDF5 file, as well
as the node name, which usually is a discipline name.
Warning
The HDF5Cache
relies on some multiprocessing features. When working on
Windows, the execution of scripts containing instances of HDF5Cache
must be protected by an if __name__ == '__main__':
statement.
Currently, the use of an HDF5Cache is not supported in parallel on Windows
platforms. This is due to the way subprocesses are forked in this architecture.
The method DOEScenario.set_optimization_history_backup()
is recommended as an alternative.
cache = HDF5Cache(hdf_file_path="my_cache.hdf5", hdf_node_path="node1")
It is possible to see the principal attributes of the cache by printing it, either using a print statement or using the logger:
cache
Cache#
In this example, we manually add data in the cache from the data dictionary
to illustrate its use. Yet, it has to be noted that a cache can be attached
to an Discipline
instance, and the user does not have to feed the
cache manually.
Here, we provide to the cache the data dictionary, and we set x as input
and y as output.
cache[{"x": array([1.0])}] = ({"y": array([2.0])}, None)
cache[{"x": array([2.0])}] = ({"y": array([3.0])}, None)
cache
Get all data#
We can now print some information from the cache, such as its length:
len(cache)
2
We can also display all the cached data so far.
list(cache)
[CacheEntry(inputs={'x': array([1.])}, outputs={'y': array([2.])}, jacobian={}), CacheEntry(inputs={'x': array([2.])}, outputs={'y': array([3.])}, jacobian={})]
Get last cached data#
It is also possible to display the last entry cached, for the inputs and the outputs.
last_entry = cache.last_entry
last_entry.inputs, last_entry.outputs
({'x': array([2.])}, {'y': array([3.])})
Clear the cache#
It is also possible to clear the cache, which removes all the data which has been stored so far in the HDF5 file.
cache.clear()
cache
Total running time of the script: (0 minutes 0.041 seconds)