【问题标题】:importing illustris hdf5 file into numpy array using h5py使用 h5py 将 illustris hdf5 文件导入 numpy 数组
【发布时间】:2018-01-02 01:27:10
【问题描述】:

我正在使用 h5py 导入一个相当大的 illustris 星系模拟代码的 hdf5 文件。 如果有人想看,我会在此处显示文件 - 它是 1.96 GB。

https://drive.google.com/file/d/0B1Kj475OJBnuaFBIS2FhTFpvNkk/view?usp=sharing

我想让 h5py 和 numpy 显示数据表,使用 numpy.sum 对列和输出向量求和,并告诉文件只提取我想要的某些文件。这是每个星系大小为 16X56 的 A 2D 表。这是一个大型数据集,包含数百万个星系的数据(近 2GB 数据),行数超过一百万。

沿表格的维度大小 56:每个 bin 代表年龄。沿维度大小 16 求和会为您提供一个大小为 56 的每个星系的一维向量,它代表恒星质量(以每个年龄 bin 内形成的 1e^10 M(太阳)为单位。

我的目标是使用 python 和 h5py 来:

1-使用numpy查看数据数组并沿维度大小16求和得到56个向量)显示在python中

2- 让 numpy 消除稳定的恒星形成率,以专门提取在 1 Gyr 和 2 Gyr 之间突然爆发然后停止的星系 - 有没有办法做到这一点?这将消除我必须查看的大量星系。这与 E+A 星系经历了突然的星暴然后停止有关。

通过 numpy 对向量求和后将显示的年龄 bin 如下:

在吉尔

<0.005, 
0.005 - 0.015, 
0.015 - 0.025, 
0.025 - 0.035, 
0.035 - 0.045, 
0.045 - 0.055, 
0.055 - 0.065, 
0.065 - 0.075, 
0.075 - 0.085, 
0.085 - 0.095, 
0.095 - 0.125, 
0.125 - 0.175,
0.175 - 0.225,
0.225 - 0.275,
0.275 - 0.325,
0.325 - 0.375,
0.375 - 0.425,
0.425 - 0.475,
0.475 - 0.55,
0.55 - 0.65,
0.65 - 0.75,
0.75 - 0.85,
0.85 - 0.95,
0.95 - 1.125,
1.125 - 1.375,
1.375 - 1.625,
1.625 - 1.875,
1.875 - 2.125,
2.125 - 2.375,
2.375 - 2.625,
2.625 - 2.875,
2.875 - 3.125,
3.125 - 3.375,
3.375 - 3.625,
3.625 - 3.875,
3.875 - 4.25,
4.25 - 4.75,
4.75 - 5.25,
5.25 - 5.75,
5.75 - 6.25,
6.25 - 6.75,
6.75 - 7.25,
7.25 - 7.75,
7.75 - 8.25,
8.25 - 8.75,
8.75 - 9.25,
9.25 - 9.75,
9.75 - 10.25,
10.25 - 10.75,
10.75 - 11.25,
11.25 - 11.75,
11.75 - 12.25,
12.25 - 12.75,
12.75 - 13.25,
13.25 - 13.75, >13.75.

我知道如何阅读数据的含义,但由于我是在编码和一般编码中使用 hdf5 文件的业余爱好者, 我无法找出具体的命令来让 h5py 和 numpy 沿着我想要的维度求和、显示向量等。

有经验的人知道怎么做吗?

谢谢,

薇诺娜

【问题讨论】:

  • 您有一个大小为 (56, 4366546) 的数据集。那么您的数据是如何真正组织起来的呢?例如 [:,0:16] 是否代表一个 bin?
  • 第一个问题 - 您是否成功地从文件中加载了一个或多个数据集?
  • 我已经完成了这些命令 f = h5py.File('SFRH_binsv2_for_ClaireDickey_L75n1820FP135_Zcollapsed.hdf5', 'r') data = f.get('/Users/Jim/Documents/illustris_python/SFRH_binsv2_for_ClaireDickey_L75n1820FP135_Zcol data_as_array = np.array(data) 但我还没有将它们加载到数据集中,这就是我试图找到要执行的命令
  • 如果您有更多信息,请编辑问题。我只能看到第一个维度是 56,这看起来很熟悉,就像你描述的那样。文件名(“Z_collapsed”)也可能会给出一些提示。文本文件是关于什么的?最简单的解决方案是 3 轴被折叠,可以通过简单的重塑来恢复。但是为什么第二个轴不能被 16 整除呢?您必须询问将 HDF5 文件提供给您的人。

标签: python numpy hdf5 h5py


【解决方案1】:

代码以确切的形式发送给我,说明它是每个星系的 16X56 阵列。如果看起来不是那样,那么格式肯定发生了一些事情。 链接:https://drive.google.com/file/d/0B1Kj475OJBnuTGpGby1iRTd4Yzg/view?usp=sharing 文本文件链接:https://drive.google.com/file/d/0B1Kj475OJBnuaWgzYVRHUnhQdkU/view?usp=sharing

    f.keys() #Get the names of the datasets
# there is one dataset called 'FormedStellarMass'
dset=f['FormedStellarMass'] #Open this dataset
shape_of_Array=dset.shape #Gives you the shape of the array
(56, 4366546) 
dset[1,0:56]

    array([  5.90118565e-04,   0.00000000e+00,   2.02415307e-03,
         1.97571842e-03,   9.65413419e-05,   0.00000000e+00,
         3.54059404e-04,   0.00000000e+00,   0.00000000e+00,
         4.17659608e-03,   0.00000000e+00,   0.00000000e+00,
         1.16594089e-03,   0.00000000e+00,   0.00000000e+00,
         5.53713227e-03,   0.00000000e+00,   0.00000000e+00,
         0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
         0.00000000e+00,   4.25061543e-03,   0.00000000e+00,
         0.00000000e+00,   0.00000000e+00,   2.06665633e-03,
         2.65859143e-03,   1.31853058e-03,   6.08496601e-04,
         0.00000000e+00,   1.06139400e-03,   4.08286955e-03,
         3.30939831e-03,   2.52765852e-03,   2.83553329e-03,
         3.42842575e-03,   0.00000000e+00,   1.43292415e-03,
         1.41140283e-03,   1.46918988e-03,   0.00000000e+00,
         4.53191859e-03,   1.17285761e-03,   4.20416283e-04,
         0.00000000e+00,   2.76596771e-03,   2.18793727e-03,
         0.00000000e+00,   2.99561035e-03,   9.63958330e-04,
         1.64320586e-03,   1.38792950e-03,   2.01430215e-04,
         0.00000000e+00,   1.59171385e-03])

【讨论】:

    【解决方案2】:

    如何打开数据集

    import numpy as np
    import h5py
    
    f=h5py.File("yourFilName") #Open the file
    f.keys() #Get the names of the datasets
    # there is one dataset called 'FormedStellarMass'
    dset=f['FormedStellarMass'] #Open this dataset
    shape_of_Array=dset.shape #Gives you the shape of the array
    # (56, 4366546) 
    

    这显然与您的预期不同。这里需要进一步解释。最后一个维度也不是能被 16 整除的整数。我调查了一下数据。

    dset[55,n:n+16] #n=0,16,32.... gives zeros
    

    如果你减少第一个维度的索引,数据看起来会增加

    dset[55,0:16]
    dset[54,0:16] 
    dset[53,0:16] 
    

    在数据集的末尾似乎只有零。

    dset[:,dset.shape[1]-17:dset.shape[1]-1]
    

    请更正您的问题并解释您的数据的真正组织方式。数据集中是否存在不完整的数据块?

    【讨论】:

    猜你喜欢
    • 2012-05-03
    • 2021-08-05
    • 2018-03-25
    • 2016-07-16
    • 2020-08-19
    • 2017-03-15
    • 2015-09-17
    • 2012-01-19
    • 2016-03-29
    相关资源
    最近更新 更多