当前位置:网站首页>H5py Quick Start Guide
H5py Quick Start Guide
2022-07-24 13:15:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
h5py yes Python Language is used to operate HDF5 Module . The following article mainly introduces h5py Quick start guide for , Translated from h5py Official documents of :http://docs.h5py.org/en/latest/quick.html . This translation is for personal learning only h5py For the purpose , If there is any improper translation , Please contact the author or provide the correct translation , Thank you very much !
install
Use Anaconda perhaps Miniconda:
conda install h5pyuse Enthought Canopy, have access to GUI Install the package or use
enpkg h5pyinstall . use pip or setup.py install , Please refer to Installation mode .
The core concept
One HDF5 A file is a container , Used to store two types of objects :datasets, A data set similar to an array ;groups, Folder like container , Can store datasets And other groups. When using h5py when , The most basic rule is :
groups It's like a dictionary (dictionaries),dataset Be similar to Numpy In the array (arrays).
Suppose someone sends you a HDF5 file , mytestfile.hdf5( How to create this file , Please refer to : appendix : Create a file ). The first thing you need to do is open this file to read data :
>>> import h5py
>>> f = h5py.File('mytestfile.hdf5', 'r')This File Object is your starting point . So what is stored in this file ? remember ,h5py.File Like a Python Dictionaries , So we can check these key values ,
>>> list(f.keys())
['mydataset']According to our observations , There is a... In this file dataset, namely mydataset. Let's take this dataset As Dataset Object to verify
>>> dset = f['mydataset']The object we get is not an array , It is a HDF5 dataset. It's like Numpy The data in ,datasets There is shape (shape) And data types (data type)
>>> dset.shape
(100,)
>>> dset.dtype
dtype('int32')They also support array style slicing . Here is how you complete one of the files dataset Reading and writing methods
>>> dset[...] = np.arange(100)
>>> dset[0]
0
>>> dset[10]
10
>>> dset[0:100:10]
array([ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90])Want more references , Please go to File Objects and Datasets.
appendix : Create a file
At the moment , You may be curious mytestdata.hdf5 How to create . When File After object initialization , We use patterns (mode) Set to w To create a file . Other modes (mode) by a( Used to read 、 Write 、 newly build ) and r+( Used to read 、 Write ). A complete File A list of patterns and their meanings can be found File object .
>>> import h5py
>>> import numpy as np
>>> f = h5py.File("mytestfile.hdf5", "w")File object There are several ways that look interesting . One is create_dataset, seeing the name of a thing one thinks of its function , It is to create a given shape and data type dataset
>>> dset = f.create_dataset("mydataset", (100,), dtype='i')File The object is the context manager , therefore , The following code can also be run
>>> import h5py
>>> import numpy as np
>>> with h5py.File("mytestfile.hdf5", "w") as f:
>>> dset = f.create_dataset("mydataset", (100,), dtype='i')Groups And hierarchical structure
“HDF” yes “Hierarchical Data Format” Abbreviation . Every HDF5 All objects in the file have a name (name), They are similar to POSIX Style layered storage , use / The separator separates
>>> dset.name
u'/mydataset'In this system “ Folder ”(folders) Was named groups. We created File The object itself is also a group, In this case, it is root group(root group), The name is /:
>>> f.name
u'/'Create a child group(subgroup) You can use a clever command create_group To complete . however , We first need to read / Open file in write mode
>>> f = h5py.File('mydataset.hdf5', 'r+')
>>> grp = f.create_group("subgroup")all Group object , Like File object , Also have create_* Method :
>>> dset2 = grp.create_dataset("another_dataset", (50,), dtype='f')
>>> dset2.name
u'/subgroup/another_dataset'By the way , You don't need to manually create all the intermediate groups. It is also possible to specify a complete path
>>> dset3 = f.create_dataset('subgroup2/dataset_three', (10,), dtype='i')
>>> dset3.name
u'/subgroup2/dataset_three'Groups Support most of Python Dictionary style interface . You can use entries to get (item-retrieval) To get the objects in this file :
>>> dataset_three = f['subgroup2/dataset_three']Iterate one group, Will produce the names of its members :
>>> for name in f:
... print name
mydataset
subgroup
subgroup2Membership detection can also be achieved by using names :
>>> "mydataset" in f
True
>>> "somethingelse" in f
FalseYou can even use the full path name :
>>> "subgroup/another_dataset" in f
TrueIt also has something you are familiar with keys(), values(), items() and iter() Methods , as well as get() Method .
Because iteration one group Only its immediate members will be generated , So I want to iterate over a complete file , have access to Group Methods visit() and visititems(), They pass through a call (callable) To achieve :
>>> def printname(name):
... print name
>>> f.visit(printname)
mydataset
subgroup
subgroup/another_dataset
subgroup2
subgroup2/dataset_threeWant more references , Please go to Groups.
attribute
HDF5 One of the best features of is that you can store metadata after the data described (metadata). be-all groups and datasets All support the subsidiary naming of several data bits , Called attribute .(All groups and datasets support attached named bits of data called attributes.)
Properties can be accessed through attrs This proxy object to get , This will execute the dictionary interface again :
>>> dset.attrs['temperature'] = 99.5
>>> dset.attrs['temperature']
99.5
>>> 'temperature' in dset.attrs
TrueWant more references , Please go to Attributes.
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/124968.html Link to the original text :https://javaforall.cn
边栏推荐
- Static attribute, super()
- 2022.07.15 summer training personal qualifying (10)
- Integer inversion of force deduction questions
- 3. Realize snake and basic game interface
- Windivert: capture and modify packages
- [C language] dynamic memory management
- The use of two-dimensional array (including the definition of two-dimensional array, the declaration and initialization of two-dimensional array (dynamic initialization, static initialization), common
- 20201127 use markdown to draw UML diagrams, graphviz installation experience hematemesis finishing
- Representation and basic application of regular expressions
- Handler learning
猜你喜欢

Nearly 65billion pieces of personal information were illegally handled in seven years, and the investigation of didi network security review case was announced
![[datasheet] interpretation of cs5480 data book of metering chip](/img/1a/e8a4ce5c393a6634b6dc8bf6d687e2.png)
[datasheet] interpretation of cs5480 data book of metering chip

SSM在线校园相册管理平台

Search engine based on boost library

Step of product switching to domestic chips, stm32f4 switching to gd32

ESP32ADC

开山之作造假!Science大曝Nature重磅论文学术不端,恐误导全球16年

Custom scroll bar

Constraintlayout learn from 0 to 0.n

Modern data architecture selection: Data fabric, data mesh
随机推荐
cookie
基于ABP实现DDD--实体创建和更新
Sorting method: bubble sorting (use an array to arrange a string of numbers in order (from large to small or from small to large))
30. Rearrange the linked list
如何用WebGPU流畅渲染百万级2D物体?
Make a fake! Science has exposed the academic misconduct of nature's heavy papers, which may mislead the world for 16 years
About thread (5) thread pool
2022.07.15 summer training personal qualifying (10)
SSM hospital inpatient management system
Atcoder beginer contest 261e / / bitwise thinking + DP
Writing browser plug-ins
基于matlab的声音识别
27. Longest increasing subsequence
Summary of embedded network problems (packet loss of network card, unrecognized network card)
The second batch of projects of Shenzhen Metro Line 12 passed the acceptance and is expected to be put into trial operation on July 28
English语法_不定代词 - 概述
Question 10: find numbers in an array with rows and columns in order
【论文阅读】TEMPORAL ENSEMBLING FOR SEMI-SUPERVISED LEARNING
Solution to embedded SD card /u disk read-only problem (fat read-only repair method)
Teach you how to use power Bi to realize four kinds of visual charts