Skip to content

Commit b436908

Browse files
authored
Add ER write capability to IO backend (#910)
1 parent 1ce34d8 commit b436908

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# HDMF Changelog
22

3-
## HDMF 3.7.1 (Upcoming)
3+
## HDMF 3.8.0 (Upcoming)
4+
5+
### New features and minor improvements
6+
- 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)
47

58
### Bug fixes
69
- 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)

src/hdmf/backends/io.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ def write(self, **kwargs):
8282
f_builder = self.__manager.build(container, source=self.__source, root=True)
8383
self.write_builder(f_builder, **kwargs)
8484

85+
if self.external_resources_path is not None:
86+
external_resources = container.get_linked_resources()
87+
if external_resources is not None:
88+
external_resources.to_norm_tsv(path=self.external_resources_path)
89+
else:
90+
msg = "Could not find linked ExternalResources. Container was still written to IO source."
91+
warn(msg)
92+
8593
@docval({'name': 'src_io', 'type': 'HDMFIO', 'doc': 'the HDMFIO object for reading the data to export'},
8694
{'name': 'container', 'type': Container,
8795
'doc': ('the Container object to export. If None, then the entire contents of the HDMFIO object will be '

tests/unit/test_io_hdf5_h5tools.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,41 @@ def test_io_read_external_resources_value_warn(self):
10201020

10211021
self.remove_er_files()
10221022

1023+
def test_io_write_er(self):
1024+
er = ExternalResources()
1025+
self.foofile.link_resources(er)
1026+
1027+
data = Data(name="species", data=['Homo sapiens', 'Mus musculus'])
1028+
er.add_ref(file=self.foofile,
1029+
container=data,
1030+
key='key1',
1031+
entity_id='entity_id1',
1032+
entity_uri='entity1')
1033+
1034+
with HDF5IO(self.path, manager=self.manager, mode='w', external_resources_path='./') as io:
1035+
io.write(self.foofile)
1036+
1037+
with HDF5IO(self.path, manager=self.manager, mode='r', external_resources_path='./') as io:
1038+
container = io.read()
1039+
self.assertIsInstance(io.external_resources, ExternalResources)
1040+
self.assertIsInstance(container.get_linked_resources(), ExternalResources)
1041+
1042+
self.remove_er_files()
1043+
1044+
def test_io_warn(self):
1045+
er = ExternalResources()
1046+
1047+
data = Data(name="species", data=['Homo sapiens', 'Mus musculus'])
1048+
er.add_ref(file=self.foofile,
1049+
container=data,
1050+
key='key1',
1051+
entity_id='entity_id1',
1052+
entity_uri='entity1')
1053+
with HDF5IO(self.path, manager=self.manager, mode='w', external_resources_path='./') as io:
1054+
with self.assertWarns(Warning):
1055+
io.write(self.foofile)
1056+
1057+
10231058
class TestMultiWrite(TestCase):
10241059

10251060
def setUp(self):

0 commit comments

Comments
 (0)