From abbf12a9f4eb6fdb3d82a5fea465318c3db04ccd Mon Sep 17 00:00:00 2001 From: mavaylon1 Date: Tue, 26 Sep 2023 07:26:48 -0700 Subject: [PATCH] checkpoint/remove field_name --- src/hdmf/common/resources.py | 26 ++++++++++++++++++++------ src/hdmf/term_set.py | 9 +-------- tests/unit/common/test_resources.py | 9 +++++---- tests/unit/common/test_table.py | 19 +++++++++---------- tests/unit/helpers/utils.py | 6 +++--- tests/unit/test_io_hdf5_h5tools.py | 10 ++-------- tests/unit/test_term_set.py | 10 +++------- 7 files changed, 43 insertions(+), 46 deletions(-) diff --git a/src/hdmf/common/resources.py b/src/hdmf/common/resources.py index acc7c4cf8..15990d96e 100644 --- a/src/hdmf/common/resources.py +++ b/src/hdmf/common/resources.py @@ -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'] @@ -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, @@ -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 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: @@ -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) @@ -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'] @@ -599,6 +606,12 @@ 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 @@ -606,6 +619,7 @@ def add_ref(self, **kwargs): 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) diff --git a/src/hdmf/term_set.py b/src/hdmf/term_set.py index 767bebf15..7bdcd8213 100644 --- a/src/hdmf/term_set.py +++ b/src/hdmf/term_set.py @@ -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): @@ -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') diff --git a/tests/unit/common/test_resources.py b/tests/unit/common/test_resources.py index a43436f77..f6761f262 100644 --- a/tests/unit/common/test_resources.py +++ b/tests/unit/common/test_resources.py @@ -276,7 +276,7 @@ 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() @@ -284,7 +284,8 @@ def test_check_termset_wrapper(self): 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): @@ -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]) @@ -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]) diff --git a/tests/unit/common/test_table.py b/tests/unit/common/test_table.py index 1f44021d6..c398981d4 100644 --- a/tests/unit/common/test_table.py +++ b/tests/unit/common/test_table.py @@ -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'}} @@ -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") @@ -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') @@ -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): @@ -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): diff --git a/tests/unit/helpers/utils.py b/tests/unit/helpers/utils.py index a97106560..bb37014ad 100644 --- a/tests/unit/helpers/utils.py +++ b/tests/unit/helpers/utils.py @@ -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): diff --git a/tests/unit/test_io_hdf5_h5tools.py b/tests/unit/test_io_hdf5_h5tools.py index 25bd7bc05..4e7d8d4ae 100644 --- a/tests/unit/test_io_hdf5_h5tools.py +++ b/tests/unit/test_io_hdf5_h5tools.py @@ -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]) @@ -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() @@ -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]) @@ -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]) diff --git a/tests/unit/test_term_set.py b/tests/unit/test_term_set.py index 5bbc70b47..9e25a9ea9 100644 --- a/tests/unit/test_term_set.py +++ b/tests/unit/test_term_set.py @@ -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', @@ -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): @@ -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'] ) @@ -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))