| 1 |
import numpy as np |
| 2 |
import os |
| 3 |
import shutil |
| 4 |
import tempfile |
| 5 |
|
| 6 |
from hdmf.backends.hdf5 import HDF5IO |
| 7 |
from hdmf.build import BuildManager, TypeMap |
| 8 |
from hdmf.common import get_type_map, DynamicTable, VectorData |
| 9 |
from hdmf.spec import GroupSpec, DatasetSpec, SpecCatalog, SpecNamespace, NamespaceCatalog |
| 10 |
from hdmf.testing import TestCase |
| 11 |
from hdmf.validate import ValidatorMap |
| 12 |
|
| 13 |
from tests.unit.helpers.utils import CORE_NAMESPACE |
| 14 |
|
| 15 |
|
| 16 |
class TestDynamicDynamicTable(TestCase): |
| 17 |
|
| 18 |
def setUp(self): |
| 19 |
self.dt_spec = GroupSpec( |
| 20 |
'A test extension that contains a dynamic table', |
| 21 |
data_type_def='TestTable', |
| 22 |
data_type_inc='DynamicTable', |
| 23 |
datasets=[ |
| 24 |
DatasetSpec( |
| 25 |
data_type_inc='VectorData', |
| 26 |
name='my_col', |
| 27 |
doc='a test column', |
| 28 |
dtype='float' |
| 29 |
), |
| 30 |
DatasetSpec( |
| 31 |
data_type_inc='VectorData', |
| 32 |
name='indexed_col', |
| 33 |
doc='a test column', |
| 34 |
dtype='float' |
| 35 |
), |
| 36 |
DatasetSpec( |
| 37 |
data_type_inc='VectorIndex', |
| 38 |
name='indexed_col_index', |
| 39 |
doc='a test column', |
| 40 |
), |
| 41 |
DatasetSpec( |
| 42 |
data_type_inc='VectorData', |
| 43 |
name='optional_col1', |
| 44 |
doc='a test column', |
| 45 |
dtype='float', |
| 46 |
quantity='?', |
| 47 |
), |
| 48 |
DatasetSpec( |
| 49 |
data_type_inc='VectorData', |
| 50 |
name='optional_col2', |
| 51 |
doc='a test column', |
| 52 |
dtype='float', |
| 53 |
quantity='?', |
| 54 |
) |
| 55 |
] |
| 56 |
) |
| 57 |
|
| 58 |
self.dt_spec2 = GroupSpec( |
| 59 |
'A test extension that contains a dynamic table', |
| 60 |
data_type_def='TestDTRTable', |
| 61 |
data_type_inc='DynamicTable', |
| 62 |
datasets=[ |
| 63 |
DatasetSpec( |
| 64 |
data_type_inc='DynamicTableRegion', |
| 65 |
name='ref_col', |
| 66 |
doc='a test column', |
| 67 |
), |
| 68 |
DatasetSpec( |
| 69 |
data_type_inc='DynamicTableRegion', |
| 70 |
name='indexed_ref_col', |
| 71 |
doc='a test column', |
| 72 |
), |
| 73 |
DatasetSpec( |
| 74 |
data_type_inc='VectorIndex', |
| 75 |
name='indexed_ref_col_index', |
| 76 |
doc='a test column', |
| 77 |
), |
| 78 |
DatasetSpec( |
| 79 |
data_type_inc='DynamicTableRegion', |
| 80 |
name='optional_ref_col', |
| 81 |
doc='a test column', |
| 82 |
quantity='?' |
| 83 |
), |
| 84 |
DatasetSpec( |
| 85 |
data_type_inc='DynamicTableRegion', |
| 86 |
name='optional_indexed_ref_col', |
| 87 |
doc='a test column', |
| 88 |
quantity='?' |
| 89 |
), |
| 90 |
DatasetSpec( |
| 91 |
data_type_inc='VectorIndex', |
| 92 |
name='optional_indexed_ref_col_index', |
| 93 |
doc='a test column', |
| 94 |
quantity='?' |
| 95 |
), |
| 96 |
DatasetSpec( |
| 97 |
data_type_inc='VectorData', |
| 98 |
name='optional_col3', |
| 99 |
doc='a test column', |
| 100 |
dtype='float', |
| 101 |
quantity='?', |
| 102 |
) |
| 103 |
] |
| 104 |
) |
| 105 |
|
| 106 |
from hdmf.spec.write import YAMLSpecWriter |
| 107 |
writer = YAMLSpecWriter(outdir='.') |
| 108 |
|
| 109 |
self.spec_catalog = SpecCatalog() |
| 110 |
self.spec_catalog.register_spec(self.dt_spec, 'test.yaml') |
| 111 |
self.spec_catalog.register_spec(self.dt_spec2, 'test.yaml') |
| 112 |
self.namespace = SpecNamespace( |
| 113 |
'a test namespace', CORE_NAMESPACE, |
| 114 |
[ |
| 115 |
dict( |
| 116 |
namespace='hdmf-common', |
| 117 |
), |
| 118 |
dict(source='test.yaml'), |
| 119 |
], |
| 120 |
version='0.1.0', |
| 121 |
catalog=self.spec_catalog |
| 122 |
) |
| 123 |
|
| 124 |
self.test_dir = tempfile.mkdtemp() |
| 125 |
spec_fpath = os.path.join(self.test_dir, 'test.yaml') |
| 126 |
namespace_fpath = os.path.join(self.test_dir, 'test-namespace.yaml') |
| 127 |
writer.write_spec(dict(groups=[self.dt_spec, self.dt_spec2]), spec_fpath) |
| 128 |
writer.write_namespace(self.namespace, namespace_fpath) |
| 129 |
self.namespace_catalog = NamespaceCatalog() |
| 130 |
hdmf_typemap = get_type_map() |
| 131 |
self.type_map = TypeMap(self.namespace_catalog) |
| 132 |
self.type_map.merge(hdmf_typemap, ns_catalog=True) |
| 133 |
self.type_map.load_namespaces(namespace_fpath) |
| 134 |
self.manager = BuildManager(self.type_map) |
| 135 |
|
| 136 |
self.TestTable = self.type_map.get_dt_container_cls('TestTable', CORE_NAMESPACE) |
| 137 |
self.TestDTRTable = self.type_map.get_dt_container_cls('TestDTRTable', CORE_NAMESPACE) |
| 138 |
|
| 139 |
def tearDown(self) -> None: |
| 140 |
shutil.rmtree(self.test_dir) |
| 141 |
|
| 142 |
def test_dynamic_table(self): |
| 143 |
assert issubclass(self.TestTable, DynamicTable) |
| 144 |
|
| 145 |
assert self.TestTable.__columns__[0] == { |
| 146 |
'name': 'my_col', |
| 147 |
'description': 'a test column', |
| 148 |
'class': VectorData, |
| 149 |
'required': True |
| 150 |
} |
| 151 |
|
| 152 |
def test_forbids_incorrect_col(self): |
| 153 |
test_table = self.TestTable(name='test_table', description='my test table') |
| 154 |
|
| 155 |
with self.assertRaises(ValueError): |
| 156 |
test_table.add_row(my_col=3.0, indexed_col=[1.0, 3.0], incorrect_col=5) |
| 157 |
|
| 158 |
def test_dynamic_column(self): |
| 159 |
test_table = self.TestTable(name='test_table', description='my test table') |
| 160 |
test_table.add_column('dynamic_column', 'this is a dynamic column') |
| 161 |
test_table.add_row( |
| 162 |
my_col=3.0, indexed_col=[1.0, 3.0], dynamic_column=4, optional_col2=.5, |
| 163 |
) |
| 164 |
test_table.add_row( |
| 165 |
my_col=4.0, indexed_col=[2.0, 4.0], dynamic_column=4, optional_col2=.5, |
| 166 |
) |
| 167 |
|
| 168 |
np.testing.assert_array_equal(test_table['indexed_col'].target.data, [1., 3., 2., 4.]) |
| 169 |
np.testing.assert_array_equal(test_table['dynamic_column'].data, [4, 4]) |
| 170 |
|
| 171 |
def test_optional_col(self): |
| 172 |
test_table = self.TestTable(name='test_table', description='my test table') |
| 173 |
test_table.add_row(my_col=3.0, indexed_col=[1.0, 3.0], optional_col2=.5) |
| 174 |
test_table.add_row(my_col=4.0, indexed_col=[2.0, 4.0], optional_col2=.5) |
| 175 |
|
| 176 |
def test_dynamic_table_region(self): |
| 177 |
test_table = self.TestTable(name='test_table', description='my test table') |
| 178 |
test_table.add_row(my_col=3.0, indexed_col=[1.0, 3.0], optional_col2=.5) |
| 179 |
test_table.add_row(my_col=4.0, indexed_col=[2.0, 4.0], optional_col2=.5) |
| 180 |
|
| 181 |
test_dtr_table = self.TestDTRTable(name='test_dtr_table', description='my table', |
| 182 |
target_tables={'ref_col': test_table, |
| 183 |
'indexed_ref_col': test_table}) |
| 184 |
self.assertIs(test_dtr_table['ref_col'].table, test_table) |
| 185 |
self.assertIs(test_dtr_table['indexed_ref_col'].target.table, test_table) |
| 186 |
|
| 187 |
test_dtr_table.add_row(ref_col=0, indexed_ref_col=[0, 1]) |
| 188 |
test_dtr_table.add_row(ref_col=0, indexed_ref_col=[0, 1]) |
| 189 |
|
| 190 |
np.testing.assert_array_equal(test_dtr_table['indexed_ref_col'].target.data, [0, 1, 0, 1]) |
| 191 |
np.testing.assert_array_equal(test_dtr_table['ref_col'].data, [0, 0]) |
| 192 |
|
| 193 |
def test_dynamic_table_region_optional(self): |
| 194 |
test_table = self.TestTable(name='test_table', description='my test table') |
| 195 |
test_table.add_row(my_col=3.0, indexed_col=[1.0, 3.0], optional_col2=.5) |
| 196 |
test_table.add_row(my_col=4.0, indexed_col=[2.0, 4.0], optional_col2=.5) |
| 197 |
|
| 198 |
test_dtr_table = self.TestDTRTable(name='test_dtr_table', description='my table', |
| 199 |
target_tables={'optional_ref_col': test_table, |
| 200 |
'optional_indexed_ref_col': test_table}) |
| 201 |
self.assertIs(test_dtr_table['optional_ref_col'].table, test_table) |
| 202 |
self.assertIs(test_dtr_table['optional_indexed_ref_col'].target.table, test_table) |
| 203 |
|
| 204 |
test_dtr_table.add_row(ref_col=0, indexed_ref_col=[0, 1], |
| 205 |
optional_ref_col=0, optional_indexed_ref_col=[0, 1]) |
| 206 |
test_dtr_table.add_row(ref_col=0, indexed_ref_col=[0, 1], |
| 207 |
optional_ref_col=0, optional_indexed_ref_col=[0, 1]) |
| 208 |
|
| 209 |
np.testing.assert_array_equal(test_dtr_table['optional_indexed_ref_col'].target.data, [0, 1, 0, 1]) |
| 210 |
np.testing.assert_array_equal(test_dtr_table['optional_ref_col'].data, [0, 0]) |
| 211 |
|
| 212 |
def test_dynamic_table_region_bad_target_col(self): |
| 213 |
test_table = self.TestTable(name='test_table', description='my test table') |
| 214 |
test_table.add_row(my_col=3.0, indexed_col=[1.0, 3.0], optional_col2=.5) |
| 215 |
test_table.add_row(my_col=4.0, indexed_col=[2.0, 4.0], optional_col2=.5) |
| 216 |
|
| 217 |
msg = r"^'bad' is not the name of a predefined column of table .*" |
| 218 |
with self.assertRaisesRegex(ValueError, msg): |
| 219 |
self.TestDTRTable(name='test_dtr_table', description='my table', target_tables={'bad': test_table}) |
| 220 |
|
| 221 |
def test_dynamic_table_region_non_dtr_target(self): |
| 222 |
test_table = self.TestTable(name='test_table', description='my test table') |
| 223 |
test_table.add_row(my_col=3.0, indexed_col=[1.0, 3.0], optional_col2=.5) |
| 224 |
test_table.add_row(my_col=4.0, indexed_col=[2.0, 4.0], optional_col2=.5) |
| 225 |
|
| 226 |
msg = "Column 'optional_col3' must be a DynamicTableRegion to have a target table." |
| 227 |
with self.assertRaisesWith(ValueError, msg): |
| 228 |
self.TestDTRTable(name='test_dtr_table', description='my table', |
| 229 |
target_tables={'optional_col3': test_table}) |
| 230 |
|
| 231 |
def test_attribute(self): |
| 232 |
test_table = self.TestTable(name='test_table', description='my test table') |
| 233 |
assert test_table.my_col is not None |
| 234 |
assert test_table.indexed_col is not None |
| 235 |
assert test_table.my_col is test_table['my_col'] |
| 236 |
assert test_table.indexed_col is test_table['indexed_col'].target |
| 237 |
|
| 238 |
def test_roundtrip(self): |
| 239 |
# NOTE this does not use H5RoundTripMixin because this requires custom validation |
| 240 |
test_table = self.TestTable(name='test_table', description='my test table') |
| 241 |
test_table.add_column('dynamic_column', 'this is a dynamic column') |
| 242 |
test_table.add_row( |
| 243 |
my_col=3.0, indexed_col=[1.0, 3.0], dynamic_column=4, optional_col2=.5, |
| 244 |
) |
| 245 |
self.filename = os.path.join(self.test_dir, 'test_TestTable.h5') |
| 246 |
|
| 247 |
with HDF5IO(self.filename, manager=self.manager, mode='w') as write_io: |
| 248 |
write_io.write(test_table, cache_spec=True) |
| 249 |
|
| 250 |
self.reader = HDF5IO(self.filename, manager=self.manager, mode='r') |
| 251 |
read_container = self.reader.read() |
| 252 |
|
| 253 |
self.assertIsNotNone(str(test_table)) # added as a test to make sure printing works |
| 254 |
self.assertIsNotNone(str(read_container)) |
| 255 |
# make sure we get a completely new object |
| 256 |
self.assertNotEqual(id(test_table), id(read_container)) |
| 257 |
# the name of the root container of a file is always 'root' (see h5tools.py ROOT_NAME) |
| 258 |
# thus, ignore the name of the container when comparing original container vs read container |
| 259 |
self.assertContainerEqual(read_container, test_table, ignore_name=True) |
| 260 |
|
| 261 |
builder = self.reader.read_builder() |
| 262 |
# TODO fix ValueError: No specification for 'Container' in namespace 'test_core' |
| 263 |
validator = ValidatorMap(self.manager.namespace_catalog.get_namespace(name=CORE_NAMESPACE)) |
| 264 |
errors = validator.validate(builder) |
| 265 |
if errors: |
| 266 |
for err in errors: |
| 267 |
raise Exception(err) |
| 268 |
self.reader.close() |