通过目录中的15 HDF5运行以计算功能,但再次获得同一表的重申
我目前已经编写了一个代码来运行目录中的所有 hdf5 文件,从文件中打印出表格,为每个表格绘制一个图形,然后吐出每个表格的曲线下面积。这是代码。
import os
directory = '/Users/xx'
for filename in os.listdir(directory):
if filename.endswith(".hdf5"):
xdata = file.get('data')
xdata= np.array(xdata)
xdata_df = pd.DataFrame(xdata)
table1 = pd.DataFrame(xdata_df).reset_index()
print(table1)
x = table1["index"]
y = table1[0]
plt.figure(figsize=(10, 10))
plt.rcParams.update({'font.size': 20})
figure1 = plt.plot(x, y)
# Compute the area using the composite trapezoidal rule.
area = trapz(y, dx=100000)
print("trapz area =", area)
# Compute the area using the composite Simpson's rule.
area = simps(y, dx=100000)
print("simpsons area =", area)
continue
else:
continue
然而,我的代码似乎运行通过目录(15 个文件),但吐出完全相同的表格 15 次,图形和曲线下的面积。有谁知道为什么会发生这种情况?
I've currently written a code to run through all the hdf5 files in a directory, print out the tables from the files, plot a figure for each table, and then spit out the area under the curve for each. This is the code.
import os
directory = '/Users/xx'
for filename in os.listdir(directory):
if filename.endswith(".hdf5"):
xdata = file.get('data')
xdata= np.array(xdata)
xdata_df = pd.DataFrame(xdata)
table1 = pd.DataFrame(xdata_df).reset_index()
print(table1)
x = table1["index"]
y = table1[0]
plt.figure(figsize=(10, 10))
plt.rcParams.update({'font.size': 20})
figure1 = plt.plot(x, y)
# Compute the area using the composite trapezoidal rule.
area = trapz(y, dx=100000)
print("trapz area =", area)
# Compute the area using the composite Simpson's rule.
area = simps(y, dx=100000)
print("simpsons area =", area)
continue
else:
continue
However, my code seems to running through the directory (15 files), but spitting out the exact same table 15 times, figure and area under the curve. Does anyone know why this may be happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简短答案,要获得y值,您应该使用
y = table1 [1]
,而不是y = table1 [0]
。您将值读取为x = table1 [“ index”]
- 应该使用x = table1 [0]
。另外,当您调用trpz()
和simps()
时,您是否意识到您不使用X
。您正在创建2个dataframes:xdata_df
和table1
,并且仅使用table1
- 为什么?如果您只需要X/Y数据,则可以直接从数据集中读取值(不需要数据框架)。注意:上面的代码缺少
h5py.file()
打开H5文件。Finally, you can simplify and cleanup your code as follows:
Or, if you prefer to use dataframes:
Short answer, to get the Y values, you should use
y = table1[1]
,and noty = table1[0]
. You read the values asx = table1["index"]
- you should usex = table1[0]
. Also, do you realize you aren't usingx
when you calltrpz()
andsimps()
. You are creating 2 dataframes:xdata_df
andtable1
and only usetable1
- Why? If you just need the X/Y data, you can read the values directly from the dataset (dataframes are not required).Note: code above is missing
h5py.File()
to open the H5 file.Finally, you can simplify and cleanup your code as follows:
Or, if you prefer to use dataframes: