|
| 1 | +from datetime import datetime |
| 2 | +from dateutil.tz import tzlocal, tzutc |
| 3 | +import os |
| 4 | +from abc import ABCMeta, abstractmethod |
| 5 | +import warnings |
| 6 | + |
| 7 | +from pynwb import NWBFile, NWBHDF5IO, validate as pynwb_validate |
| 8 | +from .utils import remove_test_file |
| 9 | +from hdmf.backends.warnings import BrokenLinkWarning |
| 10 | +from hdmf.build.warnings import MissingRequiredWarning, OrphanContainerWarning |
| 11 | + |
| 12 | + |
| 13 | +class TestNWBH5IOMixin(metaclass=ABCMeta): |
| 14 | + """ |
| 15 | + Mixin class for methods to run a roundtrip test writing an NWB file with an Container and reading the Container |
| 16 | + from the NWB file. The setUp, test_roundtrip, and tearDown methods will be run by unittest. |
| 17 | +
|
| 18 | + The abstract methods setUpContainer, addContainer, and getContainer needs to be implemented by classes that include |
| 19 | + this mixin. |
| 20 | +
|
| 21 | + Example: |
| 22 | + class TestMyContainerIO(TestNWBH5IOMixin, TestCase): |
| 23 | + def setUpContainer(self): |
| 24 | + # return a test Container to read/write |
| 25 | + def addContainer(self, nwbfile): |
| 26 | + # add the test Container to an NWB file |
| 27 | + def getContainer(self, nwbfile): |
| 28 | + # return the test Container from an NWB file |
| 29 | +
|
| 30 | + This code is adapted from hdmf.testing.TestH5RoundTripMixin. |
| 31 | + """ |
| 32 | + |
| 33 | + def setUp(self): |
| 34 | + self.container = self.setUpContainer() |
| 35 | + self.start_time = datetime(1971, 1, 1, 12, tzinfo=tzutc()) |
| 36 | + self.create_date = datetime(2018, 4, 15, 12, tzinfo=tzlocal()) |
| 37 | + self.container_type = self.container.__class__.__name__ |
| 38 | + self.filename = 'test_%s.nwb' % self.container_type |
| 39 | + self.writer = None |
| 40 | + self.reader = None |
| 41 | + |
| 42 | + def tearDown(self): |
| 43 | + if self.writer is not None: |
| 44 | + self.writer.close() |
| 45 | + if self.reader is not None: |
| 46 | + self.reader.close() |
| 47 | + remove_test_file(self.filename) |
| 48 | + |
| 49 | + @abstractmethod |
| 50 | + def setUpContainer(self): |
| 51 | + """ Should return the test Container to read/write """ |
| 52 | + raise NotImplementedError('Cannot run test unless setUpContainer is implemented') |
| 53 | + |
| 54 | + def test_roundtrip(self): |
| 55 | + """ |
| 56 | + Test whether the test Container read from file has the same contents as the original test Container and |
| 57 | + validate the file |
| 58 | + """ |
| 59 | + self.read_container = self.roundtripContainer() |
| 60 | + self.assertIsNotNone(str(self.container)) # added as a test to make sure printing works |
| 61 | + self.assertIsNotNone(str(self.read_container)) |
| 62 | + # make sure we get a completely new object |
| 63 | + self.assertNotEqual(id(self.container), id(self.read_container)) |
| 64 | + self.assertIs(self.read_nwbfile.objects[self.container.object_id], self.read_container) |
| 65 | + self.assertContainerEqual(self.read_container, self.container) |
| 66 | + |
| 67 | + def roundtripContainer(self, cache_spec=False): |
| 68 | + """ |
| 69 | + Add the test Container to an NWBFile, write it to file, read the file, and return the test Container from the |
| 70 | + file |
| 71 | + """ |
| 72 | + description = 'a file to test writing and reading a %s' % self.container_type |
| 73 | + identifier = 'TEST_%s' % self.container_type |
| 74 | + nwbfile = NWBFile(description, identifier, self.start_time, file_create_date=self.create_date) |
| 75 | + self.addContainer(nwbfile) |
| 76 | + |
| 77 | + with warnings.catch_warnings(record=True) as ws: |
| 78 | + self.writer = NWBHDF5IO(self.filename, mode='w') |
| 79 | + self.writer.write(nwbfile, cache_spec=cache_spec) |
| 80 | + self.writer.close() |
| 81 | + |
| 82 | + self.validate() |
| 83 | + |
| 84 | + self.reader = NWBHDF5IO(self.filename, mode='r') |
| 85 | + self.read_nwbfile = self.reader.read() |
| 86 | + |
| 87 | + if ws: |
| 88 | + for w in ws: |
| 89 | + if issubclass(w.category, (MissingRequiredWarning, |
| 90 | + OrphanContainerWarning, |
| 91 | + BrokenLinkWarning)): |
| 92 | + raise Exception('%s: %s' % (w.category.__name__, w.message)) |
| 93 | + else: |
| 94 | + warnings.warn(w.message, w.category) |
| 95 | + |
| 96 | + try: |
| 97 | + return self.getContainer(self.read_nwbfile) |
| 98 | + except Exception as e: |
| 99 | + self.reader.close() |
| 100 | + self.reader = None |
| 101 | + raise e |
| 102 | + |
| 103 | + @abstractmethod |
| 104 | + def addContainer(self, nwbfile): |
| 105 | + """ Should add the test Container to the given NWBFile """ |
| 106 | + raise NotImplementedError('Cannot run test unless addContainer is implemented') |
| 107 | + |
| 108 | + @abstractmethod |
| 109 | + def getContainer(self, nwbfile): |
| 110 | + """ Should return the test Container from the given NWBFile """ |
| 111 | + raise NotImplementedError('Cannot run test unless getContainer is implemented') |
| 112 | + |
| 113 | + def validate(self): |
| 114 | + """ Validate the created file """ |
| 115 | + if os.path.exists(self.filename): |
| 116 | + with NWBHDF5IO(self.filename, mode='r') as io: |
| 117 | + errors = pynwb_validate(io) |
| 118 | + if errors: |
| 119 | + for err in errors: |
| 120 | + raise Exception(err) |
| 121 | + |
| 122 | + |
| 123 | +class TestAcquisitionH5IOMixin(TestNWBH5IOMixin): |
| 124 | + """ |
| 125 | + Mixin class for methods to run a roundtrip test writing an NWB file with an Container as an acquisition and reading |
| 126 | + the Container as an acquisition from the NWB file. The setUp, test_roundtrip, and tearDown methods will be run by |
| 127 | + unittest. |
| 128 | +
|
| 129 | + The abstract method setUpContainer needs to be implemented by classes that include this mixin. |
| 130 | +
|
| 131 | + Example: |
| 132 | + class TestMyContainerIO(TestNWBH5IOMixin, TestCase): |
| 133 | + def setUpContainer(self): |
| 134 | + # return a test Container to read/write |
| 135 | +
|
| 136 | + This code is adapted from hdmf.testing.TestH5RoundTripMixin. |
| 137 | + """ |
| 138 | + |
| 139 | + def addContainer(self, nwbfile): |
| 140 | + ''' Add an NWBDataInterface object to the file as an acquisition ''' |
| 141 | + nwbfile.add_acquisition(self.container) |
| 142 | + |
| 143 | + def getContainer(self, nwbfile): |
| 144 | + ''' Get the NWBDataInterface object from the file ''' |
| 145 | + return nwbfile.get_acquisition(self.container.name) |
0 commit comments