Debian Codegraph @

tests/unit/common/test_alignedtable.py

raw
1 import numpy as np
2 from pandas.testing import assert_frame_equal
3 import warnings
4
5 from hdmf.backends.hdf5 import HDF5IO
6 from hdmf.common import DynamicTable, VectorData, get_manager, AlignedDynamicTable, DynamicTableRegion
7 from hdmf.testing import TestCase, remove_test_file
8
9
10 class TestAlignedDynamicTableContainer(TestCase):
11 """
12 Test the AlignedDynamicTable Container class.
13
14 NOTE: Functions specific to linked tables, specifically the:
15 * has_foreign_columns
16 * get_foreign_columns
17 * get_linked_tables
18 methods are tested in the test_linkedtables.TestLinkedAlignedDynamicTables class instead of here.
19 """
20 def setUp(self):
21 warnings.simplefilter("always") # Trigger all warnings
22 self.path = 'test_icephys_meta_intracellularrecording.h5'
23
24 def tearDown(self):
25 remove_test_file(self.path)
26
27 def test_init(self):
28 """Test that just checks that populating the tables with data works correctly"""
29 AlignedDynamicTable(
30 name='test_aligned_table',
31 description='Test aligned container')
32
33 def test_init_categories_without_category_tables_error(self):
34 # Test raise error if categories is given without category_tables
35 with self.assertRaisesWith(ValueError, "Categories provided but no category_tables given"):
36 AlignedDynamicTable(
37 name='test_aligned_table',
38 description='Test aligned container',
39 categories=['cat1', 'cat2'])
40
41 def test_init_length_mismatch_between_categories_and_category_tables(self):
42 # Test length mismatch between categories and category_tables
43 with self.assertRaisesWith(ValueError, "0 category_tables given but 2 categories specified"):
44 AlignedDynamicTable(
45 name='test_aligned_table',
46 description='Test aligned container',
47 categories=['cat1', 'cat2'],
48 category_tables=[])
49
50 def test_init_category_table_names_do_not_match_categories(self):
51 # Construct some categories for testing
52 category_names = ['test1', 'test2', 'test3']
53 num_rows = 10
54 categories = [DynamicTable(name=val,
55 description=val+" description",
56 columns=[VectorData(name=val+t,
57 description=val+t+' description',
58 data=np.arange(num_rows)) for t in ['c1', 'c2', 'c3']]
59 ) for val in category_names]
60 # Test add category_table that is not listed in the categories list
61 with self.assertRaisesWith(ValueError,
62 "DynamicTable test3 does not appear in categories ['test1', 'test2', 't3']"):
63 AlignedDynamicTable(
64 name='test_aligned_table',
65 description='Test aligned container',
66 categories=['test1', 'test2', 't3'], # bad name for 'test3'
67 category_tables=categories)
68
69 def test_init_duplicate_category_table_name(self):
70 # Test duplicate table name
71 with self.assertRaisesWith(ValueError, "Duplicate table name test1 found in input dynamic_tables"):
72 categories = [DynamicTable(name=val,
73 description=val+" description",
74 columns=[VectorData(name=val+t,
75 description=val+t+' description',
76 data=np.arange(10)) for t in ['c1', 'c2', 'c3']]
77 ) for val in ['test1', 'test1', 'test3']]
78 AlignedDynamicTable(
79 name='test_aligned_table',
80 description='Test aligned container',
81 categories=['test1', 'test2', 'test3'],
82 category_tables=categories)
83
84 def test_init_misaligned_category_tables(self):
85 """Test misaligned category tables"""
86 categories = [DynamicTable(name=val,
87 description=val+" description",
88 columns=[VectorData(name=val+t,
89 description=val+t+' description',
90 data=np.arange(10)) for t in ['c1', 'c2', 'c3']]
91 ) for val in ['test1', 'test2']]
92 categories.append(DynamicTable(name='test3',
93 description="test3 description",
94 columns=[VectorData(name='test3 '+t,
95 description='test3 '+t+' description',
96 data=np.arange(8)) for t in ['c1', 'c2', 'c3']]))
97 with self.assertRaisesWith(ValueError,
98 "Category DynamicTable test3 does not align, it has 8 rows expected 10"):
99 AlignedDynamicTable(
100 name='test_aligned_table',
101 description='Test aligned container',
102 categories=['test1', 'test2', 'test3'],
103 category_tables=categories)
104
105 def test_init_with_custom_empty_categories(self):
106 """Test that we can create an empty table with custom categories"""
107 category_names = ['test1', 'test2', 'test3']
108 categories = [DynamicTable(name=val, description=val+" description") for val in category_names]
109 AlignedDynamicTable(
110 name='test_aligned_table',
111 description='Test aligned container',
112 category_tables=categories)
113
114 def test_init_with_custom_nonempty_categories(self):
115 """Test that we can create an empty table with custom categories"""
116 category_names = ['test1', 'test2', 'test3']
117 num_rows = 10
118 categories = [DynamicTable(name=val,
119 description=val+" description",
120 columns=[VectorData(name=val+t,
121 description=val+t+' description',
122 data=np.arange(num_rows)) for t in ['c1', 'c2', 'c3']]
123 ) for val in category_names]
124 temp = AlignedDynamicTable(
125 name='test_aligned_table',
126 description='Test aligned container',
127 category_tables=categories)
128 self.assertEqual(temp.categories, category_names)
129
130 def test_init_with_custom_nonempty_categories_and_main(self):
131 """
132 Test that we can create a non-empty table with custom non-empty categories
133 """
134 category_names = ['test1', 'test2', 'test3']
135 num_rows = 10
136 categories = [DynamicTable(name=val,
137 description=val+" description",
138 columns=[VectorData(name=t,
139 description=val+t+' description',
140 data=np.arange(num_rows)) for t in ['c1', 'c2', 'c3']]
141 ) for val in category_names]
142 temp = AlignedDynamicTable(
143 name='test_aligned_table',
144 description='Test aligned container',
145 category_tables=categories,
146 columns=[VectorData(name='main_' + t,
147 description='main_'+t+'_description',
148 data=np.arange(num_rows)) for t in ['c1', 'c2', 'c3']])
149
150 self.assertEqual(temp.categories, category_names)
151 self.assertTrue('test1' in temp) # test that contains category works
152 self.assertTrue(('test1', 'c1') in temp) # test that contains a column works
153 # test the error case of a tuple with len !=2
154 with self.assertRaisesWith(ValueError, "Expected tuple of strings of length 2 got tuple of length 3"):
155 ('test1', 'c1', 't3') in temp
156 self.assertTupleEqual(temp.colnames, ('main_c1', 'main_c2', 'main_c3')) # confirm column names
157
158 def test_init_with_custom_misaligned_categories(self):
159 """Test that we cannot create an empty table with custom categories"""
160 num_rows = 10
161 val1 = 'test1'
162 val2 = 'test2'
163 categories = [DynamicTable(name=val1,
164 description=val1+" description",
165 columns=[VectorData(name=val1+t,
166 description=val1+t+' description',
167 data=np.arange(num_rows)) for t in ['c1', 'c2', 'c3']]),
168 DynamicTable(name=val2,
169 description=val2+" description",
170 columns=[VectorData(name=val2+t,
171 description=val2+t+' description',
172 data=np.arange(num_rows+1)) for t in ['c1', 'c2', 'c3']])
173 ]
174 with self.assertRaisesWith(ValueError,
175 "Category DynamicTable test2 does not align, it has 11 rows expected 10"):
176 AlignedDynamicTable(
177 name='test_aligned_table',
178 description='Test aligned container',
179 category_tables=categories)
180
181 def test_init_with_duplicate_custom_categories(self):
182 """Test that we can create an empty table with custom categories"""
183 category_names = ['test1', 'test1']
184 num_rows = 10
185 categories = [DynamicTable(name=val,
186 description=val+" description",
187 columns=[VectorData(name=val+t,
188 description=val+t+' description',
189 data=np.arange(num_rows)) for t in ['c1', 'c2', 'c3']]
190 ) for val in category_names]
191 with self.assertRaisesWith(ValueError, "Duplicate table name test1 found in input dynamic_tables"):
192 AlignedDynamicTable(
193 name='test_aligned_table',
194 description='Test aligned container',
195 category_tables=categories)
196
197 def test_init_with_bad_custom_categories(self):
198 """Test that we cannot provide a category that is not a DynamicTable"""
199 num_rows = 10
200 categories = [ # good category
201 DynamicTable(name='test1',
202 description="test1 description",
203 columns=[VectorData(name='test1'+t,
204 description='test1' + t + ' description',
205 data=np.arange(num_rows)) for t in ['c1', 'c2', 'c3']]
206 ),
207 # use a list as a bad category example
208 [0, 1, 2]]
209 with self.assertRaisesWith(ValueError, "Category table with index 1 is not a DynamicTable"):
210 AlignedDynamicTable(
211 name='test_aligned_table',
212 description='Test aligned container',
213 category_tables=categories)
214
215 def test_round_trip_container(self):
216 """Test read and write the container by itself"""
217 category_names = ['test1', 'test2', 'test3']
218 num_rows = 10
219 categories = [DynamicTable(name=val,
220 description=val+" description",
221 columns=[VectorData(name=t,
222 description=val+t+' description',
223 data=np.arange(num_rows)) for t in ['c1', 'c2', 'c3']]
224 ) for val in category_names]
225 curr = AlignedDynamicTable(
226 name='test_aligned_table',
227 description='Test aligned container',
228 category_tables=categories)
229
230 with HDF5IO(self.path, manager=get_manager(), mode='w') as io:
231 io.write(curr)
232
233 with HDF5IO(self.path, manager=get_manager(), mode='r') as io:
234 incon = io.read()
235 self.assertListEqual(incon.categories, curr.categories)
236 for n in category_names:
237 assert_frame_equal(incon[n], curr[n])
238
239 def test_add_category(self):
240 """Test that we can correct a non-empty category to an existing table"""
241 category_names = ['test1', 'test2', 'test3']
242 num_rows = 10
243 categories = [DynamicTable(name=val,
244 description=val+" description",
245 columns=[VectorData(name=val+t,
246 description=val+t+' description',
247 data=np.arange(num_rows)) for t in ['c1', 'c2', 'c3']]
248 ) for val in category_names]
249 adt = AlignedDynamicTable(
250 name='test_aligned_table',
251 description='Test aligned container',
252 category_tables=categories[0:2])
253 self.assertListEqual(adt.categories, category_names[0:2])
254 adt.add_category(categories[-1])
255 self.assertListEqual(adt.categories, category_names)
256
257 def test_add_category_misaligned_rows(self):
258 """Test that we can correct a non-empty category to an existing table"""
259 category_names = ['test1', 'test2']
260 num_rows = 10
261 categories = [DynamicTable(name=val,
262 description=val+" description",
263 columns=[VectorData(name=val+t,
264 description=val+t+' description',
265 data=np.arange(num_rows)) for t in ['c1', 'c2', 'c3']]
266 ) for val in category_names]
267 adt = AlignedDynamicTable(
268 name='test_aligned_table',
269 description='Test aligned container',
270 category_tables=categories)
271 self.assertListEqual(adt.categories, category_names)
272 with self.assertRaisesWith(ValueError, "New category DynamicTable does not align, it has 8 rows expected 10"):
273 adt.add_category(DynamicTable(name='test3',
274 description='test3_description',
275 columns=[VectorData(name='test3_'+t,
276 description='test3 '+t+' description',
277 data=np.arange(num_rows - 2)) for t in ['c1', 'c2', 'c3']
278 ]))
279
280 def test_add_category_already_in_table(self):
281 category_names = ['test1', 'test2', 'test2']
282 num_rows = 10
283 categories = [DynamicTable(name=val,
284 description=val+" description",
285 columns=[VectorData(name=val+t,
286 description=val+t+' description',
287 data=np.arange(num_rows)) for t in ['c1', 'c2', 'c3']]
288 ) for val in category_names]
289 adt = AlignedDynamicTable(
290 name='test_aligned_table',
291 description='Test aligned container',
292 category_tables=categories[0:2])
293 self.assertListEqual(adt.categories, category_names[0:2])
294 with self.assertRaisesWith(ValueError, "Category test2 already in the table"):
295 adt.add_category(categories[-1])
296
297 def test_add_column(self):
298 adt = AlignedDynamicTable(
299 name='test_aligned_table',
300 description='Test aligned container',
301 columns=[VectorData(name='test_'+t,
302 description='test_'+t+' description',
303 data=np.arange(10)) for t in ['c1', 'c2', 'c3']])
304 # Test successful add
305 adt.add_column(name='testA', description='testA', data=np.arange(10))
306 self.assertTupleEqual(adt.colnames, ('test_c1', 'test_c2', 'test_c3', 'testA'))
307
308 def test_add_column_bad_category(self):
309 """Test add column with bad category"""
310 adt = AlignedDynamicTable(
311 name='test_aligned_table',
312 description='Test aligned container',
313 columns=[VectorData(name='test_'+t,
314 description='test_'+t+' description',
315 data=np.arange(10)) for t in ['c1', 'c2', 'c3']])
316 with self.assertRaisesWith(KeyError, "'Category mycat not in table'"):
317 adt.add_column(category='mycat', name='testA', description='testA', data=np.arange(10))
318
319 def test_add_column_bad_length(self):
320 """Test add column that is too short"""
321 adt = AlignedDynamicTable(
322 name='test_aligned_table',
323 description='Test aligned container',
324 columns=[VectorData(name='test_'+t,
325 description='test_'+t+' description',
326 data=np.arange(10)) for t in ['c1', 'c2', 'c3']])
327 # Test successful add
328 with self.assertRaisesWith(ValueError, "column must have the same number of rows as 'id'"):
329 adt.add_column(name='testA', description='testA', data=np.arange(8))
330
331 def test_add_column_to_subcategory(self):
332 """Test adding a column to a subcategory"""
333 category_names = ['test1', 'test2', 'test3']
334 num_rows = 10
335 categories = [DynamicTable(name=val,
336 description=val+" description",
337 columns=[VectorData(name=val+t,
338 description=val+t+' description',
339 data=np.arange(num_rows)) for t in ['c1', 'c2', 'c3']]
340 ) for val in category_names]
341 adt = AlignedDynamicTable(
342 name='test_aligned_table',
343 description='Test aligned container',
344 category_tables=categories)
345 self.assertListEqual(adt.categories, category_names)
346 # Test successful add
347 adt.add_column(category='test2', name='testA', description='testA', data=np.arange(10))
348 self.assertTupleEqual(adt.get_category('test2').colnames, ('test2c1', 'test2c2', 'test2c3', 'testA'))
349
350 def test_add_row(self):
351 """Test adding a row to a non_empty table"""
352 category_names = ['test1', ]
353 num_rows = 10
354 categories = [DynamicTable(name=val,
355 description=val+" description",
356 columns=[VectorData(name=t,
357 description=val+t+' description',
358 data=np.arange(num_rows)) for t in ['c1', 'c2']]
359 ) for val in category_names]
360 temp = AlignedDynamicTable(
361 name='test_aligned_table',
362 description='Test aligned container',
363 category_tables=categories,
364 columns=[VectorData(name='main_' + t,
365 description='main_'+t+'_description',
366 data=np.arange(num_rows)) for t in ['c1', 'c2']])
367 self.assertListEqual(temp.categories, category_names)
368 # Test successful add
369 temp.add_row(test1=dict(c1=1, c2=2), main_c1=3, main_c2=5)
370 self.assertListEqual(temp[10].iloc[0].tolist(), [3, 5, 10, 1, 2])
371 # Test successful add version 2
372 temp.add_row(data=dict(test1=dict(c1=1, c2=2), main_c1=4, main_c2=5))
373 self.assertListEqual(temp[11].iloc[0].tolist(), [4, 5, 11, 1, 2])
374 # Test missing categories data
375 with self.assertRaises(KeyError) as ke:
376 temp.add_row(main_c1=3, main_c2=5)
377 self.assertTrue("row data keys do not match" in str(ke.exception))
378
379 def test_get_item(self):
380 """Test getting elements from the table"""
381 category_names = ['test1', ]
382 num_rows = 10
383 categories = [DynamicTable(name=val,
384 description=val+" description",
385 columns=[VectorData(name=t,
386 description=val+t+' description',
387 data=np.arange(num_rows) + i + 3)
388 for i, t in enumerate(['c1', 'c2'])]
389 ) for val in category_names]
390 temp = AlignedDynamicTable(
391 name='test_aligned_table',
392 description='Test aligned container',
393 category_tables=categories,
394 columns=[VectorData(name='main_' + t,
395 description='main_'+t+'_description',
396 data=np.arange(num_rows)+2) for t in ['c1', 'c2']])
397 self.assertListEqual(temp.categories, category_names)
398 # Test slicing with a single index
399 self.assertListEqual(temp[5].iloc[0].tolist(), [7, 7, 5, 8, 9])
400 # Test slice with list
401 self.assertListEqual(temp[[5, 7]].iloc[0].tolist(), [7, 7, 5, 8, 9])
402 self.assertListEqual(temp[[5, 7]].iloc[1].tolist(), [9, 9, 7, 10, 11])
403 # Test slice with slice
404 self.assertListEqual(temp[5:7].iloc[0].tolist(), [7, 7, 5, 8, 9])
405 self.assertListEqual(temp[5:7].iloc[1].tolist(), [8, 8, 6, 9, 10])
406 # Test slice with numpy index array
407 self.assertListEqual(temp[np.asarray([5, 8])].iloc[0].tolist(), [7, 7, 5, 8, 9])
408 self.assertListEqual(temp[np.asarray([5, 8])].iloc[1].tolist(), [10, 10, 8, 11, 12])
409 # Test slicing for a single column
410 self.assertListEqual(temp['main_c1'][:].tolist(), (np.arange(num_rows)+2).tolist())
411 # Test slicing for a single category
412 assert_frame_equal(temp['test1'], categories[0].to_dataframe())
413 # Test getting the main table
414 assert_frame_equal(temp[None], temp.to_dataframe())
415 # Test getting a specific column
416 self.assertListEqual(temp['test1', 'c1'][:].tolist(), (np.arange(num_rows) + 3).tolist())
417 # Test getting a specific cell
418 self.assertEqual(temp[None, 'main_c1', 1], 3)
419 self.assertEqual(temp[1, None, 'main_c1'], 3)
420 # Test bad selection tuple
421 with self.assertRaisesWith(ValueError,
422 "Expected tuple of length 2 of the form [category, column], [row, category], "
423 "[row, (category, column)] or a tuple of length 3 of the form "
424 "[category, column, row], [row, category, column]"):
425 temp[('main_c1',)]
426 # Test selecting a single cell or row of a category table by having a
427 # [int, str] or [int, (str, str)] type selection
428 # Select row 0 from category 'test1'
429 re = temp[0, 'test1']
430 self.assertListEqual(re.columns.to_list(), ['id', 'c1', 'c2'])
431 self.assertListEqual(re.index.names, [('test_aligned_table', 'id')])
432 self.assertListEqual(re.values.tolist()[0], [0, 3, 4])
433 # Select a single cell from a column
434 self.assertEqual(temp[1, ('test_aligned_table', 'main_c1')], 3)
435
436 def test_to_dataframe(self):
437 """Test that the to_dataframe method works"""
438 category_names = ['test1', 'test2', 'test3']
439 num_rows = 10
440 categories = [DynamicTable(name=val,
441 description=val+" description",
442 columns=[VectorData(name=t,
443 description=val+t+' description',
444 data=np.arange(num_rows)) for t in ['c1', 'c2', 'c3']]
445 ) for val in category_names]
446 adt = AlignedDynamicTable(
447 name='test_aligned_table',
448 description='Test aligned container',
449 category_tables=categories,
450 columns=[VectorData(name='main_' + t,
451 description='main_'+t+'_description',
452 data=np.arange(num_rows)) for t in ['c1', 'c2', 'c3']])
453
454 # Test the to_dataframe method with default settings
455 tdf = adt.to_dataframe()
456 self.assertListEqual(tdf.index.tolist(), list(range(10)))
457 self.assertTupleEqual(tdf.index.name, ('test_aligned_table', 'id'))
458 expected_cols = [('test_aligned_table', 'main_c1'),
459 ('test_aligned_table', 'main_c2'),
460 ('test_aligned_table', 'main_c3'),
461 ('test1', 'id'), ('test1', 'c1'), ('test1', 'c2'), ('test1', 'c3'),
462 ('test2', 'id'), ('test2', 'c1'), ('test2', 'c2'), ('test2', 'c3'),
463 ('test3', 'id'), ('test3', 'c1'), ('test3', 'c2'), ('test3', 'c3')]
464 tdf_cols = tdf.columns.tolist()
465 for v in zip(expected_cols, tdf_cols):
466 self.assertTupleEqual(v[0], v[1])
467
468 # test the to_dataframe method with ignore_category_ids set to True
469 tdf = adt.to_dataframe(ignore_category_ids=True)
470 self.assertListEqual(tdf.index.tolist(), list(range(10)))
471 self.assertTupleEqual(tdf.index.name, ('test_aligned_table', 'id'))
472 expected_cols = [('test_aligned_table', 'main_c1'),
473 ('test_aligned_table', 'main_c2'),
474 ('test_aligned_table', 'main_c3'),
475 ('test1', 'c1'), ('test1', 'c2'), ('test1', 'c3'),
476 ('test2', 'c1'), ('test2', 'c2'), ('test2', 'c3'),
477 ('test3', 'c1'), ('test3', 'c2'), ('test3', 'c3')]
478 tdf_cols = tdf.columns.tolist()
479 for v in zip(expected_cols, tdf_cols):
480 self.assertTupleEqual(v[0], v[1])
481
482 def test_nested_aligned_dynamic_table_not_allowed(self):
483 """
484 Test that using and AlignedDynamicTable as category for an AlignedDynamicTable is not allowed
485 """
486 # create an AlignedDynamicTable as category
487 subsubcol1 = VectorData(name='sub_sub_column1', description='test sub sub column', data=['test11', 'test12'])
488 sub_category = DynamicTable(name='sub_category1', description='test subcategory table', columns=[subsubcol1, ])
489 subcol1 = VectorData(name='sub_column1', description='test-subcolumn', data=['test1', 'test2'])
490 adt_category = AlignedDynamicTable(
491 name='category1',
492 description='test using AlignedDynamicTable as a category',
493 columns=[subcol1, ],
494 category_tables=[sub_category, ])
495
496 # Create a regular column for our main AlignedDynamicTable
497 col1 = VectorData(name='column1', description='regular test column', data=['test1', 'test2'])
498
499 # test 1: Make sure we can't add the AlignedDynamicTable category on init
500 msg = ("Category table with index %i is an AlignedDynamicTable. "
501 "Nesting of AlignedDynamicTable is currently not supported." % 0)
502 with self.assertRaisesWith(ValueError, msg):
503 # create the nested AlignedDynamicTable with our adt_category as a sub-category
504 AlignedDynamicTable(
505 name='nested_adt',
506 description='test nesting AlignedDynamicTable',
507 columns=[col1, ],
508 category_tables=[adt_category, ])
509
510 # test 2: Make sure we can't add the AlignedDynamicTable category via add_category
511 adt = AlignedDynamicTable(
512 name='nested_adt',
513 description='test nesting AlignedDynamicTable',
514 columns=[col1, ])
515 msg = "Category is an AlignedDynamicTable. Nesting of AlignedDynamicTable is currently not supported."
516 with self.assertRaisesWith(ValueError, msg):
517 adt.add_category(adt_category)
518
519 def test_dynamictable_region_to_aligneddynamictable(self):
520 """
521 Test to ensure data is being retrieved correctly when pointing to an AlignedDynamicTable.
522 In particular, make sure that all columns are being used, including those of the
523 category tables, not just the ones from the main table.
524 """
525 temp_table = DynamicTable(name='t1', description='t1',
526 colnames=['c1', 'c2'],
527 columns=[VectorData(name='c1', description='c1', data=np.arange(4)),
528 VectorData(name='c2', description='c2', data=np.arange(4))])
529 temp_aligned_table = AlignedDynamicTable(name='my_aligned_table',
530 description='my test table',
531 category_tables=[temp_table],
532 colnames=['a1', 'a2'],
533 columns=[VectorData(name='a1', description='c1', data=np.arange(4)),
534 VectorData(name='a2', description='c1', data=np.arange(4))])
535 dtr = DynamicTableRegion(name='test', description='test', data=np.arange(4), table=temp_aligned_table)
536 dtr_df = dtr[:]
537 # Full number of rows
538 self.assertEqual(len(dtr_df), 4)
539 # Test num columns: 2 columns from the main table, 2 columns from the category, 1 id columns from the category
540 self.assertEqual(len(dtr_df.columns), 5)
541 # Test that the data is correct
542 for i, v in enumerate([('my_aligned_table', 'a1'), ('my_aligned_table', 'a2'),
543 ('t1', 'id'), ('t1', 'c1'), ('t1', 'c2')]):
544 self.assertTupleEqual(dtr_df.columns[i], v)
545 # Test the column data
546 for c in dtr_df.columns:
547 self.assertListEqual(dtr_df[c].to_list(), list(range(4)))
548
549 def test_get_colnames(self):
550 """
551 Test the AlignedDynamicTable.get_colnames function
552 """
553 category_names = ['test1', 'test2', 'test3']
554 num_rows = 10
555 categories = [DynamicTable(name=val,
556 description=val+" description",
557 columns=[VectorData(name=t,
558 description=val+t+' description',
559 data=np.arange(num_rows)) for t in ['c1', 'c2', 'c3']]
560 ) for val in category_names]
561 adt = AlignedDynamicTable(
562 name='test_aligned_table',
563 description='Test aligned container',
564 category_tables=categories,
565 columns=[VectorData(name='main_' + t,
566 description='main_'+t+'_description',
567 data=np.arange(num_rows)) for t in ['c1', 'c2', 'c3']])
568 # Default, only get the colnames of the main table. Same as adt.colnames property
569 expected_colnames = ('main_c1', 'main_c2', 'main_c3')
570 self.assertTupleEqual(adt.get_colnames(), expected_colnames)
571 # Same as default because if we don't include the categories than ignore_category_ids has no effect
572 self.assertTupleEqual(adt.get_colnames(include_category_tables=False, ignore_category_ids=True),
573 expected_colnames)
574 # Full set of columns
575 expected_colnames = [('test_aligned_table', 'main_c1'), ('test_aligned_table', 'main_c2'),
576 ('test_aligned_table', 'main_c3'), ('test1', 'id'), ('test1', 'c1'),
577 ('test1', 'c2'), ('test1', 'c3'), ('test2', 'id'), ('test2', 'c1'),
578 ('test2', 'c2'), ('test2', 'c3'), ('test3', 'id'), ('test3', 'c1'),
579 ('test3', 'c2'), ('test3', 'c3')]
580 self.assertListEqual(adt.get_colnames(include_category_tables=True, ignore_category_ids=False),
581 expected_colnames)
582 # All columns without the id columns of the category tables
583 expected_colnames = [('test_aligned_table', 'main_c1'), ('test_aligned_table', 'main_c2'),
584 ('test_aligned_table', 'main_c3'), ('test1', 'c1'), ('test1', 'c2'),
585 ('test1', 'c3'), ('test2', 'c1'), ('test2', 'c2'), ('test2', 'c3'),
586 ('test3', 'c1'), ('test3', 'c2'), ('test3', 'c3')]
587 self.assertListEqual(adt.get_colnames(include_category_tables=True, ignore_category_ids=True),
588 expected_colnames)