diff --git a/ipd/AN_IPD_PIC2.ipynb b/ipd/AN_IPD_PIC2.ipynb index f862fc15de1cf28c0c208f90c66d7f539e1f92ca..4e7de1566be77224084af4b97131dd22b6ded9a2 100644 --- a/ipd/AN_IPD_PIC2.ipynb +++ b/ipd/AN_IPD_PIC2.ipynb @@ -142,7 +142,7 @@ "metadata": {}, "outputs": [], "source": [ - "analysis = powering.Pic2Analysis('', test_parameters)\n", + "analysis = powering.Pic2CircuitQuenchViaQpsForIPQandIPDAnalysisMP3('', test_parameters)\n", "analysis.set_spark(spark)" ] }, diff --git a/ipq/AN_IPQ_PIC2_MI.ipynb b/ipq/AN_IPQ_PIC2_MI.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..7200f27b3853f3d7bf1e911df589f9a11f437fab --- /dev/null +++ b/ipq/AN_IPQ_PIC2_MI.ipynb @@ -0,0 +1,445 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# IPQ: PIC2 CIRCUIT QUENCH VIA QPS (MI Analysis Notebook)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from IPython.display import display, Markdown\n", + "import importlib.resources\n", + "\n", + "with open(importlib.resources.files(\"lhcsmapi.api.analysis.powering.pic2\") / \"README.md\", \"r\") as f:\n", + " long_description = f.read()\n", + "display(Markdown(long_description))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import getpass\n", + "import logging\n", + "import sys\n", + "\n", + "from matplotlib import pyplot as plt\n", + "import pandas as pd\n", + "import numpy as np\n", + "from IPython.display import display, Javascript, clear_output\n", + "\n", + "import lhcsmapi\n", + "from lhcsmapi.Time import Time\n", + "from lhcsmapi.api.analysis import powering\n", + "from lhcsmapi.api.analysis.powering import pic2\n", + "from lhcsmapi.analysis.expert_input import get_expert_decision\n", + "\n", + "%xmode Minimal\n", + "logging.getLogger().setLevel(logging.INFO)\n", + "logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))\n", + "\n", + "clear_output()\n", + "print(f\"Analysis executed with lhc-sm-api version: {lhcsmapi.__version__}\")\n", + "print(f\"Analysis performed by {getpass.getuser()}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "if \"spark\" not in locals() and \"spark\" not in globals():\n", + " from nxcals.spark_session_builder import get_or_create\n", + "\n", + " spark = get_or_create()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## User Input\n", + "Copy/Paste from AccTesting" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# hwc_test = \"PIC2 CIRCUIT QUENCH VIA QPS\"\n", + "# circuit_name = \"RQ5.L8\"\n", + "# campaign = \"Commissioning post ITL8\"\n", + "# t_start = \"2023-08-25 07:31:10.073000000\"\n", + "# t_end = \"2023-08-25 08:03:33.250000000\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "test_parameters = powering.TestParameterInput.create(hwc_test, circuit_name, campaign, t_start, t_end)\n", + "test_parameters" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Query and Analyse" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "analysis_class = pic2.Pic2CircuitQuenchViaQpsForIPQAnalysisMI(\"pic2\", test_parameters)\n", + "analysis_class.set_spark(spark)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%%time\n", + "analysis_class.query()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%%time\n", + "analysis_class.analyze()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Overall Result" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%xmode Verbose\n", + "print(f\"The Overall Analysis output is \\033[1m{analysis_class.get_analysis_output()}\\033[0m\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Analyses of PC signals from FGC PMs" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%matplotlib widget\n", + "\n", + "st_unlatched_b1_df: pd.DataFrame = analysis_class.get_result(\"st_unlatched_b1_df\").copy()\n", + "st_unlatched_b1_df.index = st_unlatched_b1_df.index * 1e-9 \n", + "st_unlatched_b2_df: pd.DataFrame = analysis_class.get_result(\"st_unlatched_b2_df\").copy()\n", + "st_unlatched_b2_df.index = st_unlatched_b2_df.index * 1e-9\n", + "st_faults_b1_df: pd.DataFrame = analysis_class.get_result(\"st_faults_b1_df\").copy()\n", + "st_faults_b1_df.index = st_faults_b1_df.index * 1e-9\n", + "st_faults_b2_df: pd.DataFrame = analysis_class.get_result(\"st_faults_b2_df\").copy()\n", + "st_faults_b2_df.index = st_faults_b2_df.index * 1e-9\n", + "\n", + "pm_event_trigger: int = analysis_class.get_result(\"pm_event_trigger\")\n", + "pm_event_trigger_sync: int = analysis_class.get_result(\"pm_event_trigger_sync\")\n", + "fast_abort_timeslot = powering.pic2.Pic2CircuitQuenchViaQpsForIPQAnalysisMI._FGC_FAST_ABORT_TIMESLOT\n", + "\n", + "fig = plt.figure(figsize=(12, 6))\n", + "gs = fig.add_gridspec(6, hspace=0)\n", + "axs: list[plt.Axes] = gs.subplots(sharex=True, sharey=True)\n", + "\n", + "fig.suptitle(\n", + " f\"{circuit_name}, PC Status Signal Analysis (FC PM), PM Event: {Time.to_string_short(pm_event_trigger)}\",\n", + " fontsize=18,\n", + ")\n", + "fig.supxlabel(\"time, [s]\", fontsize=12)\n", + "fig.supylabel(\"State (True/False)\", fontsize=12)\n", + "\n", + "for ax, df, columm, beam in (\n", + " (axs[0], st_faults_b1_df, \"FAST_ABORT\", \"B1\"),\n", + " (axs[1], st_faults_b2_df, \"FAST_ABORT\", \"B2\"),\n", + " (axs[2], st_unlatched_b1_df, \"PC_PERMIT\", \"B1\"),\n", + " (axs[3], st_unlatched_b2_df, \"PC_PERMIT\", \"B2\"),\n", + " (axs[4], st_unlatched_b1_df, \"PC_DISCH_RQ\", \"B1\"),\n", + " (axs[5], st_unlatched_b2_df, \"PC_DISCH_RQ\", \"B2\"),\n", + "):\n", + " df[columm].astype(int).plot(ax=ax)\n", + " ax.legend([f\"{columm}_{beam}\"], loc=\"center left\")\n", + "\n", + " # set linestyle to steps-post\n", + " for line in ax.get_lines():\n", + " line.set_marker(\"o\")\n", + " line.set_drawstyle(\"steps-post\")\n", + "\n", + " ax.set_xlim(-0.050, +0.050)\n", + " ax.set_ylim(-0.3, +1.3)\n", + " ax.set_xticks(np.arange(ax.get_xlim()[0], ax.get_xlim()[1], 0.010))\n", + " ax.set_xticks(np.arange(ax.get_xlim()[0], ax.get_xlim()[1]), minor=True)\n", + " ax.grid(which=\"major\", alpha=0.5)\n", + " ax.grid(which=\"minor\", alpha=0.2)\n", + " ax.axvline(x=pm_event_trigger_sync, color=\"green\", linestyle=\"--\", linewidth=2.0)\n", + " ax.axvline(\n", + " x=pm_event_trigger_sync - fast_abort_timeslot[0] * 1e-9,\n", + " color=\"red\",\n", + " linestyle=\"--\",\n", + " linewidth=1.0,\n", + " )\n", + " ax.axvline(\n", + " x=pm_event_trigger_sync + fast_abort_timeslot[1] * 1e-9,\n", + " color=\"red\",\n", + " linestyle=\"--\",\n", + " linewidth=1.0,\n", + " )\n", + "\n", + "plt.yticks([1.0, 0.0], [\"True\", \"False\"])\n", + "plt.show()\n", + "\n", + "print(f\"PC signals checks: {analysis_class.get_result('pc_signal_checks')}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Analyses of PIC signals from NXCALS " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%matplotlib widget\n", + "\n", + "st_abort_pic_df: pd.DataFrame = analysis_class.get_result(\"st_abort_pic_df\").copy()\n", + "st_abort_pic_df.index = st_abort_pic_df.index * 1e-9\n", + "cmd_abort_pic_df: pd.DataFrame = analysis_class.get_result(\"cmd_abort_pic_df\").copy()\n", + "cmd_abort_pic_df.index = cmd_abort_pic_df.index * 1e-9\n", + "cmd_pwr_perm_b1_pic_df: pd.DataFrame = analysis_class.get_result(\"cmd_pwr_perm_b1_pic_df\").copy()\n", + "cmd_pwr_perm_b1_pic_df.index = cmd_pwr_perm_b1_pic_df.index * 1e-9\n", + "cmd_pwr_perm_b2_pic_df: pd.DataFrame = analysis_class.get_result(\"cmd_pwr_perm_b2_pic_df\").copy()\n", + "cmd_pwr_perm_b2_pic_df.index = cmd_pwr_perm_b2_pic_df.index * 1e-9\n", + "pm_event_trigger: int = analysis_class.get_result(\"pm_event_trigger\")\n", + "\n", + "fig = plt.figure(figsize=(12, 4))\n", + "gs = fig.add_gridspec(4, hspace=0)\n", + "axs: list[plt.Axes] = gs.subplots(sharex=True, sharey=True)\n", + "\n", + "fig.suptitle(\n", + " f\"{circuit_name}, PIC Signal Analysis (NXCALS), PM Event: {Time.to_string_short(pm_event_trigger)}\", fontsize=18\n", + ")\n", + "fig.supxlabel(\"Time, [s]\", fontsize=12)\n", + "fig.supylabel(\"State\", fontsize=12)\n", + "\n", + "\n", + "st_abort_pic_df.plot(ax=axs[0])\n", + "cmd_abort_pic_df.plot(ax=axs[1])\n", + "cmd_pwr_perm_b1_pic_df.plot(ax=axs[2])\n", + "cmd_pwr_perm_b2_pic_df.plot(ax=axs[3])\n", + "\n", + "for ax in axs:\n", + " # set linestyle to steps-post\n", + " for line in ax.get_lines():\n", + " line.set_marker(\"o\")\n", + " line.set_drawstyle(\"steps-post\")\n", + "\n", + " ax.set_xlim(-0.050, +0.050)\n", + " ax.set_ylim(-0.3, +1.3)\n", + " ax.set_xticks(np.arange(ax.get_xlim()[0], ax.get_xlim()[1], 0.010))\n", + " ax.set_xticks(np.arange(ax.get_xlim()[0], ax.get_xlim()[1], 0.001), minor=True)\n", + " ax.grid(which=\"major\", alpha=0.5)\n", + " ax.grid(which=\"minor\", alpha=0.2)\n", + " ax.axvline(x=pm_event_trigger_sync, color=\"green\", linestyle=\"--\", linewidth=2.0)\n", + "\n", + "plt.yticks([1.0, 0.0], [\"True\", \"False\"])\n", + "plt.show()\n", + "\n", + "print(f\"PIC signals checks: {analysis_class.get_result('pic_signals_checks')}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Analyses of QPS_OK signals from QDS PMs" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%matplotlib widget\n", + "\n", + "st_circuit_ok_qps_a_df: pd.DataFrame = analysis_class.get_result(\"st_circuit_ok_qps_a_df\").copy()\n", + "st_circuit_ok_qps_a_df.index = st_circuit_ok_qps_a_df.index * 1e-9\n", + "st_circuit_ok_qps_b_df: pd.DataFrame = analysis_class.get_result(\"st_circuit_ok_qps_b_df\").copy()\n", + "st_circuit_ok_qps_b_df.index = st_circuit_ok_qps_b_df.index * 1e-9\n", + "cmd_pwr_perm_b1_pic_df: pd.DataFrame = analysis_class.get_result(\"cmd_pwr_perm_b1_pic_df\").copy()\n", + "cmd_pwr_perm_b1_pic_df.index = cmd_pwr_perm_b1_pic_df.index * 1e-9\n", + "cmd_pwr_perm_b2_pic_df: pd.DataFrame = analysis_class.get_result(\"cmd_pwr_perm_b2_pic_df\").copy()\n", + "cmd_pwr_perm_b2_pic_df.index = cmd_pwr_perm_b2_pic_df.index * 1e-9\n", + "pm_event_trigger: int = analysis_class.get_result(\"pm_event_trigger\")\n", + "\n", + "fig = plt.figure(figsize=(12, 4))\n", + "gs = fig.add_gridspec(4, hspace=0)\n", + "axs: list[plt.Axes] = gs.subplots(sharex=True, sharey=True)\n", + "\n", + "fig.suptitle(\n", + " f\"{circuit_name}, QPS_OK Signal Analysis (QDS PM), PM Event: {Time.to_string_short(pm_event_trigger)}\", fontsize=18\n", + ")\n", + "fig.supxlabel(\"Time, [s]\", fontsize=12)\n", + "fig.supylabel(\"State\", fontsize=12)\n", + "\n", + "cmd_pwr_perm_b1_pic_df.astype(int).plot(ax=axs[0])\n", + "cmd_pwr_perm_b2_pic_df.astype(int).plot(ax=axs[1])\n", + "st_circuit_ok_qps_a_df.astype(int).plot(ax=axs[2])\n", + "st_circuit_ok_qps_b_df.astype(int).plot(ax=axs[3])\n", + "\n", + "for ax in axs:\n", + " for line in ax.get_lines():\n", + " line.set_marker(\"o\")\n", + " line.set_drawstyle(\"steps-post\")\n", + "\n", + " ax.set_xlim(-0.30, +0.80)\n", + " ax.set_ylim(-0.3, +1.3)\n", + " ax.grid(which=\"major\", alpha=0.5)\n", + " ax.grid(which=\"minor\", alpha=0.2)\n", + " ax.axvline(x=pm_event_trigger_sync, color=\"green\", linestyle=\"--\", linewidth=2.0)\n", + "\n", + "ax.set_xticks(np.arange(ax.get_xlim()[0], ax.get_xlim()[1], 0.10))\n", + "ax.set_xticks(np.arange(ax.get_xlim()[0], ax.get_xlim()[1], 0.01), minor=True)\n", + "plt.yticks([1.0, 0.0], [\"True\", \"False\"])\n", + "plt.show()\n", + "\n", + "print(f\"QDS signals checks: {analysis_class.get_result('qds_signals_checks')}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Acceptance Decision" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "signature = get_expert_decision(\"Expert Signature Decision: \", [\"PASSED\", \"FAILED\"])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Final Report" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "path_to_notebook = \"./AN_IPQ_PIC2_MI.ipynb\"\n", + "report_destination_path_template = \"/eos/project/m/mp3/IPQ/{}/{}/{}\" # prefix, circuit_name, hwc_test\n", + "report_filename_template = \"{}_{}-{}-{}_{}\" # circuit_name, hwc_test, t_start, analysis_time, signature\n", + "#\n", + "analysis_start_time = Time.get_analysis_start_time()\n", + "prefix_circuit_name = circuit_name.split(\".\")[0]\n", + "hwc_test = hwc_test.split(\" \")[0]\n", + "#\n", + "report_destination_path = report_destination_path_template.format(prefix_circuit_name, circuit_name, hwc_test)\n", + "report_filename = report_filename_template.format(\n", + " circuit_name,\n", + " hwc_test,\n", + " Time.to_datetime(t_start).strftime(\"%Y-%m-%d-%Hh%M\"),\n", + " analysis_start_time,\n", + " signature,\n", + ")\n", + "#\n", + "!mkdir -p $report_destination_path\n", + "html_filename = f\"{report_filename}.html\"\n", + "html_path = f\"{report_destination_path}/{report_filename}.html\"\n", + "print(\"Compact notebook report saved to (Windows): \" + \"\\\\\\\\eosproject-smb\" + html_path.replace(\"/\", \"\\\\\"))\n", + "display(Javascript(\"IPython.notebook.save_notebook();\"))\n", + "Time.sleep(5)\n", + "!{sys.executable} -m jupyter nbconvert --to html $path_to_notebook --output-dir $report_destination_path --output $html_filename --TemplateExporter.exclude_input=True --TagRemovePreprocessor.remove_all_outputs_tags skip_output" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Additional plots and analyses by MI-experts (optional) " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# copy/paste here?" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "my_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.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/ipq/AN_IPQ_PIC2.ipynb b/ipq/AN_IPQ_PIC2_MP3.ipynb similarity index 97% rename from ipq/AN_IPQ_PIC2.ipynb rename to ipq/AN_IPQ_PIC2_MP3.ipynb index 7df876eaa912f93b22752e3f8e1e2868e206a25d..5dbe20825017effcd1b1bb1d8a3acc26842f2862 100644 --- a/ipq/AN_IPQ_PIC2.ipynb +++ b/ipq/AN_IPQ_PIC2_MP3.ipynb @@ -12,9 +12,7 @@ "cell_type": "code", "execution_count": null, "id": "53ae34d2", - "metadata": { - "scrolled": false - }, + "metadata": {}, "outputs": [], "source": [ "from IPython.display import display, Markdown, HTML\n", @@ -141,7 +139,7 @@ "metadata": {}, "outputs": [], "source": [ - "analysis = pic2.Pic2Analysis('pic2', test_parameters)\n", + "analysis = pic2.Pic2CircuitQuenchViaQpsForIPQandIPDAnalysisMP3('pic2', test_parameters)\n", "analysis.set_spark(spark)" ] }, @@ -160,9 +158,7 @@ "cell_type": "code", "execution_count": null, "id": "b14daff4", - "metadata": { - "scrolled": false - }, + "metadata": {}, "outputs": [], "source": [ "%%time\n", @@ -235,9 +231,7 @@ "cell_type": "code", "execution_count": null, "id": "d4b7aaad", - "metadata": { - "scrolled": false - }, + "metadata": {}, "outputs": [], "source": [ "%matplotlib widget\n", @@ -311,7 +305,7 @@ "metadata": {}, "outputs": [], "source": [ - "path_to_notebook = './AN_IPQ_PIC2.ipynb'\n", + "path_to_notebook = './AN_IPQ_PIC2_MP3.ipynb'\n", "report_destination_path_template = '/eos/project/m/mp3/IPQ/{}/{}/{}' # prefix, circuit_name, hwc_test\n", "report_filename_template = '{}_{}-{}-{}_{}' # circuit_name, hwc_test, t_start, analysis_time, signature\n", "#\n",