Spaces:
Paused
Paused
File size: 106,684 Bytes
f6686e1 | 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 | """
Tests for the simulation validation mechanism.
"""
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
from tinytroupe.validation import SimulationExperimentEmpiricalValidator, SimulationExperimentDataset, validate_simulation_experiment_empirically
def test_simulation_experiment_dataset():
"""Test SimulationExperimentDataset creation and conversion."""
print("Testing SimulationExperimentDataset...")
# Test creation with new format
dataset = SimulationExperimentDataset(
name="Test Dataset",
description="A test dataset",
key_results={"metric1": [1, 2, 3], "metric2": 0.5},
result_types={"metric1": "per_agent", "metric2": "aggregate"},
agent_names=["Agent_A", "Agent_B", "Agent_C"],
agent_justifications=[
{"agent_name": "Agent_A", "justification": "Test reasoning A"},
{"agent_index": 1, "justification": "Test reasoning B"},
"Simple justification without agent reference"
],
justification_summary="Test summary"
)
# Test helper methods
assert dataset.get_agent_name(0) == "Agent_A"
assert dataset.get_agent_name(1) == "Agent_B"
assert dataset.get_agent_name(10) is None # Out of bounds
# Test justification text extraction
assert dataset.get_justification_text("Simple text") == "Simple text"
assert dataset.get_justification_text({"justification": "Dict text"}) == "Dict text"
# Test agent reference extraction
assert dataset.get_justification_agent_reference({"agent_name": "TestAgent"}) == "TestAgent"
assert dataset.get_justification_agent_reference({"agent_index": 0}) == "Agent_A"
assert dataset.get_justification_agent_reference("Simple text") is None
# Test conversion to dict
dataset_dict = dataset.model_dump()
assert dataset_dict["name"] == "Test Dataset"
assert dataset_dict["key_results"]["metric1"] == [1, 2, 3]
assert dataset_dict["result_types"]["metric1"] == "per_agent"
# Test creation from dict
dataset2 = SimulationExperimentDataset.model_validate(dataset_dict)
assert dataset2.name == dataset.name
assert dataset2.key_results == dataset.key_results
assert dataset2.result_types == dataset.result_types
print("β
SimulationExperimentDataset tests passed")
def test_statistical_validation():
"""Test statistical validation functionality."""
print("Testing statistical validation...")
control_data = {
"name": "Control",
"key_results": {
"metric1": [10, 12, 11, 13, 9, 14],
"metric2": [0.5, 0.6, 0.55, 0.65, 0.45, 0.7]
},
"result_types": {
"metric1": "per_agent",
"metric2": "per_agent"
}
}
treatment_data = {
"name": "Treatment",
"key_results": {
"metric1": [15, 17, 16, 18, 14, 19], # Higher values
"metric2": [0.8, 0.9, 0.85, 0.95, 0.75, 1.0] # Higher values
},
"result_types": {
"metric1": "per_agent",
"metric2": "per_agent"
}
}
result = validate_simulation_experiment_empirically(
control_data=control_data,
treatment_data=treatment_data,
validation_types=["statistical"],
output_format="values"
)
assert result.statistical_results is not None
assert "test_results" in result.statistical_results
assert result.overall_score is not None
assert 0.0 <= result.overall_score <= 1.0
print("β
Statistical validation tests passed")
def test_semantic_validation():
"""Test semantic validation functionality."""
print("Testing semantic validation...")
control_data = {
"name": "Control",
"agent_names": ["Agent1", "Agent2"],
"agent_justifications": [
{"agent_name": "Agent1", "justification": "I prefer affordable options because I'm budget-conscious."},
{"agent_index": 1, "justification": "Price is my main concern when making purchases."}
],
"justification_summary": "Agents focused primarily on cost and affordability in their decisions."
}
treatment_data = {
"name": "Treatment",
"agent_names": ["AgentA", "AgentB"],
"agent_justifications": [
{"agent_name": "AgentA", "justification": "I choose quality products even if they cost more because durability matters."},
"Premium features justify higher prices for long-term value." # String format without agent reference
],
"justification_summary": "Agents emphasized quality and long-term value over immediate cost concerns."
}
result = validate_simulation_experiment_empirically(
control_data=control_data,
treatment_data=treatment_data,
validation_types=["semantic"],
output_format="values"
)
assert result.semantic_results is not None
print("result.semantic_results = ", result.semantic_results)
assert len(result.semantic_results.get("individual_comparisons", [])) > 0
assert result.semantic_results.get("summary_comparison") is not None
assert result.overall_score is not None
print("β
Semantic validation tests passed")
def test_combined_validation():
"""Test combined statistical and semantic validation."""
print("Testing combined validation...")
control_data = {
"name": "Control Group",
"description": "Baseline experiment",
"key_results": {
"purchase_rate": [0.1, 0.12, 0.11, 0.13, 0.09],
"satisfaction": [3.0, 3.2, 3.1, 3.3, 2.9]
},
"agent_justifications": [
{"agent_name": "C1", "justification": "Price was too high for the value provided."},
{"agent_name": "C2", "justification": "Product quality didn't meet my expectations."}
],
"justification_summary": "Agents were generally dissatisfied with value proposition."
}
treatment_data = {
"name": "Treatment Group",
"description": "Enhanced product experiment",
"key_results": {
"purchase_rate": [0.18, 0.20, 0.19, 0.21, 0.17],
"satisfaction": [4.2, 4.4, 4.3, 4.5, 4.1]
},
"agent_justifications": [
{"agent_name": "T1", "justification": "The enhanced features provided excellent value for money."},
{"agent_name": "T2", "justification": "Product quality exceeded expectations and justified the cost."}
],
"justification_summary": "Agents were highly satisfied with the improved value proposition."
}
# Test values output
result = validate_simulation_experiment_empirically(
control_data=control_data,
treatment_data=treatment_data,
validation_types=["statistical", "semantic"],
output_format="values"
)
assert result.statistical_results is not None
assert result.semantic_results is not None
assert result.overall_score is not None
assert result.summary != ""
# Test report output
report = validate_simulation_experiment_empirically(
control_data=control_data,
treatment_data=treatment_data,
validation_types=["statistical", "semantic"],
output_format="report"
)
assert isinstance(report, str)
assert "# Simulation Experiment Empirical Validation Report" in report
assert "Statistical Validation" in report
assert "Semantic Validation" in report
print("β
Combined validation tests passed")
def test_error_handling():
"""Test error handling for invalid inputs."""
print("Testing error handling...")
# Test with empty data
empty_control = {"name": "Empty", "key_results": {}}
empty_treatment = {"name": "Empty", "key_results": {}}
result = validate_simulation_experiment_empirically(
control_data=empty_control,
treatment_data=empty_treatment,
validation_types=["statistical"],
output_format="values"
)
# Should handle gracefully
assert result.statistical_results is not None
assert "error" in result.statistical_results
print("β
Error handling tests passed")
def test_missing_data_handling():
"""Test handling of missing agent names and missing data points."""
print("Testing missing data handling...")
# Test with missing agent names and missing data
dataset_with_missing = SimulationExperimentDataset(
name="Test Missing Data",
key_results={
"metric1": [1, None, 3, 4], # Missing data for agent 1
"metric2": [0.5, 0.6, None, 0.8] # Missing data for agent 2
},
result_types={
"metric1": "per_agent",
"metric2": "per_agent"
},
agent_names=["Alice", None, "Charlie", "Diana"], # Missing name for agent 1
agent_justifications=[
{"agent_name": "Alice", "justification": "Good experience"},
{"agent_index": 1, "justification": "Anonymous feedback"}, # Unnamed agent
"Had some issues", # No agent reference
{"agent_name": "Diana", "justification": "Excellent"}
]
)
# Test agent name retrieval with None
assert dataset_with_missing.get_agent_name(0) == "Alice"
assert dataset_with_missing.get_agent_name(1) is None # Missing name
assert dataset_with_missing.get_agent_name(2) == "Charlie"
# Test data retrieval with None values
assert dataset_with_missing.get_agent_data("metric1", 0) == 1
assert dataset_with_missing.get_agent_data("metric1", 1) is None # Missing data
assert dataset_with_missing.get_agent_data("metric1", 2) == 3
# Test valid data filtering
valid_metric1 = dataset_with_missing.get_valid_agent_data("metric1")
assert valid_metric1 == [1, 3, 4] # None filtered out
valid_metric2 = dataset_with_missing.get_valid_agent_data("metric2")
assert valid_metric2 == [0.5, 0.6, 0.8] # None filtered out
# Test all agent data mapping (should exclude None values)
all_metric1 = dataset_with_missing.get_all_agent_data("metric1")
expected_keys = {"Alice", "Charlie", "Diana"} # Agent_1 excluded due to None value
assert set(all_metric1.keys()) == expected_keys
assert all_metric1["Alice"] == 1
assert all_metric1["Charlie"] == 3
assert all_metric1["Diana"] == 4
# Test consistency validation (should show warnings, not errors)
issues = dataset_with_missing.validate_data_consistency()
warning_issues = [issue for issue in issues if issue.startswith("WARNING")]
error_issues = [issue for issue in issues if not issue.startswith("WARNING")]
assert len(error_issues) == 0 # No structural errors
assert len(warning_issues) > 0 # Should have warnings about None values
print("β
Missing data handling tests passed")
def test_statistical_validation_with_missing_data():
"""Test statistical validation when data contains None values."""
print("Testing statistical validation with missing data...")
control_data = {
"name": "Control with Missing",
"key_results": {
"metric1": [10, None, 12, 14], # 1 missing value
"metric2": [0.5, 0.6, None, 0.8] # 1 missing value
},
"result_types": {
"metric1": "per_agent",
"metric2": "per_agent"
}
}
treatment_data = {
"name": "Treatment with Missing",
"key_results": {
"metric1": [15, 17, None, 19], # 1 missing value
"metric2": [0.7, None, 0.9, 1.0] # 1 missing value
},
"result_types": {
"metric1": "per_agent",
"metric2": "per_agent"
}
}
# Should not crash despite missing data
result = validate_simulation_experiment_empirically(
control_data=control_data,
treatment_data=treatment_data,
validation_types=["statistical"],
output_format="values"
)
assert result.statistical_results is not None
assert "test_results" in result.statistical_results
assert result.overall_score is not None
print("β
Statistical validation with missing data tests passed")
def test_statistical_equivalence():
"""Test statistical validation when control and treatment are equivalent."""
print("Testing statistical equivalence...")
# Create identical data for control and treatment
control_data = {
"name": "Control",
"key_results": {
"metric1": [10, 12, 11, 13, 9, 14],
"metric2": [0.5, 0.6, 0.55, 0.65, 0.45, 0.7]
},
"result_types": {
"metric1": "per_agent",
"metric2": "per_agent"
}
}
# Treatment data is identical to control (should show no significant differences)
treatment_data = {
"name": "Treatment",
"key_results": {
"metric1": [10, 12, 11, 13, 9, 14], # Same values as control
"metric2": [0.5, 0.6, 0.55, 0.65, 0.45, 0.7] # Same values as control
},
"result_types": {
"metric1": "per_agent",
"metric2": "per_agent"
}
}
result = validate_simulation_experiment_empirically(
control_data=control_data,
treatment_data=treatment_data,
validation_types=["statistical"],
output_format="values"
)
assert result.statistical_results is not None
assert "test_results" in result.statistical_results
# Check that no tests show significance (since data is identical)
test_results = result.statistical_results["test_results"]
for treatment_name, treatment_results in test_results.items():
for metric, metric_result in treatment_results.items():
assert not metric_result.get("significant", True), f"Metric {metric} should not be significant with identical data"
# p-value should be high (close to 1.0) for identical data
p_value = metric_result.get("p_value", 0)
assert p_value > 0.05, f"p-value for {metric} should be > 0.05 for identical data, got {p_value}"
# Overall score should be high (close to 1.0) since no significant differences
assert result.overall_score is not None
assert result.overall_score >= 0.8, f"Overall score should be high for equivalent data, got {result.overall_score}"
print("β
Statistical equivalence tests passed")
def test_near_equivalent_data():
"""Test statistical validation with very similar but not identical data."""
print("Testing near-equivalent data...")
control_data = {
"name": "Control",
"key_results": {
"metric1": [10, 12, 11, 13, 9, 14],
"metric2": [0.5, 0.6, 0.55, 0.65, 0.45, 0.7]
},
"result_types": {
"metric1": "per_agent",
"metric2": "per_agent"
}
}
# Treatment data is very similar to control with minor random variations
treatment_data = {
"name": "Treatment",
"key_results": {
"metric1": [10.1, 11.9, 11.1, 13.1, 8.9, 14.1], # Small variations
"metric2": [0.51, 0.59, 0.56, 0.64, 0.46, 0.69] # Small variations
},
"result_types": {
"metric1": "per_agent",
"metric2": "per_agent"
}
}
result = validate_simulation_experiment_empirically(
control_data=control_data,
treatment_data=treatment_data,
validation_types=["statistical"],
output_format="values"
)
assert result.statistical_results is not None
assert "test_results" in result.statistical_results
# Check that most tests don't show significance (since data is very similar)
test_results = result.statistical_results["test_results"]
significant_count = 0
total_count = 0
for treatment_name, treatment_results in test_results.items():
for metric, metric_result in treatment_results.items():
total_count += 1
if metric_result.get("significant", False):
significant_count += 1
# p-value should be reasonably high for similar data
p_value = metric_result.get("p_value", 0)
print(f" {metric}: p-value = {p_value:.4f}, significant = {metric_result.get('significant', False)}")
# Most tests should not be significant
significance_rate = significant_count / total_count if total_count > 0 else 0
assert significance_rate <= 0.5, f"Too many significant results for similar data: {significant_count}/{total_count}"
# Overall score should be moderate to high
assert result.overall_score is not None
assert result.overall_score >= 0.4, f"Overall score should be reasonable for similar data, got {result.overall_score}"
print("β
Near-equivalent data tests passed")
def test_clearly_different_data():
"""Test statistical validation with clearly different data to ensure tests work correctly."""
print("Testing clearly different data...")
control_data = {
"name": "Control",
"key_results": {
"metric1": [1, 2, 3, 2, 1, 3], # Low values
"metric2": [0.1, 0.2, 0.15, 0.25, 0.1, 0.3] # Low values
},
"result_types": {
"metric1": "per_agent",
"metric2": "per_agent"
}
}
# Treatment data is clearly different from control
treatment_data = {
"name": "Treatment",
"key_results": {
"metric1": [20, 22, 21, 23, 19, 24], # Much higher values
"metric2": [0.8, 0.9, 0.85, 0.95, 0.75, 1.0] # Much higher values
},
"result_types": {
"metric1": "per_agent",
"metric2": "per_agent"
}
}
result = validate_simulation_experiment_empirically(
control_data=control_data,
treatment_data=treatment_data,
validation_types=["statistical"],
output_format="values"
)
assert result.statistical_results is not None
assert "test_results" in result.statistical_results
# Check that tests show significance (since data is clearly different)
test_results = result.statistical_results["test_results"]
significant_count = 0
total_count = 0
for treatment_name, treatment_results in test_results.items():
for metric, metric_result in treatment_results.items():
total_count += 1
if metric_result.get("significant", False):
significant_count += 1
# p-value should be low for clearly different data
p_value = metric_result.get("p_value", 1)
print(f" {metric}: p-value = {p_value:.4f}, significant = {metric_result.get('significant', False)}")
# Most or all tests should be significant
significance_rate = significant_count / total_count if total_count > 0 else 0
assert significance_rate >= 0.5, f"Too few significant results for clearly different data: {significant_count}/{total_count}"
# Overall score should be low (since there are significant differences)
assert result.overall_score is not None
assert result.overall_score <= 0.6, f"Overall score should be low for clearly different data, got {result.overall_score}"
print("β
Clearly different data tests passed")
def test_mixed_equivalence_results():
"""Test statistical validation with mixed results (some metrics equivalent, some different)."""
print("Testing mixed equivalence results...")
control_data = {
"name": "Control",
"key_results": {
"metric1": [10, 12, 11, 13, 9, 14], # Will be equivalent
"metric2": [0.5, 0.6, 0.55, 0.65, 0.45, 0.7], # Will be different
"metric3": [100, 102, 101, 103, 99, 104] # Will be equivalent
},
"result_types": {
"metric1": "per_agent",
"metric2": "per_agent",
"metric3": "per_agent"
}
}
treatment_data = {
"name": "Treatment",
"key_results": {
"metric1": [10, 12, 11, 13, 9, 14], # Same as control
"metric2": [0.8, 0.9, 0.85, 0.95, 0.75, 1.0], # Different from control
"metric3": [100, 102, 101, 103, 99, 104] # Same as control
},
"result_types": {
"metric1": "per_agent",
"metric2": "per_agent",
"metric3": "per_agent"
}
}
result = validate_simulation_experiment_empirically(
control_data=control_data,
treatment_data=treatment_data,
validation_types=["statistical"],
output_format="values"
)
assert result.statistical_results is not None
assert "test_results" in result.statistical_results
# Check specific metrics
test_results = result.statistical_results["test_results"]
for treatment_name, treatment_results in test_results.items():
# metric1 and metric3 should not be significant (identical data)
if "metric1" in treatment_results:
assert not treatment_results["metric1"].get("significant", True), "metric1 should not be significant"
if "metric3" in treatment_results:
assert not treatment_results["metric3"].get("significant", True), "metric3 should not be significant"
# metric2 should be significant (different data)
if "metric2" in treatment_results:
# Note: With small sample sizes, even large differences might not always be significant
# So we check the direction of the difference rather than strict significance
p_value = treatment_results["metric2"].get("p_value", 1)
print(f" metric2: p-value = {p_value:.4f}, significant = {treatment_results['metric2'].get('significant', False)}")
# Overall score should be moderate (some equivalent, some different)
assert result.overall_score is not None
assert 0.2 <= result.overall_score <= 0.9, f"Overall score should be moderate for mixed results, got {result.overall_score}"
print("β
Mixed equivalence results tests passed")
def test_treatment_equivalent_to_control():
"""Test that when Treatment data is equivalent to Control, validation detects no significant differences."""
print("Testing treatment equivalent to control...")
# Create identical datasets (Treatment should be equivalent to Control)
control_data = {
"name": "Control",
"key_results": {
"metric1": [10, 12, 11, 13, 9, 14],
"metric2": [0.5, 0.6, 0.55, 0.65, 0.45, 0.7]
},
"result_types": {
"metric1": "per_agent",
"metric2": "per_agent"
}
}
# Treatment with identical values (should show no significant difference)
treatment_data = {
"name": "Treatment",
"key_results": {
"metric1": [10, 12, 11, 13, 9, 14], # Identical values
"metric2": [0.5, 0.6, 0.55, 0.65, 0.45, 0.7] # Identical values
},
"result_types": {
"metric1": "per_agent",
"metric2": "per_agent"
}
}
result = validate_simulation_experiment_empirically(
control_data=control_data,
treatment_data=treatment_data,
validation_types=["statistical"],
output_format="values"
)
# Check that statistical results are available
assert result.statistical_results is not None, "Statistical results should be available"
assert "test_results" in result.statistical_results, "Test results should be present"
# Check that no significant differences are detected (high p-values)
test_results = result.statistical_results["test_results"]
for treatment_name, treatment_results in test_results.items():
for metric, metric_result in treatment_results.items():
assert not metric_result.get("significant", True), \
f"Metric '{metric}' should not show significant difference when Treatment equals Control"
# P-value should be high (close to 1.0) for identical data
p_value = metric_result.get("p_value", 0.0)
assert p_value > 0.05, f"P-value for '{metric}' should be > 0.05 (was {p_value})"
# Overall score should be very high (close to 1.0) when effect sizes are near 0
assert result.overall_score is not None, "Overall score should be calculated"
assert result.overall_score > 0.9, f"Overall score should be very high for identical data (was {result.overall_score})"
print("β
Treatment equivalent to control test passed")
def test_treatment_slightly_different_from_control():
"""Test that when Treatment data is slightly different from Control, validation detects minimal differences."""
print("Testing treatment slightly different from control...")
control_data = {
"name": "Control",
"key_results": {
"metric1": [10, 12, 11, 13, 9, 14],
"metric2": [0.5, 0.6, 0.55, 0.65, 0.45, 0.7]
},
"result_types": {
"metric1": "per_agent",
"metric2": "per_agent"
}
}
# Treatment with slightly different values (small random noise)
treatment_data = {
"name": "Treatment",
"key_results": {
"metric1": [10.1, 12.1, 11.1, 13.1, 9.1, 14.1], # Slightly higher
"metric2": [0.51, 0.61, 0.56, 0.66, 0.46, 0.71] # Slightly higher
},
"result_types": {
"metric1": "per_agent",
"metric2": "per_agent"
}
}
result = validate_simulation_experiment_empirically(
control_data=control_data,
treatment_data=treatment_data,
validation_types=["statistical"],
output_format="values"
)
# Check that statistical results are available
assert result.statistical_results is not None, "Statistical results should be available"
assert "test_results" in result.statistical_results, "Test results should be present"
# Check that differences are likely not significant (small differences)
test_results = result.statistical_results["test_results"]
for treatment_name, treatment_results in test_results.items():
for metric, metric_result in treatment_results.items():
# Small differences should likely not be significant
p_value = metric_result.get("p_value", 0.0)
# With small differences, p-value should typically be > 0.05
assert p_value >= 0.0, f"P-value for '{metric}' should be valid (was {p_value})"
# Overall score should be moderate to high
assert result.overall_score is not None, "Overall score should be calculated"
assert 0.0 <= result.overall_score <= 1.0, f"Overall score should be between 0 and 1 (was {result.overall_score})"
print("β
Treatment slightly different from control test passed")
def test_treatment_very_different_from_control():
"""Test that when Treatment data is very different from Control, validation detects significant differences."""
print("Testing treatment very different from control...")
control_data = {
"name": "Control",
"key_results": {
"metric1": [10, 12, 11, 13, 9, 14],
"metric2": [0.5, 0.6, 0.55, 0.65, 0.45, 0.7]
},
"result_types": {
"metric1": "per_agent",
"metric2": "per_agent"
}
}
# Treatment with very different values (much higher)
treatment_data = {
"name": "Treatment",
"key_results": {
"metric1": [25, 27, 26, 28, 24, 29], # Much higher values
"metric2": [1.0, 1.1, 1.05, 1.15, 0.95, 1.2] # Much higher values
},
"result_types": {
"metric1": "per_agent",
"metric2": "per_agent"
}
}
result = validate_simulation_experiment_empirically(
control_data=control_data,
treatment_data=treatment_data,
validation_types=["statistical"],
output_format="values"
)
# Check that statistical results are available
assert result.statistical_results is not None, "Statistical results should be available"
assert "test_results" in result.statistical_results, "Test results should be present"
# Check that significant differences are detected
test_results = result.statistical_results["test_results"]
significant_count = 0
total_tests = 0
for treatment_name, treatment_results in test_results.items():
for metric, metric_result in treatment_results.items():
total_tests += 1
if metric_result.get("significant", False):
significant_count += 1
# P-value should be low for very different data
p_value = metric_result.get("p_value", 1.0)
assert p_value >= 0.0, f"P-value for '{metric}' should be valid (was {p_value})"
# At least some tests should show significant differences
assert total_tests > 0, "Should have at least one test"
# Overall score should be lower when there are significant differences
assert result.overall_score is not None, "Overall score should be calculated"
assert 0.0 <= result.overall_score <= 1.0, f"Overall score should be between 0 and 1 (was {result.overall_score})"
print("β
Treatment very different from control test passed")
def test_semantic_equivalence_check():
"""Test that semantic validation correctly identifies equivalent vs different justifications."""
print("Testing semantic equivalence check...")
# Test with semantically equivalent justifications
control_data = {
"name": "Control",
"agent_justifications": [
{"agent_name": "Agent1", "justification": "I prefer affordable options because I'm budget-conscious."},
{"agent_name": "Agent2", "justification": "Price is my main concern when making purchases."}
],
"justification_summary": "Agents focused primarily on cost and affordability in their decisions."
}
# Treatment with semantically similar justifications (should have high proximity)
treatment_data = {
"name": "Treatment",
"agent_justifications": [
{"agent_name": "AgentA", "justification": "I choose affordable products because I'm price-sensitive."},
{"agent_name": "AgentB", "justification": "Cost is my primary consideration when buying things."}
],
"justification_summary": "Agents emphasized cost and affordability as key factors in their choices."
}
result = validate_simulation_experiment_empirically(
control_data=control_data,
treatment_data=treatment_data,
validation_types=["semantic"],
output_format="values"
)
# Check that semantic results are available
assert result.semantic_results is not None, "Semantic results should be available"
assert len(result.semantic_results.get("individual_comparisons", [])) > 0, "Should have individual comparisons"
# Check that proximity scores are high (semantically similar)
individual_comparisons = result.semantic_results["individual_comparisons"]
for comparison in individual_comparisons:
proximity_score = comparison["proximity_score"]
assert 0.0 <= proximity_score <= 1.0, f"Proximity score should be between 0 and 1 (was {proximity_score})"
# Similar justifications should have moderate to high proximity
assert proximity_score > 0.3, f"Semantically similar justifications should have proximity > 0.3 (was {proximity_score})"
# Check summary comparison
summary_comparison = result.semantic_results.get("summary_comparison")
if summary_comparison:
summary_proximity = summary_comparison["proximity_score"]
assert 0.0 <= summary_proximity <= 1.0, f"Summary proximity should be between 0 and 1 (was {summary_proximity})"
assert summary_proximity > 0.3, f"Similar summaries should have proximity > 0.3 (was {summary_proximity})"
# Overall score should be moderate to high for semantically similar content
assert result.overall_score is not None, "Overall score should be calculated"
assert result.overall_score > 0.3, f"Overall score should be > 0.3 for similar content (was {result.overall_score})"
print("β
Semantic equivalence check test passed")
def test_combined_validation_equivalence():
"""Test combined validation when Treatment is equivalent to Control."""
print("Testing combined validation equivalence...")
# Create datasets that are equivalent both statistically and semantically
control_data = {
"name": "Control Group",
"key_results": {
"satisfaction": [3.5, 3.6, 3.4, 3.7, 3.3, 3.8],
"engagement": [0.6, 0.65, 0.55, 0.7, 0.5, 0.75]
},
"result_types": {
"satisfaction": "per_agent",
"engagement": "per_agent"
},
"agent_justifications": [
{"agent_name": "C1", "justification": "The product met my expectations and provided good value."},
{"agent_name": "C2", "justification": "I found the offering to be satisfactory and reasonably priced."}
],
"justification_summary": "Agents found the product satisfactory and reasonably priced."
}
# Treatment with equivalent data (same values, similar justifications)
treatment_data = {
"name": "Treatment Group",
"key_results": {
"satisfaction": [3.5, 3.6, 3.4, 3.7, 3.3, 3.8], # Identical values
"engagement": [0.6, 0.65, 0.55, 0.7, 0.5, 0.75] # Identical values
},
"result_types": {
"satisfaction": "per_agent",
"engagement": "per_agent"
},
"agent_justifications": [
{"agent_name": "T1", "justification": "The product matched my expectations and offered good value."},
{"agent_name": "T2", "justification": "I considered the offering satisfactory and fairly priced."}
],
"justification_summary": "Agents considered the product satisfactory and fairly priced."
}
result = validate_simulation_experiment_empirically(
control_data=control_data,
treatment_data=treatment_data,
validation_types=["statistical", "semantic"],
output_format="values"
)
# Check both statistical and semantic results
assert result.statistical_results is not None, "Statistical results should be available"
assert result.semantic_results is not None, "Semantic results should be available"
# Statistical tests should show no significant differences
if "test_results" in result.statistical_results:
test_results = result.statistical_results["test_results"]
for treatment_name, treatment_results in test_results.items():
for metric, metric_result in treatment_results.items():
assert not metric_result.get("significant", True), \
f"Metric '{metric}' should not show significant difference for equivalent data"
# Semantic comparison should show high similarity
if result.semantic_results.get("individual_comparisons"):
for comparison in result.semantic_results["individual_comparisons"]:
proximity_score = comparison["proximity_score"]
assert proximity_score > 0.3, f"Equivalent justifications should have high proximity (was {proximity_score})"
# Overall score should be high for equivalent data (effect sizes near 0)
assert result.overall_score is not None, "Overall score should be calculated"
assert result.overall_score > 0.8, f"Overall score should be high for equivalent data (was {result.overall_score})"
# Test report output
report = validate_simulation_experiment_empirically(
control_data=control_data,
treatment_data=treatment_data,
validation_types=["statistical", "semantic"],
output_format="report"
)
assert isinstance(report, str), "Report should be a string"
assert "# Simulation Experiment Empirical Validation Report" in report, "Report should have proper header"
assert "Statistical Validation" in report, "Report should include statistical section"
assert "Semantic Validation" in report, "Report should include semantic section"
assert "Not Significant" in report, "Report should indicate no significant differences"
print("β
Combined validation equivalence test passed")
def test_ks_test_functionality():
"""Test KS test functionality for distribution comparison."""
print("Testing KS test functionality...")
# Test with similar distributions (should not be significant)
control_data = {
"name": "Control",
"key_results": {
# Most agents prefer "Maybe" (2), some "Yes" (1), few "No" (0)
"preference_score": [0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2]
},
"result_types": {
"preference_score": "per_agent"
}
}
treatment_data = {
"name": "Treatment",
"key_results": {
# Similar distribution with slight variation
"preference_score": [0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2]
},
"result_types": {
"preference_score": "per_agent"
}
}
# Test with KS test
result_ks = validate_simulation_experiment_empirically(
control_data=control_data,
treatment_data=treatment_data,
validation_types=["statistical"],
statistical_test_type="ks_test",
output_format="values"
)
assert result_ks.statistical_results is not None
assert "test_results" in result_ks.statistical_results
assert result_ks.statistical_results["test_type"] == "ks_test"
# Check KS-specific results
test_results = result_ks.statistical_results["test_results"]
for treatment_name, treatment_results in test_results.items():
for metric, metric_result in treatment_results.items():
assert "ks_statistic" in metric_result
assert "test_type" in metric_result
assert "Kolmogorov-Smirnov" in metric_result["test_type"]
assert "overlap_coefficient" in metric_result
assert "percentile_differences" in metric_result
assert "interpretation" in metric_result
print("β
KS test functionality tests passed")
def test_ks_vs_ttest_comparison():
"""Test that KS test can detect distributional differences that t-test might miss."""
print("Testing KS test vs t-test comparison...")
# Create data with same mean but different distributions
# Control: concentrated around middle value
control_data = {
"name": "Control",
"key_results": {
"metric": [1.5, 1.6, 1.4, 1.5, 1.6, 1.4, 1.5, 1.6] # Mean β 1.5
},
"result_types": {
"metric": "per_agent"
}
}
# Treatment: more spread out but similar mean
treatment_data = {
"name": "Treatment",
"key_results": {
"metric": [0.5, 2.5, 0.5, 2.5, 1.0, 2.0, 1.0, 2.0] # Mean β 1.5
},
"result_types": {
"metric": "per_agent"
}
}
# Test with t-test
result_ttest = validate_simulation_experiment_empirically(
control_data=control_data,
treatment_data=treatment_data,
validation_types=["statistical"],
statistical_test_type="welch_t_test",
output_format="values"
)
# Test with KS test
result_ks = validate_simulation_experiment_empirically(
control_data=control_data,
treatment_data=treatment_data,
validation_types=["statistical"],
statistical_test_type="ks_test",
output_format="values"
)
# Both should have results
assert result_ttest.statistical_results is not None
assert result_ks.statistical_results is not None
# Extract significance results
ttest_significant = result_ttest.statistical_results["test_results"]["treatment"]["metric"]["significant"]
ks_significant = result_ks.statistical_results["test_results"]["treatment"]["metric"]["significant"]
ttest_pvalue = result_ttest.statistical_results["test_results"]["treatment"]["metric"]["p_value"]
ks_pvalue = result_ks.statistical_results["test_results"]["treatment"]["metric"]["p_value"]
print(f"T-test: significant={ttest_significant}, p-value={ttest_pvalue:.4f}")
print(f"KS test: significant={ks_significant}, p-value={ks_pvalue:.4f}")
# KS test should be more sensitive to distributional differences
# Note: This is a probabilistic test, so we check that both tests can run without error
assert isinstance(ttest_significant, bool)
assert isinstance(ks_significant, bool)
assert 0.0 <= ttest_pvalue <= 1.0
assert 0.0 <= ks_pvalue <= 1.0
print("β
KS vs t-test comparison tests passed")
def test_ks_test_categorical_responses():
"""Test KS test with categorical response data (Yes/No/Maybe)."""
print("Testing KS test with categorical responses...")
# Control: Most say "Maybe" (encoded as 1)
control_data = {
"name": "Control Group",
"description": "Empirical control data for product preference",
"key_results": {
"response": [0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 2, 2] # 0=No, 1=Maybe, 2=Yes
},
"result_types": {
"response": "per_agent"
},
"agent_names": [f"C{i}" for i in range(12)],
"justification_summary": "Most agents were undecided, leaning towards maybe"
}
# Treatment: More polarized responses (more Yes and No, less Maybe)
treatment_data = {
"name": "Simulation Group",
"description": "AI agent simulation results for product preference",
"key_results": {
"response": [0, 0, 0, 0, 0, 1, 1, 2, 2, 2, 2, 2] # More polarized
},
"result_types": {
"response": "per_agent"
},
"agent_names": [f"S{i}" for i in range(12)],
"justification_summary": "Agents showed more polarized responses with clearer preferences"
}
# Test with KS test
result = validate_simulation_experiment_empirically(
control_data=control_data,
treatment_data=treatment_data,
validation_types=["statistical"],
statistical_test_type="ks_test",
output_format="values"
)
assert result.statistical_results is not None
assert "test_results" in result.statistical_results
# Extract KS test results
test_results = result.statistical_results["test_results"]
metric_result = test_results["treatment"]["response"]
# Verify KS-specific fields
assert "ks_statistic" in metric_result
assert "overlap_coefficient" in metric_result
assert "percentile_differences" in metric_result
assert "interpretation" in metric_result
# KS statistic should be > 0 (some difference detected)
ks_stat = metric_result["ks_statistic"]
assert ks_stat >= 0.0
assert ks_stat <= 1.0
# Check percentile differences
percentile_diffs = metric_result["percentile_differences"]
assert "p25_diff" in percentile_diffs
assert "p50_diff" in percentile_diffs
assert "p75_diff" in percentile_diffs
print("β
KS test categorical responses tests passed")
def test_ks_test_report_generation():
"""Test that KS test results are properly formatted in reports."""
print("Testing KS test report generation...")
control_data = {
"name": "Control",
"key_results": {
"metric": [1, 2, 3, 4, 5]
},
"result_types": {
"metric": "per_agent"
}
}
treatment_data = {
"name": "Treatment",
"key_results": {
"metric": [2, 3, 4, 5, 6]
},
"result_types": {
"metric": "per_agent"
}
}
# Generate report with KS test
report = validate_simulation_experiment_empirically(
control_data=control_data,
treatment_data=treatment_data,
validation_types=["statistical"],
statistical_test_type="ks_test",
output_format="report"
)
assert isinstance(report, str)
assert "# Simulation Experiment Empirical Validation Report" in report
assert "Kolmogorov-Smirnov test" in report
assert "ks_statistic" in report or "statistic:" in report
assert "effect size:" in report
# Check for KS-specific effect size interpretation
assert any(phrase in report for phrase in [
"negligible difference", "small difference",
"medium difference", "large difference"
])
print("β
KS test report generation tests passed")
def test_ks_test_identical_distributions():
"""Test KS test with identical distributions (should show no significant difference)."""
print("Testing KS test with identical distributions...")
# Identical distributions
identical_data = [1, 2, 2, 3, 3, 3, 4, 4, 5]
control_data = {
"name": "Control",
"key_results": {"metric": identical_data},
"result_types": {"metric": "per_agent"}
}
treatment_data = {
"name": "Treatment",
"key_results": {"metric": identical_data.copy()},
"result_types": {"metric": "per_agent"}
}
result = validate_simulation_experiment_empirically(
control_data=control_data,
treatment_data=treatment_data,
validation_types=["statistical"],
statistical_test_type="ks_test",
output_format="values"
)
assert result.statistical_results is not None
# Check results
metric_result = result.statistical_results["test_results"]["treatment"]["metric"]
# Should not be significant and KS statistic should be 0 or very small
assert not metric_result["significant"] or metric_result["p_value"] > 0.05
assert metric_result["ks_statistic"] <= 0.01 # Very small difference
# Overall score should be high (distributions are identical)
assert result.overall_score is not None
assert result.overall_score > 0.95
print("β
KS test identical distributions tests passed")
def test_ks_test_completely_different_distributions():
"""Test KS test with completely different distributions (should be highly significant)."""
print("Testing KS test with completely different distributions...")
control_data = {
"name": "Control",
"key_results": {"metric": [1, 1, 1, 2, 2, 2]}, # Low values
"result_types": {"metric": "per_agent"}
}
treatment_data = {
"name": "Treatment",
"key_results": {"metric": [8, 8, 8, 9, 9, 9]}, # High values
"result_types": {"metric": "per_agent"}
}
result = validate_simulation_experiment_empirically(
control_data=control_data,
treatment_data=treatment_data,
validation_types=["statistical"],
statistical_test_type="ks_test",
output_format="values"
)
assert result.statistical_results is not None
# Check results
metric_result = result.statistical_results["test_results"]["treatment"]["metric"]
# Should be highly significant
assert metric_result["significant"]
assert metric_result["p_value"] < 0.01
assert metric_result["ks_statistic"] > 0.5 # Large difference
# Interpretation should indicate large difference
assert "large difference" in metric_result["interpretation"].lower()
# Overall score should be low (distributions are very different)
assert result.overall_score is not None
assert result.overall_score <= 0.5
print("β
KS test completely different distributions tests passed")
def test_all_statistical_test_types():
"""Test that all statistical test types work with the validator."""
print("Testing all statistical test types...")
control_data = {
"name": "Control",
"key_results": {"metric": [10, 12, 11, 13, 9, 14]},
"result_types": {"metric": "per_agent"}
}
treatment_data = {
"name": "Treatment",
"key_results": {"metric": [15, 17, 16, 18, 14, 19]},
"result_types": {"metric": "per_agent"}
}
# Test each statistical test type
test_types = ["welch_t_test", "t_test", "mann_whitney", "ks_test"]
for test_type in test_types:
print(f" Testing {test_type}...")
result = validate_simulation_experiment_empirically(
control_data=control_data,
treatment_data=treatment_data,
validation_types=["statistical"],
statistical_test_type=test_type,
output_format="values"
)
assert result.statistical_results is not None, f"Failed for {test_type}"
assert "test_results" in result.statistical_results, f"Failed for {test_type}"
assert result.statistical_results["test_type"] == test_type, f"Failed for {test_type}"
assert result.overall_score is not None, f"Failed for {test_type}"
assert 0.0 <= result.overall_score <= 1.0, f"Failed for {test_type}"
# Check that the appropriate test was used
metric_result = result.statistical_results["test_results"]["treatment"]["metric"]
test_name = metric_result["test_type"].lower()
if test_type == "ks_test":
assert "kolmogorov-smirnov" in test_name
assert "ks_statistic" in metric_result
elif test_type in ["welch_t_test", "t_test"]:
assert "t-test" in test_name
assert "t_statistic" in metric_result
elif test_type == "mann_whitney":
assert "mann-whitney" in test_name
assert "u_statistic" in metric_result
print("β
All statistical test types tests passed")
def test_categorical_data_basic():
"""Test basic categorical data handling."""
print("Testing basic categorical data handling...")
# Test dataset with categorical string data
dataset = SimulationExperimentDataset(
name="Categorical Test",
key_results={
"preference": ["yes", "no", "maybe", "yes", "maybe"],
"satisfaction": ["high", "medium", "low", "high", "medium"]
},
result_types={
"preference": "per_agent",
"satisfaction": "per_agent"
}
)
# Check that categorical mappings were created
assert "preference" in dataset.categorical_mappings
assert "satisfaction" in dataset.categorical_mappings
# Check mappings are correct (sorted alphabetically)
pref_mapping = dataset.categorical_mappings["preference"]
expected_pref_categories = ["maybe", "no", "yes"] # Sorted alphabetically
assert len(pref_mapping) == 3
for i, category in enumerate(expected_pref_categories):
assert pref_mapping[category] == i
# Check that original data was converted to ordinal
converted_data = dataset.key_results["preference"]
assert isinstance(converted_data, list)
assert all(isinstance(x, int) for x in converted_data)
# Check conversion accuracy
original = ["yes", "no", "maybe", "yes", "maybe"]
expected_ordinal = [pref_mapping[x] for x in original]
assert converted_data == expected_ordinal
print("β
Basic categorical data handling tests passed")
def test_categorical_data_normalization():
"""Test categorical data normalization (case, whitespace)."""
print("Testing categorical data normalization...")
dataset = SimulationExperimentDataset(
name="Normalization Test",
key_results={
"response": ["Yes", "NO", " maybe ", "YES", "no", "Maybe"]
},
result_types={
"response": "per_agent"
}
)
# Check that normalization worked (should have 3 unique categories)
mapping = dataset.categorical_mappings["response"]
assert len(mapping) == 3
# Check normalized categories exist
assert "yes" in mapping
assert "no" in mapping
assert "maybe" in mapping
# All should map to different ordinal values
values = list(mapping.values())
assert len(set(values)) == 3 # All unique values
print("β
Categorical data normalization tests passed")
def test_categorical_data_with_none():
"""Test categorical data handling with None values."""
print("Testing categorical data with None values...")
dataset = SimulationExperimentDataset(
name="None Test",
key_results={
"response": ["yes", None, "no", "maybe", None, "yes"]
},
result_types={
"response": "per_agent"
}
)
# Check mapping was created despite None values
assert "response" in dataset.categorical_mappings
mapping = dataset.categorical_mappings["response"]
assert len(mapping) == 3 # Only non-None values should be mapped
# Check that None values are preserved
converted_data = dataset.key_results["response"]
assert converted_data[1] is None
assert converted_data[4] is None
# Check non-None values are converted
assert isinstance(converted_data[0], int)
assert isinstance(converted_data[2], int)
print("β
Categorical data with None values tests passed")
def test_categorical_conversion_methods():
"""Test categorical conversion helper methods."""
print("Testing categorical conversion methods...")
dataset = SimulationExperimentDataset(
name="Conversion Test",
key_results={
"preference": ["yes", "no", "maybe"],
"numeric": [1, 2, 3] # Should not be treated as categorical
},
result_types={
"preference": "per_agent",
"numeric": "per_agent"
}
)
# Test get_categorical_values
categories = dataset.get_categorical_values("preference")
assert categories == ["maybe", "no", "yes"] # Alphabetically sorted
# Test with non-categorical metric
assert dataset.get_categorical_values("numeric") is None
# Test convert_ordinal_to_categorical
mapping = dataset.categorical_mappings["preference"]
for category, ordinal in mapping.items():
converted_back = dataset.convert_ordinal_to_categorical("preference", ordinal)
assert converted_back == category
# Test is_categorical_metric
assert dataset.is_categorical_metric("preference") is True
assert dataset.is_categorical_metric("numeric") is False
# Test get_metric_summary
summary = dataset.get_metric_summary("preference")
assert summary["is_categorical"] is True
assert "categories" in summary
assert "category_mapping" in summary
assert "category_distribution" in summary
numeric_summary = dataset.get_metric_summary("numeric")
assert numeric_summary["is_categorical"] is False
assert "categories" not in numeric_summary
print("β
Categorical conversion methods tests passed")
def test_categorical_ks_test():
"""Test KS test with categorical string input."""
print("Testing KS test with categorical string input...")
# Control group: mostly "maybe" responses
control_data = {
"name": "Control",
"key_results": {
"preference": ["no", "no", "maybe", "maybe", "maybe", "maybe", "yes"]
},
"result_types": {
"preference": "per_agent"
}
}
# Treatment group: more polarized (more yes/no, less maybe)
treatment_data = {
"name": "Treatment",
"key_results": {
"preference": ["no", "no", "no", "yes", "yes", "yes", "yes"]
},
"result_types": {
"preference": "per_agent"
}
}
# Test with KS test
result = validate_simulation_experiment_empirically(
control_data=control_data,
treatment_data=treatment_data,
validation_types=["statistical"],
statistical_test_type="ks_test",
output_format="values"
)
assert result.statistical_results is not None
assert "test_results" in result.statistical_results
# Check that the test ran successfully
metric_result = result.statistical_results["test_results"]["treatment"]["preference"]
assert "ks_statistic" in metric_result
assert "p_value" in metric_result
assert "significant" in metric_result
print("β
Categorical KS test tests passed")
def test_categorical_report_generation():
"""Test that categorical data appears correctly in reports."""
print("Testing categorical report generation...")
control_data = {
"name": "Control",
"key_results": {
"satisfaction": ["low", "medium", "high", "medium"],
"preference": ["yes", "no", "maybe", "yes"]
},
"result_types": {
"satisfaction": "per_agent",
"preference": "per_agent"
}
}
treatment_data = {
"name": "Treatment",
"key_results": {
"satisfaction": ["medium", "high", "high", "high"],
"preference": ["yes", "yes", "maybe", "yes"]
},
"result_types": {
"satisfaction": "per_agent",
"preference": "per_agent"
}
}
# Generate report
report = validate_simulation_experiment_empirically(
control_data=control_data,
treatment_data=treatment_data,
validation_types=["statistical"],
statistical_test_type="ks_test",
output_format="report"
)
assert isinstance(report, str)
assert "Data Type Information" in report
assert "satisfaction" in report
assert "preference" in report
# New format uses "**Control:**" and "**Treatment:**" (with markdown bold)
assert "**Control:**" in report
assert "**Treatment:**" in report
assert "- Distribution:" in report # Updated to match actual format
# Check that categorical mappings are shown
assert "low" in report
assert "medium" in report
assert "high" in report
assert "yes" in report
assert "no" in report
assert "maybe" in report
print("β
Categorical report generation tests passed")
def test_mixed_categorical_numeric_data():
"""Test datasets with both categorical and numeric metrics."""
print("Testing mixed categorical and numeric data...")
dataset = SimulationExperimentDataset(
name="Mixed Test",
key_results={
"satisfaction": ["low", "medium", "high"], # Categorical
"score": [1.5, 2.8, 4.2], # Numeric
"preference": ["yes", "no", "maybe"], # Categorical
"count": [10, 15, 8] # Numeric
},
result_types={
"satisfaction": "per_agent",
"score": "per_agent",
"preference": "per_agent",
"count": "per_agent"
}
)
# Check that only categorical metrics have mappings
assert "satisfaction" in dataset.categorical_mappings
assert "preference" in dataset.categorical_mappings
assert "score" not in dataset.categorical_mappings
assert "count" not in dataset.categorical_mappings
# Check data conversion
assert isinstance(dataset.key_results["satisfaction"], list)
assert all(isinstance(x, int) for x in dataset.key_results["satisfaction"])
assert isinstance(dataset.key_results["score"], list)
assert all(isinstance(x, float) for x in dataset.key_results["score"])
# Test metric summaries
cat_summary = dataset.get_metric_summary("satisfaction")
assert cat_summary["is_categorical"] is True
num_summary = dataset.get_metric_summary("score")
assert num_summary["is_categorical"] is False
print("β
Mixed categorical and numeric data tests passed")
def test_categorical_edge_cases():
"""Test edge cases for categorical data handling."""
print("Testing categorical edge cases...")
# Test with single categorical value
dataset1 = SimulationExperimentDataset(
name="Single Value",
key_results={"response": ["yes", "yes", "yes"]},
result_types={"response": "per_agent"}
)
assert "response" in dataset1.categorical_mappings
assert len(dataset1.categorical_mappings["response"]) == 1
assert "yes" in dataset1.categorical_mappings["response"]
# Test with empty list
dataset2 = SimulationExperimentDataset(
name="Empty",
key_results={"response": []},
result_types={"response": "per_agent"}
)
assert "response" not in dataset2.categorical_mappings
# Test with all None values
dataset3 = SimulationExperimentDataset(
name="All None",
key_results={"response": [None, None, None]},
result_types={"response": "per_agent"}
)
assert "response" not in dataset3.categorical_mappings
# Test aggregate categorical data
dataset4 = SimulationExperimentDataset(
name="Aggregate",
key_results={"response": "yes"},
result_types={"response": "aggregate"}
)
assert "response" in dataset4.categorical_mappings
assert dataset4.key_results["response"] == 0 # Converted to ordinal
print("β
Categorical edge cases tests passed")
# New tests for additional data types
def test_ordinal_data_basic():
"""Test basic ordinal data handling."""
print("Testing ordinal data basic functionality...")
# Test numeric ordinal data (Likert scale)
dataset = SimulationExperimentDataset(
name="Likert Test",
key_results={
"satisfaction": [1, 2, 3, 4, 5, 4, 3, 2, 1],
"quality_rating": [2, 3, 4, 5, 5, 4, 3, 2, 1]
},
result_types={
"satisfaction": "per_agent",
"quality_rating": "per_agent"
},
data_types={
"satisfaction": "ordinal",
"quality_rating": "ordinal"
}
)
# Check data type detection
assert dataset.data_types["satisfaction"] == "ordinal"
assert dataset.data_types["quality_rating"] == "ordinal"
# Check ordinal mappings were created
assert "satisfaction" in dataset.ordinal_mappings
assert "quality_rating" in dataset.ordinal_mappings
# Check ordinal info
satisfaction_info = dataset.ordinal_mappings["satisfaction"]
assert satisfaction_info["min_value"] == 1
assert satisfaction_info["max_value"] == 5
assert satisfaction_info["num_levels"] == 5
print("β
Ordinal data basic tests passed")
def test_ordinal_data_string_based():
"""Test string-based ordinal data handling."""
print("Testing string-based ordinal data...")
# Test with standard Likert strings
dataset = SimulationExperimentDataset(
name="String Ordinal Test",
key_results={
"agreement": ["Strongly Disagree", "Disagree", "Neutral", "Agree", "Strongly Agree"],
"quality": ["Poor", "Fair", "Good", "Very Good", "Excellent"]
},
result_types={
"agreement": "per_agent",
"quality": "per_agent"
},
data_types={
"agreement": "ordinal",
"quality": "ordinal"
}
)
# Check that string ordinal data was processed
assert dataset.data_types["agreement"] == "ordinal"
assert dataset.data_types["quality"] == "ordinal"
# Check that strings were converted to ordinal values
agreement_values = dataset.key_results["agreement"]
assert all(isinstance(val, int) for val in agreement_values)
# Check ordinal mappings
assert "agreement" in dataset.ordinal_mappings
agreement_mapping = dataset.ordinal_mappings["agreement"]
# Should be ordered: Strongly Disagree < Disagree < Neutral < Agree < Strongly Agree
assert agreement_mapping["strongly disagree"] < agreement_mapping["disagree"]
assert agreement_mapping["disagree"] < agreement_mapping["neutral"]
assert agreement_mapping["neutral"] < agreement_mapping["agree"]
assert agreement_mapping["agree"] < agreement_mapping["strongly agree"]
# Test conversion back to categorical
assert dataset.convert_ordinal_to_categorical("agreement", 0) == "strongly disagree"
# Test is_categorical_metric (should return True for string-based ordinal)
assert dataset.is_categorical_metric("agreement") == True
print("β
String-based ordinal data tests passed")
def test_ranking_data():
"""Test ranking data handling."""
print("Testing ranking data...")
# Test ranking data (1=best, higher=worse)
dataset = SimulationExperimentDataset(
name="Ranking Test",
key_results={
"preference_rank": [1, 2, 3, 1, 2, 3, 1, 2], # Ranking 3 options
"performance_rank": [1, 3, 2, 4, 1, 2, 3, 4] # Ranking 4 options
},
result_types={
"preference_rank": "per_agent",
"performance_rank": "per_agent"
},
data_types={
"preference_rank": "ranking",
"performance_rank": "ranking"
}
)
# Check data type detection
assert dataset.data_types["preference_rank"] == "ranking"
assert dataset.data_types["performance_rank"] == "ranking"
# Check ranking info
assert "preference_rank" in dataset.ranking_info
pref_info = dataset.ranking_info["preference_rank"]
assert pref_info["min_rank"] == 1
assert pref_info["max_rank"] == 3
assert pref_info["num_ranks"] == 3
assert pref_info["direction"] == "ascending"
perf_info = dataset.ranking_info["performance_rank"]
assert perf_info["min_rank"] == 1
assert perf_info["max_rank"] == 4
assert perf_info["num_ranks"] == 4
# Test data type info
data_type_info = dataset.get_data_type_info("preference_rank")
assert data_type_info["data_type"] == "ranking"
assert "ranking_info" in data_type_info
# Test metric summary
summary = dataset.get_metric_summary("preference_rank")
assert summary["data_type"] == "ranking"
assert "rank_distribution" in summary
print("β
Ranking data tests passed")
def test_count_data():
"""Test count data handling."""
print("Testing count data...")
# Test count data (non-negative integers)
dataset = SimulationExperimentDataset(
name="Count Test",
key_results={
"click_count": [0, 1, 2, 5, 10, 0, 3, 7],
"visit_count": [1, 2, 3, 4, 5, 6, 7, 8]
},
result_types={
"click_count": "per_agent",
"visit_count": "per_agent"
},
data_types={
"click_count": "count",
"visit_count": "count"
}
)
# Check data type detection
assert dataset.data_types["click_count"] == "count"
assert dataset.data_types["visit_count"] == "count"
# Test metric summary
summary = dataset.get_metric_summary("click_count")
assert summary["data_type"] == "count"
assert summary["min_value"] == 0
assert summary["max_value"] == 10
print("β
Count data tests passed")
def test_count_data_validation():
"""Test count data validation (should reject negative numbers)."""
print("Testing count data validation...")
try:
# This should raise an error due to negative values
dataset = SimulationExperimentDataset(
name="Invalid Count Test",
key_results={
"invalid_count": [1, 2, -1, 4] # Negative value should cause error
},
result_types={
"invalid_count": "per_agent"
},
data_types={
"invalid_count": "count"
}
)
assert False, "Should have raised ValueError for negative count data"
except ValueError as e:
assert "non-negative integers" in str(e)
print("β
Count data validation tests passed")
def test_proportion_data():
"""Test proportion data handling."""
print("Testing proportion data...")
# Test proportion data (0-1 range)
dataset = SimulationExperimentDataset(
name="Proportion Test",
key_results={
"success_rate": [0.1, 0.2, 0.8, 1.0, 0.0, 0.5],
"completion_rate": [10, 20, 80, 100, 0, 50] # Percentages that should be normalized
},
result_types={
"success_rate": "per_agent",
"completion_rate": "per_agent"
},
data_types={
"success_rate": "proportion",
"completion_rate": "proportion"
}
)
# Check data type detection
assert dataset.data_types["success_rate"] == "proportion"
assert dataset.data_types["completion_rate"] == "proportion"
# Check that percentages were normalized to 0-1 range
completion_values = dataset.key_results["completion_rate"]
assert all(0 <= val <= 1 for val in completion_values)
assert completion_values[0] == 0.1 # 10 -> 0.1
assert completion_values[3] == 1.0 # 100 -> 1.0
# Success rate should remain unchanged (already 0-1)
success_values = dataset.key_results["success_rate"]
assert success_values == [0.1, 0.2, 0.8, 1.0, 0.0, 0.5]
print("β
Proportion data tests passed")
def test_binary_data():
"""Test binary data handling."""
print("Testing binary data...")
# Test various binary representations
dataset = SimulationExperimentDataset(
name="Binary Test",
key_results={
"bool_values": [True, False, True, False],
"string_values": ["Yes", "No", "True", "False", "1", "0"],
"numeric_values": [1, 0, 1, 0, 2, -1] # 2 and -1 should become 1 and 0
},
result_types={
"bool_values": "per_agent",
"string_values": "per_agent",
"numeric_values": "per_agent"
},
data_types={
"bool_values": "binary",
"string_values": "binary",
"numeric_values": "binary"
}
)
# Check data type detection
assert dataset.data_types["bool_values"] == "binary"
assert dataset.data_types["string_values"] == "binary"
assert dataset.data_types["numeric_values"] == "binary"
# Check that all values were converted to 0 or 1
bool_values = dataset.key_results["bool_values"]
assert bool_values == [1, 0, 1, 0]
string_values = dataset.key_results["string_values"]
assert string_values == [1, 0, 1, 0, 1, 0]
numeric_values = dataset.key_results["numeric_values"]
assert numeric_values == [1, 0, 1, 0, 1, 1] # 2 -> 1, -1 -> 1 (non-zero)
# Test metric summary with binary distribution
summary = dataset.get_metric_summary("bool_values")
assert summary["data_type"] == "binary"
assert "binary_distribution" in summary
distribution = summary["binary_distribution"]
assert distribution["true"] == 2
assert distribution["false"] == 2
print("β
Binary data tests passed")
def test_auto_data_type_detection():
"""Test automatic data type detection."""
print("Testing automatic data type detection...")
# Test auto-detection without explicit data types
dataset = SimulationExperimentDataset(
name="Auto Detection Test",
key_results={
"categorical_data": ["Red", "Blue", "Green", "Red"],
"ordinal_numbers": [1, 2, 3, 4, 5],
"ranking_data": [1, 2, 3, 1, 2, 3], # Should detect as ranking
"count_data": [0, 1, 2, 5, 10],
"proportion_data": [0.1, 0.5, 0.8, 1.0],
"binary_bool": [True, False, True, False],
"numeric_data": [1.5, 2.7, 3.2, 4.1]
},
result_types={name: "per_agent" for name in [
"categorical_data", "ordinal_numbers", "ranking_data",
"count_data", "proportion_data", "binary_bool", "numeric_data"
]}
# No explicit data_types - should auto-detect
)
# Check auto-detection results
assert dataset.data_types["categorical_data"] == "categorical"
assert dataset.data_types["ranking_data"] == "ranking" # Consecutive 1,2,3
assert dataset.data_types["count_data"] == "count"
assert dataset.data_types["proportion_data"] == "proportion"
assert dataset.data_types["binary_bool"] == "binary"
assert dataset.data_types["numeric_data"] == "numeric"
# ordinal_numbers is tricky - could be count or ranking
# [1, 2, 3, 4, 5] has no repetition, so should be detected as count (not ranking)
assert dataset.data_types["ordinal_numbers"] == "count"
print("β
Auto data type detection tests passed")
def test_mixed_data_types_validation():
"""Test validation with mixed data types."""
print("Testing mixed data types validation...")
control_data = {
"name": "Control Mixed",
"key_results": {
"satisfaction": ["Poor", "Fair", "Good", "Very Good", "Excellent"],
"ranking": [1, 2, 3, 1, 2],
"success": [True, False, True, False, True],
"click_count": [0, 1, 2, 3, 4],
"conversion_rate": [0.1, 0.2, 0.3, 0.4, 0.5]
},
"result_types": {name: "per_agent" for name in [
"satisfaction", "ranking", "success", "click_count", "conversion_rate"
]},
"data_types": {
"satisfaction": "ordinal",
"ranking": "ranking",
"success": "binary",
"click_count": "count",
"conversion_rate": "proportion"
}
}
treatment_data = {
"name": "Treatment Mixed",
"key_results": {
"satisfaction": ["Fair", "Good", "Very Good", "Excellent", "Excellent"],
"ranking": [1, 1, 2, 1, 1], # Better rankings
"success": [True, True, True, True, False], # Higher success rate
"click_count": [1, 2, 3, 4, 5], # Higher counts
"conversion_rate": [0.2, 0.3, 0.4, 0.5, 0.6] # Higher rates
},
"result_types": {name: "per_agent" for name in [
"satisfaction", "ranking", "success", "click_count", "conversion_rate"
]},
"data_types": {
"satisfaction": "ordinal",
"ranking": "ranking",
"success": "binary",
"click_count": "count",
"conversion_rate": "proportion"
}
}
# Test statistical validation with mixed data types
result = validate_simulation_experiment_empirically(
control_data=control_data,
treatment_data=treatment_data,
validation_types=["statistical"],
output_format="values"
)
assert result.statistical_results is not None
assert "test_results" in result.statistical_results
assert result.overall_score is not None
# Test report generation with mixed data types
report = validate_simulation_experiment_empirically(
control_data=control_data,
treatment_data=treatment_data,
validation_types=["statistical"],
output_format="report"
)
assert isinstance(report, str)
assert "Data Type Information" in report
assert "Ordinal Data" in report
assert "Ranking Data" in report
assert "Binary Data" in report
assert "Count Data" in report
assert "Proportion Data" in report
print("β
Mixed data types validation tests passed")
def test_data_type_edge_cases():
"""Test edge cases for data type handling."""
print("Testing data type edge cases...")
# Test with None values
dataset = SimulationExperimentDataset(
name="Edge Cases Test",
key_results={
"with_nones": [1, None, 3, None, 5],
"empty_list": [],
"single_value": [42],
"mixed_types": [1, "text", 3.14] # Should default to numeric
},
result_types={
"with_nones": "per_agent",
"empty_list": "per_agent",
"single_value": "per_agent",
"mixed_types": "per_agent"
}
# Let auto-detection handle data types
)
# Should handle None values gracefully
assert dataset.data_types["with_nones"] == "count" # Non-negative integers
assert dataset.data_types["empty_list"] == "numeric" # Default fallback
assert dataset.data_types["single_value"] == "count" # Single positive integer
assert dataset.data_types["mixed_types"] == "categorical" # Mixed types with strings default to categorical
# Test data consistency validation
issues = dataset.validate_data_consistency()
# Should not crash despite edge cases
assert isinstance(issues, list)
print("β
Data type edge cases tests passed")
def test_ordinal_ranking_comparison():
"""Test validation specifically comparing ordinal vs ranking data."""
print("Testing ordinal vs ranking comparison...")
# Control with ordinal satisfaction ratings
control_data = {
"name": "Control Ordinal",
"key_results": {
"satisfaction": [1, 2, 3, 4, 5, 3, 2, 4, 5, 1] # Likert scale
},
"result_types": {"satisfaction": "per_agent"},
"data_types": {"satisfaction": "ordinal"}
}
# Treatment with ranking data (different concept - preference ranks)
treatment_data = {
"name": "Treatment Ranking",
"key_results": {
"satisfaction": [1, 1, 2, 1, 1, 2, 3, 1, 1, 2] # Rankings (1=best)
},
"result_types": {"satisfaction": "per_agent"},
"data_types": {"satisfaction": "ranking"}
}
# Should be able to compare despite different data type semantics
result = validate_simulation_experiment_empirically(
control_data=control_data,
treatment_data=treatment_data,
validation_types=["statistical"],
output_format="values"
)
assert result.statistical_results is not None
assert result.overall_score is not None
print("β
Ordinal vs ranking comparison tests passed")
def test_comprehensive_data_type_report():
"""Test comprehensive data type reporting."""
print("Testing comprehensive data type report...")
# Create datasets with all data types
control_data = {
"name": "Comprehensive Control",
"key_results": {
"categorical": ["Red", "Blue", "Green"],
"ordinal_string": ["Poor", "Good", "Excellent"],
"ordinal_numeric": [1, 3, 5],
"ranking": [1, 2, 3],
"count": [0, 5, 10],
"proportion": [0.1, 0.5, 0.9],
"binary": [True, False, True],
"numeric": [1.5, 2.7, 3.9]
},
"result_types": {name: "per_agent" for name in [
"categorical", "ordinal_string", "ordinal_numeric", "ranking",
"count", "proportion", "binary", "numeric"
]},
"data_types": {
"categorical": "categorical",
"ordinal_string": "ordinal",
"ordinal_numeric": "ordinal",
"ranking": "ranking",
"count": "count",
"proportion": "proportion",
"binary": "binary",
"numeric": "numeric"
}
}
treatment_data = {
"name": "Comprehensive Treatment",
"key_results": {
"categorical": ["Blue", "Green", "Red"],
"ordinal_string": ["Good", "Excellent", "Excellent"],
"ordinal_numeric": [3, 5, 5],
"ranking": [2, 1, 1],
"count": [2, 7, 12],
"proportion": [0.2, 0.6, 1.0],
"binary": [True, True, False],
"numeric": [2.1, 3.2, 4.5]
},
"result_types": {name: "per_agent" for name in [
"categorical", "ordinal_string", "ordinal_numeric", "ranking",
"count", "proportion", "binary", "numeric"
]},
"data_types": {
"categorical": "categorical",
"ordinal_string": "ordinal",
"ordinal_numeric": "ordinal",
"ranking": "ranking",
"count": "count",
"proportion": "proportion",
"binary": "binary",
"numeric": "numeric"
}
}
# Generate comprehensive report
report = validate_simulation_experiment_empirically(
control_data=control_data,
treatment_data=treatment_data,
validation_types=["statistical"],
output_format="report"
)
# Check that all data types are documented
data_type_sections = [
"Categorical Data", "Ordinal Data", "Ranking Data",
"Count Data", "Proportion Data", "Binary Data", "Numeric Data"
]
for section in data_type_sections:
assert section in report, f"Missing section: {section}"
# Check for specific data type information
assert "String categories converted to ordinal" in report
assert "Ordered categories or levels" in report
assert "Rank positions" in report
assert "Non-negative integer counts" in report
assert "Values between 0-1" in report
assert "Binary outcomes converted to 0/1" in report
print("β
Comprehensive data type report tests passed")
def test_csv_reading_single_value_per_agent():
"""Test CSV reading functionality with single value per agent data."""
print("Testing CSV reading with single value per agent...")
# Create test CSV content
csv_content = """Responder #,Vote,Explanation,Age Range,Gender Identity,Political Affiliation,Racial Or Ethnic Identity
1,4,Yes because it is something i have never tried and willing to give it a go,25-34,Male,Democrat,White
2,1,I HATE soup. Cold soup would be worse.,65+,Female,Democrat,White
3,1,i would not purchase a ready to drink gazpacho because i am not a fan of it.,35-44,Female,Democrat,Black
4,5,"It sounds healthy and if it's ready to drink and doesn't require me to do all that chopping of the vegetables, what's not to like!",55-64,Female,Democrat,White
5,3,"Like most things, I would try it at the right price. I normally don't eat gazpacho though.",35-44,Non-binary,Independent / other,Other"""
# Write to temporary CSV file
import tempfile
import os
with tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False) as f:
f.write(csv_content)
csv_file_path = f.name
try:
# Test CSV reading
dataset = SimulationExperimentEmpiricalValidator.read_empirical_data_from_csv(
file_path=csv_file_path,
experimental_data_type="single_value_per_agent",
agent_id_column="Responder #",
value_column="Vote",
agent_comments_column="Explanation",
agent_attributes_columns=["Age Range", "Gender Identity", "Political Affiliation", "Racial Or Ethnic Identity"],
dataset_name="Test Gazpacho Survey"
)
# Verify dataset structure
assert dataset.name == "Test Gazpacho Survey"
assert "Vote" in dataset.key_results
assert "Age Range" in dataset.agent_attributes
assert "Gender Identity" in dataset.agent_attributes
assert "Political Affiliation" in dataset.agent_attributes
assert "Racial Or Ethnic Identity" in dataset.agent_attributes
# Check data values
assert dataset.key_results["Vote"] == [4, 1, 1, 5, 3]
assert len(dataset.agent_justifications) == 5
assert len(dataset.agent_names) == 5
# Check agent names
assert dataset.agent_names == ["1", "2", "3", "4", "5"]
# Check justifications include agent references
first_justification = dataset.agent_justifications[0]
assert isinstance(first_justification, dict)
assert first_justification["agent_name"] == "1"
assert "never tried" in first_justification["justification"]
# Check data types were auto-detected
assert dataset.data_types["Vote"] == "count" # Should be detected as count (1-5 integers)
# Agent attributes should not have data types since they're not in key_results
assert "Age Range" not in dataset.data_types
assert "Gender Identity" not in dataset.data_types
# Check that agent attributes are stored correctly
assert dataset.agent_attributes["Age Range"] == ["25-34", "65+", "35-44", "55-64", "35-44"]
assert dataset.agent_attributes["Gender Identity"] == ["Male", "Female", "Female", "Female", "Non-binary"]
assert dataset.agent_attributes["Political Affiliation"] == ["Democrat", "Democrat", "Democrat", "Democrat", "Independent / other"]
assert dataset.agent_attributes["Racial Or Ethnic Identity"] == ["White", "White", "Black", "White", "Other"]
# Categorical mappings should not be created for agent attributes
assert "Age Range" not in dataset.categorical_mappings
assert "Gender Identity" not in dataset.categorical_mappings
print("β
CSV reading with single value per agent tests passed")
# Agent attributes should not be in key_results anymore
assert "Political Affiliation" not in dataset.key_results
assert "Racial Or Ethnic Identity" not in dataset.key_results
# Check data values
assert dataset.key_results["Vote"] == [4, 1, 1, 5, 3]
assert len(dataset.agent_justifications) == 5
assert len(dataset.agent_names) == 5
# Check agent names
assert dataset.agent_names == ["1", "2", "3", "4", "5"]
# Check justifications include agent references
first_justification = dataset.agent_justifications[0]
assert isinstance(first_justification, dict)
assert first_justification["agent_name"] == "1"
assert "never tried" in first_justification["justification"]
print("β
CSV reading with single value per agent tests passed")
finally:
# Clean up temporary file
os.unlink(csv_file_path)
def test_csv_reading_ranking_per_agent():
"""Test CSV reading functionality with ranking per agent data."""
print("Testing CSV reading with ranking per agent...")
# Create test ranking CSV content
csv_content = """Agent,Product_A_Rank,Product_B_Rank,Product_C_Rank,Comments,Age,Gender
Agent1,1,2,3,Product A was clearly the best,25-34,Male
Agent2,2,1,3,Product B had better value,35-44,Female
Agent3,1,3,2,Liked A most and C second,45-54,Male
Agent4,3,2,1,Product C was my favorite,25-34,Female"""
# Write to temporary CSV file
import tempfile
import os
with tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False) as f:
f.write(csv_content)
csv_file_path = f.name
try:
# Test CSV reading for ranking data
dataset = SimulationExperimentEmpiricalValidator.read_empirical_data_from_csv(
file_path=csv_file_path,
experimental_data_type="ranking_per_agent",
agent_id_column="Agent",
ranking_columns=["Product_A_Rank", "Product_B_Rank", "Product_C_Rank"],
agent_comments_column="Comments",
agent_attributes_columns=["Age", "Gender"],
dataset_name="Product Ranking Study"
)
# Verify dataset structure
assert dataset.name == "Product Ranking Study"
assert "Product_A_Rank" in dataset.key_results
assert "Product_B_Rank" in dataset.key_results
assert "Product_C_Rank" in dataset.key_results
assert "Age" in dataset.agent_attributes
assert "Gender" in dataset.agent_attributes
# Check ranking data values
assert dataset.key_results["Product_A_Rank"] == [1, 2, 1, 3]
assert dataset.key_results["Product_B_Rank"] == [2, 1, 3, 2]
assert dataset.key_results["Product_C_Rank"] == [3, 3, 2, 1]
# Check agent data
assert len(dataset.agent_justifications) == 4
assert len(dataset.agent_names) == 4
assert dataset.agent_names == ["Agent1", "Agent2", "Agent3", "Agent4"]
# Check data types for ranking
assert dataset.data_types["Product_A_Rank"] == "ranking"
assert dataset.data_types["Product_B_Rank"] == "ranking"
assert dataset.data_types["Product_C_Rank"] == "ranking"
# Agent attributes should not have data types since they're not in key_results
assert "Age" not in dataset.data_types
assert "Gender" not in dataset.data_types
# Check that agent attributes are stored correctly
assert dataset.agent_attributes["Age"] == ["25-34", "35-44", "45-54", "25-34"]
assert dataset.agent_attributes["Gender"] == ["Male", "Female", "Male", "Female"]
# Check ranking info was stored
assert "Product_A_Rank" in dataset.ranking_info
ranking_info = dataset.ranking_info["Product_A_Rank"]
assert ranking_info["min_rank"] == 1
assert ranking_info["max_rank"] == 3
print("β
CSV reading with ranking per agent tests passed")
finally:
# Clean up temporary file
os.unlink(csv_file_path)
def test_csv_reading_error_handling():
"""Test CSV reading error handling for invalid inputs."""
print("Testing CSV reading error handling...")
validator = SimulationExperimentEmpiricalValidator()
# Test with non-existent file
try:
dataset = SimulationExperimentEmpiricalValidator.read_empirical_data_from_csv(
file_path="nonexistent_file.csv",
experimental_data_type="single_value_per_agent"
)
assert False, "Should have raised FileNotFoundError"
except FileNotFoundError:
pass # Expected
# Test with invalid experiment type
import tempfile
import os
csv_content = "col1,col2\n1,2\n3,4\n"
with tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False) as f:
f.write(csv_content)
csv_file_path = f.name
try:
try:
dataset = SimulationExperimentEmpiricalValidator.read_empirical_data_from_csv(
file_path=csv_file_path,
experimental_data_type="invalid_type"
)
assert False, "Should have raised ValueError"
except ValueError as e:
assert "experimental_data_type" in str(e)
# Test with missing required column
try:
dataset = SimulationExperimentEmpiricalValidator.read_empirical_data_from_csv(
file_path=csv_file_path,
experimental_data_type="single_value_per_agent",
value_column="nonexistent_column"
)
assert False, "Should have raised ValueError"
except ValueError as e:
assert "not found in CSV" in str(e)
finally:
os.unlink(csv_file_path)
print("β
CSV reading error handling tests passed")
def test_csv_reading_with_missing_data():
"""Test CSV reading with missing/empty values in data."""
print("Testing CSV reading with missing data...")
# Create CSV with some missing values
csv_content = """ID,Score,Comment,Category
1,4,Good product,A
2,,No comment,B
3,5,Excellent,
4,2,Poor quality,A
5,,"",C"""
import tempfile
import os
with tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False) as f:
f.write(csv_content)
csv_file_path = f.name
try:
dataset = SimulationExperimentEmpiricalValidator.read_empirical_data_from_csv(
file_path=csv_file_path,
experimental_data_type="single_value_per_agent",
agent_id_column="ID",
value_column="Score",
agent_comments_column="Comment",
agent_attributes_columns=["Category"],
dataset_name="Test with Missing Data"
)
# Check that missing values are handled properly (converted to None)
scores = dataset.key_results["Score"]
assert scores == [4.0, None, 5.0, 2.0, None] # Missing scores should be None
# Check categories with missing values - now in agent_attributes
categories = dataset.agent_attributes["Category"]
assert None in categories # Should have None for missing category
assert categories == ["A", "B", None, "A", "C"]
# Check justifications - empty comments should still be included
assert len(dataset.agent_justifications) == 5
# Check that missing values don't break data type detection
assert dataset.data_types["Score"] == "count" # Should still detect as count despite missing values
print("β
CSV reading with missing data tests passed")
finally:
os.unlink(csv_file_path)
def test_csv_reading_convenience_method():
"""Test the convenience method for creating validator and reading CSV in one step."""
print("Testing CSV reading convenience method...")
csv_content = """Agent,Rating,Feedback
A1,3,Good
A2,4,Very good
A3,2,Fair"""
import tempfile
import os
with tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False) as f:
f.write(csv_content)
csv_file_path = f.name
try:
# Test convenience method
validator, dataset = SimulationExperimentEmpiricalValidator.create_from_csv(
file_path=csv_file_path,
experimental_data_type="single_value_per_agent",
agent_id_column="Agent",
value_column="Rating",
agent_comments_column="Feedback",
dataset_name="Convenience Test"
)
# Verify it's a properly constructed validator with loaded data
assert dataset is not None
assert dataset.name == "Convenience Test"
assert dataset.key_results["Rating"] == [3.0, 4.0, 2.0]
assert len(dataset.agent_justifications) == 3
print("β
CSV reading convenience method tests passed")
finally:
os.unlink(csv_file_path)
def test_csv_reading_ordinal_ranking_per_agent():
"""Test CSV reading functionality with ordinal ranking per agent data (single column with separator)."""
print("Testing CSV reading with ordinal ranking per agent...")
# Create test ordinal ranking CSV content
csv_content = """Agent,Product_Ranking,Comments,Age,Gender
Agent1,B-A-C,Product B was clearly the best,25-34,Male
Agent2,A-C-B,Product A had better value,35-44,Female
Agent3,C-A-B,Liked C most and A second,45-54,Male
Agent4,A-B-C,Product A was my favorite,25-34,Female
Agent5,B-C-A,B then C then A,35-44,Male"""
# Write to temporary CSV file
import tempfile
import os
with tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False) as f:
f.write(csv_content)
csv_file_path = f.name
try:
# Test CSV reading for ordinal ranking data
dataset = SimulationExperimentEmpiricalValidator.read_empirical_data_from_csv(
file_path=csv_file_path,
experimental_data_type="ordinal_ranking_per_agent",
agent_id_column="Agent",
ordinal_ranking_column="Product_Ranking",
ordinal_ranking_separator="-",
ordinal_ranking_options=["A", "B", "C"],
agent_comments_column="Comments",
agent_attributes_columns=["Age", "Gender"],
dataset_name="Product Ordinal Ranking Study"
)
# Verify dataset structure
assert dataset.name == "Product Ordinal Ranking Study"
assert "A_rank" in dataset.key_results
assert "B_rank" in dataset.key_results
assert "C_rank" in dataset.key_results
assert "Age" in dataset.agent_attributes
assert "Gender" in dataset.agent_attributes
# Check ranking data values (based on the ordinal positions)
# Agent1: B-A-C means B=1st, A=2nd, C=3rd
# Agent2: A-C-B means A=1st, C=2nd, B=3rd
# Agent3: C-A-B means C=1st, A=2nd, B=3rd
# Agent4: A-B-C means A=1st, B=2nd, C=3rd
# Agent5: B-C-A means B=1st, C=2nd, A=3rd
assert dataset.key_results["A_rank"] == [2, 1, 2, 1, 3]
assert dataset.key_results["B_rank"] == [1, 3, 3, 2, 1]
assert dataset.key_results["C_rank"] == [3, 2, 1, 3, 2]
# Check agent data
assert len(dataset.agent_justifications) == 5
assert len(dataset.agent_names) == 5
assert dataset.agent_names == ["Agent1", "Agent2", "Agent3", "Agent4", "Agent5"]
# Check data types for ranking
assert dataset.data_types["A_rank"] == "ranking"
assert dataset.data_types["B_rank"] == "ranking"
assert dataset.data_types["C_rank"] == "ranking"
# Agent attributes should not have data types since they're not in key_results
assert "Age" not in dataset.data_types
assert "Gender" not in dataset.data_types
# Check that agent attributes are stored correctly
assert dataset.agent_attributes["Age"] == ["25-34", "35-44", "45-54", "25-34", "35-44"]
assert dataset.agent_attributes["Gender"] == ["Male", "Female", "Male", "Female", "Male"]
# Check ranking info was stored with ordinal-specific details
assert "A_rank" in dataset.ranking_info
ranking_info = dataset.ranking_info["A_rank"]
assert ranking_info["min_rank"] == 1
assert ranking_info["max_rank"] == 3
assert ranking_info["original_options"] == ["A", "B", "C"]
assert ranking_info["separator"] == "-"
assert ranking_info["source_column"] == "Product_Ranking"
print("β
CSV reading with ordinal ranking per agent tests passed")
finally:
# Clean up temporary file
os.unlink(csv_file_path)
def test_csv_reading_ordinal_ranking_auto_detection():
"""Test auto-detection features for ordinal ranking CSV reading."""
print("Testing ordinal ranking CSV auto-detection...")
# Create test CSV content without explicit options (auto-detect)
csv_content = """participant_id,preference_order,feedback
P001,Option2-Option1-Option3,Option2 was clearly superior
P002,Option1-Option3-Option2,Option1 had the best features
P003,,No response provided
P004,Option3-Option2-Option1,Preferred Option3 overall"""
# Write to temporary CSV file
import tempfile
import os
with tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False) as f:
f.write(csv_content)
csv_file_path = f.name
try:
# Test auto-detection (no explicit column or options specified)
dataset = SimulationExperimentEmpiricalValidator.read_empirical_data_from_csv(
file_path=csv_file_path,
experimental_data_type="ordinal_ranking_per_agent",
agent_id_column="participant_id",
agent_comments_column="feedback",
ordinal_ranking_separator="-"
# No ordinal_ranking_column or ordinal_ranking_options specified
)
# Should auto-detect "preference_order" as the ranking column
# Should auto-detect ["Option1", "Option2", "Option3"] as options
assert "Option1_rank" in dataset.key_results
assert "Option2_rank" in dataset.key_results
assert "Option3_rank" in dataset.key_results
# Check that missing data is handled (P003 has empty ranking)
# P001: Option2-Option1-Option3 β Option1=2, Option2=1, Option3=3
# P002: Option1-Option3-Option2 β Option1=1, Option3=2, Option2=3
# P003: (empty) β All None
# P004: Option3-Option2-Option1 β Option3=1, Option2=2, Option1=3
assert dataset.key_results["Option1_rank"] == [2, 1, None, 3]
assert dataset.key_results["Option2_rank"] == [1, 3, None, 2]
assert dataset.key_results["Option3_rank"] == [3, 2, None, 1]
# Verify ranking info includes auto-detected information
ranking_info = dataset.ranking_info["Option1_rank"]
assert set(ranking_info["original_options"]) == {"Option1", "Option2", "Option3"}
assert ranking_info["separator"] == "-"
assert ranking_info["source_column"] == "preference_order"
print("β
Ordinal ranking CSV auto-detection tests passed")
finally:
# Clean up temporary file
os.unlink(csv_file_path)
def test_csv_reading_ordinal_ranking_error_handling():
"""Test error handling for ordinal ranking CSV reading."""
print("Testing ordinal ranking CSV error handling...")
# Test with malformed data
csv_content = """Agent,Ranking
Agent1,A|B|C
Agent2,InvalidFormat"""
import tempfile
import os
with tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False) as f:
f.write(csv_content)
csv_file_path = f.name
try:
# Should handle malformed data gracefully
dataset = SimulationExperimentEmpiricalValidator.read_empirical_data_from_csv(
file_path=csv_file_path,
experimental_data_type="ordinal_ranking_per_agent",
ordinal_ranking_column="Ranking",
ordinal_ranking_separator="-", # Different separator than data
ordinal_ranking_options=["A", "B", "C"]
)
# Should have created the structure but with None values for malformed data
assert "A_rank" in dataset.key_results
assert "B_rank" in dataset.key_results
assert "C_rank" in dataset.key_results
# Both agents should have None values due to malformed data
assert dataset.key_results["A_rank"] == [None, None]
assert dataset.key_results["B_rank"] == [None, None]
assert dataset.key_results["C_rank"] == [None, None]
print("β
Ordinal ranking CSV error handling tests passed")
finally:
# Clean up temporary file
os.unlink(csv_file_path)
def test_csv_reading_ordinal_ranking_custom_separator():
"""Test ordinal ranking with custom separators."""
print("Testing ordinal ranking with custom separators...")
# Test with different separators
test_cases = [
("|", "X|Y|Z", [1, 2, 3]), # Pipe separator
(">", "Z>X>Y", [2, 3, 1]) # Arrow separator
]
# Note that comma separators would be illegal
for separator, ranking_string, expected_x_y_z in test_cases:
csv_content = f"""Agent,Ranking
Agent1,{ranking_string}"""
import tempfile
import os
with tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False) as f:
f.write(csv_content)
csv_file_path = f.name
try:
dataset = SimulationExperimentEmpiricalValidator.read_empirical_data_from_csv(
file_path=csv_file_path,
experimental_data_type="ordinal_ranking_per_agent",
ordinal_ranking_column="Ranking",
ordinal_ranking_separator=separator,
ordinal_ranking_options=["X", "Y", "Z"]
)
# Verify the parsing worked correctly
x_rank, y_rank, z_rank = expected_x_y_z
assert dataset.key_results["X_rank"] == [x_rank]
assert dataset.key_results["Y_rank"] == [y_rank]
assert dataset.key_results["Z_rank"] == [z_rank]
finally:
os.unlink(csv_file_path)
print("β
Ordinal ranking custom separator tests passed")
def test_csv_reading_explicit_data_types():
"""Test CSV reading with explicit data type specification."""
print("Testing CSV reading with explicit data types...")
csv_content = """ID,Satisfaction,Priority,Active,Score
1,Very Satisfied,High,Yes,85
2,Satisfied,Medium,No,75
3,Neutral,Low,Yes,65"""
import tempfile
import os
with tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False) as f:
f.write(csv_content)
csv_file_path = f.name
try:
# Test CSV reading with explicit data types
dataset = SimulationExperimentEmpiricalValidator.read_empirical_data_from_csv(
file_path=csv_file_path,
experimental_data_type="single_value_per_agent",
agent_id_column="ID",
value_column="Score",
agent_attributes_columns=["Satisfaction", "Priority", "Active"],
dataset_name="Explicit Types Test"
)
# Verify agent attributes were loaded (these are stored separately from key_results)
assert "Satisfaction" in dataset.agent_attributes
assert "Priority" in dataset.agent_attributes
assert "Active" in dataset.agent_attributes
assert dataset.agent_attributes["Satisfaction"] == ["Very Satisfied", "Satisfied", "Neutral"]
assert dataset.agent_attributes["Priority"] == ["High", "Medium", "Low"]
assert dataset.agent_attributes["Active"] == ["Yes", "No", "Yes"]
# Verify main data column was loaded
assert dataset.key_results["Score"] == [85, 75, 65]
assert dataset.result_types["Score"] == "per_agent"
# Check that the Score data type was auto-detected properly
assert dataset.data_types["Score"] == "count" # Auto-detected
print("β
CSV reading with explicit data types tests passed")
finally:
os.unlink(csv_file_path)
|