{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "101e0116-b2a4-4bee-beb0-3bae0d3f340d",
   "metadata": {},
   "source": [
    "## Displaying a legend with CHADA elements for all MOCHADA themes in a single diagram"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ac922004-fc53-4d8a-9d20-cb19785813dc",
   "metadata": {},
   "source": [
    "This example shows how to pack a legend (a.k.a colour key) showing all CHADA elements, for each MOCHADA theme into a single svg image for easy comparison."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d0d50342-7630-43fb-a0e0-43f275a651bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "import contextlib\n",
    "import glob\n",
    "import pathlib\n",
    "\n",
    "import mochada_kit.running as mr"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8535da94-afb3-4482-9b68-aab8364de82a",
   "metadata": {},
   "source": [
    "<br>\n",
    "\n",
    "Get the paths to all the MOCHADA themes in `mochada_kit/themes`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c70f9347-f3d3-4a7c-8a83-7ab8bf3c6249",
   "metadata": {},
   "outputs": [],
   "source": [
    "theme_files = glob.glob(str(pathlib.Path(\"../../themes/puml-theme-MOCHADA-*.puml\")))\n",
    "themes = [str(i).split(\"puml-theme-\")[1][:-5] for i in theme_files]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "76ffc211-00d9-417b-b40e-b3251b5f35b0",
   "metadata": {},
   "source": [
    "<br>\n",
    "\n",
    "Make sure the theme based on the current working agreement (CWA) comes first in the list, and remove the themes \"MOCHADA-plasma_A4w\" and \"MOCHADA-plasma_wide\", if they are present - these themes look the same as \"MOCHADA-plasma\" in a colour key but have one parameter changed to make CHADA tables fit better onto an A4 page/wide screen."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "20be72f7-bbba-4a1b-9818-9ff8070c3863",
   "metadata": {},
   "outputs": [],
   "source": [
    "themes.remove(\"MOCHADA-CWA\")\n",
    "themes.insert(0, \"MOCHADA-CWA\")\n",
    "for i in [\"MOCHADA-plasma_A4w\", \"MOCHADA-plasma_wide\"]:\n",
    "    with contextlib.suppress(ValueError):\n",
    "        themes.remove(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "70bc5245-2a75-444d-9b68-f2e82c1afebb",
   "metadata": {},
   "source": [
    "<br>\n",
    "\n",
    "Now we write the same key (an activity element for each of the CHADA elements with its corresponding stereotype) for each theme into a different \"label\" in the puml code file. There appears to be little control of placing the keys in rows and columns but this adjusts automatically as you add more themes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c21260bb-a285-42f6-b80a-c95a23cc874f",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\n",
    "    pathlib.Path(\"../../gallery/puml_code/legend_chada_all_themes.puml\"), \"w\"\n",
    ") as f:\n",
    "    f.write(\"@startuml\\n\")\n",
    "\n",
    "    for i, j in enumerate(themes):\n",
    "        f.write(f\"label l{i+1} [\\n\")\n",
    "        f.write(\"{{\\n\")\n",
    "        chada_blocks_uml = f\"\"\"!theme {j} from ../../themes\n",
    "title {j}\n",
    ":User Case; <<user_case>>\n",
    ":Experiment; <<experiment>>\n",
    ":Raw Data; <<raw_data>>\n",
    ":Data Processing; <<data_processing>>\n",
    ":Final Data; <<final_data>>\n",
    "\"\"\"\n",
    "\n",
    "        f.write(chada_blocks_uml)\n",
    "        f.write(\"}}\\n]\\n\")\n",
    "\n",
    "    f.write(\"@enduml\\n\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cbb3b66b-39c5-4d60-b148-3be2ab2bc7c9",
   "metadata": {},
   "source": [
    "<br>\n",
    "\n",
    "Finally we run the puml code to generate the svg diagram"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6b26c0c0-7642-4725-ada8-697e00c70499",
   "metadata": {},
   "outputs": [],
   "source": [
    "mr.run_plantuml_code(\n",
    "    pathlib.Path(\"../../gallery/puml_code/legend_chada_all_themes.puml\"),\n",
    "    output_dir=pathlib.Path(\"../\"),\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "45583f44-22f3-42d6-89a8-9c239d11fdb4",
   "metadata": {},
   "source": [
    "Here's the diagram:\n",
    "\n",
    "<img src=../../gallery/legend_chada_all_themes.svg>"
   ]
  }
 ],
 "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.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}