{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "dfeba988-6a50-456a-b419-bca6b50c6224",
   "metadata": {},
   "source": [
    "# Results Data\n",
    "\n",
    "This section will explain how to viasualize and extract results from the MobsPy simulation object.\n",
    "\n",
    "We start with a simple model:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "df8e675d-5966-49be-898b-6c5fbb8f8369",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Compiling model\n",
      "Starting Simulator\n",
      "Simulation is Over\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 10748 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from mobspy import *\n",
    "\n",
    "Color, Disease = BaseSpecies()\n",
    "\n",
    "Color.blue, Color.red, Color.yellow\n",
    "Disease.not_sick, Disease.sick\n",
    "\n",
    "Disease.not_sick >> Disease.sick [1]\n",
    "\n",
    "Tree = Color*Disease\n",
    "\n",
    "Tree.yellow(20), Tree.red(20), Tree.blue(20)\n",
    "\n",
    "S = Simulation(Tree)\n",
    "S.method = 'stochastic'\n",
    "S.repetitions = 3\n",
    "S.step_size = 0.25\n",
    "S.duration = 3\n",
    "S.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "611f87ec-b356-49cd-bc01-95964c544eb7",
   "metadata": {},
   "source": [
    "The default plot does not reveal much, as the number of Trees remains constant throughout the simulation.\n",
    "However, MobPy possesses three plotting methods available to the user: plot, plot_stochastic, and plot_raw. \n",
    "The plot function plots all runs for all species together in the same figure. The plot_stochastic function plots two figures for each species, one for the runs and the other for the average and standard deviation of the runs of each species. The plot_raw function is more complex so that it will be discussed in another section.\n",
    "\n",
    "Furthermore, both plot and plot_stochastic accept the dot operator in meta-species used as its arguments. The user is then capable of plotting specific states of meta-species. To continue our example, we plot all instances of Tree which are not sick (Tree.not_sick.blue, Tree.not_sick.red, Tree.not_sick.yellow) and which are sick \n",
    "(Tree.sick.blue, Tree.sick.red, Tree.sick.yellow):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9b28c8e6-8cd6-43a7-8956-863095d4bd18",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\u001b[91mWARNING: Time Series length is different. \n",
      "MobsPy disregards time-series that have already finished during calculations\u001b[0m\n",
      "\u001b[91mWARNING: Time Series length is different. \n",
      "MobsPy disregards time-series that have already finished during calculations\u001b[0m\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 37384 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 4 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "S.plot_stochastic(Tree.not_sick, Tree.sick)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d829ee2-9eac-4556-b3c3-0474b36cb8e0",
   "metadata": {},
   "source": [
    "We also show the same example with the plot function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0f198b63-0238-4c20-b5e1-6cdfb81f17bb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 29212 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "S.plot(Tree.not_sick, Tree.sick)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fcb9ad8f-ac4a-400f-a6b8-e0a739832459",
   "metadata": {},
   "source": [
    "One can also access the data directly from the simulation object using the meta-species objects or strings. \n",
    "Similarly to the plotting function you can perform queries in the result data. Below, we show an example to acess the data directly:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a35e7f4e-67c5-4d09-99cd-29eba06af496",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tree: \n",
      " [[60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0], [60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0], [60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0]]\n",
      "Tree.not_sick: \n",
      " [[60.0, 44.0, 41.0, 33.0, 26.0, 20.0, 13.0, 11.0, 10.0, 9.0, 6.0, 4.0, 4.0], [60.0, 52.0, 40.0, 32.0, 26.0, 20.0, 13.0, 11.0, 8.0, 4.0, 4.0, 4.0, 3.0], [60.0, 47.0, 38.0, 33.0, 21.0, 15.0, 13.0, 11.0, 9.0, 9.0, 8.0, 6.0, 2.0]]\n",
      "Tree.sick: \n",
      " [[0.0, 16.0, 19.0, 27.0, 34.0, 40.0, 47.0, 49.0, 50.0, 51.0, 54.0, 56.0, 56.0], [0.0, 8.0, 20.0, 28.0, 34.0, 40.0, 47.0, 49.0, 52.0, 56.0, 56.0, 56.0, 57.0], [0.0, 13.0, 22.0, 27.0, 39.0, 45.0, 47.0, 49.0, 51.0, 51.0, 52.0, 54.0, 58.0]]\n",
      "Tree.sick.blue: \n",
      " [[0.0, 6.0, 7.0, 8.0, 11.0, 12.0, 15.0, 16.0, 17.0, 17.0, 19.0, 19.0, 19.0], [0.0, 4.0, 7.0, 9.0, 12.0, 15.0, 17.0, 19.0, 19.0, 20.0, 20.0, 20.0, 20.0], [0.0, 2.0, 5.0, 9.0, 13.0, 17.0, 17.0, 17.0, 17.0, 17.0, 17.0, 17.0, 20.0]]\n"
     ]
    }
   ],
   "source": [
    "R = S.results\n",
    "print('Tree: \\n', R[Tree])\n",
    "print('Tree.not_sick: \\n', R[Tree.not_sick])\n",
    "print('Tree.sick: \\n', R['Tree.sick'])\n",
    "print('Tree.sick.blue: \\n', R['Tree.sick.blue'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "23ef615c-b3cc-4c77-b6f7-72d33d80eba1",
   "metadata": {},
   "source": [
    "The get item S.results[Meta_Species_Name] will return a list of lists with all the runs performed in the simulation. \n",
    "However, for cases containing a single run (1 repetition), the following method we will present is more useful. The method fres from the simulation object acess directly the first results the simulation object. See it in the example below:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "16e849c9-3138-4602-a557-eeab98197604",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tree: \n",
      " [60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0]\n",
      "Tree.not_sick: \n",
      " [60.0, 44.0, 41.0, 33.0, 26.0, 20.0, 13.0, 11.0, 10.0, 9.0, 6.0, 4.0, 4.0]\n",
      "Tree.sick: \n",
      " [0.0, 16.0, 19.0, 27.0, 34.0, 40.0, 47.0, 49.0, 50.0, 51.0, 54.0, 56.0, 56.0]\n",
      "Tree.sick.blue: \n",
      " [0.0, 6.0, 7.0, 8.0, 11.0, 12.0, 15.0, 16.0, 17.0, 17.0, 19.0, 19.0, 19.0]\n"
     ]
    }
   ],
   "source": [
    "R = S.fres\n",
    "print('Tree: \\n', R[Tree])\n",
    "print('Tree.not_sick: \\n', R[Tree.not_sick])\n",
    "print('Tree.sick: \\n', R['Tree.sick'])\n",
    "print('Tree.sick.blue: \\n', R['Tree.sick.blue'])"
   ]
  }
 ],
 "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.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
