Skip to content

Commit

Permalink
Merge pull request #821 from bp/hdf5_overwrite
Browse files Browse the repository at this point in the history
whole array overwrite method in Model
  • Loading branch information
andy-beer committed Aug 27, 2024
2 parents 8d87f0e + 4ec3b75 commit adef702
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
8 changes: 8 additions & 0 deletions resqpy/model/_hdf5.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,14 @@ def _h5_overwrite_array_slice(model, h5_key_pair, slice_tuple, array_slice):
dset[slice_tuple] = array_slice


def _h5_overwrite_array(model, h5_key_pair, array):
"""Overwrites (updates) the whole of an hdf5 array."""

h5_root = _h5_access(model, h5_key_pair[0], mode = 'a')
dset = h5_root[h5_key_pair[1]]
dset[...] = array


def h5_clear_filename_cache(model):
"""Clears the cached filenames associated with all ext uuids."""

Expand Down
15 changes: 15 additions & 0 deletions resqpy/model/_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1475,6 +1475,21 @@ def h5_overwrite_array_slice(self, h5_key_pair, slice_tuple, array_slice):

return m_h._h5_overwrite_array_slice(self, h5_key_pair, slice_tuple, array_slice)

def h5_overwrite_array(self, h5_key_pair, array):
"""Overwrites (updates) the whole of an hdf5 array.
arguments:
h5_key_pair (uuid, string): the uuid of the hdf5 ext part and the hdf5 internal path to the
required hdf5 array
array (numpy array of shape to match existing hdf5 dataset): the data to write
notes:
this method naively updates an hdf5 array without using mpi to look after parallel updates;
metadata (such as uuid or property min, max values) is not modified in any way by the method
"""

return m_h._h5_overwrite_array(self, h5_key_pair, array)

def h5_clear_filename_cache(self):
"""Clears the cached filenames associated with all ext uuids."""

Expand Down
25 changes: 25 additions & 0 deletions tests/unit_tests/model/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,31 @@ def test_h5_array_element(example_model_with_properties):
assert zone == 2


def test_h5_overwrite_array(example_model_with_properties):
model = example_model_with_properties
zone_root = model.root(obj_type = 'DiscreteProperty', title = 'Zone')
assert zone_root is not None
key_pair = model.h5_uuid_and_path_for_node(rqet.find_nested_tags(zone_root, ['PatchOfValues', 'Values']))
assert key_pair is not None and all([x is not None for x in key_pair])
# check full array is expected size and shape
shape, dtype = model.h5_array_shape_and_type(key_pair)
assert shape == (3, 5, 5)
newzone = np.arange(3 * 5 * 5, dtype = int).reshape(shape)
model.h5_overwrite_array(key_pair, newzone)
model = rq.Model(model.epc_file)
zone_root = model.root(obj_type = 'DiscreteProperty', title = 'Zone')
assert zone_root is not None
key_pair = model.h5_uuid_and_path_for_node(rqet.find_nested_tags(zone_root, ['PatchOfValues', 'Values']))
model.h5_array_element(key_pair, cache_array = True, object = model, array_attribute = 'zone_reload')
assert model.zone_reload.shape == (3, 5, 5)
assert np.all(model.zone_reload == np.arange(3 * 5 * 5, dtype = int).reshape(shape))
slice_tuple = (slice(1), slice(2, 5), slice(1, 3))
model.h5_overwrite_array_slice(key_pair, slice_tuple, 0)
model.h5_array_element(key_pair, cache_array = True, object = model, array_attribute = 'zone_reloaded_again')
assert np.count_nonzero(model.zone_reloaded_again == model.zone_reload) == 69
assert np.min(model.zone_reloaded_again) == 0


def add_grids(model, crs, add_lengths):
grid_a = grr.RegularGrid(model,
extent_kji = (2, 2, 2),
Expand Down

0 comments on commit adef702

Please sign in to comment.