Skip to content

Commit

Permalink
Add ER write capability to IO backend (#910)
Browse files Browse the repository at this point in the history
  • Loading branch information
mavaylon1 authored Jul 20, 2023
1 parent 1ce34d8 commit b436908
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# HDMF Changelog

## HDMF 3.7.1 (Upcoming)
## HDMF 3.8.0 (Upcoming)

### New features and minor improvements
- Added the ability to write ExternalResources if the path is provided and the container has a linked instance of ExternalResources. @mavaylon1 [#910](https://github.com/hdmf-dev/hdmf/pull/910)

### Bug fixes
- Fixed bug on `add_ref_term_set` in which attributes that were not subscribtable returned an error. @mavaylon1 [#909](https://github.com/hdmf-dev/hdmf/pull/909)
Expand Down
8 changes: 8 additions & 0 deletions src/hdmf/backends/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ def write(self, **kwargs):
f_builder = self.__manager.build(container, source=self.__source, root=True)
self.write_builder(f_builder, **kwargs)

if self.external_resources_path is not None:
external_resources = container.get_linked_resources()
if external_resources is not None:
external_resources.to_norm_tsv(path=self.external_resources_path)
else:
msg = "Could not find linked ExternalResources. Container was still written to IO source."
warn(msg)

@docval({'name': 'src_io', 'type': 'HDMFIO', 'doc': 'the HDMFIO object for reading the data to export'},
{'name': 'container', 'type': Container,
'doc': ('the Container object to export. If None, then the entire contents of the HDMFIO object will be '
Expand Down
35 changes: 35 additions & 0 deletions tests/unit/test_io_hdf5_h5tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,41 @@ def test_io_read_external_resources_value_warn(self):

self.remove_er_files()

def test_io_write_er(self):
er = ExternalResources()
self.foofile.link_resources(er)

data = Data(name="species", data=['Homo sapiens', 'Mus musculus'])
er.add_ref(file=self.foofile,
container=data,
key='key1',
entity_id='entity_id1',
entity_uri='entity1')

with HDF5IO(self.path, manager=self.manager, mode='w', external_resources_path='./') as io:
io.write(self.foofile)

with HDF5IO(self.path, manager=self.manager, mode='r', external_resources_path='./') as io:
container = io.read()
self.assertIsInstance(io.external_resources, ExternalResources)
self.assertIsInstance(container.get_linked_resources(), ExternalResources)

self.remove_er_files()

def test_io_warn(self):
er = ExternalResources()

data = Data(name="species", data=['Homo sapiens', 'Mus musculus'])
er.add_ref(file=self.foofile,
container=data,
key='key1',
entity_id='entity_id1',
entity_uri='entity1')
with HDF5IO(self.path, manager=self.manager, mode='w', external_resources_path='./') as io:
with self.assertWarns(Warning):
io.write(self.foofile)


class TestMultiWrite(TestCase):

def setUp(self):
Expand Down

0 comments on commit b436908

Please sign in to comment.