首先,非常感谢您花时间查看我的问题和代码。我知道我的编码需要改进,但与所有新事物一样,它需要时间来完善。
我正在使用不同的功能来执行以下操作:
我在主函数中调用这些函数。
一切都运行良好,直到我第二次在DataFilter函数中运行时间循环。但是,我确实检查过我可以访问字典中压力和时间的所有三个不同的列表。为什么此时我的'索引超出范围':t=timeFROMtimes [i]
当函数第二次运行时? This is the output I get for the code
#IMPORT LIBRARIES
import glob
import pandas as pd
import matplotlib.pyplot as plt, numpy as np
from scipy.interpolate import spline
#INPUT PARAMETERS
max_iter = 3 #maximum number of iterations
i = 0 #starting number of iteration
tcounter = 0
number_of_files = 3 #number of files in directory
sourcePath = 'C:\\Users\\*' #not displaying actual directory
list_of_source_files = glob.glob(sourcePath + '/*.TXT')
pressures = {} #initialize a dictionary
times ={} #initialize a dictionary
#GET SPECIFIC FILE & PRESSURE, TIME VALUES
def Get_source_files(list_of_source_files,i):
#print('Get_source_files')
with open(list_of_source_files[i]) as source_file:
print("file entered:",i+1)
lst = []
for line in source_file:
lst.append([ float(x) for x in line.split()])
time = np.array([ x[0] for x in lst]) #first row in file and make array
void = np.array([ x[1] for x in lst]) #second row in file and make array
pressure =(np.array([ x[2] for x in lst]))*-1 #etc. & change the sign of the imported pressure data
return pressure,time
#SAVE THE TIME AND PRESSURE IN DICTIONARIES
def SaveInDictionary (Pressure,time,i):
#print('SaveInDictionary')
pressures[i]=[Pressure]
times[i]=[time]
return pressures,times
#FILTER PRESSURE DATA AND ADJUST TIME
def FilterData (pressureFROMpressures,timeFROMtimes,i):
print('FilterData')
t=timeFROMtimes [i]
p=pressureFROMpressures[i]
data_points=int((t[0]-t[-1])/-0.02) #determine nr of data points per column of the imported file
filtered_pressure = [] #create an empty list for the filtered pressure
pcounter = 0 #initiate a counter
tcounter = 0
time_new =[0,0.02] #these initial values are needed for the for-loop
for j in range(data_points):
if p[j]<8: #filter out all garbage pressure data points above 8 bar
if p[j]>0:
filtered_pressure.append(p[j]) #append the empty list ofsave all the new pressure values in new_pressure
pcounter += 1
for i in range(pcounter-1):
time_new[0]=0
time_new[i+1]=time_new[i]+0.02 #add 0.02 to the previous value
time_new.append(time) #append the time list
tcounter += 1 #increment the counter
del time_new[-1] #somehow a list of the entire time is formed at the end that should be removed
return filtered_pressure, time_new
#MAIN!!
P = list()
while (i <= number_of_files and i!=max_iter):
pressure,time = Get_source_files(list_of_source_files,i) #return pressure and time from specific file
pressures, times = SaveInDictionary (pressure,time,i)#save pressure and time in the dictionaries
i+=1 #increment i to loop
print('P1=',pressures[0])
print('P2=',pressures[1])
print('P3=',pressures[2])
print('t1=',times[0])
print('t2=',times[1])
print('t3=',times[2])
#I took this out of the main function to check my error:
for i in range(2):
filtered_pressure,changed_time = FilterData(pressures[i],times[i],i)
finalP, finalT = SaveInDictionary (filtered_pressure,changed_time,i)#save pressure and time in the dictionaries
答案 0 :(得分:0)
在循环中,您已从主要函数中取出已编入times
的主函数,但在
#I took this out of the main function to check my error:
for i in range(2):
filtered_pressure,changed_time = FilterData(pressures[i],times[i],i)
# Here you call FilterData with times[i]
但是在FilterData函数中,您调用传递的变量timeFROMtimes
,然后使用i
再次对其进行索引:
def FilterData (pressureFROMpressures,timeFROMtimes,i):
print('FilterData')
t=timeFROMtimes [i] # <-- right here
这看起来很奇怪。尝试删除其中一个索引运算符([i]
),同时删除pressures
。
正如@strubbly指出的那样,在SaveInDictionary
函数中,您指定了值[Pressure]
。方括号表示一个包含一个元素的列表,即numpy数组Pressure
。当您打印P1-3和t1-3时,可以通过数组前面的左括号在错误消息中看到这一点。
要直接保存numpy数组,请松开括号:
def save_in_dictionary(pressure_array, time, i):
pressures[i] = pressure_array
times[i] = time
return pressures, times