Skip to content

Commit

Permalink
Merge pull request #37 from jjhelmus/read_direct
Browse files Browse the repository at this point in the history
ENH: Dataset.read_direct method
  • Loading branch information
jjhelmus authored Oct 3, 2017
2 parents 9680c20 + a8a782c commit 3bb186f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
14 changes: 11 additions & 3 deletions pyfive/high_level.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,17 @@ def __getitem__(self, args):
return data
return data.astype(self._astype)

def read_direct(self, array, source_sel=None, dset_sel=None):
""" Read from a HDF5 dataset directly into a NumPy array. """
raise NotImplementedError
def read_direct(self, array, source_sel=None, dest_sel=None):
"""
Read from a HDF5 dataset directly into a NumPy array.
This is equivalent to dset[source_sel] = arr[dset_sel].
Creation of intermediates is not avoided. This method if provided from
compatibility with h5py, it is not efficient.
"""
array[dest_sel] = self[source_sel]

def astype(self, dtype):
"""
Expand Down
18 changes: 18 additions & 0 deletions tests/test_high_level.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,21 @@ def test_astype():
assert dset1[:].dtype == np.dtype('i2')
with dset1.astype('f8'):
assert dset1[:].dtype == np.dtype('f8')


def test_read_direct():

with pyfive.File(EARLIEST_HDF5_FILE) as hfile:
dset1 = hfile['dataset1']

arr = np.zeros(4)
dset1.read_direct(arr)
assert_array_equal(arr, [0, 1, 2, 3])

arr = np.zeros(4)
dset1.read_direct(arr, np.s_[:2], np.s_[:2])
assert_array_equal(arr, [0, 1, 0, 0])

arr = np.zeros(4)
dset1.read_direct(arr, np.s_[1:3], np.s_[2:])
assert_array_equal(arr, [0, 0, 1, 2])

0 comments on commit 3bb186f

Please sign in to comment.