{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "eefdf8a5-2b7f-45ec-93dd-44687396bb4f",
   "metadata": {},
   "source": [
    "# Ising 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 Ising 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 readIntsFile\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": "8f3cc152-2695-4830-915b-ba1bb0306b69",
   "metadata": {},
   "source": [
    "Next load in a sample data file and check that we can plot the spins at one time step ok.\n",
    "\n",
    "Make sure that you have either downloaded and unzipped the [sample data](https://github.com/jlizier/jidt-cc-extras/raw/main/course/Module08-InfoTheoryAndSelfOrganisation/Ising/Ising-data.zip) or generated sample data. If generating yourself, then fix this range of temperatures that we have data files for:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "80112a52-e42a-44c2-abbd-5d612905fec5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# We may want to loop over the data files later:\n",
    "tempsToExamine = numpy.array([2.00, 2.20, 2.26, 2.27, 2.28, 2.29, 2.34, 2.54]);\n",
    "\n",
    "# Check one data file - the way we reshape it into a grid may be useful for you to copy later:\n",
    "sampleTemp = tempsToExamine[3];\n",
    "filenameTemplate = './spins-%.2f.txt'\n",
    "sampleDataFilename = filenameTemplate % sampleTemp\n",
    "\n",
    "import os.path\n",
    "if (not os.path.isfile(sampleDataFilename)):\n",
    "    raise SystemExit(\"Ising model exported data files (e.g. %s) do not appear to exist in the current folder -- please fix this!\" % sampleDataFilename)\n",
    "\n",
    "dataRaw = readIntsFile.readIntsFile(sampleDataFilename)\n",
    "# As numpy array:\n",
    "data = numpy.array(dataRaw)\n",
    "import math\n",
    "modelSize = int(math.sqrt(data.shape[1]));\n",
    "timeSteps = data.shape[0];\n",
    "dataInLayout = numpy.zeros((timeSteps, modelSize, modelSize), dtype=int);\n",
    "for t in range(timeSteps):\n",
    "    # This reshapes in a transposed fashion to Matlab, but that doesn't matter for our purposes\n",
    "    dataInLayout[t,:,:] = numpy.reshape(data[t,:], (modelSize, modelSize));\n",
    "\n",
    "# Let's plot the last time step to check that it looks ok:\n",
    "import matplotlib.pyplot as plt\n",
    "plt.imshow( dataInLayout[timeSteps-1,:,:] )\n",
    "plt.title('Spins at t=%d for T=%.2f' % (timeSteps, sampleTemp));"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "26bd6e3c-3dfa-4e42-a384-158783eec2e2",
   "metadata": {},
   "source": [
    "# Your analysis goes next ...:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0feb0c6d-aed2-4e1c-a385-2e4398d0cfc7",
   "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
}
