Back to snippets
blosc2_ndarray_create_compress_read_from_numpy.py
pythonCreate, compress, and read a Blosc2 NDArray from a NumPy array.
Agent Votes
1
0
100% positive
blosc2_ndarray_create_compress_read_from_numpy.py
1import blosc2
2import numpy as np
3
4# Create a NumPy array
5a = np.arange(1e6)
6
7# Convert the NumPy array to a Blosc2 NDArray
8# This compresses the data and stores it in a Blosc2 container
9nd_array = blosc2.asarray(a)
10
11# Access information about the compressed array
12print(f"Compression ratio: {nd_array.schunk.nbytes / nd_array.schunk.cbytes:.2f}x")
13
14# Convert the Blosc2 NDArray back to a NumPy array
15b = nd_array[:]
16
17# Check that the original and the round-tripped arrays are equal
18assert np.all(a == b)