-
-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathtree-store-blog.py
More file actions
94 lines (73 loc) · 3.13 KB
/
Copy pathtree-store-blog.py
File metadata and controls
94 lines (73 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#######################################################################
# Copyright (c) 2019-present, Blosc Development Team <blosc@blosc.org>
# All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause
#######################################################################
import os
import numpy as np
import blosc2
# --- 1. Creating and populating a TreeStore ---
print("--- 1. Creating and populating a TreeStore ---")
# Create a new TreeStore
with blosc2.TreeStore("my_experiment.b2z", mode="w") as ts:
# You can store numpy arrays, which are converted to blosc2.NDArray
ts["/dataset0"] = np.arange(100)
# Create a group with a dataset that can be a blosc2 NDArray
ts["/group1/dataset1"] = blosc2.zeros((10,))
# You can also store blosc2 arrays directly (vlmeta included)
ext = blosc2.linspace(0, 1, 10_000, dtype=np.float32)
ext.vlmeta["desc"] = "dataset2 metadata"
ts["/group1/dataset2"] = ext
print("Created 'my_experiment.b2z' with initial data.\n")
# --- 2. Reading from a TreeStore ---
print("--- 2. Reading from a TreeStore ---")
# Open the TreeStore in read-only mode ('r')
with blosc2.TreeStore("my_experiment.b2z", mode="r") as ts:
# Access a dataset
dataset1 = ts["/group1/dataset1"]
print("Dataset 1:", dataset1[:]) # Use [:] to decompress and get a NumPy array
# Access the external array that has been stored internally
dataset2 = ts["/group1/dataset2"]
print("Dataset 2", dataset2[:])
print("Dataset 2 metadata:", dataset2.vlmeta[:])
# List all paths in the store
print("Paths in TreeStore:", list(ts))
print()
# --- 3. Storing Metadata with `vlmeta` ---
print("--- 3. Storing Metadata with `vlmeta` ---")
with blosc2.TreeStore("my_experiment.b2z", mode="a") as ts: # 'a' for append/modify
# Add metadata to the root
ts.vlmeta["author"] = "The Blosc Team"
ts.vlmeta["date"] = "2025-08-17"
# Add metadata to a group
ts["/group1"].vlmeta["description"] = "Data from the first run"
# Reading metadata
with blosc2.TreeStore("my_experiment.b2z", mode="r") as ts:
print("Root metadata:", ts.vlmeta[:])
print("Group 1 metadata:", ts["/group1"].vlmeta[:])
print()
# --- 4. Working with Subtrees (Groups) ---
print("--- 4. Working with Subtrees (Groups) ---")
with blosc2.TreeStore("my_experiment.b2z", mode="r") as ts:
# Get the group as a subtree
group1 = ts["/group1"]
# Now you can access datasets relative to this group
dataset2 = group1["dataset2"]
print("Dataset 2 from group object:", dataset2[:])
# You can also list contents relative to the group
print("Contents of group1:", list(group1))
print()
# --- 5. Iterating Through a TreeStore ---
print("--- 5. Iterating Through a TreeStore ---")
with blosc2.TreeStore("my_experiment.b2z", mode="r") as ts:
for path, node in ts.items():
if isinstance(node, blosc2.NDArray):
print(f"Found dataset at '{path}' with shape {node.shape}")
else: # It's a group
print(f"Found group at '{path}' with metadata: {node.vlmeta[:]}")
print()
# --- Cleanup ---
print("--- Cleanup ---")
os.remove("my_experiment.b2z")
print("Removed 'my_experiment.b2z'.")