[h5py] 244/455: Update examples
Ghislain Vaillant
ghisvail-guest at moszumanska.debian.org
Thu Jul 2 18:19:37 UTC 2015
This is an automated email from the git hooks/post-receive script.
ghisvail-guest pushed a commit to annotated tag 1.3.0
in repository h5py.
commit fd4e9e77835a6148f9cbb62f4bd6f6309df59c13
Author: andrewcollette <andrew.collette at gmail.com>
Date: Fri May 22 21:14:23 2009 +0000
Update examples
---
examples/groups.py | 76 +++++++++++++++++++++++++++++++++---------------------
examples/simple.py | 65 ++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 112 insertions(+), 29 deletions(-)
diff --git a/examples/groups.py b/examples/groups.py
index 6bf1778..435119d 100644
--- a/examples/groups.py
+++ b/examples/groups.py
@@ -1,50 +1,68 @@
"""
- Example demonstrating some features of groups in HDF5, and how to
- use them from h5py.
+ HDF5 for Python (h5py) is a Python interface to the HDF5 library. Built
+ on a near-complete Python wrapping of the HDF5 C API, it exposes a simple,
+ NumPy-like interface for interacting with HDF5 files, datasets, attributes
+ and groups.
+
+ This module demonstrates the use of HDF5 groups from h5py. HDF5 groups
+ are analagous to directories in a filesystem; they even use the UNIX-style
+ /path/to/resource syntax. In h5py, groups act like dictionaries. They
+ also provide the necessary methods to create subgroups, datasets, and
+ attributes.
+
+ HDF5 for Python is available at h5py.alfven.org.
"""
+import numpy as np
import h5py
-groups = ['/foo', '/foo/bar', '/foo/bar/baz',
- '/grp1', '/grp1/grp2', '/mygroup']
+f = h5py.File('myfile.hdf5','w')
-f = h5py.File('group_test.hdf5','w')
+# The file object is also the "root group" ("/") in HDF5. It's currently
+# empty:
+print "Number of items in the root group: %d" % len(f)
-for grpname in groups:
- f.create_group(grpname)
+# Create some groups
+g1 = f.create_group('Group1')
+g2 = f.create_group('Another Group')
+g3 = f.create_group('Yet another group')
-print "Root group names:"
+# All groups, including the root group, support a basic dictionary-style
+# interface
+print "There are now %d items in the root group" % len(f)
+print "They are: %s" % ", ".join(f) # iterating yields member names
-for name in f:
- print " ", name
+# Groups can contain subgroups themselves
+sub1 = g1.create_group("Subgroup1")
-print "Root group info:"
+# Prints "/Group1/Subgroup1"
+print "Full name of subgroup is %s" % sub1.name
-for name, grp in f.iteritems():
- print " %s: %s items" % (name, len(grp))
+# You can retrieve them using __getitem__ syntax
+sub2 = g1['Subgroup1']
-if h5py.version.api_version_tuple >= (1,8):
+# You can attach attributes to groups, just like datasets, containing just
+# about anything NumPy can handle.
+g1.attrs['purpose'] = "A demonstration group"
+g1.attrs['Life, universe, everything'] = 42
+g1.attrs['A numpy array'] = np.ones((3,), dtype='>i2')
- def treewalker(name):
- """ Callback function for visit() """
- print " Called for %s" % name
+# Create datasets using group methods. (See other examples for a more in-
+# depth introduction to datasets).
- print "Walking..."
- f.visit(treewalker)
+data = np.arange(100*100).reshape((100,100))
+
+dset = sub1.create_dataset("My dataset", data=data)
+
+print "The new dataset has full name %s, shape %s and type %s" % \
+ (dset.name, dset.shape, dset.dtype)
+
+# Closing the file closes all open objects
+f.close()
- print "Copying /foo to /mygroup/newfoo..."
- f.copy("/foo", "/mygroup/newfoo")
- print "Walking again..."
- f.visit(treewalker)
- g = f['/grp1']
- print "Walking from /grp1..."
- g.visit(treewalker)
-else:
- print "HDF5 1.8 is needed for extra demos"
-f.close()
diff --git a/examples/simple.py b/examples/simple.py
new file mode 100644
index 0000000..d43c8a5
--- /dev/null
+++ b/examples/simple.py
@@ -0,0 +1,65 @@
+
+"""
+ HDF5 for Python (h5py) is a Python interface to the HDF5 library. Built
+ on a near-complete Python wrapping of the HDF5 C API, it exposes a simple,
+ NumPy-like interface for interacting with HDF5 files, datasets, attributes
+ and groups.
+
+ This is a simple module which demonstrates some of the features of HDF5,
+ including the ability to interact with large on-disk datasets in a
+ NumPy-like fashion.
+
+ In this example, we create a file containing a 1 GB dataset, populate it
+ from NumPy, and then slice into it. HDF5 attributes are also demonstrated.
+
+ HDF5 for Python is available at h5py.alfven.org.
+"""
+
+import numpy as np
+import h5py
+
+f = h5py.File('myfile.hdf5','w')
+
+# Create a new, empty dataset to hold 1GB of floats
+dset = f.create_dataset('MyDataset', (256, 1024, 1024), dtype='f')
+
+# Datasets have some of the same properties as NumPy arrays
+print "The new dataset has shape %s and type %s" % (dset.shape, dset.dtype)
+
+# Attach some attributes
+dset.attrs['purpose'] = "Demonstration dataset for floats"
+dset.attrs['original size'] = (256, 1024, 1024) # This tuple is auto-
+ # converted to an HDF5 array.
+dset.attrs['constant'] = 42
+
+# Populate the file in a loop. Note that you can use NumPy-style slicing
+# on datasets directly, including the row-like selection demonstrated here.
+
+base = np.arange(1024*1024, dtype='f').reshape((1024,1024))
+for idx in xrange(256):
+ if(idx%16==0): print 'Populating row %d' % idx
+
+ base += idx*(1024*1024)
+ dset[idx] = base
+
+
+# Perform some operations requiring random access. Note these operations use
+# HDF5 "dataspaces" for efficient read/write.
+
+print "Resetting some indices to one"
+dset[15, 24, 100:200] = np.ones((100,), dtype='f')
+
+print 'Retrieving every 64th element... '
+subarray = dset[...,::64]
+print 'Retrived array has shape %s' % (subarray.shape,)
+
+# We can also access attributes using dictionary-style syntax
+for name, value in dset.attrs.iteritems():
+ print 'Attribute "%s" has value: %r' % (name, value)
+
+# When finished, close the file. The dataset (and all other open objects)
+# are closed automatically.
+f.close()
+
+
+
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/h5py.git
More information about the debian-science-commits
mailing list