Skip to content

Commit 09a4b16

Browse files
committed
Fix test_dataset.py, add to testall
1 parent f783446 commit 09a4b16

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

h5pyd/_hl/dataset.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,25 +1369,29 @@ def __setitem__(self, args, val):
13691369
# Generally we try to avoid converting the arrays on the Python
13701370
# side. However, for compound literals this is unavoidable.
13711371
# For h5pyd, do extra check and convert type on client side for efficiency
1372-
vlen = check_dtype(vlen=self.dtype)
1373-
1374-
if not isinstance(val, numpy.ndarray) and vlen is not None and vlen not in (bytes, str):
1372+
vlen_base_class = check_dtype(vlen=self.dtype)
1373+
if vlen_base_class is not None and vlen_base_class not in (bytes, str):
13751374
try:
1376-
val = numpy.asarray(val, dtype=vlen)
1375+
# Attempt to directly convert the input array of vlen data to its base class
1376+
val = numpy.asarray(val, dtype=vlen_base_class)
13771377

13781378
except ValueError as ve:
1379+
# Failed to convert input array to vlen base class directly, instead create a new array where
1380+
# each element is an array of the Dataset's dtype
13791381
self.log.debug(f"asarray ValueError: {ve}")
13801382
try:
1381-
val = numpy.array(
1382-
[numpy.array(x, dtype=self.dtype) for x in val],
1383-
dtype=self.dtype,
1384-
)
1383+
# Force output shape
1384+
tmp = numpy.empty(shape=val.shape, dtype=self.dtype)
1385+
tmp[:] = [numpy.array(x, dtype=self.dtype) for x in val]
1386+
val = tmp
13851387
except ValueError as e:
13861388
msg = f"ValueError converting value element by element: {e}"
13871389
self.log.debug(msg)
13881390

1389-
if vlen == val.dtype:
1391+
if vlen_base_class == val.dtype:
13901392
if val.ndim > 1:
1393+
# Reshape array to 2D, where first dim = product of all dims except last, and second dim = last dim
1394+
# Then flatten it to 1D
13911395
tmp = numpy.empty(shape=val.shape[:-1], dtype=self.dtype)
13921396
tmp.ravel()[:] = [
13931397
i
@@ -1434,6 +1438,7 @@ def __setitem__(self, args, val):
14341438
else:
14351439
dtype = self.dtype
14361440
cast_compound = False
1441+
14371442
val = numpy.asarray(val, dtype=dtype, order="C")
14381443
if cast_compound:
14391444
val = val.astype(numpy.dtype([(names[0], dtype)]))
@@ -1520,7 +1525,7 @@ def __setitem__(self, args, val):
15201525
if self.id.uuid.startswith("d-"):
15211526
# server is HSDS, use binary data, use param values for selection
15221527
format = "binary"
1523-
body = arrayToBytes(val, vlen=vlen)
1528+
body = arrayToBytes(val, vlen=vlen_base_class)
15241529
self.log.debug(f"writing binary data, {len(body)}")
15251530
else:
15261531
# h5serv, base64 encode, body json for selection

test/hl/test_vlentype.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -349,14 +349,16 @@ def test_variable_len_float_dset(self):
349349
e1 = np.array([1.9, 2.8, 3.7], dtype=np.float64)
350350

351351
data = np.array([e0, e1], dtype=dtvlen)
352-
try:
353-
# This will fail on HSDS because data is a ndarray of shape (2,3) of floats
352+
353+
if isinstance(dset.id.id, str):
354+
# id is str for HSDS, int for h5py
354355
dset[...] = data
355-
if isinstance(dset.id.id, str):
356-
# id is str for HSDS, int for h5py
357-
self.assertTrue(False)
358-
except ValueError:
359-
pass # expected
356+
else:
357+
try:
358+
# This will fail on h5py due to a different in internal array handling.
359+
dset[...] = data
360+
except ValueError:
361+
pass # expected on h5py
360362

361363
data = np.zeros((2,), dtype=dtvlen)
362364
data[0] = e0

testall.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
hl_tests = ('test_attribute',
1919
'test_committedtype',
2020
'test_complex_numbers',
21+
'test_dataset',
2122
'test_dataset_compound',
2223
'test_dataset_create',
2324
'test_dataset_extend',

0 commit comments

Comments
 (0)