{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {
    "raw_mimetype": "text/restructuredtext"
   },
   "source": [
    "Download this notebook :download:`here <visualization.ipynb>`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Visualize decay topologies"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {
    "raw_mimetype": "text/restructuredtext"
   },
   "source": [
    "The `.io.dot` module allows you to convert a `.StateTransitionGraph` to `DOT language <https://graphviz.org/doc/info/lang.html>`_, which you can then visualize with third-party libraries such as `Graphviz <https://graphviz.org/>`_. This is particularly useful after running `~.StateTransitionManager.find_solutions`, which produces a `list` of `.StateTransitionGraph` instances (see the :doc:`/usage/quickstart`).\n",
    "\n",
    "First, we quickly create some dummy solutions. We're not interested in the propagate process, so we decrease the `logging` level as well"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import logging\n",
    "\n",
    "logging.basicConfig(level=logging.ERROR)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from expertsystem.amplitude.helicity_decay import HelicityAmplitudeGenerator\n",
    "from expertsystem.ui import InteractionTypes, StateTransitionManager\n",
    "\n",
    "stm = StateTransitionManager(\n",
    "    initial_state=[(\"J/psi(1S)\", [-1, 1])],\n",
    "    final_state=[\"gamma\", \"pi0\", \"pi0\"],\n",
    "    allowed_intermediate_particles=[\"f(0)(980)\", \"f(0)(1500)\",],\n",
    ")\n",
    "stm.set_allowed_interaction_types([InteractionTypes.EM])\n",
    "graph_interaction_settings_groups = stm.prepare_graphs()\n",
    "solutions, _ = stm.find_solutions(graph_interaction_settings_groups)"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {
    "raw_mimetype": "text/restructuredtext"
   },
   "source": [
    "Now, we use the `~.io.dot.convert_to_dot` function to create a `str` of DOT language for the list of solutions (`.StateTransitionGraph` instances):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from expertsystem import io\n",
    "\n",
    "dot_source = io.dot.convert_to_dot(solutions)"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {
    "raw_mimetype": "text/restructuredtext"
   },
   "source": [
    "This `str` can then be visualized with a third-party library, for instance, with `graphviz.Source`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import graphviz\n",
    "\n",
    "graphviz.Source(dot_source)"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {
    "raw_mimetype": "text/restructuredtext"
   },
   "source": [
    ".. note::\n",
    "  `graphviz <graphviz.Source>` requires your system to have DOT installed, see :doc:`Installation <graphviz:index>`."
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {
    "raw_mimetype": "text/restructuredtext"
   },
   "source": [
    "You can also serialize the DOT string to file with `.io.write`. The file extension for a DOT file is :code:`.gv`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "io.write(instance=solutions, filename=\"decay_topologies.gv\")"
   ]
  }
 ],
 "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.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
