Skip to content

Commit 6f94e07

Browse files
committed
adjust dataset updates for resize
1 parent df8ce23 commit 6f94e07

File tree

4 files changed

+81
-6
lines changed

4 files changed

+81
-6
lines changed

src/h5json/hdf5db.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def deleted_objects(self):
181181
return self._deleted_objects
182182

183183
@property
184-
def resized_datsets(self):
184+
def resized_datasets(self):
185185
return self._resized_datasets
186186

187187
def _getDatasetUpdates(self, dset_id):
@@ -769,8 +769,24 @@ def resizeDataset(self, dset_id, shape):
769769
if dset_id not in self.new_objects:
770770
self._resized_datasets.add(dset_id)
771771

772-
# if the shape has shrunk in any dimension, do a flush now
773772
new_dims = getShapeDims(dset_json)
773+
rank = len(new_dims)
774+
775+
# adjust any selections in the update list
776+
updates = self._getDatasetUpdates(dset_id)
777+
for i in range(len(updates)):
778+
(sel_update, arr) = updates[i]
779+
if sel_update.select_type == selections.H5S_SELECT_HYPERSLABS:
780+
slices = list(sel_update.slices)
781+
for dim in range(rank):
782+
s = slices[dim]
783+
if s.stop > new_dims[dim]:
784+
# selection outside new bounds of dataset
785+
slices[dim] = slice(s.start, new_dims[dim], s.step)
786+
sel_update = selections.select(new_dims, tuple(slices))
787+
updates[i] = (sel_update, arr)
788+
789+
# if the shape has shrunk in any dimension, do a flush now
774790
do_flush = False
775791
for i in range(len(new_dims)):
776792
if new_dims[i] < old_dims[i]:

src/h5json/selections.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,6 @@ def __getitem__(self, args):
422422
start, count, step, scalar = _handle_simple(self._shape, args)
423423
self._sel = (start, count, step, scalar)
424424

425-
# self._id.select_hyperslab(start, count, step)
426425
self._select_type = H5S_SELECT_HYPERSLABS
427426

428427
self._mshape = tuple(x for x, y in zip(count, scalar) if not y)

src/h5json/shape_util.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ def getShapeJson(dims, maxdims=None):
4040
datasets) """
4141
if isinstance(dims, int):
4242
dims = (dims, )
43-
if isinstance(maxdims, int):
44-
maxdims = (maxdims, )
43+
elif dims == "H5S_NULL":
44+
dims = None
4545
if dims is None:
4646
shape_class = "H5S_NULL"
4747
elif len(dims) == 0:
@@ -58,6 +58,8 @@ def getShapeJson(dims, maxdims=None):
5858
if maxdims is not None:
5959
if shape_class != "H5S_SIMPLE":
6060
raise ValueError(f"maxdims can not be used with shape class: {shape_class}")
61+
if isinstance(maxdims, int):
62+
maxdims = (maxdims, )
6163
if len(maxdims) != len(dims):
6264
raise ValueError("maxdims must match dataspace rank")
6365
# convert any 0 or None vlues to "H5S_UNLIMITED"

test/unit/shape_util_test.py

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import unittest
1313
import logging
1414

15-
from h5json.shape_util import getShapeClass, getShapeDims, getNumElements, getRank
15+
from h5json.shape_util import getShapeClass, getShapeDims, getNumElements, getRank, getShapeJson
1616
from h5json.shape_util import isNullSpace, isScalar, getDataSize, isExtensible, getMaxDims
1717

1818

@@ -23,6 +23,64 @@ def __init__(self, *args, **kwargs):
2323
self.logger = logging.getLogger()
2424
self.logger.setLevel(logging.WARNING)
2525

26+
def testGetShape(self):
27+
28+
null_shape = getShapeJson("H5S_NULL")
29+
self.assertTrue("class" in null_shape)
30+
self.assertEqual(null_shape["class"], "H5S_NULL")
31+
self.assertFalse("dims" in null_shape)
32+
self.assertFalse("maxdims" in null_shape)
33+
34+
null_shape = getShapeJson(None)
35+
self.assertTrue("class" in null_shape)
36+
self.assertEqual(null_shape["class"], "H5S_NULL")
37+
self.assertFalse("dims" in null_shape)
38+
self.assertFalse("maxdims" in null_shape)
39+
40+
scalar_shape = getShapeJson(())
41+
self.assertTrue("class" in scalar_shape)
42+
self.assertEqual(scalar_shape["class"], "H5S_SCALAR")
43+
self.assertTrue("dims" not in scalar_shape)
44+
self.assertFalse("maxdims" in scalar_shape)
45+
46+
simple_shape = getShapeJson(42)
47+
self.assertTrue("class" in simple_shape)
48+
self.assertEqual(simple_shape["class"], "H5S_SIMPLE")
49+
self.assertTrue("dims" in simple_shape)
50+
self.assertEqual(simple_shape["dims"], (42, ))
51+
self.assertFalse("maxdims" in simple_shape)
52+
53+
simple_shape = getShapeJson((42, ))
54+
self.assertTrue("class" in simple_shape)
55+
self.assertEqual(simple_shape["class"], "H5S_SIMPLE")
56+
self.assertTrue("dims" in simple_shape)
57+
self.assertEqual(simple_shape["dims"], (42, ))
58+
self.assertFalse("maxdims" in simple_shape)
59+
60+
extendable_shape = getShapeJson((4, 5), maxdims=("H5S_UNLIMITED", 10))
61+
self.assertTrue("class" in extendable_shape)
62+
self.assertEqual(extendable_shape["class"], "H5S_SIMPLE")
63+
self.assertTrue("dims" in extendable_shape)
64+
self.assertEqual(extendable_shape["dims"], (4, 5))
65+
self.assertTrue("maxdims" in extendable_shape)
66+
self.assertTrue(extendable_shape["maxdims"], ("H5S_UNLIMITED", 10))
67+
68+
extendable_shape = getShapeJson((4, 5), maxdims=(None, 10))
69+
self.assertTrue("class" in extendable_shape)
70+
self.assertEqual(extendable_shape["class"], "H5S_SIMPLE")
71+
self.assertTrue("dims" in extendable_shape)
72+
self.assertEqual(extendable_shape["dims"], (4, 5))
73+
self.assertTrue("maxdims" in extendable_shape)
74+
self.assertTrue(extendable_shape["maxdims"], ("H5S_UNLIMITED", 10))
75+
76+
extendable_shape = getShapeJson((4, 5), maxdims=(0, 10))
77+
self.assertTrue("class" in extendable_shape)
78+
self.assertEqual(extendable_shape["class"], "H5S_SIMPLE")
79+
self.assertTrue("dims" in extendable_shape)
80+
self.assertEqual(extendable_shape["dims"], (4, 5))
81+
self.assertTrue("maxdims" in extendable_shape)
82+
self.assertTrue(extendable_shape["maxdims"], ("H5S_UNLIMITED", 10))
83+
2684
def testSimple(self):
2785

2886
type_json = {

0 commit comments

Comments
 (0)