From 85cc9e6afb120b481a13d612430f0a7a05a5d83a Mon Sep 17 00:00:00 2001 From: Ivan Vilata-i-Balaguer Date: Tue, 5 Dec 2023 12:28:14 +0100 Subject: [PATCH] Readme, example: How to enable/disable optimization via API funcs. --- README.rst | 2 +- examples/blosc2_optimized_slicing.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 8ec0a65..9d55c43 100644 --- a/README.rst +++ b/README.rst @@ -23,7 +23,7 @@ After that, optimization will be attempted for any slicing of a dataset (of the .. _hdf5plugin: https://github.com/silx-kit/hdf5plugin -Even if the module is imported and the ``Dataset`` class is patched, you may still force-disable the optimization by setting ``BLOSC2_FILTER=1`` in the environment. +You may globally disable the optimization after importing ``b2h5py`` by calling ``b2h5py.unpatch_dataset_class()``, and enable it again with ``b2h5py.patch_dataset_class()``. Even if the module is imported and the ``Dataset`` class is patched, you may still force-disable the optimization by setting ``BLOSC2_FILTER=1`` in the environment. Building -------- diff --git a/examples/blosc2_optimized_slicing.py b/examples/blosc2_optimized_slicing.py index db803fb..903ff83 100644 --- a/examples/blosc2_optimized_slicing.py +++ b/examples/blosc2_optimized_slicing.py @@ -95,3 +95,14 @@ print("Slice from dataset:", slice_, sep='\n') print("Slice from input array:", data[150:, 150:], sep='\n') os.environ['BLOSC2_FILTER'] = '0' # back to normal +# Utility functions are also provided to enable and disable optimization +# (`BLOSC2_FILTER` still takes priority, though). +print("Disabling Blosc2 optimized slicing via API.") +with h5py.File(file_name, 'r') as f: + import b2h5py + b2h5py.unpatch_dataset_class() + dataset = f[dataset_name] + slice_ = dataset[150:, 150:] + print("Slice from dataset:", slice_, sep='\n') + print("Slice from input array:", data[150:, 150:], sep='\n') + b2h5py.patch_dataset_class() # back to normal