h5py:如何重命名尺寸? [英] h5py : how to rename dimensions?
问题描述
我创建了一个句柄为fw的新文件。
I created a new file whose handle is fw.
fw.create_dataset('grp1/varname',data=arr)
在此命令之前创建了组。 arr是一个尺寸为(2,3)的numpy数组。文件创建成功。但是,这些维度分别命名为phony_0和phony_1。
The groups are created before this command. arr is a numpy array with dimensions (2,3). The file is created successfully. However, the dimensions are named phony_0, and phony_1. How do I change them to say m and n ?
一般来说,如何在组内创建维,然后将变量与它们关联?
In general how does one create dimensions within a group and then associate variables with them?
我尝试过,
fw['grp1/varname'].dims[0].label = 'm'
但这并没有达到预期的效果。
But this does not have the desired effect.
ncdump -h
在创建的文件上显示:
ncdump -h
on the created file shows :
group: grp1 {
dimensions:
phony_dim_0 = 2 ;
phony_dim_1 = 3 ;
variables:
float varname(phony_dim_0, phony_dim_1) ;
string varname:DIMENSION_LABELS = "m", NIL, NIL ;
} // group grp1
谢谢
print([fw ['grp1 / varname']。dims]中dim的dim.label
确实产生一致的输出。
[u’m,u’]
print([ dim.label for dim in fw['grp1/varname'].dims]) does produce consistent output. [u'm', u'']
似乎hdffile没有提供将维与组关联的规定。但是,varname是变量。如何获得:
It does seem that hdffiles do not have provision to associate dimensions with groups. However varname is a variable. How does one get :
variables:
float varname(m, phony_dim_1) ;
string varname:DIMENSION_LABELS = "m", NIL ;
} // group grp1
吗?我确实使用h5dump尝试了其他选项。
in the output of ncdump -h or h5dump ? I did try different options with h5dump.
谢谢。
推荐答案
部分问题可能是您使用 ncdump
。
Part of the problem may your use of ncdump
.
我可以制作一个简单的文件,并设置<$数据集的c $ c> dims 标签:
I can make a simple file, and set the dims
label for a dataset:
In [420]: import h5py
In [421]: f = h5py.File('testdim.h5','w')
In [422]: ds = f.create_dataset('grp1/varname', data = np.arange(10))
In [423]: ds
Out[423]: <HDF5 dataset "varname": shape (10,), type "<i8">
查看 dims
属性:
In [424]: ds.dims
Out[424]: <Dimensions of HDF5 object at 140382697336904>
In [426]: ds.dims[0]
Out[426]: <"" dimension 0 of HDF5 dataset at 140382697336904>
In [427]: ds.dims[0].label
Out[427]: ''
In [428]: ds.dims[0].label = 'm'
In [436]: dd=ds.dims[0]
In [437]: dd?
Type: DimensionProxy
String form: <"m" dimension 0 of HDF5 dataset at 140382697336904>
Length: 0
File: ~/.local/lib/python3.6/site-packages/h5py/_hl/dims.py
Docstring: Represents an HDF5 "dimension".
In [439]: dd.values()
Out[439]: []
In [440]: dd.label
Out[440]: 'm'
该组没有点心
:
In [442]: g = f['grp1']
In [443]: g
Out[443]: <HDF5 group "/grp1" (1 members)>
In [444]: g.dims
AttributeError: 'Group' object has no attribute 'dims'
In [446]: f.flush()
具有 h5dump
:
1902:~/mypy$ h5dump testdim.h5
HDF5 "testdim.h5" {
GROUP "/" {
GROUP "grp1" {
DATASET "varname" {
DATATYPE H5T_STD_I64LE
DATASPACE SIMPLE { ( 10 ) / ( 10 ) }
DATA {
(0): 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
}
ATTRIBUTE "DIMENSION_LABELS" {
DATATYPE H5T_STRING {
STRSIZE H5T_VARIABLE;
STRPAD H5T_STR_NULLTERM;
CSET H5T_CSET_ASCII;
CTYPE H5T_C_S1;
}
DATASPACE SIMPLE { ( 1 ) / ( 1 ) }
DATA {
(0): "m"
}
}
}
}
}
}
其中 ncdump
旨在显示 netcdf
文件:
1902:~/mypy$ ncdump -h testdim.h5
netcdf testdim {
group: grp1 {
dimensions:
phony_dim_0 = 10 ;
variables:
int64 varname(phony_dim_0) ;
string varname:DIMENSION_LABELS = "m" ;
} // group grp1
}
c $ c> h5df 格式没有组尺寸; ncdump
为此创建了一个虚拟属性。
As best I can the h5df
format does not have group dimensions; ncdump
creates a dummy attribute for that.
要重申您先前问题的答案,请参阅HDF5尺寸文档是:
To reiterate the answer to your previous question, the documentation of HDF5 dimensions is:
http ://docs.h5py.org/en/latest/high/dims.html
https://www.unidata.ucar.edu/software/netcdf/docs/interoperability_hdf5.html
对于HDF5文件
如果未使用尺寸比例,则netCDF-4仍然可以编辑
If dimension scales are not used, then netCDF-4 can still edit the file, and will invent anonymous dimensions for each variable shape.
NETCDF具有共享尺寸,HDF5具有尺寸比例。它们并不完全相同。
NETCDF has shared dimensions, HDF5 has dimension scales. They aren't quite the same.
http://www.stcorp.nl/beat/documentation/harp/conventions/hdf5.html
在HDF5数据模型中,没有共享尺寸的概念(与netCDF不同)。 HDF5数据集的形状被指定为尺寸长度的列表。但是,netCDF-4库使用HDF5作为其存储后端。它使用HDF5尺寸比例尺表示共享尺寸。
In the HDF5 data model there is no concept of shared dimensions (unlike netCDF). The shape of an HDF5 dataset is specified as a list of dimension lengths. However, the netCDF-4 library uses HDF5 as its storage backend. It represents shared dimensions using HDF5 dimension scales.
这篇关于h5py:如何重命名尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!