{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "from pathlib import Path\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import seaborn as sns\n",
    "\n",
    "from sec_certs.dataset import FIPSDataset\n",
    "\n",
    "sns.set_theme(context=\"notebook\", style=\"ticks\", palette=\"Set2\")\n",
    "\n",
    "RESULTS_DIR = Path(\"./results/icmc\")\n",
    "RESULTS_DIR.mkdir(exist_ok=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset = FIPSDataset.from_web()\n",
    "print(f\"The loaded FIPSDataset contains {len(dataset)} certificates\")\n",
    "df = dataset.to_pandas().loc[lambda _df: _df[\"name\"].notna()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create new figure\n",
    "fig = plt.figure(figsize=(4, 3))\n",
    "\n",
    "dfg = df.groupby([\"year_from\", \"standard\"], observed=True).size().reset_index(name=\"n_modules\")\n",
    "sns.lineplot(data=dfg.loc[dfg.year_from < 2024], x=\"year_from\", y=\"n_modules\", hue=\"standard\", marker=\"o\")\n",
    "\n",
    "plt.xlim(1995, 2025)\n",
    "plt.xlabel(\"Year\")\n",
    "plt.ylabel(\"Number of certified modules\")\n",
    "plt.legend(title=\"\")\n",
    "plt.tight_layout()\n",
    "\n",
    "plt.savefig(RESULTS_DIR / \"n_certs_by_standard.pdf\", bbox_inches=\"tight\", dpi=300)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure(figsize=(6, 4))\n",
    "\n",
    "dfg = df.groupby([\"year_from\", \"type\"], observed=True).size().reset_index(name=\"n_modules\")\n",
    "sns.lineplot(data=dfg.loc[dfg.year_from < 2024], x=\"year_from\", y=\"n_modules\", hue=\"type\", marker=\"o\")\n",
    "\n",
    "plt.xlim(1995, 2025)\n",
    "plt.xlabel(\"Year\")\n",
    "plt.ylabel(\"Number of certified modules\")\n",
    "plt.legend(title=\"\")\n",
    "\n",
    "plt.tight_layout()\n",
    "plt.savefig(RESULTS_DIR / \"n_certs_by_type.pdf\", bbox_inches=\"tight\", dpi=300)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure(figsize=(4, 3))\n",
    "\n",
    "dfg = df.groupby([\"year_from\", \"level\"], observed=True).size().reset_index(name=\"n_modules\")\n",
    "sns.lineplot(\n",
    "    data=dfg.loc[dfg.year_from < 2024], x=\"year_from\", y=\"n_modules\", hue=\"level\", marker=\"o\", palette=\"viridis\"\n",
    ")\n",
    "\n",
    "plt.xlim(1995, 2025)\n",
    "plt.xlabel(\"Year\")\n",
    "plt.ylabel(\"Number of certified modules\")\n",
    "plt.legend(title=\"Level\")\n",
    "\n",
    "plt.savefig(RESULTS_DIR / \"n_certs_by_level.pdf\", bbox_inches=\"tight\", dpi=300)\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Average security level\n",
    "fig = plt.figure(figsize=(4, 3))\n",
    "\n",
    "dfg = df.groupby(\"year_from\").agg({\"level\": \"mean\"}).reset_index()\n",
    "sns.lineplot(data=dfg.loc[dfg.year_from < 2024], x=\"year_from\", y=\"level\", marker=\"o\")\n",
    "\n",
    "plt.xlim(1995, 2025)\n",
    "plt.xlabel(\"Year\")\n",
    "plt.ylabel(\"Average security level\")\n",
    "\n",
    "plt.savefig(RESULTS_DIR / \"avg_sec_level.pdf\", bbox_inches=\"tight\", dpi=300)\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure(figsize=(5, 4))\n",
    "\n",
    "dfg = df.groupby([\"year_from\", \"embodiment\"], observed=True).size().reset_index(name=\"n_modules\")\n",
    "sns.lineplot(\n",
    "    data=dfg.loc[dfg.year_from < 2024], x=\"year_from\", y=\"n_modules\", hue=\"embodiment\", marker=\"o\", palette=\"viridis\"\n",
    ")\n",
    "\n",
    "plt.xlim(1995, 2025)\n",
    "plt.xlabel(\"Year\")\n",
    "plt.ylabel(\"Number of certified modules\")\n",
    "plt.legend(title=\"Embodiment\")\n",
    "\n",
    "plt.savefig(RESULTS_DIR / \"n_certs_by_embodiment.pdf\", bbox_inches=\"tight\", dpi=300)\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "venv",
   "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.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
