Skip to content

Commit 59d2959

Browse files
committed
Honor the contiguous param
1 parent f8b2bfd commit 59d2959

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

blosc2/core.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -598,15 +598,15 @@ def pack_tensor(tensor, chunksize=None, **kwargs):
598598
arr = np.asarray(tensor)
599599
# If not passed, set a sensible typesize
600600
if 'cparams' in kwargs and 'typesize' not in kwargs['cparams']:
601-
cparams = kwargs.pop('cparams')
602-
cparams = cparams.copy()
601+
cparams = kwargs.pop('cparams').copy()
603602
cparams['typesize'] = arr.itemsize
604603
kwargs['cparams'] = cparams
605604
elif 'typesize' not in kwargs:
606605
kwargs['typesize'] = arr.itemsize
607606

608607
urlpath = kwargs.get('urlpath', None)
609-
contiguous = False if urlpath is None else True
608+
if 'contiguous' not in kwargs:
609+
kwargs['contiguous'] = False if urlpath is None else True
610610

611611
if chunksize is None:
612612
chunksize = arr.size * arr.itemsize
@@ -615,7 +615,7 @@ def pack_tensor(tensor, chunksize=None, **kwargs):
615615
chunksize = 2 ** 28
616616
# Make that a multiple of typesize
617617
chunksize = chunksize // arr.itemsize * arr.itemsize
618-
schunk = blosc2.SChunk(chunksize=chunksize, contiguous=contiguous, data=arr, **kwargs)
618+
schunk = blosc2.SChunk(chunksize=chunksize, data=arr, **kwargs)
619619
# Guess the kind of tensor / array
620620
repr_tensor = repr(tensor)
621621
if "tensor" in repr_tensor:

tests/test_tensor.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
########################################################################
66

77

8+
import os
89
import numpy as np
910
import pytest
1011
import blosc2
@@ -170,3 +171,18 @@ def test_save_tensor_torch(size, dtype, urlpath):
170171
tensor2 = blosc2.load_tensor(urlpath)
171172
blosc2.remove_urlpath(urlpath)
172173
assert np.array_equal(nparray, np.asarray(tensor2))
174+
175+
@pytest.mark.parametrize(
176+
"size, sparse, urlpath", [
177+
(1e6, True, "test.bl2"),
178+
(1e6, False, "test.bl2"),
179+
])
180+
def test_save_tensor_sparse(size, sparse, urlpath):
181+
nparray = np.arange(size, dtype=np.int32)
182+
serial_size = blosc2.save_tensor(nparray, urlpath, mode="w", contiguous=not sparse)
183+
assert serial_size < nparray.size * nparray.itemsize
184+
185+
a2 = blosc2.load_tensor(urlpath)
186+
assert os.path.isdir(urlpath) == sparse
187+
blosc2.remove_urlpath(urlpath)
188+
assert np.array_equal(nparray, a2)

0 commit comments

Comments
 (0)