| 1 |
from abc import ABCMeta, abstractmethod |
| 2 |
|
| 3 |
import numpy as np |
| 4 |
from hdmf import Container, Data, TermSet, TermSetWrapper |
| 5 |
from hdmf.common import VectorData, get_type_map |
| 6 |
from hdmf.build import ObjectMapper, BuildManager, TypeMap, GroupBuilder, DatasetBuilder |
| 7 |
from hdmf.build.warnings import DtypeConversionWarning, IncorrectDatasetShapeBuildWarning |
| 8 |
from hdmf.spec import GroupSpec, AttributeSpec, DatasetSpec, SpecCatalog, SpecNamespace, NamespaceCatalog, Spec |
| 9 |
from hdmf.testing import TestCase |
| 10 |
from hdmf.utils import docval, getargs |
| 11 |
|
| 12 |
from tests.unit.helpers.utils import CORE_NAMESPACE |
| 13 |
|
| 14 |
try: |
| 15 |
import linkml_runtime # noqa: F401 |
| 16 |
LINKML_INSTALLED = True |
| 17 |
except ImportError: |
| 18 |
LINKML_INSTALLED = False |
| 19 |
|
| 20 |
|
| 21 |
class TestUnwrapTermSetWrapperBuild(TestCase): |
| 22 |
""" |
| 23 |
Test the unwrapping of TermSetWrapper on regular datasets within build. |
| 24 |
""" |
| 25 |
def setUp(self): |
| 26 |
if not LINKML_INSTALLED: |
| 27 |
self.skipTest("optional LinkML module is not installed") |
| 28 |
|
| 29 |
def test_unwrap(self): |
| 30 |
manager = BuildManager(get_type_map()) |
| 31 |
terms = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml') |
| 32 |
build = manager.build(VectorData(name='test_data', |
| 33 |
description='description', |
| 34 |
data=TermSetWrapper(value=['Homo sapiens'], termset= terms))) |
| 35 |
|
| 36 |
self.assertEqual(build.data, ['Homo sapiens']) |
| 37 |
|
| 38 |
# TODO: test build of extended group/dataset that modifies an attribute dtype (commented out below), shape, value, etc. |
| 39 |
# by restriction. also check that attributes cannot be deleted or scope expanded. |
| 40 |
# TODO: test build of extended dataset that modifies shape by restriction. |
| 41 |
|
| 42 |
class Bar(Container): |
| 43 |
|
| 44 |
@docval({'name': 'name', 'type': str, 'doc': 'the name of this Bar'}, |
| 45 |
{'name': 'attr1', 'type': str, 'doc': 'a string attribute'}, |
| 46 |
{'name': 'attr2', 'type': 'int', 'doc': 'an int attribute', 'default': None}, |
| 47 |
{'name': 'ext_attr', 'type': bool, 'doc': 'a boolean attribute', 'default': True}) |
| 48 |
def __init__(self, **kwargs): |
| 49 |
name, attr1, attr2, ext_attr = getargs('name', 'attr1', 'attr2', 'ext_attr', kwargs) |
| 50 |
super().__init__(name=name) |
| 51 |
self.__attr1 = attr1 |
| 52 |
self.__attr2 = attr2 |
| 53 |
self.__ext_attr = kwargs['ext_attr'] |
| 54 |
|
| 55 |
@property |
| 56 |
def data_type(self): |
| 57 |
return 'Bar' |
| 58 |
|
| 59 |
@property |
| 60 |
def attr1(self): |
| 61 |
return self.__attr1 |
| 62 |
|
| 63 |
@property |
| 64 |
def attr2(self): |
| 65 |
return self.__attr2 |
| 66 |
|
| 67 |
@property |
| 68 |
def ext_attr(self): |
| 69 |
return self.__ext_attr |
| 70 |
|
| 71 |
|
| 72 |
class BarHolder(Container): |
| 73 |
|
| 74 |
@docval({'name': 'name', 'type': str, 'doc': 'the name of this BarHolder'}, |
| 75 |
{'name': 'bars', 'type': ('data', 'array_data'), 'doc': 'bars', 'default': list()}) |
| 76 |
def __init__(self, **kwargs): |
| 77 |
name, bars = getargs('name', 'bars', kwargs) |
| 78 |
super().__init__(name=name) |
| 79 |
self.__bars = bars |
| 80 |
for b in bars: |
| 81 |
if b is not None and b.parent is None: |
| 82 |
b.parent = self |
| 83 |
|
| 84 |
@property |
| 85 |
def data_type(self): |
| 86 |
return 'BarHolder' |
| 87 |
|
| 88 |
@property |
| 89 |
def bars(self): |
| 90 |
return self.__bars |
| 91 |
|
| 92 |
|
| 93 |
class ExtBarMapper(ObjectMapper): |
| 94 |
|
| 95 |
@docval({"name": "spec", "type": Spec, "doc": "the spec to get the attribute value for"}, |
| 96 |
{"name": "container", "type": Bar, "doc": "the container to get the attribute value from"}, |
| 97 |
{"name": "manager", "type": BuildManager, "doc": "the BuildManager used for managing this build"}, |
| 98 |
returns='the value of the attribute') |
| 99 |
def get_attr_value(self, **kwargs): |
| 100 |
''' Get the value of the attribute corresponding to this spec from the given container ''' |
| 101 |
spec, container, manager = getargs('spec', 'container', 'manager', kwargs) |
| 102 |
# handle custom mapping of field 'ext_attr' within container BarHolder/Bar -> spec BarHolder/Bar.ext_attr |
| 103 |
if isinstance(container.parent, BarHolder): |
| 104 |
if spec.name == 'ext_attr': |
| 105 |
return container.ext_attr |
| 106 |
return super().get_attr_value(**kwargs) |
| 107 |
|
| 108 |
|
| 109 |
class BuildGroupExtAttrsMixin(TestCase, metaclass=ABCMeta): |
| 110 |
|
| 111 |
def setUp(self): |
| 112 |
self.setUpBarSpec() |
| 113 |
self.setUpBarHolderSpec() |
| 114 |
spec_catalog = SpecCatalog() |
| 115 |
spec_catalog.register_spec(self.bar_spec, 'test.yaml') |
| 116 |
spec_catalog.register_spec(self.bar_holder_spec, 'test.yaml') |
| 117 |
namespace = SpecNamespace( |
| 118 |
doc='a test namespace', |
| 119 |
name=CORE_NAMESPACE, |
| 120 |
schema=[{'source': 'test.yaml'}], |
| 121 |
version='0.1.0', |
| 122 |
catalog=spec_catalog |
| 123 |
) |
| 124 |
namespace_catalog = NamespaceCatalog() |
| 125 |
namespace_catalog.add_namespace(CORE_NAMESPACE, namespace) |
| 126 |
type_map = TypeMap(namespace_catalog) |
| 127 |
type_map.register_container_type(CORE_NAMESPACE, 'Bar', Bar) |
| 128 |
type_map.register_container_type(CORE_NAMESPACE, 'BarHolder', BarHolder) |
| 129 |
type_map.register_map(Bar, ExtBarMapper) |
| 130 |
type_map.register_map(BarHolder, ObjectMapper) |
| 131 |
self.manager = BuildManager(type_map) |
| 132 |
|
| 133 |
def setUpBarSpec(self): |
| 134 |
attr1_attr = AttributeSpec( |
| 135 |
name='attr1', |
| 136 |
dtype='text', |
| 137 |
doc='an example string attribute', |
| 138 |
) |
| 139 |
attr2_attr = AttributeSpec( |
| 140 |
name='attr2', |
| 141 |
dtype='int', |
| 142 |
doc='an example int attribute', |
| 143 |
) |
| 144 |
self.bar_spec = GroupSpec( |
| 145 |
doc='A test group specification with a data type', |
| 146 |
data_type_def='Bar', |
| 147 |
attributes=[attr1_attr, attr2_attr], |
| 148 |
) |
| 149 |
|
| 150 |
@abstractmethod |
| 151 |
def setUpBarHolderSpec(self): |
| 152 |
pass |
| 153 |
|
| 154 |
|
| 155 |
class TestBuildGroupAddedAttr(BuildGroupExtAttrsMixin, TestCase): |
| 156 |
""" |
| 157 |
If the spec defines a group data_type A (Bar) using 'data_type_def' and defines another data_type B (BarHolder) |
| 158 |
that includes A using 'data_type_inc', then the included A spec is an extended (or refined) spec of A - call it A'. |
| 159 |
The spec of A' can refine or add attributes to the spec of A. This test ensures that *added attributes* in A' are |
| 160 |
handled properly. |
| 161 |
""" |
| 162 |
|
| 163 |
def setUpBarHolderSpec(self): |
| 164 |
ext_attr = AttributeSpec( |
| 165 |
name='ext_attr', |
| 166 |
dtype='bool', |
| 167 |
doc='A boolean attribute', |
| 168 |
) |
| 169 |
bar_ext_no_name_spec = GroupSpec( |
| 170 |
doc='A Bar extended with attribute ext_attr', |
| 171 |
data_type_inc='Bar', |
| 172 |
quantity='*', |
| 173 |
attributes=[ext_attr], |
| 174 |
) |
| 175 |
self.bar_holder_spec = GroupSpec( |
| 176 |
doc='A container of multiple extended Bar objects', |
| 177 |
data_type_def='BarHolder', |
| 178 |
groups=[bar_ext_no_name_spec], |
| 179 |
) |
| 180 |
|
| 181 |
def test_build_added_attr(self): |
| 182 |
""" |
| 183 |
Test build of BarHolder which can contain multiple extended Bar objects, which have a new attribute. |
| 184 |
""" |
| 185 |
ext_bar_inst = Bar( |
| 186 |
name='my_bar', |
| 187 |
attr1='a string', |
| 188 |
attr2=10, |
| 189 |
ext_attr=False, |
| 190 |
) |
| 191 |
bar_holder_inst = BarHolder( |
| 192 |
name='my_bar_holder', |
| 193 |
bars=[ext_bar_inst], |
| 194 |
) |
| 195 |
|
| 196 |
expected_inner = GroupBuilder( |
| 197 |
name='my_bar', |
| 198 |
attributes={ |
| 199 |
'attr1': 'a string', |
| 200 |
'attr2': 10, |
| 201 |
'data_type': 'Bar', |
| 202 |
'ext_attr': False, |
| 203 |
'namespace': CORE_NAMESPACE, |
| 204 |
'object_id': ext_bar_inst.object_id, |
| 205 |
}, |
| 206 |
) |
| 207 |
expected = GroupBuilder( |
| 208 |
name='my_bar_holder', |
| 209 |
groups={'my_bar': expected_inner}, |
| 210 |
attributes={ |
| 211 |
'data_type': 'BarHolder', |
| 212 |
'namespace': CORE_NAMESPACE, |
| 213 |
'object_id': bar_holder_inst.object_id, |
| 214 |
}, |
| 215 |
) |
| 216 |
|
| 217 |
# the object mapper automatically maps the spec of extended Bars to the 'BarMapper.bars' field |
| 218 |
builder = self.manager.build(bar_holder_inst, source='test.h5') |
| 219 |
self.assertDictEqual(builder, expected) |
| 220 |
|
| 221 |
|
| 222 |
class TestBuildGroupRefinedAttr(BuildGroupExtAttrsMixin, TestCase): |
| 223 |
""" |
| 224 |
If the spec defines a group data_type A (Bar) using 'data_type_def' and defines another data_type B (BarHolder) |
| 225 |
that includes A using 'data_type_inc', then the included A spec is an extended (or refined) spec of A - call it A'. |
| 226 |
The spec of A' can refine or add attributes to the spec of A. This test ensures that *refine attributes* in A' are |
| 227 |
handled properly. |
| 228 |
""" |
| 229 |
|
| 230 |
def setUpBarHolderSpec(self): |
| 231 |
int_attr2 = AttributeSpec( |
| 232 |
name='attr2', |
| 233 |
dtype='int64', |
| 234 |
doc='Refine Bar spec from int to int64', |
| 235 |
) |
| 236 |
bar_ext_no_name_spec = GroupSpec( |
| 237 |
doc='A Bar extended with modified attribute attr2', |
| 238 |
data_type_inc='Bar', |
| 239 |
quantity='*', |
| 240 |
attributes=[int_attr2], |
| 241 |
) |
| 242 |
self.bar_holder_spec = GroupSpec( |
| 243 |
doc='A container of multiple extended Bar objects', |
| 244 |
data_type_def='BarHolder', |
| 245 |
groups=[bar_ext_no_name_spec], |
| 246 |
) |
| 247 |
|
| 248 |
def test_build_refined_attr(self): |
| 249 |
""" |
| 250 |
Test build of BarHolder which can contain multiple extended Bar objects, which have a modified attr2. |
| 251 |
""" |
| 252 |
ext_bar_inst = Bar( |
| 253 |
name='my_bar', |
| 254 |
attr1='a string', |
| 255 |
attr2=np.int64(10), |
| 256 |
) |
| 257 |
bar_holder_inst = BarHolder( |
| 258 |
name='my_bar_holder', |
| 259 |
bars=[ext_bar_inst], |
| 260 |
) |
| 261 |
|
| 262 |
expected_inner = GroupBuilder( |
| 263 |
name='my_bar', |
| 264 |
attributes={ |
| 265 |
'attr1': 'a string', |
| 266 |
'attr2': np.int64(10), |
| 267 |
'data_type': 'Bar', |
| 268 |
'namespace': CORE_NAMESPACE, |
| 269 |
'object_id': ext_bar_inst.object_id, |
| 270 |
} |
| 271 |
) |
| 272 |
expected = GroupBuilder( |
| 273 |
name='my_bar_holder', |
| 274 |
groups={'my_bar': expected_inner}, |
| 275 |
attributes={ |
| 276 |
'data_type': 'BarHolder', |
| 277 |
'namespace': CORE_NAMESPACE, |
| 278 |
'object_id': bar_holder_inst.object_id, |
| 279 |
}, |
| 280 |
) |
| 281 |
|
| 282 |
# the object mapper automatically maps the spec of extended Bars to the 'BarMapper.bars' field |
| 283 |
builder = self.manager.build(bar_holder_inst, source='test.h5') |
| 284 |
self.assertDictEqual(builder, expected) |
| 285 |
|
| 286 |
# def test_build_refined_attr_wrong_type(self): |
| 287 |
# """ |
| 288 |
# Test build of BarHolder which contains a Bar that has the wrong dtype for an attr. |
| 289 |
# """ |
| 290 |
# ext_bar_inst = Bar( |
| 291 |
# name='my_bar', |
| 292 |
# attr1='a string', |
| 293 |
# attr2=10, # spec specifies attr2 should be an int64 for Bars within BarHolder |
| 294 |
# ) |
| 295 |
# bar_holder_inst = BarHolder( |
| 296 |
# name='my_bar_holder', |
| 297 |
# bars=[ext_bar_inst], |
| 298 |
# ) |
| 299 |
# |
| 300 |
# expected_inner = GroupBuilder( |
| 301 |
# name='my_bar', |
| 302 |
# attributes={ |
| 303 |
# 'attr1': 'a string', |
| 304 |
# 'attr2': np.int64(10), |
| 305 |
# 'data_type': 'Bar', |
| 306 |
# 'namespace': CORE_NAMESPACE, |
| 307 |
# 'object_id': ext_bar_inst.object_id, |
| 308 |
# } |
| 309 |
# ) |
| 310 |
# expected = GroupBuilder( |
| 311 |
# name='my_bar_holder', |
| 312 |
# groups={'my_bar': expected_inner}, |
| 313 |
# attributes={ |
| 314 |
# 'data_type': 'BarHolder', |
| 315 |
# 'namespace': CORE_NAMESPACE, |
| 316 |
# 'object_id': bar_holder_inst.object_id, |
| 317 |
# }, |
| 318 |
# ) |
| 319 |
# |
| 320 |
# # the object mapper automatically maps the spec of extended Bars to the 'BarMapper.bars' field |
| 321 |
# |
| 322 |
# # TODO build should raise a conversion warning for converting 10 (int32) to np.int64 |
| 323 |
# builder = self.manager.build(bar_holder_inst, source='test.h5') |
| 324 |
# self.assertDictEqual(builder, expected) |
| 325 |
|
| 326 |
|
| 327 |
class BarData(Data): |
| 328 |
|
| 329 |
@docval({'name': 'name', 'type': str, 'doc': 'the name of this BarData'}, |
| 330 |
{'name': 'data', 'type': ('data', 'array_data'), 'doc': 'the data'}, |
| 331 |
{'name': 'attr1', 'type': str, 'doc': 'a string attribute'}, |
| 332 |
{'name': 'attr2', 'type': 'int', 'doc': 'an int attribute', 'default': None}, |
| 333 |
{'name': 'ext_attr', 'type': bool, 'doc': 'a boolean attribute', 'default': True}) |
| 334 |
def __init__(self, **kwargs): |
| 335 |
name, data, attr1, attr2, ext_attr = getargs('name', 'data', 'attr1', 'attr2', 'ext_attr', kwargs) |
| 336 |
super().__init__(name=name, data=data) |
| 337 |
self.__attr1 = attr1 |
| 338 |
self.__attr2 = attr2 |
| 339 |
self.__ext_attr = kwargs['ext_attr'] |
| 340 |
|
| 341 |
@property |
| 342 |
def data_type(self): |
| 343 |
return 'BarData' |
| 344 |
|
| 345 |
@property |
| 346 |
def attr1(self): |
| 347 |
return self.__attr1 |
| 348 |
|
| 349 |
@property |
| 350 |
def attr2(self): |
| 351 |
return self.__attr2 |
| 352 |
|
| 353 |
@property |
| 354 |
def ext_attr(self): |
| 355 |
return self.__ext_attr |
| 356 |
|
| 357 |
|
| 358 |
class BarDataHolder(Container): |
| 359 |
|
| 360 |
@docval({'name': 'name', 'type': str, 'doc': 'the name of this BarDataHolder'}, |
| 361 |
{'name': 'bar_datas', 'type': ('data', 'array_data'), 'doc': 'bar_datas', 'default': list()}) |
| 362 |
def __init__(self, **kwargs): |
| 363 |
name, bar_datas = getargs('name', 'bar_datas', kwargs) |
| 364 |
super().__init__(name=name) |
| 365 |
self.__bar_datas = bar_datas |
| 366 |
for b in bar_datas: |
| 367 |
if b is not None and b.parent is None: |
| 368 |
b.parent = self |
| 369 |
|
| 370 |
@property |
| 371 |
def data_type(self): |
| 372 |
return 'BarDataHolder' |
| 373 |
|
| 374 |
@property |
| 375 |
def bar_datas(self): |
| 376 |
return self.__bar_datas |
| 377 |
|
| 378 |
|
| 379 |
class ExtBarDataMapper(ObjectMapper): |
| 380 |
|
| 381 |
@docval({"name": "spec", "type": Spec, "doc": "the spec to get the attribute value for"}, |
| 382 |
{"name": "container", "type": BarData, "doc": "the container to get the attribute value from"}, |
| 383 |
{"name": "manager", "type": BuildManager, "doc": "the BuildManager used for managing this build"}, |
| 384 |
returns='the value of the attribute') |
| 385 |
def get_attr_value(self, **kwargs): |
| 386 |
''' Get the value of the attribute corresponding to this spec from the given container ''' |
| 387 |
spec, container, manager = getargs('spec', 'container', 'manager', kwargs) |
| 388 |
# handle custom mapping of field 'ext_attr' within container |
| 389 |
# BardataHolder/BarData -> spec BarDataHolder/BarData.ext_attr |
| 390 |
if isinstance(container.parent, BarDataHolder): |
| 391 |
if spec.name == 'ext_attr': |
| 392 |
return container.ext_attr |
| 393 |
return super().get_attr_value(**kwargs) |
| 394 |
|
| 395 |
|
| 396 |
class BuildDatasetExtAttrsMixin(TestCase, metaclass=ABCMeta): |
| 397 |
|
| 398 |
def setUp(self): |
| 399 |
self.set_up_specs() |
| 400 |
spec_catalog = SpecCatalog() |
| 401 |
spec_catalog.register_spec(self.bar_data_spec, 'test.yaml') |
| 402 |
spec_catalog.register_spec(self.bar_data_holder_spec, 'test.yaml') |
| 403 |
namespace = SpecNamespace( |
| 404 |
doc='a test namespace', |
| 405 |
name=CORE_NAMESPACE, |
| 406 |
schema=[{'source': 'test.yaml'}], |
| 407 |
version='0.1.0', |
| 408 |
catalog=spec_catalog |
| 409 |
) |
| 410 |
namespace_catalog = NamespaceCatalog() |
| 411 |
namespace_catalog.add_namespace(CORE_NAMESPACE, namespace) |
| 412 |
type_map = TypeMap(namespace_catalog) |
| 413 |
type_map.register_container_type(CORE_NAMESPACE, 'BarData', BarData) |
| 414 |
type_map.register_container_type(CORE_NAMESPACE, 'BarDataHolder', BarDataHolder) |
| 415 |
type_map.register_map(BarData, ExtBarDataMapper) |
| 416 |
type_map.register_map(BarDataHolder, ObjectMapper) |
| 417 |
self.manager = BuildManager(type_map) |
| 418 |
|
| 419 |
def set_up_specs(self): |
| 420 |
attr1_attr = AttributeSpec( |
| 421 |
name='attr1', |
| 422 |
dtype='text', |
| 423 |
doc='an example string attribute', |
| 424 |
) |
| 425 |
attr2_attr = AttributeSpec( |
| 426 |
name='attr2', |
| 427 |
dtype='int', |
| 428 |
doc='an example int attribute', |
| 429 |
) |
| 430 |
self.bar_data_spec = DatasetSpec( |
| 431 |
doc='A test dataset specification with a data type', |
| 432 |
data_type_def='BarData', |
| 433 |
dtype='int', |
| 434 |
shape=[[None], [None, None]], |
| 435 |
attributes=[attr1_attr, attr2_attr], |
| 436 |
) |
| 437 |
self.bar_data_holder_spec = GroupSpec( |
| 438 |
doc='A container of multiple extended BarData objects', |
| 439 |
data_type_def='BarDataHolder', |
| 440 |
datasets=[self.get_refined_bar_data_spec()], |
| 441 |
) |
| 442 |
|
| 443 |
@abstractmethod |
| 444 |
def get_refined_bar_data_spec(self): |
| 445 |
pass |
| 446 |
|
| 447 |
|
| 448 |
class TestBuildDatasetAddedAttrs(BuildDatasetExtAttrsMixin, TestCase): |
| 449 |
""" |
| 450 |
If the spec defines a dataset data_type A (BarData) using 'data_type_def' and defines another data_type B |
| 451 |
(BarHolder) that includes A using 'data_type_inc', then the included A spec is an extended (or refined) spec of A - |
| 452 |
call it A'. The spec of A' can refine or add attributes, refine the dtype, refine the shape, or set a fixed value |
| 453 |
to the spec of A. This test ensures that *added attributes* in A' are handled properly. This is similar to how the |
| 454 |
spec for a subtype of DynamicTable can contain a VectorData that has a new attribute. |
| 455 |
""" |
| 456 |
|
| 457 |
def get_refined_bar_data_spec(self): |
| 458 |
ext_attr = AttributeSpec( |
| 459 |
name='ext_attr', |
| 460 |
dtype='bool', |
| 461 |
doc='A boolean attribute', |
| 462 |
) |
| 463 |
refined_spec = DatasetSpec( |
| 464 |
doc='A BarData extended with attribute ext_attr', |
| 465 |
data_type_inc='BarData', |
| 466 |
quantity='*', |
| 467 |
attributes=[ext_attr], |
| 468 |
) |
| 469 |
return refined_spec |
| 470 |
|
| 471 |
def test_build_added_attr(self): |
| 472 |
""" |
| 473 |
Test build of BarHolder which can contain multiple extended BarData objects, which have a new attribute. |
| 474 |
""" |
| 475 |
ext_bar_data_inst = BarData( |
| 476 |
name='my_bar', |
| 477 |
data=list(range(10)), |
| 478 |
attr1='a string', |
| 479 |
attr2=10, |
| 480 |
ext_attr=False, |
| 481 |
) |
| 482 |
bar_data_holder_inst = BarDataHolder( |
| 483 |
name='my_bar_holder', |
| 484 |
bar_datas=[ext_bar_data_inst], |
| 485 |
) |
| 486 |
|
| 487 |
expected_inner = DatasetBuilder( |
| 488 |
name='my_bar', |
| 489 |
data=list(range(10)), |
| 490 |
attributes={ |
| 491 |
'attr1': 'a string', |
| 492 |
'attr2': 10, |
| 493 |
'data_type': 'BarData', |
| 494 |
'ext_attr': False, |
| 495 |
'namespace': CORE_NAMESPACE, |
| 496 |
'object_id': ext_bar_data_inst.object_id, |
| 497 |
}, |
| 498 |
) |
| 499 |
expected = GroupBuilder( |
| 500 |
name='my_bar_holder', |
| 501 |
datasets={'my_bar': expected_inner}, |
| 502 |
attributes={ |
| 503 |
'data_type': 'BarDataHolder', |
| 504 |
'namespace': CORE_NAMESPACE, |
| 505 |
'object_id': bar_data_holder_inst.object_id, |
| 506 |
}, |
| 507 |
) |
| 508 |
|
| 509 |
# the object mapper automatically maps the spec of extended Bars to the 'BarMapper.bars' field |
| 510 |
builder = self.manager.build(bar_data_holder_inst, source='test.h5') |
| 511 |
self.assertDictEqual(builder, expected) |
| 512 |
|
| 513 |
|
| 514 |
class TestBuildDatasetRefinedDtype(BuildDatasetExtAttrsMixin, TestCase): |
| 515 |
""" |
| 516 |
If the spec defines a dataset data_type A (BarData) using 'data_type_def' and defines another data_type B |
| 517 |
(BarHolder) that includes A using 'data_type_inc', then the included A spec is an extended (or refined) spec of A - |
| 518 |
call it A'. The spec of A' can refine or add attributes, refine the dtype, refine the shape, or set a fixed value |
| 519 |
to the spec of A. This test ensures that if A' refines the dtype of A, the build process uses the correct dtype for |
| 520 |
conversion. |
| 521 |
""" |
| 522 |
|
| 523 |
def get_refined_bar_data_spec(self): |
| 524 |
refined_spec = DatasetSpec( |
| 525 |
doc='A BarData with refined int64 dtype', |
| 526 |
data_type_inc='BarData', |
| 527 |
dtype='int64', |
| 528 |
quantity='*', |
| 529 |
) |
| 530 |
return refined_spec |
| 531 |
|
| 532 |
def test_build_refined_dtype_convert(self): |
| 533 |
""" |
| 534 |
Test build of BarDataHolder which contains a BarData with data that needs to be converted to the refined dtype. |
| 535 |
""" |
| 536 |
ext_bar_data_inst = BarData( |
| 537 |
name='my_bar', |
| 538 |
data=np.array([1, 2], dtype=np.int32), # the refined spec says data should be int64s |
| 539 |
attr1='a string', |
| 540 |
attr2=10, |
| 541 |
) |
| 542 |
bar_data_holder_inst = BarDataHolder( |
| 543 |
name='my_bar_holder', |
| 544 |
bar_datas=[ext_bar_data_inst], |
| 545 |
) |
| 546 |
|
| 547 |
expected_inner = DatasetBuilder( |
| 548 |
name='my_bar', |
| 549 |
data=np.array([1, 2], dtype=np.int64), # the objectmapper should convert the given data to int64s |
| 550 |
attributes={ |
| 551 |
'attr1': 'a string', |
| 552 |
'attr2': 10, |
| 553 |
'data_type': 'BarData', |
| 554 |
'namespace': CORE_NAMESPACE, |
| 555 |
'object_id': ext_bar_data_inst.object_id, |
| 556 |
}, |
| 557 |
) |
| 558 |
expected = GroupBuilder( |
| 559 |
name='my_bar_holder', |
| 560 |
datasets={'my_bar': expected_inner}, |
| 561 |
attributes={ |
| 562 |
'data_type': 'BarDataHolder', |
| 563 |
'namespace': CORE_NAMESPACE, |
| 564 |
'object_id': bar_data_holder_inst.object_id, |
| 565 |
}, |
| 566 |
) |
| 567 |
|
| 568 |
# the object mapper automatically maps the spec of extended Bars to the 'BarMapper.bars' field |
| 569 |
msg = ("Spec 'BarDataHolder/BarData': Value with data type int32 is being converted to data type int64 " |
| 570 |
"as specified.") |
| 571 |
with self.assertWarnsWith(DtypeConversionWarning, msg): |
| 572 |
builder = self.manager.build(bar_data_holder_inst, source='test.h5') |
| 573 |
np.testing.assert_array_equal(builder.datasets['my_bar'].data, expected.datasets['my_bar'].data) |
| 574 |
self.assertEqual(builder.datasets['my_bar'].data.dtype, np.int64) |
| 575 |
|
| 576 |
|
| 577 |
class TestBuildDatasetNotRefinedDtype(BuildDatasetExtAttrsMixin, TestCase): |
| 578 |
""" |
| 579 |
If the spec defines a dataset data_type A (BarData) using 'data_type_def' and defines another data_type B |
| 580 |
(BarHolder) that includes A using 'data_type_inc', then the included A spec is an extended (or refined) spec of A - |
| 581 |
call it A'. The spec of A' can refine or add attributes, refine the dtype, refine the shape, or set a fixed value |
| 582 |
to the spec of A. This test ensures that if A' does not refine the dtype of A, the build process uses the correct |
| 583 |
dtype for conversion. |
| 584 |
""" |
| 585 |
|
| 586 |
def get_refined_bar_data_spec(self): |
| 587 |
refined_spec = DatasetSpec( |
| 588 |
doc='A BarData', |
| 589 |
data_type_inc='BarData', |
| 590 |
quantity='*', |
| 591 |
) |
| 592 |
return refined_spec |
| 593 |
|
| 594 |
def test_build_correct_dtype(self): |
| 595 |
""" |
| 596 |
Test build of BarDataHolder which contains a BarData. |
| 597 |
""" |
| 598 |
ext_bar_data_inst = BarData( |
| 599 |
name='my_bar', |
| 600 |
data=[1, 2], |
| 601 |
attr1='a string', |
| 602 |
attr2=10, |
| 603 |
) |
| 604 |
bar_data_holder_inst = BarDataHolder( |
| 605 |
name='my_bar_holder', |
| 606 |
bar_datas=[ext_bar_data_inst], |
| 607 |
) |
| 608 |
|
| 609 |
expected_inner = DatasetBuilder( |
| 610 |
name='my_bar', |
| 611 |
data=[1, 2], |
| 612 |
attributes={ |
| 613 |
'attr1': 'a string', |
| 614 |
'attr2': 10, |
| 615 |
'data_type': 'BarData', |
| 616 |
'namespace': CORE_NAMESPACE, |
| 617 |
'object_id': ext_bar_data_inst.object_id, |
| 618 |
}, |
| 619 |
) |
| 620 |
expected = GroupBuilder( |
| 621 |
name='my_bar_holder', |
| 622 |
datasets={'my_bar': expected_inner}, |
| 623 |
attributes={ |
| 624 |
'data_type': 'BarDataHolder', |
| 625 |
'namespace': CORE_NAMESPACE, |
| 626 |
'object_id': bar_data_holder_inst.object_id, |
| 627 |
}, |
| 628 |
) |
| 629 |
|
| 630 |
# the object mapper automatically maps the spec of extended Bars to the 'BarMapper.bars' field |
| 631 |
builder = self.manager.build(bar_data_holder_inst, source='test.h5') |
| 632 |
self.assertDictEqual(builder, expected) |
| 633 |
|
| 634 |
def test_build_incorrect_dtype(self): |
| 635 |
""" |
| 636 |
Test build of BarDataHolder which contains a BarData |
| 637 |
""" |
| 638 |
ext_bar_data_inst = BarData( |
| 639 |
name='my_bar', |
| 640 |
data=['a', 'b'], |
| 641 |
attr1='a string', |
| 642 |
attr2=10, |
| 643 |
) |
| 644 |
bar_data_holder_inst = BarDataHolder( |
| 645 |
name='my_bar_holder', |
| 646 |
bar_datas=[ext_bar_data_inst], |
| 647 |
) |
| 648 |
|
| 649 |
# the object mapper automatically maps the spec of extended Bars to the 'BarMapper.bars' field |
| 650 |
msg = "could not resolve dtype for BarData 'my_bar'" |
| 651 |
with self.assertRaisesWith(Exception, msg): |
| 652 |
self.manager.build(bar_data_holder_inst, source='test.h5') |
| 653 |
|
| 654 |
|
| 655 |
class BuildDatasetShapeMixin(TestCase, metaclass=ABCMeta): |
| 656 |
|
| 657 |
def setUp(self): |
| 658 |
self.set_up_specs() |
| 659 |
spec_catalog = SpecCatalog() |
| 660 |
spec_catalog.register_spec(self.bar_data_spec, 'test.yaml') |
| 661 |
spec_catalog.register_spec(self.bar_data_holder_spec, 'test.yaml') |
| 662 |
namespace = SpecNamespace( |
| 663 |
doc='a test namespace', |
| 664 |
name=CORE_NAMESPACE, |
| 665 |
schema=[{'source': 'test.yaml'}], |
| 666 |
version='0.1.0', |
| 667 |
catalog=spec_catalog |
| 668 |
) |
| 669 |
namespace_catalog = NamespaceCatalog() |
| 670 |
namespace_catalog.add_namespace(CORE_NAMESPACE, namespace) |
| 671 |
type_map = TypeMap(namespace_catalog) |
| 672 |
type_map.register_container_type(CORE_NAMESPACE, 'BarData', BarData) |
| 673 |
type_map.register_container_type(CORE_NAMESPACE, 'BarDataHolder', BarDataHolder) |
| 674 |
type_map.register_map(BarData, ExtBarDataMapper) |
| 675 |
type_map.register_map(BarDataHolder, ObjectMapper) |
| 676 |
self.manager = BuildManager(type_map) |
| 677 |
|
| 678 |
def set_up_specs(self): |
| 679 |
shape, dims = self.get_base_shape_dims() |
| 680 |
self.bar_data_spec = DatasetSpec( |
| 681 |
doc='A test dataset specification with a data type', |
| 682 |
data_type_def='BarData', |
| 683 |
dtype='int', |
| 684 |
shape=shape, |
| 685 |
dims=dims, |
| 686 |
) |
| 687 |
self.bar_data_holder_spec = GroupSpec( |
| 688 |
doc='A container of multiple extended BarData objects', |
| 689 |
data_type_def='BarDataHolder', |
| 690 |
datasets=[self.get_dataset_inc_spec()], |
| 691 |
) |
| 692 |
|
| 693 |
@abstractmethod |
| 694 |
def get_base_shape_dims(self): |
| 695 |
pass |
| 696 |
|
| 697 |
@abstractmethod |
| 698 |
def get_dataset_inc_spec(self): |
| 699 |
pass |
| 700 |
|
| 701 |
|
| 702 |
class TestBuildDatasetOneOptionBadShapeUnspecified1(BuildDatasetShapeMixin): |
| 703 |
"""Test dataset spec shape = 2D any length, data = 1D. Should raise warning and set dimension_labels to None.""" |
| 704 |
|
| 705 |
def get_base_shape_dims(self): |
| 706 |
return [None, None], ['a', 'b'] |
| 707 |
|
| 708 |
def get_dataset_inc_spec(self): |
| 709 |
dataset_inc_spec = DatasetSpec( |
| 710 |
doc='A BarData', |
| 711 |
data_type_inc='BarData', |
| 712 |
quantity='*', |
| 713 |
) |
| 714 |
return dataset_inc_spec |
| 715 |
|
| 716 |
def test_build(self): |
| 717 |
""" |
| 718 |
Test build of BarDataHolder which contains a BarData. |
| 719 |
""" |
| 720 |
# NOTE: attr1 doesn't map to anything but is required in the test container class |
| 721 |
bar_data_inst = BarData(name='my_bar', data=[1, 2, 3], attr1='a string') |
| 722 |
bar_data_holder_inst = BarDataHolder( |
| 723 |
name='my_bar_holder', |
| 724 |
bar_datas=[bar_data_inst], |
| 725 |
) |
| 726 |
|
| 727 |
msg = "Shape of data does not match shape in spec 'BarData'" |
| 728 |
with self.assertWarnsWith(IncorrectDatasetShapeBuildWarning, msg): |
| 729 |
builder = self.manager.build(bar_data_holder_inst, source='test.h5') |
| 730 |
assert builder.datasets['my_bar'].dimension_labels is None |
| 731 |
|
| 732 |
|
| 733 |
class TestBuildDatasetOneOptionBadShapeUnspecified2(BuildDatasetShapeMixin): |
| 734 |
"""Test dataset spec shape = (any, 2), data = (3, 1). Should raise warning and set dimension_labels to None.""" |
| 735 |
|
| 736 |
def get_base_shape_dims(self): |
| 737 |
return [None, 2], ['a', 'b'] |
| 738 |
|
| 739 |
def get_dataset_inc_spec(self): |
| 740 |
dataset_inc_spec = DatasetSpec( |
| 741 |
doc='A BarData', |
| 742 |
data_type_inc='BarData', |
| 743 |
quantity='*', |
| 744 |
) |
| 745 |
return dataset_inc_spec |
| 746 |
|
| 747 |
def test_build(self): |
| 748 |
""" |
| 749 |
Test build of BarDataHolder which contains a BarData. |
| 750 |
""" |
| 751 |
# NOTE: attr1 doesn't map to anything but is required in the test container class |
| 752 |
bar_data_inst = BarData(name='my_bar', data=[[1], [2], [3]], attr1='a string') |
| 753 |
bar_data_holder_inst = BarDataHolder( |
| 754 |
name='my_bar_holder', |
| 755 |
bar_datas=[bar_data_inst], |
| 756 |
) |
| 757 |
|
| 758 |
msg = "Shape of data does not match shape in spec 'BarData'" |
| 759 |
with self.assertWarnsWith(IncorrectDatasetShapeBuildWarning, msg): |
| 760 |
builder = self.manager.build(bar_data_holder_inst, source='test.h5') |
| 761 |
assert builder.datasets['my_bar'].dimension_labels is None |
| 762 |
|
| 763 |
|
| 764 |
class TestBuildDatasetTwoOptionsBadShapeUnspecified(BuildDatasetShapeMixin): |
| 765 |
"""Test dataset spec shape = (any, 2) or (any, 3), data = (3, 1). |
| 766 |
Should raise warning and set dimension_labels to None. |
| 767 |
""" |
| 768 |
|
| 769 |
def get_base_shape_dims(self): |
| 770 |
return [[None, 2], [None, 3]], [['a', 'b1'], ['a', 'b2']] |
| 771 |
|
| 772 |
def get_dataset_inc_spec(self): |
| 773 |
dataset_inc_spec = DatasetSpec( |
| 774 |
doc='A BarData', |
| 775 |
data_type_inc='BarData', |
| 776 |
quantity='*', |
| 777 |
) |
| 778 |
return dataset_inc_spec |
| 779 |
|
| 780 |
def test_build(self): |
| 781 |
""" |
| 782 |
Test build of BarDataHolder which contains a BarData. |
| 783 |
""" |
| 784 |
# NOTE: attr1 doesn't map to anything but is required in the test container class |
| 785 |
bar_data_inst = BarData(name='my_bar', data=[[1], [2], [3]], attr1='a string') |
| 786 |
bar_data_holder_inst = BarDataHolder( |
| 787 |
name='my_bar_holder', |
| 788 |
bar_datas=[bar_data_inst], |
| 789 |
) |
| 790 |
|
| 791 |
msg = "Shape of data does not match any allowed shapes in spec 'BarData'" |
| 792 |
with self.assertWarnsWith(IncorrectDatasetShapeBuildWarning, msg): |
| 793 |
builder = self.manager.build(bar_data_holder_inst, source='test.h5') |
| 794 |
assert builder.datasets['my_bar'].dimension_labels is None |
| 795 |
|
| 796 |
|
| 797 |
class TestBuildDatasetDimensionLabelsUnspecified(BuildDatasetShapeMixin): |
| 798 |
|
| 799 |
def get_base_shape_dims(self): |
| 800 |
return None, None |
| 801 |
|
| 802 |
def get_dataset_inc_spec(self): |
| 803 |
dataset_inc_spec = DatasetSpec( |
| 804 |
doc='A BarData', |
| 805 |
data_type_inc='BarData', |
| 806 |
quantity='*', |
| 807 |
) |
| 808 |
return dataset_inc_spec |
| 809 |
|
| 810 |
def test_build(self): |
| 811 |
""" |
| 812 |
Test build of BarDataHolder which contains a BarData. |
| 813 |
""" |
| 814 |
# NOTE: attr1 doesn't map to anything but is required in the test container class |
| 815 |
bar_data_inst = BarData(name='my_bar', data=[[1, 2, 3], [4, 5, 6]], attr1='a string') |
| 816 |
bar_data_holder_inst = BarDataHolder( |
| 817 |
name='my_bar_holder', |
| 818 |
bar_datas=[bar_data_inst], |
| 819 |
) |
| 820 |
|
| 821 |
builder = self.manager.build(bar_data_holder_inst, source='test.h5') |
| 822 |
assert builder.datasets['my_bar'].dimension_labels is None |
| 823 |
|
| 824 |
|
| 825 |
class TestBuildDatasetDimensionLabelsOneOption(BuildDatasetShapeMixin): |
| 826 |
|
| 827 |
def get_base_shape_dims(self): |
| 828 |
return [None, None], ['a', 'b'] |
| 829 |
|
| 830 |
def get_dataset_inc_spec(self): |
| 831 |
dataset_inc_spec = DatasetSpec( |
| 832 |
doc='A BarData', |
| 833 |
data_type_inc='BarData', |
| 834 |
quantity='*', |
| 835 |
) |
| 836 |
return dataset_inc_spec |
| 837 |
|
| 838 |
def test_build(self): |
| 839 |
""" |
| 840 |
Test build of BarDataHolder which contains a BarData. |
| 841 |
""" |
| 842 |
# NOTE: attr1 doesn't map to anything but is required in the test container class |
| 843 |
bar_data_inst = BarData(name='my_bar', data=[[1, 2, 3], [4, 5, 6]], attr1='a string') |
| 844 |
bar_data_holder_inst = BarDataHolder( |
| 845 |
name='my_bar_holder', |
| 846 |
bar_datas=[bar_data_inst], |
| 847 |
) |
| 848 |
|
| 849 |
builder = self.manager.build(bar_data_holder_inst, source='test.h5') |
| 850 |
assert builder.datasets['my_bar'].dimension_labels == ('a', 'b') |
| 851 |
|
| 852 |
|
| 853 |
class TestBuildDatasetDimensionLabelsTwoOptionsOneMatch(BuildDatasetShapeMixin): |
| 854 |
|
| 855 |
def get_base_shape_dims(self): |
| 856 |
return [[None], [None, None]], [['a'], ['a', 'b']] |
| 857 |
|
| 858 |
def get_dataset_inc_spec(self): |
| 859 |
dataset_inc_spec = DatasetSpec( |
| 860 |
doc='A BarData', |
| 861 |
data_type_inc='BarData', |
| 862 |
quantity='*', |
| 863 |
) |
| 864 |
return dataset_inc_spec |
| 865 |
|
| 866 |
def test_build(self): |
| 867 |
""" |
| 868 |
Test build of BarDataHolder which contains a BarData. |
| 869 |
""" |
| 870 |
# NOTE: attr1 doesn't map to anything but is required in the test container class |
| 871 |
bar_data_inst = BarData(name='my_bar', data=[[1, 2, 3], [4, 5, 6]], attr1='a string') |
| 872 |
bar_data_holder_inst = BarDataHolder( |
| 873 |
name='my_bar_holder', |
| 874 |
bar_datas=[bar_data_inst], |
| 875 |
) |
| 876 |
|
| 877 |
builder = self.manager.build(bar_data_holder_inst, source='test.h5') |
| 878 |
assert builder.datasets['my_bar'].dimension_labels == ('a', 'b') |
| 879 |
|
| 880 |
|
| 881 |
class TestBuildDatasetDimensionLabelsTwoOptionsTwoMatches(BuildDatasetShapeMixin): |
| 882 |
|
| 883 |
def get_base_shape_dims(self): |
| 884 |
return [[None, None], [None, 3]], [['a', 'b1'], ['a', 'b2']] |
| 885 |
|
| 886 |
def get_dataset_inc_spec(self): |
| 887 |
dataset_inc_spec = DatasetSpec( |
| 888 |
doc='A BarData', |
| 889 |
data_type_inc='BarData', |
| 890 |
quantity='*', |
| 891 |
) |
| 892 |
return dataset_inc_spec |
| 893 |
|
| 894 |
def test_build(self): |
| 895 |
""" |
| 896 |
Test build of BarDataHolder which contains a BarData. |
| 897 |
""" |
| 898 |
# NOTE: attr1 doesn't map to anything but is required in the test container class |
| 899 |
bar_data_inst = BarData(name='my_bar', data=[[1, 2, 3], [4, 5, 6]], attr1='a string') |
| 900 |
bar_data_holder_inst = BarDataHolder( |
| 901 |
name='my_bar_holder', |
| 902 |
bar_datas=[bar_data_inst], |
| 903 |
) |
| 904 |
|
| 905 |
builder = self.manager.build(bar_data_holder_inst, source='test.h5') |
| 906 |
assert builder.datasets['my_bar'].dimension_labels == ('a', 'b2') |
| 907 |
|
| 908 |
|
| 909 |
class TestBuildDatasetDimensionLabelsOneOptionRefined(BuildDatasetShapeMixin): |
| 910 |
|
| 911 |
def get_base_shape_dims(self): |
| 912 |
return [None, None], ['a', 'b1'] |
| 913 |
|
| 914 |
def get_dataset_inc_spec(self): |
| 915 |
dataset_inc_spec = DatasetSpec( |
| 916 |
doc='A BarData', |
| 917 |
data_type_inc='BarData', |
| 918 |
quantity='*', |
| 919 |
shape=[None, 3], |
| 920 |
dims=['a', 'b2'], |
| 921 |
) |
| 922 |
return dataset_inc_spec |
| 923 |
|
| 924 |
def test_build(self): |
| 925 |
""" |
| 926 |
Test build of BarDataHolder which contains a BarData. |
| 927 |
""" |
| 928 |
# NOTE: attr1 doesn't map to anything but is required in the test container class |
| 929 |
bar_data_inst = BarData(name='my_bar', data=[[1, 2, 3], [4, 5, 6]], attr1='a string') |
| 930 |
bar_data_holder_inst = BarDataHolder( |
| 931 |
name='my_bar_holder', |
| 932 |
bar_datas=[bar_data_inst], |
| 933 |
) |
| 934 |
|
| 935 |
builder = self.manager.build(bar_data_holder_inst, source='test.h5') |
| 936 |
assert builder.datasets['my_bar'].dimension_labels == ('a', 'b2') |