如何用python pandas打开nxs文件?

0 投票
1 回答
1189 浏览
提问于 2025-04-18 06:00

我有一个Nexus文件(foo.nxs),里面是测量得到的数据,我想用pandas来打开它。但是,当我尝试用通常的方法时:

HDFStore('foo.nxs') or read_hdf('foo.nxs','/group')

结果却是一个空的存储:

<class 'pandas.io.pytables.HDFStore'>
File path: /foo.nxs
Empty

或者出现了类型错误:

TypeError: cannot create a storer if the object is not existing nor a value are passed

文档里的所有例子都是先创建一个hdf文件,把数据存进去,然后再取出来,但这些都是在同一个pandas里完成的。我想知道是否可以读取一个不是用pandas生成的hdf文件。

这是根据@Jeff的要求,ptdump输出的一部分:

/ (RootGroup) ''
  /._v_attrs (AttributeSet), 4 attributes:
    [HDF5_Version := '1.8.11',
    NeXus_version := '4.3.0',
    file_name := '/Messdaten/C9/20140423/messung_21h14m01.197.nxs',
    file_time := '2014-04-23T21:14:01+01:00']
/exp_root.Kerr.DoublePulse2D.SuperDuperScan_V2_00001 (Group) ''
  /exp_root.Kerr.DoublePulse2D.SuperDuperScan_V2_00001._v_attrs (AttributeSet), 1 attributes:
   [NX_class := 'NXentry']
/exp_root.Kerr.DoublePulse2D.SuperDuperScan_V2_00001/scan_data (Group) ''
  /exp_root.Kerr.DoublePulse2D.SuperDuperScan_V2_00001/scan_data._v_attrs (AttributeSet), 1 attributes:
   [NX_class := 'NXdata']
/exp_root.Kerr.DoublePulse2D.SuperDuperScan_V2_00001/scan_data/actuator_1_1 (CArray(5, 121), zlib(6)) ''
  atom := Float64Atom(shape=(), dflt=0.0)
  maindim := 0
  flavor := 'numpy'
  byteorder := 'little'
  chunkshape := (5, 121)

1 个回答

0

你有一个普通的PyTables存储。这种存储其实很容易读取(而且不需要用到pandas),下面会教你怎么把它们读出来(以numpy数组的形式)。

with pd.get_store('test.h5') as store:
     data = store.root.exp_root.Kerr.DoublePulse2D.SuperDuperScan_V2_00001/scan_data/actuator_1_1.read()

相关文档可以在这里找到。

这段代码只是获取了PyTables的根节点,并读取里面的数据。这些数据是以CArray格式存储的,属于简单的数组。

HDFStore能够读取普通的PyTables Table对象(没有元数据),但读取数组的过程也很简单。

撰写回答