Skip to content

Commit 7bab5f1

Browse files
bugfix. not returning group or dataset from attrs
1 parent 80e519a commit 7bab5f1

File tree

4 files changed

+42
-43
lines changed

4 files changed

+42
-43
lines changed

h5rdmtoolbox/convention/toolbox_validators.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
"""general validation functions of the toolbox usable by convention. If users wish to user
22
their own validators, they need to define them separately. The respective python script then
33
must be provided during initialization of a Convention"""
4+
import re
5+
from typing import Union, Dict
46

57
import pint
6-
import re
78
import typing_extensions
89
from pydantic.functional_validators import WrapValidator
9-
from typing import Union, Dict
1010
from typing_extensions import Annotated
1111

1212
from h5rdmtoolbox import get_ureg, errors
@@ -70,7 +70,7 @@ def __validate_standard_name(value, handler, info) -> "StandardName":
7070
data_scale = parent.attrs.get('DATA_SCALE', None)
7171
if data_scale is not None:
7272
# scale_ds = parent.rootparent[data_scale]
73-
ds_scale_units = data_scale.attrs.raw.get('units', '')
73+
ds_scale_units = parent.rootparent[data_scale].attrs.get('units', '')
7474
ureg = get_ureg()
7575
units = str(ureg.Unit(ds_scale_units) * units)
7676

h5rdmtoolbox/wrapper/h5attr.py

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,6 @@ def __str__(self) -> str:
8686
return self.__repr__()
8787

8888

89-
# def Attribute(value, definition: Optional[str] = None,
90-
# rdf_predicate=None, rdf_object=None) -> AttributeValue:
91-
# """attribute interface function"""
92-
# return AttributeValue(
93-
# value,
94-
# definition,
95-
# rdf_predicate,
96-
# rdf_object
97-
# )
98-
99-
10089
def pop_hdf_attributes(attrs: Dict) -> Dict:
10190
"""Remove HDF attributes like NAME, CLASS, .... from the input dictionary
10291
@@ -156,29 +145,29 @@ def _parse_return_value(_id, ret):
156145
if isinstance(v, str):
157146
if not v:
158147
dictionary[k] = ''
159-
else:
160-
if v[0] == '/':
161-
if isinstance(_id, h5py.h5g.GroupID):
162-
rootgrp = get_rootparent(h5py.Group(_id))
163-
dictionary[k] = rootgrp.get(v)
164-
elif isinstance(_id, h5py.h5d.DatasetID):
165-
rootgrp = get_rootparent(h5py.Dataset(_id).parent)
166-
dictionary[k] = rootgrp.get(v)
148+
# else:
149+
# if v[0] == '/':
150+
# if isinstance(_id, h5py.h5g.GroupID):
151+
# rootgrp = get_rootparent(h5py.Group(_id))
152+
# dictionary[k] = rootgrp.get(v)
153+
# elif isinstance(_id, h5py.h5d.DatasetID):
154+
# rootgrp = get_rootparent(h5py.Dataset(_id).parent)
155+
# dictionary[k] = rootgrp.get(v)
167156
return dictionary
168-
if ret[0] == '/':
169-
# it may be group or dataset path or actually just a filepath stored by the user
170-
if isinstance(_id, h5py.h5g.GroupID):
171-
# call like this, otherwise recursive call!
172-
from .core import Group
173-
rootgrp = get_rootparent(Group(_id))
174-
if rootgrp.get(ret) is None:
175-
# not a dataset or group, maybe just a filename that has been stored
176-
return ret
177-
return rootgrp.get(ret)
178-
else:
179-
from .core import Dataset
180-
rootgrp = get_rootparent(Dataset(_id).parent)
181-
return rootgrp.get(ret)
157+
# if ret[0] == '/':
158+
# # it may be group or dataset path or actually just a filepath stored by the user
159+
# if isinstance(_id, h5py.h5g.GroupID):
160+
# # call like this, otherwise recursive call!
161+
# from .core import Group
162+
# rootgrp = get_rootparent(Group(_id))
163+
# if rootgrp.get(ret) is None:
164+
# # not a dataset or group, maybe just a filename that has been stored
165+
# return ret
166+
# return rootgrp.get(ret)
167+
# else:
168+
# from .core import Dataset
169+
# rootgrp = get_rootparent(Dataset(_id).parent)
170+
# return rootgrp.get(ret)
182171
if ret[0] == '(':
183172
if ret[-1] == ')':
184173
# might be a tuple object

tests/wrapper/test_core.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,16 @@ def test_setattr(self):
147147
h5.attrs['none_attr'] = None
148148
self.assertEqual(h5.attrs['none_attr'], 'None')
149149

150+
def test_filename_attr(self):
151+
with h5tbx.File() as h5:
152+
153+
h5.create_dataset('ds', data=np.arange(10))
154+
h5['ds'].attrs['filename'] = '/path/to/file.csv'
155+
self.assertEqual(h5['ds'].attrs['filename'], '/path/to/file.csv')
156+
157+
h5.attrs['filename'] = '/path/to/file.csv'
158+
self.assertEqual(h5.attrs['filename'], '/path/to/file.csv')
159+
150160
def test_rootparent(self):
151161
with h5tbx.File() as h5:
152162
g = h5.create_group('g')
@@ -541,15 +551,15 @@ def test_special_attribute_types(self):
541551

542552
# testing links:
543553
obj.attrs['link_to_group'] = h5['/']
544-
self.assertEqual(obj.attrs['link_to_group'], h5['/'])
545-
self.assertIsInstance(obj.attrs['link_to_group'], h5py.Group)
554+
self.assertEqual(obj.attrs['link_to_group'], '/')
555+
self.assertIsInstance(obj.attrs['link_to_group'], str)
546556
obj.attrs['link_to_ds'] = ds
547557
self.assertEqual(obj.attrs['link_to_ds'], ds)
548-
self.assertIsInstance(obj.attrs['link_to_ds'], h5py.Dataset)
558+
self.assertIsInstance(obj.attrs['link_to_ds'], str)
549559
obj.attrs['attribute_of_links_to_ds'] = {'ds': ds, 'grp': grp, 'astr': 'test', 'afloat': 3.1}
550560
self.assertIsInstance(obj.attrs['attribute_of_links_to_ds'], dict)
551-
self.assertIsInstance(obj.attrs['attribute_of_links_to_ds']['ds'], h5py.Dataset)
552-
self.assertIsInstance(obj.attrs['attribute_of_links_to_ds']['grp'], h5py.Group)
561+
self.assertIsInstance(obj.attrs['attribute_of_links_to_ds']['ds'], str)
562+
self.assertIsInstance(obj.attrs['attribute_of_links_to_ds']['grp'], str)
553563
self.assertIsInstance(obj.attrs['attribute_of_links_to_ds']['astr'], str)
554564
self.assertIsInstance(obj.attrs['attribute_of_links_to_ds']['afloat'], float)
555565

@@ -920,7 +930,7 @@ def test_attr_group_link_and_xarray(self):
920930

921931
ds.attrs['link to grp'] = grp
922932

923-
self.assertIsInstance(ds.attrs['link to grp'], h5py.Group)
933+
self.assertIsInstance(ds.attrs['link to grp'], str)
924934

925935
da = ds[()]
926936
self.assertIsInstance(da, xr.DataArray)

tests/wrapper/test_rdf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ def test_set_single_PSO(self):
252252
method_grp.rdf.subject = M4I.Method
253253
method_grp.attrs['has_participants', OBO.RO_0000057] = h5['contact_person'] # has participants
254254

255-
self.assertEqual(method_grp.attrs['has_participants'], h5['contact_person'])
255+
self.assertEqual(method_grp.attrs['has_participants'], h5['contact_person'].name)
256256

257257
grp.attrs['arbitrary'] = 'arbitrary'
258258
self.assertEqual(grp.rdf.predicate.get('arbitrary', 'invalid'), 'invalid')

0 commit comments

Comments
 (0)