{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "88c6c7e6-596f-4b0c-8012-02b92ea50bb4",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import pymbse\n",
    "from pymbse.query.api import PyMBSE\n",
    "from pymbse.microservice.api import init_upload_and_run, download_artefact, get_artefact_names\n",
    "\n",
    "from roxieapi.commons.roxie_constants import PlotLabels\n",
    "from roxieapi.commons.types import Plot2D, Plot3D\n",
    "from roxieapi.input.builder import RoxieInputBuilder\n",
    "from roxieapi.output.plots import RoxiePlotOutputs\n",
    "\n",
    "import scrapbook as sb\n",
    "\n",
    "from pathlib import Path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2c61e260-a20e-4b0a-bf0b-eb4fd0784ffa",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# General settings\n",
    "config_file = './pymbse_example.yml'\n",
    "tmp_folder = Path('./tmp')\n",
    "input_folder = Path('./inputs')\n",
    "output_folder = Path('./outputs')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d130ba72-82de-4a78-9d81-f90918833880",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Model specific settings\n",
    "\n",
    "model_name = 'material_data'\n",
    "\n",
    "# Files required by a roxie run. This will collect inputs from artefacts and input files\n",
    "filelist = {\n",
    "    'cadata' : None,\n",
    "    'bhdata' : None,\n",
    "}\n",
    "\n",
    "# A list of all input files (for later use)\n",
    "input_files = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6020009e-63f2-487f-8500-e02e86cb6823",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Load values from config\n",
    "config = pymbse.query.config.pymbse_config.load_pymbse_config(config_file)\n",
    "model_config = config.models[model_name]\n",
    "\n",
    "for file in model_config.input_files:\n",
    "    suf = Path(file).suffix[1:]\n",
    "    if suf in filelist:\n",
    "        filelist[suf]=file\n",
    "    if suf != 'data':\n",
    "        input_files.append(file)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d797440-ec56-42e1-b838-bccef7604241",
   "metadata": {},
   "source": [
    "## Dependency execution\n",
    "\n",
    "Next step is to run dependend models. Iterates through all dependend models, executes them, and stores the results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "840b236a-7204-4667-986d-3076c15e5f0a",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "dependend_models = {}\n",
    "artefacts = {}\n",
    "for dm in model_config.needs:\n",
    "    dependend_models[dm] = PyMBSE(config_path=model_config,source_model=model_name,target_model=dm).build()\n",
    "    model_artefacts = dependend_models[dm].get_artefacts()\n",
    "    for ma in model_artefacts:\n",
    "        filename = ma\n",
    "        if ma in filelist:\n",
    "            filename = f\"{dm}.{ma}\"\n",
    "            filelist[ma] = filename\n",
    "        with open(tmp_folder / filename, 'w') as f:\n",
    "            f.write(model_artefacts[ma])\n",
    "        input_files.append(str(tmp_folder / filename))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7fe477f0-3774-489e-9ee7-0497f687bb60",
   "metadata": {},
   "source": [
    "## Sanity check - Are all files present\n",
    "\n",
    "A quick check to see if all required input files are defined.\n",
    "\n",
    "The minimum requirements are a datafile + a cadata file.\n",
    "\n",
    "If there is a hmo or iron file linked, there also needs to be a bhdata file present"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "da9b43b1-c6d6-476c-bded-38b0ded2f055",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "assert filelist['cadata'], \"Missing cadata file\"\n",
    "assert filelist['bhdata'], \"Iron mesh given, but no bhdata file\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98fe4015-95c3-4d7b-800a-4f85fb9257d9",
   "metadata": {},
   "source": [
    "## Storage of Results\n",
    "\n",
    "For this notebook to act as a model, we need to define its outputs and store them.\n",
    "The following lines of code create two dictionaries: FOM and artefacts.\n",
    "\n",
    "### fom\n",
    "\n",
    "The Figures of Merit (fom) are a list of Key-Value pairs for output values. This could be objectives, or results for specific purposes.\n",
    "\n",
    "More generally, the artefacts output is a list of files, which will be attached as a whole."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "69bd7695-ec37-4425-81a9-7ced2e91dde0",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "fom_dct = {}\n",
    "artefacts_dct = {'cadata': filelist['cadata'], 'bhdata': filelist['bhdata']}\n",
    "data = {'fom': fom_dct, 'artefacts': artefacts_dct}\n",
    "sb.glue('model_results', data=data, encoder='json')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b72deaf0-9f98-4861-a14c-0dc8c5bdcb68",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.11.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
