Skip to content

Commit a8a782c

Browse files
committed
ENH: Dataset.read_direct method
Provide a read_direct method for compatibility with h5py. This method is not efficient, it does not avoid making intermediates.
1 parent 9680c20 commit a8a782c

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

pyfive/high_level.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,17 @@ def __getitem__(self, args):
282282
return data
283283
return data.astype(self._astype)
284284

285-
def read_direct(self, array, source_sel=None, dset_sel=None):
286-
""" Read from a HDF5 dataset directly into a NumPy array. """
287-
raise NotImplementedError
285+
def read_direct(self, array, source_sel=None, dest_sel=None):
286+
"""
287+
Read from a HDF5 dataset directly into a NumPy array.
288+
289+
This is equivalent to dset[source_sel] = arr[dset_sel].
290+
291+
Creation of intermediates is not avoided. This method if provided from
292+
compatibility with h5py, it is not efficient.
293+
294+
"""
295+
array[dest_sel] = self[source_sel]
288296

289297
def astype(self, dtype):
290298
"""

tests/test_high_level.py

+18
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,21 @@ def test_astype():
166166
assert dset1[:].dtype == np.dtype('i2')
167167
with dset1.astype('f8'):
168168
assert dset1[:].dtype == np.dtype('f8')
169+
170+
171+
def test_read_direct():
172+
173+
with pyfive.File(EARLIEST_HDF5_FILE) as hfile:
174+
dset1 = hfile['dataset1']
175+
176+
arr = np.zeros(4)
177+
dset1.read_direct(arr)
178+
assert_array_equal(arr, [0, 1, 2, 3])
179+
180+
arr = np.zeros(4)
181+
dset1.read_direct(arr, np.s_[:2], np.s_[:2])
182+
assert_array_equal(arr, [0, 1, 0, 0])
183+
184+
arr = np.zeros(4)
185+
dset1.read_direct(arr, np.s_[1:3], np.s_[2:])
186+
assert_array_equal(arr, [0, 0, 1, 2])

0 commit comments

Comments
 (0)