Read HDF5 data with numpy axis order with Julia HDF5

时间:2015-07-13 20:59:55

标签: julia hdf5

I have an HDF5 file containing arrays that are saved with Python/numpy. When I read them into Julia using HDF5.jl, the axes are in the reverse of the order in which they appear in Python. To reduce the mental gymnastics involved in moving between the Python and Julia codebases, I reverse the axis order when I read the data into Julia. I have written my own function to do this:

function reversedims(ary::Array)
  permutedims(ary, [ ndims(ary):-1:1 ])
end

data = HDF5.read(someh5file, somekey) |> reversedims

This is not ideal because (1) I always have to import reversedims to use this; (2) I have to remember to do this for each Array I read. I am wondering if it is possible to either:

  • instruct HDF5.jl to read in the arrays with a numpy-style axis order, either through a keyword argument or some kind of global configuration parameter
  • use a builtin single argument function to reverse the axes

2 个答案:

答案 0 :(得分:2)

最好的方法是创建一个H5py.jl包,模仿MAT.jl(读取和写入由Matlab创建的.mat文件)。另请参阅https://github.com/timholy/HDF5.jl/issues/180

答案 1 :(得分:0)

在我看来permutedims!做了你正在寻找的东西,但是它确实做了数组拷贝。如果你可以在python中重写hdf5文件,numpy.asfortranarray声称要返回以列主格式存储的数据,尽管numpy internals docs似乎表明数据没有被改变,只是步幅是的,所以我不知道hdf5文件输出是否会有任何不同

修改抱歉,我刚看到您已在功能中使用permutedims。我无法在Julia方面找到任何其他内容,但我仍会尝试numpy.asfortranarray,看看是否有帮助。