Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for reading dataset fillvalues #29

Merged
merged 2 commits into from
Sep 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyfive/high_level.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ def fletcher32(self):
@property
def fillvalue(self):
""" fillvalue attribute. """
raise NotImplementedError
return self._dataobjects.fillvalue

@property
def dims(self):
Expand Down
47 changes: 47 additions & 0 deletions pyfive/low_level.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,37 @@ def shape(self):
msg_offset = msg['offset_to_message']
return determine_data_shape(self.msg_data, msg_offset)

@property
def fillvalue(self):
""" Fillvalue of the dataset. """
msg = self.find_msg_type(FILLVALUE_MSG_TYPE)[0]
offset = msg['offset_to_message']
version = struct.unpack_from('<B', self.msg_data, offset)[0]
if version == 1 or version == 2:
info = _unpack_struct_from(FILLVAL_MSG_V1V2, self.msg_data, offset)
offset += FILLVAL_MSG_V1V2_SIZE
is_defined = info['fillvalue_defined']
elif version == 3:
info = _unpack_struct_from(FILLVAL_MSG_V3, self.msg_data, offset)
offset += FILLVAL_MSG_V3_SIZE
is_defined = info['flags'] & 2**5
else:
raise InvalidHDF5File(
"Unknown fillvalue msg version:" + str(version))

if is_defined:
size = struct.unpack_from('<I', self.msg_data, offset)[0]
offset += 4
else:
size = 0

if size:
payload = self.msg_data[offset:offset+size]
fillvalue = np.fromstring(payload, self.dtype, count=1)[0]
else:
fillvalue = 0
return fillvalue

@property
def dtype(self):
""" Datatype of the dataset. """
Expand Down Expand Up @@ -1318,6 +1349,22 @@ def _unpack_struct_from(structure, buf, offset=0):
('heap_address', 'Q'), # 8 byte addressing
))

# IV.A.2.f. The Data Storage - Fill Value Message
FILLVAL_MSG_V1V2 = OrderedDict((
('version', 'B'),
('space_allocation_time', 'B'),
('fillvalue_write_time', 'B'),
('fillvalue_defined', 'B'),
))
FILLVAL_MSG_V1V2_SIZE = _structure_size(FILLVAL_MSG_V1V2)

FILLVAL_MSG_V3 = OrderedDict((
('version', 'B'),
('flags', 'B'),
))
FILLVAL_MSG_V3_SIZE = _structure_size(FILLVAL_MSG_V3)


# IV.A.2.l The Data Storage - Filter Pipeline message
FILTER_PIPELINE_DESCR_V1 = OrderedDict((
('filter_id', 'H'),
Expand Down
Binary file added tests/fillvalue_earliest.hdf5
Binary file not shown.
Binary file added tests/fillvalue_latest.hdf5
Binary file not shown.
24 changes: 24 additions & 0 deletions tests/make_fillvalue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#! /usr/bin/env python
""" Create two HDF5 files with datasets with and without fillvalues. """
import h5py
import numpy as np


def make_fillvalue(f):

common_args = {
'shape': (4, ),
'data': np.arange(4),
'track_times': False,
}

f.create_dataset('dset1', dtype='<i1', fillvalue=42, **common_args)
f.create_dataset('dset2', dtype='<i1', **common_args)
f.create_dataset('dset3', dtype='<f4', fillvalue=99.5, **common_args)


with h5py.File('fillvalue_earliest.hdf5', 'w', libver='earliest') as f:
make_fillvalue(f)

with h5py.File('fillvalue_latest.hdf5', 'w', libver='latest') as f:
make_fillvalue(f)
24 changes: 24 additions & 0 deletions tests/test_fillvalue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
""" Test pyfive's Dataset fillvalue attribute. """
import os

import pyfive

DIRNAME = os.path.dirname(__file__)
DATASET_FILLVALUE_EARLIEST_HDF5_FILE = os.path.join(
DIRNAME, 'fillvalue_earliest.hdf5')
DATASET_FILLVALUE_LATEST_HDF5_FILE = os.path.join(
DIRNAME, 'fillvalue_latest.hdf5')


def test_dataset_fillvalue_earliest():
with pyfive.File(DATASET_FILLVALUE_EARLIEST_HDF5_FILE) as hfile:
assert hfile['dset1'].fillvalue == 42
assert hfile['dset2'].fillvalue == 0
assert abs(hfile['dset3'].fillvalue - 99.5) < 0.05


def test_dataset_fillvalue_latest():
with pyfive.File(DATASET_FILLVALUE_LATEST_HDF5_FILE) as hfile:
assert hfile['dset1'].fillvalue == 42
assert hfile['dset2'].fillvalue == 0
assert abs(hfile['dset3'].fillvalue - 99.5) < 0.05