如何在Python中快速高效地从多维txt文件读取数据? (实时监控应用)
我不是程序员,我正在尝试为传感器系统实现实时监控应用程序。文件太大,无法上传到 GitHub,但我会尽力解释主要问题。
数据
x: 时间(以秒为单位)
y1:以逗号分隔的强度列表
y2:以逗号分隔的波长列表。
因此,对于每个数据点/时间,传感器都会保存完整的光谱并关闭 txt 文件。然后它再次打开它并在非常短的毫秒积分时间后保存下一个数据点/时间。
我的目标
我想从光谱中选择一个波长(或多个波长)并实时绘制时间/强度曲线。
我的问题
我的代码效率很差。它需要大量时间来转换原始数据,它本质上有两个不同的分隔符(制表符和逗号),而且还需要找到接近我选择的值的波长(我选择一个整数,但列表只包含小数)。
我的代码
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
folder = '...'
file = '...txt'
wavelength1 = 940
wavelength2 = 930
wavelength3 = 950
df = pd.read_table(folder+file, skiprows=[1], index_col=0)
time = df['Elapsed Time (Seconds)']
intensity = df['Spectra (Spectra Intensity)'].str.split(',', expand=False)
spectrum = df['Spectra Wavelength (nanometers)'].str.split(',', expand=False)
x = []
y1 = []
y2 = []
y3 = []
for i in range(len(df)):
A = np.array([float(j) for j in spectrum[i]])
index1 = np.where(A == min(A, key=lambda k:abs(k-wavelength1)))
index2 = np.where(A == min(A, key=lambda k:abs(k-wavelength2)))
index3 = np.where(A == min(A, key=lambda k:abs(k-wavelength3)))
x.append(float(time[i]))
y1.append(float(intensity[i][int(index1[0])]))
y2.append(float(intensity[i][int(index2[0])]))
y3.append(float(intensity[i][int(index3[0])]))
plt.plot(x, y1, label='940 nm')
plt.plot(x, y2, label='930 nm')
plt.plot(x, y3, label='950 nm')
plt.show()
如果您有此类实现的经验,我也会感谢有关高效实时绘图库等的提示。:)
非常感谢。
更新
我在这里制作了一个测试文件A2422_Spectra,以便您自己查看。
I am not a programmer and I am trying to implement a real-time monitoring application for a sensor system. The file is too big to upload to GitHub, but I will try my best to explain the main problem.
The data
x: time in seconds
y1: comma-separated list of intensities
y2: comma-separated list of wavelengths.
So for each datapoint/time, the sensor saves a complete spectrum and closes the txt file. Then it opens it again and saves the next datapoint/time after a very short integration time of milliseconds.
My goal
I want to choose a wavelength (or multiple ones) from the spectrum and plot the time/intensity curve in real-time.
My problem
The efficiency of my code is very bad. It needs a lot of time to convert the raw data, which essentially has two different delimiters (tab and comma), but also to find the wavelengths close to my chosen value (I choose an integer, but the list only contains decimals).
My code
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
folder = '...'
file = '...txt'
wavelength1 = 940
wavelength2 = 930
wavelength3 = 950
df = pd.read_table(folder+file, skiprows=[1], index_col=0)
time = df['Elapsed Time (Seconds)']
intensity = df['Spectra (Spectra Intensity)'].str.split(',', expand=False)
spectrum = df['Spectra Wavelength (nanometers)'].str.split(',', expand=False)
x = []
y1 = []
y2 = []
y3 = []
for i in range(len(df)):
A = np.array([float(j) for j in spectrum[i]])
index1 = np.where(A == min(A, key=lambda k:abs(k-wavelength1)))
index2 = np.where(A == min(A, key=lambda k:abs(k-wavelength2)))
index3 = np.where(A == min(A, key=lambda k:abs(k-wavelength3)))
x.append(float(time[i]))
y1.append(float(intensity[i][int(index1[0])]))
y2.append(float(intensity[i][int(index2[0])]))
y3.append(float(intensity[i][int(index3[0])]))
plt.plot(x, y1, label='940 nm')
plt.plot(x, y2, label='930 nm')
plt.plot(x, y3, label='950 nm')
plt.show()
If you have experience with such implementations, I would also appreciate tips for efficient live plotting libraries, etc. :)
Thank you very much.
Update
I made a test file here A2422_Spectra so you can see for yourself.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论