我有一些.csv文件,其数据长度不同。我需要加载一些已定义的色谱柱,对它们进行排序并重新采样为相同的长度(100%)。
我已经编写了som定义的嵌套循环来对数据进行排序,但最终重采样功能无法正常工作。
subjects = ['S01_']
conditions = ['Shoe1_', 'Shoe2_']
trials = ['Run_1', 'Run_2','Run_3']
results_GRF_AP = np.empty(shape=(100,6))*np.NaN
results_GRF_ML = np.empty(shape=(100,6))*np.NaN
results_GRF_VERT = np.empty(shape=(100,6))*np.NaN
results_AnkPower = np.empty(shape=(100,6))*np.NaN
ind = 0
for s, subject in enumerate(subjects):
for c, condition in enumerate(conditions):
for t, trial in enumerate(trials):
ind += 1
filename = path + subject + condition + trial + extension
try:
data2 = pd.read_csv(filename,delimiter = ';')
data2=np.array(data2)
except Exception as err:
print(filename, err)
continue
else:
print(filename, 'loaded')
pass
threshold = 30 ## defined threshold value in Newton from vertical GRF
signal = np.array(data2[:,4])
indices_bigger_than_threshold = np.where(signal > threshold)[0] # get item
non_consecutive = np.where(np.diff(indices_bigger_than_threshold) != 1)[0]+1 # +1 for selecting the next
first_bigger_than_threshold1 = np.zeros_like(signal, dtype=np.bool)
first_bigger_than_threshold1[indices_bigger_than_threshold[0]] = True # retain the first
first_bigger_than_threshold1[indices_bigger_than_threshold[non_consecutive]] = True
indices_bigger_than_threshold = np.array(indices_bigger_than_threshold)
GRF_AP = (data2[indices_bigger_than_threshold,2])
GRF_ML = (data2[indices_bigger_than_threshold,3])
GRF_VERT= (data2[indices_bigger_than_threshold,4])
Ank_Power = (data2[indices_bigger_than_threshold,28])
GRF_AP_Norm = signal.resample(GRF_AP,100)
GRF_ML_Norm = signal.resample(GRF_ML,100)
GRF_VERT_Norm= signal.resample(GRF_VERT,100)
Ank_Power_Norm = signal.resample(Ank_Power,100)
# talking one coloum from the loaded .csv file to store in the results parameter.
results_GRF_AP[:,ind-1] = GRF_AP_Norm
results_GRF_ML[:,ind-1] = GRF_ML_Norm
results_GRF_VERT[:,ind-1] = GRF_VERT_Norm
results_AnkPower[:,ind-1] = Ank_Power_Norm
所有零件都单独工作,但是在循环中我遇到一个错误 AttributeError:'numpy.ndarry'对象没有属性'resample'
如果我从scipy导入信号运行,然后对加载的主题,条件和试用版使用重新采样,则该效果正常。
答案 0 :(得分:1)
我认为您的问题在于,您正在使用调用signal的变量覆盖scipy中的signal函数:
signal = np.array(data2[:,4])
如果重命名变量,则应将其修复。