| 1 |
""" |
| 2 |
Module for testing functions specific to tables containing DynamicTableRegion columns |
| 3 |
""" |
| 4 |
|
| 5 |
import warnings |
| 6 |
import numpy as np |
| 7 |
from hdmf.common import DynamicTable, AlignedDynamicTable, VectorData, DynamicTableRegion, VectorIndex |
| 8 |
from hdmf.testing import TestCase |
| 9 |
from hdmf.utils import docval, popargs, get_docval |
| 10 |
from hdmf.common.hierarchicaltable import to_hierarchical_dataframe, drop_id_columns, flatten_column_index |
| 11 |
from pandas.testing import assert_frame_equal |
| 12 |
|
| 13 |
|
| 14 |
class DynamicTableSingleDTR(DynamicTable): |
| 15 |
"""Test table class that references a single foreign table""" |
| 16 |
__columns__ = ( |
| 17 |
{'name': 'child_table_ref1', |
| 18 |
'description': 'Column with a references to the next level in the hierarchy', |
| 19 |
'required': True, |
| 20 |
'index': True, |
| 21 |
'table': True}, |
| 22 |
) |
| 23 |
|
| 24 |
@docval({'name': 'name', 'type': str, 'doc': 'The name of the table'}, |
| 25 |
{'name': 'child_table1', |
| 26 |
'type': DynamicTable, |
| 27 |
'doc': 'the child DynamicTable this DynamicTableSingleDTR point to.'}, |
| 28 |
*get_docval(DynamicTable.__init__, 'id', 'columns', 'colnames')) |
| 29 |
def __init__(self, **kwargs): |
| 30 |
# Define default name and description settings |
| 31 |
kwargs['description'] = (kwargs['name'] + " DynamicTableSingleDTR") |
| 32 |
child_table1 = popargs('child_table1', kwargs) |
| 33 |
# Initialize the DynamicTable |
| 34 |
super().__init__(**kwargs) |
| 35 |
if self['child_table_ref1'].target.table is None: |
| 36 |
self['child_table_ref1'].target.table = child_table1 |
| 37 |
|
| 38 |
|
| 39 |
class DynamicTableMultiDTR(DynamicTable): |
| 40 |
"""Test table class that references multiple related tables""" |
| 41 |
__columns__ = ( |
| 42 |
{'name': 'child_table_ref1', |
| 43 |
'description': 'Column with a references to the next level in the hierarchy', |
| 44 |
'required': True, |
| 45 |
'index': True, |
| 46 |
'table': True}, |
| 47 |
{'name': 'child_table_ref2', |
| 48 |
'description': 'Column with a references to the next level in the hierarchy', |
| 49 |
'required': True, |
| 50 |
'index': True, |
| 51 |
'table': True}, |
| 52 |
) |
| 53 |
|
| 54 |
@docval({'name': 'name', 'type': str, 'doc': 'The name of the table'}, |
| 55 |
{'name': 'child_table1', |
| 56 |
'type': DynamicTable, |
| 57 |
'doc': 'the child DynamicTable this DynamicTableSingleDTR point to.'}, |
| 58 |
{'name': 'child_table2', |
| 59 |
'type': DynamicTable, |
| 60 |
'doc': 'the child DynamicTable this DynamicTableSingleDTR point to.'}, |
| 61 |
*get_docval(DynamicTable.__init__, 'id', 'columns', 'colnames')) |
| 62 |
def __init__(self, **kwargs): |
| 63 |
# Define default name and description settings |
| 64 |
kwargs['description'] = (kwargs['name'] + " DynamicTableSingleDTR") |
| 65 |
child_table1 = popargs('child_table1', kwargs) |
| 66 |
child_table2 = popargs('child_table2', kwargs) |
| 67 |
# Initialize the DynamicTable |
| 68 |
super().__init__(**kwargs) |
| 69 |
if self['child_table_ref1'].target.table is None: |
| 70 |
self['child_table_ref1'].target.table = child_table1 |
| 71 |
if self['child_table_ref2'].target.table is None: |
| 72 |
self['child_table_ref2'].target.table = child_table2 |
| 73 |
|
| 74 |
|
| 75 |
class TestLinkedAlignedDynamicTables(TestCase): |
| 76 |
""" |
| 77 |
Test functionality specific to AlignedDynamicTables containing DynamicTableRegion columns. |
| 78 |
|
| 79 |
Since these functions only implements front-end convenient functions for DynamicTable |
| 80 |
we do not need to worry about I/O here (that is tested elsewhere), but it is sufficient if |
| 81 |
we test with container class. The only time I/O becomes relevant is on read in case that, e.g., a |
| 82 |
h5py.Dataset may behave differently than a numpy array. |
| 83 |
""" |
| 84 |
def setUp(self): |
| 85 |
""" |
| 86 |
Create basic set of linked tables consisting of |
| 87 |
|
| 88 |
aligned_table |
| 89 |
| |
| 90 |
+--> category0 ---> table_level_0_0 |
| 91 |
| |
| 92 |
+--> category1 ---> table_level_0_1 |
| 93 |
""" |
| 94 |
# Level 0 0 table. I.e., first table on level 0 |
| 95 |
self.table_level0_0 = DynamicTable(name='level0_0', description="level0_0 DynamicTable") |
| 96 |
self.table_level0_0.add_row(id=10) |
| 97 |
self.table_level0_0.add_row(id=11) |
| 98 |
self.table_level0_0.add_row(id=12) |
| 99 |
self.table_level0_0.add_row(id=13) |
| 100 |
self.table_level0_0.add_column(data=['tag1', 'tag2', 'tag2', 'tag1', 'tag3', 'tag4', 'tag5'], |
| 101 |
name='tags', |
| 102 |
description='custom tags', |
| 103 |
index=[1, 2, 4, 7]) |
| 104 |
self.table_level0_0.add_column(data=np.arange(4), |
| 105 |
name='myid', |
| 106 |
description='custom ids', |
| 107 |
index=False) |
| 108 |
# Level 0 1 table. I.e., second table on level 0 |
| 109 |
self.table_level0_1 = DynamicTable(name='level0_1', description="level0_1 DynamicTable") |
| 110 |
self.table_level0_1.add_row(id=14) |
| 111 |
self.table_level0_1.add_row(id=15) |
| 112 |
self.table_level0_1.add_row(id=16) |
| 113 |
self.table_level0_1.add_row(id=17) |
| 114 |
self.table_level0_1.add_column(data=['tag1', 'tag1', 'tag2', 'tag2', 'tag3', 'tag3', 'tag4'], |
| 115 |
name='tags', |
| 116 |
description='custom tags', |
| 117 |
index=[2, 4, 6, 7]) |
| 118 |
self.table_level0_1.add_column(data=np.arange(4), |
| 119 |
name='myid', |
| 120 |
description='custom ids', |
| 121 |
index=False) |
| 122 |
|
| 123 |
# category 0 table |
| 124 |
self.category0 = DynamicTableSingleDTR(name='category0', child_table1=self.table_level0_0) |
| 125 |
self.category0.add_row(id=0, child_table_ref1=[0, ]) |
| 126 |
self.category0.add_row(id=1, child_table_ref1=[1, 2]) |
| 127 |
self.category0.add_row(id=1, child_table_ref1=[3, ]) |
| 128 |
self.category0.add_column(data=[10, 11, 12], |
| 129 |
name='filter', |
| 130 |
description='filter value', |
| 131 |
index=False) |
| 132 |
|
| 133 |
# category 1 table |
| 134 |
self.category1 = DynamicTableSingleDTR(name='category1', child_table1=self.table_level0_1) |
| 135 |
self.category1.add_row(id=0, child_table_ref1=[0, 1]) |
| 136 |
self.category1.add_row(id=1, child_table_ref1=[2, 3]) |
| 137 |
self.category1.add_row(id=1, child_table_ref1=[1, 3]) |
| 138 |
self.category1.add_column(data=[1, 2, 3], |
| 139 |
name='filter', |
| 140 |
description='filter value', |
| 141 |
index=False) |
| 142 |
# Aligned table |
| 143 |
with warnings.catch_warnings(): |
| 144 |
msg = "The linked table for DynamicTableRegion '.*' does not share an ancestor with the DynamicTableRegion." |
| 145 |
warnings.filterwarnings("ignore", category=UserWarning, message=msg) |
| 146 |
self.aligned_table = AlignedDynamicTable( |
| 147 |
name='my_aligned_table', |
| 148 |
description='my test table', |
| 149 |
columns=[VectorData(name='a1', description='a1', data=np.arange(3)), ], |
| 150 |
colnames=['a1', ], |
| 151 |
category_tables=[self.category0, self.category1] |
| 152 |
) |
| 153 |
|
| 154 |
def tearDown(self): |
| 155 |
del self.table_level0_0 |
| 156 |
del self.table_level0_1 |
| 157 |
del self.category0 |
| 158 |
del self.category1 |
| 159 |
del self.aligned_table |
| 160 |
|
| 161 |
def test_to_hierarchical_dataframe(self): |
| 162 |
"""Test that converting an AlignedDynamicTable with links works""" |
| 163 |
hier_df = to_hierarchical_dataframe(self.aligned_table) |
| 164 |
self.assertListEqual(hier_df.columns.to_list(), |
| 165 |
[('level0_0', 'id'), ('level0_0', 'tags'), ('level0_0', 'myid')]) |
| 166 |
self.assertListEqual(hier_df.index.names, |
| 167 |
[('my_aligned_table', 'id'), ('my_aligned_table', ('my_aligned_table', 'a1')), |
| 168 |
('my_aligned_table', ('category0', 'id')), ('my_aligned_table', ('category0', 'filter')), |
| 169 |
('my_aligned_table', ('category1', 'id')), |
| 170 |
('my_aligned_table', ('category1', 'child_table_ref1')), |
| 171 |
('my_aligned_table', ('category1', 'filter'))]) |
| 172 |
self.assertListEqual(hier_df.index.to_list(), |
| 173 |
[(0, 0, 0, 10, 0, (0, 1), 1), |
| 174 |
(1, 1, 1, 11, 1, (2, 3), 2), |
| 175 |
(1, 1, 1, 11, 1, (2, 3), 2), |
| 176 |
(2, 2, 1, 12, 1, (1, 3), 3)]) |
| 177 |
self.assertListEqual(hier_df[('level0_0', 'tags')].values.tolist(), |
| 178 |
[['tag1'], ['tag2'], ['tag2', 'tag1'], ['tag3', 'tag4', 'tag5']]) |
| 179 |
|
| 180 |
def test_has_foreign_columns_in_category_tables(self): |
| 181 |
"""Test confirming working order for DynamicTableRegions in subtables""" |
| 182 |
self.assertTrue(self.aligned_table.has_foreign_columns()) |
| 183 |
self.assertFalse(self.aligned_table.has_foreign_columns(ignore_category_tables=True)) |
| 184 |
|
| 185 |
def test_has_foreign_columns_false(self): |
| 186 |
"""Test false if there are no DynamicTableRegionColumns""" |
| 187 |
temp_table = DynamicTable(name='t1', description='t1', |
| 188 |
colnames=['c1', 'c2'], |
| 189 |
columns=[VectorData(name='c1', description='c1', data=np.arange(4)), |
| 190 |
VectorData(name='c2', description='c2', data=np.arange(4))]) |
| 191 |
temp_aligned_table = AlignedDynamicTable(name='my_aligned_table', |
| 192 |
description='my test table', |
| 193 |
category_tables=[temp_table], |
| 194 |
colnames=['a1', 'a2'], |
| 195 |
columns=[VectorData(name='a1', description='c1', data=np.arange(4)), |
| 196 |
VectorData(name='a2', description='c2', data=np.arange(4))]) |
| 197 |
self.assertFalse(temp_aligned_table.has_foreign_columns()) |
| 198 |
self.assertFalse(temp_aligned_table.has_foreign_columns(ignore_category_tables=True)) |
| 199 |
|
| 200 |
def test_has_foreign_column_in_main_table(self): |
| 201 |
temp_table = DynamicTable(name='t1', description='t1', |
| 202 |
colnames=['c1', 'c2'], |
| 203 |
columns=[VectorData(name='c1', description='c1', data=np.arange(4)), |
| 204 |
VectorData(name='c2', description='c2', data=np.arange(4))]) |
| 205 |
temp_aligned_table = AlignedDynamicTable(name='my_aligned_table', |
| 206 |
description='my test table', |
| 207 |
category_tables=[temp_table], |
| 208 |
colnames=['a1', 'a2'], |
| 209 |
columns=[VectorData(name='a1', description='c1', data=np.arange(4)), |
| 210 |
DynamicTableRegion(name='a2', description='c2', |
| 211 |
data=np.arange(4), table=temp_table)]) |
| 212 |
self.assertTrue(temp_aligned_table.has_foreign_columns()) |
| 213 |
self.assertTrue(temp_aligned_table.has_foreign_columns(ignore_category_tables=True)) |
| 214 |
|
| 215 |
def test_get_foreign_columns(self): |
| 216 |
# check without subcateogries |
| 217 |
foreign_cols = self.aligned_table.get_foreign_columns(ignore_category_tables=True) |
| 218 |
self.assertListEqual(foreign_cols, []) |
| 219 |
# check with subcateogries |
| 220 |
foreign_cols = self.aligned_table.get_foreign_columns() |
| 221 |
self.assertEqual(len(foreign_cols), 2) |
| 222 |
for i, v in enumerate([('category0', 'child_table_ref1'), ('category1', 'child_table_ref1')]): |
| 223 |
self.assertTupleEqual(foreign_cols[i], v) |
| 224 |
|
| 225 |
def test_get_foreign_columns_none(self): |
| 226 |
"""Test false if there are no DynamicTableRegionColumns""" |
| 227 |
temp_table = DynamicTable(name='t1', description='t1', |
| 228 |
colnames=['c1', 'c2'], |
| 229 |
columns=[VectorData(name='c1', description='c1', data=np.arange(4)), |
| 230 |
VectorData(name='c2', description='c2', data=np.arange(4))]) |
| 231 |
temp_aligned_table = AlignedDynamicTable(name='my_aligned_table', |
| 232 |
description='my test table', |
| 233 |
category_tables=[temp_table], |
| 234 |
colnames=['a1', 'a2'], |
| 235 |
columns=[VectorData(name='a1', description='c1', data=np.arange(4)), |
| 236 |
VectorData(name='a2', description='c2', data=np.arange(4))]) |
| 237 |
self.assertListEqual(temp_aligned_table.get_foreign_columns(), []) |
| 238 |
self.assertListEqual(temp_aligned_table.get_foreign_columns(ignore_category_tables=True), []) |
| 239 |
|
| 240 |
def test_get_foreign_column_in_main_and_category_table(self): |
| 241 |
temp_table0 = DynamicTable(name='t0', description='t1', |
| 242 |
colnames=['c1', 'c2'], |
| 243 |
columns=[VectorData(name='c1', description='c1', data=np.arange(4)), |
| 244 |
VectorData(name='c2', description='c2', data=np.arange(4))]) |
| 245 |
temp_table = DynamicTable(name='t1', description='t1', |
| 246 |
colnames=['c1', 'c2'], |
| 247 |
columns=[VectorData(name='c1', description='c1', data=np.arange(4)), |
| 248 |
DynamicTableRegion(name='c2', description='c2', |
| 249 |
data=np.arange(4), table=temp_table0)]) |
| 250 |
with warnings.catch_warnings(): |
| 251 |
msg = "The linked table for DynamicTableRegion '.*' does not share an ancestor with the DynamicTableRegion." |
| 252 |
warnings.filterwarnings("ignore", category=UserWarning, message=msg) |
| 253 |
temp_aligned_table = AlignedDynamicTable(name='my_aligned_table', |
| 254 |
description='my test table', |
| 255 |
category_tables=[temp_table], |
| 256 |
colnames=['a1', 'a2'], |
| 257 |
columns=[VectorData(name='a1', description='c1', data=np.arange(4)), |
| 258 |
DynamicTableRegion(name='a2', description='c2', |
| 259 |
data=np.arange(4), table=temp_table)]) |
| 260 |
# We should get both the DynamicTableRegion from the main table and the category 't1' |
| 261 |
self.assertListEqual(temp_aligned_table.get_foreign_columns(), [(None, 'a2'), ('t1', 'c2')]) |
| 262 |
# We should only get the column from the main table |
| 263 |
self.assertListEqual(temp_aligned_table.get_foreign_columns(ignore_category_tables=True), [(None, 'a2')]) |
| 264 |
|
| 265 |
def test_get_linked_tables(self): |
| 266 |
# check without subcateogries |
| 267 |
linked_table = self.aligned_table.get_linked_tables(ignore_category_tables=True) |
| 268 |
self.assertListEqual(linked_table, []) |
| 269 |
# check with subcateogries |
| 270 |
linked_tables = self.aligned_table.get_linked_tables() |
| 271 |
self.assertEqual(len(linked_tables), 2) |
| 272 |
self.assertTupleEqual((linked_tables[0].source_table.name, |
| 273 |
linked_tables[0].source_column.name, |
| 274 |
linked_tables[0].target_table.name), |
| 275 |
('category0', 'child_table_ref1', 'level0_0')) |
| 276 |
self.assertTupleEqual((linked_tables[1].source_table.name, |
| 277 |
linked_tables[1].source_column.name, |
| 278 |
linked_tables[1].target_table.name), |
| 279 |
('category1', 'child_table_ref1', 'level0_1')) |
| 280 |
|
| 281 |
def test_get_linked_tables_none(self): |
| 282 |
"""Test false if there are no DynamicTableRegionColumns""" |
| 283 |
temp_table = DynamicTable(name='t1', description='t1', |
| 284 |
colnames=['c1', 'c2'], |
| 285 |
columns=[VectorData(name='c1', description='c1', data=np.arange(4)), |
| 286 |
VectorData(name='c2', description='c2', data=np.arange(4))]) |
| 287 |
with warnings.catch_warnings(): |
| 288 |
msg = "The linked table for DynamicTableRegion '.*' does not share an ancestor with the DynamicTableRegion." |
| 289 |
warnings.filterwarnings("ignore", category=UserWarning, message=msg) |
| 290 |
temp_aligned_table = AlignedDynamicTable(name='my_aligned_table', |
| 291 |
description='my test table', |
| 292 |
category_tables=[temp_table], |
| 293 |
colnames=['a1', 'a2'], |
| 294 |
columns=[VectorData(name='a1', description='c1', data=np.arange(4)), |
| 295 |
VectorData(name='a2', description='c2', data=np.arange(4))]) |
| 296 |
self.assertListEqual(temp_aligned_table.get_linked_tables(), []) |
| 297 |
self.assertListEqual(temp_aligned_table.get_linked_tables(ignore_category_tables=True), []) |
| 298 |
|
| 299 |
def test_get_linked_tables_complex_link(self): |
| 300 |
temp_table0 = DynamicTable(name='t0', description='t1', |
| 301 |
colnames=['c1', 'c2'], |
| 302 |
columns=[VectorData(name='c1', description='c1', data=np.arange(4)), |
| 303 |
VectorData(name='c2', description='c2', data=np.arange(4))]) |
| 304 |
temp_table = DynamicTable(name='t1', description='t1', |
| 305 |
colnames=['c1', 'c2'], |
| 306 |
columns=[VectorData(name='c1', description='c1', data=np.arange(4)), |
| 307 |
DynamicTableRegion(name='c2', description='c2', |
| 308 |
data=np.arange(4), table=temp_table0)]) |
| 309 |
with warnings.catch_warnings(): |
| 310 |
msg = "The linked table for DynamicTableRegion '.*' does not share an ancestor with the DynamicTableRegion." |
| 311 |
warnings.filterwarnings("ignore", category=UserWarning, message=msg) |
| 312 |
temp_aligned_table = AlignedDynamicTable(name='my_aligned_table', |
| 313 |
description='my test table', |
| 314 |
category_tables=[temp_table], |
| 315 |
colnames=['a1', 'a2'], |
| 316 |
columns=[VectorData(name='a1', description='c1', data=np.arange(4)), |
| 317 |
DynamicTableRegion(name='a2', description='c2', |
| 318 |
data=np.arange(4), table=temp_table)]) |
| 319 |
# NOTE: in this example templ_aligned_table both points to temp_table and at the |
| 320 |
# same time contains temp_table as a category. This could lead to temp_table |
| 321 |
# visited multiple times and we want to make sure this doesn't happen |
| 322 |
# We should get both the DynamicTableRegion from the main table and the category 't1' |
| 323 |
linked_tables = temp_aligned_table.get_linked_tables() |
| 324 |
self.assertEqual(len(linked_tables), 2) |
| 325 |
for i, v in enumerate([('my_aligned_table', 'a2', 't1'), ('t1', 'c2', 't0')]): |
| 326 |
self.assertTupleEqual((linked_tables[i].source_table.name, |
| 327 |
linked_tables[i].source_column.name, |
| 328 |
linked_tables[i].target_table.name), v) |
| 329 |
# Now, since our main table links to the category table the result should remain the same |
| 330 |
# even if we ignore the category table |
| 331 |
linked_tables = temp_aligned_table.get_linked_tables(ignore_category_tables=True) |
| 332 |
self.assertEqual(len(linked_tables), 2) |
| 333 |
for i, v in enumerate([('my_aligned_table', 'a2', 't1'), ('t1', 'c2', 't0')]): |
| 334 |
self.assertTupleEqual((linked_tables[i].source_table.name, |
| 335 |
linked_tables[i].source_column.name, |
| 336 |
linked_tables[i].target_table.name), v) |
| 337 |
|
| 338 |
def test_get_linked_tables_simple_link(self): |
| 339 |
temp_table0 = DynamicTable(name='t0', description='t1', |
| 340 |
colnames=['c1', 'c2'], |
| 341 |
columns=[VectorData(name='c1', description='c1', data=np.arange(4)), |
| 342 |
VectorData(name='c2', description='c2', data=np.arange(4))]) |
| 343 |
temp_table = DynamicTable(name='t1', description='t1', |
| 344 |
colnames=['c1', 'c2'], |
| 345 |
columns=[VectorData(name='c1', description='c1', data=np.arange(4)), |
| 346 |
DynamicTableRegion(name='c2', description='c2', |
| 347 |
data=np.arange(4), table=temp_table0)]) |
| 348 |
with warnings.catch_warnings(): |
| 349 |
msg = "The linked table for DynamicTableRegion '.*' does not share an ancestor with the DynamicTableRegion." |
| 350 |
warnings.filterwarnings("ignore", category=UserWarning, message=msg) |
| 351 |
temp_aligned_table = AlignedDynamicTable(name='my_aligned_table', |
| 352 |
description='my test table', |
| 353 |
category_tables=[temp_table], |
| 354 |
colnames=['a1', 'a2'], |
| 355 |
columns=[VectorData(name='a1', description='c1', data=np.arange(4)), |
| 356 |
DynamicTableRegion(name='a2', description='c2', |
| 357 |
data=np.arange(4), table=temp_table0)]) |
| 358 |
# NOTE: in this example temp_aligned_table and temp_table both point to temp_table0 |
| 359 |
# We should get both the DynamicTableRegion from the main table and the category 't1' |
| 360 |
linked_tables = temp_aligned_table.get_linked_tables() |
| 361 |
self.assertEqual(len(linked_tables), 2) |
| 362 |
for i, v in enumerate([('my_aligned_table', 'a2', 't0'), ('t1', 'c2', 't0')]): |
| 363 |
self.assertTupleEqual((linked_tables[i].source_table.name, |
| 364 |
linked_tables[i].source_column.name, |
| 365 |
linked_tables[i].target_table.name), v) |
| 366 |
# Since no table ever link to our category temp_table we should only get the link from our |
| 367 |
# main table here, in contrast to what happens in the test_get_linked_tables_complex_link case |
| 368 |
linked_tables = temp_aligned_table.get_linked_tables() |
| 369 |
self.assertEqual(len(linked_tables), 2) |
| 370 |
for i, v in enumerate([('my_aligned_table', 'a2', 't0'), ]): |
| 371 |
self.assertTupleEqual((linked_tables[i].source_table.name, |
| 372 |
linked_tables[i].source_column.name, |
| 373 |
linked_tables[i].target_table.name), v) |
| 374 |
|
| 375 |
|
| 376 |
class TestHierarchicalTable(TestCase): |
| 377 |
|
| 378 |
def setUp(self): |
| 379 |
""" |
| 380 |
Create basic set of linked tables consisting of |
| 381 |
|
| 382 |
super_parent_table ---> parent_table ---> aligned_table |
| 383 |
| |
| 384 |
+--> category0 |
| 385 |
""" |
| 386 |
# Level 0 0 table. I.e., first table on level 0 |
| 387 |
self.category0 = DynamicTable(name='level0_0', description="level0_0 DynamicTable") |
| 388 |
self.category0.add_row(id=10) |
| 389 |
self.category0.add_row(id=11) |
| 390 |
self.category0.add_row(id=12) |
| 391 |
self.category0.add_row(id=13) |
| 392 |
self.category0.add_column(data=['tag1', 'tag2', 'tag2', 'tag1', 'tag3', 'tag4', 'tag5'], |
| 393 |
name='tags', |
| 394 |
description='custom tags', |
| 395 |
index=[1, 2, 4, 7]) |
| 396 |
self.category0.add_column(data=np.arange(4), |
| 397 |
name='myid', |
| 398 |
description='custom ids', |
| 399 |
index=False) |
| 400 |
|
| 401 |
# Aligned table |
| 402 |
self.aligned_table = AlignedDynamicTable(name='aligned_table', |
| 403 |
description='parent_table', |
| 404 |
columns=[VectorData(name='a1', description='a1', data=np.arange(4)), ], |
| 405 |
colnames=['a1', ], |
| 406 |
category_tables=[self.category0, ]) |
| 407 |
|
| 408 |
# Parent table |
| 409 |
self.parent_table = DynamicTable(name='parent_table', |
| 410 |
description='parent_table', |
| 411 |
columns=[VectorData(name='p1', description='p1', data=np.arange(4)), |
| 412 |
DynamicTableRegion(name='l1', description='l1', |
| 413 |
data=np.arange(4), table=self.aligned_table)]) |
| 414 |
# Super-parent table |
| 415 |
dtr_sp = DynamicTableRegion(name='sl1', description='sl1', data=np.arange(4), table=self.parent_table) |
| 416 |
vi_dtr_sp = VectorIndex(name='sl1_index', data=[1, 2, 3], target=dtr_sp) |
| 417 |
self.super_parent_table = DynamicTable(name='super_parent_table', |
| 418 |
description='super_parent_table', |
| 419 |
columns=[VectorData(name='sp1', description='sp1', data=np.arange(3)), |
| 420 |
dtr_sp, vi_dtr_sp]) |
| 421 |
|
| 422 |
def tearDown(self): |
| 423 |
del self.category0 |
| 424 |
del self.aligned_table |
| 425 |
del self.parent_table |
| 426 |
|
| 427 |
def test_to_hierarchical_dataframe_no_dtr_on_top_level(self): |
| 428 |
# Cover the case where our top dtr is flat (i.e., without a VectorIndex) |
| 429 |
dtr_sp = DynamicTableRegion(name='sl1', description='sl1', data=np.arange(4), table=self.parent_table) |
| 430 |
spttable = DynamicTable(name='super_parent_table', |
| 431 |
description='super_parent_table', |
| 432 |
columns=[VectorData(name='sp1', description='sp1', data=np.arange(4)), dtr_sp]) |
| 433 |
hier_df = to_hierarchical_dataframe(spttable).reset_index() |
| 434 |
expected_columns = [('super_parent_table', 'id'), ('super_parent_table', 'sp1'), |
| 435 |
('parent_table', 'id'), ('parent_table', 'p1'), |
| 436 |
('aligned_table', 'id'), |
| 437 |
('aligned_table', ('aligned_table', 'a1')), ('aligned_table', ('level0_0', 'id')), |
| 438 |
('aligned_table', ('level0_0', 'tags')), ('aligned_table', ('level0_0', 'myid'))] |
| 439 |
self.assertListEqual(hier_df.columns.to_list(), expected_columns) |
| 440 |
|
| 441 |
def test_to_hierarchical_dataframe_indexed_dtr_on_last_level(self): |
| 442 |
# Parent table |
| 443 |
dtr_p1 = DynamicTableRegion(name='l1', description='l1', data=np.arange(4), table=self.aligned_table) |
| 444 |
vi_dtr_p1 = VectorIndex(name='sl1_index', data=[1, 2, 3], target=dtr_p1) |
| 445 |
p1 = DynamicTable(name='parent_table', description='parent_table', |
| 446 |
columns=[VectorData(name='p1', description='p1', data=np.arange(3)), dtr_p1, vi_dtr_p1]) |
| 447 |
# Super-parent table |
| 448 |
dtr_sp = DynamicTableRegion(name='sl1', description='sl1', data=np.arange(4), table=p1) |
| 449 |
vi_dtr_sp = VectorIndex(name='sl1_index', data=[1, 2, 3], target=dtr_sp) |
| 450 |
spt = DynamicTable(name='super_parent_table', description='super_parent_table', |
| 451 |
columns=[VectorData(name='sp1', description='sp1', data=np.arange(3)), dtr_sp, vi_dtr_sp]) |
| 452 |
hier_df = to_hierarchical_dataframe(spt).reset_index() |
| 453 |
expected_columns = [('super_parent_table', 'id'), ('super_parent_table', 'sp1'), |
| 454 |
('parent_table', 'id'), ('parent_table', 'p1'), |
| 455 |
('aligned_table', 'id'), |
| 456 |
('aligned_table', ('aligned_table', 'a1')), ('aligned_table', ('level0_0', 'id')), |
| 457 |
('aligned_table', ('level0_0', 'tags')), ('aligned_table', ('level0_0', 'myid'))] |
| 458 |
self.assertListEqual(hier_df.columns.to_list(), expected_columns) # make sure we have the right columns |
| 459 |
self.assertListEqual(hier_df[('aligned_table', ('level0_0', 'tags'))].to_list(), |
| 460 |
[['tag1'], ['tag2'], ['tag2', 'tag1']]) |
| 461 |
|
| 462 |
def test_to_hierarchical_dataframe_indexed_data_nparray(self): |
| 463 |
# Test that we can convert a table that contains a VectorIndex column as regular data, |
| 464 |
# i.e., it is not our DynamicTableRegion column that is index but a regular data column. |
| 465 |
# In this test the data is defined as an numpy ndarray so that an ndarray is injected |
| 466 |
# into the MultiIndex of the table. As a numpy array is not hashable this would normally |
| 467 |
# create an error when creating the MultiIndex |
| 468 |
# Parent table |
| 469 |
dtr_p1 = DynamicTableRegion(name='l1', description='l1', data=np.arange(4), table=self.aligned_table) |
| 470 |
vi_dtr_p1 = VectorIndex(name='sl1_index', data=[1, 2, 3], target=dtr_p1) |
| 471 |
p1 = DynamicTable(name='parent_table', description='parent_table', |
| 472 |
columns=[VectorData(name='p1', description='p1', data=np.arange(3)), dtr_p1, vi_dtr_p1]) |
| 473 |
# Super-parent table |
| 474 |
dtr_sp = DynamicTableRegion(name='sl1', description='sl1', data=np.arange(3), table=p1) |
| 475 |
spt = DynamicTable(name='super_parent_table', description='super_parent_table', |
| 476 |
columns=[VectorData(name='sp1', description='sp1', data=np.arange(3)), dtr_sp]) |
| 477 |
spt.add_column(name='vic', description='vic', data=np.arange(9), index=[2, 4, 6]) |
| 478 |
hier_df = to_hierarchical_dataframe(spt).reset_index() |
| 479 |
expected_columns = [('super_parent_table', 'id'), ('super_parent_table', 'sp1'), ('super_parent_table', 'vic'), |
| 480 |
('parent_table', 'id'), ('parent_table', 'p1'), |
| 481 |
('aligned_table', 'id'), |
| 482 |
('aligned_table', ('aligned_table', 'a1')), ('aligned_table', ('level0_0', 'id')), |
| 483 |
('aligned_table', ('level0_0', 'tags')), ('aligned_table', ('level0_0', 'myid'))] |
| 484 |
self.assertListEqual(hier_df.columns.to_list(), expected_columns) # make sure we have the right columns |
| 485 |
self.assertListEqual(hier_df[('aligned_table', ('level0_0', 'tags'))].to_list(), |
| 486 |
[['tag1'], ['tag2'], ['tag2', 'tag1']]) |
| 487 |
|
| 488 |
def test_to_hierarchical_dataframe_indexed_data_list(self): |
| 489 |
# Test that we can convert a table that contains a VectorIndex column as regular data, |
| 490 |
# i.e., it is not our DynamicTableRegion column that is index but a regular data column. |
| 491 |
# In this test the data is defined as an list so that a list is injected |
| 492 |
# into the MultiIndex of the table. As a list is not hashable this would normally |
| 493 |
# create an error when creating the MultiIndex |
| 494 |
# Parent table |
| 495 |
dtr_p1 = DynamicTableRegion(name='l1', description='l1', data=np.arange(4), table=self.aligned_table) |
| 496 |
vi_dtr_p1 = VectorIndex(name='sl1_index', data=[1, 2, 3], target=dtr_p1) |
| 497 |
p1 = DynamicTable(name='parent_table', description='parent_table', |
| 498 |
columns=[VectorData(name='p1', description='p1', data=np.arange(3)), dtr_p1, vi_dtr_p1]) |
| 499 |
# Super-parent table |
| 500 |
dtr_sp = DynamicTableRegion(name='sl1', description='sl1', data=np.arange(3), table=p1) |
| 501 |
spt = DynamicTable(name='super_parent_table', description='super_parent_table', |
| 502 |
columns=[VectorData(name='sp1', description='sp1', data=np.arange(3)), dtr_sp]) |
| 503 |
spt.add_column(name='vic', description='vic', data=list(range(9)), index=list([2, 4, 6])) |
| 504 |
hier_df = to_hierarchical_dataframe(spt).reset_index() |
| 505 |
expected_columns = [('super_parent_table', 'id'), ('super_parent_table', 'sp1'), ('super_parent_table', 'vic'), |
| 506 |
('parent_table', 'id'), ('parent_table', 'p1'), |
| 507 |
('aligned_table', 'id'), |
| 508 |
('aligned_table', ('aligned_table', 'a1')), ('aligned_table', ('level0_0', 'id')), |
| 509 |
('aligned_table', ('level0_0', 'tags')), ('aligned_table', ('level0_0', 'myid'))] |
| 510 |
self.assertListEqual(hier_df.columns.to_list(), expected_columns) # make sure we have the right columns |
| 511 |
self.assertListEqual(hier_df[('aligned_table', ('level0_0', 'tags'))].to_list(), |
| 512 |
[['tag1'], ['tag2'], ['tag2', 'tag1']]) |
| 513 |
|
| 514 |
def test_to_hierarchical_dataframe_empty_tables(self): |
| 515 |
# Setup empty tables with the following hierarchy |
| 516 |
# super_parent_table ---> parent_table ---> child_table |
| 517 |
a1 = DynamicTable(name='level0_0', description="level0_0 DynamicTable", |
| 518 |
columns=[VectorData(name='l0', description='l0', data=[])]) |
| 519 |
p1 = DynamicTable(name='parent_table', description='parent_table', |
| 520 |
columns=[DynamicTableRegion(name='l1', description='l1', data=[], table=a1), |
| 521 |
VectorData(name='p1c', description='l0', data=[])]) |
| 522 |
dtr_sp = DynamicTableRegion(name='sl1', description='sl1', data=np.arange(4), table=p1) |
| 523 |
vi_dtr_sp = VectorIndex(name='sl1_index', data=[], target=dtr_sp) |
| 524 |
spt = DynamicTable(name='super_parent_table', description='super_parent_table', |
| 525 |
columns=[dtr_sp, vi_dtr_sp, VectorData(name='sptc', description='l0', data=[])]) |
| 526 |
# Convert to hierarchical dataframe and make sure we get the right columns |
| 527 |
hier_df = to_hierarchical_dataframe(spt).reset_index() |
| 528 |
expected_columns = [('super_parent_table', 'id'), ('super_parent_table', 'sptc'), |
| 529 |
('parent_table', 'id'), ('parent_table', 'p1c'), |
| 530 |
('level0_0', 'id'), ('level0_0', 'l0')] |
| 531 |
self.assertListEqual(hier_df.columns.to_list(), expected_columns) |
| 532 |
|
| 533 |
def test_to_hierarchical_dataframe_multilevel(self): |
| 534 |
hier_df = to_hierarchical_dataframe(self.super_parent_table).reset_index() |
| 535 |
expected_cols = [('super_parent_table', 'id'), ('super_parent_table', 'sp1'), |
| 536 |
('parent_table', 'id'), ('parent_table', 'p1'), |
| 537 |
('aligned_table', 'id'), |
| 538 |
('aligned_table', ('aligned_table', 'a1')), |
| 539 |
('aligned_table', ('level0_0', 'id')), |
| 540 |
('aligned_table', ('level0_0', 'tags')), |
| 541 |
('aligned_table', ('level0_0', 'myid'))] |
| 542 |
# Check that we have all the columns |
| 543 |
self.assertListEqual(hier_df.columns.to_list(), expected_cols) |
| 544 |
# Spot-check the data in two columns |
| 545 |
self.assertListEqual(hier_df[('aligned_table', ('level0_0', 'tags'))].to_list(), |
| 546 |
[['tag1'], ['tag2'], ['tag2', 'tag1']]) |
| 547 |
self.assertListEqual(hier_df[('aligned_table', ('aligned_table', 'a1'))].to_list(), list(range(3))) |
| 548 |
|
| 549 |
def test_to_hierarchical_dataframe(self): |
| 550 |
hier_df = to_hierarchical_dataframe(self.parent_table) |
| 551 |
self.assertEqual(len(hier_df), 4) |
| 552 |
self.assertEqual(len(hier_df.columns), 5) |
| 553 |
self.assertEqual(len(hier_df.index.names), 2) |
| 554 |
columns = [('aligned_table', 'id'), |
| 555 |
('aligned_table', ('aligned_table', 'a1')), |
| 556 |
('aligned_table', ('level0_0', 'id')), |
| 557 |
('aligned_table', ('level0_0', 'tags')), |
| 558 |
('aligned_table', ('level0_0', 'myid'))] |
| 559 |
for i, c in enumerate(hier_df.columns): |
| 560 |
self.assertTupleEqual(c, columns[i]) |
| 561 |
index_names = [('parent_table', 'id'), ('parent_table', 'p1')] |
| 562 |
self.assertListEqual(hier_df.index.names, index_names) |
| 563 |
self.assertListEqual(hier_df.index.to_list(), [(i, i) for i in range(4)]) |
| 564 |
self.assertListEqual(hier_df[('aligned_table', ('aligned_table', 'a1'))].to_list(), list(range(4))) |
| 565 |
self.assertListEqual(hier_df[('aligned_table', ('level0_0', 'id'))].to_list(), list(range(10, 14))) |
| 566 |
self.assertListEqual(hier_df[('aligned_table', ('level0_0', 'myid'))].to_list(), list(range(4))) |
| 567 |
tags = [['tag1'], ['tag2'], ['tag2', 'tag1'], ['tag3', 'tag4', 'tag5']] |
| 568 |
for i, v in enumerate(hier_df[('aligned_table', ('level0_0', 'tags'))].to_list()): |
| 569 |
self.assertListEqual(v, tags[i]) |
| 570 |
|
| 571 |
def test_to_hierarchical_dataframe_flat_table(self): |
| 572 |
hier_df = to_hierarchical_dataframe(self.category0) |
| 573 |
assert_frame_equal(hier_df, self.category0.to_dataframe()) |
| 574 |
hier_df = to_hierarchical_dataframe(self.aligned_table) |
| 575 |
assert_frame_equal(hier_df, self.aligned_table.to_dataframe()) |
| 576 |
|
| 577 |
def test_drop_id_columns(self): |
| 578 |
hier_df = to_hierarchical_dataframe(self.parent_table) |
| 579 |
cols = hier_df.columns.to_list() |
| 580 |
mod_df = drop_id_columns(hier_df, inplace=False) |
| 581 |
expected_cols = [('aligned_table', ('aligned_table', 'a1')), |
| 582 |
('aligned_table', ('level0_0', 'tags')), |
| 583 |
('aligned_table', ('level0_0', 'myid'))] |
| 584 |
self.assertListEqual(hier_df.columns.to_list(), cols) # Test that no columns are dropped with inplace=False |
| 585 |
self.assertListEqual(mod_df.columns.to_list(), expected_cols) # Assert that we got back a modified dataframe |
| 586 |
drop_id_columns(hier_df, inplace=True) |
| 587 |
self.assertListEqual(hier_df.columns.to_list(), |
| 588 |
expected_cols) |
| 589 |
flat_df = to_hierarchical_dataframe(self.parent_table).reset_index(inplace=False) |
| 590 |
drop_id_columns(flat_df, inplace=True) |
| 591 |
self.assertListEqual(flat_df.columns.to_list(), |
| 592 |
[('parent_table', 'p1'), |
| 593 |
('aligned_table', ('aligned_table', 'a1')), |
| 594 |
('aligned_table', ('level0_0', 'tags')), |
| 595 |
('aligned_table', ('level0_0', 'myid'))]) |
| 596 |
|
| 597 |
def test_flatten_column_index(self): |
| 598 |
hier_df = to_hierarchical_dataframe(self.parent_table).reset_index() |
| 599 |
cols = hier_df.columns.to_list() |
| 600 |
expexted_cols = [('parent_table', 'id'), |
| 601 |
('parent_table', 'p1'), |
| 602 |
('aligned_table', 'id'), |
| 603 |
('aligned_table', 'aligned_table', 'a1'), |
| 604 |
('aligned_table', 'level0_0', 'id'), |
| 605 |
('aligned_table', 'level0_0', 'tags'), |
| 606 |
('aligned_table', 'level0_0', 'myid')] |
| 607 |
df = flatten_column_index(hier_df, inplace=False) |
| 608 |
# Test that our columns have not changed with inplace=False |
| 609 |
self.assertListEqual(hier_df.columns.to_list(), cols) |
| 610 |
self.assertListEqual(df.columns.to_list(), expexted_cols) # make sure we got back a modified dataframe |
| 611 |
flatten_column_index(hier_df, inplace=True) # make sure we can also directly flatten inplace |
| 612 |
self.assertListEqual(hier_df.columns.to_list(), expexted_cols) |
| 613 |
# Test that we can apply flatten_column_index again on our already modified dataframe to reduce the levels |
| 614 |
flatten_column_index(hier_df, inplace=True, max_levels=2) |
| 615 |
expexted_cols = [('parent_table', 'id'), ('parent_table', 'p1'), ('aligned_table', 'id'), |
| 616 |
('aligned_table', 'a1'), ('level0_0', 'id'), ('level0_0', 'tags'), ('level0_0', 'myid')] |
| 617 |
self.assertListEqual(hier_df.columns.to_list(), expexted_cols) |
| 618 |
# Test that we can directly reduce the max_levels to just 1 |
| 619 |
hier_df = to_hierarchical_dataframe(self.parent_table).reset_index() |
| 620 |
flatten_column_index(hier_df, inplace=True, max_levels=1) |
| 621 |
expexted_cols = ['id', 'p1', 'id', 'a1', 'id', 'tags', 'myid'] |
| 622 |
self.assertListEqual(hier_df.columns.to_list(), expexted_cols) |
| 623 |
|
| 624 |
def test_flatten_column_index_already_flat_index(self): |
| 625 |
hier_df = to_hierarchical_dataframe(self.parent_table).reset_index() |
| 626 |
flatten_column_index(hier_df, inplace=True, max_levels=1) |
| 627 |
expexted_cols = ['id', 'p1', 'id', 'a1', 'id', 'tags', 'myid'] |
| 628 |
self.assertListEqual(hier_df.columns.to_list(), expexted_cols) |
| 629 |
# Now try to flatten the already flat columns again to make sure nothing changes |
| 630 |
flatten_column_index(hier_df, inplace=True, max_levels=1) |
| 631 |
self.assertListEqual(hier_df.columns.to_list(), expexted_cols) |
| 632 |
|
| 633 |
def test_flatten_column_index_bad_maxlevels(self): |
| 634 |
hier_df = to_hierarchical_dataframe(self.parent_table) |
| 635 |
with self.assertRaisesWith(ValueError, 'max_levels must be greater than 0'): |
| 636 |
flatten_column_index(dataframe=hier_df, inplace=True, max_levels=-1) |
| 637 |
with self.assertRaisesWith(ValueError, 'max_levels must be greater than 0'): |
| 638 |
flatten_column_index(dataframe=hier_df, inplace=True, max_levels=0) |
| 639 |
|
| 640 |
|
| 641 |
class TestLinkedDynamicTables(TestCase): |
| 642 |
""" |
| 643 |
Test functionality specific to DynamicTables containing DynamicTableRegion columns. |
| 644 |
|
| 645 |
Since these functions only implements front-end convenient functions for DynamicTable |
| 646 |
we do not need to worry about I/O here (that is tested elsewhere), ut it is sufficient if |
| 647 |
we test with container class. The only time I/O becomes relevant is on read in case that, e.g., a |
| 648 |
h5py.Dataset may behave differently than a numpy array. |
| 649 |
""" |
| 650 |
def setUp(self): |
| 651 |
""" |
| 652 |
Create basic set of linked tables consisting of |
| 653 |
|
| 654 |
table_level2 ---> table_level1 ----> table_level_0_0 |
| 655 |
\ |
| 656 |
------> table_level_0_1 |
| 657 |
|
| 658 |
""" |
| 659 |
self.table_level0_0 = DynamicTable(name='level0_0', description="level0_0 DynamicTable") |
| 660 |
self.table_level0_1 = DynamicTable(name='level0_1', description="level0_1 DynamicTable") |
| 661 |
self.table_level1 = DynamicTableMultiDTR(name='level1', |
| 662 |
child_table1=self.table_level0_0, |
| 663 |
child_table2=self.table_level0_1) |
| 664 |
self.table_level2 = DynamicTableSingleDTR(name='level2', child_table1=self.table_level1) |
| 665 |
|
| 666 |
def tearDown(self): |
| 667 |
del self.table_level0_0 |
| 668 |
del self.table_level0_1 |
| 669 |
del self.table_level1 |
| 670 |
del self.table_level2 |
| 671 |
|
| 672 |
def popolate_tables(self): |
| 673 |
"""Helper function to populate our tables generate in setUp with some simple data""" |
| 674 |
# Level 0 0 table. I.e., first table on level 0 |
| 675 |
self.table_level0_0.add_row(id=10) |
| 676 |
self.table_level0_0.add_row(id=11) |
| 677 |
self.table_level0_0.add_row(id=12) |
| 678 |
self.table_level0_0.add_row(id=13) |
| 679 |
self.table_level0_0.add_column(data=['tag1', 'tag2', 'tag2', 'tag1', 'tag3', 'tag4', 'tag5'], |
| 680 |
name='tags', |
| 681 |
description='custom tags', |
| 682 |
index=[1, 2, 4, 7]) |
| 683 |
self.table_level0_0.add_column(data=np.arange(4), |
| 684 |
name='myid', |
| 685 |
description='custom ids', |
| 686 |
index=False) |
| 687 |
# Level 0 1 table. I.e., second table on level 0 |
| 688 |
self.table_level0_1.add_row(id=14) |
| 689 |
self.table_level0_1.add_row(id=15) |
| 690 |
self.table_level0_1.add_row(id=16) |
| 691 |
self.table_level0_1.add_row(id=17) |
| 692 |
self.table_level0_1.add_column(data=['tag1', 'tag1', 'tag2', 'tag2', 'tag3', 'tag3', 'tag4'], |
| 693 |
name='tags', |
| 694 |
description='custom tags', |
| 695 |
index=[2, 4, 6, 7]) |
| 696 |
self.table_level0_1.add_column(data=np.arange(4), |
| 697 |
name='myid', |
| 698 |
description='custom ids', |
| 699 |
index=False) |
| 700 |
# Level 1 table |
| 701 |
self.table_level1.add_row(id=0, child_table_ref1=[0, 1], child_table_ref2=[0]) |
| 702 |
self.table_level1.add_row(id=1, child_table_ref1=[2], child_table_ref2=[1, 2]) |
| 703 |
self.table_level1.add_row(id=2, child_table_ref1=[3], child_table_ref2=[3]) |
| 704 |
self.table_level1.add_column(data=['tag1', 'tag2', 'tag2'], |
| 705 |
name='tag', |
| 706 |
description='custom tag', |
| 707 |
index=False) |
| 708 |
self.table_level1.add_column(data=['tag1', 'tag2', 'tag2', 'tag3', 'tag3', 'tag4', 'tag5'], |
| 709 |
name='tags', |
| 710 |
description='custom tags', |
| 711 |
index=[2, 4, 7]) |
| 712 |
# Level 2 data |
| 713 |
self.table_level2.add_row(id=0, child_table_ref1=[0, ]) |
| 714 |
self.table_level2.add_row(id=1, child_table_ref1=[1, 2]) |
| 715 |
self.table_level2.add_column(data=[10, 12], |
| 716 |
name='filter', |
| 717 |
description='filter value', |
| 718 |
index=False) |
| 719 |
|
| 720 |
def test_populate_table_hierarchy(self): |
| 721 |
"""Test that just checks that populating the tables with data works correctly""" |
| 722 |
self.popolate_tables() |
| 723 |
# Check level0 0 data |
| 724 |
self.assertListEqual(self.table_level0_0.id[:], np.arange(10, 14, 1).tolist()) |
| 725 |
self.assertListEqual(self.table_level0_0['tags'][:], |
| 726 |
[['tag1'], ['tag2'], ['tag2', 'tag1'], ['tag3', 'tag4', 'tag5']]) |
| 727 |
self.assertListEqual(self.table_level0_0['myid'][:].tolist(), np.arange(0, 4, 1).tolist()) |
| 728 |
# Check level0 1 data |
| 729 |
self.assertListEqual(self.table_level0_1.id[:], np.arange(14, 18, 1).tolist()) |
| 730 |
self.assertListEqual(self.table_level0_1['tags'][:], |
| 731 |
[['tag1', 'tag1'], ['tag2', 'tag2'], ['tag3', 'tag3'], ['tag4']]) |
| 732 |
self.assertListEqual(self.table_level0_1['myid'][:].tolist(), np.arange(0, 4, 1).tolist()) |
| 733 |
# Check level1 data |
| 734 |
self.assertListEqual(self.table_level1.id[:], np.arange(0, 3, 1).tolist()) |
| 735 |
self.assertListEqual(self.table_level1['tag'][:], ['tag1', 'tag2', 'tag2']) |
| 736 |
self.assertTrue(self.table_level1['child_table_ref1'].target.table is self.table_level0_0) |
| 737 |
self.assertTrue(self.table_level1['child_table_ref2'].target.table is self.table_level0_1) |
| 738 |
self.assertEqual(len(self.table_level1['child_table_ref1'].target.table), 4) |
| 739 |
self.assertEqual(len(self.table_level1['child_table_ref2'].target.table), 4) |
| 740 |
# Check level2 data |
| 741 |
self.assertListEqual(self.table_level2.id[:], np.arange(0, 2, 1).tolist()) |
| 742 |
self.assertListEqual(self.table_level2['filter'][:], [10, 12]) |
| 743 |
self.assertTrue(self.table_level2['child_table_ref1'].target.table is self.table_level1) |
| 744 |
self.assertEqual(len(self.table_level2['child_table_ref1'].target.table), 3) |
| 745 |
|
| 746 |
def test_get_foreign_columns(self): |
| 747 |
"""Test DynamicTable.get_foreign_columns""" |
| 748 |
self.popolate_tables() |
| 749 |
self.assertListEqual(self.table_level0_0.get_foreign_columns(), []) |
| 750 |
self.assertListEqual(self.table_level0_1.get_foreign_columns(), []) |
| 751 |
self.assertListEqual(self.table_level1.get_foreign_columns(), ['child_table_ref1', 'child_table_ref2']) |
| 752 |
self.assertListEqual(self.table_level2.get_foreign_columns(), ['child_table_ref1']) |
| 753 |
|
| 754 |
def test_has_foreign_columns(self): |
| 755 |
"""Test DynamicTable.get_foreign_columns""" |
| 756 |
self.popolate_tables() |
| 757 |
self.assertFalse(self.table_level0_0.has_foreign_columns()) |
| 758 |
self.assertFalse(self.table_level0_1.has_foreign_columns()) |
| 759 |
self.assertTrue(self.table_level1.has_foreign_columns()) |
| 760 |
self.assertTrue(self.table_level2.has_foreign_columns()) |
| 761 |
|
| 762 |
def test_get_linked_tables(self): |
| 763 |
"""Test DynamicTable.get_linked_tables""" |
| 764 |
self.popolate_tables() |
| 765 |
# check level0_0 |
| 766 |
self.assertListEqual(self.table_level0_0.get_linked_tables(), []) |
| 767 |
# check level0_0 |
| 768 |
self.assertListEqual(self.table_level0_1.get_linked_tables(), []) |
| 769 |
# check level1 |
| 770 |
temp = self.table_level1.get_linked_tables() |
| 771 |
self.assertEqual(len(temp), 2) |
| 772 |
self.assertEqual(temp[0].source_table.name, self.table_level1.name) |
| 773 |
self.assertEqual(temp[0].source_column.name, 'child_table_ref1') |
| 774 |
self.assertEqual(temp[0].target_table.name, self.table_level0_0.name) |
| 775 |
self.assertEqual(temp[1].source_table.name, self.table_level1.name) |
| 776 |
self.assertEqual(temp[1].source_column.name, 'child_table_ref2') |
| 777 |
self.assertEqual(temp[1].target_table.name, self.table_level0_1.name) |
| 778 |
# check level2 |
| 779 |
temp = self.table_level2.get_linked_tables() |
| 780 |
self.assertEqual(len(temp), 3) |
| 781 |
self.assertEqual(temp[0].source_table.name, self.table_level2.name) |
| 782 |
self.assertEqual(temp[0].source_column.name, 'child_table_ref1') |
| 783 |
self.assertEqual(temp[0].target_table.name, self.table_level1.name) |
| 784 |
self.assertEqual(temp[1].source_table.name, self.table_level1.name) |
| 785 |
self.assertEqual(temp[1].source_column.name, 'child_table_ref1') |
| 786 |
self.assertEqual(temp[1].target_table.name, self.table_level0_0.name) |
| 787 |
self.assertEqual(temp[2].source_table.name, self.table_level1.name) |
| 788 |
self.assertEqual(temp[2].source_column.name, 'child_table_ref2') |
| 789 |
self.assertEqual(temp[2].target_table.name, self.table_level0_1.name) |