File size: 87,892 Bytes
7885a28 |
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 |
#
# Author: Damian Eads
# Date: April 17, 2008
#
# Copyright (C) 2008 Damian Eads
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
#
# 3. The name of the author may not be used to endorse or promote
# products derived from this software without specific prior
# written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import sys
import os.path
from functools import wraps, partial
import weakref
import numpy as np
import warnings
from numpy.linalg import norm
from numpy.testing import (verbose, assert_,
assert_array_equal, assert_equal,
assert_almost_equal, assert_allclose,
break_cycles, IS_PYPY)
import pytest
import scipy.spatial.distance
from scipy.spatial.distance import (
squareform, pdist, cdist, num_obs_y, num_obs_dm, is_valid_dm, is_valid_y,
_validate_vector, _METRICS_NAMES)
# these were missing: chebyshev cityblock
# jensenshannon and seuclidean are referenced by string name.
from scipy.spatial.distance import (braycurtis, canberra, chebyshev, cityblock,
correlation, cosine, dice, euclidean,
hamming, jaccard, jensenshannon,
kulczynski1, mahalanobis,
minkowski, rogerstanimoto,
russellrao, seuclidean, sokalmichener, # noqa: F401
sokalsneath, sqeuclidean, yule)
from scipy._lib._util import np_long, np_ulong
@pytest.fixture(params=_METRICS_NAMES, scope="session")
def metric(request):
"""
Fixture for all metrics in scipy.spatial.distance
"""
return request.param
_filenames = [
"cdist-X1.txt",
"cdist-X2.txt",
"iris.txt",
"pdist-boolean-inp.txt",
"pdist-chebyshev-ml-iris.txt",
"pdist-chebyshev-ml.txt",
"pdist-cityblock-ml-iris.txt",
"pdist-cityblock-ml.txt",
"pdist-correlation-ml-iris.txt",
"pdist-correlation-ml.txt",
"pdist-cosine-ml-iris.txt",
"pdist-cosine-ml.txt",
"pdist-double-inp.txt",
"pdist-euclidean-ml-iris.txt",
"pdist-euclidean-ml.txt",
"pdist-hamming-ml.txt",
"pdist-jaccard-ml.txt",
"pdist-jensenshannon-ml-iris.txt",
"pdist-jensenshannon-ml.txt",
"pdist-minkowski-3.2-ml-iris.txt",
"pdist-minkowski-3.2-ml.txt",
"pdist-minkowski-5.8-ml-iris.txt",
"pdist-seuclidean-ml-iris.txt",
"pdist-seuclidean-ml.txt",
"pdist-spearman-ml.txt",
"random-bool-data.txt",
"random-double-data.txt",
"random-int-data.txt",
"random-uint-data.txt",
]
_tdist = np.array([[0, 662, 877, 255, 412, 996],
[662, 0, 295, 468, 268, 400],
[877, 295, 0, 754, 564, 138],
[255, 468, 754, 0, 219, 869],
[412, 268, 564, 219, 0, 669],
[996, 400, 138, 869, 669, 0]], dtype='double')
_ytdist = squareform(_tdist)
# A hashmap of expected output arrays for the tests. These arrays
# come from a list of text files, which are read prior to testing.
# Each test loads inputs and outputs from this dictionary.
eo = {}
def load_testing_files():
for fn in _filenames:
name = fn.replace(".txt", "").replace("-ml", "")
fqfn = os.path.join(os.path.dirname(__file__), 'data', fn)
fp = open(fqfn)
eo[name] = np.loadtxt(fp)
fp.close()
eo['pdist-boolean-inp'] = np.bool_(eo['pdist-boolean-inp'])
eo['random-bool-data'] = np.bool_(eo['random-bool-data'])
eo['random-float32-data'] = np.float32(eo['random-double-data'])
eo['random-int-data'] = np_long(eo['random-int-data'])
eo['random-uint-data'] = np_ulong(eo['random-uint-data'])
load_testing_files()
def _is_32bit():
return np.intp(0).itemsize < 8
def _chk_asarrays(arrays, axis=None):
arrays = [np.asanyarray(a) for a in arrays]
if axis is None:
# np < 1.10 ravel removes subclass from arrays
arrays = [np.ravel(a) if a.ndim != 1 else a
for a in arrays]
axis = 0
arrays = tuple(np.atleast_1d(a) for a in arrays)
if axis < 0:
if not all(a.ndim == arrays[0].ndim for a in arrays):
raise ValueError("array ndim must be the same for neg axis")
axis = range(arrays[0].ndim)[axis]
return arrays + (axis,)
def _chk_weights(arrays, weights=None, axis=None,
force_weights=False, simplify_weights=True,
pos_only=False, neg_check=False,
nan_screen=False, mask_screen=False,
ddof=None):
chked = _chk_asarrays(arrays, axis=axis)
arrays, axis = chked[:-1], chked[-1]
simplify_weights = simplify_weights and not force_weights
if not force_weights and mask_screen:
force_weights = any(np.ma.getmask(a) is not np.ma.nomask for a in arrays)
if nan_screen:
has_nans = [np.isnan(np.sum(a)) for a in arrays]
if any(has_nans):
mask_screen = True
force_weights = True
arrays = tuple(np.ma.masked_invalid(a) if has_nan else a
for a, has_nan in zip(arrays, has_nans))
if weights is not None:
weights = np.asanyarray(weights)
elif force_weights:
weights = np.ones(arrays[0].shape[axis])
else:
return arrays + (weights, axis)
if ddof:
weights = _freq_weights(weights)
if mask_screen:
weights = _weight_masked(arrays, weights, axis)
if not all(weights.shape == (a.shape[axis],) for a in arrays):
raise ValueError("weights shape must match arrays along axis")
if neg_check and (weights < 0).any():
raise ValueError("weights cannot be negative")
if pos_only:
pos_weights = np.nonzero(weights > 0)[0]
if pos_weights.size < weights.size:
arrays = tuple(np.take(a, pos_weights, axis=axis) for a in arrays)
weights = weights[pos_weights]
if simplify_weights and (weights == 1).all():
weights = None
return arrays + (weights, axis)
def _freq_weights(weights):
if weights is None:
return weights
int_weights = weights.astype(int)
if (weights != int_weights).any():
raise ValueError(f"frequency (integer count-type) weights required {weights}")
return int_weights
def _weight_masked(arrays, weights, axis):
if axis is None:
axis = 0
weights = np.asanyarray(weights)
for a in arrays:
axis_mask = np.ma.getmask(a)
if axis_mask is np.ma.nomask:
continue
if a.ndim > 1:
not_axes = tuple(i for i in range(a.ndim) if i != axis)
axis_mask = axis_mask.any(axis=not_axes)
weights *= 1 - axis_mask.astype(int)
return weights
def _rand_split(arrays, weights, axis, split_per, seed=None):
# Coerce `arrays` to float64 if integer, to avoid nan-to-integer issues
arrays = [arr.astype(np.float64) if np.issubdtype(arr.dtype, np.integer)
else arr for arr in arrays]
# inverse operation for stats.collapse_weights
weights = np.array(weights, dtype=np.float64) # modified inplace; need a copy
seeded_rand = np.random.RandomState(seed)
def mytake(a, ix, axis):
record = np.asanyarray(np.take(a, ix, axis=axis))
return record.reshape([a.shape[i] if i != axis else 1
for i in range(a.ndim)])
n_obs = arrays[0].shape[axis]
assert all(a.shape[axis] == n_obs for a in arrays), \
"data must be aligned on sample axis"
for i in range(int(split_per) * n_obs):
split_ix = seeded_rand.randint(n_obs + i)
prev_w = weights[split_ix]
q = seeded_rand.rand()
weights[split_ix] = q * prev_w
weights = np.append(weights, (1. - q) * prev_w)
arrays = [np.append(a, mytake(a, split_ix, axis=axis),
axis=axis) for a in arrays]
return arrays, weights
assert_allclose_forgiving = partial(assert_allclose, atol=1e-5)
def _rough_check(a, b, compare_assert=assert_allclose_forgiving,
key=lambda x: x, w=None):
check_a = key(a)
check_b = key(b)
try:
if np.array(check_a != check_b).any(): # try strict equality for string types
compare_assert(check_a, check_b)
except AttributeError: # masked array
compare_assert(check_a, check_b)
except (TypeError, ValueError): # nested data structure
for a_i, b_i in zip(check_a, check_b):
_rough_check(a_i, b_i, compare_assert=compare_assert)
# diff from test_stats:
# n_args=2, weight_arg='w', default_axis=None
# ma_safe = False, nan_safe = False
def _weight_checked(fn, n_args=2, default_axis=None, key=lambda x: x, weight_arg='w',
squeeze=True, silent=False,
ones_test=True, const_test=True, dup_test=True,
split_test=True, dud_test=True, ma_safe=False, ma_very_safe=False,
nan_safe=False, split_per=1.0, seed=0,
compare_assert=assert_allclose_forgiving):
"""runs fn on its arguments 2 or 3 ways, checks that the results are the same,
then returns the same thing it would have returned before"""
@wraps(fn)
def wrapped(*args, **kwargs):
result = fn(*args, **kwargs)
arrays = args[:n_args]
rest = args[n_args:]
weights = kwargs.get(weight_arg, None)
axis = kwargs.get('axis', default_axis)
chked = _chk_weights(arrays, weights=weights, axis=axis,
force_weights=True, mask_screen=True)
arrays, weights, axis = chked[:-2], chked[-2], chked[-1]
if squeeze:
arrays = [np.atleast_1d(a.squeeze()) for a in arrays]
try:
# WEIGHTS CHECK 1: EQUAL WEIGHTED OBSERVATIONS
args = tuple(arrays) + rest
if ones_test:
kwargs[weight_arg] = weights
_rough_check(result, fn(*args, **kwargs), key=key)
if const_test:
kwargs[weight_arg] = weights * 101.0
_rough_check(result, fn(*args, **kwargs), key=key)
kwargs[weight_arg] = weights * 0.101
try:
_rough_check(result, fn(*args, **kwargs), key=key)
except Exception as e:
raise type(e)((e, arrays, weights)) from e
# WEIGHTS CHECK 2: ADDL 0-WEIGHTED OBS
if dud_test:
# add randomly resampled rows, weighted at 0
dud_arrays, dud_weights = _rand_split(arrays, weights, axis,
split_per=split_per, seed=seed)
dud_weights[:weights.size] = weights # not exactly 1 because of masked arrays # noqa: E501
dud_weights[weights.size:] = 0
dud_args = tuple(dud_arrays) + rest
kwargs[weight_arg] = dud_weights
_rough_check(result, fn(*dud_args, **kwargs), key=key)
# increase the value of those 0-weighted rows
for a in dud_arrays:
indexer = [slice(None)] * a.ndim
indexer[axis] = slice(weights.size, None)
indexer = tuple(indexer)
a[indexer] = a[indexer] * 101
dud_args = tuple(dud_arrays) + rest
_rough_check(result, fn(*dud_args, **kwargs), key=key)
# set those 0-weighted rows to NaNs
for a in dud_arrays:
indexer = [slice(None)] * a.ndim
indexer[axis] = slice(weights.size, None)
indexer = tuple(indexer)
a[indexer] = a[indexer] * np.nan
if kwargs.get("nan_policy", None) == "omit" and nan_safe:
dud_args = tuple(dud_arrays) + rest
_rough_check(result, fn(*dud_args, **kwargs), key=key)
# mask out those nan values
if ma_safe:
dud_arrays = [np.ma.masked_invalid(a) for a in dud_arrays]
dud_args = tuple(dud_arrays) + rest
_rough_check(result, fn(*dud_args, **kwargs), key=key)
if ma_very_safe:
kwargs[weight_arg] = None
_rough_check(result, fn(*dud_args, **kwargs), key=key)
del dud_arrays, dud_args, dud_weights
# WEIGHTS CHECK 3: DUPLICATE DATA (DUMB SPLITTING)
if dup_test:
dup_arrays = [np.append(a, a, axis=axis) for a in arrays]
dup_weights = np.append(weights, weights) / 2.0
dup_args = tuple(dup_arrays) + rest
kwargs[weight_arg] = dup_weights
_rough_check(result, fn(*dup_args, **kwargs), key=key)
del dup_args, dup_arrays, dup_weights
# WEIGHT CHECK 3: RANDOM SPLITTING
if split_test and split_per > 0:
split = _rand_split(arrays, weights, axis,
split_per=split_per, seed=seed)
split_arrays, split_weights = split
split_args = tuple(split_arrays) + rest
kwargs[weight_arg] = split_weights
_rough_check(result, fn(*split_args, **kwargs), key=key)
except NotImplementedError as e:
# when some combination of arguments makes weighting impossible,
# this is the desired response
if not silent:
warnings.warn(f"{fn.__name__} NotImplemented weights: {e}",
stacklevel=3)
return result
return wrapped
class DummyContextManager:
def __enter__(self):
pass
def __exit__(self, *args):
pass
def maybe_deprecated(metric: str):
if metric in ('kulczynski1', 'sokalmichener'):
return pytest.deprecated_call()
else:
return DummyContextManager()
wcdist = _weight_checked(cdist, default_axis=1, squeeze=False)
wcdist_no_const = _weight_checked(cdist, default_axis=1,
squeeze=False, const_test=False)
wpdist = _weight_checked(pdist, default_axis=1, squeeze=False, n_args=1)
wpdist_no_const = _weight_checked(pdist, default_axis=1, squeeze=False,
const_test=False, n_args=1)
wrogerstanimoto = _weight_checked(rogerstanimoto)
wmatching = whamming = _weight_checked(hamming, dud_test=False)
wyule = _weight_checked(yule)
wdice = _weight_checked(dice)
wcityblock = _weight_checked(cityblock)
wchebyshev = _weight_checked(chebyshev)
wcosine = _weight_checked(cosine)
wcorrelation = _weight_checked(correlation)
wkulczynski1 = _weight_checked(kulczynski1)
wjaccard = _weight_checked(jaccard)
weuclidean = _weight_checked(euclidean, const_test=False)
wsqeuclidean = _weight_checked(sqeuclidean, const_test=False)
wbraycurtis = _weight_checked(braycurtis)
wcanberra = _weight_checked(canberra, const_test=False)
wsokalsneath = _weight_checked(sokalsneath)
wsokalmichener = _weight_checked(sokalmichener)
wrussellrao = _weight_checked(russellrao)
class TestCdist:
def setup_method(self):
self.rnd_eo_names = ['random-float32-data', 'random-int-data',
'random-uint-data', 'random-double-data',
'random-bool-data']
self.valid_upcasts = {'bool': [np_ulong, np_long, np.float32, np.float64],
'uint': [np_long, np.float32, np.float64],
'int': [np.float32, np.float64],
'float32': [np.float64]}
@pytest.mark.thread_unsafe
def test_cdist_extra_args(self, metric):
# Tests that args and kwargs are correctly handled
X1 = [[1., 2., 3.], [1.2, 2.3, 3.4], [2.2, 2.3, 4.4]]
X2 = [[7., 5., 8.], [7.5, 5.8, 8.4], [5.5, 5.8, 4.4]]
kwargs = {"N0tV4l1D_p4raM": 3.14, "w": np.arange(3)}
args = [3.14] * 200
with pytest.raises(TypeError):
with maybe_deprecated(metric):
cdist(X1, X2, metric=metric, **kwargs)
with pytest.raises(TypeError):
with maybe_deprecated(metric):
cdist(X1, X2, metric=eval(metric), **kwargs)
with pytest.raises(TypeError):
with maybe_deprecated(metric):
cdist(X1, X2, metric="test_" + metric, **kwargs)
with pytest.raises(TypeError):
cdist(X1, X2, metric=metric, *args)
with pytest.raises(TypeError):
cdist(X1, X2, metric=eval(metric), *args)
with pytest.raises(TypeError):
cdist(X1, X2, metric="test_" + metric, *args)
def test_cdist_extra_args_custom(self):
# Tests that args and kwargs are correctly handled
# also for custom metric
def _my_metric(x, y, arg, kwarg=1, kwarg2=2):
return arg + kwarg + kwarg2
X1 = [[1., 2., 3.], [1.2, 2.3, 3.4], [2.2, 2.3, 4.4]]
X2 = [[7., 5., 8.], [7.5, 5.8, 8.4], [5.5, 5.8, 4.4]]
kwargs = {"N0tV4l1D_p4raM": 3.14, "w": np.arange(3)}
args = [3.14] * 200
with pytest.raises(TypeError):
cdist(X1, X2, _my_metric)
with pytest.raises(TypeError):
cdist(X1, X2, _my_metric, *args)
with pytest.raises(TypeError):
cdist(X1, X2, _my_metric, **kwargs)
with pytest.raises(TypeError):
cdist(X1, X2, _my_metric, kwarg=2.2, kwarg2=3.3)
with pytest.raises(TypeError):
cdist(X1, X2, _my_metric, 1, 2, kwarg=2.2)
with pytest.raises(TypeError):
cdist(X1, X2, _my_metric, 1, 2, kwarg=2.2)
with pytest.raises(TypeError):
cdist(X1, X2, _my_metric, 1.1, 2.2, 3.3)
with pytest.raises(TypeError):
cdist(X1, X2, _my_metric, 1.1, 2.2)
with pytest.raises(TypeError):
cdist(X1, X2, _my_metric, 1.1)
with pytest.raises(TypeError):
cdist(X1, X2, _my_metric, 1.1, kwarg=2.2, kwarg2=3.3)
# this should work
assert_allclose(cdist(X1, X2, metric=_my_metric,
arg=1.1, kwarg2=3.3), 5.4)
def test_cdist_euclidean_random_unicode(self):
eps = 1e-15
X1 = eo['cdist-X1']
X2 = eo['cdist-X2']
Y1 = wcdist_no_const(X1, X2, 'euclidean')
Y2 = wcdist_no_const(X1, X2, 'test_euclidean')
assert_allclose(Y1, Y2, rtol=eps, verbose=verbose > 2)
@pytest.mark.parametrize("p", [0.1, 0.25, 1.0, 1.23,
2.0, 3.8, 4.6, np.inf])
def test_cdist_minkowski_random(self, p):
eps = 1e-13
X1 = eo['cdist-X1']
X2 = eo['cdist-X2']
Y1 = wcdist_no_const(X1, X2, 'minkowski', p=p)
Y2 = wcdist_no_const(X1, X2, 'test_minkowski', p=p)
assert_allclose(Y1, Y2, atol=0, rtol=eps, verbose=verbose > 2)
def test_cdist_cosine_random(self):
eps = 1e-14
X1 = eo['cdist-X1']
X2 = eo['cdist-X2']
Y1 = wcdist(X1, X2, 'cosine')
# Naive implementation
def norms(X):
return np.linalg.norm(X, axis=1).reshape(-1, 1)
Y2 = 1 - np.dot((X1 / norms(X1)), (X2 / norms(X2)).T)
assert_allclose(Y1, Y2, rtol=eps, verbose=verbose > 2)
def test_cdist_mahalanobis(self):
# 1-dimensional observations
x1 = np.array([[2], [3]])
x2 = np.array([[2], [5]])
dist = cdist(x1, x2, metric='mahalanobis')
assert_allclose(dist, [[0.0, np.sqrt(4.5)], [np.sqrt(0.5), np.sqrt(2)]])
# 2-dimensional observations
x1 = np.array([[0, 0], [-1, 0]])
x2 = np.array([[0, 2], [1, 0], [0, -2]])
dist = cdist(x1, x2, metric='mahalanobis')
rt2 = np.sqrt(2)
assert_allclose(dist, [[rt2, rt2, rt2], [2, 2 * rt2, 2]])
# Too few observations
with pytest.raises(ValueError):
cdist([[0, 1]], [[2, 3]], metric='mahalanobis')
def test_cdist_custom_notdouble(self):
class myclass:
pass
def _my_metric(x, y):
if not isinstance(x[0], myclass) or not isinstance(y[0], myclass):
raise ValueError("Type has been changed")
return 1.123
data = np.array([[myclass()]], dtype=object)
cdist_y = cdist(data, data, metric=_my_metric)
right_y = 1.123
assert_equal(cdist_y, right_y, verbose=verbose > 2)
def _check_calling_conventions(self, X1, X2, metric, eps=1e-07, **kwargs):
# helper function for test_cdist_calling_conventions
try:
y1 = cdist(X1, X2, metric=metric, **kwargs)
y2 = cdist(X1, X2, metric=eval(metric), **kwargs)
y3 = cdist(X1, X2, metric="test_" + metric, **kwargs)
except Exception as e:
e_cls = e.__class__
if verbose > 2:
print(e_cls.__name__)
print(e)
with pytest.raises(e_cls):
cdist(X1, X2, metric=metric, **kwargs)
with pytest.raises(e_cls):
cdist(X1, X2, metric=eval(metric), **kwargs)
with pytest.raises(e_cls):
cdist(X1, X2, metric="test_" + metric, **kwargs)
else:
assert_allclose(y1, y2, rtol=eps, verbose=verbose > 2)
assert_allclose(y1, y3, rtol=eps, verbose=verbose > 2)
def test_cdist_calling_conventions(self, metric):
# Ensures that specifying the metric with a str or scipy function
# gives the same behaviour (i.e. same result or same exception).
# NOTE: The correctness should be checked within each metric tests.
for eo_name in self.rnd_eo_names:
# subsampling input data to speed-up tests
# NOTE: num samples needs to be > than dimensions for mahalanobis
X1 = eo[eo_name][::5, ::-2]
X2 = eo[eo_name][1::5, ::2]
if verbose > 2:
print("testing: ", metric, " with: ", eo_name)
if metric in {'dice', 'yule',
'rogerstanimoto',
'russellrao', 'sokalmichener',
'sokalsneath',
'kulczynski1'} and 'bool' not in eo_name:
# python version permits non-bools e.g. for fuzzy logic
continue
self._check_calling_conventions(X1, X2, metric)
# Testing built-in metrics with extra args
if metric == "seuclidean":
X12 = np.vstack([X1, X2]).astype(np.float64)
V = np.var(X12, axis=0, ddof=1)
self._check_calling_conventions(X1, X2, metric, V=V)
elif metric == "mahalanobis":
X12 = np.vstack([X1, X2]).astype(np.float64)
V = np.atleast_2d(np.cov(X12.T))
VI = np.array(np.linalg.inv(V).T)
self._check_calling_conventions(X1, X2, metric, VI=VI)
def test_cdist_dtype_equivalence(self, metric):
# Tests that the result is not affected by type up-casting
eps = 1e-07
tests = [(eo['random-bool-data'], self.valid_upcasts['bool']),
(eo['random-uint-data'], self.valid_upcasts['uint']),
(eo['random-int-data'], self.valid_upcasts['int']),
(eo['random-float32-data'], self.valid_upcasts['float32'])]
for test in tests:
X1 = test[0][::5, ::-2]
X2 = test[0][1::5, ::2]
try:
y1 = cdist(X1, X2, metric=metric)
except Exception as e:
e_cls = e.__class__
if verbose > 2:
print(e_cls.__name__)
print(e)
for new_type in test[1]:
X1new = new_type(X1)
X2new = new_type(X2)
with pytest.raises(e_cls):
cdist(X1new, X2new, metric=metric)
else:
for new_type in test[1]:
y2 = cdist(new_type(X1), new_type(X2), metric=metric)
assert_allclose(y1, y2, rtol=eps, verbose=verbose > 2)
@pytest.mark.thread_unsafe
def test_cdist_out(self, metric):
# Test that out parameter works properly
eps = 1e-15
X1 = eo['cdist-X1']
X2 = eo['cdist-X2']
out_r, out_c = X1.shape[0], X2.shape[0]
kwargs = dict()
if metric == 'minkowski':
kwargs['p'] = 1.23
out1 = np.empty((out_r, out_c), dtype=np.float64)
with maybe_deprecated(metric):
Y1 = cdist(X1, X2, metric, **kwargs)
with maybe_deprecated(metric):
Y2 = cdist(X1, X2, metric, out=out1, **kwargs)
# test that output is numerically equivalent
assert_allclose(Y1, Y2, rtol=eps, verbose=verbose > 2)
# test that Y_test1 and out1 are the same object
assert_(Y2 is out1)
# test for incorrect shape
out2 = np.empty((out_r-1, out_c+1), dtype=np.float64)
with pytest.raises(ValueError):
with maybe_deprecated(metric):
cdist(X1, X2, metric, out=out2, **kwargs)
# test for C-contiguous order
out3 = np.empty(
(2 * out_r, 2 * out_c), dtype=np.float64)[::2, ::2]
out4 = np.empty((out_r, out_c), dtype=np.float64, order='F')
with pytest.raises(ValueError):
with maybe_deprecated(metric):
cdist(X1, X2, metric, out=out3, **kwargs)
with pytest.raises(ValueError):
with maybe_deprecated(metric):
cdist(X1, X2, metric, out=out4, **kwargs)
# test for incorrect dtype
out5 = np.empty((out_r, out_c), dtype=np.int64)
with pytest.raises(ValueError):
with maybe_deprecated(metric):
cdist(X1, X2, metric, out=out5, **kwargs)
@pytest.mark.thread_unsafe
def test_striding(self, metric):
# test that striding is handled correct with calls to
# _copy_array_if_base_present
eps = 1e-15
X1 = eo['cdist-X1'][::2, ::2]
X2 = eo['cdist-X2'][::2, ::2]
X1_copy = X1.copy()
X2_copy = X2.copy()
# confirm equivalence
assert_equal(X1, X1_copy)
assert_equal(X2, X2_copy)
# confirm contiguity
assert_(not X1.flags.c_contiguous)
assert_(not X2.flags.c_contiguous)
assert_(X1_copy.flags.c_contiguous)
assert_(X2_copy.flags.c_contiguous)
kwargs = dict()
if metric == 'minkowski':
kwargs['p'] = 1.23
with maybe_deprecated(metric):
Y1 = cdist(X1, X2, metric, **kwargs)
with maybe_deprecated(metric):
Y2 = cdist(X1_copy, X2_copy, metric, **kwargs)
# test that output is numerically equivalent
assert_allclose(Y1, Y2, rtol=eps, verbose=verbose > 2)
@pytest.mark.thread_unsafe
def test_cdist_refcount(self, metric):
x1 = np.random.rand(10, 10)
x2 = np.random.rand(10, 10)
kwargs = dict()
if metric == 'minkowski':
kwargs['p'] = 1.23
with maybe_deprecated(metric):
out = cdist(x1, x2, metric=metric, **kwargs)
# Check reference counts aren't messed up. If we only hold weak
# references, the arrays should be deallocated.
weak_refs = [weakref.ref(v) for v in (x1, x2, out)]
del x1, x2, out
if IS_PYPY:
break_cycles()
assert all(weak_ref() is None for weak_ref in weak_refs)
class TestPdist:
def setup_method(self):
self.rnd_eo_names = ['random-float32-data', 'random-int-data',
'random-uint-data', 'random-double-data',
'random-bool-data']
self.valid_upcasts = {'bool': [np_ulong, np_long, np.float32, np.float64],
'uint': [np_long, np.float32, np.float64],
'int': [np.float32, np.float64],
'float32': [np.float64]}
@pytest.mark.thread_unsafe
def test_pdist_extra_args(self, metric):
# Tests that args and kwargs are correctly handled
X1 = [[1., 2.], [1.2, 2.3], [2.2, 2.3]]
kwargs = {"N0tV4l1D_p4raM": 3.14, "w": np.arange(2)}
args = [3.14] * 200
with pytest.raises(TypeError):
with maybe_deprecated(metric):
pdist(X1, metric=metric, **kwargs)
with pytest.raises(TypeError):
with maybe_deprecated(metric):
pdist(X1, metric=eval(metric), **kwargs)
with pytest.raises(TypeError):
with maybe_deprecated(metric):
pdist(X1, metric="test_" + metric, **kwargs)
with pytest.raises(TypeError):
pdist(X1, metric=metric, *args)
with pytest.raises(TypeError):
pdist(X1, metric=eval(metric), *args)
with pytest.raises(TypeError):
pdist(X1, metric="test_" + metric, *args)
def test_pdist_extra_args_custom(self):
# Tests that args and kwargs are correctly handled
# also for custom metric
def _my_metric(x, y, arg, kwarg=1, kwarg2=2):
return arg + kwarg + kwarg2
X1 = [[1., 2.], [1.2, 2.3], [2.2, 2.3]]
kwargs = {"N0tV4l1D_p4raM": 3.14, "w": np.arange(2)}
args = [3.14] * 200
with pytest.raises(TypeError):
pdist(X1, _my_metric)
with pytest.raises(TypeError):
pdist(X1, _my_metric, *args)
with pytest.raises(TypeError):
pdist(X1, _my_metric, **kwargs)
with pytest.raises(TypeError):
pdist(X1, _my_metric, kwarg=2.2, kwarg2=3.3)
with pytest.raises(TypeError):
pdist(X1, _my_metric, 1, 2, kwarg=2.2)
with pytest.raises(TypeError):
pdist(X1, _my_metric, 1, 2, kwarg=2.2)
with pytest.raises(TypeError):
pdist(X1, _my_metric, 1.1, 2.2, 3.3)
with pytest.raises(TypeError):
pdist(X1, _my_metric, 1.1, 2.2)
with pytest.raises(TypeError):
pdist(X1, _my_metric, 1.1)
with pytest.raises(TypeError):
pdist(X1, _my_metric, 1.1, kwarg=2.2, kwarg2=3.3)
# these should work
assert_allclose(pdist(X1, metric=_my_metric,
arg=1.1, kwarg2=3.3), 5.4)
def test_pdist_euclidean_random(self):
eps = 1e-07
X = eo['pdist-double-inp']
Y_right = eo['pdist-euclidean']
Y_test1 = wpdist_no_const(X, 'euclidean')
assert_allclose(Y_test1, Y_right, rtol=eps)
def test_pdist_euclidean_random_u(self):
eps = 1e-07
X = eo['pdist-double-inp']
Y_right = eo['pdist-euclidean']
Y_test1 = wpdist_no_const(X, 'euclidean')
assert_allclose(Y_test1, Y_right, rtol=eps)
def test_pdist_euclidean_random_float32(self):
eps = 1e-07
X = np.float32(eo['pdist-double-inp'])
Y_right = eo['pdist-euclidean']
Y_test1 = wpdist_no_const(X, 'euclidean')
assert_allclose(Y_test1, Y_right, rtol=eps)
def test_pdist_euclidean_random_nonC(self):
eps = 1e-07
X = eo['pdist-double-inp']
Y_right = eo['pdist-euclidean']
Y_test2 = wpdist_no_const(X, 'test_euclidean')
assert_allclose(Y_test2, Y_right, rtol=eps)
@pytest.mark.slow
def test_pdist_euclidean_iris_double(self):
eps = 1e-7
X = eo['iris']
Y_right = eo['pdist-euclidean-iris']
Y_test1 = wpdist_no_const(X, 'euclidean')
assert_allclose(Y_test1, Y_right, rtol=eps)
@pytest.mark.slow
def test_pdist_euclidean_iris_float32(self):
eps = 1e-5
X = np.float32(eo['iris'])
Y_right = eo['pdist-euclidean-iris']
Y_test1 = wpdist_no_const(X, 'euclidean')
assert_allclose(Y_test1, Y_right, rtol=eps, verbose=verbose > 2)
@pytest.mark.slow
def test_pdist_euclidean_iris_nonC(self):
# Test pdist(X, 'test_euclidean') [the non-C implementation] on the
# Iris data set.
eps = 1e-7
X = eo['iris']
Y_right = eo['pdist-euclidean-iris']
Y_test2 = wpdist_no_const(X, 'test_euclidean')
assert_allclose(Y_test2, Y_right, rtol=eps)
def test_pdist_seuclidean_random(self):
eps = 1e-7
X = eo['pdist-double-inp']
Y_right = eo['pdist-seuclidean']
Y_test1 = pdist(X, 'seuclidean')
assert_allclose(Y_test1, Y_right, rtol=eps)
def test_pdist_seuclidean_random_float32(self):
eps = 1e-7
X = np.float32(eo['pdist-double-inp'])
Y_right = eo['pdist-seuclidean']
Y_test1 = pdist(X, 'seuclidean')
assert_allclose(Y_test1, Y_right, rtol=eps)
# Check no error is raise when V has float32 dtype (#11171).
V = np.var(X, axis=0, ddof=1)
Y_test2 = pdist(X, 'seuclidean', V=V)
assert_allclose(Y_test2, Y_right, rtol=eps)
def test_pdist_seuclidean_random_nonC(self):
# Test pdist(X, 'test_sqeuclidean') [the non-C implementation]
eps = 1e-07
X = eo['pdist-double-inp']
Y_right = eo['pdist-seuclidean']
Y_test2 = pdist(X, 'test_seuclidean')
assert_allclose(Y_test2, Y_right, rtol=eps)
def test_pdist_seuclidean_iris(self):
eps = 1e-7
X = eo['iris']
Y_right = eo['pdist-seuclidean-iris']
Y_test1 = pdist(X, 'seuclidean')
assert_allclose(Y_test1, Y_right, rtol=eps)
def test_pdist_seuclidean_iris_float32(self):
# Tests pdist(X, 'seuclidean') on the Iris data set (float32).
eps = 1e-5
X = np.float32(eo['iris'])
Y_right = eo['pdist-seuclidean-iris']
Y_test1 = pdist(X, 'seuclidean')
assert_allclose(Y_test1, Y_right, rtol=eps)
def test_pdist_seuclidean_iris_nonC(self):
# Test pdist(X, 'test_seuclidean') [the non-C implementation] on the
# Iris data set.
eps = 1e-7
X = eo['iris']
Y_right = eo['pdist-seuclidean-iris']
Y_test2 = pdist(X, 'test_seuclidean')
assert_allclose(Y_test2, Y_right, rtol=eps)
def test_pdist_cosine_random(self):
eps = 1e-7
X = eo['pdist-double-inp']
Y_right = eo['pdist-cosine']
Y_test1 = wpdist(X, 'cosine')
assert_allclose(Y_test1, Y_right, rtol=eps)
def test_pdist_cosine_random_float32(self):
eps = 1e-7
X = np.float32(eo['pdist-double-inp'])
Y_right = eo['pdist-cosine']
Y_test1 = wpdist(X, 'cosine')
assert_allclose(Y_test1, Y_right, rtol=eps)
def test_pdist_cosine_random_nonC(self):
# Test pdist(X, 'test_cosine') [the non-C implementation]
eps = 1e-7
X = eo['pdist-double-inp']
Y_right = eo['pdist-cosine']
Y_test2 = wpdist(X, 'test_cosine')
assert_allclose(Y_test2, Y_right, rtol=eps)
@pytest.mark.slow
def test_pdist_cosine_iris(self):
eps = 1e-05
X = eo['iris']
Y_right = eo['pdist-cosine-iris']
Y_test1 = wpdist(X, 'cosine')
assert_allclose(Y_test1, Y_right, atol=eps)
@pytest.mark.slow
def test_pdist_cosine_iris_float32(self):
eps = 1e-05
X = np.float32(eo['iris'])
Y_right = eo['pdist-cosine-iris']
Y_test1 = wpdist(X, 'cosine')
assert_allclose(Y_test1, Y_right, atol=eps, verbose=verbose > 2)
@pytest.mark.slow
def test_pdist_cosine_iris_nonC(self):
eps = 1e-05
X = eo['iris']
Y_right = eo['pdist-cosine-iris']
Y_test2 = wpdist(X, 'test_cosine')
assert_allclose(Y_test2, Y_right, atol=eps)
def test_pdist_cosine_bounds(self):
# Test adapted from @joernhees's example at gh-5208: case where
# cosine distance used to be negative. XXX: very sensitive to the
# specific norm computation.
x = np.abs(np.random.RandomState(1337).rand(91))
X = np.vstack([x, x])
assert_(wpdist(X, 'cosine')[0] >= 0,
msg='cosine distance should be non-negative')
def test_pdist_cityblock_random(self):
eps = 1e-7
X = eo['pdist-double-inp']
Y_right = eo['pdist-cityblock']
Y_test1 = wpdist_no_const(X, 'cityblock')
assert_allclose(Y_test1, Y_right, rtol=eps)
def test_pdist_cityblock_random_float32(self):
eps = 1e-7
X = np.float32(eo['pdist-double-inp'])
Y_right = eo['pdist-cityblock']
Y_test1 = wpdist_no_const(X, 'cityblock')
assert_allclose(Y_test1, Y_right, rtol=eps)
def test_pdist_cityblock_random_nonC(self):
eps = 1e-7
X = eo['pdist-double-inp']
Y_right = eo['pdist-cityblock']
Y_test2 = wpdist_no_const(X, 'test_cityblock')
assert_allclose(Y_test2, Y_right, rtol=eps)
@pytest.mark.slow
def test_pdist_cityblock_iris(self):
eps = 1e-14
X = eo['iris']
Y_right = eo['pdist-cityblock-iris']
Y_test1 = wpdist_no_const(X, 'cityblock')
assert_allclose(Y_test1, Y_right, rtol=eps)
@pytest.mark.slow
def test_pdist_cityblock_iris_float32(self):
eps = 1e-5
X = np.float32(eo['iris'])
Y_right = eo['pdist-cityblock-iris']
Y_test1 = wpdist_no_const(X, 'cityblock')
assert_allclose(Y_test1, Y_right, rtol=eps, verbose=verbose > 2)
@pytest.mark.slow
def test_pdist_cityblock_iris_nonC(self):
# Test pdist(X, 'test_cityblock') [the non-C implementation] on the
# Iris data set.
eps = 1e-14
X = eo['iris']
Y_right = eo['pdist-cityblock-iris']
Y_test2 = wpdist_no_const(X, 'test_cityblock')
assert_allclose(Y_test2, Y_right, rtol=eps)
def test_pdist_correlation_random(self):
eps = 1e-7
X = eo['pdist-double-inp']
Y_right = eo['pdist-correlation']
Y_test1 = wpdist(X, 'correlation')
assert_allclose(Y_test1, Y_right, rtol=eps)
def test_pdist_correlation_random_float32(self):
eps = 1e-7
X = np.float32(eo['pdist-double-inp'])
Y_right = eo['pdist-correlation']
Y_test1 = wpdist(X, 'correlation')
assert_allclose(Y_test1, Y_right, rtol=eps)
def test_pdist_correlation_random_nonC(self):
eps = 1e-7
X = eo['pdist-double-inp']
Y_right = eo['pdist-correlation']
Y_test2 = wpdist(X, 'test_correlation')
assert_allclose(Y_test2, Y_right, rtol=eps)
@pytest.mark.slow
def test_pdist_correlation_iris(self):
eps = 1e-7
X = eo['iris']
Y_right = eo['pdist-correlation-iris']
Y_test1 = wpdist(X, 'correlation')
assert_allclose(Y_test1, Y_right, rtol=eps)
@pytest.mark.slow
def test_pdist_correlation_iris_float32(self):
eps = 1e-7
X = eo['iris']
Y_right = np.float32(eo['pdist-correlation-iris'])
Y_test1 = wpdist(X, 'correlation')
assert_allclose(Y_test1, Y_right, rtol=eps, verbose=verbose > 2)
@pytest.mark.slow
def test_pdist_correlation_iris_nonC(self):
if sys.maxsize > 2**32:
eps = 1e-7
else:
pytest.skip("see gh-16456")
X = eo['iris']
Y_right = eo['pdist-correlation-iris']
Y_test2 = wpdist(X, 'test_correlation')
assert_allclose(Y_test2, Y_right, rtol=eps)
@pytest.mark.parametrize("p", [0.1, 0.25, 1.0, 2.0, 3.2, np.inf])
def test_pdist_minkowski_random_p(self, p):
eps = 1e-13
X = eo['pdist-double-inp']
Y1 = wpdist_no_const(X, 'minkowski', p=p)
Y2 = wpdist_no_const(X, 'test_minkowski', p=p)
assert_allclose(Y1, Y2, atol=0, rtol=eps)
def test_pdist_minkowski_random(self):
eps = 1e-7
X = eo['pdist-double-inp']
Y_right = eo['pdist-minkowski-3.2']
Y_test1 = wpdist_no_const(X, 'minkowski', p=3.2)
assert_allclose(Y_test1, Y_right, rtol=eps)
def test_pdist_minkowski_random_float32(self):
eps = 1e-7
X = np.float32(eo['pdist-double-inp'])
Y_right = eo['pdist-minkowski-3.2']
Y_test1 = wpdist_no_const(X, 'minkowski', p=3.2)
assert_allclose(Y_test1, Y_right, rtol=eps)
def test_pdist_minkowski_random_nonC(self):
eps = 1e-7
X = eo['pdist-double-inp']
Y_right = eo['pdist-minkowski-3.2']
Y_test2 = wpdist_no_const(X, 'test_minkowski', p=3.2)
assert_allclose(Y_test2, Y_right, rtol=eps)
@pytest.mark.slow
def test_pdist_minkowski_3_2_iris(self):
eps = 1e-7
X = eo['iris']
Y_right = eo['pdist-minkowski-3.2-iris']
Y_test1 = wpdist_no_const(X, 'minkowski', p=3.2)
assert_allclose(Y_test1, Y_right, rtol=eps)
@pytest.mark.slow
def test_pdist_minkowski_3_2_iris_float32(self):
eps = 1e-5
X = np.float32(eo['iris'])
Y_right = eo['pdist-minkowski-3.2-iris']
Y_test1 = wpdist_no_const(X, 'minkowski', p=3.2)
assert_allclose(Y_test1, Y_right, rtol=eps)
@pytest.mark.slow
def test_pdist_minkowski_3_2_iris_nonC(self):
eps = 1e-7
X = eo['iris']
Y_right = eo['pdist-minkowski-3.2-iris']
Y_test2 = wpdist_no_const(X, 'test_minkowski', p=3.2)
assert_allclose(Y_test2, Y_right, rtol=eps)
@pytest.mark.slow
def test_pdist_minkowski_5_8_iris(self):
eps = 1e-7
X = eo['iris']
Y_right = eo['pdist-minkowski-5.8-iris']
Y_test1 = wpdist_no_const(X, 'minkowski', p=5.8)
assert_allclose(Y_test1, Y_right, rtol=eps)
@pytest.mark.slow
def test_pdist_minkowski_5_8_iris_float32(self):
eps = 1e-5
X = np.float32(eo['iris'])
Y_right = eo['pdist-minkowski-5.8-iris']
Y_test1 = wpdist_no_const(X, 'minkowski', p=5.8)
assert_allclose(Y_test1, Y_right, rtol=eps, verbose=verbose > 2)
@pytest.mark.slow
def test_pdist_minkowski_5_8_iris_nonC(self):
eps = 1e-7
X = eo['iris']
Y_right = eo['pdist-minkowski-5.8-iris']
Y_test2 = wpdist_no_const(X, 'test_minkowski', p=5.8)
assert_allclose(Y_test2, Y_right, rtol=eps)
def test_pdist_mahalanobis(self):
# 1-dimensional observations
x = np.array([2.0, 2.0, 3.0, 5.0]).reshape(-1, 1)
dist = pdist(x, metric='mahalanobis')
assert_allclose(dist, [0.0, np.sqrt(0.5), np.sqrt(4.5),
np.sqrt(0.5), np.sqrt(4.5), np.sqrt(2.0)])
# 2-dimensional observations
x = np.array([[0, 0], [-1, 0], [0, 2], [1, 0], [0, -2]])
dist = pdist(x, metric='mahalanobis')
rt2 = np.sqrt(2)
assert_allclose(dist, [rt2, rt2, rt2, rt2, 2, 2 * rt2, 2, 2, 2 * rt2, 2])
# Too few observations
with pytest.raises(ValueError):
wpdist([[0, 1], [2, 3]], metric='mahalanobis')
def test_pdist_hamming_random(self):
eps = 1e-15
X = eo['pdist-boolean-inp']
Y_right = eo['pdist-hamming']
Y_test1 = wpdist(X, 'hamming')
assert_allclose(Y_test1, Y_right, rtol=eps)
def test_pdist_hamming_random_float32(self):
eps = 1e-15
X = np.float32(eo['pdist-boolean-inp'])
Y_right = eo['pdist-hamming']
Y_test1 = wpdist(X, 'hamming')
assert_allclose(Y_test1, Y_right, rtol=eps)
def test_pdist_hamming_random_nonC(self):
eps = 1e-15
X = eo['pdist-boolean-inp']
Y_right = eo['pdist-hamming']
Y_test2 = wpdist(X, 'test_hamming')
assert_allclose(Y_test2, Y_right, rtol=eps)
def test_pdist_dhamming_random(self):
eps = 1e-15
X = np.float64(eo['pdist-boolean-inp'])
Y_right = eo['pdist-hamming']
Y_test1 = wpdist(X, 'hamming')
assert_allclose(Y_test1, Y_right, rtol=eps)
def test_pdist_dhamming_random_float32(self):
eps = 1e-15
X = np.float32(eo['pdist-boolean-inp'])
Y_right = eo['pdist-hamming']
Y_test1 = wpdist(X, 'hamming')
assert_allclose(Y_test1, Y_right, rtol=eps)
def test_pdist_dhamming_random_nonC(self):
eps = 1e-15
X = np.float64(eo['pdist-boolean-inp'])
Y_right = eo['pdist-hamming']
Y_test2 = wpdist(X, 'test_hamming')
assert_allclose(Y_test2, Y_right, rtol=eps)
def test_pdist_jensenshannon_random(self):
eps = 1e-11
X = eo['pdist-double-inp']
Y_right = eo['pdist-jensenshannon']
Y_test1 = pdist(X, 'jensenshannon')
assert_allclose(Y_test1, Y_right, rtol=eps)
def test_pdist_jensenshannon_random_float32(self):
eps = 1e-8
X = np.float32(eo['pdist-double-inp'])
Y_right = eo['pdist-jensenshannon']
Y_test1 = pdist(X, 'jensenshannon')
assert_allclose(Y_test1, Y_right, rtol=eps, verbose=verbose > 2)
def test_pdist_jensenshannon_random_nonC(self):
eps = 1e-11
X = eo['pdist-double-inp']
Y_right = eo['pdist-jensenshannon']
Y_test2 = pdist(X, 'test_jensenshannon')
assert_allclose(Y_test2, Y_right, rtol=eps)
def test_pdist_jensenshannon_iris(self):
if _is_32bit():
# Test failing on 32-bit Linux on Azure otherwise, see gh-12810
eps = 2.5e-10
else:
eps = 1e-12
X = eo['iris']
Y_right = eo['pdist-jensenshannon-iris']
Y_test1 = pdist(X, 'jensenshannon')
assert_allclose(Y_test1, Y_right, atol=eps)
def test_pdist_jensenshannon_iris_float32(self):
eps = 1e-06
X = np.float32(eo['iris'])
Y_right = eo['pdist-jensenshannon-iris']
Y_test1 = pdist(X, 'jensenshannon')
assert_allclose(Y_test1, Y_right, atol=eps, verbose=verbose > 2)
def test_pdist_jensenshannon_iris_nonC(self):
eps = 5e-5
X = eo['iris']
Y_right = eo['pdist-jensenshannon-iris']
Y_test2 = pdist(X, 'test_jensenshannon')
assert_allclose(Y_test2, Y_right, rtol=eps)
def test_pdist_matching_mtica1(self):
# Test matching(*,*) with mtica example #1 (nums).
m = wmatching(np.array([1, 0, 1, 1, 0]),
np.array([1, 1, 0, 1, 1]))
m2 = wmatching(np.array([1, 0, 1, 1, 0], dtype=bool),
np.array([1, 1, 0, 1, 1], dtype=bool))
assert_allclose(m, 0.6, rtol=0, atol=1e-10)
assert_allclose(m2, 0.6, rtol=0, atol=1e-10)
def test_pdist_matching_mtica2(self):
# Test matching(*,*) with mtica example #2.
m = wmatching(np.array([1, 0, 1]),
np.array([1, 1, 0]))
m2 = wmatching(np.array([1, 0, 1], dtype=bool),
np.array([1, 1, 0], dtype=bool))
assert_allclose(m, 2 / 3, rtol=0, atol=1e-10)
assert_allclose(m2, 2 / 3, rtol=0, atol=1e-10)
def test_pdist_yule_mtica1(self):
m = wyule(np.array([1, 0, 1, 1, 0]),
np.array([1, 1, 0, 1, 1]))
m2 = wyule(np.array([1, 0, 1, 1, 0], dtype=bool),
np.array([1, 1, 0, 1, 1], dtype=bool))
if verbose > 2:
print(m)
assert_allclose(m, 2, rtol=0, atol=1e-10)
assert_allclose(m2, 2, rtol=0, atol=1e-10)
def test_pdist_yule_mtica2(self):
m = wyule(np.array([1, 0, 1]),
np.array([1, 1, 0]))
m2 = wyule(np.array([1, 0, 1], dtype=bool),
np.array([1, 1, 0], dtype=bool))
if verbose > 2:
print(m)
assert_allclose(m, 2, rtol=0, atol=1e-10)
assert_allclose(m2, 2, rtol=0, atol=1e-10)
def test_pdist_dice_mtica1(self):
m = wdice(np.array([1, 0, 1, 1, 0]),
np.array([1, 1, 0, 1, 1]))
m2 = wdice(np.array([1, 0, 1, 1, 0], dtype=bool),
np.array([1, 1, 0, 1, 1], dtype=bool))
if verbose > 2:
print(m)
assert_allclose(m, 3 / 7, rtol=0, atol=1e-10)
assert_allclose(m2, 3 / 7, rtol=0, atol=1e-10)
def test_pdist_dice_mtica2(self):
m = wdice(np.array([1, 0, 1]),
np.array([1, 1, 0]))
m2 = wdice(np.array([1, 0, 1], dtype=bool),
np.array([1, 1, 0], dtype=bool))
if verbose > 2:
print(m)
assert_allclose(m, 0.5, rtol=0, atol=1e-10)
assert_allclose(m2, 0.5, rtol=0, atol=1e-10)
def test_pdist_sokalsneath_mtica1(self):
m = sokalsneath(np.array([1, 0, 1, 1, 0]),
np.array([1, 1, 0, 1, 1]))
m2 = sokalsneath(np.array([1, 0, 1, 1, 0], dtype=bool),
np.array([1, 1, 0, 1, 1], dtype=bool))
if verbose > 2:
print(m)
assert_allclose(m, 3 / 4, rtol=0, atol=1e-10)
assert_allclose(m2, 3 / 4, rtol=0, atol=1e-10)
def test_pdist_sokalsneath_mtica2(self):
m = wsokalsneath(np.array([1, 0, 1]),
np.array([1, 1, 0]))
m2 = wsokalsneath(np.array([1, 0, 1], dtype=bool),
np.array([1, 1, 0], dtype=bool))
if verbose > 2:
print(m)
assert_allclose(m, 4 / 5, rtol=0, atol=1e-10)
assert_allclose(m2, 4 / 5, rtol=0, atol=1e-10)
def test_pdist_rogerstanimoto_mtica1(self):
m = wrogerstanimoto(np.array([1, 0, 1, 1, 0]),
np.array([1, 1, 0, 1, 1]))
m2 = wrogerstanimoto(np.array([1, 0, 1, 1, 0], dtype=bool),
np.array([1, 1, 0, 1, 1], dtype=bool))
if verbose > 2:
print(m)
assert_allclose(m, 3 / 4, rtol=0, atol=1e-10)
assert_allclose(m2, 3 / 4, rtol=0, atol=1e-10)
def test_pdist_rogerstanimoto_mtica2(self):
m = wrogerstanimoto(np.array([1, 0, 1]),
np.array([1, 1, 0]))
m2 = wrogerstanimoto(np.array([1, 0, 1], dtype=bool),
np.array([1, 1, 0], dtype=bool))
if verbose > 2:
print(m)
assert_allclose(m, 4 / 5, rtol=0, atol=1e-10)
assert_allclose(m2, 4 / 5, rtol=0, atol=1e-10)
def test_pdist_russellrao_mtica1(self):
m = wrussellrao(np.array([1, 0, 1, 1, 0]),
np.array([1, 1, 0, 1, 1]))
m2 = wrussellrao(np.array([1, 0, 1, 1, 0], dtype=bool),
np.array([1, 1, 0, 1, 1], dtype=bool))
if verbose > 2:
print(m)
assert_allclose(m, 3 / 5, rtol=0, atol=1e-10)
assert_allclose(m2, 3 / 5, rtol=0, atol=1e-10)
def test_pdist_russellrao_mtica2(self):
m = wrussellrao(np.array([1, 0, 1]),
np.array([1, 1, 0]))
m2 = wrussellrao(np.array([1, 0, 1], dtype=bool),
np.array([1, 1, 0], dtype=bool))
if verbose > 2:
print(m)
assert_allclose(m, 2 / 3, rtol=0, atol=1e-10)
assert_allclose(m2, 2 / 3, rtol=0, atol=1e-10)
@pytest.mark.slow
def test_pdist_canberra_match(self):
D = eo['iris']
if verbose > 2:
print(D.shape, D.dtype)
eps = 1e-15
y1 = wpdist_no_const(D, "canberra")
y2 = wpdist_no_const(D, "test_canberra")
assert_allclose(y1, y2, rtol=eps, verbose=verbose > 2)
def test_pdist_canberra_ticket_711(self):
# Test pdist(X, 'canberra') to see if Canberra gives the right result
# as reported on gh-1238.
eps = 1e-8
pdist_y = wpdist_no_const(([3.3], [3.4]), "canberra")
right_y = 0.01492537
assert_allclose(pdist_y, right_y, atol=eps, verbose=verbose > 2)
def test_pdist_custom_notdouble(self):
# tests that when using a custom metric the data type is not altered
class myclass:
pass
def _my_metric(x, y):
if not isinstance(x[0], myclass) or not isinstance(y[0], myclass):
raise ValueError("Type has been changed")
return 1.123
data = np.array([[myclass()], [myclass()]], dtype=object)
pdist_y = pdist(data, metric=_my_metric)
right_y = 1.123
assert_equal(pdist_y, right_y, verbose=verbose > 2)
def _check_calling_conventions(self, X, metric, eps=1e-07, **kwargs):
# helper function for test_pdist_calling_conventions
try:
y1 = pdist(X, metric=metric, **kwargs)
y2 = pdist(X, metric=eval(metric), **kwargs)
y3 = pdist(X, metric="test_" + metric, **kwargs)
except Exception as e:
e_cls = e.__class__
if verbose > 2:
print(e_cls.__name__)
print(e)
with pytest.raises(e_cls):
pdist(X, metric=metric, **kwargs)
with pytest.raises(e_cls):
pdist(X, metric=eval(metric), **kwargs)
with pytest.raises(e_cls):
pdist(X, metric="test_" + metric, **kwargs)
else:
assert_allclose(y1, y2, rtol=eps, verbose=verbose > 2)
assert_allclose(y1, y3, rtol=eps, verbose=verbose > 2)
def test_pdist_calling_conventions(self, metric):
# Ensures that specifying the metric with a str or scipy function
# gives the same behaviour (i.e. same result or same exception).
# NOTE: The correctness should be checked within each metric tests.
# NOTE: Extra args should be checked with a dedicated test
for eo_name in self.rnd_eo_names:
# subsampling input data to speed-up tests
# NOTE: num samples needs to be > than dimensions for mahalanobis
X = eo[eo_name][::5, ::2]
if verbose > 2:
print("testing: ", metric, " with: ", eo_name)
if metric in {'dice', 'yule', 'matching',
'rogerstanimoto', 'russellrao', 'sokalmichener',
'sokalsneath',
'kulczynski1'} and 'bool' not in eo_name:
# python version permits non-bools e.g. for fuzzy logic
continue
self._check_calling_conventions(X, metric)
# Testing built-in metrics with extra args
if metric == "seuclidean":
V = np.var(X.astype(np.float64), axis=0, ddof=1)
self._check_calling_conventions(X, metric, V=V)
elif metric == "mahalanobis":
V = np.atleast_2d(np.cov(X.astype(np.float64).T))
VI = np.array(np.linalg.inv(V).T)
self._check_calling_conventions(X, metric, VI=VI)
def test_pdist_dtype_equivalence(self, metric):
# Tests that the result is not affected by type up-casting
eps = 1e-07
tests = [(eo['random-bool-data'], self.valid_upcasts['bool']),
(eo['random-uint-data'], self.valid_upcasts['uint']),
(eo['random-int-data'], self.valid_upcasts['int']),
(eo['random-float32-data'], self.valid_upcasts['float32'])]
for test in tests:
X1 = test[0][::5, ::2]
try:
y1 = pdist(X1, metric=metric)
except Exception as e:
e_cls = e.__class__
if verbose > 2:
print(e_cls.__name__)
print(e)
for new_type in test[1]:
X2 = new_type(X1)
with pytest.raises(e_cls):
pdist(X2, metric=metric)
else:
for new_type in test[1]:
y2 = pdist(new_type(X1), metric=metric)
assert_allclose(y1, y2, rtol=eps, verbose=verbose > 2)
@pytest.mark.thread_unsafe
def test_pdist_out(self, metric):
# Test that out parameter works properly
eps = 1e-15
X = eo['random-float32-data'][::5, ::2]
out_size = int((X.shape[0] * (X.shape[0] - 1)) / 2)
kwargs = dict()
if metric == 'minkowski':
kwargs['p'] = 1.23
out1 = np.empty(out_size, dtype=np.float64)
with maybe_deprecated(metric):
Y_right = pdist(X, metric, **kwargs)
with maybe_deprecated(metric):
Y_test1 = pdist(X, metric, out=out1, **kwargs)
# test that output is numerically equivalent
assert_allclose(Y_test1, Y_right, rtol=eps)
# test that Y_test1 and out1 are the same object
assert_(Y_test1 is out1)
# test for incorrect shape
out2 = np.empty(out_size + 3, dtype=np.float64)
with pytest.raises(ValueError):
with maybe_deprecated(metric):
pdist(X, metric, out=out2, **kwargs)
# test for (C-)contiguous output
out3 = np.empty(2 * out_size, dtype=np.float64)[::2]
with pytest.raises(ValueError):
with maybe_deprecated(metric):
pdist(X, metric, out=out3, **kwargs)
# test for incorrect dtype
out5 = np.empty(out_size, dtype=np.int64)
with pytest.raises(ValueError):
with maybe_deprecated(metric):
pdist(X, metric, out=out5, **kwargs)
@pytest.mark.thread_unsafe
def test_striding(self, metric):
# test that striding is handled correct with calls to
# _copy_array_if_base_present
eps = 1e-15
X = eo['random-float32-data'][::5, ::2]
X_copy = X.copy()
# confirm contiguity
assert_(not X.flags.c_contiguous)
assert_(X_copy.flags.c_contiguous)
kwargs = dict()
if metric == 'minkowski':
kwargs['p'] = 1.23
with maybe_deprecated(metric):
Y1 = pdist(X, metric, **kwargs)
with maybe_deprecated(metric):
Y2 = pdist(X_copy, metric, **kwargs)
# test that output is numerically equivalent
assert_allclose(Y1, Y2, rtol=eps, verbose=verbose > 2)
class TestSomeDistanceFunctions:
def setup_method(self):
# 1D arrays
x = np.array([1.0, 2.0, 3.0])
y = np.array([1.0, 1.0, 5.0])
self.cases = [(x, y)]
def test_minkowski(self):
for x, y in self.cases:
dist1 = minkowski(x, y, p=1)
assert_almost_equal(dist1, 3.0)
dist1p5 = minkowski(x, y, p=1.5)
assert_almost_equal(dist1p5, (1.0 + 2.0**1.5)**(2. / 3))
dist2 = minkowski(x, y, p=2)
assert_almost_equal(dist2, 5.0 ** 0.5)
dist0p25 = minkowski(x, y, p=0.25)
assert_almost_equal(dist0p25, (1.0 + 2.0 ** 0.25) ** 4)
# Check that casting input to minimum scalar type doesn't affect result
# (issue #10262). This could be extended to more test inputs with
# np.min_scalar_type(np.max(input_matrix)).
a = np.array([352, 916])
b = np.array([350, 660])
assert_equal(minkowski(a, b),
minkowski(a.astype('uint16'), b.astype('uint16')))
def test_euclidean(self):
for x, y in self.cases:
dist = weuclidean(x, y)
assert_almost_equal(dist, np.sqrt(5))
def test_sqeuclidean(self):
for x, y in self.cases:
dist = wsqeuclidean(x, y)
assert_almost_equal(dist, 5.0)
def test_cosine(self):
for x, y in self.cases:
dist = wcosine(x, y)
assert_almost_equal(dist, 1.0 - 18.0 / (np.sqrt(14) * np.sqrt(27)))
def test_cosine_output_dtype(self):
# Regression test for gh-19541
assert isinstance(wcorrelation([1, 1], [1, 1], centered=False), float)
assert isinstance(wcosine([1, 1], [1, 1]), float)
def test_correlation(self):
xm = np.array([-1.0, 0, 1.0])
ym = np.array([-4.0 / 3, -4.0 / 3, 5.0 - 7.0 / 3])
for x, y in self.cases:
dist = wcorrelation(x, y)
assert_almost_equal(dist, 1.0 - np.dot(xm, ym) / (norm(xm) * norm(ym)))
def test_correlation_positive(self):
# Regression test for gh-12320 (negative return value due to rounding
x = np.array([0., 0., 0., 0., 0., 0., -2., 0., 0., 0., -2., -2., -2.,
0., -2., 0., -2., 0., 0., -1., -2., 0., 1., 0., 0., -2.,
0., 0., -2., 0., -2., -2., -2., -2., -2., -2., 0.])
y = np.array([1., 1., 1., 1., 1., 1., -1., 1., 1., 1., -1., -1., -1.,
1., -1., 1., -1., 1., 1., 0., -1., 1., 2., 1., 1., -1.,
1., 1., -1., 1., -1., -1., -1., -1., -1., -1., 1.])
dist = correlation(x, y)
assert 0 <= dist <= 10 * np.finfo(np.float64).eps
@pytest.mark.thread_unsafe
@pytest.mark.filterwarnings('ignore:Casting complex')
@pytest.mark.parametrize("func", [correlation, cosine])
def test_corr_dep_complex(self, func):
x = [1+0j, 2+0j]
y = [3+0j, 4+0j]
with pytest.deprecated_call(match="Complex `u` and `v` are deprecated"):
func(x, y)
def test_mahalanobis(self):
x = np.array([1.0, 2.0, 3.0])
y = np.array([1.0, 1.0, 5.0])
vi = np.array([[2.0, 1.0, 0.0], [1.0, 2.0, 1.0], [0.0, 1.0, 2.0]])
for x, y in self.cases:
dist = mahalanobis(x, y, vi)
assert_almost_equal(dist, np.sqrt(6.0))
class TestSquareForm:
checked_dtypes = [np.float64, np.float32, np.int32, np.int8, bool]
def test_squareform_matrix(self):
for dtype in self.checked_dtypes:
self.check_squareform_matrix(dtype)
def test_squareform_vector(self):
for dtype in self.checked_dtypes:
self.check_squareform_vector(dtype)
def check_squareform_matrix(self, dtype):
A = np.zeros((0, 0), dtype=dtype)
rA = squareform(A)
assert_equal(rA.shape, (0,))
assert_equal(rA.dtype, dtype)
A = np.zeros((1, 1), dtype=dtype)
rA = squareform(A)
assert_equal(rA.shape, (0,))
assert_equal(rA.dtype, dtype)
A = np.array([[0, 4.2], [4.2, 0]], dtype=dtype)
rA = squareform(A)
assert_equal(rA.shape, (1,))
assert_equal(rA.dtype, dtype)
assert_array_equal(rA, np.array([4.2], dtype=dtype))
def check_squareform_vector(self, dtype):
v = np.zeros((0,), dtype=dtype)
rv = squareform(v)
assert_equal(rv.shape, (1, 1))
assert_equal(rv.dtype, dtype)
assert_array_equal(rv, [[0]])
v = np.array([8.3], dtype=dtype)
rv = squareform(v)
assert_equal(rv.shape, (2, 2))
assert_equal(rv.dtype, dtype)
assert_array_equal(rv, np.array([[0, 8.3], [8.3, 0]], dtype=dtype))
def test_squareform_multi_matrix(self):
for n in range(2, 5):
self.check_squareform_multi_matrix(n)
def check_squareform_multi_matrix(self, n):
X = np.random.rand(n, 4)
Y = wpdist_no_const(X)
assert_equal(len(Y.shape), 1)
A = squareform(Y)
Yr = squareform(A)
s = A.shape
k = 0
if verbose >= 3:
print(A.shape, Y.shape, Yr.shape)
assert_equal(len(s), 2)
assert_equal(len(Yr.shape), 1)
assert_equal(s[0], s[1])
for i in range(0, s[0]):
for j in range(i + 1, s[1]):
if i != j:
assert_equal(A[i, j], Y[k])
k += 1
else:
assert_equal(A[i, j], 0)
class TestNumObsY:
def test_num_obs_y_multi_matrix(self):
for n in range(2, 10):
X = np.random.rand(n, 4)
Y = wpdist_no_const(X)
assert_equal(num_obs_y(Y), n)
def test_num_obs_y_1(self):
# Tests num_obs_y(y) on a condensed distance matrix over 1
# observations. Expecting exception.
with pytest.raises(ValueError):
self.check_y(1)
def test_num_obs_y_2(self):
# Tests num_obs_y(y) on a condensed distance matrix over 2
# observations.
assert_(self.check_y(2))
def test_num_obs_y_3(self):
assert_(self.check_y(3))
def test_num_obs_y_4(self):
assert_(self.check_y(4))
def test_num_obs_y_5_10(self):
for i in range(5, 16):
self.minit(i)
def test_num_obs_y_2_100(self):
# Tests num_obs_y(y) on 100 improper condensed distance matrices.
# Expecting exception.
a = set()
for n in range(2, 16):
a.add(n * (n - 1) / 2)
for i in range(5, 105):
if i not in a:
with pytest.raises(ValueError):
self.bad_y(i)
def minit(self, n):
assert_(self.check_y(n))
def bad_y(self, n):
y = np.random.rand(n)
return num_obs_y(y)
def check_y(self, n):
return num_obs_y(self.make_y(n)) == n
def make_y(self, n):
return np.random.rand((n * (n - 1)) // 2)
class TestNumObsDM:
def test_num_obs_dm_multi_matrix(self):
for n in range(1, 10):
X = np.random.rand(n, 4)
Y = wpdist_no_const(X)
A = squareform(Y)
if verbose >= 3:
print(A.shape, Y.shape)
assert_equal(num_obs_dm(A), n)
def test_num_obs_dm_0(self):
# Tests num_obs_dm(D) on a 0x0 distance matrix. Expecting exception.
assert_(self.check_D(0))
def test_num_obs_dm_1(self):
# Tests num_obs_dm(D) on a 1x1 distance matrix.
assert_(self.check_D(1))
def test_num_obs_dm_2(self):
assert_(self.check_D(2))
def test_num_obs_dm_3(self):
assert_(self.check_D(2))
def test_num_obs_dm_4(self):
assert_(self.check_D(4))
def check_D(self, n):
return num_obs_dm(self.make_D(n)) == n
def make_D(self, n):
return np.random.rand(n, n)
def is_valid_dm_throw(D):
return is_valid_dm(D, throw=True)
class TestIsValidDM:
def test_is_valid_dm_improper_shape_1D_E(self):
D = np.zeros((5,), dtype=np.float64)
with pytest.raises(ValueError):
is_valid_dm_throw(D)
def test_is_valid_dm_improper_shape_1D_F(self):
D = np.zeros((5,), dtype=np.float64)
assert_equal(is_valid_dm(D), False)
def test_is_valid_dm_improper_shape_3D_E(self):
D = np.zeros((3, 3, 3), dtype=np.float64)
with pytest.raises(ValueError):
is_valid_dm_throw(D)
def test_is_valid_dm_improper_shape_3D_F(self):
D = np.zeros((3, 3, 3), dtype=np.float64)
assert_equal(is_valid_dm(D), False)
def test_is_valid_dm_nonzero_diagonal_E(self):
y = np.random.rand(10)
D = squareform(y)
for i in range(0, 5):
D[i, i] = 2.0
with pytest.raises(ValueError):
is_valid_dm_throw(D)
def test_is_valid_dm_nonzero_diagonal_F(self):
y = np.random.rand(10)
D = squareform(y)
for i in range(0, 5):
D[i, i] = 2.0
assert_equal(is_valid_dm(D), False)
def test_is_valid_dm_asymmetric_E(self):
y = np.random.rand(10)
D = squareform(y)
D[1, 3] = D[3, 1] + 1
with pytest.raises(ValueError):
is_valid_dm_throw(D)
def test_is_valid_dm_asymmetric_F(self):
y = np.random.rand(10)
D = squareform(y)
D[1, 3] = D[3, 1] + 1
assert_equal(is_valid_dm(D), False)
def test_is_valid_dm_correct_1_by_1(self):
D = np.zeros((1, 1), dtype=np.float64)
assert_equal(is_valid_dm(D), True)
def test_is_valid_dm_correct_2_by_2(self):
y = np.random.rand(1)
D = squareform(y)
assert_equal(is_valid_dm(D), True)
def test_is_valid_dm_correct_3_by_3(self):
y = np.random.rand(3)
D = squareform(y)
assert_equal(is_valid_dm(D), True)
def test_is_valid_dm_correct_4_by_4(self):
y = np.random.rand(6)
D = squareform(y)
assert_equal(is_valid_dm(D), True)
def test_is_valid_dm_correct_5_by_5(self):
y = np.random.rand(10)
D = squareform(y)
assert_equal(is_valid_dm(D), True)
def is_valid_y_throw(y):
return is_valid_y(y, throw=True)
class TestIsValidY:
# If test case name ends on "_E" then an exception is expected for the
# given input, if it ends in "_F" then False is expected for the is_valid_y
# check. Otherwise the input is expected to be valid.
def test_is_valid_y_improper_shape_2D_E(self):
y = np.zeros((3, 3,), dtype=np.float64)
with pytest.raises(ValueError):
is_valid_y_throw(y)
def test_is_valid_y_improper_shape_2D_F(self):
y = np.zeros((3, 3,), dtype=np.float64)
assert_equal(is_valid_y(y), False)
def test_is_valid_y_improper_shape_3D_E(self):
y = np.zeros((3, 3, 3), dtype=np.float64)
with pytest.raises(ValueError):
is_valid_y_throw(y)
def test_is_valid_y_improper_shape_3D_F(self):
y = np.zeros((3, 3, 3), dtype=np.float64)
assert_equal(is_valid_y(y), False)
def test_is_valid_y_correct_2_by_2(self):
y = self.correct_n_by_n(2)
assert_equal(is_valid_y(y), True)
def test_is_valid_y_correct_3_by_3(self):
y = self.correct_n_by_n(3)
assert_equal(is_valid_y(y), True)
def test_is_valid_y_correct_4_by_4(self):
y = self.correct_n_by_n(4)
assert_equal(is_valid_y(y), True)
def test_is_valid_y_correct_5_by_5(self):
y = self.correct_n_by_n(5)
assert_equal(is_valid_y(y), True)
def test_is_valid_y_2_100(self):
a = set()
for n in range(2, 16):
a.add(n * (n - 1) / 2)
for i in range(5, 105):
if i not in a:
with pytest.raises(ValueError):
self.bad_y(i)
def bad_y(self, n):
y = np.random.rand(n)
return is_valid_y(y, throw=True)
def correct_n_by_n(self, n):
y = np.random.rand((n * (n - 1)) // 2)
return y
@pytest.mark.parametrize("p", [-10.0, -0.5, 0.0])
def test_bad_p(p):
# Raise ValueError if p <=0.
with pytest.raises(ValueError):
minkowski([1, 2], [3, 4], p)
with pytest.raises(ValueError):
minkowski([1, 2], [3, 4], p, [1, 1])
def test_sokalsneath_all_false():
# Regression test for ticket #876
with pytest.raises(ValueError):
sokalsneath([False, False, False], [False, False, False])
def test_canberra():
# Regression test for ticket #1430.
assert_equal(wcanberra([1, 2, 3], [2, 4, 6]), 1)
assert_equal(wcanberra([1, 1, 0, 0], [1, 0, 1, 0]), 2)
def test_braycurtis():
# Regression test for ticket #1430.
assert_almost_equal(wbraycurtis([1, 2, 3], [2, 4, 6]), 1. / 3, decimal=15)
assert_almost_equal(wbraycurtis([1, 1, 0, 0], [1, 0, 1, 0]), 0.5, decimal=15)
def test_euclideans():
# Regression test for ticket #1328.
x1 = np.array([1, 1, 1])
x2 = np.array([0, 0, 0])
# Basic test of the calculation.
assert_almost_equal(wsqeuclidean(x1, x2), 3.0, decimal=14)
assert_almost_equal(weuclidean(x1, x2), np.sqrt(3), decimal=14)
# Check flattening for (1, N) or (N, 1) inputs
with pytest.raises(ValueError, match="Input vector should be 1-D"):
weuclidean(x1[np.newaxis, :], x2[np.newaxis, :]), np.sqrt(3)
with pytest.raises(ValueError, match="Input vector should be 1-D"):
wsqeuclidean(x1[np.newaxis, :], x2[np.newaxis, :])
with pytest.raises(ValueError, match="Input vector should be 1-D"):
wsqeuclidean(x1[:, np.newaxis], x2[:, np.newaxis])
# Distance metrics only defined for vectors (= 1-D)
x = np.arange(4).reshape(2, 2)
with pytest.raises(ValueError):
weuclidean(x, x)
with pytest.raises(ValueError):
wsqeuclidean(x, x)
# Another check, with random data.
rs = np.random.RandomState(1234567890)
x = rs.rand(10)
y = rs.rand(10)
d1 = weuclidean(x, y)
d2 = wsqeuclidean(x, y)
assert_almost_equal(d1**2, d2, decimal=14)
def test_hamming_unequal_length():
# Regression test for gh-4290.
x = [0, 0, 1]
y = [1, 0, 1, 0]
# Used to give an AttributeError from ndarray.mean called on bool
with pytest.raises(ValueError):
whamming(x, y)
def test_hamming_unequal_length_with_w():
u = [0, 0, 1]
v = [0, 0, 1]
w = [1, 0, 1, 0]
msg = "'w' should have the same length as 'u' and 'v'."
with pytest.raises(ValueError, match=msg):
whamming(u, v, w)
def test_hamming_string_array():
# https://github.com/scikit-learn/scikit-learn/issues/4014
a = np.array(['eggs', 'spam', 'spam', 'eggs', 'spam', 'spam', 'spam',
'spam', 'spam', 'spam', 'spam', 'eggs', 'eggs', 'spam',
'eggs', 'eggs', 'eggs', 'eggs', 'eggs', 'spam'],
dtype='|S4')
b = np.array(['eggs', 'spam', 'spam', 'eggs', 'eggs', 'spam', 'spam',
'spam', 'spam', 'eggs', 'spam', 'eggs', 'spam', 'eggs',
'spam', 'spam', 'eggs', 'spam', 'spam', 'eggs'],
dtype='|S4')
desired = 0.45
assert_allclose(whamming(a, b), desired)
def test_minkowski_w():
# Regression test for gh-8142.
arr_in = np.array([[83.33333333, 100., 83.33333333, 100., 36.,
60., 90., 150., 24., 48.],
[83.33333333, 100., 83.33333333, 100., 36.,
60., 90., 150., 24., 48.]])
p0 = pdist(arr_in, metric='minkowski', p=1, w=None)
c0 = cdist(arr_in, arr_in, metric='minkowski', p=1, w=None)
p1 = pdist(arr_in, metric='minkowski', p=1)
c1 = cdist(arr_in, arr_in, metric='minkowski', p=1)
assert_allclose(p0, p1, rtol=1e-15)
assert_allclose(c0, c1, rtol=1e-15)
def test_sqeuclidean_dtypes():
# Assert that sqeuclidean returns the right types of values.
# Integer types should be converted to floating for stability.
# Floating point types should be the same as the input.
x = [1, 2, 3]
y = [4, 5, 6]
for dtype in [np.int8, np.int16, np.int32, np.int64]:
d = wsqeuclidean(np.asarray(x, dtype=dtype), np.asarray(y, dtype=dtype))
assert_(np.issubdtype(d.dtype, np.floating))
for dtype in [np.uint8, np.uint16, np.uint32, np.uint64]:
umax = np.iinfo(dtype).max
d1 = wsqeuclidean([0], np.asarray([umax], dtype=dtype))
d2 = wsqeuclidean(np.asarray([umax], dtype=dtype), [0])
assert_equal(d1, d2)
assert_equal(d1, np.float64(umax)**2)
dtypes = [np.float32, np.float64, np.complex64, np.complex128]
for dtype in ['float16', 'float128']:
# These aren't present in older numpy versions; float128 may also not
# be present on all platforms.
if hasattr(np, dtype):
dtypes.append(getattr(np, dtype))
for dtype in dtypes:
d = wsqeuclidean(np.asarray(x, dtype=dtype), np.asarray(y, dtype=dtype))
assert_equal(d.dtype, dtype)
@pytest.mark.thread_unsafe
def test_sokalmichener():
# Test that sokalmichener has the same result for bool and int inputs.
p = [True, True, False]
q = [True, False, True]
x = [int(b) for b in p]
y = [int(b) for b in q]
with pytest.deprecated_call():
dist1 = sokalmichener(p, q)
with pytest.deprecated_call():
dist2 = sokalmichener(x, y)
# These should be exactly the same.
assert_equal(dist1, dist2)
@pytest.mark.thread_unsafe
def test_sokalmichener_with_weight():
# from: | 1 | | 0 |
# to: | 1 | | 1 |
# weight| | 1 | | 0.2
ntf = 0 * 1 + 0 * 0.2
nft = 0 * 1 + 1 * 0.2
ntt = 1 * 1 + 0 * 0.2
nff = 0 * 1 + 0 * 0.2
expected = 2 * (nft + ntf) / (ntt + nff + 2 * (nft + ntf))
assert_almost_equal(expected, 0.2857143)
with pytest.deprecated_call():
actual = sokalmichener([1, 0], [1, 1], w=[1, 0.2])
assert_almost_equal(expected, actual)
a1 = [False, False, True, True, True, False, False, True, True, True, True,
True, True, False, True, False, False, False, True, True]
a2 = [True, True, True, False, False, True, True, True, False, True,
True, True, True, True, False, False, False, True, True, True]
for w in [0.05, 0.1, 1.0, 20.0]:
with pytest.deprecated_call():
assert_almost_equal(sokalmichener(a2, a1, [w]), 0.6666666666666666)
@pytest.mark.thread_unsafe
def test_modifies_input(metric):
# test whether cdist or pdist modifies input arrays
X1 = np.asarray([[1., 2., 3.],
[1.2, 2.3, 3.4],
[2.2, 2.3, 4.4],
[22.2, 23.3, 44.4]])
X1_copy = X1.copy()
with maybe_deprecated(metric):
cdist(X1, X1, metric)
with maybe_deprecated(metric):
pdist(X1, metric)
assert_array_equal(X1, X1_copy)
@pytest.mark.thread_unsafe
def test_Xdist_deprecated_args(metric):
# testing both cdist and pdist deprecated warnings
X1 = np.asarray([[1., 2., 3.],
[1.2, 2.3, 3.4],
[2.2, 2.3, 4.4],
[22.2, 23.3, 44.4]])
with pytest.raises(TypeError):
cdist(X1, X1, metric, 2.)
with pytest.raises(TypeError):
pdist(X1, metric, 2.)
for arg in ["p", "V", "VI"]:
kwargs = {arg: "foo"}
if ((arg == "V" and metric == "seuclidean")
or (arg == "VI" and metric == "mahalanobis")
or (arg == "p" and metric == "minkowski")):
continue
with pytest.raises(TypeError):
with maybe_deprecated(metric):
cdist(X1, X1, metric, **kwargs)
with pytest.raises(TypeError):
with maybe_deprecated(metric):
pdist(X1, metric, **kwargs)
@pytest.mark.thread_unsafe
def test_Xdist_non_negative_weights(metric):
X = eo['random-float32-data'][::5, ::2]
w = np.ones(X.shape[1])
w[::5] = -w[::5]
if metric in ['seuclidean', 'mahalanobis', 'jensenshannon']:
pytest.skip("not applicable")
for m in [metric, eval(metric), "test_" + metric]:
with pytest.raises(ValueError):
with maybe_deprecated(metric):
pdist(X, m, w=w)
with pytest.raises(ValueError):
with maybe_deprecated(metric):
cdist(X, X, m, w=w)
def test__validate_vector():
x = [1, 2, 3]
y = _validate_vector(x)
assert_array_equal(y, x)
y = _validate_vector(x, dtype=np.float64)
assert_array_equal(y, x)
assert_equal(y.dtype, np.float64)
x = [1]
y = _validate_vector(x)
assert_equal(y.ndim, 1)
assert_equal(y, x)
x = 1
with pytest.raises(ValueError, match="Input vector should be 1-D"):
_validate_vector(x)
x = np.arange(5).reshape(1, -1, 1)
with pytest.raises(ValueError, match="Input vector should be 1-D"):
_validate_vector(x)
x = [[1, 2], [3, 4]]
with pytest.raises(ValueError, match="Input vector should be 1-D"):
_validate_vector(x)
def test_yule_all_same():
# Test yule avoids a divide by zero when exactly equal
x = np.ones((2, 6), dtype=bool)
d = wyule(x[0], x[0])
assert d == 0.0
d = pdist(x, 'yule')
assert_equal(d, [0.0])
d = cdist(x[:1], x[:1], 'yule')
assert_equal(d, [[0.0]])
def test_jensenshannon():
assert_almost_equal(jensenshannon([1.0, 0.0, 0.0], [0.0, 1.0, 0.0], 2.0),
1.0)
assert_almost_equal(jensenshannon([1.0, 0.0], [0.5, 0.5]),
0.46450140402245893)
assert_almost_equal(jensenshannon([1.0, 0.0, 0.0], [1.0, 0.0, 0.0]), 0.0)
assert_almost_equal(jensenshannon([[1.0, 2.0]], [[0.5, 1.5]], axis=0),
[0.0, 0.0])
assert_almost_equal(jensenshannon([[1.0, 2.0]], [[0.5, 1.5]], axis=1),
[0.0649045])
assert_almost_equal(jensenshannon([[1.0, 2.0]], [[0.5, 1.5]], axis=0,
keepdims=True), [[0.0, 0.0]])
assert_almost_equal(jensenshannon([[1.0, 2.0]], [[0.5, 1.5]], axis=1,
keepdims=True), [[0.0649045]])
a = np.array([[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]])
b = np.array([[13, 14, 15, 16],
[17, 18, 19, 20],
[21, 22, 23, 24]])
assert_almost_equal(jensenshannon(a, b, axis=0),
[0.1954288, 0.1447697, 0.1138377, 0.0927636])
assert_almost_equal(jensenshannon(a, b, axis=1),
[0.1402339, 0.0399106, 0.0201815])
def test_gh_17703():
arr_1 = np.array([1, 0, 0])
arr_2 = np.array([2, 0, 0])
expected = dice(arr_1, arr_2)
actual = pdist([arr_1, arr_2], metric='dice')
assert_allclose(actual, expected)
actual = cdist(np.atleast_2d(arr_1),
np.atleast_2d(arr_2), metric='dice')
assert_allclose(actual, expected)
@pytest.mark.thread_unsafe
def test_immutable_input(metric):
if metric in ("jensenshannon", "mahalanobis", "seuclidean"):
pytest.skip("not applicable")
x = np.arange(10, dtype=np.float64)
x.setflags(write=False)
with maybe_deprecated(metric):
getattr(scipy.spatial.distance, metric)(x, x, w=x)
class TestJaccard:
def test_pdist_jaccard_random(self):
eps = 1e-8
X = eo['pdist-boolean-inp']
Y_right = eo['pdist-jaccard']
Y_test1 = wpdist(X, 'jaccard')
assert_allclose(Y_test1, Y_right, rtol=eps)
def test_pdist_jaccard_random_float32(self):
eps = 1e-8
X = np.float32(eo['pdist-boolean-inp'])
Y_right = eo['pdist-jaccard']
Y_test1 = wpdist(X, 'jaccard')
assert_allclose(Y_test1, Y_right, rtol=eps)
def test_pdist_jaccard_random_nonC(self):
eps = 1e-8
X = eo['pdist-boolean-inp']
Y_right = eo['pdist-jaccard']
Y_test2 = wpdist(X, 'test_jaccard')
assert_allclose(Y_test2, Y_right, rtol=eps)
def test_pdist_djaccard_random(self):
eps = 1e-8
X = np.float64(eo['pdist-boolean-inp'])
Y_right = eo['pdist-jaccard']
Y_test1 = wpdist(X, 'jaccard')
assert_allclose(Y_test1, Y_right, rtol=eps)
def test_pdist_djaccard_random_float32(self):
eps = 1e-8
X = np.float32(eo['pdist-boolean-inp'])
Y_right = eo['pdist-jaccard']
Y_test1 = wpdist(X, 'jaccard')
assert_allclose(Y_test1, Y_right, rtol=eps)
def test_pdist_djaccard_allzeros(self):
eps = 1e-15
Y = pdist(np.zeros((5, 3)), 'jaccard')
assert_allclose(np.zeros(10), Y, rtol=eps)
def test_pdist_djaccard_random_nonC(self):
eps = 1e-8
X = np.float64(eo['pdist-boolean-inp'])
Y_right = eo['pdist-jaccard']
Y_test2 = wpdist(X, 'test_jaccard')
assert_allclose(Y_test2, Y_right, rtol=eps)
def test_pdist_djaccard_allzeros_nonC(self):
eps = 1e-15
Y = pdist(np.zeros((5, 3)), 'test_jaccard')
assert_allclose(np.zeros(10), Y, rtol=eps)
def test_pdist_jaccard_mtica1(self):
m = wjaccard(np.array([1, 0, 1, 1, 0]),
np.array([1, 1, 0, 1, 1]))
m2 = wjaccard(np.array([1, 0, 1, 1, 0], dtype=bool),
np.array([1, 1, 0, 1, 1], dtype=bool))
assert_allclose(m, 0.6, rtol=0, atol=1e-10)
assert_allclose(m2, 0.6, rtol=0, atol=1e-10)
def test_pdist_jaccard_mtica2(self):
m = wjaccard(np.array([1, 0, 1]),
np.array([1, 1, 0]))
m2 = wjaccard(np.array([1, 0, 1], dtype=bool),
np.array([1, 1, 0], dtype=bool))
assert_allclose(m, 2 / 3, rtol=0, atol=1e-10)
assert_allclose(m2, 2 / 3, rtol=0, atol=1e-10)
def test_non_01_input(self):
# Non-0/1 numeric input should be cast to bool before computation.
# See gh-21176.
x = np.array([-10, 2.5, 0]) # [True, True, False]
y = np.array([ 2, -5, 2]) # [True, True, True]
eps = np.finfo(float).eps
assert_allclose(jaccard(x, y), 1/3, rtol=eps)
assert_allclose(cdist([x], [y], 'jaccard'), [[1/3]])
assert_allclose(pdist([x, y], 'jaccard'), [1/3])
class TestChebyshev:
def test_pdist_chebyshev_random(self):
eps = 1e-8
X = eo['pdist-double-inp']
Y_right = eo['pdist-chebyshev']
Y_test1 = pdist(X, 'chebyshev')
assert_allclose(Y_test1, Y_right, rtol=eps)
def test_pdist_chebyshev_random_float32(self):
eps = 1e-7
X = np.float32(eo['pdist-double-inp'])
Y_right = eo['pdist-chebyshev']
Y_test1 = pdist(X, 'chebyshev')
assert_allclose(Y_test1, Y_right, rtol=eps, verbose=verbose > 2)
def test_pdist_chebyshev_random_nonC(self):
eps = 1e-8
X = eo['pdist-double-inp']
Y_right = eo['pdist-chebyshev']
Y_test2 = pdist(X, 'test_chebyshev')
assert_allclose(Y_test2, Y_right, rtol=eps)
def test_pdist_chebyshev_iris(self):
eps = 1e-14
X = eo['iris']
Y_right = eo['pdist-chebyshev-iris']
Y_test1 = pdist(X, 'chebyshev')
assert_allclose(Y_test1, Y_right, rtol=eps)
def test_pdist_chebyshev_iris_float32(self):
eps = 1e-5
X = np.float32(eo['iris'])
Y_right = eo['pdist-chebyshev-iris']
Y_test1 = pdist(X, 'chebyshev')
assert_allclose(Y_test1, Y_right, rtol=eps, verbose=verbose > 2)
def test_pdist_chebyshev_iris_nonC(self):
eps = 1e-14
X = eo['iris']
Y_right = eo['pdist-chebyshev-iris']
Y_test2 = pdist(X, 'test_chebyshev')
assert_allclose(Y_test2, Y_right, rtol=eps)
def test_weighted(self):
# Basic test for weighted Chebyshev. Only components with non-zero
# weight participate in the 'max'.
x = [1, 2, 3]
y = [6, 5, 4]
w = [0, 1, 5]
assert_equal(chebyshev(x, y, w), 3)
assert_equal(pdist([x, y], 'chebyshev', w=w), [3])
assert_equal(cdist([x], [y], 'chebyshev', w=w), [[3]])
def test_zero_weight(self):
# If the weight is identically zero, the distance should be zero.
x = [1, 2, 3]
y = [6, 5, 4]
w = [0, 0, 0]
assert_equal(chebyshev(x, y, w), 0)
assert_equal(pdist([x, y], 'chebyshev', w=w), [0])
assert_equal(cdist([x], [y], 'chebyshev', w=w), [[0]])
|