File: test_table.py

package info (click to toggle)
hdmf 3.14.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,372 kB
  • sloc: python: 34,738; makefile: 303; sh: 35
file content (2931 lines) | stat: -rw-r--r-- 132,660 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
from collections import OrderedDict
import h5py
import numpy as np
import os
import pandas as pd
import unittest

from hdmf import Container
from hdmf import TermSet, TermSetWrapper
from hdmf.backends.hdf5 import H5DataIO, HDF5IO
from hdmf.backends.hdf5.h5tools import H5_TEXT, H5PY_3
from hdmf.common import (
    DynamicTable,
    VectorData,
    VectorIndex,
    ElementIdentifiers,
    EnumData,
    DynamicTableRegion,
    get_manager,
    SimpleMultiContainer)
from hdmf.testing import TestCase, H5RoundTripMixin, remove_test_file
from hdmf.utils import StrDataset
from hdmf.data_utils import DataChunkIterator

from tests.unit.helpers.utils import (
    get_temp_filepath,
    FooExtendDynamicTable0,
    FooExtendDynamicTable1,
    FooExtendDynamicTable2,
)

try:
    import linkml_runtime  # noqa: F401
    REQUIREMENTS_INSTALLED = True
except ImportError:
    REQUIREMENTS_INSTALLED = False


class TestDynamicTable(TestCase):

    def setUp(self):
        self.spec = [
            {'name': 'foo', 'description': 'foo column'},
            {'name': 'bar', 'description': 'bar column'},
            {'name': 'baz', 'description': 'baz column'},
        ]
        self.data = [
            [1, 2, 3, 4, 5],
            [10.0, 20.0, 30.0, 40.0, 50.0],
            ['cat', 'dog', 'bird', 'fish', 'lizard']
        ]

    def with_table_columns(self):
        cols = [VectorData(**d) for d in self.spec]
        table = DynamicTable(name="with_table_columns", description='a test table', columns=cols)
        return table

    def with_columns_and_data(self):
        columns = [
            VectorData(name=s['name'], description=s['description'], data=d)
            for s, d in zip(self.spec, self.data)
        ]
        return DynamicTable(name="with_columns_and_data", description='a test table', columns=columns)

    def with_spec(self):
        table = DynamicTable(name="with_spec", description='a test table', columns=self.spec)
        return table

    def check_empty_table(self, table):
        self.assertIsInstance(table.columns, tuple)
        self.assertIsInstance(table.columns[0], VectorData)
        self.assertEqual(len(table.columns), 3)
        self.assertTupleEqual(table.colnames, ('foo', 'bar', 'baz'))

    def test_constructor_table_columns(self):
        table = self.with_table_columns()
        self.assertEqual(table.name, 'with_table_columns')
        self.check_empty_table(table)

    def test_constructor_spec(self):
        table = self.with_spec()
        self.assertEqual(table.name, 'with_spec')
        self.check_empty_table(table)

    def check_table(self, table):
        self.assertEqual(len(table), 5)
        self.assertEqual(table.columns[0].data, [1, 2, 3, 4, 5])
        self.assertEqual(table.columns[1].data, [10.0, 20.0, 30.0, 40.0, 50.0])
        self.assertEqual(table.columns[2].data, ['cat', 'dog', 'bird', 'fish', 'lizard'])
        self.assertEqual(table.id.data, [0, 1, 2, 3, 4])
        self.assertTrue(hasattr(table, 'baz'))

    def test_constructor_ids_default(self):
        columns = [VectorData(name=s['name'], description=s['description'], data=d)
                   for s, d in zip(self.spec, self.data)]
        table = DynamicTable(name="with_spec", description='a test table', columns=columns)
        self.check_table(table)

    def test_constructor_ids(self):
        columns = [VectorData(name=s['name'], description=s['description'], data=d)
                   for s, d in zip(self.spec, self.data)]
        table = DynamicTable(name="with_columns", description='a test table', id=[0, 1, 2, 3, 4], columns=columns)
        self.check_table(table)

    def test_constructor_ElementIdentifier_ids(self):
        columns = [VectorData(name=s['name'], description=s['description'], data=d)
                   for s, d in zip(self.spec, self.data)]
        ids = ElementIdentifiers(name='ids', data=[0, 1, 2, 3, 4])
        table = DynamicTable(name="with_columns", description='a test table', id=ids, columns=columns)
        self.check_table(table)

    def test_constructor_ids_bad_ids(self):
        columns = [VectorData(name=s['name'], description=s['description'], data=d)
                   for s, d in zip(self.spec, self.data)]
        msg = "Must provide same number of ids as length of columns"
        with self.assertRaisesWith(ValueError, msg):
            DynamicTable(name="with_columns", description='a test table', id=[0, 1], columns=columns)

    def test_constructor_all_columns_are_iterators(self):
        """
        All columns are specified via AbstractDataChunkIterator but no id's are given.
        Test that an error is being raised because we can't determine the id's.
        """
        data = np.array([1., 2., 3.])
        column = VectorData(name="TestColumn", description="", data=DataChunkIterator(data))
        msg = ("Cannot determine row id's for table. Must provide ids with same length "
               "as the columns when all columns are specified via DataChunkIterator objects.")
        with self.assertRaisesWith(ValueError, msg):
            _ = DynamicTable(name="TestTable", description="", columns=[column])
        # now test that when we supply id's that the error goes away
        _ = DynamicTable(name="TestTable", description="", columns=[column], id=list(range(3)))

    @unittest.skipIf(not REQUIREMENTS_INSTALLED, "optional LinkML module is not installed")
    def test_add_col_validate(self):
        terms = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml')
        col1 = VectorData(
            name='Species_1',
            description='...',
            data=TermSetWrapper(value=['Homo sapiens'], termset=terms)
        )
        species = DynamicTable(name='species', description='My species', columns=[col1])
        species.add_column(name='Species_2',
                           description='Species data',
                           data=TermSetWrapper(value=['Mus musculus'], termset=terms))
        expected_df_data = \
            {'Species_1': {0: 'Homo sapiens'},
             'Species_2': {0: 'Mus musculus'}}
        expected_df = pd.DataFrame.from_dict(expected_df_data)
        expected_df.index.name = 'id'
        pd.testing.assert_frame_equal(species.to_dataframe(), expected_df)

    @unittest.skipIf(not REQUIREMENTS_INSTALLED, "optional LinkML module is not installed")
    def test_add_col_validate_bad_data(self):
        terms = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml')
        col1 = VectorData(
            name='Species_1',
            description='...',
            data=TermSetWrapper(value=['Homo sapiens'], termset=terms)
        )
        species = DynamicTable(name='species', description='My species', columns=[col1])
        with self.assertRaises(ValueError):
            species.add_column(name='Species_2',
                               description='Species data',
                               data=TermSetWrapper(value=['bad data'],
                                                   termset=terms))

    @unittest.skipIf(not REQUIREMENTS_INSTALLED, "optional LinkML module is not installed")
    def test_add_row_validate(self):
        terms = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml')
        col1 = VectorData(
            name='Species_1',
            description='...',
            data=TermSetWrapper(value=['Homo sapiens'], termset=terms)
        )
        col2 = VectorData(
            name='Species_2',
            description='...',
            data=TermSetWrapper(value=['Mus musculus'], termset=terms)
        )
        species = DynamicTable(name='species', description='My species', columns=[col1,col2])
        species.add_row(Species_1='Myrmecophaga tridactyla', Species_2='Ursus arctos horribilis')
        expected_df_data = \
            {'Species_1': {0: 'Homo sapiens', 1: 'Myrmecophaga tridactyla'},
             'Species_2': {0: 'Mus musculus', 1: 'Ursus arctos horribilis'}}
        expected_df = pd.DataFrame.from_dict(expected_df_data)
        expected_df.index.name = 'id'
        pd.testing.assert_frame_equal(species.to_dataframe(), expected_df)

    @unittest.skipIf(not REQUIREMENTS_INSTALLED, "optional LinkML module is not installed")
    def test_add_row_validate_bad_data_one_col(self):
        terms = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml')
        col1 = VectorData(
            name='Species_1',
            description='...',
            data=TermSetWrapper(value=['Homo sapiens'], termset=terms)
        )
        col2 = VectorData(
            name='Species_2',
            description='...',
            data=TermSetWrapper(value=['Mus musculus'], termset=terms)
        )
        species = DynamicTable(name='species', description='My species', columns=[col1,col2])
        with self.assertRaises(ValueError):
            species.add_row(Species_1='bad', Species_2='Ursus arctos horribilis')

    @unittest.skipIf(not REQUIREMENTS_INSTALLED, "optional LinkML module is not installed")
    def test_add_row_validate_bad_data_all_col(self):
        terms = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml')
        col1 = VectorData(
            name='Species_1',
            description='...',
            data=TermSetWrapper(value=['Homo sapiens'], termset=terms)
        )
        col2 = VectorData(
            name='Species_2',
            description='...',
            data=TermSetWrapper(value=['Mus musculus'], termset=terms)
        )
        species = DynamicTable(name='species', description='My species', columns=[col1,col2])
        with self.assertRaises(ValueError):
            species.add_row(Species_1='bad data', Species_2='bad data')

    def test_compound_data_append(self):
        c_data = np.array([('Homo sapiens', 24)], dtype=[('species', 'U50'), ('age', 'i4')])
        c_data2 = np.array([('Mus musculus', 24)], dtype=[('species', 'U50'), ('age', 'i4')])
        compound_vector_data = VectorData(
            name='Species_1',
            description='...',
            data=c_data
        )
        compound_vector_data.append(c_data2)

        np.testing.assert_array_equal(compound_vector_data.data, np.append(c_data, c_data2))

    @unittest.skipIf(not REQUIREMENTS_INSTALLED, "optional LinkML module is not installed")
    def test_array_append_error(self):
        c_data = np.array(['Homo sapiens'])
        c_data2 = np.array(['Mus musculus'])

        terms = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml')
        vectordata_termset = VectorData(
            name='Species_1',
            description='...',
            data=TermSetWrapper(value=c_data, termset=terms)
        )

        with self.assertRaises(ValueError):
            vectordata_termset.append(c_data2)

    def test_compound_data_extend(self):
        c_data = np.array([('Homo sapiens', 24)], dtype=[('species', 'U50'), ('age', 'i4')])
        c_data2 = np.array([('Mus musculus', 24)], dtype=[('species', 'U50'), ('age', 'i4')])
        compound_vector_data = VectorData(
            name='Species_1',
            description='...',
            data=c_data
        )
        compound_vector_data.extend(c_data2)

        np.testing.assert_array_equal(compound_vector_data.data, np.vstack((c_data, c_data2)))

    @unittest.skipIf(not REQUIREMENTS_INSTALLED, "optional LinkML module is not installed")
    def test_add_ref_wrapped_array_append(self):
        data = np.array(['Homo sapiens'])
        data2 = 'Mus musculus'
        terms = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml')
        vector_data = VectorData(
            name='Species_1',
            description='...',
            data=TermSetWrapper(value=data, termset=terms)
        )
        vector_data.append(data2)

        np.testing.assert_array_equal(vector_data.data.data, np.append(data, data2))

    @unittest.skipIf(not REQUIREMENTS_INSTALLED, "optional LinkML module is not installed")
    def test_add_ref_wrapped_array_extend(self):
        data = np.array(['Homo sapiens'])
        data2 = np.array(['Mus musculus'])
        terms = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml')
        vector_data = VectorData(
            name='Species_1',
            description='...',
            data=TermSetWrapper(value=data, termset=terms)
        )
        vector_data.extend(data2)

        np.testing.assert_array_equal(vector_data.data.data, np.vstack((data, data2)))

    @unittest.skipIf(not REQUIREMENTS_INSTALLED, "optional LinkML module is not installed")
    def test_add_ref_wrapped_compound_data_append(self):
        c_data = np.array([('Homo sapiens', 24)], dtype=[('species', 'U50'), ('age', 'i4')])
        c_data2 = np.array([('Mus musculus', 24)], dtype=[('species', 'U50'), ('age', 'i4')])
        terms = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml')
        compound_vector_data = VectorData(
            name='Species_1',
            description='...',
            data=TermSetWrapper(value=c_data, field='species', termset=terms)
        )
        compound_vector_data.append(c_data2)

        np.testing.assert_array_equal(compound_vector_data.data.data, np.append(c_data, c_data2))

    @unittest.skipIf(not REQUIREMENTS_INSTALLED, "optional LinkML module is not installed")
    def test_add_ref_wrapped_compound_data_extend(self):
        c_data = np.array([('Homo sapiens', 24)], dtype=[('species', 'U50'), ('age', 'i4')])
        c_data2 = np.array([('Mus musculus', 24)], dtype=[('species', 'U50'), ('age', 'i4')])
        terms = TermSet(term_schema_path='tests/unit/example_test_term_set.yaml')
        compound_vector_data = VectorData(
            name='Species_1',
            description='...',
            data=TermSetWrapper(value=c_data, field='species', termset=terms)
        )
        compound_vector_data.extend(c_data2)

        np.testing.assert_array_equal(compound_vector_data.data.data, np.vstack((c_data, c_data2)))

    def test_constructor_bad_columns(self):
        columns = ['bad_column']
        msg = "'columns' must be a list of dict, VectorData, DynamicTableRegion, or VectorIndex"
        with self.assertRaisesWith(ValueError, msg):
            DynamicTable(name="with_columns", description='a test table', columns=columns)

    def test_constructor_unequal_length_columns(self):
        columns = [VectorData(name='col1', description='desc', data=[1, 2, 3]),
                   VectorData(name='col2', description='desc', data=[1, 2])]
        msg = "Columns must be the same length"
        with self.assertRaisesWith(ValueError, msg):
            DynamicTable(name="with_columns", description='a test table', columns=columns)

    def test_constructor_colnames(self):
        """Test that passing colnames correctly sets the order of the columns."""
        cols = [VectorData(**d) for d in self.spec]
        table = DynamicTable(name="with_columns", description='a test table',
                             columns=cols, colnames=['baz', 'bar', 'foo'])
        self.assertTupleEqual(table.columns, tuple(cols[::-1]))

    def test_constructor_colnames_no_columns(self):
        """Test that passing colnames without columns raises an error."""
        msg = "Must supply 'columns' if specifying 'colnames'"
        with self.assertRaisesWith(ValueError, msg):
            DynamicTable(name="with_columns", description='a test table',  colnames=['baz', 'bar', 'foo'])

    def test_constructor_colnames_vectorindex(self):
        """Test that passing colnames with a VectorIndex column puts the index in the right location in columns."""
        cols = [VectorData(**d) for d in self.spec]
        ind = VectorIndex(name='foo_index', data=list(), target=cols[0])
        cols.append(ind)
        table = DynamicTable(name="with_columns", description='a test table', columns=cols,
                             colnames=['baz', 'bar', 'foo'])
        self.assertTupleEqual(table.columns, (cols[2], cols[1], ind, cols[0]))

    def test_constructor_colnames_vectorindex_rev(self):
        """Test that passing colnames with a VectorIndex column puts the index in the right location in columns."""
        cols = [VectorData(**d) for d in self.spec]
        ind = VectorIndex(name='foo_index', data=list(), target=cols[0])
        cols.insert(0, ind)  # put index before its target
        table = DynamicTable(name="with_columns", description='a test table', columns=cols,
                             colnames=['baz', 'bar', 'foo'])
        self.assertTupleEqual(table.columns, (cols[3], cols[2], ind, cols[1]))

    def test_constructor_dup_index(self):
        """Test that passing two indices for the same column raises an error."""
        cols = [VectorData(**d) for d in self.spec]
        cols.append(VectorIndex(name='foo_index', data=list(), target=cols[0]))
        cols.append(VectorIndex(name='foo_index2', data=list(), target=cols[0]))
        msg = "'columns' contains index columns with the same target: ['foo', 'foo']"
        with self.assertRaisesWith(ValueError, msg):
            DynamicTable(name="with_columns", description='a test table', columns=cols)

    def test_constructor_index_missing_target(self):
        """Test that passing an index without its target raises an error."""
        cols = [VectorData(**d) for d in self.spec]
        missing_col = cols.pop(2)
        cols.append(VectorIndex(name='foo_index', data=list(), target=missing_col))
        msg = "Found VectorIndex 'foo_index' but not its target 'baz'"
        with self.assertRaisesWith(ValueError, msg):
            DynamicTable(name="with_columns", description='a test table', columns=cols)

    def add_rows(self, table):
        table.add_row({'foo': 1, 'bar': 10.0, 'baz': 'cat'})
        table.add_row({'foo': 2, 'bar': 20.0, 'baz': 'dog'})
        table.add_row({'foo': 3, 'bar': 30.0, 'baz': 'bird'})
        table.add_row({'foo': 4, 'bar': 40.0, 'baz': 'fish'})
        table.add_row({'foo': 5, 'bar': 50.0, 'baz': 'lizard'})

    def test_add_row(self):
        table = self.with_spec()
        self.add_rows(table)
        self.check_table(table)

    def test_get(self):
        table = self.with_spec()
        self.add_rows(table)
        self.assertIsInstance(table.get('foo'), VectorData)
        self.assertEqual(table.get('foo'), table['foo'])

    def test_get_not_found(self):
        table = self.with_spec()
        self.add_rows(table)
        self.assertIsNone(table.get('qux'))

    def test_get_not_found_default(self):
        table = self.with_spec()
        self.add_rows(table)
        self.assertEqual(table.get('qux', 1), 1)

    def test_get_item(self):
        table = self.with_spec()
        self.add_rows(table)
        self.check_table(table)

    def test_add_column(self):
        table = self.with_spec()
        table.add_column(name='qux', description='qux column')
        self.assertTupleEqual(table.colnames, ('foo', 'bar', 'baz', 'qux'))
        self.assertTrue(hasattr(table, 'qux'))

    def test_add_column_twice(self):
        table = self.with_spec()
        table.add_column(name='qux', description='qux column')

        msg = "column 'qux' already exists in DynamicTable 'with_spec'"
        with self.assertRaisesWith(ValueError, msg):
            table.add_column(name='qux', description='qux column')

    def test_add_column_vectorindex(self):
        table = self.with_spec()
        table.add_column(name='qux', description='qux column')
        ind = VectorIndex(name='quux', data=list(), target=table['qux'])

        msg = ("Passing a VectorIndex in for index may lead to unexpected behavior. This functionality will be "
               "deprecated in a future version of HDMF.")
        with self.assertWarnsWith(FutureWarning, msg):
            table.add_column(name='bad', description='bad column', index=ind)

    def test_add_column_multi_index(self):
        table = self.with_spec()
        table.add_column(name='qux', description='qux column', index=2)
        table.add_row(foo=5, bar=50.0, baz='lizard',
                      qux=[
                            [1, 2, 3],
                            [1, 2, 3, 4]
                      ])
        table.add_row(foo=5, bar=50.0, baz='lizard',
                      qux=[
                            [1, 2]
                      ]
                      )

    def test_add_column_without_required_index(self):
        """
        Add a column with different element lengths without specifying an index parameter
        """
        table = self.with_spec()
        table.add_row(foo=5, bar=50.0, baz='lizard')
        table.add_row(foo=5, bar=50.0, baz='lizard')

        # testing adding column without a necessary index parameter
        lol_data = [[1, 2, 3], [1, 2, 3, 4]]
        str_data = [['a', 'b'], ['a', 'b', 'c']]
        empty_data = [[1, 2], []]
        multi_nested_data = [[[1, 2, 3], [1, 2, 3, 4]], [1, 2]]
        tuple_data = ((1, 2, 3), (1, 2, 3, 4))

        msg = ("Data has elements with different lengths and therefore cannot be coerced into an N-dimensional "
               "array. Use the 'index' argument when adding a column of data with different lengths.")
        with self.assertWarnsWith(UserWarning, msg):
            table.add_column(name='col1', description='', data=lol_data,)
        with self.assertWarnsWith(UserWarning, msg):
            table.add_column(name='col2', description='', data=str_data,)
        with self.assertWarnsWith(UserWarning, msg):
            table.add_column(name='col3', description='', data=empty_data,)
        with self.assertWarnsWith(UserWarning, msg):
            table.add_column(name='col4', description='', data=multi_nested_data,)
        with self.assertWarnsWith(UserWarning, msg):
            table.add_column(name='col5', description='', data=tuple_data,)

    def test_add_column_without_required_index_and_no_ragged_check(self):
        """
        Add a column with different element lengths without checking for raggedness
        """
        lol_data = [[1, 2, 3], [1, 2, 3, 4]]
        table = self.with_spec()
        table.add_row(foo=5, bar=50.0, baz='lizard')
        table.add_row(foo=5, bar=50.0, baz='lizard')
        table.add_column(name='col1', description='', data=lol_data, check_ragged=False)

    def test_add_row_without_required_index(self):
        """
        Add rows with different element lengths without specifying an index parameter
        """

        # test adding row of list data with different lengths without index parameter
        msg = ("Data has elements with different lengths and therefore cannot be coerced into an N-dimensional "
               "array. Use the 'index' argument when creating a column to add rows with different lengths.")
        table = self.with_spec()
        table.add_column(name='qux', description='qux column')
        table.add_row(foo=5, bar=50.0, baz='lizard', qux=[1, 2, 3])
        with self.assertWarnsWith(UserWarning, msg):
            table.add_row(foo=5, bar=50.0, baz='lizard', qux=[1, 2, 3 ,4])

        # test adding row of tuple/str data with different lengths without index parameter
        table = self.with_spec()
        table.add_column(name='qux', description='qux column')
        table.add_row(foo=5, bar=50.0, baz='lizard', qux=('a', 'b'))
        with self.assertWarnsWith(UserWarning, msg):
            table.add_row(foo=5, bar=50.0, baz='lizard', qux=('a', 'b', 'c'))

    def test_add_row_without_required_index_and_no_ragged_check(self):
        """
        Add rows with different element lengths without checking for raggedness
        """
        table = self.with_spec()
        table.add_column(name='qux', description='qux column')
        table.add_row(foo=5, bar=50.0, baz='lizard', qux=[1, 2, 3])
        table.add_row(foo=5, bar=50.0, baz='lizard', qux=[1, 2, 3 ,4], check_ragged=False)

    def test_add_column_auto_index_int(self):
        """
        Add a column as a list of lists after we have already added data so that we need to create a single VectorIndex
        with index=1 as parameter
        """
        table = self.with_spec()
        table.add_row(foo=5, bar=50.0, baz='lizard')
        table.add_row(foo=5, bar=50.0, baz='lizard')
        expected = [[1, 2, 3],
                    [1, 2, 3, 4]]
        table.add_column(name='qux',
                         description='qux column',
                         data=expected,
                         index=1)
        self.assertListEqual(table['qux'][:], expected)
        self.assertListEqual(table.qux_index.data, [3, 7])
        # Add more rows after we created the column
        table.add_row(foo=5, bar=50.0, baz='lizard', qux=[10, 11, 12])
        self.assertListEqual(table['qux'][:], expected + [[10, 11, 12], ])
        self.assertListEqual(table.qux_index.data, [3, 7, 10])

    def test_add_column_auto_index_bool(self):
        """
        Add a column as a list of lists after we have already added data so that we need to create a single VectorIndex
        with index=True as parameter
        """
        table = self.with_spec()
        table.add_row(foo=5, bar=50.0, baz='lizard')
        table.add_row(foo=5, bar=50.0, baz='lizard')
        expected = [[1, 2, 3],
                    [1, 2, 3, 4]]
        table.add_column(name='qux',
                         description='qux column',
                         data=expected,
                         index=True)
        self.assertListEqual(table['qux'][:], expected)
        self.assertListEqual(table.qux_index.data, [3, 7])
        # Add more rows after we created the column
        table.add_row(foo=5, bar=50.0, baz='lizard', qux=[10, 11, 12])
        self.assertListEqual(table['qux'][:], expected + [[10, 11, 12], ])
        self.assertListEqual(table.qux_index.data, [3, 7, 10])

    def test_add_column_auto_multi_index_int(self):
        """
        Add a column as a list of lists of lists after we have already added data so that we need to create a
        two VectorIndex for the column so we set index=2 as parameter
        """
        table = self.with_spec()
        table.add_row(foo=5, bar=50.0, baz='lizard')
        table.add_row(foo=5, bar=50.0, baz='lizard')
        expected = [[[1, 2, 3], [1]],
                    [[1, 2, 3, 4], [1, 2]]]
        table.add_column(name='qux',
                         description='qux column',
                         data=expected,
                         index=2)
        self.assertListEqual(table['qux'][:], expected)
        self.assertListEqual(table.qux_index_index.data, [2, 4])
        self.assertListEqual(table.qux_index.data, [3, 4, 8, 10])
        # Add more rows after we created the column
        table.add_row(foo=5, bar=50.0, baz='lizard', qux=[[10, 11, 12], ])
        self.assertListEqual(table['qux'][:], expected + [[[10, 11, 12], ]])
        self.assertListEqual(table.qux_index_index.data, [2, 4, 5])
        self.assertListEqual(table.qux_index.data, [3, 4, 8, 10, 13])

    def test_add_column_auto_multi_index_int_bad_index_levels(self):
        """
        Add a column as a list of lists if lists after we have already added data so that we need to create a
        a two-level index, but we ask for either too many or too few index levels.
        """
        table = self.with_spec()
        table.add_row(foo=5, bar=50.0, baz='lizard')
        table.add_row(foo=5, bar=50.0, baz='lizard')
        expected = [[[1, 2, 3], [1]],
                    [[1, 2, 3, 4], [1, 2]]]
        msg = "Cannot automatically construct VectorIndex for nested array. Invalid data array element found."
        with self.assertRaisesWith(ValueError, msg):
            table.add_column(name='qux',
                             description='qux column',
                             data=expected,
                             index=3)  # Too many index levels given
        # Asking for too few indexes will work here but should then later fail on write
        msg = ("Cannot automatically construct VectorIndex for nested array. "
               "Column data contains arrays as cell values. Please check the 'data' and 'index' parameters.")
        with self.assertRaisesWith(ValueError, msg + " 'index=1' may be too small for the given data."):
            table.add_column(name='qux',
                             description='qux column',
                             data=expected,
                             index=1)
        with self.assertRaisesWith(ValueError, msg + " 'index=True' may be too small for the given data."):
            table.add_column(name='qux',
                             description='qux column',
                             data=expected,
                             index=True)

    def test_add_column_auto_multi_index_int_with_empty_slots(self):
        """
        Add a column as a list of lists of lists after we have already added data so that we need to create 2
        VectorIndex levels so we set index=2 as parameter. For the data the first two rows have no entries in the
        multi-indexed column.
        """
        table = self.with_spec()
        table.add_row(foo=5, bar=50.0, baz='lizard')
        table.add_row(foo=5, bar=50.0, baz='lizard')
        expected = [[[], []],
                    [[], []]]
        table.add_column(name='qux',
                         description='qux column',
                         data=expected,
                         index=2)
        self.assertListEqual(table['qux'][:], expected)
        self.assertListEqual(table.qux_index_index.data, [2, 4])
        self.assertListEqual(table.qux_index.data, [0, 0, 0, 0])
        # Add more rows after we created the column
        table.add_row(foo=5, bar=50.0, baz='lizard', qux=[[10, 11, 12], ])
        self.assertListEqual(table['qux'][:], expected + [[[10, 11, 12], ]])
        self.assertListEqual(table.qux_index_index.data, [2, 4, 5])
        self.assertListEqual(table.qux_index.data, [0, 0, 0, 0, 3])

    def test_auto_multi_index_required(self):

        class TestTable(DynamicTable):
            __columns__ = (dict(name='qux', description='qux column', index=3, required=True),)

        table = TestTable(name='table_name', description='table_description')
        self.assertIsInstance(table.qux, VectorData)  # check that the attribute is set
        self.assertIsInstance(table.qux_index, VectorIndex)  # check that the attribute is set
        self.assertIsInstance(table.qux_index_index, VectorIndex)  # check that the attribute is set
        self.assertIsInstance(table.qux_index_index_index, VectorIndex)  # check that the attribute is set
        table.add_row(
            qux=[
                    [
                        [1, 2, 3],
                        [1, 2, 3, 4]
                    ]
                ]
        )
        table.add_row(
            qux=[
                    [
                        [1, 2]
                    ]
                ]
        )

        expected = [
            [
                [
                    [1, 2, 3],
                    [1, 2, 3, 4]
                ]
            ],
            [
                [
                    [1, 2]
                ]
            ]
        ]
        self.assertListEqual(table['qux'][:], expected)
        self.assertEqual(table.qux_index_index_index.data, [1, 2])

    def test_auto_multi_index(self):

        class TestTable(DynamicTable):
            __columns__ = (dict(name='qux', description='qux column', index=3),)  # this is optional

        table = TestTable(name='table_name', description='table_description')
        self.assertIsNone(table.qux)  # these are reserved as attributes but not yet initialized
        self.assertIsNone(table.qux_index)
        self.assertIsNone(table.qux_index_index)
        self.assertIsNone(table.qux_index_index_index)
        table.add_row(
            qux=[
                    [
                        [1, 2, 3],
                        [1, 2, 3, 4]
                    ]
                ]
        )
        table.add_row(
            qux=[
                    [
                        [1, 2]
                    ]
                ]
        )

        expected = [
            [
                [
                    [1, 2, 3],
                    [1, 2, 3, 4]
                ]
            ],
            [
                [
                    [1, 2]
                ]
            ]
        ]
        self.assertListEqual(table['qux'][:], expected)
        self.assertEqual(table.qux_index_index_index.data, [1, 2])

    def test_getitem_row_num(self):
        table = self.with_spec()
        self.add_rows(table)
        row = table[2]
        self.assertTupleEqual(row.shape, (1, 3))
        self.assertTupleEqual(tuple(row.iloc[0]), (3, 30.0, 'bird'))

    def test_getitem_row_slice(self):
        table = self.with_spec()
        self.add_rows(table)
        rows = table[1:3]
        self.assertIsInstance(rows, pd.DataFrame)
        self.assertTupleEqual(rows.shape, (2, 3))
        self.assertTupleEqual(tuple(rows.iloc[1]), (3, 30.0, 'bird'))

    def test_getitem_row_slice_with_step(self):
        table = self.with_spec()
        self.add_rows(table)
        rows = table[0:5:2]
        self.assertIsInstance(rows, pd.DataFrame)
        self.assertTupleEqual(rows.shape, (3, 3))
        self.assertEqual(rows.iloc[2].iloc[0], 5)
        self.assertEqual(rows.iloc[2].iloc[1], 50.0)
        self.assertEqual(rows.iloc[2].iloc[2], 'lizard')

    def test_getitem_invalid_keytype(self):
        table = self.with_spec()
        self.add_rows(table)
        with self.assertRaises(KeyError):
            _ = table[0.1]

    def test_getitem_col_select_and_row_slice(self):
        table = self.with_spec()
        self.add_rows(table)
        col = table[1:3, 'bar']
        self.assertEqual(len(col), 2)
        self.assertEqual(col[0], 20.0)
        self.assertEqual(col[1], 30.0)

    def test_getitem_column(self):
        table = self.with_spec()
        self.add_rows(table)
        col = table['bar']
        self.assertEqual(col[0], 10.0)
        self.assertEqual(col[1], 20.0)
        self.assertEqual(col[2], 30.0)
        self.assertEqual(col[3], 40.0)
        self.assertEqual(col[4], 50.0)

    def test_getitem_list_idx(self):
        table = self.with_spec()
        self.add_rows(table)
        row = table[[0, 2, 4]]
        self.assertEqual(len(row), 3)
        self.assertTupleEqual(tuple(row.iloc[0]), (1, 10.0, 'cat'))
        self.assertTupleEqual(tuple(row.iloc[1]), (3, 30.0, 'bird'))
        self.assertTupleEqual(tuple(row.iloc[2]), (5, 50.0, 'lizard'))

    def test_getitem_point_idx_colname(self):
        table = self.with_spec()
        self.add_rows(table)
        val = table[2, 'bar']
        self.assertEqual(val, 30.0)

    def test_getitem_point_idx(self):
        table = self.with_spec()
        self.add_rows(table)
        row = table[2]
        self.assertTupleEqual(tuple(row.iloc[0]), (3, 30.0, 'bird'))

    def test_getitem_point_idx_colidx(self):
        table = self.with_spec()
        self.add_rows(table)
        val = table[2, 2]
        self.assertEqual(val, 30.0)

    def test_pandas_roundtrip(self):
        df = pd.DataFrame({
            'a': [1, 2, 3, 4],
            'b': ['a', 'b', 'c', '4']
        }, index=pd.Index(name='an_index', data=[2, 4, 6, 8]))

        table = DynamicTable.from_dataframe(df, 'foo')
        obtained = table.to_dataframe()
        self.assertTrue(df.equals(obtained))

    def test_to_dataframe(self):
        table = self.with_columns_and_data()
        data = OrderedDict()
        for name in table.colnames:
            if name == 'foo':
                data[name] = [1, 2, 3, 4, 5]
            elif name == 'bar':
                data[name] = [10.0, 20.0, 30.0, 40.0, 50.0]
            elif name == 'baz':
                data[name] = ['cat', 'dog', 'bird', 'fish', 'lizard']
        expected_df = pd.DataFrame(data)
        obtained_df = table.to_dataframe()
        self.assertTrue(expected_df.equals(obtained_df))

    def test_from_dataframe(self):
        df = pd.DataFrame({
            'foo': [1, 2, 3, 4, 5],
            'bar': [10.0, 20.0, 30.0, 40.0, 50.0],
            'baz': ['cat', 'dog', 'bird', 'fish', 'lizard']
        }).loc[:, ('foo', 'bar', 'baz')]

        obtained_table = DynamicTable.from_dataframe(df, 'test')
        self.check_table(obtained_table)

    def test_from_dataframe_eq(self):
        expected = DynamicTable(name='test_table', description='the expected table')
        expected.add_column('a', '2d column')
        expected.add_column('b', '1d column')
        expected.add_row(a=[1, 2, 3], b='4')
        expected.add_row(a=[1, 2, 3], b='5')
        expected.add_row(a=[1, 2, 3], b='6')

        df = pd.DataFrame({
            'a': [[1, 2, 3],
                  [1, 2, 3],
                  [1, 2, 3]],
            'b': ['4', '5', '6']
        })
        coldesc = {'a': '2d column', 'b': '1d column'}
        received = DynamicTable.from_dataframe(df,
                                               'test_table',
                                               table_description='the expected table',
                                               column_descriptions=coldesc)
        self.assertContainerEqual(expected, received, ignore_hdmf_attrs=True)

    def test_from_dataframe_dup_attr(self):
        """
        Test that when a DynamicTable is generated from a dataframe where one of the column names is an existing
        DynamicTable attribute (e.g., description), that the table can be created, the existing attribute is not
        altered, a warning is raised, and the column can still be accessed using the table[col_name] syntax.
        """
        df = pd.DataFrame({
            'parent': [1, 2, 3, 4, 5],
            'name': [10.0, 20.0, 30.0, 40.0, 50.0],
            'description': ['cat', 'dog', 'bird', 'fish', 'lizard']
        })

        # technically there are three separate warnings but just catch one here
        msg1 = ("An attribute 'parent' already exists on DynamicTable 'test' so this column cannot be accessed "
                "as an attribute, e.g., table.parent; it can only be accessed using other methods, e.g., "
                "table['parent'].")
        with self.assertWarnsWith(UserWarning, msg1):
            table = DynamicTable.from_dataframe(df, 'test')
        self.assertEqual(table.name, 'test')
        self.assertEqual(table.description, '')
        self.assertIsNone(table.parent)
        self.assertEqual(table['name'].name, 'name')
        self.assertEqual(table['description'].name, 'description')
        self.assertEqual(table['parent'].name, 'parent')

    def test_missing_columns(self):
        table = self.with_spec()
        with self.assertRaises(ValueError):
            table.add_row({'bar': 60.0, 'foo': [6]}, None)

    def test_enforce_unique_id_error(self):
        table = self.with_spec()
        table.add_row(id=10, data={'foo': 1, 'bar': 10.0, 'baz': 'cat'}, enforce_unique_id=True)
        with self.assertRaises(ValueError):
            table.add_row(id=10, data={'foo': 1, 'bar': 10.0, 'baz': 'cat'}, enforce_unique_id=True)

    def test_not_enforce_unique_id_error(self):
        table = self.with_spec()
        table.add_row(id=10, data={'foo': 1, 'bar': 10.0, 'baz': 'cat'}, enforce_unique_id=False)
        try:
            table.add_row(id=10, data={'foo': 1, 'bar': 10.0, 'baz': 'cat'}, enforce_unique_id=False)
        except ValueError as e:
            self.fail("add row with non unique id raised error %s" % str(e))

    def test_bad_id_type_error(self):
        table = self.with_spec()
        with self.assertRaises(TypeError):
            table.add_row(id=10.1, data={'foo': 1, 'bar': 10.0, 'baz': 'cat'}, enforce_unique_id=True)
        with self.assertRaises(TypeError):
            table.add_row(id='str', data={'foo': 1, 'bar': 10.0, 'baz': 'cat'}, enforce_unique_id=True)

    def test_extra_columns(self):
        table = self.with_spec()

        with self.assertRaises(ValueError):
            table.add_row({'bar': 60.0, 'foo': 6, 'baz': 'oryx', 'qax': -1}, None)

    def test_nd_array_to_df(self):
        data = np.array([[1, 1, 1], [2, 2, 2], [3, 3, 3]])
        col = VectorData(name='data', description='desc', data=data)
        df = DynamicTable(name='test', description='desc', id=np.arange(3, dtype='int'),
                          columns=(col, )).to_dataframe()
        df2 = pd.DataFrame({'data': [x for x in data]},
                           index=pd.Index(name='id', data=[0, 1, 2]))
        pd.testing.assert_frame_equal(df, df2)

    def test_id_search(self):
        table = self.with_spec()
        data = [{'foo': 1, 'bar': 10.0, 'baz': 'cat'},
                {'foo': 2, 'bar': 20.0, 'baz': 'dog'},
                {'foo': 3, 'bar': 30.0, 'baz': 'bird'},    # id=2
                {'foo': 4, 'bar': 40.0, 'baz': 'fish'},
                {'foo': 5, 'bar': 50.0, 'baz': 'lizard'}   # id=4
                ]
        for i in data:
            table.add_row(i)
        res = table[table.id == [2, 4]]
        self.assertEqual(len(res), 2)
        self.assertTupleEqual(tuple(res.iloc[0]), (3, 30.0, 'bird'))
        self.assertTupleEqual(tuple(res.iloc[1]), (5, 50.0, 'lizard'))

    def test_repr(self):
        table = self.with_spec()
        expected = """with_spec hdmf.common.table.DynamicTable at 0x%d
Fields:
  colnames: ['foo' 'bar' 'baz']
  columns: (
    foo <class 'hdmf.common.table.VectorData'>,
    bar <class 'hdmf.common.table.VectorData'>,
    baz <class 'hdmf.common.table.VectorData'>
  )
  description: a test table
"""
        expected = expected % id(table)
        self.assertEqual(str(table), expected)

    def test_repr_html(self):
        table = self.with_spec()
        html = table._repr_html_()

        assert html == (
            '\n        <style>\n            .container-fields {\n                font-family: "Open Sans", Arial, '
            'sans-serif;\n            }\n            .container-fields .field-value {\n                color: '
            '#00788E;\n            }\n            .container-fields details > summary {\n                cursor: '
            'pointer;\n                display: list-item;\n            }\n            .container-fields details > '
            'summary:hover {\n                color: #0A6EAA;\n            }\n        </style>\n        \n        '
            '<script>\n            function copyToClipboard(text) {\n                navigator.clipboard.writeText('
            'text).then(function() {\n                    console.log(\'Copied to clipboard: \' + text);\n            '
            '    }, function(err) {\n                    console.error(\'Could not copy text: \', err);\n             '
            '   });\n            }\n\n            document.addEventListener(\'DOMContentLoaded\', function() {\n      '
            '          let fieldKeys = document.querySelectorAll(\'.container-fields .field-key\');\n                '
            'fieldKeys.forEach(function(fieldKey) {\n                    fieldKey.addEventListener(\'click\', '
            'function() {\n                        let accessCode = fieldKey.getAttribute(\'title\').replace(\'Access '
            'code: \', \'\');\n                        copyToClipboard(accessCode);\n                    });\n        '
            '        });\n            });\n        </script>\n        <div class=\'container-wrap\'><div '
            'class=\'container-header\'><div class=\'xr-obj-type\'><h3>with_spec (DynamicTable)</h3></div></div><div '
            'style="margin-left: 0px;" class="container-fields"><span class="field-key" title="">description: '
            '</span><span class="field-value">a test table</span></div><details><summary style="display: list-item; '
            'margin-left: 0px;" class="container-fields field-key" title=""><b>table</b></summary><table border="1" '
            'class="dataframe">\n  <thead>\n    <tr style="text-align: right;">\n      <th></th>\n      '
            '<th>foo</th>\n      <th>bar</th>\n      <th>baz</th>\n    </tr>\n    <tr>\n      <th>id</th>\n      '
            '<th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n  '
            '</tbody>\n</table></details></div>'
        )


    def test_add_column_existing_attr(self):
        table = self.with_table_columns()
        attrs = ['name', 'description', 'parent', 'id', 'fields']  # just a few
        for attr in attrs:
            with self.subTest(attr=attr):
                msg = ("An attribute '%s' already exists on DynamicTable 'with_table_columns' so this column cannot be "
                       "accessed as an attribute, e.g., table.%s; it can only be accessed using other methods, "
                       "e.g., table['%s']." % (attr, attr, attr))
                with self.assertWarnsWith(UserWarning, msg):
                    table.add_column(name=attr, description='')

    def test_init_columns_existing_attr(self):
        attrs = ['name', 'description', 'parent', 'id', 'fields']  # just a few
        for attr in attrs:
            with self.subTest(attr=attr):
                cols = [VectorData(name=attr, description='')]
                msg = ("An attribute '%s' already exists on DynamicTable 'test_table' so this column cannot be "
                       "accessed as an attribute, e.g., table.%s; it can only be accessed using other methods, "
                       "e.g., table['%s']." % (attr, attr, attr))
                with self.assertWarnsWith(UserWarning, msg):
                    DynamicTable(name="test_table", description='a test table', columns=cols)

    def test_colnames_none(self):
        table = DynamicTable(name='table0', description='an example table')
        self.assertTupleEqual(table.colnames, tuple())
        self.assertTupleEqual(table.columns, tuple())

    def test_index_out_of_bounds(self):
        table = self.with_columns_and_data()
        msg = "Row index out of range for DynamicTable 'with_columns_and_data' (length 5)."
        with self.assertRaisesWith(IndexError, msg):
            table[5]

    def test_no_df_nested(self):
        table = self.with_columns_and_data()
        msg = 'DynamicTable.get() with df=False and index=False is not yet supported.'
        with self.assertRaisesWith(ValueError, msg):
            table.get(0, df=False, index=False)

    def test_multidim_col(self):
        multidim_data = [
            [[1, 2], [3, 4], [5, 6]],
            ((1, 2), (3, 4), (5, 6)),
            [(1, 'a', True), (2, 'b', False), (3, 'c', True)],
        ]
        columns = [
            VectorData(name=s['name'], description=s['description'], data=d)
            for s, d in zip(self.spec, multidim_data)
        ]
        table = DynamicTable(name="with_columns_and_data", description='a test table', columns=columns)
        df = table.to_dataframe()
        df2 = pd.DataFrame({'foo': multidim_data[0],
                            'bar': multidim_data[1],
                            'baz': multidim_data[2]},
                           index=pd.Index(name='id', data=[0, 1, 2]))
        pd.testing.assert_frame_equal(df, df2)

        df3 = pd.DataFrame({'foo': [multidim_data[0][0]],
                            'bar': [multidim_data[1][0]],
                            'baz': [multidim_data[2][0]]},
                           index=pd.Index(name='id', data=[0]))
        pd.testing.assert_frame_equal(table.get(0), df3)

    def test_multidim_col_one_elt_list(self):
        data = [[1, 2]]
        col = VectorData(name='data', description='desc', data=data)
        table = DynamicTable(name='test', description='desc', columns=(col, ))
        df = table.to_dataframe()
        df2 = pd.DataFrame({'data': [x for x in data]},
                           index=pd.Index(name='id', data=[0]))
        pd.testing.assert_frame_equal(df, df2)
        pd.testing.assert_frame_equal(table.get(0), df2)

    def test_multidim_col_one_elt_tuple(self):
        data = [(1, 2)]
        col = VectorData(name='data', description='desc', data=data)
        table = DynamicTable(name='test', description='desc', columns=(col, ))
        df = table.to_dataframe()
        df2 = pd.DataFrame({'data': [x for x in data]},
                           index=pd.Index(name='id', data=[0]))
        pd.testing.assert_frame_equal(df, df2)
        pd.testing.assert_frame_equal(table.get(0), df2)

    def test_eq(self):
        columns = [
            VectorData(name=s['name'], description=s['description'], data=d)
            for s, d in zip(self.spec, self.data)
        ]
        test_table = DynamicTable(name="with_columns_and_data", description='a test table', columns=columns)

        table = self.with_columns_and_data()
        self.assertTrue(table == test_table)

    def test_eq_from_df(self):
        df = pd.DataFrame({
            'foo': [1, 2, 3, 4, 5],
            'bar': [10.0, 20.0, 30.0, 40.0, 50.0],
            'baz': ['cat', 'dog', 'bird', 'fish', 'lizard']
        }).loc[:, ('foo', 'bar', 'baz')]

        test_table = DynamicTable.from_dataframe(df, 'with_columns_and_data', table_description='a test table')
        table = self.with_columns_and_data()
        self.assertTrue(table == test_table)

    def test_eq_diff_missing_col(self):
        columns = [
            VectorData(name=s['name'], description=s['description'], data=d)
            for s, d in zip(self.spec, self.data)
        ]
        del columns[-1]
        test_table = DynamicTable(name="with_columns_and_data", description='a test table', columns=columns)

        table = self.with_columns_and_data()
        self.assertFalse(table == test_table)

    def test_eq_diff_name(self):
        columns = [
            VectorData(name=s['name'], description=s['description'], data=d)
            for s, d in zip(self.spec, self.data)
        ]
        test_table = DynamicTable(name="wrong name", description='a test table', columns=columns)

        table = self.with_columns_and_data()
        self.assertFalse(table == test_table)

    def test_eq_diff_desc(self):
        columns = [
            VectorData(name=s['name'], description=s['description'], data=d)
            for s, d in zip(self.spec, self.data)
        ]
        test_table = DynamicTable(name="with_columns_and_data", description='wrong description', columns=columns)

        table = self.with_columns_and_data()
        self.assertFalse(table == test_table)

    def test_eq_bad_type(self):
        container = Container('test_container')
        table = self.with_columns_and_data()
        self.assertFalse(table == container)


class TestDynamicTableRoundTrip(H5RoundTripMixin, TestCase):

    def setUpContainer(self):
        table = DynamicTable(name='table0', description='an example table')
        table.add_column(name='foo', description='an int column')
        table.add_column(name='bar', description='a float column')
        table.add_column(name='baz', description='a string column')
        table.add_column(name='qux', description='a boolean column')
        table.add_column(name='corge', description='a doubly indexed int column', index=2)
        table.add_column(name='quux', description='an enum column', enum=True)
        table.add_row(foo=27, bar=28.0, baz="cat", corge=[[1, 2, 3], [4, 5, 6]], qux=True, quux='a')
        table.add_row(foo=37, bar=38.0, baz="dog", corge=[[11, 12, 13], [14, 15, 16]], qux=False, quux='b')
        table.add_column(name='agv', description='a column with autogenerated multi vector index',
                         data=[[[1, 2, 3], [4, 5]], [[6, 7], [8, 9, 10]]], index=2)
        return table

    def test_index_out_of_bounds(self):
        table = self.roundtripContainer()
        msg = "Row index 5 out of range for DynamicTable 'root' (length 2)."
        with self.assertRaisesWith(IndexError, msg):
            table[5]


class TestEmptyDynamicTableRoundTrip(H5RoundTripMixin, TestCase):
    """Test roundtripping a DynamicTable with no rows and no columns."""

    def setUpContainer(self):
        table = DynamicTable(name='table0', description='an example table')
        return table


class TestDynamicTableRegion(TestCase):

    def setUp(self):
        self.spec = [
            {'name': 'foo', 'description': 'foo column'},
            {'name': 'bar', 'description': 'bar column'},
            {'name': 'baz', 'description': 'baz column'},
        ]
        self.data = [
            [1, 2, 3, 4, 5],
            [10.0, 20.0, 30.0, 40.0, 50.0],
            ['cat', 'dog', 'bird', 'fish', 'lizard']
        ]

    def with_columns_and_data(self):
        columns = [
            VectorData(name=s['name'], description=s['description'], data=d)
            for s, d in zip(self.spec, self.data)
        ]
        return DynamicTable(name="with_columns_and_data", description='a test table', columns=columns)

    def test_indexed_dynamic_table_region(self):
        table = self.with_columns_and_data()
        dynamic_table_region = DynamicTableRegion(name='dtr', data=[1, 2, 2], description='desc', table=table)
        fetch_ids = dynamic_table_region[:3].index.values
        self.assertListEqual(fetch_ids.tolist(), [1, 2, 2])

    def test_dynamic_table_region_iteration(self):
        table = self.with_columns_and_data()
        dynamic_table_region = DynamicTableRegion(name='dtr', data=[0, 1, 2, 3, 4], description='desc', table=table)
        for ii, item in enumerate(dynamic_table_region):
            self.assertTrue(table[ii].equals(item))

    def test_dynamic_table_region_shape(self):
        table = self.with_columns_and_data()
        dynamic_table_region = DynamicTableRegion(name='dtr', data=[0, 1, 2, 3, 4], description='desc', table=table)
        self.assertTupleEqual(dynamic_table_region.shape, (5, 3))

    def test_dynamic_table_region_to_dataframe(self):
        table = self.with_columns_and_data()
        dynamic_table_region = DynamicTableRegion(name='dtr', data=[0, 1, 2, 2], description='desc', table=table)
        res = dynamic_table_region.to_dataframe()
        self.assertListEqual(res.index.tolist(), [0, 1, 2, 2])
        self.assertListEqual(res['foo'].tolist(), [1, 2, 3, 3])
        self.assertListEqual(res['bar'].tolist(), [10.0, 20.0, 30.0, 30.0])
        self.assertListEqual(res['baz'].tolist(), ['cat', 'dog', 'bird', 'bird'])

    def test_dynamic_table_region_to_dataframe_exclude_cols(self):
        table = self.with_columns_and_data()
        dynamic_table_region = DynamicTableRegion(name='dtr', data=[0, 1, 2, 2], description='desc', table=table)
        res = dynamic_table_region.to_dataframe(exclude={'baz', 'foo'})
        self.assertListEqual(res.index.tolist(), [0, 1, 2, 2])
        self.assertEqual(len(res.columns), 1)
        self.assertListEqual(res['bar'].tolist(), [10.0, 20.0, 30.0, 30.0])

    def test_dynamic_table_region_getitem_slice(self):
        table = self.with_columns_and_data()
        dynamic_table_region = DynamicTableRegion(name='dtr', data=[0, 1, 2, 2], description='desc', table=table)
        res = dynamic_table_region[1:3]
        self.assertListEqual(res.index.tolist(), [1, 2])
        self.assertListEqual(res['foo'].tolist(), [2, 3])
        self.assertListEqual(res['bar'].tolist(), [20.0, 30.0])
        self.assertListEqual(res['baz'].tolist(), ['dog', 'bird'])

    def test_dynamic_table_region_getitem_single_row_by_index(self):
        table = self.with_columns_and_data()
        dynamic_table_region = DynamicTableRegion(name='dtr', data=[0, 1, 2, 2], description='desc', table=table)
        res = dynamic_table_region[2]
        self.assertListEqual(res.index.tolist(), [2, ])
        self.assertListEqual(res['foo'].tolist(), [3, ])
        self.assertListEqual(res['bar'].tolist(), [30.0, ])
        self.assertListEqual(res['baz'].tolist(), ['bird', ])

    def test_dynamic_table_region_getitem_single_cell(self):
        table = self.with_columns_and_data()
        dynamic_table_region = DynamicTableRegion(name='dtr', data=[0, 1, 2, 2], description='desc', table=table)
        res = dynamic_table_region[2, 'foo']
        self.assertEqual(res, 3)
        res = dynamic_table_region[1, 'baz']
        self.assertEqual(res, 'dog')

    def test_dynamic_table_region_getitem_slice_of_column(self):
        table = self.with_columns_and_data()
        dynamic_table_region = DynamicTableRegion(name='dtr', data=[0, 1, 2, 2], description='desc', table=table)
        res = dynamic_table_region[0:3, 'foo']
        self.assertListEqual(res, [1, 2, 3])
        res = dynamic_table_region[1:3, 'baz']
        self.assertListEqual(res, ['dog', 'bird'])

    def test_dynamic_table_region_getitem_bad_index(self):
        table = self.with_columns_and_data()
        dynamic_table_region = DynamicTableRegion(name='dtr', data=[0, 1, 2, 2], description='desc', table=table)
        with self.assertRaises(ValueError):
            _ = dynamic_table_region[True]

    def test_dynamic_table_region_table_prop(self):
        table = self.with_columns_and_data()
        dynamic_table_region = DynamicTableRegion(name='dtr', data=[0, 1, 2, 2], description='desc', table=table)
        self.assertEqual(table, dynamic_table_region.table)

    def test_dynamic_table_region_set_table_prop(self):
        table = self.with_columns_and_data()
        dynamic_table_region = DynamicTableRegion(name='dtr', data=[0, 1, 2, 2], description='desc')
        dynamic_table_region.table = table
        self.assertEqual(table, dynamic_table_region.table)

    def test_dynamic_table_region_set_table_prop_to_none(self):
        table = self.with_columns_and_data()
        dynamic_table_region = DynamicTableRegion(name='dtr', data=[0, 1, 2, 2], description='desc', table=table)
        try:
            dynamic_table_region.table = None
        except AttributeError:
            self.fail("DynamicTableRegion table setter raised AttributeError unexpectedly!")

    @unittest.skip('we no longer check data contents for performance reasons')
    def test_dynamic_table_region_set_with_bad_data(self):
        table = self.with_columns_and_data()
        # index 5 is out of range
        dynamic_table_region = DynamicTableRegion(name='dtr', data=[5, 1], description='desc')
        with self.assertRaises(IndexError):
            dynamic_table_region.table = table
        self.assertIsNone(dynamic_table_region.table)

    def test_repr(self):
        table = self.with_columns_and_data()
        dynamic_table_region = DynamicTableRegion(name='dtr', data=[1, 2, 2], description='desc', table=table)
        expected = """dtr hdmf.common.table.DynamicTableRegion at 0x%d
    Target table: with_columns_and_data hdmf.common.table.DynamicTable at 0x%d
"""
        expected = expected % (id(dynamic_table_region), id(table))
        self.assertEqual(str(dynamic_table_region), expected)

    def test_no_df_nested(self):
        table = self.with_columns_and_data()
        dynamic_table_region = DynamicTableRegion(name='dtr', data=[0, 1, 2, 2], description='desc', table=table)
        msg = 'DynamicTableRegion.get() with df=False and index=False is not yet supported.'
        with self.assertRaisesWith(ValueError, msg):
            dynamic_table_region.get(0, df=False, index=False)


class DynamicTableRegionRoundTrip(H5RoundTripMixin, TestCase):

    def make_tables(self):
        self.spec2 = [
            {'name': 'qux', 'description': 'qux column'},
            {'name': 'quz', 'description': 'quz column'},
        ]
        self.data2 = [
            ['qux_1', 'qux_2'],
            ['quz_1', 'quz_2'],
        ]

        target_columns = [
            VectorData(name=s['name'], description=s['description'], data=d)
            for s, d in zip(self.spec2, self.data2)
        ]
        target_table = DynamicTable(name="target_table",
                                    description='example table to target with a DynamicTableRegion',
                                    columns=target_columns)

        self.spec1 = [
            {'name': 'foo', 'description': 'foo column'},
            {'name': 'bar', 'description': 'bar column'},
            {'name': 'baz', 'description': 'baz column'},
            {'name': 'dtr', 'description': 'DTR'},
        ]
        self.data1 = [
            [1, 2, 3, 4, 5],
            [10.0, 20.0, 30.0, 40.0, 50.0],
            ['cat', 'dog', 'bird', 'fish', 'lizard']
        ]
        columns = [
            VectorData(name=s['name'], description=s['description'], data=d)
            for s, d in zip(self.spec1, self.data1)
        ]
        columns.append(DynamicTableRegion(name='dtr', description='example DynamicTableRegion',
                                          data=[0, 1, 1, 0, 1], table=target_table))
        table = DynamicTable(name="table_with_dtr",
                             description='a test table that has a DynamicTableRegion',
                             columns=columns)
        return table, target_table

    def setUp(self):
        self.table, self.target_table = self.make_tables()
        super().setUp()

    def setUpContainer(self):
        multi_container = SimpleMultiContainer(name='multi', containers=[self.target_table, self.table])
        return multi_container

    def _get(self, arg):
        mc = self.roundtripContainer()
        table = mc.containers['table_with_dtr']
        return table.get(arg)

    def _get_nested(self, arg):
        mc = self.roundtripContainer()
        table = mc.containers['table_with_dtr']
        return table.get(arg, index=False)

    def _get_nodf(self, arg):
        mc = self.roundtripContainer()
        table = mc.containers['table_with_dtr']
        return table.get(arg, df=False)

    def _getitem(self, arg):
        mc = self.roundtripContainer()
        table = mc.containers['table_with_dtr']
        return table[arg]

    def test_getitem_oor(self):
        msg = 'Row index 12 out of range for DynamicTable \'table_with_dtr\' (length 5).'
        with self.assertRaisesWith(IndexError, msg):
            self._getitem(12)

    def test_getitem_badcol(self):
        with self.assertRaises(KeyError):
            self._getitem('boo')

    def _assert_two_elem_df(self, rec):
        columns = ['foo', 'bar', 'baz', 'dtr']
        data = [[1, 10.0, 'cat', 0],
                [2, 20.0, 'dog', 1]]
        exp = pd.DataFrame(data=data, columns=columns, index=pd.Series(name='id', data=[0, 1]))
        pd.testing.assert_frame_equal(rec, exp, check_dtype=False)

    def _assert_one_elem_df(self, rec):
        columns = ['foo', 'bar', 'baz', 'dtr']
        data = [[1, 10.0, 'cat', 0]]
        exp = pd.DataFrame(data=data, columns=columns, index=pd.Series(name='id', data=[0]))
        pd.testing.assert_frame_equal(rec, exp, check_dtype=False)

    def _assert_two_elem_df_nested(self, rec):
        nested_columns = ['qux', 'quz']
        nested_data = [['qux_1', 'quz_1'], ['qux_2', 'quz_2']]
        nested_df = pd.DataFrame(data=nested_data, columns=nested_columns, index=pd.Series(name='id', data=[0, 1]))

        columns = ['foo', 'bar', 'baz']
        data = [[1, 10.0, 'cat'],
                [2, 20.0, 'dog']]
        exp = pd.DataFrame(data=data, columns=columns, index=pd.Series(name='id', data=[0, 1]))

        # remove nested dataframe and test each df separately
        pd.testing.assert_frame_equal(rec['dtr'][0], nested_df.iloc[[0]])
        pd.testing.assert_frame_equal(rec['dtr'][1], nested_df.iloc[[1]])
        del rec['dtr']
        pd.testing.assert_frame_equal(rec, exp, check_dtype=False)

    def _assert_one_elem_df_nested(self, rec):
        nested_columns = ['qux', 'quz']
        nested_data = [['qux_1', 'quz_1'], ['qux_2', 'quz_2']]
        nested_df = pd.DataFrame(data=nested_data, columns=nested_columns, index=pd.Series(name='id', data=[0, 1]))

        columns = ['foo', 'bar', 'baz']
        data = [[1, 10.0, 'cat']]
        exp = pd.DataFrame(data=data, columns=columns, index=pd.Series(name='id', data=[0]))

        # remove nested dataframe and test each df separately
        pd.testing.assert_frame_equal(rec['dtr'][0], nested_df.iloc[[0]])
        del rec['dtr']
        pd.testing.assert_frame_equal(rec, exp, check_dtype=False)

    #####################
    # tests DynamicTableRegion.__getitem__
    def test_getitem_int(self):
        rec = self._getitem(0)
        self._assert_one_elem_df(rec)

    def test_getitem_list_single(self):
        rec = self._getitem([0])
        self._assert_one_elem_df(rec)

    def test_getitem_list(self):
        rec = self._getitem([0, 1])
        self._assert_two_elem_df(rec)

    def test_getitem_slice(self):
        rec = self._getitem(slice(0, 2, None))
        self._assert_two_elem_df(rec)

    #####################
    # tests DynamicTableRegion.get, return a DataFrame
    def test_get_int(self):
        rec = self._get(0)
        self._assert_one_elem_df(rec)

    def test_get_list_single(self):
        rec = self._get([0])
        self._assert_one_elem_df(rec)

    def test_get_list(self):
        rec = self._get([0, 1])
        self._assert_two_elem_df(rec)

    def test_get_slice(self):
        rec = self._get(slice(0, 2, None))
        self._assert_two_elem_df(rec)

    #####################
    # tests DynamicTableRegion.get, return a DataFrame with nested DataFrame
    def test_get_nested_int(self):
        rec = self._get_nested(0)
        self._assert_one_elem_df_nested(rec)

    def test_get_nested_list_single(self):
        rec = self._get_nested([0])
        self._assert_one_elem_df_nested(rec)

    def test_get_nested_list(self):
        rec = self._get_nested([0, 1])
        self._assert_two_elem_df_nested(rec)

    def test_get_nested_slice(self):
        rec = self._get_nested(slice(0, 2, None))
        self._assert_two_elem_df_nested(rec)

    #####################
    # tests DynamicTableRegion.get, DO NOT return a DataFrame
    def test_get_nodf_int(self):
        rec = self._get_nodf(0)
        exp = [0, 1, 10.0, 'cat', 0]
        self.assertListEqual(rec, exp)

    def _assert_list_of_ndarray_equal(self, l1, l2):
        """
        This is a helper function for test_get_nodf_list and test_get_nodf_slice.
        It compares ndarrays from a list of ndarrays
        """
        for a1, a2 in zip(l1, l2):
            if isinstance(a1, list):
                self._assert_list_of_ndarray_equal(a1, a2)
            else:
                np.testing.assert_array_equal(a1, a2)

    def test_get_nodf_list_single(self):
        rec = self._get_nodf([0])
        exp = [np.array([0]), np.array([1]), np.array([10.0]), np.array(['cat']), np.array([0])]
        self._assert_list_of_ndarray_equal(exp, rec)

    def test_get_nodf_list(self):
        rec = self._get_nodf([0, 1])
        exp = [np.array([0, 1]), np.array([1, 2]), np.array([10.0, 20.0]), np.array(['cat', 'dog']), np.array([0, 1])]
        self._assert_list_of_ndarray_equal(exp, rec)

    def test_get_nodf_slice(self):
        rec = self._get_nodf(slice(0, 2, None))
        exp = [np.array([0, 1]), np.array([1, 2]), np.array([10.0, 20.0]), np.array(['cat', 'dog']), np.array([0, 1])]
        self._assert_list_of_ndarray_equal(exp, rec)

    def test_getitem_int_str(self):
        """Test DynamicTableRegion.__getitem__ with (int, str)."""
        mc = self.roundtripContainer()
        table = mc.containers['table_with_dtr']
        rec = table['dtr'][0, 'qux']
        self.assertEqual(rec, 'qux_1')

    def test_getitem_str(self):
        """Test DynamicTableRegion.__getitem__ with str."""
        mc = self.roundtripContainer()
        table = mc.containers['table_with_dtr']
        rec = table['dtr']['qux']
        self.assertIs(rec, mc.containers['target_table']['qux'])


class TestElementIdentifiers(TestCase):

    def setUp(self):
        self.e = ElementIdentifiers(name='ids', data=[0, 1, 2, 3, 4])

    def test_identifier_search_single_list(self):
        a = (self.e == [1])
        np.testing.assert_array_equal(a, [1])

    def test_identifier_search_single_int(self):
        a = (self.e == 2)
        np.testing.assert_array_equal(a, [2])

    def test_identifier_search_single_list_not_found(self):
        a = (self.e == [10])
        np.testing.assert_array_equal(a, [])

    def test_identifier_search_single_int_not_found(self):
        a = (self.e == 10)
        np.testing.assert_array_equal(a, [])

    def test_identifier_search_single_list_all_match(self):
        a = (self.e == [1, 2, 3])
        np.testing.assert_array_equal(a, [1, 2, 3])

    def test_identifier_search_single_list_partial_match(self):
        a = (self.e == [1, 2, 10])
        np.testing.assert_array_equal(a, [1, 2])
        a = (self.e == [-1, 2, 10])
        np.testing.assert_array_equal(a, [2, ])

    def test_identifier_search_with_element_identifier(self):
        a = (self.e == ElementIdentifiers(name='ids', data=[1, 2, 10]))
        np.testing.assert_array_equal(a, [1, 2])

    def test_identifier_search_with_bad_ids(self):
        with self.assertRaises(TypeError):
            _ = (self.e == 0.1)
        with self.assertRaises(TypeError):
            _ = (self.e == 'test')


class TestBadElementIdentifiers(TestCase):

    def test_bad_dtype(self):
        with self.assertRaisesWith(ValueError, "ElementIdentifiers must contain integers"):
            ElementIdentifiers(name='ids', data=["1", "2"])

        with self.assertRaisesWith(ValueError, "ElementIdentifiers must contain integers"):
            ElementIdentifiers(name='ids', data=np.array(["1", "2"]))

        with self.assertRaisesWith(ValueError, "ElementIdentifiers must contain integers"):
            ElementIdentifiers(name='ids', data=[1.0, 2.0])

    def test_dci_int_ok(self):
        a = np.arange(30)
        dci = DataChunkIterator(data=a, buffer_size=1)
        e = ElementIdentifiers(name='ids', data=dci)  # test that no error is raised
        self.assertIs(e.data, dci)

    def test_dci_float_bad(self):
        a = np.arange(30.0)
        dci = DataChunkIterator(data=a, buffer_size=1)
        with self.assertRaisesWith(ValueError, "ElementIdentifiers must contain integers"):
            ElementIdentifiers(name='ids', data=dci)

    def test_dataio_dci_ok(self):
        a = np.arange(30)
        dci = DataChunkIterator(data=a, buffer_size=1)
        dio = H5DataIO(dci)
        e = ElementIdentifiers(name='ids', data=dio)  # test that no error is raised
        self.assertIs(e.data, dio)


class SubTable(DynamicTable):

    __columns__ = (
        {'name': 'col1', 'description': 'required column', 'required': True},
        {'name': 'col2', 'description': 'optional column'},
        {'name': 'col3', 'description': 'required, indexed column', 'required': True, 'index': True},
        {'name': 'col4', 'description': 'optional, indexed column', 'index': True},
        {'name': 'col5', 'description': 'required region', 'required': True, 'table': True},
        {'name': 'col6', 'description': 'optional region', 'table': True},
        {'name': 'col7', 'description': 'required, indexed region', 'required': True, 'index': True, 'table': True},
        {'name': 'col8', 'description': 'optional, indexed region', 'index': True, 'table': True},
        {'name': 'col10', 'description': 'optional, indexed enum column', 'index': True, 'class': EnumData},
        {'name': 'col11', 'description': 'optional, enumerable column', 'enum': True, 'index': True},
    )


class SubSubTable(SubTable):

    __columns__ = (
        {'name': 'col9', 'description': 'required column', 'required': True},
        # TODO handle edge case where subclass re-defines a column from superclass
        # {'name': 'col2', 'description': 'optional column subsub', 'required': True},  # make col2 required
    )


class TestDynamicTableClassColumns(TestCase):
    """Test functionality related to the predefined __columns__ field of a DynamicTable class."""

    def test_init(self):
        """Test that required columns, and not optional columns, in __columns__ are created on init."""
        table = SubTable(name='subtable', description='subtable description')
        self.assertEqual(table.colnames, ('col1', 'col3', 'col5', 'col7'))
        # test different access methods. note: table.get('col1') is equivalent to table['col1']
        self.assertEqual(table.col1.description, 'required column')
        self.assertEqual(table.col3.description, 'required, indexed column')
        self.assertEqual(table.col5.description, 'required region')
        self.assertEqual(table.col7.description, 'required, indexed region')
        self.assertEqual(table['col1'].description, 'required column')
        # self.assertEqual(table['col3'].description, 'required, indexed column')  # TODO this should work

        self.assertIsNone(table.col2)
        self.assertIsNone(table.col4)
        self.assertIsNone(table.col4_index)
        self.assertIsNone(table.col6)
        self.assertIsNone(table.col8)
        self.assertIsNone(table.col8_index)
        self.assertIsNone(table.col11)
        self.assertIsNone(table.col11_index)

        # uninitialized optional predefined columns cannot be accessed in this manner
        with self.assertRaises(KeyError):
            table['col2']

    def test_gather_columns_inheritance(self):
        """Test that gathering columns across a type hierarchy works."""
        table = SubSubTable(name='subtable', description='subtable description')
        self.assertEqual(table.colnames, ('col1', 'col3', 'col5', 'col7', 'col9'))

    def test_bad_predefined_columns(self):
        """Test that gathering columns across a type hierarchy works."""
        msg = "'__columns__' must be of type tuple, found <class 'list'>"
        with self.assertRaisesWith(TypeError, msg):
            class BadSubTable(DynamicTable):

                __columns__ = []

    def test_add_req_column(self):
        """Test that adding a required column from __columns__ raises an error."""
        table = SubTable(name='subtable', description='subtable description')
        msg = "column 'col1' already exists in SubTable 'subtable'"
        with self.assertRaisesWith(ValueError, msg):
            table.add_column(name='col1', description='column #1')

    def test_add_req_ind_column(self):
        """Test that adding a required, indexed column from __columns__ raises an error."""
        table = SubTable(name='subtable', description='subtable description')
        msg = "column 'col3' already exists in SubTable 'subtable'"
        with self.assertRaisesWith(ValueError, msg):
            table.add_column(name='col3', description='column #3')

    def test_add_opt_column(self):
        """Test that adding an optional column from __columns__ with matching specs except for description works."""
        table = SubTable(name='subtable', description='subtable description')

        table.add_column(name='col2', description='column #2')  # override __columns__ description
        self.assertEqual(table.col2.description, 'column #2')

        table.add_column(name='col4', description='column #4', index=True)
        self.assertEqual(table.col4.description, 'column #4')

        table.add_column(name='col6', description='column #6', table=True)
        self.assertEqual(table.col6.description, 'column #6')

        table.add_column(name='col8', description='column #8', index=True, table=True)
        self.assertEqual(table.col8.description, 'column #8')

        table.add_column(name='col10', description='column #10', index=True, col_cls=EnumData)
        self.assertIsInstance(table.col10, EnumData)

        table.add_column(name='col11', description='column #11', enum=True, index=True)
        self.assertIsInstance(table.col11, EnumData)

    def test_add_opt_column_mismatched_table_true(self):
        """Test that adding an optional column from __columns__ with non-matched table raises a warning."""
        table = SubTable(name='subtable', description='subtable description')
        msg = ("Column 'col2' is predefined in SubTable with table=False which does not match the entered table "
               "argument. The predefined table spec will be ignored. "
               "Please ensure the new column complies with the spec. "
               "This will raise an error in a future version of HDMF.")
        with self.assertWarnsWith(UserWarning, msg):
            table.add_column(name='col2', description='column #2', table=True)
        self.assertEqual(table.col2.description, 'column #2')
        self.assertEqual(type(table.col2), DynamicTableRegion)  # not VectorData

    def test_add_opt_column_mismatched_table_table(self):
        """Test that adding an optional column from __columns__ with non-matched table raises a warning."""
        table = SubTable(name='subtable', description='subtable description')
        msg = ("Column 'col2' is predefined in SubTable with table=False which does not match the entered table "
               "argument. The predefined table spec will be ignored. "
               "Please ensure the new column complies with the spec. "
               "This will raise an error in a future version of HDMF.")
        with self.assertWarnsWith(UserWarning, msg):
            table.add_column(name='col2', description='column #2',
                             table=DynamicTable(name='dummy', description='dummy'))
        self.assertEqual(table.col2.description, 'column #2')
        self.assertEqual(type(table.col2), DynamicTableRegion)  # not VectorData

    def test_add_opt_column_mismatched_index_true(self):
        """Test that adding an optional column from __columns__ with non-matched table raises a warning."""
        table = SubTable(name='subtable', description='subtable description')
        msg = ("Column 'col2' is predefined in SubTable with index=False which does not match the entered index "
               "argument. The predefined index spec will be ignored. "
               "Please ensure the new column complies with the spec. "
               "This will raise an error in a future version of HDMF.")
        with self.assertWarnsWith(UserWarning, msg):
            table.add_column(name='col2', description='column #2', index=True)
        self.assertEqual(table.col2.description, 'column #2')
        self.assertEqual(type(table.get('col2')), VectorIndex)  # not VectorData

    def test_add_opt_column_mismatched_index_data(self):
        """Test that adding an optional column from __columns__ with non-matched table raises a warning."""
        table = SubTable(name='subtable', description='subtable description')
        table.add_row(col1='a', col3='c', col5='e', col7='g')
        table.add_row(col1='a', col3='c', col5='e', col7='g')
        msg = ("Column 'col2' is predefined in SubTable with index=False which does not match the entered index "
               "argument. The predefined index spec will be ignored. "
               "Please ensure the new column complies with the spec. "
               "This will raise an error in a future version of HDMF.")
        with self.assertWarnsWith(UserWarning, msg):
            table.add_column(name='col2', description='column #2', data=[1, 2, 3], index=[1, 2])
        self.assertEqual(table.col2.description, 'column #2')
        self.assertEqual(type(table.get('col2')), VectorIndex)  # not VectorData

    def test_add_opt_column_mismatched_col_cls(self):
        """Test that adding an optional column from __columns__ with non-matched table raises a warning."""
        table = SubTable(name='subtable', description='subtable description')
        msg = ("Column 'col10' is predefined in SubTable with class=<class 'hdmf.common.table.EnumData'> "
               "which does not match the entered col_cls "
               "argument. The predefined class spec will be ignored. "
               "Please ensure the new column complies with the spec. "
               "This will raise an error in a future version of HDMF.")
        with self.assertWarnsWith(UserWarning, msg):
            table.add_column(name='col10', description='column #10', index=True)
        self.assertEqual(table.col10.description, 'column #10')
        self.assertEqual(type(table.col10), VectorData)
        self.assertEqual(type(table.get('col10')), VectorIndex)

    def test_add_opt_column_twice(self):
        """Test that adding an optional column from __columns__ twice fails the second time."""
        table = SubTable(name='subtable', description='subtable description')
        table.add_column(name='col2', description='column #2')

        msg = "column 'col2' already exists in SubTable 'subtable'"
        with self.assertRaisesWith(ValueError, msg):
            table.add_column(name='col2', description='column #2b')

    def test_add_opt_column_after_data(self):
        """Test that adding an optional column from __columns__ with data works."""
        table = SubTable(name='subtable', description='subtable description')
        table.add_row(col1='a', col3='c', col5='e', col7='g')
        table.add_column(name='col2', description='column #2', data=('b', ))
        self.assertTupleEqual(table.col2.data, ('b', ))

    def test_add_opt_ind_column_after_data(self):
        """Test that adding an optional, indexed column from __columns__ with data works."""
        table = SubTable(name='subtable', description='subtable description')
        table.add_row(col1='a', col3='c', col5='e', col7='g')
        # TODO this use case is tricky and should not be allowed
        # table.add_column(name='col4', description='column #4', data=(('b', 'b2'), ))

    def test_add_row_opt_column(self):
        """Test that adding a row with an optional column works."""
        table = SubTable(name='subtable', description='subtable description')
        table.add_row(col1='a', col2='b', col3='c', col4=('d1', 'd2'), col5='e', col7='g')
        table.add_row(col1='a', col2='b2', col3='c', col4=('d3', 'd4'), col5='e', col7='g')
        self.assertTupleEqual(table.colnames, ('col1', 'col3', 'col5', 'col7', 'col2', 'col4'))
        self.assertEqual(table.col2.description, 'optional column')
        self.assertEqual(table.col4.description, 'optional, indexed column')
        self.assertListEqual(table.col2.data, ['b', 'b2'])
        # self.assertListEqual(table.col4.data, [('d1', 'd2'), ('d3', 'd4')])  # TODO this should work

    def test_add_row_opt_column_after_data(self):
        """Test that adding a row with an optional column after adding a row without the column raises an error."""
        table = SubTable(name='subtable', description='subtable description')
        table.add_row(col1='a', col3='c', col5='e', col7='g')
        msg = "column must have the same number of rows as 'id'"  # TODO improve error message
        with self.assertRaisesWith(ValueError, msg):
            table.add_row(col1='a', col2='b', col3='c', col5='e', col7='g')

    def test_init_columns_add_req_column(self):
        """Test that passing a required column to init works."""
        col1 = VectorData(name='col1', description='column #1')  # override __columns__ description
        table = SubTable(name='subtable', description='subtable description', columns=[col1])
        self.assertEqual(table.colnames, ('col1', 'col3', 'col5', 'col7'))
        self.assertEqual(table.col1.description, 'column #1')
        self.assertTrue(hasattr(table, 'col1'))

    def test_init_columns_add_req_column_mismatch_index(self):
        """Test that passing a required column that does not match the predefined column specs raises an error."""
        col1 = VectorData(name='col1', description='column #1')  # override __columns__ description
        col1_ind = VectorIndex(name='col1_index', data=list(), target=col1)

        # TODO raise an error
        SubTable(name='subtable', description='subtable description', columns=[col1_ind, col1])

    def test_init_columns_add_req_column_mismatch_table(self):
        """Test that passing a required column that does not match the predefined column specs raises an error."""
        dummy_table = DynamicTable(name='dummy', description='dummy table')
        col1 = DynamicTableRegion(name='col1', data=list(), description='column #1', table=dummy_table)

        # TODO raise an error
        SubTable(name='subtable', description='subtable description', columns=[col1])

    def test_init_columns_add_opt_column(self):
        """Test that passing an optional column to init works."""
        col2 = VectorData(name='col2', description='column #2')  # override __columns__ description
        table = SubTable(name='subtable', description='subtable description', columns=[col2])
        self.assertEqual(table.colnames, ('col2', 'col1', 'col3', 'col5', 'col7'))
        self.assertEqual(table.col2.description, 'column #2')

    def test_init_columns_add_dup_column(self):
        """Test that passing two columns with the same name raises an error."""
        col1 = VectorData(name='col1', description='column #1')  # override __columns__ description
        col1_ind = VectorIndex(name='col1', data=list(), target=col1)

        msg = "'columns' contains columns with duplicate names: ['col1', 'col1']"
        with self.assertRaisesWith(ValueError, msg):
            SubTable(name='subtable', description='subtable description', columns=[col1_ind, col1])

    def test_no_set_target_tables(self):
        """Test that the target table of a predefined DTR column is None."""
        table = SubTable(name='subtable', description='subtable description')
        self.assertIsNone(table.col5.table)

    def test_set_target_tables(self):
        """Test setting target tables for predefined DTR columns."""
        table1 = SubTable(name='subtable1', description='subtable description')
        table2 = SubTable(
            name='subtable2',
            description='subtable description',
            target_tables={
                'col5': table1,
                'col6': table1,
                'col7': table1,
                'col8': table1,
            },
        )
        self.assertIs(table2.col5.table, table1)
        self.assertIs(table2.col6.table, table1)
        self.assertIs(table2.col7.table, table1)
        self.assertIs(table2.col8.table, table1)

    def test_set_target_tables_unknown_col(self):
        """Test setting target tables for unknown columns."""
        table1 = SubTable(name='subtable1', description='subtable description')
        msg = r"'bad_col' is not the name of a predefined column of table subtable2 .*"
        with self.assertRaisesRegex(ValueError, msg):
            SubTable(
                name='subtable2',
                description='subtable description',
                target_tables={
                    'bad_col': table1,
                },
            )

    def test_set_target_tables_bad_init_col(self):
        """Test setting target tables for predefined, required non-DTR columns."""
        table1 = SubTable(name='subtable1', description='subtable description')
        msg = "Column 'col1' must be a DynamicTableRegion to have a target table."
        with self.assertRaisesWith(ValueError, msg):
            SubTable(
                name='subtable2',
                description='subtable description',
                target_tables={
                    'col1': table1,
                },
            )

    def test_set_target_tables_bad_opt_col(self):
        """Test setting target tables for predefined, optional non-DTR columns."""
        table1 = SubTable(name='subtable1', description='subtable description')
        msg = "Column 'col2' must be a DynamicTableRegion to have a target table."
        with self.assertRaisesWith(ValueError, msg):
            SubTable(
                name='subtable2',
                description='subtable description',
                target_tables={
                    'col2': table1,
                },
            )

    def test_set_target_tables_existing_col_mismatch(self):
        """Test setting target tables for an existing DTR column with a mismatched, existing target table."""
        table1 = SubTable(name='subtable1', description='subtable description')
        table2 = SubTable(name='subtable2', description='subtable description')
        dtr = DynamicTableRegion(name='dtr', data=[], description='desc', table=table1)
        msg = "Column 'dtr' already has a target table that is not the passed table."
        with self.assertRaisesWith(ValueError, msg):
            SubTable(
                name='subtable3',
                description='subtable description',
                columns=[dtr],
                target_tables={
                    'dtr': table2,
                },
            )

    def test_set_target_tables_existing_col_match(self):
        """Test setting target tables for an existing DTR column with a matching, existing target table."""
        table1 = SubTable(name='subtable1', description='subtable description')
        dtr = DynamicTableRegion(name='dtr', data=[], description='desc', table=table1)
        SubTable(
            name='subtable2',
            description='subtable description',
            columns=[dtr],
            target_tables={
                'dtr': table1,
            },
        )


class TestEnumData(TestCase):

    def test_init(self):
        ed = EnumData(name='cv_data', description='a test EnumData', elements=['a', 'b', 'c'],
                      data=np.array([0, 0, 1, 1, 2, 2]))
        self.assertIsInstance(ed.elements, VectorData)

    def test_get(self):
        ed = EnumData(name='cv_data', description='a test EnumData', elements=['a', 'b', 'c'],
                      data=np.array([0, 0, 1, 1, 2, 2]))
        dat = ed[2]
        self.assertEqual(dat, 'b')
        dat = ed[-1]
        self.assertEqual(dat, 'c')
        dat = ed[0]
        self.assertEqual(dat, 'a')

    def test_get_list(self):
        ed = EnumData(name='cv_data', description='a test EnumData', elements=['a', 'b', 'c'],
                      data=np.array([0, 0, 1, 1, 2, 2]))
        dat = ed[[0, 1, 2]]
        np.testing.assert_array_equal(dat, ['a', 'a', 'b'])

    def test_get_list_join(self):
        ed = EnumData(name='cv_data', description='a test EnumData', elements=['a', 'b', 'c'],
                      data=np.array([0, 0, 1, 1, 2, 2]))
        dat = ed.get([0, 1, 2], join=True)
        self.assertEqual(dat, 'aab')

    def test_get_list_indices(self):
        ed = EnumData(name='cv_data', description='a test EnumData', elements=['a', 'b', 'c'],
                      data=np.array([0, 0, 1, 1, 2, 2]))
        dat = ed.get([0, 1, 2], index=True)
        np.testing.assert_array_equal(dat, [0, 0, 1])

    def test_get_2d(self):
        ed = EnumData(name='cv_data', description='a test EnumData',
                      elements=['a', 'b', 'c'],
                      data=np.array([[0, 0], [1, 1], [2, 2]]))
        dat = ed[0]
        np.testing.assert_array_equal(dat, ['a', 'a'])

    def test_get_2d_w_2d(self):
        ed = EnumData(name='cv_data', description='a test EnumData',
                      elements=['a', 'b', 'c'],
                      data=np.array([[0, 0], [1, 1], [2, 2]]))
        dat = ed[[0, 1]]
        np.testing.assert_array_equal(dat, [['a', 'a'], ['b', 'b']])

    def test_add_row(self):
        ed = EnumData(name='cv_data', description='a test EnumData', elements=['a', 'b', 'c'])
        ed.add_row('b')
        ed.add_row('a')
        ed.add_row('c')
        np.testing.assert_array_equal(ed.data, np.array([1, 0, 2], dtype=np.uint8))

    def test_add_row_index(self):
        ed = EnumData(name='cv_data', description='a test EnumData', elements=['a', 'b', 'c'])
        ed.add_row(1, index=True)
        ed.add_row(0, index=True)
        ed.add_row(2, index=True)
        np.testing.assert_array_equal(ed.data, np.array([1, 0, 2], dtype=np.uint8))


class TestIndexedEnumData(TestCase):

    def test_init(self):
        ed = EnumData(name='cv_data', description='a test EnumData',
                      elements=['a', 'b', 'c'], data=np.array([0, 0, 1, 1, 2, 2]))
        idx = VectorIndex(name='enum_index', data=[2, 4, 6], target=ed)
        self.assertIsInstance(ed.elements, VectorData)
        self.assertIsInstance(idx.target, EnumData)

    def test_add_row(self):
        ed = EnumData(name='cv_data', description='a test EnumData', elements=['a', 'b', 'c'])
        idx = VectorIndex(name='enum_index', data=list(), target=ed)
        idx.add_row(['a', 'a', 'a'])
        idx.add_row(['b', 'b'])
        idx.add_row(['c', 'c', 'c', 'c'])
        np.testing.assert_array_equal(idx[0], ['a', 'a', 'a'])
        np.testing.assert_array_equal(idx[1], ['b', 'b'])
        np.testing.assert_array_equal(idx[2], ['c', 'c', 'c', 'c'])

    def test_add_row_index(self):
        ed = EnumData(name='cv_data', description='a test EnumData', elements=['a', 'b', 'c'])
        idx = VectorIndex(name='enum_index', data=list(), target=ed)
        idx.add_row([0, 0, 0], index=True)
        idx.add_row([1, 1], index=True)
        idx.add_row([2, 2, 2, 2], index=True)
        np.testing.assert_array_equal(idx[0], ['a', 'a', 'a'])
        np.testing.assert_array_equal(idx[1], ['b', 'b'])
        np.testing.assert_array_equal(idx[2], ['c', 'c', 'c', 'c'])

    @unittest.skip("feature is not yet supported")
    def test_add_2d_row_index(self):
        ed = EnumData(name='cv_data', description='a test EnumData', elements=['a', 'b', 'c'])
        idx = VectorIndex(name='enum_index', data=list(), target=ed)
        idx.add_row([['a', 'a'], ['a', 'a'], ['a', 'a']])
        idx.add_row([['b', 'b'], ['b', 'b']])
        idx.add_row([['c', 'c'], ['c', 'c'], ['c', 'c'], ['c', 'c']])
        np.testing.assert_array_equal(idx[0], [['a', 'a'], ['a', 'a'], ['a', 'a']])
        np.testing.assert_array_equal(idx[1], [['b', 'b'], ['b', 'b']])
        np.testing.assert_array_equal(idx[2], [['c', 'c'], ['c', 'c'], ['c', 'c'], ['c', 'c']])


class SelectionTestMixin:

    def setUp(self):
        # table1 contains a non-ragged DTR and a ragged DTR, both of which point to table2
        # table2 contains a non-ragged DTR and a ragged DTR, both of which point to table3
        self.table3 = DynamicTable(
            name='table3',
            description='a test table',
            id=[20, 21, 22]
        )
        self.table3.add_column('foo', 'scalar column', data=self._wrap([20.0, 21.0, 22.0]))
        self.table3.add_column('bar', 'ragged column', index=self._wrap([2, 3, 6]),
                               data=self._wrap(['t11', 't12', 't21', 't31', 't32', 't33']))
        self.table3.add_column('baz', 'multi-dimension column',
                               data=self._wrap([[210.0, 211.0, 212.0],
                                                [220.0, 221.0, 222.0],
                                                [230.0, 231.0, 232.0]]))
        # generate expected dataframe for table3
        data = OrderedDict()
        data['foo'] = [20.0, 21.0, 22.0]
        data['bar'] = [['t11', 't12'], ['t21'], ['t31', 't32', 't33']]
        data['baz'] = [[210.0, 211.0, 212.0], [220.0, 221.0, 222.0], [230.0, 231.0, 232.0]]
        idx = [20, 21, 22]
        self.table3_df = pd.DataFrame(data=data, index=pd.Index(name='id', data=idx))

        self.table2 = DynamicTable(
            name='table2',
            description='a test table',
            id=[10, 11, 12]
        )
        self.table2.add_column('foo', 'scalar column', data=self._wrap([10.0, 11.0, 12.0]))
        self.table2.add_column('bar', 'ragged column', index=self._wrap([2, 3, 6]),
                               data=self._wrap(['s11', 's12', 's21', 's31', 's32', 's33']))
        self.table2.add_column('baz', 'multi-dimension column',
                               data=self._wrap([[110.0, 111.0, 112.0],
                                                [120.0, 121.0, 122.0],
                                                [130.0, 131.0, 132.0]]))
        self.table2.add_column('qux', 'DTR column', table=self.table3, data=self._wrap([0, 1, 0]))
        self.table2.add_column('corge', 'ragged DTR column', index=self._wrap([2, 3, 6]), table=self.table3,
                               data=self._wrap([0, 1, 2, 0, 1, 2]))
        # TODO test when ragged DTR indices are not in ascending order

        # generate expected dataframe for table2 *without DTR*
        data = OrderedDict()
        data['foo'] = [10.0, 11.0, 12.0]
        data['bar'] = [['s11', 's12'], ['s21'], ['s31', 's32', 's33']]
        data['baz'] = [[110.0, 111.0, 112.0], [120.0, 121.0, 122.0], [130.0, 131.0, 132.0]]
        idx = [10, 11, 12]
        self.table2_df = pd.DataFrame(data=data, index=pd.Index(name='id', data=idx))

        self.table1 = DynamicTable(
            name='table1',
            description='a table to test slicing',
            id=[0, 1]
        )
        self.table1.add_column('foo', 'scalar column', data=self._wrap([0.0, 1.0]))
        self.table1.add_column('bar', 'ragged column', index=self._wrap([2, 3]),
                               data=self._wrap(['r11', 'r12', 'r21']))
        self.table1.add_column('baz', 'multi-dimension column',
                               data=self._wrap([[10.0, 11.0, 12.0],
                                                [20.0, 21.0, 22.0]]))
        self.table1.add_column('qux', 'DTR column', table=self.table2, data=self._wrap([0, 1]))
        self.table1.add_column('corge', 'ragged DTR column', index=self._wrap([2, 3]), table=self.table2,
                               data=self._wrap([0, 1, 2]))
        self.table1.add_column('barz', 'ragged column of tuples (cpd type)', index=self._wrap([2, 3]),
                               data=self._wrap([(1.0, 11), (2.0, 12), (3.0, 21)]))

        # generate expected dataframe for table1 *without DTR*
        data = OrderedDict()
        data['foo'] = self._wrap_check([0.0, 1.0])
        data['bar'] = [self._wrap_check(['r11', 'r12']), self._wrap_check(['r21'])]
        data['baz'] = [self._wrap_check([10.0, 11.0, 12.0]),
                       self._wrap_check([20.0, 21.0, 22.0])]
        data['barz'] = [self._wrap_check([(1.0, 11), (2.0, 12)]), self._wrap_check([(3.0, 21)])]
        idx = [0, 1]
        self.table1_df = pd.DataFrame(data=data, index=pd.Index(name='id', data=idx))

    def _check_two_rows_df(self, rec):
        data = OrderedDict()
        data['foo'] = self._wrap_check([0.0, 1.0])
        data['bar'] = [self._wrap_check(['r11', 'r12']), self._wrap_check(['r21'])]
        data['baz'] = [self._wrap_check([10.0, 11.0, 12.0]),
                       self._wrap_check([20.0, 21.0, 22.0])]
        data['qux'] = self._wrap_check([0, 1])
        data['corge'] = [self._wrap_check([0, 1]), self._wrap_check([2])]
        data['barz'] = [self._wrap_check([(1.0, 11), (2.0, 12)]), self._wrap_check([(3.0, 21)])]
        idx = [0, 1]
        exp = pd.DataFrame(data=data, index=pd.Index(name='id', data=idx))
        pd.testing.assert_frame_equal(rec, exp)

    def _check_two_rows_df_nested(self, rec):
        # first level: cache nested df cols and remove them before calling pd.testing.assert_frame_equal
        qux_series = rec['qux']
        corge_series = rec['corge']
        del rec['qux']
        del rec['corge']

        idx = [0, 1]
        pd.testing.assert_frame_equal(rec, self.table1_df.loc[idx])

        # second level: compare the nested columns separately
        self.assertEqual(len(qux_series), 2)
        rec_qux1 = qux_series[0]
        rec_qux2 = qux_series[1]
        self._check_table2_first_row_qux(rec_qux1)
        self._check_table2_second_row_qux(rec_qux2)

        self.assertEqual(len(corge_series), 2)
        rec_corge1 = corge_series[0]
        rec_corge2 = corge_series[1]
        self._check_table2_first_row_corge(rec_corge1)
        self._check_table2_second_row_corge(rec_corge2)

    def _check_one_row_df(self, rec):
        data = OrderedDict()
        data['foo'] = self._wrap_check([0.0])
        data['bar'] = [self._wrap_check(['r11', 'r12'])]
        data['baz'] = [self._wrap_check([10.0, 11.0, 12.0])]
        data['qux'] = self._wrap_check([0])
        data['corge'] = [self._wrap_check([0, 1])]
        data['barz'] = [self._wrap_check([(1.0, 11), (2.0, 12)])]
        idx = [0]
        exp = pd.DataFrame(data=data, index=pd.Index(name='id', data=idx))
        pd.testing.assert_frame_equal(rec, exp)

    def _check_one_row_df_nested(self, rec):
        # first level: cache nested df cols and remove them before calling pd.testing.assert_frame_equal
        qux_series = rec['qux']
        corge_series = rec['corge']
        del rec['qux']
        del rec['corge']

        idx = [0]
        pd.testing.assert_frame_equal(rec, self.table1_df.loc[idx])

        # second level: compare the nested columns separately
        self.assertEqual(len(qux_series), 1)
        rec_qux = qux_series[0]
        self._check_table2_first_row_qux(rec_qux)

        self.assertEqual(len(corge_series), 1)
        rec_corge = corge_series[0]
        self._check_table2_first_row_corge(rec_corge)

    def _check_table2_first_row_qux(self, rec_qux):
        # second level: cache nested df cols and remove them before calling pd.testing.assert_frame_equal
        qux_qux_series = rec_qux['qux']
        qux_corge_series = rec_qux['corge']
        del rec_qux['qux']
        del rec_qux['corge']

        qux_idx = [10]
        pd.testing.assert_frame_equal(rec_qux, self.table2_df.loc[qux_idx])

        # third level: compare the nested columns separately
        self.assertEqual(len(qux_qux_series), 1)
        pd.testing.assert_frame_equal(qux_qux_series[qux_idx[0]], self.table3_df.iloc[[0]])
        self.assertEqual(len(qux_corge_series), 1)
        pd.testing.assert_frame_equal(qux_corge_series[qux_idx[0]], self.table3_df.iloc[[0, 1]])

    def _check_table2_second_row_qux(self, rec_qux):
        # second level: cache nested df cols and remove them before calling pd.testing.assert_frame_equal
        qux_qux_series = rec_qux['qux']
        qux_corge_series = rec_qux['corge']
        del rec_qux['qux']
        del rec_qux['corge']

        qux_idx = [11]
        pd.testing.assert_frame_equal(rec_qux, self.table2_df.loc[qux_idx])

        # third level: compare the nested columns separately
        self.assertEqual(len(qux_qux_series), 1)
        pd.testing.assert_frame_equal(qux_qux_series[qux_idx[0]], self.table3_df.iloc[[1]])
        self.assertEqual(len(qux_corge_series), 1)
        pd.testing.assert_frame_equal(qux_corge_series[qux_idx[0]], self.table3_df.iloc[[2]])

    def _check_table2_first_row_corge(self, rec_corge):
        # second level: cache nested df cols and remove them before calling pd.testing.assert_frame_equal
        corge_qux_series = rec_corge['qux']
        corge_corge_series = rec_corge['corge']
        del rec_corge['qux']
        del rec_corge['corge']

        corge_idx = [10, 11]
        pd.testing.assert_frame_equal(rec_corge, self.table2_df.loc[corge_idx])

        # third level: compare the nested columns separately
        self.assertEqual(len(corge_qux_series), 2)
        pd.testing.assert_frame_equal(corge_qux_series[corge_idx[0]], self.table3_df.iloc[[0]])
        pd.testing.assert_frame_equal(corge_qux_series[corge_idx[1]], self.table3_df.iloc[[1]])
        self.assertEqual(len(corge_corge_series), 2)
        pd.testing.assert_frame_equal(corge_corge_series[corge_idx[0]], self.table3_df.iloc[[0, 1]])
        pd.testing.assert_frame_equal(corge_corge_series[corge_idx[1]], self.table3_df.iloc[[2]])

    def _check_table2_second_row_corge(self, rec_corge):
        # second level: cache nested df cols and remove them before calling pd.testing.assert_frame_equal
        corge_qux_series = rec_corge['qux']
        corge_corge_series = rec_corge['corge']
        del rec_corge['qux']
        del rec_corge['corge']

        corge_idx = [12]
        pd.testing.assert_frame_equal(rec_corge, self.table2_df.loc[corge_idx])

        # third level: compare the nested columns separately
        self.assertEqual(len(corge_qux_series), 1)
        pd.testing.assert_frame_equal(corge_qux_series[corge_idx[0]], self.table3_df.iloc[[0]])
        self.assertEqual(len(corge_corge_series), 1)
        pd.testing.assert_frame_equal(corge_corge_series[corge_idx[0]], self.table3_df.iloc[[0, 1, 2]])

    def _check_two_rows_no_df(self, rec):
        self.assertEqual(rec[0], [0, 1])
        np.testing.assert_array_equal(rec[1], self._wrap_check([0.0, 1.0]))
        expected = [self._wrap_check(['r11', 'r12']), self._wrap_check(['r21'])]
        self._assertNestedRaggedArrayEqual(rec[2], expected)
        np.testing.assert_array_equal(rec[3], self._wrap_check([[10.0, 11.0, 12.0], [20.0, 21.0, 22.0]]))
        np.testing.assert_array_equal(rec[4], self._wrap_check([0, 1]))
        expected = [self._wrap_check([0, 1]), self._wrap_check([2])]
        for i, j in zip(rec[5], expected):
            np.testing.assert_array_equal(i, j)

    def _check_one_row_no_df(self, rec):
        self.assertEqual(rec[0], 0)
        self.assertEqual(rec[1], 0.0)
        np.testing.assert_array_equal(rec[2], self._wrap_check(['r11', 'r12']))
        np.testing.assert_array_equal(rec[3], self._wrap_check([10.0, 11.0, 12.0]))
        self.assertEqual(rec[4], 0)
        np.testing.assert_array_equal(rec[5], self._wrap_check([0, 1]))
        np.testing.assert_array_equal(rec[6], self._wrap_check([(1.0, 11), (2.0, 12)]))

    def _check_one_row_multiselect_no_df(self, rec):
        # difference from _check_one_row_no_df is that everything is wrapped in a list
        self.assertEqual(rec[0], [0])
        self.assertEqual(rec[1], [0.0])
        np.testing.assert_array_equal(rec[2], [self._wrap_check(['r11', 'r12'])])
        np.testing.assert_array_equal(rec[3], [self._wrap_check([10.0, 11.0, 12.0])])
        self.assertEqual(rec[4], [0])
        np.testing.assert_array_equal(rec[5], [self._wrap_check([0, 1])])
        np.testing.assert_array_equal(rec[6], [self._wrap_check([(1.0, 11), (2.0, 12)])])

    def _assertNestedRaggedArrayEqual(self, arr1, arr2):
        """
        This is a helper function for _check_two_rows_no_df.
        It compares arrays or lists containing numpy arrays that may be ragged
        """
        self.assertEqual(type(arr1), type(arr2))
        self.assertEqual(len(arr1), len(arr2))
        if isinstance(arr1, np.ndarray):
            if arr1.dtype == object:  # both are arrays containing arrays, lists, or h5py.Dataset strings
                for i, j in zip(arr1, arr2):
                    self._assertNestedRaggedArrayEqual(i, j)
            elif np.issubdtype(arr1.dtype, np.number):
                np.testing.assert_allclose(arr1, arr2)
            else:
                np.testing.assert_array_equal(arr1, arr2)
        elif isinstance(arr1, list):
            for i, j in zip(arr1, arr2):
                self._assertNestedRaggedArrayEqual(i, j)
        else:  # scalar
            self.assertEqual(arr1, arr2)

    def test_single_item(self):
        rec = self.table1[0]
        self._check_one_row_df(rec)

    def test_single_item_nested(self):
        rec = self.table1.get(0, index=False)
        self._check_one_row_df_nested(rec)

    def test_single_item_no_df(self):
        rec = self.table1.get(0, df=False)
        self._check_one_row_no_df(rec)

    def test_slice(self):
        rec = self.table1[0:2]
        self._check_two_rows_df(rec)

    def test_slice_nested(self):
        rec = self.table1.get(slice(0, 2), index=False)
        self._check_two_rows_df_nested(rec)

    def test_slice_no_df(self):
        rec = self.table1.get(slice(0, 2), df=False)
        self._check_two_rows_no_df(rec)

    def test_slice_single(self):
        rec = self.table1[0:1]
        self._check_one_row_df(rec)

    def test_slice_single_nested(self):
        rec = self.table1.get(slice(0, 1), index=False)
        self._check_one_row_df_nested(rec)

    def test_slice_single_no_df(self):
        rec = self.table1.get(slice(0, 1), df=False)
        self._check_one_row_multiselect_no_df(rec)

    def test_list(self):
        rec = self.table1[[0, 1]]
        self._check_two_rows_df(rec)

    def test_list_nested(self):
        rec = self.table1.get([0, 1], index=False)
        self._check_two_rows_df_nested(rec)

    def test_list_no_df(self):
        rec = self.table1.get([0, 1], df=False)
        self._check_two_rows_no_df(rec)

    def test_list_single(self):
        rec = self.table1[[0]]
        self._check_one_row_df(rec)

    def test_list_single_nested(self):
        rec = self.table1.get([0], index=False)
        self._check_one_row_df_nested(rec)

    def test_list_single_no_df(self):
        rec = self.table1.get([0], df=False)
        self._check_one_row_multiselect_no_df(rec)

    def test_array(self):
        rec = self.table1[np.array([0, 1])]
        self._check_two_rows_df(rec)

    def test_array_nested(self):
        rec = self.table1.get(np.array([0, 1]), index=False)
        self._check_two_rows_df_nested(rec)

    def test_array_no_df(self):
        rec = self.table1.get(np.array([0, 1]), df=False)
        self._check_two_rows_no_df(rec)

    def test_array_single(self):
        rec = self.table1[np.array([0])]
        self._check_one_row_df(rec)

    def test_array_single_nested(self):
        rec = self.table1.get(np.array([0]), index=False)
        self._check_one_row_df_nested(rec)

    def test_array_single_no_df(self):
        rec = self.table1.get(np.array([0]), df=False)
        self._check_one_row_multiselect_no_df(rec)

    def test_to_dataframe_nested(self):
        rec = self.table1.to_dataframe()
        self._check_two_rows_df_nested(rec)

    def test_to_dataframe(self):
        rec = self.table1.to_dataframe(index=True)
        self._check_two_rows_df(rec)


class TestSelectionArray(SelectionTestMixin, TestCase):

    def _wrap(self, my_list):
        return np.array(my_list)

    def _wrap_check(self, my_list):
        return self._wrap(my_list)


class TestSelectionList(SelectionTestMixin, TestCase):

    def _wrap(self, my_list):
        return my_list

    def _wrap_check(self, my_list):
        return self._wrap(my_list)


class TestSelectionH5Dataset(SelectionTestMixin, TestCase):

    def setUp(self):
        self.path = get_temp_filepath()
        self.file = h5py.File(self.path, 'w')
        self.dset_counter = 0
        super().setUp()

    def tearDown(self):
        super().tearDown()
        self.file.close()
        if os.path.exists(self.path):
            os.remove(self.path)

    def _wrap(self, my_list):
        self.dset_counter = self.dset_counter + 1
        kwargs = dict()
        if isinstance(my_list[0], str):
            kwargs['dtype'] = H5_TEXT
        elif isinstance(my_list[0], tuple):  # compound dtype
            # normally for cpd dtype, __resolve_dtype__ takes a list of DtypeSpec objects
            cpd_type = [dict(name='cpd_float', dtype=np.dtype('float64')),
                        dict(name='cpd_int', dtype=np.dtype('int32'))]
            kwargs['dtype'] = HDF5IO.__resolve_dtype__(cpd_type, my_list[0])
        dset = self.file.create_dataset('dset%d' % self.dset_counter, data=np.array(my_list, **kwargs))
        if H5PY_3 and isinstance(my_list[0], str):
            return StrDataset(dset, None)  # return a wrapper to read data as str instead of bytes
        else:
            # NOTE: h5py.Dataset with compound dtype are read as numpy arrays with compound dtype, not tuples
            return dset

    def _wrap_check(self, my_list):
        # getitem on h5dataset backed data will return np.array
        kwargs = dict()
        if isinstance(my_list[0], str):
            kwargs['dtype'] = H5_TEXT
        elif isinstance(my_list[0], tuple):
            cpd_type = [dict(name='cpd_float', dtype=np.dtype('float64')),
                        dict(name='cpd_int', dtype=np.dtype('int32'))]
            kwargs['dtype'] = np.dtype([(x['name'], x['dtype']) for x in cpd_type])
            # compound dtypes with str are read as bytes, see https://github.com/h5py/h5py/issues/1751
        return np.array(my_list, **kwargs)


class TestVectorIndex(TestCase):

    def test_init_empty(self):
        foo = VectorData(name='foo', description='foo column')
        foo_ind = VectorIndex(name='foo_index', target=foo, data=list())
        self.assertEqual(foo_ind.name, 'foo_index')
        self.assertEqual(foo_ind.description, "Index for VectorData 'foo'")
        self.assertIs(foo_ind.target, foo)
        self.assertListEqual(foo_ind.data, list())

    def test_init_data(self):
        foo = VectorData(name='foo', description='foo column', data=['a', 'b', 'c'])
        foo_ind = VectorIndex(name='foo_index', target=foo, data=[2, 3])
        self.assertListEqual(foo_ind.data, [2, 3])
        self.assertListEqual(foo_ind[0], ['a', 'b'])
        self.assertListEqual(foo_ind[1], ['c'])


class TestDoubleIndex(TestCase):

    def test_index(self):
        # row 1 has three entries
        # the first entry has two sub-entries
        # the first sub-entry has two values, the second sub-entry has one value
        # the second entry has one sub-entry, which has one value
        foo = VectorData(name='foo', description='foo column', data=['a11', 'a12', 'a21', 'b11'])
        foo_ind = VectorIndex(name='foo_index', target=foo, data=[2, 3, 4])
        foo_ind_ind = VectorIndex(name='foo_index_index', target=foo_ind, data=[2, 3])

        self.assertListEqual(foo_ind[0], ['a11', 'a12'])
        self.assertListEqual(foo_ind[1], ['a21'])
        self.assertListEqual(foo_ind[2], ['b11'])
        self.assertListEqual(foo_ind_ind[0], [['a11', 'a12'], ['a21']])
        self.assertListEqual(foo_ind_ind[1], [['b11']])

    def test_add_vector(self):
        # row 1 has three entries
        # the first entry has two sub-entries
        # the first sub-entry has two values, the second sub-entry has one value
        # the second entry has one sub-entry, which has one value
        foo = VectorData(name='foo', description='foo column', data=['a11', 'a12', 'a21', 'b11'])
        foo_ind = VectorIndex(name='foo_index', target=foo, data=[2, 3, 4])
        foo_ind_ind = VectorIndex(name='foo_index_index', target=foo_ind, data=[2, 3])

        foo_ind_ind.add_vector([['c11', 'c12', 'c13'], ['c21', 'c22']])

        self.assertListEqual(foo.data, ['a11', 'a12', 'a21', 'b11', 'c11', 'c12', 'c13', 'c21', 'c22'])
        self.assertListEqual(foo_ind.data, [2, 3, 4, 7, 9])
        self.assertListEqual(foo_ind[3], ['c11', 'c12', 'c13'])
        self.assertListEqual(foo_ind[4], ['c21', 'c22'])
        self.assertListEqual(foo_ind_ind.data, [2, 3, 5])
        self.assertListEqual(foo_ind_ind[2], [['c11', 'c12', 'c13'], ['c21', 'c22']])


class TestDTDoubleIndex(TestCase):

    def test_double_index(self):
        foo = VectorData(name='foo', description='foo column', data=['a11', 'a12', 'a21', 'b11'])
        foo_ind = VectorIndex(name='foo_index', target=foo, data=[2, 3, 4])
        foo_ind_ind = VectorIndex(name='foo_index_index', target=foo_ind, data=[2, 3])

        table = DynamicTable(name='table0', description='an example table', columns=[foo, foo_ind, foo_ind_ind])

        self.assertIs(table['foo'], foo_ind_ind)
        self.assertIs(table.foo, foo)
        self.assertListEqual(table['foo'][0], [['a11', 'a12'], ['a21']])
        self.assertListEqual(table[0, 'foo'], [['a11', 'a12'], ['a21']])
        self.assertListEqual(table[1, 'foo'], [['b11']])

    def test_double_index_reverse(self):
        foo = VectorData(name='foo', description='foo column', data=['a11', 'a12', 'a21', 'b11'])
        foo_ind = VectorIndex(name='foo_index', target=foo, data=[2, 3, 4])
        foo_ind_ind = VectorIndex(name='foo_index_index', target=foo_ind, data=[2, 3])

        table = DynamicTable(name='table0', description='an example table', columns=[foo_ind_ind, foo_ind, foo])

        self.assertIs(table['foo'], foo_ind_ind)
        self.assertIs(table.foo, foo)
        self.assertListEqual(table['foo'][0], [['a11', 'a12'], ['a21']])
        self.assertListEqual(table[0, 'foo'], [['a11', 'a12'], ['a21']])
        self.assertListEqual(table[1, 'foo'], [['b11']])

    def test_double_index_colnames(self):
        foo = VectorData(name='foo', description='foo column', data=['a11', 'a12', 'a21', 'b11'])
        foo_ind = VectorIndex(name='foo_index', target=foo, data=[2, 3, 4])
        foo_ind_ind = VectorIndex(name='foo_index_index', target=foo_ind, data=[2, 3])
        bar = VectorData(name='bar', description='bar column', data=[1, 2])

        table = DynamicTable(name='table0', description='an example table', columns=[foo, foo_ind, foo_ind_ind, bar],
                             colnames=['foo', 'bar'])

        self.assertTupleEqual(table.columns, (foo_ind_ind, foo_ind, foo, bar))

    def test_double_index_reverse_colnames(self):
        foo = VectorData(name='foo', description='foo column', data=['a11', 'a12', 'a21', 'b11'])
        foo_ind = VectorIndex(name='foo_index', target=foo, data=[2, 3, 4])
        foo_ind_ind = VectorIndex(name='foo_index_index', target=foo_ind, data=[2, 3])
        bar = VectorData(name='bar', description='bar column', data=[1, 2])

        table = DynamicTable(name='table0', description='an example table', columns=[foo_ind_ind, foo_ind, foo, bar],
                             colnames=['bar', 'foo'])

        self.assertTupleEqual(table.columns, (bar, foo_ind_ind, foo_ind, foo))


class TestDTDoubleIndexSkipMiddle(TestCase):

    def test_index(self):
        foo = VectorData(name='foo', description='foo column', data=['a11', 'a12', 'a21', 'b11'])
        foo_ind = VectorIndex(name='foo_index', target=foo, data=[2, 3, 4])
        foo_ind_ind = VectorIndex(name='foo_index_index', target=foo_ind, data=[2, 3])

        msg = "Found VectorIndex 'foo_index_index' but not its target 'foo_index'"
        with self.assertRaisesWith(ValueError, msg):
            DynamicTable(name='table0', description='an example table', columns=[foo_ind_ind, foo])


class TestDynamicTableAddIndexRoundTrip(H5RoundTripMixin, TestCase):

    def setUpContainer(self):
        table = DynamicTable(name='table0', description='an example table')
        table.add_column('foo', 'an int column', index=True)
        table.add_row(foo=[1, 2, 3])
        return table


class TestDynamicTableAddEnumRoundTrip(H5RoundTripMixin, TestCase):

    def setUpContainer(self):
        table = DynamicTable(name='table0', description='an example table')
        table.add_column('bar', 'an enumerable column', enum=True)
        table.add_row(bar='a')
        table.add_row(bar='b')
        table.add_row(bar='a')
        table.add_row(bar='c')
        return table


class TestDynamicTableAddEnum(TestCase):

    def test_enum(self):
        table = DynamicTable(name='table0', description='an example table')
        table.add_column('bar', 'an enumerable column', enum=True)
        table.add_row(bar='a')
        table.add_row(bar='b')
        table.add_row(bar='a')
        table.add_row(bar='c')
        rec = table.to_dataframe()
        exp = pd.DataFrame(data={'bar': ['a', 'b', 'a', 'c']}, index=pd.Series(name='id', data=[0, 1, 2, 3]))
        pd.testing.assert_frame_equal(exp, rec)

    def test_enum_index(self):
        table = DynamicTable(name='table0', description='an example table')
        table.add_column('bar', 'an indexed enumerable column', enum=True, index=True)
        table.add_row(bar=['a', 'a', 'a'])
        table.add_row(bar=['b', 'b', 'b', 'b'])
        table.add_row(bar=['c', 'c'])
        rec = table.to_dataframe()
        exp = pd.DataFrame(data={'bar': [['a', 'a', 'a'],
                                         ['b', 'b', 'b', 'b'],
                                         ['c', 'c']]},
                           index=pd.Series(name='id', data=[0, 1, 2]))
        pd.testing.assert_frame_equal(exp, rec)


class TestDynamicTableInitIndexRoundTrip(H5RoundTripMixin, TestCase):

    def setUpContainer(self):
        foo = VectorData(name='foo', description='foo column', data=['a', 'b', 'c'])
        foo_ind = VectorIndex(name='foo_index', target=foo, data=[2, 3])

        # NOTE: on construct, columns are ordered such that indices go before data, so create the table that way
        # for proper comparison of the columns list
        table = DynamicTable(name='table0', description='an example table', columns=[foo_ind, foo])
        return table


class TestDoubleIndexRoundtrip(H5RoundTripMixin, TestCase):

    def setUpContainer(self):
        foo = VectorData(name='foo', description='foo column', data=['a11', 'a12', 'a21', 'b11'])
        foo_ind = VectorIndex(name='foo_index', target=foo, data=[2, 3, 4])
        foo_ind_ind = VectorIndex(name='foo_index_index', target=foo_ind, data=[2, 3])

        # NOTE: on construct, columns are ordered such that indices go before data, so create the table that way
        # for proper comparison of the columns list
        table = DynamicTable(name='table0', description='an example table', columns=[foo_ind_ind, foo_ind, foo])
        return table


class TestDataIOColumns(H5RoundTripMixin, TestCase):
    def setUpContainer(self):
        self.chunked_data = H5DataIO(
            data=[i for i in range(10)],
            chunks=(3,),
            fillvalue=-1,
        )
        self.compressed_data = H5DataIO(
            data=np.arange(10),
            compression=1,
            shuffle=True,
            fletcher32=True,
            allow_plugin_filters=True,
        )
        foo = VectorData(name='foo', description='chunked column', data=self.chunked_data)
        bar = VectorData(name='bar', description='chunked column', data=self.compressed_data)

        # NOTE: on construct, columns are ordered such that indices go before data, so create the table that way
        # for proper comparison of the columns list
        table = DynamicTable(name='table0', description='an example table', columns=[foo, bar])
        table.add_row(foo=1, bar=1)
        return table

    def test_roundtrip(self):
        super().test_roundtrip()

        with h5py.File(self.filename, 'r') as f:
            chunked_dset = f['foo']
            self.assertTrue(np.all(chunked_dset[:] == self.chunked_data.data))
            self.assertEqual(chunked_dset.chunks, (3,))
            self.assertEqual(chunked_dset.fillvalue, -1)

            compressed_dset = f['bar']
            self.assertTrue(np.all(compressed_dset[:] == self.compressed_data.data))
            self.assertEqual(compressed_dset.compression, 'gzip')
            self.assertEqual(compressed_dset.shuffle, True)
            self.assertEqual(compressed_dset.fletcher32, True)


class TestDataIOIndexedColumns(H5RoundTripMixin, TestCase):

    def setUpContainer(self):
        self.chunked_data = H5DataIO(
            data=np.arange(30).reshape(5, 2, 3),
            chunks=(1, 1, 3),
            fillvalue=-1,
        )
        self.compressed_data = H5DataIO(
            data=np.arange(30).reshape(5, 2, 3),
            compression=1,
            shuffle=True,
            fletcher32=True,
            allow_plugin_filters=True,
        )
        foo = VectorData(name='foo', description='chunked column', data=self.chunked_data)
        foo_ind = VectorIndex(name='foo_index', target=foo, data=[2, 3, 4])
        bar = VectorData(name='bar', description='chunked column', data=self.compressed_data)
        bar_ind = VectorIndex(name='bar_index', target=bar, data=[2, 3, 4])

        # NOTE: on construct, columns are ordered such that indices go before data, so create the table that way
        # for proper comparison of the columns list
        table = DynamicTable(name='table0', description='an example table', columns=[foo_ind, foo, bar_ind, bar])

        # check for add_row
        table.add_row(foo=np.arange(30).reshape(5, 2, 3), bar=np.arange(30).reshape(5, 2, 3))

        return table

    def test_roundtrip(self):
        super().test_roundtrip()

        with h5py.File(self.filename, 'r') as f:
            chunked_dset = f['foo']
            self.assertTrue(np.all(chunked_dset[:] == self.chunked_data.data))
            self.assertEqual(chunked_dset.chunks, (1, 1, 3))
            self.assertEqual(chunked_dset.fillvalue, -1)

            compressed_dset = f['bar']
            self.assertTrue(np.all(compressed_dset[:] == self.compressed_data.data))
            self.assertEqual(compressed_dset.compression, 'gzip')
            self.assertEqual(compressed_dset.shuffle, True)
            self.assertEqual(compressed_dset.fletcher32, True)


class TestDataIOIndex(H5RoundTripMixin, TestCase):

    def setUpContainer(self):
        self.chunked_data = H5DataIO(
            data=np.arange(30).reshape(5, 2, 3),
            chunks=(1, 1, 3),
            fillvalue=-1,
            maxshape=(None, 2, 3)
        )
        self.chunked_index_data = H5DataIO(
            data=np.array([2, 3, 5], dtype=np.uint),
            chunks=(2, ),
            fillvalue=np.uint(10),
            maxshape=(None,)
        )
        self.compressed_data = H5DataIO(
            data=np.arange(30).reshape(5, 2, 3),
            compression=1,
            shuffle=True,
            fletcher32=True,
            allow_plugin_filters=True,
            maxshape=(None, 2, 3)
        )
        self.compressed_index_data = H5DataIO(
            data=np.array([2, 4, 5], dtype=np.uint),
            compression=1,
            shuffle=True,
            fletcher32=False,
            allow_plugin_filters=True,
            maxshape=(None,)
        )
        foo = VectorData(name='foo', description='chunked column', data=self.chunked_data)
        foo_ind = VectorIndex(name='foo_index', target=foo, data=self.chunked_index_data)
        bar = VectorData(name='bar', description='chunked column', data=self.compressed_data)
        bar_ind = VectorIndex(name='bar_index', target=bar, data=self.compressed_index_data)

        # NOTE: on construct, columns are ordered such that indices go before data, so create the table that way
        # for proper comparison of the columns list
        table = DynamicTable(name='table0', description='an example table', columns=[foo_ind, foo, bar_ind, bar],
                             id=H5DataIO(data=[0, 1, 2], chunks=True, maxshape=(None,)))

        # check for add_row
        table.add_row(foo=np.arange(30).reshape(5, 2, 3),
                      bar=np.arange(30).reshape(5, 2, 3))

        return table

    def test_append(self, cache_spec=False):
        """Write the container to an HDF5 file, read the container from the file, and append to it."""
        with HDF5IO(self.filename, manager=get_manager(), mode='w') as write_io:
            write_io.write(self.container, cache_spec=cache_spec)

        self.reader = HDF5IO(self.filename, manager=get_manager(), mode='a')
        read_table = self.reader.read()

        data = np.arange(30, 60).reshape(5, 2, 3)
        read_table.add_row(foo=data, bar=data)

        np.testing.assert_array_equal(read_table['foo'][-1], data)


class TestDTRReferences(TestCase):

    def setUp(self):
        self.filename = 'test_dtr_references.h5'

    def tearDown(self):
        remove_test_file(self.filename)

    def test_dtr_references(self):
        """Test roundtrip of a table with a ragged DTR to another table containing a column of references."""
        group1 = Container('group1')
        group2 = Container('group2')

        table1 = DynamicTable(
            name='table1',
            description='test table 1'
        )
        table1.add_column(
            name='x',
            description='test column of ints'
        )
        table1.add_column(
            name='y',
            description='test column of reference'
        )
        table1.add_row(id=101, x=1, y=group1)
        table1.add_row(id=102, x=2, y=group1)
        table1.add_row(id=103, x=3, y=group2)

        table2 = DynamicTable(
            name='table2',
            description='test table 2'
        )

        # create a ragged column that references table1
        # each row of table2 corresponds to one or more rows of table 1
        table2.add_column(
            name='electrodes',
            description='column description',
            index=True,
            table=table1
        )

        table2.add_row(id=10, electrodes=[1, 2])

        multi_container = SimpleMultiContainer(name='multi')
        multi_container.add_container(group1)
        multi_container.add_container(group2)
        multi_container.add_container(table1)
        multi_container.add_container(table2)

        with HDF5IO(self.filename, manager=get_manager(), mode='w') as io:
            io.write(multi_container)

        with HDF5IO(self.filename, manager=get_manager(), mode='r') as io:
            read_multi_container = io.read()
            self.assertContainerEqual(read_multi_container, multi_container, ignore_name=True)

            # test DTR access
            read_group1 = read_multi_container['group1']
            read_group2 = read_multi_container['group2']
            read_table = read_multi_container['table2']
            ret = read_table[0, 'electrodes']
            expected = pd.DataFrame({'x': np.array([2, 3]),
                                     'y': [read_group1, read_group2]},
                                    index=pd.Index(data=[102, 103], name='id'))
            pd.testing.assert_frame_equal(ret, expected)


class TestVectorIndexDtype(TestCase):

    def set_up_array_index(self):
        data = VectorData(name='data', description='desc')
        index = VectorIndex(name='index', data=np.array([]), target=data)
        return index

    def set_up_list_index(self):
        data = VectorData(name='data', description='desc')
        index = VectorIndex(name='index', data=[], target=data)
        return index

    def test_array_inc_precision(self):
        index = self.set_up_array_index()
        index.add_vector(np.empty((255, )))
        self.assertEqual(index.data[0], 255)
        self.assertEqual(index.data.dtype, np.uint8)

    def test_array_inc_precision_1step(self):
        index = self.set_up_array_index()
        index.add_vector(np.empty((65535, )))
        self.assertEqual(index.data[0], 65535)
        self.assertEqual(index.data.dtype, np.uint16)

    def test_array_inc_precision_2steps(self):
        index = self.set_up_array_index()
        index.add_vector(np.empty((65536, )))
        self.assertEqual(index.data[0], 65536)
        self.assertEqual(index.data.dtype, np.uint32)

    def test_array_prev_data_inc_precision_2steps(self):
        index = self.set_up_array_index()
        index.add_vector(np.empty((255, )))  # dtype is still uint8
        index.add_vector(np.empty((65536, )))
        self.assertEqual(index.data[0], 255)  # make sure the 255 is upgraded
        self.assertEqual(index.data.dtype, np.uint32)

    def test_list_inc_precision(self):
        index = self.set_up_list_index()
        index.add_vector(list(range(255)))
        self.assertEqual(index.data[0], 255)
        self.assertEqual(type(index.data[0]), np.uint8)

    def test_list_inc_precision_1step(self):
        index = self.set_up_list_index()
        index.add_vector(list(range(65535)))
        self.assertEqual(index.data[0], 65535)
        self.assertEqual(type(index.data[0]), np.uint16)

    def test_list_inc_precision_2steps(self):
        index = self.set_up_list_index()
        index.add_vector(list(range(65536)))
        self.assertEqual(index.data[0], 65536)
        self.assertEqual(type(index.data[0]), np.uint32)

    def test_list_prev_data_inc_precision_2steps(self):
        index = self.set_up_list_index()
        index.add_vector(list(range(255)))
        index.add_vector(list(range(65536 - 255)))
        self.assertEqual(index.data[0], 255)  # make sure the 255 is upgraded
        self.assertEqual(type(index.data[0]), np.uint32)


class TestDynamicTableSubclassColumns(TestCase):
    def setUp(self):
        self.foo1 = FooExtendDynamicTable0()
        self.foo2 = FooExtendDynamicTable1()
        self.foo3 = FooExtendDynamicTable2()

    def test_columns(self):
        self.assertEqual(self.foo1.__columns__,
                        ({'name': 'col1', 'description': '...'}, {'name': 'col2', 'description': '...'}))
        self.assertEqual(self.foo2.__columns__,
                        ({'name': 'col1', 'description': '...'}, {'name': 'col2', 'description': '...'},
                         {'name': 'col3', 'description': '...'}, {'name': 'col4', 'description': '...'})
)
        self.assertEqual(self.foo2.__columns__, self.foo3.__columns__)