Skip to content

Commit

Permalink
Example: Use custom function for more compact print calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivilata committed Dec 11, 2023
1 parent f8d43ed commit 99760f3
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions examples/blosc2_optimized_slicing.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
file_name = 'b2nd-example.h5'
dataset_name = 'data'


def printl(*args, **kwargs):
print(*args, **kwargs, sep='\n')

# Creating a Blosc2-compressed dataset
# ------------------------------------
with h5py.File(file_name, 'w') as f:
Expand All @@ -71,15 +75,13 @@
# One just uses slicing as usual.
dataset = f[dataset_name]
# Slices with step == 1 may be optimized.
print("Contiguous slice from dataset (optimized):", dataset[150:, 150:],
sep='\n')
print("Contiguous slice from input array:", data[150:, 150:], sep='\n')
printl("Contiguous slice from dataset (optimized):", dataset[150:, 150:])
printl("Contiguous slice from input array:", data[150:, 150:])
# Slices with step != 1 (or with datasets of a foreign endianness)
# are not optimized, but still work
# (via the HDF5 filter pipeline and hdf5plugin).
print("Sparse slice from dataset (filter):", dataset[150::2, 150::2],
sep='\n')
print("Sparse slice from input array:", data[150::2, 150::2], sep='\n')
printl("Sparse slice from dataset (filter):", dataset[150::2, 150::2])
printl("Sparse slice from input array:", data[150::2, 150::2])
print()

# Disabling Blosc2 optimized slicing
Expand All @@ -92,8 +94,8 @@
b2h5py.unpatch_dataset_class()
assert(not b2h5py.is_dataset_class_patched())
dataset = f[dataset_name]
print("Slice from dataset (filter):", dataset[150:, 150:], sep='\n')
print("Slice from input array:", data[150:, 150:], sep='\n')
printl("Slice from dataset (filter):", dataset[150:, 150:])
printl("Slice from input array:", data[150:, 150:])
b2h5py.patch_dataset_class() # back to normal
assert(b2h5py.is_dataset_class_patched())
print()
Expand All @@ -108,11 +110,10 @@
b2h5py.unpatch_dataset_class()
assert(not b2h5py.is_dataset_class_patched())
dataset = f[dataset_name]
print("Slice from dataset (filter):", dataset[150:, 150:], sep='\n')
printl("Slice from dataset (filter):", dataset[150:, 150:])
with b2h5py.patching_dataset_class():
assert(b2h5py.is_dataset_class_patched())
print("Slice from dataset (optimized):", dataset[150:, 150:],
sep='\n')
printl("Slice from dataset (optimized):", dataset[150:, 150:])
assert(not b2h5py.is_dataset_class_patched())
print("Slice from input array:", data[150:, 150:], sep='\n')
printl("Slice from input array:", data[150:, 150:])
print()

0 comments on commit 99760f3

Please sign in to comment.