{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# HDF5 cache\n\nIn this example, we will see how to use :class:`.HDF5Cache`.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from __future__ import annotations\n\nfrom gemseo.api import configure_logger\nfrom gemseo.caches.hdf5_cache import HDF5Cache\nfrom numpy import array\n\nconfigure_logger()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Import\nLet's first import the :class:`array` and the :class:`.HDF5Cache` classes.\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Create\nAn instance of :class:`.HDF5Cache` can be instantiated with the following\nstatement.  The user has to provide the file path of the HDF5 file, as well\nas the node name, which usually is a discipline name.\n\n<div class=\"alert alert-danger\"><h4>Warning</h4><p>The :class:`.HDF5Cache` relies on some multiprocessing features. When working on\n    Windows, the execution of scripts containing instances of :class:`.HDF5Cache`\n    must be protected by an ``if __name__ == '__main__':`` statement.</p></div>\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "cache = HDF5Cache(\"my_cache.hdf5\", \"node1\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "It is possible to see the principal attributes of the cache by printing it,\neither using a print statement or using the logger:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(cache)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Cache\nIn this example, we manually add data in the cache from the data dictionary\nto illustrate its use.  Yet, it has to be noted that a cache can be attached\nto an :class:`.MDODiscipline` instance, and the user does not have to feed the\ncache manually.\nHere, we provide to the cache the data dictionary, and we set `x` as input\nand `y` as output.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "cache[{\"x\": array([1.0])}] = ({\"y\": array([2.0])}, None)\ncache[{\"x\": array([2.0])}] = ({\"y\": array([3.0])}, None)\nprint(cache)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Get all data\nWe can now print some information from the cache, such as its length. We can\nalso display all the cached data so far.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(len(cache))\nfor data in cache:\n    print(data)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Get last cached data\nIt is also possible to display the last entry cached, for the inputs and the\noutputs.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "last_entry = cache.last_entry\nprint(last_entry.inputs)\nprint(last_entry.outputs)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Clear the cache\nIt is also possible to clear the cache, which removes all the data which has\nbeen stored so far in the HDF5 file.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "cache.clear()\nprint(cache)"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.9.13"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}