jidt/demos/python/example1TeBinaryData.ipynb

102 lines
3.6 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "17e37cfc-4332-43e2-9cb7-4c1849ad894e",
"metadata": {},
"source": [
"# Example 1 - Transfer entropy on binary data\n",
"\n",
"_Copyright (C) 2024-, J.T. Lizier; Distributed under GNU General Public License v3_\n",
"\n",
"This is a sample notebook to run to check that your installation works ok"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7f18beca-d55b-4b8d-8647-256e27787c34",
"metadata": {},
"outputs": [],
"source": [
"# Import relevant libraries and start the JVM:\n",
"\n",
"from jpype import *\n",
"import numpy\n",
"import random\n",
"import os\n",
"\n",
"if (not isJVMStarted()):\n",
" # Add JIDT jar library to the path -- it should be two folders up from the location of this notebook.\n",
" jarLocation = os.path.join(os.getcwd(), \"..\", \"..\", \"infodynamics.jar\");\n",
" if (not(os.path.isfile(jarLocation))):\n",
" \texit(\"infodynamics.jar not found (expected at \" + os.path.abspath(jarLocation))\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)\n",
"\n",
"# Generate some random binary data.\n",
"sourceArray = [random.randint(0,1) for r in range(100)]\n",
"destArray = [0] + sourceArray[0:99]\n",
"sourceArray2 = [random.randint(0,1) for r in range(100)]\n",
"\n",
"# Create a TE calculator and run it:\n",
"teCalcClass = JPackage(\"infodynamics.measures.discrete\").TransferEntropyCalculatorDiscrete\n",
"teCalc = teCalcClass(2,1)\n",
"teCalc.initialise()\n",
"\n",
"# First use simple arrays of ints, which we can directly pass in:\n",
"teCalc.addObservations(sourceArray, destArray)\n",
"print(\"For copied source, result should be close to 1 bit : %.4f\" % teCalc.computeAverageLocalOfObservations())\n",
"teCalc.initialise()\n",
"teCalc.addObservations(sourceArray2, destArray)\n",
"print(\"For random source, result should be close to 0 bits: %.4f\" % teCalc.computeAverageLocalOfObservations())\n",
"\n",
"# Next, demonstrate how to do this with a numpy array\n",
"teCalc.initialise()\n",
"# Create the numpy arrays:\n",
"sourceNumpy = numpy.array(sourceArray, dtype=int)\n",
"destNumpy = numpy.array(destArray, dtype=int)\n",
"# The above can be passed straight through to JIDT in python 2:\n",
"# teCalc.addObservations(sourceNumpy, destNumpy)\n",
"# But you need to do this in python 3:\n",
"sourceNumpyJArray = JArray(JInt, 1)(sourceNumpy.tolist())\n",
"destNumpyJArray = JArray(JInt, 1)(destNumpy.tolist())\n",
"teCalc.addObservations(sourceNumpyJArray, destNumpyJArray)\n",
"print(\"Using numpy array for copied source, result confirmed as: %.4f\" % teCalc.computeAverageLocalOfObservations())\n",
"\n",
"print()\n",
"print(\"If you've got no error messages, then your JIDT set-up is ok!\");"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "da8ef381-5391-4706-be61-ad931f20d62b",
"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
}