lHistory = 1 else: lHistory = int(sys.argv[2]) if (len(sys.argv) < 4): knns = [4] else: knnsStrings = sys.argv[3].split(",") knns = [int(i) for i in knnsStrings] if (len(sys.argv) < 5): numSurrogates = 0 else: numSurrogates = int(sys.argv[4]) # Read in the data datafile = '../../data/SFI-heartRate_breathVol_bloodOx.txt' rawData = readFloatsFile.readFloatsFile(datafile) # As numpy array: data = numpy.array(rawData) # Heart rate is first column, and we restrict to the samples that Schreiber mentions (2350:3550) heart = data[2349:3550, 0] # Extracts what Matlab does with 2350:3550 argument there. # Chest vol is second column chestVol = data[2349:3550, 1] # bloodOx = data[2349:3550,2]; timeSteps = len(heart) print( "TE for heart rate <-> breath rate for Kraskov estimation with %d samples:" % timeSteps)
from jpype import * import numpy # Our python data file readers are a bit of a hack, python users will do better on this: sys.path.append( "/home/abolfazl/prog/install-dir/MI_TE/infodynamics-dist-1.5/demos/python") import readFloatsFile # Add JIDT jar library to the path jarLocation = "/home/abolfazl/prog/install-dir/MI_TE/infodynamics-dist-1.5/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) # 0. Load/prepare the data: dataRaw = readFloatsFile.readFloatsFile( "/home/abolfazl/prog/install-dir/MI_TE/infodynamics-dist-1.5/demos/data/2coupledRandomCols-1.txt" ) # As numpy array: data = numpy.array(dataRaw) variable = data[:, 0] # 1. Construct the calculator: calcClass = JPackage("infodynamics.measures.continuous.kozachenko" ).EntropyCalculatorMultiVariateKozachenko calc = calcClass() # 2. Set any properties to non-default values: # No properties were set to non-default values # 3. Initialise the calculator for (re-)use: calc.initialise() # 4. Supply the sample data: calc.setObservations(variable) # 5. Compute the estimate:
from jpype import * import numpy # Our python data file readers are a bit of a hack, python users will do better on this: sys.path.append( "/home/deepak/Desktop/Research/PaperSubmissions/IROS2018/InfoDynamics/demos/python" ) import readFloatsFile # Add JIDT jar library to the path jarLocation = "/home/deepak/Desktop/Research/PaperSubmissions/IROS2018/InfoDynamics/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) # 0. Load/prepare the data: dataRaw = readFloatsFile.readFloatsFile( "/home/deepak/Desktop/Research/PaperSubmissions/IROS2018/InfoDynamics/demos/data/2coupledRandomCols-1.txt" ) # As numpy array: data = numpy.array(dataRaw) source = data[:, 0] destination = data[:, 1] # 1. Construct the calculator: calcClass = JPackage("infodynamics.measures.continuous.kraskov" ).TransferEntropyCalculatorKraskov calc = calcClass() # 2. Set any properties to non-default values: calc.setProperty("k_HISTORY", "2") # 3. Initialise the calculator for (re-)use: calc.initialise() # 4. Supply the sample data:
# List of column numbers for joint variables 1 and 2: # (you can select any columns you wish to be contained in each variable) jointVariable1Columns = [0, 1] # array indices start from 0 in python jointVariable2Columns = [2, 3] # 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;3) as 0.10044, MI([1,2], [3,4]) = 0.36353 (NATS not bits) # implementingClass = "infodynamics.measures.continuous.kernel.MutualInfoCalculatorMultiVariateKernel" # implementingClass = "infodynamics.measures.continuous.gaussian.MutualInfoCalculatorMultiVariateGaussian" implementingClass = "infodynamics.measures.continuous.kraskov.MutualInfoCalculatorMultiVariateKraskov1" # --------------------- # 2. Load in the data data = readFloatsFile.readFloatsFile(datafile) # As numpy array: A = numpy.array(data) # Pull out the columns from the data set for a univariate MI calculation: univariateSeries1 = A[:, univariateSeries1Column] univariateSeries2 = A[:, univariateSeries2Column] # Pull out the columns from the data set for a multivariate MI calculation: jointVariable1 = A[:, jointVariable1Columns] jointVariable2 = A[:, jointVariable2Columns] # -------------------- # 3. Dynamically instantiate an object of the given class: # (in fact, all java object creation in python 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/example6 where we have to handle this manually) indexOfLastDot = string.rfind(implementingClass, ".")
def te_alt_motor(args): """calc sensor/motor TE""" tblfilename = "bf_optimize_mavlink.h5" h5file = tb.open_file(tblfilename, mode = "a") table = h5file.root.v2.evaluations # table.cols.mse.createCSIndex() from jpype import * # I think this is a bit of a hack, python users will do better on this: sys.path.append("../../infodynamics-dist/demos/python") import readFloatsFile # Add JIDT jar library to the path jarLocation = "../../infodynamics-dist/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) # 0. Load/prepare the data: dataRaw = readFloatsFile.readFloatsFile("/home/src/QK/infodynamics-dist/demos/data/2coupledBinaryColsUseK2.txt") # As numpy array: data = np.array(dataRaw) source = data[:,0] dest = data[:,1] print type(source), source.shape print source.dtype # mutual information # 1. Construct the calculator: calcClassMI = JPackage("infodynamics.measures.continuous.kraskov").MutualInfoCalculatorMultiVariateKraskov1 calcMI = calcClassMI() # 2. Set any properties to non-default values: calcMI.setProperty("TIME_DIFF", "1") # 3. Initialise the calculator for (re-)use: calcMI.initialise() # transfer entropy # 1. Construct the calculator: calcClass = JPackage("infodynamics.measures.continuous.kraskov").TransferEntropyCalculatorKraskov calc = calcClass() # 2. Set any properties to non-default values: calc.setProperty("k_HISTORY", "1") # calc.setProperty("k_TAU", "2") calc.setProperty("l_HISTORY", "100") # calc.setProperty("l_TAU", "2") # 3. Initialise the calculator for (re-)use: calc.initialise() # 4. Supply the sample data: print source.dtype, dest.dtype print source.shape, dest.shape calc.setObservations(source, dest) # 5. Compute the estimate: result = calc.computeAverageLocalOfObservations() print("TE_Kraskov (KSG)(col_0 -> col_1) = %.4f nats\n" % result) for x in table.itersorted("mse"): sensor = x["timeseries"][:,1].astype(np.float64) motor = x["timeseries"][:,4].astype(np.float64) pl.plot(sensor) pl.plot(motor) pl.show() # sys.exit() # print "s,m", sensor, motor # print "s,m (mean)", np.mean(sensor), np.mean(motor) # print "s", type(sensor), sensor.shape # print "m", type(motor), motor.shape # # 4. Supply the sample data: # calcMI.initialise() # calcMI.setObservations(sensor, motor) # # 5. Compute the estimate: # result = calcMI.computeAverageLocalOfObservations() # print("mse = %f, mi = %.4f nats" % (x["mse"], result)) # 4. Supply the sample data: # print calc calc.setObservations(sensor, motor) # 5. Compute the estimate: result = calc.computeAverageLocalOfObservations() print("mse: %f, TE_Kraskov (KSG)(col_0 -> col_1) = %.4f nats" % (x["mse"], result))
import numpy as np import readFloatsFile #--------------------- # Change location of jar to match yours: # 0. Set package 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) #--------------------- # 1. Load in the data (loop for transfer matrix) datafile1 = 'data/returns_sorted.txt' data1 = readFloatsFile.readFloatsFile(datafile1) A = np.array(data1) C = np.transpose(A) dim = np.shape(C) t = dim[0] n = dim[1] print t,n TRANSFER = np.zeros(shape=(n,n)) for j in range(n): for k in range(n): transfer = 0.0 serie1 = []; serie2 = [] for i in range(t): serie1.append(C[i,j])