通过目录中的15 HDF5运行以计算功能,但再次获得同一表的重申

发布于 2025-01-18 07:33:33 字数 973 浏览 2 评论 0原文

我目前已经编写了一个代码来运行目录中的所有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雾里花 2025-01-25 07:33:33

简短答案,要获得y值,您应该使用y = table1 [1],而不是y = table1 [0]。您将值读取为x = table1 [“ index”] - 应该使用x = table1 [0]。另外,当您调用trpz()simps()时,您是否意识到您不使用X。您正在创建2个dataframes:xdata_dftable1,并且仅使用table1 - 为什么?如果您只需要X/Y数据,则可以直接从数据集中读取值(不需要数据框架)。

注意:上面的代码缺少h5py.file()打开H5文件。

Finally, you can simplify and cleanup your code as follows:

for filename in glob.iglob(f'{directory}/*.hdf5'):
    with h5py.File(filename,'r') as file:
        xdata = file['data'][()]
        x = xdata[:,0] # or x = file['data'][:,0]
        y = xdata[:,1] # or y = file['data'][:,1]       

        # 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)

Or, if you prefer to use dataframes:

for filename in glob.iglob(f'{directory}/*.hdf5'):
    with h5py.File(filename,'r') as file:
        xdata = file['data'][()]
        xdata_df = pd.DataFrame(xdata)
        table1 = pd.DataFrame(xdata_df).reset_index() 
        x = table1[0]
        y = table1[1]  

        # 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)

Short answer, to get the Y values, you should use y = table1[1],and not y = table1[0]. You read the values as x = table1["index"] - you should use x = table1[0]. Also, do you realize you aren't using x when you calltrpz() and simps(). You are creating 2 dataframes: xdata_df and table1 and only use table1 - 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:

for filename in glob.iglob(f'{directory}/*.hdf5'):
    with h5py.File(filename,'r') as file:
        xdata = file['data'][()]
        x = xdata[:,0] # or x = file['data'][:,0]
        y = xdata[:,1] # or y = file['data'][:,1]       

        # 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)

Or, if you prefer to use dataframes:

for filename in glob.iglob(f'{directory}/*.hdf5'):
    with h5py.File(filename,'r') as file:
        xdata = file['data'][()]
        xdata_df = pd.DataFrame(xdata)
        table1 = pd.DataFrame(xdata_df).reset_index() 
        x = table1[0]
        y = table1[1]  

        # 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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文