Skip to content

Commit de97a34

Browse files
committed
revised selection vs data shape checking
1 parent 5e25696 commit de97a34

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

h5json/hdf5db.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2313,21 +2313,31 @@ def setDatasetValuesByUuid(self, obj_uuid, data, slices=None, format="json"):
23132313
arr = arr.reshape(np_shape) # conform to selection shape
23142314

23152315
else:
2316-
# data is json
2317-
if npoints == 1 and len(dset.dtype) > 1:
2316+
# data is json
2317+
if npoints == 1 and len(dset.dtype) > 1:
23182318
# convert to tuple for compound singleton writes
23192319
data = [tuple(data),]
23202320

2321-
arr = np.array(data, dtype=dset.dtype)
2322-
# raise an exception of the array shape doesn't match the selection shape
2323-
# allow if the array is a scalar and the selection shape is one element,
2324-
# numpy is ok with this
2325-
if arr.shape == () and np_shape == (1,):
2326-
np_shape = ()
2327-
if arr.shape == (1,) and np_shape == ():
2328-
np_shape = (1,)
2329-
2330-
if arr.shape != np_shape:
2321+
arr = np.array(data, dtype=dset.dtype)
2322+
# raise an exception of the array shape doesn't match the selection shape
2323+
# allow if the array is a scalar and the selection shape is one element,
2324+
# numpy is ok with this
2325+
np_index = 0
2326+
for dim in range(len(arr.shape)):
2327+
data_extent = arr.shape[dim]
2328+
selection_extent = 1
2329+
if np_index < len(np_shape):
2330+
selection_extent = np_shape[np_index]
2331+
if selection_extent == data_extent:
2332+
np_index += 1
2333+
continue # good
2334+
if data_extent == 1:
2335+
continue # skip singleton selection
2336+
if selection_extent == 1:
2337+
np_index += 1
2338+
continue # skip singleton selection
2339+
2340+
# selection/data mismatch!
23312341
msg = "data shape doesn't match selection shape"
23322342
msg += "--data shape: " + str(arr.shape)
23332343
msg += "--selection shape: " + str(np_shape)

0 commit comments

Comments
 (0)