首页 > 解决方案 > 在 runHeartBreathRateKraskov.py 代码中出现错误 IndentationError:意外缩进?

问题描述

我正在运行下面的代码以获取 runHeartBreathRateKraskov,我正面临这个问题和以下错误。想要在 runHeartBreathRateKraskov 程序中运行下面的代码来计算传输熵。我是新手,对熵转移和互信息知之甚少。我还附上了我的信息数据集。

from jpype import *
    ^
IndentationError: unexpected indent
# Run e.g. python runHeartBreathRateKraskov.py 2 2 1,2,3,4,5,6,7,8,9,10

from jpype import *
import sys
import os
import random
import math
import string
import numpy
# Import our readFloatsFile utility in the above directory:
sys.path.append(os.path.relpath(".."))
import readFloatsFile

# Change location of jar to match yours:
#jarLocation = "../../../infodynamics.jar"
jarLocation = "/home/humair/Documents/Transfer Entropy/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)

# Read in the command line arguments and assign default if required.
# first argument in argv is the filename, so program arguments start from index 1.
if (len(sys.argv) < 2):
    kHistory = 1;
else:
    kHistory = int(sys.argv[1]);
if (len(sys.argv) < 3):
    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 = '/home/humair/Documents/Transfer Entropy/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);

# Using a KSG estimator for TE is the least biased way to run this:
teCalcClass = JPackage("infodynamics.measures.continuous.kraskov").TransferEntropyCalculatorKraskov
teCalc = teCalcClass();

teHeartToBreath = [];
teBreathToHeart = [];

for knnIndex in range(len(knns)):
    knn = knns[knnIndex];
    # Compute a TE value for knn nearest neighbours

    # Perform calculation for heart -> breath (lag 1)
    teCalc.initialise(kHistory,1,lHistory,1,1);
    teCalc.setProperty("k", str(knn));
    teCalc.setObservations(JArray(JDouble, 1)(heart),
                JArray(JDouble, 1)(chestVol));
    teHeartToBreath.append( teCalc.computeAverageLocalOfObservations() );
    if (numSurrogates > 0):
        teHeartToBreathNullDist = teCalc.computeSignificance(numSurrogates);
        teHeartToBreathNullMean = teHeartToBreathNullDist.getMeanOfDistribution();
        teHeartToBreathNullStd = teHeartToBreathNullDist.getStdOfDistribution();

    # Perform calculation for breath -> heart (lag 1)
    teCalc.initialise(kHistory,1,lHistory,1,1);
    teCalc.setProperty("k", str(knn));
    teCalc.setObservations(JArray(JDouble, 1)(chestVol),
                JArray(JDouble, 1)(heart));
    teBreathToHeart.append( teCalc.computeAverageLocalOfObservations() );
    if (numSurrogates > 0):
        teBreathToHeartNullDist = teCalc.computeSignificance(numSurrogates);
        teBreathToHeartNullMean = teBreathToHeartNullDist.getMeanOfDistribution();
        teBreathToHeartNullStd = teBreathToHeartNullDist.getStdOfDistribution();

    print("TE(k=%d,l=%d,knn=%d): h->b = %.3f" % (kHistory, lHistory, knn, teHeartToBreath[knnIndex])), # , for no newline
    if (numSurrogates > 0):
        print(" (null = %.3f +/- %.3f)" % (teHeartToBreathNullMean, teHeartToBreathNullStd)),
    print(", b->h = %.3f nats" % teBreathToHeart[knnIndex]),
    if (numSurrogates > 0):
        print("(null = %.3f +/- %.3f)" % (teBreathToHeartNullMean, teBreathToHeartNullStd)),
    print

# Exercise: plot the results

数据集是:

The first column is heart rate, the second is chest volume, and the third is blood oxygen concentration.
76.53 8320 7771 
76.53 8117 7774 
76.15 7620 7788 
75.39 6413 7787 
75.51 7518 7767 
76.67 1247 7773 
78.55 -3525 7784 
79.96 2388 7764 
79.71 8296 7775 
78.30 7190 7784 
77.02 6024 7777 
76.62 5825 7784 
76.53 5154 7809 
76.65 7464 7805 
76.95 5345 7806 
78.46 -993 7813 

标签: pythonnumpyentropyjpype

解决方案


推荐阅读