我有一个程序从串行读取数据,在数组中搜索大于阈值的位置,然后使用这些数组进行后续处理。当我使用我创建的数组运行模拟时(减去连续部分,我可以使用此行(其中sens1是我的数组)
i1 = np.where(sens1 >= threshold)
sens1hits = np.concatenate(i1).tolist()
读取超过阈值的值以进行处理。但是,当我使用串行读取时,会抛出此错误。
TOAsens1 = timepoints.item(s1)
AttributeError: 'list' object has no attribute 'item'
这是该行
TOAsens1 = timepoints.item(s1)
我不确定为何在与串行阅读配对时不起作用。据我所知,它正在抛出错误,因为数组是空的,因为我在没有传感器设备的情况下进行测试。以下是代码的完整上下文:
import numpy as np
import math
import serial
import xlsxwriter
from time import time
# initialize serial
ser = serial.Serial('/dev/cu.usbmodem1411', 1000000)
# set up arrays for sensor readings
start_time = time()
timepoints = []
sens1 = []
sens2 = []
sens3 = []
sens4 = []
duration = 5 # total seconds to collect data
threshold = 4 #threshold for sampling
# set up arrays to write hit times into
sens1hits = np.array([])
sens2hits = np.array([])
sens3hits = np.array([])
sens4hits = np.array([])
TOA1 = []
TOA2 = []
TOA3 = []
TOA4 = []
xcord =[]
ycord =[]
# flush any junk left in the serial buffer
ser.flushInput()
ser.reset_input_buffer() # for pyserial 3.0+
run = True
# collect the data
while run:
ser.reset_input_buffer()
data = ser.readline().split('\t')
# sometimes the incoming data is garbage, so just 'try' to do this
try:
# store the entire dataset for sensor 1 for later
sens1.append(float(data[0])*5.0/1024)
timepoints.append(time()-start_time)
current_time = timepoints[-1]
# store the entire dataset for sensor 2 for later
sens2.append(float(data[1])*5.0/1024)
# store the entire dataset for sensor 3 for later
sens3.append(float(data[2])*5.0/1024)
# store the entire dataset for sensor 2 for later
sens4.append(float(data[3])*5.0/1024)
# when time's up, kill the collect loop
if timepoints[-1] > duration: run=False
# if the try statement throws an error, just do nothing
except: pass
# close serial port
ser.close()
# find positions of target hits in string and print locations in array
print('Sensor 1 Hits:')
i1 = np.where(sens1 == threshold)
sens1hits = np.concatenate(i1).tolist()
print (sens1hits)
print('\nSensor 2 Hits:')
i2 = np.where(sens2 == threshold)
sens2hits = np.concatenate(i2).tolist()
print (sens2hits)
print('\nSensor 3 Hits:')
i3 = np.where(sens3 == threshold)
sens3hits = np.concatenate(i3).tolist()
print (sens3hits)
print('\nSensor 4 Hits:')
i4 = np.where(sens4 == threshold)
sens4hits = np.concatenate(i4).tolist()
print (sens4hits)
print('\nNumber of Hits Detected:')
n = len(sens1hits)
print n
print ('\n')
# set counter variable
f = 0
# begin array TOA array creation for each sensor
for x in xrange(0,n):
# sensor 1
s1 = sens1hits[f]
TOAsens1 = timepoints.item(s1)
TOA1.append(TOAsens1)
# sensor 2
s2 = sens2hits[f]
TOAsens2 = timepoints.item(s2)
TOA2.append(TOAsens2)
# sensor 3
s3 = sens3hits[f]
TOAsens3 = timepoints.item(s3)
TOA3.append(TOAsens3)
# sensor 4
s4 = sens4hits[f]
TOAsens4 = timepoints.item(s4)
TOA4.append(TOAsens4)
#increase counter
f += 1