Skip to content

Commit

Permalink
checkpoint/remove field_name
Browse files Browse the repository at this point in the history
  • Loading branch information
mavaylon1 committed Sep 26, 2023
1 parent 4f5d833 commit abbf12a
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 46 deletions.
26 changes: 20 additions & 6 deletions src/hdmf/common/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ def _get_file_from_container(self, **kwargs):
def __check_termset_wrapper(self, **kwargs):
"""
Takes a list of objects and checks the fields for TermSetWrapper.
:return: [[object, wrapper1], [object, wrapper2], ...]
:return: [[object, attribute_name, wrapper1], [object, attribute_name, wrapper2], ...]
"""
objects = kwargs['objects']

Expand All @@ -423,11 +423,14 @@ def __check_termset_wrapper(self, **kwargs):
for obj in objects:
# Get all the fields, parse out the methods and internal variables
obj_fields = [a for a in dir(obj) if not a.startswith('_') and not callable(getattr(obj, a))]

for attribute in obj_fields:
attr = getattr(obj, attribute)
if isinstance(attr, TermSetWrapper):
# breakpoint()
# Search objects that are wrapped
ret.append([obj, attr])
ret.append([obj, attribute, attr])
# breakpoint()
return ret

@docval({'name': 'root_container', 'type': HERDManager,
Expand All @@ -443,8 +446,8 @@ def add_ref_term_set(self, **kwargs):
all_objects = root_container.all_children() # list of child objects and the container itslef

Check failure on line 446 in src/hdmf/common/resources.py

View workflow job for this annotation

GitHub Actions / Check for spelling errors

itslef ==> itself

add_ref_items = self.__check_termset_wrapper(objects=all_objects)
for ref_pairs in add_ref_items:
container, wrapper = ref_pairs
for ref in add_ref_items:
container, attr_name, wrapper = ref
if isinstance(wrapper.value, (list, np.ndarray, tuple)):
values = wrapper.value
else:
Expand All @@ -456,7 +459,7 @@ def add_ref_term_set(self, **kwargs):
entity_uri = term_info[2]
self.add_ref(file=root_container,
container=container,
attribute=wrapper.field_name,
attribute=attr_name,
key=term,
entity_id=entity_id,
entity_uri=entity_uri)
Expand Down Expand Up @@ -544,8 +547,12 @@ def add_ref(self, **kwargs):
###############################################################
container = kwargs['container']
attribute = kwargs['attribute']
if attribute == 'data':
if container.__class__.__name__ == 'VectorData':
attribute = None
# if attribute == 'my_data':
# attribute = None
# if attribute == 'data':
# attribute = None
key = kwargs['key']
field = kwargs['field']
entity_id = kwargs['entity_id']
Expand Down Expand Up @@ -599,13 +606,20 @@ def add_ref(self, **kwargs):
relative_path=relative_path,
field=field)

# try:
# # key_name, container, relative_path, field
# key = self.get_key(key_name=key, container=container)
# except ValueError:
# pass

if not isinstance(key, Key):
key_idx_matches = self.keys.which(key=key)
# if same key is used multiple times, determine
# which instance based on the Container
for row_idx in self.object_keys.which(objects_idx=object_field.idx):
key_idx = self.object_keys['keys_idx', row_idx]
if key_idx in key_idx_matches:
breakpoint()
msg = "Use Key Object when referencing an existing (container, relative_path, key)"
raise ValueError(msg)

Expand Down
9 changes: 1 addition & 8 deletions src/hdmf/term_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,10 @@ class TermSetWrapper:
{'name': 'value',
'type': (list, np.ndarray, dict, str, tuple),
'doc': 'The target item that is wrapped, either data or attribute.'},
{'name': 'field_name',
'type': str,
'doc': 'The name of the field being wrapped.'})
)
def __init__(self, **kwargs):
self.__value = kwargs['value']
self.__termset = kwargs['termset']
self.__field_name = kwargs['field_name']
self.__validate()

def __validate(self):
Expand Down Expand Up @@ -220,10 +217,6 @@ def value(self):
def termset(self):
return self.__termset

@property
def field_name(self):
return self.__field_name

@property
def dtype(self):
return self.__getattr__('dtype')
Expand Down
9 changes: 5 additions & 4 deletions tests/unit/common/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,15 +276,16 @@ def test_check_termset_wrapper(self):
col1 = VectorData(
name='Species_1',
description='...',
data=TermSetWrapper(value=['Homo sapiens'], field_name='data', termset=terms)
data=TermSetWrapper(value=['Homo sapiens'], termset=terms)
)
species = DynamicTable(name='species', description='My species', columns=[col1])
objs = species.all_children()

er = HERD()
ret = er._HERD__check_termset_wrapper(objs)
self.assertEqual(ret[0][0].__class__.__name__, 'VectorData')
self.assertEqual(ret[0][1].__class__.__name__, 'TermSetWrapper')
self.assertEqual(ret[0][1], 'data')
self.assertEqual(ret[0][2].__class__.__name__, 'TermSetWrapper')

@unittest.skipIf(not LINKML_INSTALLED, "optional LinkML module is not installed")
def test_add_ref_termset_data(self):
Expand All @@ -297,7 +298,7 @@ def test_add_ref_termset_data(self):
col1 = VectorData(
name='Species_1',
description='...',
data=TermSetWrapper(value=['Homo sapiens'], field_name='data', termset=terms)
data=TermSetWrapper(value=['Homo sapiens'], termset=terms)
)
species = DynamicTable(name='species', description='My species', columns=[col1])

Expand All @@ -319,7 +320,7 @@ def test_add_ref_termset_attr(self):
# create children and add parent
col1 = VectorData(
name='Species_1',
description=TermSetWrapper(value='Homo sapiens', field_name='description', termset=terms),
description=TermSetWrapper(value='Homo sapiens', termset=terms),
data=['Human']
)
species = DynamicTable(name='species', description='My species', columns=[col1])
Expand Down
19 changes: 9 additions & 10 deletions tests/unit/common/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ def test_add_col_validate(self):
col1 = VectorData(
name='Species_1',
description='...',
data=TermSetWrapper(value=['Homo sapiens'], field_name='data', termset=terms)
data=TermSetWrapper(value=['Homo sapiens'], termset=terms)
)
species = DynamicTable(name='species', description='My species', columns=[col1])
species.add_column(name='Species_2',
description='Species data',
data=TermSetWrapper(value=['Mus musculus'], field_name='data', termset=terms))
data=TermSetWrapper(value=['Mus musculus'], termset=terms))
expected_df_data = \
{'Species_1': {0: 'Homo sapiens'},
'Species_2': {0: 'Mus musculus'}}
Expand All @@ -143,14 +143,13 @@ def test_add_col_validate_bad_data(self):
col1 = VectorData(
name='Species_1',
description='...',
data=TermSetWrapper(value=['Homo sapiens'], field_name='data', termset=terms)
data=TermSetWrapper(value=['Homo sapiens'], termset=terms)
)
species = DynamicTable(name='species', description='My species', columns=[col1])
with self.assertRaises(ValueError):
species.add_column(name='Species_2',
description='Species data',
data=TermSetWrapper(value=['bad data'],
field_name='data',
termset=terms))

@unittest.skipIf(not LINKML_INSTALLED, "optional LinkML module is not installed")
Expand All @@ -159,12 +158,12 @@ def test_add_row_validate(self):
col1 = VectorData(
name='Species_1',
description='...',
data=TermSetWrapper(value=['Homo sapiens'], field_name='data', termset=terms)
data=TermSetWrapper(value=['Homo sapiens'], termset=terms)
)
col2 = VectorData(
name='Species_2',
description='...',
data=TermSetWrapper(value=['Mus musculus'], field_name='data', termset=terms)
data=TermSetWrapper(value=['Mus musculus'], termset=terms)
)
species = DynamicTable(name='species', description='My species', columns=[col1,col2])
species.add_row(Species_1='Myrmecophaga tridactyla', Species_2='Ursus arctos horribilis')
Expand All @@ -181,12 +180,12 @@ def test_add_row_validate_bad_data_one_col(self):
col1 = VectorData(
name='Species_1',
description='...',
data=TermSetWrapper(value=['Homo sapiens'], field_name='data', termset=terms)
data=TermSetWrapper(value=['Homo sapiens'], termset=terms)
)
col2 = VectorData(
name='Species_2',
description='...',
data=TermSetWrapper(value=['Mus musculus'], field_name='data', termset=terms)
data=TermSetWrapper(value=['Mus musculus'], termset=terms)
)
species = DynamicTable(name='species', description='My species', columns=[col1,col2])
with self.assertRaises(ValueError):
Expand All @@ -198,12 +197,12 @@ def test_add_row_validate_bad_data_all_col(self):
col1 = VectorData(
name='Species_1',
description='...',
data=TermSetWrapper(value=['Homo sapiens'], field_name='data', termset=terms)
data=TermSetWrapper(value=['Homo sapiens'], termset=terms)
)
col2 = VectorData(
name='Species_2',
description='...',
data=TermSetWrapper(value=['Mus musculus'], field_name='data', termset=terms)
data=TermSetWrapper(value=['Mus musculus'], termset=terms)
)
species = DynamicTable(name='species', description='My species', columns=[col1,col2])
with self.assertRaises(ValueError):
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/helpers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ def __str__(self):
def my_data(self):
return self.__data

@property
def data(self):
return self.__data
# @property
# def data(self):
# return self.__data

@property
def attr1(self):
Expand Down
10 changes: 2 additions & 8 deletions tests/unit/test_io_hdf5_h5tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def test_write_dataset_string(self):
##########################################
def test_write_dataset_TermSetWrapper(self):
terms = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml')
a = TermSetWrapper(value=['Homo sapiens'], field_name='data', termset=terms)
a = TermSetWrapper(value=['Homo sapiens'], termset=terms)
self.io.write_dataset(self.f, DatasetBuilder('test_dataset', a, attributes={}))
dset = self.f['test_dataset']
self.assertEqual(dset[0].decode('utf-8'), a.value[0])
Expand Down Expand Up @@ -821,15 +821,12 @@ def test_roundtrip_TermSetWrapper_dataset(self):
terms = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml')
foo = Foo(name="species", attr1='attr1', attr2=0,
my_data=TermSetWrapper(value=['Homo sapiens', 'Mus musculus'],
field_name='data',
termset=terms))
foobucket = FooBucket('bucket1', [foo])
foofile = FooFile(buckets=[foobucket])

# with HDF5IO(self.path, manager=self.manager, mode='w') as io:
# io.write(foofile)
with HDF5IO(self.path, manager=self.manager, mode='w', herd_path='./HERD.zip') as io:
io.write(foofile, herd=er)
io.write(foofile)
#
# with HDF5IO(self.path, manager=self.manager, mode='r') as io:
# read_foofile = io.read()
Expand Down Expand Up @@ -1059,9 +1056,7 @@ def test_io_write_extend_herd(self):
terms = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml')
foo = Foo(name="foo", attr1='attr1', attr2=0,
my_data=TermSetWrapper(value=['Homo sapiens', 'Mus musculus'],
field_name='my_data',
termset=terms))
# breakpoint()
foobucket = FooBucket('bucket1', [foo])
file = FooFile(buckets=[foobucket])

Expand All @@ -1088,7 +1083,6 @@ def test_io_write_create_herd(self):
terms = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml')
foo = Foo(name="my_data", attr1='attr1', attr2=0,
my_data=TermSetWrapper(value=['Homo sapiens', 'Mus musculus'],
field_name='data',
termset=terms))
foobucket = FooBucket('bucket1', [foo])
file = FooFile(buckets=[foobucket])
Expand Down
10 changes: 3 additions & 7 deletions tests/unit/test_term_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ class TestTermSetWrapper(TestCase):
def setUp(self):
self.termset = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml')

self.wrapped_array = TermSetWrapper(value=np.array(['Homo sapiens']), termset=self.termset, field_name='data')
self.wrapped_list = TermSetWrapper(value=['Homo sapiens'], termset=self.termset, field_name='data')
self.wrapped_array = TermSetWrapper(value=np.array(['Homo sapiens']), termset=self.termset)
self.wrapped_list = TermSetWrapper(value=['Homo sapiens'], termset=self.termset)

self.np_data = VectorData(
name='Species_1',
Expand All @@ -128,7 +128,6 @@ def setUp(self):
def test_properties(self):
self.assertEqual(self.wrapped_array.value, ['Homo sapiens'])
self.assertEqual(self.wrapped_array.termset.view_set, self.termset.view_set)
self.assertEqual(self.wrapped_list.field_name, 'data')
self.assertEqual(self.wrapped_array.dtype, 'U12') # this covers __getattr__

def test_get_item(self):
Expand All @@ -148,14 +147,12 @@ def test_validate_error(self):
data = VectorData(name='Species_1',
description='...',
data=TermSetWrapper(value=['Missing Term'],
termset=self.termset,
field_name='data'))
termset=self.termset))

def test_wrapper_validate_attribute(self):
col1 = VectorData(
name='Species_1',
description=TermSetWrapper(value='Homo sapiens',
field_name='description',
termset=self.termset),
data=['Human']
)
Expand All @@ -166,7 +163,6 @@ def test_wrapper_validate_dataset(self):
name='Species_1',
description='...',
data=TermSetWrapper(value=['Homo sapiens'],
field_name='data',
termset=self.termset)
)
self.assertTrue(isinstance(col1.data, TermSetWrapper))
Expand Down

0 comments on commit abbf12a

Please sign in to comment.