Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Python examples #4546

Merged
merged 2 commits into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions HDF5Examples/PYTHON/h5_compound.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#
# This example creates an HDF5 file compound.h5 and an empty datasets /DSC in it.
#
import h5py
import numpy as np
#
# Create a new file using default properties.
#
file = h5py.File('compound.h5','w')
#
# Create a dataset under the Root group.
#
comp_type = np.dtype([('Orbit', 'i'), ('Location', np.str_, 6), ('Temperature (F)', 'f8'), ('Pressure (inHg)', 'f8')])
dataset = file.create_dataset("DSC",(4,), comp_type)
data = np.array([(1153, "Sun ", 53.23, 24.57), (1184, "Moon ", 55.12, 22.95), (1027, "Venus ", 103.55, 31.23), (1313, "Mars ", 1252.89, 84.11)], dtype = comp_type)
dataset[...] = data
#
# Close the file before exiting
#
file.close()
file = h5py.File('compound.h5', 'r')
dataset = file["DSC"]
print("Reading Orbit and Location fields...")
orbit = dataset['Orbit']
print("Orbit: ", orbit)
location = dataset['Location']
print("Location: ", location)
data = dataset[...]
print("Reading all records:")
print(data)
print("Second element of the third record:", dataset[2, 'Location'])
file.close()

22 changes: 22 additions & 0 deletions HDF5Examples/PYTHON/h5_crtdat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#
# This examaple creates an HDF5 file dset.h5 and an empty datasets /dset in it.
#
import h5py
#
# Create a new file using defaut properties.

Check failure on line 6 in HDF5Examples/PYTHON/h5_crtdat.py

View workflow job for this annotation

GitHub Actions / Check for spelling errors

defaut ==> default
#
file = h5py.File('dset.h5','w')
#
# Create a dataset under the Root group.
#
dataset = file.create_dataset("dset",(4, 6), h5py.h5t.STD_I32BE)
print("Dataset dataspace is", dataset.shape)
print("Dataset Numpy datatype is", dataset.dtype)
print("Dataset name is", dataset.name)
print("Dataset is a member of the group", dataset.parent)
print("Dataset was created in the file", dataset.file)
#
# Close the file before exiting
#
file.close()

37 changes: 37 additions & 0 deletions HDF5Examples/PYTHON/h5_gzip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#
# This example creates and writes GZIP compressed dataset.
#
import h5py
import numpy as np
#
# Create gzip.h5 file.
#
file = h5py.File('gzip.h5','w')
#
# Create /DS1 dataset; in order to use compression, dataset has to be chunked.
#
dataset = file.create_dataset('DS1',(32,64),'i',chunks=(4,8),compression='gzip',compression_opts=9)
#
# Initialize data.
#
data = np.zeros((32,64))
for i in range(32):
for j in range(64):
data[i][j]= i*j-j
#
# Write data.
#
print("Writing data...")
dataset[...] = data
file.close()
#
# Read data back; display compression properties and dataset max value.
#
file = h5py.File('gzip.h5','r')
dataset = file['DS1']
print("Compression method is", dataset.compression)
print("Compression parameter is", dataset.compression_opts)
data = dataset[...]
print("Maximum value in", dataset.name, "is:", max(data.ravel()))
file.close()

54 changes: 54 additions & 0 deletions HDF5Examples/PYTHON/h5_hype.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#
# This example shows how to write a hyperslab to an existing dataset.
#
import h5py
import numpy as np
#
# Create a file using default properties.
#
file = h5py.File('hype.h5','w')
#
# Create "IntArray" dataset.
#
dim0 = 8
dim1 = 10
dataset = file.create_dataset("IntArray", (dim0,dim1), "i")
#
# Initialize data object with 0.
#
data = np.zeros((dim0, dim1))
#
# Initialize data for writing.
#
for i in range(dim0):
for j in range(dim1):
if j < dim1/2:
data[i][j]= 1
else:
data[i][j] = 2
#
# Write data
#
dataset[...] = data
print("Data written to file:")
print(dataset[...])
#
# Close the file before exiting
#
file.close()
#
# Open the file and dataset.
#
file = h5py.File('hype.h5','r+')
dataset = file['IntArray']
#
# Write a selection.
#
dataset[1:4, 2:6] = 5
print("Data after selection is written:")
print(dataset[...])
#
# Close the file before exiting
#
file.close()

31 changes: 31 additions & 0 deletions HDF5Examples/PYTHON/h5_hypeb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#
# This example shows how to read a hyperslab from an existing dataset.
#
import h5py
import numpy as np
#
# Open file and read dataset.
#
file = h5py.File('hype.h5', 'r')
dataset = file['IntArray']
data_in_file = dataset[...]
print("Data in file ...")
print(data_in_file[...])
#
# Initialize data with 0s.
#
data_selected = np.zeros((8,10), dtype=np.int32)
#
# Read selection.
#
space_id = dataset.id.get_space()
space_id.select_hyperslab((1,1), (2,2), stride=(4,4), block=(2,2))
#---> Doesn't work dataset.id.read(space_id, space_id, data_selected, h5py.h5t.STD_I32LE)
dataset.id.read(space_id, space_id, data_selected)
print("Seleted data read from file....")

Check failure on line 25 in HDF5Examples/PYTHON/h5_hypeb.py

View workflow job for this annotation

GitHub Actions / Check for spelling errors

Seleted ==> Selected, Deleted
print(data_selected[...])
#
# Close the file before exiting
#
file.close()

71 changes: 71 additions & 0 deletions HDF5Examples/PYTHON/h5_links.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#
# This example demonstrates the concepts of hard, soft and external links.
#
# We will create file links.h5 with the following members and then will try to access objects
# using hard, soft and external links.
# / Group
# /A Group
# /A/a Dataset {10}
# /B Group
# /B/External External Link {dset.h5//dset}
# /a Dataset, same as /A/a
# /dangling Soft Link {/B/XXX}
# /soft Soft Link {/A/a}

import h5py
import numpy as np
file = h5py.File('links.h5', 'w')
#
# Create a group structure in the file
#
A = file.create_group("A")
B = file.create_group("B")
a = A.create_dataset("a", (10,), 'i')
#
# Create a hard link in a root group pointing to dataset /A/a
#
file["a"] = a
#
# Create a soft link (alias) in a root group with a value /A/a
#
file["soft"] = h5py.SoftLink('/A/a')
#
# Create a soft link (alias) in a root group with a value /B/XXX that cannot be resolved
#
file["dangling"] = h5py.SoftLink('/B/XXX')
#
# Create an external link to a dataset "dset" in file dset.h5
#
B['External'] = h5py.ExternalLink("dset.h5", "/dset")
#
# List objects in the root group in the file
#
print("Root group members in links.h5:")
try:
print("Trying to get the items...")
print(list(file.items()))
except:
print("...but can only get the keys...")
print(list(file.keys()))
print(" ")
print("Why? Because the library cannot resolve the dangling link.")
print("We will delete the 'dangling' link and try again.")
del file["dangling"]
print(list(file.items()))
print(" ")
print("Group A members:")
print(list(A.items()))
print(" ")
print("Group B members:")
print(list(B.items()))
print(" ")
print("Reading dataset pointed by the external link...")
dset = B['External']
data = np.zeros((4,6))
data = dset[...]
print(data)
#
# Copy link to /A/a to /B/b
#
B["b"]=A["a"]
file.close()
37 changes: 37 additions & 0 deletions HDF5Examples/PYTHON/h5_objref.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#
# This example shows how to create dataset with object references
#
import h5py
import numpy as np
#
# Create a new file using default properties.
#
file = h5py.File('objref.h5','w')
#
# Create a group and scalar datasets under the Root group.
#
group = file.create_group("G1")
dataset = file.create_dataset("DS2",(), 'i')
#
# Create references to the group and the dataset and store them in another dataset.
#
refs = (group.ref, dataset.ref)
ref_type = h5py.h5t.special_dtype(ref=h5py.Reference)
dataset_ref = file.create_dataset("DS1", (2,),ref_type)
dataset_ref[...] = refs

#
# Close the file before exiting
#
file.close()

file = h5py.File('objref.h5','r')
dataset_ref = file["DS1"]
refs = dataset_ref[...]
refs_list = list(refs)
for obj in refs_list:
index = refs_list.index(obj)
print("DS["+str(index)+"]:")
print(file[obj])
file.close()

31 changes: 31 additions & 0 deletions HDF5Examples/PYTHON/h5_readtofloat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#
# This example reads integer data from dset.h5 file into Python floatng buffers.
#
import h5py
import numpy as np
#
# Open an existing file using default properties.
#
file = h5py.File('dset.h5','r+')
#
# Open "dset" dataset under the root group.
#
dataset = file['/dset']
#
# Initialize buffers,read and print data.
#
# Python float type is 64-bit, one needs to use NATIVE_DOUBLE HDF5 type to read data.
data_read64 = np.zeros((4,6,), dtype=float)
dataset.id.read(h5py.h5s.ALL, h5py.h5s.ALL, data_read64, mtype=h5py.h5t.NATIVE_DOUBLE)
print("Printing data 64-bit floating numbers...")
print(data_read64)

data_read32 = np.zeros((4,6,), dtype=np.float32)
dataset.id.read(h5py.h5s.ALL, h5py.h5s.ALL, data_read32, mtype=h5py.h5t.NATIVE_FLOAT)
print("Printing data 32-bit floating numbers...")
print(data_read32)
#
# Close the file before exiting
#
file.close()

52 changes: 52 additions & 0 deletions HDF5Examples/PYTHON/h5_regref.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#
# This example shows how to create a dataset with region references.
#
import h5py
import numpy as np
#
# Create a new file using default properties.
#
file = h5py.File('regref.h5','w')
#
# Create a group and (3x2) dataset under the Root group.
#
dataset = file.create_dataset("DS2",(3,2), h5py.h5t.STD_I8LE)
dataset[...] = np.array([[1,1], [2,2], [3,3]])
#
# Create references to each row in the dataset.
#
refs = (dataset.regionref[0,:],dataset.regionref[1,:],dataset.regionref[2,:])
#
# Create a dataset to store region references.
#
ref_type = h5py.h5t.special_dtype(ref=h5py.RegionReference)
dataset_ref = file.create_dataset("DS1", (3,),ref_type)
dataset_ref[...] = refs
#
# Close the file before exiting.
#
file.close()
#
# Open the file, read the second element of the dataset with the region references
# and dereference it to get data.
#
file = h5py.File('regref.h5', 'r')
dataset = file["DS1"]
regref = dataset[1]
#
# Region reference can be used to find a dataset it points to.
#
dataset_name = file[regref].name
print(dataset_name)
#
# Get hyperslab the reference points to.
#
data = file[dataset_name]
#
# Region reference can be used as a slicing argument!
print(data[regref])
file.close()




Loading
Loading