|
| 1 | +####################################################################### |
| 2 | +# Copyright (c) 2019-present, Blosc Development Team <[email protected]> |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# This source code is licensed under a BSD-style license (found in the |
| 6 | +# LICENSE file in the root directory of this source tree) |
| 7 | +####################################################################### |
| 8 | + |
| 9 | +import os |
| 10 | + |
| 11 | +import numpy as np |
| 12 | + |
| 13 | +import blosc2 |
| 14 | + |
| 15 | +# --- 1. Creating and populating a TreeStore --- |
| 16 | +print("--- 1. Creating and populating a TreeStore ---") |
| 17 | +# Create a new TreeStore in write mode ('w') |
| 18 | +with blosc2.TreeStore("my_experiment.b2z", mode="w") as ts: |
| 19 | + # You can store numpy arrays, which are converted to blosc2.NDArray |
| 20 | + ts["/group1/dataset1"] = np.arange(100) |
| 21 | + |
| 22 | + # You can also store blosc2 arrays directly |
| 23 | + ts["/group1/dataset2"] = blosc2.full((5, 5), fill_value=3.14) |
| 24 | + |
| 25 | + # And external arrays with vlmeta attached (these are included internally too) |
| 26 | + ext = blosc2.zeros((10,), urlpath="external_array.b2nd", mode="w") |
| 27 | + ext.vlmeta["desc"] = "included array metadata" |
| 28 | + ts["/group1/included_array"] = ext |
| 29 | + |
| 30 | + # Create another group with a dataset |
| 31 | + ts["/group2/another_dataset"] = blosc2.zeros((10,)) |
| 32 | +print("Created 'my_experiment.b2z' with initial data.\n") |
| 33 | + |
| 34 | + |
| 35 | +# --- 2. Reading from a TreeStore --- |
| 36 | +print("--- 2. Reading from a TreeStore ---") |
| 37 | +# Open the TreeStore in read mode ('r') |
| 38 | +with blosc2.TreeStore("my_experiment.b2z", mode="r") as ts: |
| 39 | + # Access a dataset |
| 40 | + dataset1 = ts["/group1/dataset1"] |
| 41 | + print("Dataset 1:", dataset1[:]) # Use [:] to decompress and get a NumPy array |
| 42 | + |
| 43 | + # Access the external array that has been included internally |
| 44 | + ext_array = ts["/group1/included_array"] |
| 45 | + print("Included array:", ext_array[:]) |
| 46 | + print("Included array metadata:", ext_array.vlmeta[:]) |
| 47 | + |
| 48 | + # List all paths in the store |
| 49 | + print("Paths in TreeStore:", list(ts)) |
| 50 | +print() |
| 51 | + |
| 52 | + |
| 53 | +# --- 3. Storing Metadata with `vlmeta` --- |
| 54 | +print("--- 3. Storing Metadata with `vlmeta` ---") |
| 55 | +with blosc2.TreeStore("my_experiment.b2z", mode="a") as ts: # 'a' for append/modify |
| 56 | + # Add metadata to the root |
| 57 | + ts.vlmeta["author"] = "The Blosc Team" |
| 58 | + ts.vlmeta["date"] = "2025-07-10" |
| 59 | + |
| 60 | + # Add metadata to a group |
| 61 | + ts["/group1"].vlmeta["description"] = "Data from the first run" |
| 62 | + |
| 63 | +# Reading metadata |
| 64 | +with blosc2.TreeStore("my_experiment.b2z", mode="r") as ts: |
| 65 | + print("Root metadata:", ts.vlmeta[:]) |
| 66 | + print("Group 1 metadata:", ts["/group1"].vlmeta[:]) |
| 67 | +print() |
| 68 | + |
| 69 | + |
| 70 | +# --- 4. Working with Subtrees (Groups) --- |
| 71 | +print("--- 4. Working with Subtrees (Groups) ---") |
| 72 | +with blosc2.TreeStore("my_experiment.b2z", mode="r") as ts: |
| 73 | + # Get the group as a subtree |
| 74 | + group1 = ts["/group1"] |
| 75 | + |
| 76 | + # Now you can access datasets relative to this group |
| 77 | + dataset2 = group1["dataset2"] |
| 78 | + print("Dataset 2 from group object:", dataset2[:]) |
| 79 | + |
| 80 | + # You can also list contents relative to the group |
| 81 | + print("Contents of group1:", list(group1)) |
| 82 | +print() |
| 83 | + |
| 84 | + |
| 85 | +# --- 5. Iterating Through a TreeStore --- |
| 86 | +print("--- 5. Iterating Through a TreeStore ---") |
| 87 | +with blosc2.TreeStore("my_experiment.b2z", mode="r") as ts: |
| 88 | + for path, node in ts.items(): |
| 89 | + if isinstance(node, blosc2.NDArray): |
| 90 | + print(f"Found dataset at '{path}' with shape {node.shape}") |
| 91 | + else: # It's a group |
| 92 | + print(f"Found group at '{path}' with metadata: {node.vlmeta[:]}") |
| 93 | +print() |
| 94 | + |
| 95 | +# --- Cleanup --- |
| 96 | +print("--- Cleanup ---") |
| 97 | +os.remove("my_experiment.b2z") |
| 98 | +print("Removed 'my_experiment.b2z'.") |
0 commit comments