|
| 1 | +from hdmf.build import BuildManager |
| 2 | +from hdmf.common import VectorData |
| 3 | +from hdmf.utils import docval, get_docval, popargs |
| 4 | + |
| 5 | +from pynwb import NWBFile |
| 6 | +from pynwb.spec import NWBDatasetSpec, NWBAttributeSpec |
| 7 | +from pynwb.testing import NWBH5IOFlexMixin, TestCase |
| 8 | + |
| 9 | +from ..helpers.utils import create_test_extension |
| 10 | + |
| 11 | + |
| 12 | +class TestDynamicTableCustomColumnWithArgs(NWBH5IOFlexMixin, TestCase): |
| 13 | + |
| 14 | + class SubVectorData(VectorData): |
| 15 | + __fields__ = ('extra_kwarg', ) |
| 16 | + |
| 17 | + @docval( |
| 18 | + *get_docval(VectorData.__init__, "name", "description", "data"), |
| 19 | + {'name': 'extra_kwarg', 'type': 'str', 'doc': 'An extra kwarg.'}, |
| 20 | + ) |
| 21 | + def __init__(self, **kwargs): |
| 22 | + extra_kwarg = popargs('extra_kwarg', kwargs) |
| 23 | + super().__init__(**kwargs) |
| 24 | + self.extra_kwarg = extra_kwarg |
| 25 | + |
| 26 | + def setUp(self): |
| 27 | + """Set up an extension with a custom VectorData column.""" |
| 28 | + |
| 29 | + spec = NWBDatasetSpec( |
| 30 | + neurodata_type_def='SubVectorData', |
| 31 | + neurodata_type_inc='VectorData', |
| 32 | + doc='A custom VectorData column.', |
| 33 | + dtype='text', |
| 34 | + shape=(None,), |
| 35 | + attributes=[ |
| 36 | + NWBAttributeSpec( |
| 37 | + name="extra_kwarg", |
| 38 | + doc='An extra kwarg.', |
| 39 | + dtype='text' |
| 40 | + ), |
| 41 | + ], |
| 42 | + ) |
| 43 | + |
| 44 | + self.type_map = create_test_extension([spec], {"SubVectorData": self.SubVectorData}) |
| 45 | + self.manager = BuildManager(self.type_map) |
| 46 | + super().setUp() |
| 47 | + |
| 48 | + def get_manager(self): |
| 49 | + return self.manager |
| 50 | + |
| 51 | + def getContainerType(self): |
| 52 | + return "TrialsWithCustomColumnsWithArgs" |
| 53 | + |
| 54 | + def addContainer(self): |
| 55 | + """ Add the test DynamicTable to the given NWBFile """ |
| 56 | + self.nwbfile.add_trial_column( |
| 57 | + name="test", |
| 58 | + description="test", |
| 59 | + col_cls=self.SubVectorData, |
| 60 | + extra_kwarg="test_extra_kwarg" |
| 61 | + ) |
| 62 | + self.nwbfile.add_trial(start_time=1.0, stop_time=2.0, test="test_data") |
| 63 | + |
| 64 | + def getContainer(self, nwbfile: NWBFile): |
| 65 | + return nwbfile.trials["test"] |
| 66 | + |
| 67 | + def test_roundtrip(self): |
| 68 | + super().test_roundtrip() |
| 69 | + assert isinstance(self.read_container, self.SubVectorData) |
| 70 | + assert self.read_container.extra_kwarg == "test_extra_kwarg" |
| 71 | + |
| 72 | + def test_roundtrip_export(self): |
| 73 | + super().test_roundtrip_export() |
| 74 | + assert isinstance(self.read_container, self.SubVectorData) |
| 75 | + assert self.read_container.extra_kwarg == "test_extra_kwarg" |
0 commit comments