{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "eefdf8a5-2b7f-45ec-93dd-44687396bb4f",
   "metadata": {},
   "source": [
    "# Ants model analyses\n",
    "\n",
    "_Copyright (C) 2023-, Joseph T. Lizier._\n",
    "_Distributed under GNU General Public License v3_\n",
    "\n",
    "This notebook template sets up reading in the data files for the Ants model simulations.\n",
    "\n",
    "First code block is just to start up the JVM. You may need to change your paths below (use the AutoAnalyser to show them for you):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0eb38cde-2a28-4740-acf7-a76df9babf4d",
   "metadata": {},
   "outputs": [],
   "source": [
    "from jpype import *\n",
    "import numpy\n",
    "import sys\n",
    "# Our python data file readers are a bit of a hack, python users will do better on this:\n",
    "sys.path.append(\"../../../demos/python/\")\n",
    "import readFloatsFile\n",
    "\n",
    "if (not isJVMStarted()):\n",
    "    # Add JIDT jar library to the path\n",
    "    jarLocation = \"../../../infodynamics.jar\"\n",
    "    # Start the JVM (add the \"-Xmx\" option with say 1024M if you get crashes due to not enough memory space)\n",
    "    startJVM(getDefaultJVMPath(), \"-ea\", \"-Djava.class.path=\" + jarLocation, convertStrings=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2efe2949-d8ec-48a7-b6c7-a0569944b844",
   "metadata": {},
   "source": [
    "Next load in the sample data file and check that we can plot the ants positions ok.\n",
    "\n",
    "Make sure that you have either downloaded the sample data ([positionsx.txt](https://github.com/jlizier/jidt-cc-extras/raw/main/course/Module08-InfoTheoryAndSelfOrganisation/Ants/positionsx.txt), [positionsy.txt](https://github.com/jlizier/jidt-cc-extras/raw/main/course/Module08-InfoTheoryAndSelfOrganisation/Ants/positionsy.txt) and [headings.txt](https://raw.githubusercontent.com/jlizier/jidt-cc-extras/main/course/Module08-InfoTheoryAndSelfOrganisation/Ants/headings.txt)) to the current folder or generated sample data.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cca3e625-d2e7-4fbd-bb65-d10bcf63c794",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 0. Load/prepare the data:\n",
    "positionsx = readFloatsFile.readFloatsFile(\"./positionsx.txt\")\n",
    "positionsy = readFloatsFile.readFloatsFile(\"./positionsy.txt\")\n",
    "headings = readFloatsFile.readFloatsFile(\"./headings.txt\")\n",
    "# As numpy arrays:\n",
    "positionsx = numpy.array(positionsx)\n",
    "positionsy = numpy.array(positionsy)\n",
    "headings = numpy.array(headings)\n",
    "\n",
    "# Plot a sample of the ants moving:\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.animation as animation\n",
    "from ipywidgets import IntProgress\n",
    "from IPython.display import display\n",
    "fig, ax = plt.subplots()\n",
    "plots = []\n",
    "timeStepsForPlot = min(positionsx.shape[0], 300)\n",
    "f = IntProgress(min=0, max=timeStepsForPlot) # instantiate the progress bar\n",
    "display(f) # display progress bar\n",
    "for t in range(timeStepsForPlot):\n",
    "    im = ax.scatter(positionsx[t,:], positionsy[t,:], marker='x', color='r', animated=True)\n",
    "    if (t == 0):\n",
    "         ax.scatter(positionsx[t,:], positionsy[t,:], marker='x', color='r')  # show an initial one first\n",
    "    # If you want to keep the plots to make a video:\n",
    "    plots.append([im])\n",
    "    # To save a png of each plot as you go, uncomment the following:\n",
    "    # plt.savefig('image-%03d.png' % t);\n",
    "    f.value += 1 # update progress bar\n",
    "plt.title('Ants positions');\n",
    "plt.xlabel('x');\n",
    "plt.ylabel('y');\n",
    "# And put the animations into a short video:\n",
    "ani = animation.ArtistAnimation(fig, plots, interval=100, blit=True,\n",
    "                                repeat_delay=1000)\n",
    "videoFilename = 'Ants-sample-video.mp4';\n",
    "ani.save(videoFilename)\n",
    "from IPython.display import Video\n",
    "Video(videoFilename)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "26bd6e3c-3dfa-4e42-a384-158783eec2e2",
   "metadata": {},
   "source": [
    "# Your analysis goes here ...:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ce9ea8dc-d2e2-4dcf-ad0c-aaf33dbf6eb8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
