Skip to content

Commit 5842366

Browse files
committed
Add an example of getting a slice out of a C2Array
1 parent e8d97b5 commit 5842366

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

examples/c2array-get-slice.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
# Example for opening and reading a C2Array (remote array)
10+
11+
from time import time
12+
13+
import numpy as np
14+
15+
import blosc2
16+
17+
urlbase = "https://cat2.cloud/demo"
18+
root = "@public"
19+
20+
# Access the server
21+
# urlpath = blosc2.URLPath(f'{root}/examples/ds-1d.b2nd', urlbase)
22+
# urlpath = blosc2.URLPath(f'{root}/examples/sa-1M.b2nd', urlbase)
23+
urlpath = blosc2.URLPath(f"{root}/examples/lung-jpeg2000_10x.b2nd", urlbase)
24+
# urlpath = blosc2.URLPath(f'{root}/examples/uncompressed_lung-jpeg2000_10x.b2nd', urlbase)
25+
26+
# Open the remote array
27+
t0 = time()
28+
remote_array = blosc2.open(urlpath, mode="r")
29+
size = np.prod(remote_array.shape) * remote_array.cparams.typesize
30+
print(f"Time for opening data (HTTP): {time() - t0:.3f}s - file size: {size / 2**10:.2f} KB")
31+
32+
# Fetch a slice of the remote array as a numpy array
33+
t0 = time()
34+
a = remote_array[5:9]
35+
print(f"Time for reading data (HTTP): {time() - t0:.3f}s - {a.nbytes / 2**10:.2f} KB")
36+
37+
# TODO: Fetch a slice of the remote array as a blosc2.NDArray

src/blosc2/ndarray.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1929,7 +1929,7 @@ def slice(self, key: int | slice | Sequence[slice], **kwargs: Any) -> NDArray:
19291929
key, mask = process_key(key, self.shape)
19301930
start, stop, step = get_ndarray_start_stop(self.ndim, key, self.shape)
19311931

1932-
# Fast path for slices made with consecutive chunks
1932+
# Fast path for slices made with aligned chunks
19331933
if step == (1,) * self.ndim:
19341934
aligned_chunks = detect_aligned_chunks(key, self.shape, self.chunks, consecutive=False)
19351935
if aligned_chunks:

0 commit comments

Comments
 (0)