用語の違いかもしれませんが、hdf5 属性はattrs
Dataset オブジェクトの属性を介してアクセスされます。あなたが持っているものを変数またはデータセットと呼びます。ともかく...
あなたの説明から、属性は単なる配列であると推測しています。次のようにして各属性のデータを取得し、numpy 配列のように最小値と最大値を計算できるはずです。
attr_data = data["U"][:] # gets a copy of the array
min = attr_data.min()
max = attr_data.max()
したがって、各属性の最小値/最大値が必要な場合は、属性名に対して for ループを実行するか、使用できます
for attr_name,attr_value in data.items():
min = attr_value[:].min()
最初のコメントに答えるために編集します。
h5py のオブジェクトは、 python 辞書のように使用できます。したがって、'keys()' を使用すると、実際にはデータを取得するのではなく、そのデータの名前(またはキー) を取得することになります。たとえば、実行the_file.keys()
すると、その hdf5 ファイルのルート パスにあるすべての hdf5 データセットのリストが取得されます。パスに沿って進むと、実際のバイナリ データを保持するデータセットに行き着きます。したがって、たとえば、(最初はインタープリターで) から始めることができます。
the_file = h5py.File("myfile.h5","r")
print the_file.keys()
# this will result in a list of keys maybe ["raw_data","meta_data"] or something
print the_file["raw_data"].keys()
# this will result in another list of keys maybe ["temperature","humidity"]
# eventually you'll get to the dataset that actually has the data or attributes you are looking for
# think of this process as going through a directory structure or a path to get to a file (or a dataset/variable in this case)
the_data_var = the_file["raw_data"]["temperature"]
the_data_array = the_data_var[:]
print the_data_var.attrs.keys()
# this will result in a list of attribute names/keys
an_attr_of_the_data = data_var.attrs["measurement_time"][:]
# So now you have "the_data_array" which is a numpy array and "an_attr_of_the_data" which is whatever it happened to be
# you can get the min/max of the data by doing like before
print the_data_array.min()
print the_data_array.max()
編集 2 - なぜ人々は hdf ファイルをこのようにフォーマットするのですか? それは目的を破ります。
可能であれば、このファイルを作成した人に相談する必要があると思います。あなたがそれを作ったなら、あなたは私の質問に自分で答えることができるでしょう. まず、元の例でdata.keys()
返されたのは確か"U","T",etc.
ですか? h5py が魔法のようなことをしている場合や、h5dump のすべての出力を提供していない場合を除き、それは出力ではありませんでした。h5dump が私に何を伝えているかを説明しますが、端末にコピー アンド ペーストするだけでなく、私が何をしているのかを理解するようにしてください。
# Get a handle to the "data" Group
data = the_file["data"]
# As you can see from the dump this data group has 3 attributes and 1 dataset
# The name of the attributes are "datafield_names","dimensions","time_variables"
# This should result in a list of those names:
print data.attrs.keys()
# The name of the dataset is "Temperature" and should be the only item in the list returned by:
print data.keys()
h5dump からわかるように、62 datafield_names
(文字列)、4 dimensions
(32 ビット整数だと思います)、および 2 time_variables
(64 ビット浮動小数点数) があります。Temperature
また、256 x 512 x 1024 (64 ビット float) の 3 次元配列であることもわかります。私がこの情報をどこで入手しているか分かりますか? ここからが難しい部分です。配列とどのようにdatafield_names
一致するかを判断する必要があります。Temperature
これはファイルを作成した人によって行われたため、配列内の各行/列が何をTemperature
意味するかを理解する必要があります。Temperature
私の最初の推測は、配列内の各行がdatafield_names
、多分毎回2つ以上?しかし、配列内の行が多すぎるため、これは機能しません。たぶん、寸法はそこにどのように収まりますか?最後に、これらの各情報を取得する方法を次に示します (前の続き)。
# Get the temperature array (I can't remember if the 3 sets of colons is required, but try it and if not just use one)
temp_array = data["Temperature"][:,:,:]
# Get all of the datafield_names (list of strings of length 62)
datafields = data.attrs["datafield_names"][:]
# Get all of the dimensions (list of integers of length 4)
dims = data.attrs["dimensions"][:]
# Get all of the time variables (list of floats of length 2)
time_variables = data.attrs["time_variables"]
# If you want the min/max of the entire temperature array this should work:
print temp_array.min()
print temp_array.max()
# If you knew that row 0 of the array had the temperatures you wanted to analyze
# then this would work, but it all depends on how the creator organized the data/file:
print temp_array[0].min()
print temp_array[1].max()
これ以上お役に立てなくて申し訳ありませんが、実際にファイルを持っていて、各フィールドが何を意味するのかを知らなければ、私にできることはこれだけです。h5py を使用して情報を読み取る方法を理解してください。ヘッダー情報 (h5dump 出力) を実際に h5py で使用できる情報にどのように変換したかを理解してください。データが配列内でどのように編成されているかを知っていれば、やりたいことができるはずです。頑張ってください。できることならもっとお手伝いします。