读取“ Julia”的JLD输出文件。在Python

发布于 2025-02-13 07:37:16 字数 743 浏览 0 评论 0原文

我有一块朱莉娅代码,其中使用“假设持有人”软件包。它基本上使用OneSamplettest运行一系列t检验,并将它们全部存储到一个JLD文件中。通过朱莉娅(Julia),我可以看到一个somplettest的内容如下。

我想编写一个python脚本以读取此文件的元素“双面P值”。

import h5py
f = h5py.File("filename.jld", "r")
test = f["p_value_Never_vs_AtoBat60_FP"][()]
print(test)

我正在使用H5PY软件包,但是当我打印元素时(此处,P_VALUE_NEVER_VS_ATOBAT60_FP),我会得到以下输出,这显然是指针而不是对象。

(100000, <HDF5 object reference>, 99999, <HDF5 object reference>, <HDF5 object reference>, <HDF5 object reference>)

我如何从Python的指针中检索对象?

谢谢,很抱歉漫长的问题!

I have a piece of Julia code, which uses "HypothesisTests" package. It basically runs a series of t-test using OneSampleTTest, and stores them all into one jld file. Through Julia, I can see that the contents for one OneSampleTTest is as follows.

enter image description here

I would like to write a Python script to read elements from this file, for example, "two-sided p-value".

import h5py
f = h5py.File("filename.jld", "r")
test = f["p_value_Never_vs_AtoBat60_FP"][()]
print(test)

I am using h5py package, but when I print the element (here, p_value_Never_vs_AtoBat60_FP), I get the following output, which is clearly the pointers not the objects.

(100000, <HDF5 object reference>, 99999, <HDF5 object reference>, <HDF5 object reference>, <HDF5 object reference>)

How can I retrieve the objects from the pointers in Python?

Thanks and sorry for the long question!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

骑趴 2025-02-20 07:37:16

有趣的...这是今天第二个有关使用H5PY访问HDF5对象引用的问题。一个对象引用指向文件中的另一个对象(通常是另一个数据集,但也可能是组)。从对象引用访问数据比将数据集读取为浮点数或INT的数组要复杂得多。首先,您必须删除对象引用,然后从引用对象中读取数据。不难,只是参与其中。您可以在此处获取详细信息:“ nofollow noreferrer”> h5py文档区域参考

您显示的H5PY输出为我提出了一些问题。通常,HDF5数据集是均匀的: - 所有数组值都是dtype。看起来您的数据集包含整数和对象引用,这是非常不寻常的。

使用您的文件,从引用中获取数据是这样完成的(很难编写完美的代码而无需进行H5文件以进行测试):

import h5py
with h5py.File("filename.jld", "r") as h5f:
    ds_name = "p_value_Never_vs_AtoBat60_FP"
    # create a h5py dataset object (can be used like a np.array)
    obj_ref_ds = h5f[ds_name]
    print(f"For Dataset name: {ds_name}; shape: {obj_ref_ds.shape}; dtype: {obj_ref_ds.dtype}")
    # the loop below assumes the dataset only contains object references
    # also dimension/shape is assumed to be 1 axis = (n,)
    # this may or MAY NOT be correct
    for n_cnt in range(obj_ref_ds.shape[0]):
        # get dataset object from object reference:
        ref_ds = obj_ref_ds[n_cnt]
        print(f"For object #: {n_cnt}; shape: {ref_ds.shape}; dtype: {ref_ds.dtype}")

Interesting...this is the second question today about accessing HDF5 object references with h5py. An object reference points to another object in the file (typically another dataset, but could also be a group). Accessing data from object references is slightly more complicated than reading a dataset as an array of floats or ints. First you have to de-reference the object reference, then read the data from the referenced object. Not hard, just more involved. You can get the details here: h5py documentation on object and region references.

The h5py output you show creates some questions (for me). Typically HDF5 datasets are homogeneous: -- all array values are the dtype. It looks like your dataset contains both integers and object references, which is very unusual.

Using your file, getting the data from the reference is done something like this (hard to write perfect code without the H5 file for testing):

import h5py
with h5py.File("filename.jld", "r") as h5f:
    ds_name = "p_value_Never_vs_AtoBat60_FP"
    # create a h5py dataset object (can be used like a np.array)
    obj_ref_ds = h5f[ds_name]
    print(f"For Dataset name: {ds_name}; shape: {obj_ref_ds.shape}; dtype: {obj_ref_ds.dtype}")
    # the loop below assumes the dataset only contains object references
    # also dimension/shape is assumed to be 1 axis = (n,)
    # this may or MAY NOT be correct
    for n_cnt in range(obj_ref_ds.shape[0]):
        # get dataset object from object reference:
        ref_ds = obj_ref_ds[n_cnt]
        print(f"For object #: {n_cnt}; shape: {ref_ds.shape}; dtype: {ref_ds.dtype}")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文