forked from jlizier/jidt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding R example 5 and 6, adding notices to jar dist, finalising pyth…
…on example 6 using file reading utilities; adding extra printout to Example 4 in Java
- Loading branch information
Showing
11 changed files
with
251 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
## | ||
## Java Information Dynamics Toolkit (JIDT) | ||
## Copyright (C) 2012, Joseph T. Lizier | ||
## | ||
## This program is free software: you can redistribute it and/or modify | ||
## it under the terms of the GNU General Public License as published by | ||
## the Free Software Foundation, either version 3 of the License, or | ||
## (at your option) any later version. | ||
## | ||
## This program is distributed in the hope that it will be useful, | ||
## but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
## GNU General Public License for more details. | ||
## | ||
## You should have received a copy of the GNU General Public License | ||
## along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
## | ||
|
||
def readFloatsFile(filename): | ||
"Read a 2D array of floats from a given file" | ||
with open(filename) as f: | ||
# Space separate numbers, one time step per line, each column is a variable | ||
array = [] | ||
for line in f: # read all lines | ||
array.append([float(x) for x in line.split()]) | ||
return array | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
## | ||
## Java Information Dynamics Toolkit (JIDT) | ||
## Copyright (C) 2012, Joseph T. Lizier | ||
## | ||
## This program is free software: you can redistribute it and/or modify | ||
## it under the terms of the GNU General Public License as published by | ||
## the Free Software Foundation, either version 3 of the License, or | ||
## (at your option) any later version. | ||
## | ||
## This program is distributed in the hope that it will be useful, | ||
## but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
## GNU General Public License for more details. | ||
## | ||
## You should have received a copy of the GNU General Public License | ||
## along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
## | ||
|
||
def readIntsFile(filename): | ||
"Read a 2D array of int from a given file" | ||
with open(filename) as f: | ||
# Space separate numbers, one time step per line, each column is a variable | ||
array = [] | ||
for line in f: # read all lines | ||
array.append([int(x) for x in line.split()]) | ||
return array | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
## | ||
## Java Information Dynamics Toolkit (JIDT) | ||
## Copyright (C) 2012, Joseph T. Lizier | ||
## | ||
## This program is free software: you can redistribute it and/or modify | ||
## it under the terms of the GNU General Public License as published by | ||
## the Free Software Foundation, either version 3 of the License, or | ||
## (at your option) any later version. | ||
## | ||
## This program is distributed in the hope that it will be useful, | ||
## but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
## GNU General Public License for more details. | ||
## | ||
## You should have received a copy of the GNU General Public License | ||
## along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
## | ||
|
||
# = Example 5 - Multivariate transfer entropy on binary data = | ||
|
||
# Multivariate transfer entropy (TE) calculation on binary data using the discrete TE calculator: | ||
|
||
# Load the rJava library and start the JVM | ||
library("rJava") | ||
.jinit() | ||
|
||
# Change location of jar to match yours: | ||
# IMPORTANT -- If using the default below, make sure you have set the working directory | ||
# in R (e.g. with setwd()) to the location of this file (i.e. demos/r) !! | ||
.jaddClassPath("../../infodynamics.jar") | ||
|
||
# Generate some random binary data. | ||
numObservations <- 100 | ||
sourceArray<-matrix(sample(0:1,numObservations*2, replace=TRUE),numObservations,2) | ||
sourceArray2<-matrix(sample(0:1,numObservations*2, replace=TRUE),numObservations,2) | ||
# Destination variable takes a copy of the first bit of the source in bit 1, | ||
# and an XOR of the two bits of the source in bit 2: | ||
destArray <- cbind( c(0L, sourceArray[1:numObservations-1,1]), # column 1 | ||
c(0L, 1L*xor(sourceArray[1:numObservations-1,1], | ||
sourceArray[1:numObservations-1,2]))) # column 2 | ||
|
||
# Convert the 2D arrays to Java format: | ||
sourceArrayJava <- .jarray(sourceArray, "[I", dispatch=TRUE) | ||
sourceArray2Java <- .jarray(sourceArray2, "[I", dispatch=TRUE) | ||
destArrayJava <- .jarray(destArray, "[I", dispatch=TRUE) | ||
|
||
# Create a TE calculator and run it: | ||
teCalc<-.jnew("infodynamics/measures/discrete/TransferEntropyCalculatorDiscrete", 4L, 1L) | ||
.jcall(teCalc,"V","initialise") # V for void return value | ||
# We need to construct the joint values for the dest and source before we pass them in, | ||
# and need to use the matrix conversion routine when calling from Matlab/Octave: | ||
mUtils<-.jnew("infodynamics/utils/MatrixUtils") | ||
.jcall(teCalc,"V","addObservations", | ||
.jcall(mUtils,"[I","computeCombinedValues", sourceArrayJava, 2L), | ||
.jcall(mUtils,"[I","computeCombinedValues", destArrayJava, 2L)) | ||
result<-.jcall(teCalc,"D","computeAverageLocalOfObservations") | ||
cat("For source which the 2 bits are determined from, result should be close to 2 bits : ", result, "\n") | ||
|
||
.jcall(teCalc,"V","initialise") | ||
.jcall(teCalc,"V","addObservations", | ||
.jcall(mUtils,"[I","computeCombinedValues", sourceArray2Java, 2L), | ||
.jcall(mUtils,"[I","computeCombinedValues", destArrayJava, 2L)) | ||
result2<-.jcall(teCalc,"D","computeAverageLocalOfObservations") | ||
cat("For random source, result should be close to 0 bits in theory: ", result2, "\n"); | ||
cat("Result for random source is inflated towards 0.3 due to finite observation length ", | ||
.jcall(teCalc,"I","getNumObservations"), "\n", | ||
"One can verify that the answer is consistent with that from a\n", | ||
"random source by checking: teCalc.computeSignificance(1000); ans.pValue\n"); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
## | ||
## Java Information Dynamics Toolkit (JIDT) | ||
## Copyright (C) 2012, Joseph T. Lizier | ||
## | ||
## This program is free software: you can redistribute it and/or modify | ||
## it under the terms of the GNU General Public License as published by | ||
## the Free Software Foundation, either version 3 of the License, or | ||
## (at your option) any later version. | ||
## | ||
## This program is distributed in the hope that it will be useful, | ||
## but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
## GNU General Public License for more details. | ||
## | ||
## You should have received a copy of the GNU General Public License | ||
## along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
## | ||
|
||
# Example 6 - Mutual information calculation with dynamic specification of calculator | ||
|
||
# This example shows how to write R code to take advantage of the | ||
# common interfaces defined for various information-theoretic calculators. | ||
# Here, we use the common form of the infodynamics.measures.continuous.MutualInfoCalculatorMultiVariate | ||
# interface (which is never named here) to write common code into which we can plug | ||
# one of three concrete implementations (kernel estimator, Kraskov estimator or | ||
# linear-Gaussian estimator) by dynamically supplying the class name of | ||
# the concrete implementation. | ||
|
||
# Load the rJava library and start the JVM | ||
library("rJava") | ||
.jinit() | ||
|
||
# Change location of jar to match yours: | ||
# IMPORTANT -- If using the default below, make sure you have set the working directory | ||
# in R (e.g. with setwd()) to the location of this file (i.e. demos/r) !! | ||
.jaddClassPath("../../infodynamics.jar") | ||
|
||
#--------------------- | ||
# 1. Properties for the calculation (these are dynamically changeable, you could | ||
# load them in from another properties file): | ||
# The name of the data file (relative to this directory) | ||
datafile <- "../data/4ColsPairedNoisyDependence-1.txt" | ||
# List of column numbers for variables 1 and 2: | ||
# (you can select any columns you wish to be contained in each variable) | ||
variable1Columns <- c(1,2) # array indices start from 1 in R | ||
variable2Columns <- c(3,4) | ||
# The name of the concrete implementation of the interface | ||
# infodynamics.measures.continuous.MutualInfoCalculatorMultiVariate | ||
# which we wish to use for the calculation. | ||
# Note that one could use any of the following calculators (try them all!): | ||
# implementingClass <- "infodynamics/measures/continuous/kraskov/MutualInfoCalculatorMultiVariateKraskov1" # MI([1,2], [3,4]) = 0.36353 | ||
# implementingClass <- "infodynamics/measures/continuous/kernel/MutualInfoCalculatorMultiVariateKernel" | ||
# implementingClass <- "infodynamics/measures/continuous/gaussian/MutualInfoCalculatorMultiVariateGaussian" | ||
implementingClass <- "infodynamics/measures/continuous/kraskov/MutualInfoCalculatorMultiVariateKraskov1" | ||
|
||
#--------------------- | ||
# 2. Load in the data | ||
data <- read.csv(datafile, header=FALSE, sep="") | ||
# Pull out the columns from the data set which correspond to each of variable 1 and 2: | ||
variable1 <- data[, variable1Columns] | ||
variable2 <- data[, variable2Columns] | ||
# Extra step to extract the raw values from these data.frame objects: | ||
variable1 <- apply(variable1, 2, function(x) as.numeric(x)) | ||
variable2 <- apply(variable2, 2, function(x) as.numeric(x)) | ||
|
||
#--------------------- | ||
# 3. Dynamically instantiate an object of the given class: | ||
# (in fact, all java object creation in octave/matlab is dynamic - it has to be, | ||
# since the languages are interpreted. This makes our life slightly easier at this | ||
# point than it is in demos/java/example6LateBindingMutualInfo where we have to handle this manually) | ||
miCalc<-.jnew(implementingClass) | ||
|
||
#--------------------- | ||
# 4. Start using the MI calculator, paying attention to only | ||
# call common methods defined in the interface type | ||
# infodynamics.measures.continuous.MutualInfoCalculatorMultiVariate | ||
# not methods only defined in a given implementation class. | ||
# a. Initialise the calculator to use the required number of | ||
# dimensions for each variable: | ||
.jcall(miCalc,"V","initialise", length(variable1Columns), length(variable2Columns)) | ||
# b. Supply the observations to compute the PDFs from: | ||
.jcall(miCalc,"V","setObservations", | ||
.jarray(variable1, "[D", dispatch=TRUE), | ||
.jarray(variable2, "[D", dispatch=TRUE)) | ||
# c. Make the MI calculation: | ||
miValue <- .jcall(miCalc,"D","computeAverageLocalOfObservations") | ||
|
||
cat("MI calculator", implementingClass, "\n computed the joint MI as ", | ||
miValue, "\n") | ||
|
Oops, something went wrong.