-
Notifications
You must be signed in to change notification settings - Fork 25
fix Enum and Empty attributes #102
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0781988
fix enum attribute
kmuehlbauer c222e53
fix Empty aka NULL Dataspace Attributes
kmuehlbauer 524e72c
add s3 test for dataspace null case
valeriupredoi a1640ca
add test for new behaviour of writing array/list of strings with h5py
kmuehlbauer b2099af
fix docstring for Empty
kmuehlbauer cdaa608
move check for NULL dataspace as suggested by review
kmuehlbauer bc57cf4
update tests to new layout
kmuehlbauer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,90 +1,111 @@ | ||
| #! /usr/bin/env python | ||
|
|
||
| """ Create a HDF5 file with all the supported attribute types. """ | ||
| import sys | ||
| import h5py | ||
| import numpy as np | ||
|
|
||
|
|
||
| f = h5py.File('attr_datatypes.hdf5', 'w') | ||
| attrs = f.attrs | ||
|
|
||
| # intergers | ||
| attrs.create('int08_little', -123, dtype='<i1') | ||
| attrs.create('int16_little', -123, dtype='<i2') | ||
| attrs.create('int32_little', -123, dtype='<i4') | ||
| attrs.create('int64_little', -123, dtype='<i8') | ||
|
|
||
| attrs.create('uint08_little', 130, dtype='<u1') | ||
| attrs.create('uint16_little', 32770, dtype='<u2') | ||
| attrs.create('uint32_little', 2147483650, dtype='<u4') | ||
| attrs.create('uint64_little', 9223372036854775810, dtype='<u8') | ||
|
|
||
| attrs.create('int08_big', -123, dtype='>i1') | ||
| attrs.create('int16_big', -123, dtype='>i2') | ||
| attrs.create('int32_big', -123, dtype='>i4') | ||
| attrs.create('int64_big', -123, dtype='>i8') | ||
|
|
||
| attrs.create('uint08_big', 130, dtype='>u1') | ||
| attrs.create('uint16_big', 32770, dtype='>u2') | ||
| attrs.create('uint32_big', 2147483650, dtype='>u4') | ||
| attrs.create('uint64_big', 9223372036854775810, dtype='>u8') | ||
|
|
||
| # floating point | ||
| attrs.create('float32_little', 123, dtype='<f4') | ||
| attrs.create('float64_little', 123, dtype='<f8') | ||
|
|
||
| attrs.create('float32_big', 123, dtype='>f4') | ||
| attrs.create('float64_big', 123, dtype='>f8') | ||
|
|
||
| # fixed-length strings | ||
| attrs.create('string_one', b'H', dtype='|S1') | ||
| attrs.create('string_two', b'Hi', dtype='|S2') | ||
|
|
||
| # variable length strings | ||
| attrs['vlen_string'] = b'Hello' | ||
|
|
||
| # variable length unicode | ||
| attrs['vlen_unicode'] = u'Hello' + chr(0x00A7) | ||
|
|
||
| # arrayed numeric types | ||
| attrs.create('int32_array', [-123, 45], dtype='<i4') | ||
| attrs.create('uint64_array', [12, 34], dtype='>u8') | ||
| attrs.create('float32_array', [123, 456], dtype='<f4') | ||
|
|
||
| # arrayed variable length strings | ||
| attrs['vlen_str_array'] = [b'Hello', b'World!'] | ||
|
|
||
| # variables length sequences | ||
| val = np.empty((2, ), dtype=np.object) | ||
kmuehlbauer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| val[0] = np.array([-1, 2], dtype='<i4') | ||
| val[1] = np.array([3, 4, 5], dtype='<i4') | ||
| dt = h5py.special_dtype(vlen=np.dtype('<i4')) | ||
| attrs.create('vlen_int32', val, dtype=dt) | ||
|
|
||
| val = np.empty((3, ), dtype=np.object) | ||
| val[0] = np.array([1, 2], dtype='>u8') | ||
| val[1] = np.array([3, 4, 5], dtype='>u8') | ||
| val[2] = np.array([42], dtype='>u8') | ||
| dt = h5py.special_dtype(vlen=np.dtype('>u8')) | ||
| attrs.create('vlen_uint64', val, dtype=dt) | ||
|
|
||
| val = np.empty((3, ), dtype=np.object) | ||
| val[0] = np.array([0, ], dtype='<f4') | ||
| val[1] = np.array([1, 2, 3], dtype='<f4') | ||
| val[2] = np.array([4, 5], dtype='<f4') | ||
| dt = h5py.special_dtype(vlen=np.dtype('<f4')) | ||
| attrs.create('vlen_float32', val, dtype=dt) | ||
|
|
||
| # TODO more complex datatypes | ||
| # complex H5T_COMPOUND | ||
| attrs.create('complex64_little', 123+456.j, dtype='<c8') | ||
| attrs.create('complex128_little', 123+456.j, dtype='<c16') | ||
|
|
||
| attrs.create('complex64_big', 123+456.j, dtype='<c8') | ||
| attrs.create('complex128_big', 123+456.j, dtype='<c16') | ||
|
|
||
| # booleans HT5_ENUM | ||
| #attrs.create('bool', True, dtype=np.bool_) | ||
|
|
||
|
|
||
| f.close() | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| def create_file(path): | ||
|
|
||
| with h5py.File(path, 'w') as f: | ||
| attrs = f.attrs | ||
|
|
||
| # integers | ||
| attrs.create('int08_little', -123, dtype='<i1') | ||
| attrs.create('int16_little', -123, dtype='<i2') | ||
| attrs.create('int32_little', -123, dtype='<i4') | ||
| attrs.create('int64_little', -123, dtype='<i8') | ||
|
|
||
| attrs.create('uint08_little', 130, dtype='<u1') | ||
| attrs.create('uint16_little', 32770, dtype='<u2') | ||
| attrs.create('uint32_little', 2147483650, dtype='<u4') | ||
| attrs.create('uint64_little', 9223372036854775810, dtype='<u8') | ||
|
|
||
| attrs.create('int08_big', -123, dtype='>i1') | ||
| attrs.create('int16_big', -123, dtype='>i2') | ||
| attrs.create('int32_big', -123, dtype='>i4') | ||
| attrs.create('int64_big', -123, dtype='>i8') | ||
|
|
||
| attrs.create('uint08_big', 130, dtype='>u1') | ||
| attrs.create('uint16_big', 32770, dtype='>u2') | ||
| attrs.create('uint32_big', 2147483650, dtype='>u4') | ||
| attrs.create('uint64_big', 9223372036854775810, dtype='>u8') | ||
|
|
||
| # floating point | ||
| attrs.create('float32_little', 123, dtype='<f4') | ||
| attrs.create('float64_little', 123, dtype='<f8') | ||
|
|
||
| attrs.create('float32_big', 123, dtype='>f4') | ||
| attrs.create('float64_big', 123, dtype='>f8') | ||
|
|
||
| # fixed-length strings | ||
| attrs.create('string_one', b'H', dtype='|S1') | ||
| attrs.create('string_two', b'Hi', dtype='|S2') | ||
|
|
||
| # variable length strings | ||
| attrs['vlen_string'] = b'Hello' | ||
|
|
||
| # variable length unicode | ||
| attrs['vlen_unicode'] = u'Hello' + chr(0x00A7) | ||
|
|
||
| # arrayed numeric types | ||
| attrs.create('int32_array', [-123, 45], dtype='<i4') | ||
| attrs.create('uint64_array', [12, 34], dtype='>u8') | ||
| attrs.create('float32_array', [123, 456], dtype='<f4') | ||
|
|
||
| # arrayed variable length strings and bytes | ||
| # see https://github.com/NCAS-CMS/pyfive/pull/102#discussion_r2393563713 | ||
| attrs['vlen_str_array'] = np.array([b'Hello', b'World!'], dtype="|S") | ||
| attrs['vlen_str_array1'] = [b'Hello', b'World!'] | ||
|
|
||
| # variables length sequences | ||
| val = np.empty((2, ), dtype=object) | ||
| val[0] = np.array([-1, 2], dtype='<i4') | ||
| val[1] = np.array([3, 4, 5], dtype='<i4') | ||
| dt = h5py.special_dtype(vlen=np.dtype('<i4')) | ||
| attrs.create('vlen_int32', val, dtype=dt) | ||
|
|
||
| val = np.empty((3, ), dtype=object) | ||
| val[0] = np.array([1, 2], dtype='>u8') | ||
| val[1] = np.array([3, 4, 5], dtype='>u8') | ||
| val[2] = np.array([42], dtype='>u8') | ||
| dt = h5py.special_dtype(vlen=np.dtype('>u8')) | ||
| attrs.create('vlen_uint64', val, dtype=dt) | ||
|
|
||
| val = np.empty((3, ), dtype=object) | ||
| val[0] = np.array([0, ], dtype='<f4') | ||
| val[1] = np.array([1, 2, 3], dtype='<f4') | ||
| val[2] = np.array([4, 5], dtype='<f4') | ||
| dt = h5py.special_dtype(vlen=np.dtype('<f4')) | ||
| attrs.create('vlen_float32', val, dtype=dt) | ||
|
|
||
| # TODO more complex datatypes | ||
| # complex H5T_COMPOUND | ||
| attrs.create('complex64_little', 123+456.j, dtype='<c8') | ||
| attrs.create('complex128_little', 123+456.j, dtype='<c16') | ||
|
|
||
| attrs.create('complex64_big', 123+456.j, dtype='<c8') | ||
| attrs.create('complex128_big', 123+456.j, dtype='<c16') | ||
|
|
||
| # booleans HT5_ENUM | ||
| #attrs.create('bool', True, dtype=np.bool_) | ||
|
|
||
| # H5T_ENUM | ||
| # Define an enum dtype | ||
| enum_dtype = h5py.special_dtype( | ||
| enum=(np.int32, {'one': 1, 'two': 2, 'three': 3}) | ||
| ) | ||
| # Create an attribute with that enum dtype | ||
| attrs.create('enum', 2, dtype=enum_dtype) | ||
|
|
||
| # empty string with NULL dataspace | ||
| # see https://github.com/NCAS-CMS/pyfive/issues/100 | ||
| attrs.create('empty_string', h5py.Empty(dtype=np.dtype('|S1'))) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| default_path = Path(__file__).parent / "attr_datatypes.hdf5" | ||
| filepath = Path(sys.argv[1]) if len(sys.argv) > 1 else default_path | ||
| create_file(filepath) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import os | ||
|
|
||
| import pyfive | ||
| import s3fs | ||
|
|
||
|
|
||
| def _load_nc_file(ncvar): | ||
| """ | ||
| Get the netcdf file and its b-tree. | ||
|
|
||
| Fixture to test loading an issue file. | ||
| """ | ||
| issue_file = "da193a_25_6hr_t_pt_cordex__198807-198807.nc" | ||
| storage_options = { | ||
| 'anon': True, | ||
| 'client_kwargs': {'endpoint_url': "https://uor-aces-o.s3-ext.jc.rl.ac.uk"}, # final proxy | ||
| } | ||
| test_file_uri = os.path.join( | ||
| "esmvaltool-zarr", | ||
| issue_file | ||
| ) | ||
| fs = s3fs.S3FileSystem(**storage_options) | ||
| s3file = fs.open(test_file_uri, 'rb') | ||
| nc = pyfive.File(s3file) | ||
| ds = nc[ncvar] | ||
|
|
||
| return ds | ||
|
|
||
|
|
||
| def test_buffer_issue(): | ||
| """ | ||
| Test the case when the attribute contains no data. | ||
|
|
||
| This happens when DATASPACE is NULL and DATA is empty. | ||
| """ | ||
| print("File with issue da193a_25_6hr_t_pt_cordex__198807-198807.nc") | ||
| print("Variable m01s30i111") | ||
| _load_nc_file('m01s30i111') | ||
kmuehlbauer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.