Added python example 2. Patched octave example 2 comments and python example 1 comments

This commit is contained in:
joseph.lizier 2013-06-05 05:33:49 +00:00
parent c767ae929f
commit 7336780b2b
3 changed files with 38 additions and 4 deletions

View File

@ -7,7 +7,7 @@
% Change location of jar to match yours:
javaaddpath('../../infodynamics.jar');
% Create many columns in a multidimensional array,
% Create many columns in a multidimensional array (2 rows by 100 columns),
% where the next time step (row 2) copies the value of the column on the left
% from the previous time step (row 1):
twoDTimeSeriesOctave = (rand(1, 100)>0.5)*1;
@ -25,8 +25,6 @@ twoDTimeSeriesJavaInt = octaveToJavaIntMatrix(twoDTimeSeriesOctave);
teCalc=javaObject('infodynamics.measures.discrete.ApparentTransferEntropyCalculator', 2, 1);
teCalc.initialise();
% Add observations of transfer across one cell to the right per time step:
% (Note this only looks at column 1->2, we don't wrap around the columns unless
% we have previously called setPeriodicBoundaryConditions(true))
teCalc.addObservations(twoDTimeSeriesJavaInt, 1);
fprintf('The result should be close to 1 bit here, since we are executing copy operations of what is effectively a random bit to each cell here: ');
result2D = teCalc.computeAverageLocalOfObservations()

View File

@ -19,7 +19,7 @@ sourceArray2 = [random.randint(0,1) for r in xrange(100)]
teCalcClass = JPackage("infodynamics.measures.discrete").ApparentTransferEntropyCalculator
teCalc = teCalcClass(2,1)
teCalc.initialise()
# Since we have simple arrays of doubles, we can directly pass these in:
# Since we have simple arrays of ints, we can directly pass these in:
teCalc.addObservations(destArray, sourceArray)
print("For copied source, result should be close to 1 bit : %.4f" % teCalc.computeAverageLocalOfObservations())
teCalc.initialise()

View File

@ -0,0 +1,36 @@
# = Example 2 - Transfer entropy on multidimensional binary data =
# Simple transfer entropy (TE) calculation on multidimensional binary data using the discrete TE calculator.
# This example is important for Python users using JPype, because it shows how to handle multidimensional arrays from Python to Java.
from jpype import *
import random
# Change location of jar to match yours:
jarLocation = "../../infodynamics.jar"
# Start the JVM (add the "-Xmx" option with say 1024M if you get crashes due to not enough memory space)
startJVM(getDefaultJVMPath(), "-ea", "-Djava.class.path=" + jarLocation)
# Create many columns in a multidimensional array, e.g. for fully random values:
# twoDTimeSeriesOctave = [[random.randint(0,1)]*2 for x in xrange(10)] # for 10 rows (time-steps) for 2 variables
# However here we want 2 rows by 100 columns where the next time step (row 2) is to copy the
# value of the column on the left from the previous time step (row 1):
numObservations = 100
row1 = [random.randint(0,1) for r in xrange(numObservations)]
row2 = [row1[numObservations-1]] + row1[0:numObservations-1] # Copy the previous row, offset one column to the right
twoDTimeSeriesPython = []
twoDTimeSeriesPython.append(row1)
twoDTimeSeriesPython.append(row2)
twoDTimeSeriesJavaInt = JArray(JInt, 2)(twoDTimeSeriesPython);
# Create a TE calculator and run it:
teCalcClass = JPackage("infodynamics.measures.discrete").ApparentTransferEntropyCalculator
teCalc = teCalcClass(2,1)
teCalc.initialise()
# Add observations of transfer across one cell to the right per time step:
teCalc.addObservations(twoDTimeSeriesJavaInt, 1)
result2D = teCalc.computeAverageLocalOfObservations()
print('The result should be close to 1 bit here, since we are executing copy operations of what is effectively a random bit to each cell here: %.3f bits from %d observations' % (result2D, teCalc.getNumObservations()))