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