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
|
# ==========================================================================
#
# Copyright NumFOCUS
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0.txt
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ==========================================================================*/
import enum
import re
from typing import Optional, Union, Dict, Any, List, Tuple, Sequence, TYPE_CHECKING
from sys import stderr as system_error_stream
import numpy as np
try:
from numpy.typing import ArrayLike
except ImportError:
from numpy import ndarray as ArrayLike
import warnings
from sys import stderr as system_error_stream
import os
import builtins
fileiotype = Union[str, bytes, os.PathLike]
import itk.support.types as itkt
from .helpers import wasm_type_from_image_type, image_type_from_wasm_type
from .helpers import wasm_type_from_mesh_type, mesh_type_from_wasm_type, python_to_js
from .helpers import wasm_type_from_pointset_type, pointset_type_from_wasm_type
from .helpers import snake_to_camel_case
from .xarray import xarray_from_image, image_from_xarray
if TYPE_CHECKING:
try:
import vtk
except ImportError:
pass
__all__ = [
"output",
"image",
"set_nthreads",
"get_nthreads",
"echo",
"size",
"physical_size",
"spacing",
"origin",
"index",
"region",
"GetArrayFromImage",
"array_from_image",
"GetArrayViewFromImage",
"array_view_from_image",
"GetImageFromArray",
"image_from_array",
"GetImageViewFromArray",
"image_view_from_array",
"array_from_vector_container",
"array_view_from_vector_container",
"vector_container_from_array",
"GetArrayFromVnlVector",
"array_from_vnl_vector",
"GetVnlVectorFromArray",
"vnl_vector_from_array",
"GetArrayViewFromVnlVector",
"array_view_from_vnl_vector",
"GetVnlMatrixFromArray",
"vnl_matrix_from_array",
"GetArrayFromVnlMatrix",
"array_from_vnl_matrix",
"GetArrayViewFromVnlMatrix",
"array_view_from_vnl_matrix",
"GetArrayFromMatrix",
"array_from_matrix",
"GetMatrixFromArray",
"matrix_from_array",
"xarray_from_image",
"image_from_xarray",
"vtk_image_from_image",
"image_from_vtk_image",
"dict_from_image",
"image_from_dict",
"image_intensity_min_max",
"imwrite",
"imread",
"meshwrite",
"meshread",
"mesh_from_dict",
"dict_from_mesh",
"pointset_from_dict",
"dict_from_pointset",
"polyline_from_dict",
"dict_from_polyline",
"transform_from_dict",
"dict_from_transform",
"transformwrite",
"transformread",
"search",
"set_inputs",
"templated_class",
"pipeline",
"auto_pipeline",
"down_cast",
"template",
"class_",
"ctype",
"python_type",
"range",
"TemplateTypeError",
]
def output(input):
"""
If input object has attribute "GetOutput()" then return an itk image,
otherwise this function simply returns the input value
"""
if hasattr(input, "GetOutput"):
return input.GetOutput()
return input
def image(input):
warnings.warn(
"WrapITK warning: itk.image() is deprecated. " "Use itk.output() instead."
)
return output(input)
def set_nthreads(number_of_threads: int) -> None:
"""
Support convenient set of the number of threads.
Use example (in python):
import itk
itk.set_nthreads(4) ## use 4 threads
"""
assert number_of_threads > 0, (
"Please set a positive number of threads instead of %d" % number_of_threads
)
import itk
threader = itk.MultiThreaderBase.New()
threader.SetGlobalDefaultNumberOfThreads(number_of_threads)
def get_nthreads() -> int:
"""
Get the number of threads
"""
import itk
threader = itk.MultiThreaderBase.New()
return threader.GetGlobalDefaultNumberOfThreads()
def echo(obj, f=system_error_stream) -> None:
"""Print an object to stream
If the object has a method Print(), this method is used.
repr(obj) is used otherwise
"""
print(f, obj)
def size(image_or_filter: "itkt.ImageOrImageSource") -> Sequence[int]:
"""Return the size of an image, or of the output image of a filter
This method take care of updating the needed information
"""
# we don't need the entire output, only its size
import itk
image_or_filter.UpdateOutputInformation()
img = itk.output(image_or_filter)
return img.GetLargestPossibleRegion().GetSize()
def physical_size(image_or_filter: "itkt.ImageOrImageSource") -> Sequence[float]:
"""Return the physical size of an image, or of the output image of a filter
This method take care of updating the needed information
"""
# required because range is overloaded in this module
from builtins import range
spacing_ = spacing(image_or_filter)
size_ = size(image_or_filter)
result = []
for i in range(0, spacing_.Size()):
result.append(spacing_.GetElement(i) * size_.GetElement(i))
return result
def spacing(image_or_filter: "itkt.ImageOrImageSource") -> Sequence[float]:
"""Return the spacing of an image, or of the output image of a filter
This method take care of updating the needed information
"""
import itk
# we don't need the entire output, only its size
image_or_filter.UpdateOutputInformation()
img = itk.output(image_or_filter)
return img.GetSpacing()
def origin(image_or_filter: "itkt.ImageOrImageSource") -> Sequence[float]:
"""Return the origin of an image, or of the output image of a filter
This method take care of updating the needed information
"""
import itk
# we don't need the entire output, only its size
image_or_filter.UpdateOutputInformation()
img = itk.output(image_or_filter)
return img.GetOrigin()
def index(image_or_filter: "itkt.ImageOrImageSource") -> Sequence[int]:
"""Return the index of an image, or of the output image of a filter
This method take care of updating the needed information
"""
import itk
# we don't need the entire output, only its size
image_or_filter.UpdateOutputInformation()
img = itk.output(image_or_filter)
return img.GetLargestPossibleRegion().GetIndex()
def region(image_or_filter: "itkt.ImageOrImageSource") -> "itkt.ImageRegion":
"""Return the region of an image, or of the output image of a filter
This method take care of updating the needed information
"""
import itk
# we don't need the entire output, only its size
image_or_filter.UpdateOutputInformation()
img = itk.output(image_or_filter)
return img.GetLargestPossibleRegion()
def _get_itk_pixelid(numpy_array_type):
"""Returns a ITK PixelID given a numpy array."""
import itk
def _long_type():
if os.name == "nt":
return itk.ULL
else:
return itk.UL
# This is a Mapping from numpy array types to itk pixel types.
_np_itk = {
np.dtype(np.uint8): itk.UC,
np.dtype(np.uint16): itk.US,
np.dtype(np.uint32): itk.UI,
np.dtype(np.uint64): _long_type(),
np.dtype(np.int8): itk.SC,
np.dtype(np.int16): itk.SS,
np.dtype(np.int32): itk.SI,
np.dtype(np.int64): itk.SL,
np.dtype(np.float32): itk.F,
np.dtype(np.float64): itk.D,
np.dtype(np.complex64): itk.complex[itk.F],
np.dtype(np.complex128): itk.complex[itk.D],
}
try:
return _np_itk[numpy_array_type.dtype]
except KeyError as e:
raise e
def _GetArrayFromImage(
image_or_filter, function_name: str, keep_axes: bool, update: bool, ttype
) -> np.ndarray:
"""Get an Array with the content of the image buffer"""
# Finds the image type
import itk
img = itk.output(image_or_filter)
if ttype is not None:
if isinstance(ttype, (tuple, list)):
if len(ttype) != 1:
raise RuntimeError("Expected 1 component in ttype tuple.")
ImageType = ttype[0]
else:
ImageType = ttype
else:
ImageType = img.__class__
keys = [k for k in itk.PyBuffer.keys() if k[0] == ImageType]
if len(keys) == 0:
raise RuntimeError("No suitable template parameter can be found.")
# Create a numpy array of the type of the input image
templatedFunction = getattr(itk.PyBuffer[keys[0]], function_name)
return templatedFunction(img, keep_axes, update)
def GetArrayFromImage(
image_or_filter: "itkt.ImageOrImageSource",
keep_axes: bool = False,
update: bool = True,
ttype=None,
) -> np.ndarray:
"""Get an array with the content of the image buffer.
When *keep_axes* is *False*, the NumPy array will have C-order
indexing. This is the reverse of how indices are specified in ITK,
i.e. k,j,i versus i,j,k. However C-order indexing is expected by most
algorithms in NumPy / SciPy.
This is a deep copy of the image buffer and is completely safe and without potential side effects.
"""
return _GetArrayFromImage(
image_or_filter, "GetArrayFromImage", keep_axes, update, ttype
)
array_from_image = GetArrayFromImage
def GetArrayViewFromImage(
image_or_filter: "itkt.ImageOrImageSource",
keep_axes: bool = False,
update: bool = True,
ttype=None,
) -> np.ndarray:
"""Get an array view with the content of the image buffer.
When *keep_axes* is *False*, the NumPy array will have C-order
indexing. This is the reverse of how indices are specified in ITK,
i.e. k,j,i versus i,j,k. However C-order indexing is expected by most
algorithms in NumPy / SciPy.
"""
return _GetArrayFromImage(
image_or_filter, "GetArrayViewFromImage", keep_axes, update, ttype
)
array_view_from_image = GetArrayViewFromImage
def _GetImageFromArray(
arr: ArrayLike,
function_name: str,
is_vector: bool,
ttype,
need_contiguous: bool = True,
):
"""Get an ITK image from a Python array."""
import itk
# Verify inputs
if not isinstance(arr, np.ndarray):
arr = np.asarray(arr)
if ttype is not None:
if is_vector:
raise RuntimeError("Cannot specify both `is_vector` and `ttype`.")
if isinstance(ttype, (tuple, list)):
if len(ttype) != 1:
raise RuntimeError("Expected 1 component in ttype tuple.")
ImageType = ttype[0]
else:
ImageType = ttype
if type(itk.template(ImageType)) != tuple or len(itk.template(ImageType)) < 2:
raise RuntimeError("Cannot determine pixel type from supplied ttype.")
is_vector = (
type(itk.template(ImageType)[1][0]) != itk.support.types.itkCType
or itk.template(ImageType)[0] == itk.VectorImage
)
else:
PixelType = _get_itk_pixelid(arr)
Dimension = arr.ndim
if is_vector:
Dimension = arr.ndim - 1
if arr.flags["C_CONTIGUOUS"]:
VectorDimension = arr.shape[-1]
else:
VectorDimension = arr.shape[0]
if PixelType == itk.UC:
if VectorDimension == 3:
ImageType = itk.Image[itk.RGBPixel[itk.UC], Dimension]
elif VectorDimension == 4:
ImageType = itk.Image[itk.RGBAPixel[itk.UC], Dimension]
else:
ImageType = itk.VectorImage[PixelType, Dimension]
else:
ImageType = itk.VectorImage[PixelType, Dimension]
else:
ImageType = itk.Image[PixelType, Dimension]
keys = [k for k in itk.PyBuffer.keys() if k[0] == ImageType]
if len(keys) == 0:
raise RuntimeError(
"""No suitable template parameter can be found.
Please specify an output type via the 'ttype' keyword parameter."""
)
templatedFunction = getattr(itk.PyBuffer[keys[0]], function_name)
if function_name == "GetImageViewFromArray":
return templatedFunction(arr, is_vector, need_contiguous)
else:
return templatedFunction(arr, is_vector)
def GetImageFromArray(
arr: ArrayLike, is_vector: bool = False, ttype=None
) -> "itkt.ImageBase":
"""Get an ITK image from a Python array.
This is a deep copy of the NumPy array buffer and is completely safe without potential
side effects.
If is_vector is True, then a 3D array will be treated as a 2D vector image,
otherwise it will be treated as a 3D image.
If the array uses Fortran-order indexing, i.e. i,j,k, the Image Size
will have the same dimensions as the array shape. If the array uses
C-order indexing, i.e. k,j,i, the image Size will have the dimensions
reversed from the array shape.
Therefore, since the *np.transpose* operator on a 2D array simply
inverts the indexing scheme, the Image representation will be the
same for an array and its transpose. If flipping is desired, see
*np.reshape*.
ttype can be used te specify a specific itk.Image type.
"""
return _GetImageFromArray(arr, "GetImageFromArray", is_vector, ttype)
image_from_array = GetImageFromArray
def GetImageViewFromArray(
arr: ArrayLike, is_vector: bool = False, ttype=None, need_contiguous=True
) -> "itkt.ImageBase":
"""Get an ITK image view (shared pixel buffer memory) from a Python array.
If is_vector is True, then a 3D array will be treated as a 2D vector image,
otherwise it will be treated as a 3D image.
If the array uses Fortran-order indexing, i.e. i,j,k, the Image Size
will have the same dimensions as the array shape. If the array uses
C-order indexing, i.e. k,j,i, the image Size will have the dimensions
reversed from the array shape.
Therefore, since the *np.transpose* operator on a 2D array simply
inverts the indexing scheme, the Image representation will be the
same for an array and its transpose. If flipping is desired, see
*np.reshape*.
By default, a warning is issued if this function is called on a non-contiguous
array, since a copy is performed and care must be taken to keep a reference
to the copied array. This warning can be suppressed with need_contiguous=False
"""
return _GetImageFromArray(
arr, "GetImageViewFromArray", is_vector, ttype, need_contiguous=need_contiguous
)
image_view_from_array = GetImageViewFromArray
def array_from_vector_container(
container: "itkt.VectorContainer", ttype=None
) -> np.ndarray:
"""Get an Array with the content of the vector container"""
import itk
container_template = itk.template(container)
IndexType = container_template[1][0]
# Find container data type
if ttype is not None:
if isinstance(ttype, (tuple, list)):
if len(ttype) != 1:
raise RuntimeError("Expected 1 component in ttype tuple.")
DataType = ttype[0]
else:
DataType = ttype
else:
DataType = container_template[1][1]
keys = [k for k in itk.PyVectorContainer.keys() if k == (IndexType, DataType)]
if len(keys) == 0:
raise RuntimeError("No suitable template parameter can be found.")
# Create numpy array of the type of the input container
return itk.PyVectorContainer[keys[0]].array_from_vector_container(container)
def array_view_from_vector_container(
container: "itkt.VectorContainer", ttype=None
) -> np.ndarray:
"""Get an Array view with the content of the vector container"""
import itk
container_template = itk.template(container)
IndexType = container_template[1][0]
# Find container type
if ttype is not None:
if isinstance(ttype, (tuple, list)):
if len(ttype) != 1:
raise RuntimeError("Expected 1 component in ttype tuple.")
DataType = ttype[0]
else:
DataType = ttype
else:
DataType = container_template[1][1]
keys = [k for k in itk.PyVectorContainer.keys() if k == (IndexType, DataType)]
if len(keys) == 0:
raise RuntimeError("No suitable template parameter can be found.")
# Create numpy array of the type of the input container
return itk.PyVectorContainer[keys[0]].array_view_from_vector_container(container)
def vector_container_from_array(arr: ArrayLike, ttype=None) -> "itkt.VectorContainer":
"""Get a vector container from a Python array"""
import itk
# Verify inputs
if not isinstance(arr, np.ndarray):
arr = np.asarray(arr)
# Return VectorContainer with 64-bit index type
if os.name == "nt":
IndexType = itk.ULL
else:
IndexType = itk.UL
# Find container type
if ttype is not None:
if isinstance(ttype, (tuple, list)):
if len(ttype) != 1:
raise RuntimeError("Expected 1 component in ttype tuple.")
DataType = ttype[0]
else:
DataType = ttype
else:
DataType = _get_itk_pixelid(arr)
keys = [k for k in itk.PyVectorContainer.keys() if k == (IndexType, DataType)]
if len(keys) == 0:
raise RuntimeError("No suitable template parameter can be found.")
# Create numpy array of the type of the input container
return itk.PyVectorContainer[keys[0]].vector_container_from_array(arr)
def _GetArrayFromVnlObject(vnl_object, function_name: str, ttype) -> np.ndarray:
"""Get an array with the content of vnl_object"""
# Finds the vnl object type
import itk
if ttype is not None:
if isinstance(ttype, (tuple, list)):
if len(ttype) != 1:
raise RuntimeError("Expected 1 component in ttype tuple.")
PixelType = ttype[0]
else:
PixelType = ttype
else:
PixelType = itk.template(vnl_object)[1][0]
keys = [k for k in itk.PyVnl.keys() if k[0] == PixelType]
if len(keys) == 0:
raise RuntimeError("No suitable template parameter can be found.")
# Create a numpy array of the type of the vnl object
templatedFunction = getattr(itk.PyVnl[keys[0]], function_name)
return templatedFunction(vnl_object)
def GetArrayFromVnlVector(vnl_vector, ttype=None) -> np.ndarray:
"""Get an array with the content of vnl_vector"""
return _GetArrayFromVnlObject(vnl_vector, "GetArrayFromVnlVector", ttype)
array_from_vnl_vector = GetArrayFromVnlVector
def GetArrayViewFromVnlVector(vnl_vector, ttype=None) -> np.ndarray:
"""Get an array view of vnl_vector"""
return _GetArrayFromVnlObject(vnl_vector, "GetArrayViewFromVnlVector", ttype)
array_view_from_vnl_vector = GetArrayFromVnlVector
def GetArrayFromVnlMatrix(vnl_matrix, ttype=None) -> np.ndarray:
"""Get an array with the content of vnl_matrix"""
return _GetArrayFromVnlObject(vnl_matrix, "GetArrayFromVnlMatrix", ttype)
array_from_vnl_matrix = GetArrayFromVnlMatrix
def GetArrayViewFromVnlMatrix(vnl_matrix, ttype=None) -> np.ndarray:
"""Get an array view of vnl_matrix"""
return _GetArrayFromVnlObject(vnl_matrix, "GetArrayViewFromVnlMatrix", ttype)
array_view_from_vnl_matrix = GetArrayViewFromVnlMatrix
def _GetVnlObjectFromArray(arr: ArrayLike, function_name: str, ttype):
"""Get a vnl object from a Python array."""
import itk
# Verify inputs
if not isinstance(arr, np.ndarray):
arr = np.asarray(arr)
if ttype is not None:
if isinstance(ttype, (tuple, list)):
if len(ttype) != 1:
raise RuntimeError("Expected 1 component in ttype tuple.")
PixelType = ttype[0]
else:
PixelType = ttype
else:
PixelType = _get_itk_pixelid(arr)
keys = [k for k in itk.PyVnl.keys() if k[0] == PixelType]
if len(keys) == 0:
raise RuntimeError("No suitable template parameter can be found.")
templatedFunction = getattr(itk.PyVnl[keys[0]], function_name)
return templatedFunction(arr)
def GetVnlVectorFromArray(arr: ArrayLike, ttype=None):
"""Get a vnl vector from a Python array."""
return _GetVnlObjectFromArray(arr, "GetVnlVectorFromArray", ttype)
vnl_vector_from_array = GetVnlVectorFromArray
def GetVnlMatrixFromArray(arr: ArrayLike, ttype=None):
"""Get a vnl matrix from a Python array."""
return _GetVnlObjectFromArray(arr, "GetVnlMatrixFromArray", ttype)
vnl_matrix_from_array = GetVnlMatrixFromArray
def GetArrayFromMatrix(itk_matrix) -> np.ndarray:
return GetArrayFromVnlMatrix(itk_matrix.GetVnlMatrix().as_matrix())
array_from_matrix = GetArrayFromMatrix
def GetMatrixFromArray(arr: ArrayLike) -> "itkt.Matrix":
import itk
# Verify inputs
if not isinstance(arr, np.ndarray):
arr = np.asarray(arr)
vnl_matrix = GetVnlMatrixFromArray(arr)
dims = arr.shape
PixelType = _get_itk_pixelid(arr)
m = itk.Matrix[PixelType, dims[0], dims[1]](vnl_matrix)
return m
matrix_from_array = GetMatrixFromArray
def vtk_image_from_image(l_image: "itkt.ImageOrImageSource") -> "vtk.vtkImageData":
"""Convert an itk.Image to a vtk.vtkImageData."""
import itk
import vtk
from vtk.util.numpy_support import numpy_to_vtk
array = itk.array_view_from_image(l_image)
vtk_image = vtk.vtkImageData()
data_array = numpy_to_vtk(array.reshape(-1))
data_array.SetNumberOfComponents(l_image.GetNumberOfComponentsPerPixel())
data_array.SetName("Scalars")
# Always set Scalars for (future?) multi-component volume rendering
vtk_image.GetPointData().SetScalars(data_array)
dim = l_image.GetImageDimension()
l_spacing = [1.0] * 3
l_spacing[:dim] = l_image.GetSpacing()
vtk_image.SetSpacing(l_spacing)
l_origin = [0.0] * 3
l_origin[:dim] = l_image.GetOrigin()
vtk_image.SetOrigin(l_origin)
dims = [1] * 3
dims[:dim] = itk.size(l_image)
vtk_image.SetDimensions(dims)
# Copy direction matrix for VTK>=9
import vtk
if vtk.vtkVersion.GetVTKMajorVersion() >= 9:
l_direction = l_image.GetDirection()
direction = itk.array_from_matrix(l_direction).flatten().tolist()
if len(direction) == 4:
# Change 2d matrix to 3d
direction = [
direction[0],
direction[1],
0.0,
direction[2],
direction[3],
0.0,
0.0,
0.0,
1.0,
]
vtk_image.SetDirectionMatrix(direction)
if l_image.GetImageDimension() == 3:
PixelType = itk.template(l_image)[1][0]
if PixelType == itk.Vector:
vtk_image.GetPointData().SetVectors(data_array)
elif PixelType == itk.CovariantVector:
vtk_image.GetPointData().SetVectors(data_array)
elif PixelType == itk.SymmetricSecondRankTensor:
vtk_image.GetPointData().SetTensors(data_array)
elif PixelType == itk.DiffusionTensor3D:
vtk_image.GetPointData().SetTensors(data_array)
return vtk_image
def image_from_vtk_image(vtk_image: "vtk.vtkImageData") -> "itkt.ImageBase":
"""Convert a vtk.vtkImageData to an itk.Image."""
import itk
from vtk.util.numpy_support import vtk_to_numpy
point_data = vtk_image.GetPointData()
array = vtk_to_numpy(point_data.GetScalars())
array = array.reshape(-1)
number_of_components = point_data.GetScalars().GetNumberOfComponents()
is_vector = number_of_components != 1
dims = list(vtk_image.GetDimensions())
if is_vector and dims[-1] == 1:
dims = dims[:-1] # 2D, not 3D
dims.reverse()
if is_vector:
dims.append(number_of_components)
array.shape = tuple(dims)
l_image = itk.image_view_from_array(array, is_vector)
dim = l_image.GetImageDimension()
l_spacing = [1.0] * dim
l_spacing[:dim] = vtk_image.GetSpacing()[:dim]
l_image.SetSpacing(l_spacing)
l_origin = [0.0] * dim
l_origin[:dim] = vtk_image.GetOrigin()[:dim]
l_image.SetOrigin(l_origin)
# Direction support with VTK 9
import vtk
if vtk.vtkVersion.GetVTKMajorVersion() >= 9:
direction = vtk_image.GetDirectionMatrix()
if dim == 3:
direction_array = np.identity(3)
for y in (0, 1, 2):
for x in (0, 1, 2):
direction_array[x, y] = direction.GetElement(x, y)
elif dim == 2:
direction_array = np.identity(2)
for y in (0, 1):
for x in (0, 1):
direction_array[x, y] = direction.GetElement(x, y)
l_direction = itk.matrix_from_array(direction_array)
l_image.SetDirection(l_direction)
return l_image
def dict_from_image(image: "itkt.Image") -> Dict:
"""Serialize a Python itk.Image object to a pickable Python dictionary."""
import itk
pixel_arr = itk.array_from_image(image)
imageType = wasm_type_from_image_type(image)
buffered_region = image.GetBufferedRegion()
bufferedRegion = {
"index": tuple(buffered_region.GetIndex()),
"size": tuple(buffered_region.GetSize()),
}
return dict(
imageType=imageType,
name=image.GetObjectName(),
origin=tuple(image.GetOrigin()),
spacing=tuple(image.GetSpacing()),
size=tuple(image.GetLargestPossibleRegion().GetSize()),
bufferedRegion=bufferedRegion,
direction=np.asarray(image.GetDirection()),
data=pixel_arr,
)
def image_from_dict(image_dict: Dict) -> "itkt.Image":
"""Deserialize an dictionary representing an itk.Image object."""
import itk
ImageType = image_type_from_wasm_type(image_dict["imageType"])
if image_dict["data"] is None:
image = ImageType.New()
image.SetRegions(image_dict["size"])
if "bufferedRegion" in image_dict:
buffered_region = itk.ImageRegion[image.GetImageDimension()]()
buffered_region.SetIndex(image_dict["bufferedRegion"]["index"])
buffered_region.SetSize(image_dict["bufferedRegion"]["size"])
image.SetBufferedRegion(buffered_region)
image.Allocate(True)
else:
image = itk.PyBuffer[ImageType].GetImageViewFromArray(image_dict["data"])
image.SetRegions(image_dict["size"])
if "bufferedRegion" in image_dict:
buffered_region = itk.ImageRegion[image.GetImageDimension()]()
buffered_region.SetIndex(image_dict["bufferedRegion"]["index"])
buffered_region.SetSize(image_dict["bufferedRegion"]["size"])
image.SetBufferedRegion(buffered_region)
image.SetOrigin(image_dict["origin"])
image.SetSpacing(image_dict["spacing"])
image.SetDirection(image_dict["direction"])
image.SetObjectName(image_dict["name"])
return image
def mesh_from_dict(mesh_dict: Dict) -> "itkt.Mesh":
"""Deserialize an dictionary representing an itk.Mesh object."""
import itk
MeshType = mesh_type_from_wasm_type(mesh_dict["meshType"])
mesh = MeshType.New()
mesh.SetObjectName(mesh_dict["name"])
points = mesh_dict["points"]
points = itk.vector_container_from_array(points)
mesh.SetPoints(points)
point_data = mesh_dict["pointData"]
point_data = itk.vector_container_from_array(point_data)
mesh.SetPointData(point_data)
cells = mesh_dict["cells"]
cells = itk.vector_container_from_array(cells)
mesh.SetCellsArray(cells)
cell_data = mesh_dict["cellData"]
cell_data = itk.vector_container_from_array(cell_data)
mesh.SetCellData(cell_data)
return mesh
def dict_from_mesh(mesh: "itkt.Mesh") -> Dict:
"""Serialize a Python itk.Mesh object to a pickable Python dictionary."""
import itk
mesh_template = itk.template(mesh)
pixel_type, mangle, pixel_type_components = wasm_type_from_mesh_type(mesh)
number_of_points = mesh.GetNumberOfPoints()
number_of_cells = mesh.GetNumberOfCells()
if number_of_cells == 0:
cells_array = np.array([], np.uint)
else:
cells_array = itk.array_from_vector_container(mesh.GetCellsArray())
if number_of_points == 0:
points_array = np.array([], np.float32)
else:
points_array = itk.array_from_vector_container(mesh.GetPoints()).flatten()
point_data = mesh.GetPointData()
if point_data.Size() == 0:
point_data_numpy = np.array([], mangle)
else:
point_data_numpy = itk.array_from_vector_container(point_data)
cell_data = mesh.GetCellData()
if cell_data.Size() == 0:
cell_data_numpy = np.array([], mangle)
else:
cell_data_numpy = itk.array_from_vector_container(cell_data)
if os.name == "nt":
cell_component_type = python_to_js(itk.ULL)
else:
cell_component_type = python_to_js(itk.UL)
point_component_type = python_to_js(itk.F)
# Currently use the same data type for point and cell data
mesh_type = dict()
mesh_type["dimension"] = mesh_template[1][1]
mesh_type["pointComponentType"] = point_component_type
mesh_type["pointPixelComponentType"] = mangle
mesh_type["pointPixelType"] = pixel_type
mesh_type["pointPixelComponents"] = pixel_type_components
mesh_type["cellComponentType"] = cell_component_type
mesh_type["cellPixelComponentType"] = mangle
mesh_type["cellPixelType"] = pixel_type
mesh_type["cellPixelComponents"] = pixel_type_components
cell_buffer_size = cells_array.size
return dict(
meshType=mesh_type,
name=mesh.GetObjectName(),
numberOfPoints=number_of_points,
points=points_array,
numberOfPointPixels=point_data.Size(),
pointData=point_data_numpy,
numberOfCells=number_of_cells,
cells=cells_array,
numberOfCellPixels=cell_data.Size(),
cellData=cell_data_numpy,
cellBufferSize=cell_buffer_size,
)
def pointset_from_dict(pointset_dict: Dict) -> "itkt.PointSet":
"""Deserialize an dictionary representing an itk.PointSet object."""
import itk
MeshType = pointset_type_from_wasm_type(pointset_dict["pointSetType"])
mesh = MeshType.New()
mesh.SetObjectName(pointset_dict["name"])
points = pointset_dict["points"]
points = itk.vector_container_from_array(points)
mesh.SetPoints(points)
point_data = pointset_dict["pointData"]
point_data = itk.vector_container_from_array(point_data)
mesh.SetPointData(point_data)
return mesh
def dict_from_pointset(pointset: "itkt.PointSet") -> Dict:
"""Serialize a Python itk.PointSet object to a pickable Python dictionary."""
import itk
pointset_template = itk.template(pointset)
pixel_type, mangle, pixel_type_components = wasm_type_from_pointset_type(pointset)
number_of_points = pointset.GetNumberOfPoints()
if number_of_points == 0:
points_array = np.array([], np.float32)
else:
points_array = itk.array_from_vector_container(pointset.GetPoints()).flatten()
point_data = pointset.GetPointData()
if point_data.Size() == 0:
point_data_numpy = np.array([], mangle)
else:
point_data_numpy = itk.array_from_vector_container(point_data)
if os.name == "nt":
cell_component_type = python_to_js(itk.ULL)
else:
cell_component_type = python_to_js(itk.UL)
point_component_type = python_to_js(itk.F)
# Currently use the same data type for point and cell data
pointset_type = dict()
pointset_type["dimension"] = pointset_template[1][1]
pointset_type["pointComponentType"] = point_component_type
pointset_type["pointPixelComponentType"] = mangle
pointset_type["pointPixelType"] = pixel_type
pointset_type["pointPixelComponents"] = pixel_type_components
return dict(
pointSetType=pointset_type,
name=pointset.GetObjectName(),
numberOfPoints=number_of_points,
points=points_array,
numberOfPointPixels=point_data.Size(),
pointData=point_data_numpy,
)
def polyline_from_dict(polyline_dict: Dict) -> "itkt.PolylineParametricPath":
"""Deserialize an dictionary representing an itk.PolylineParametricPath object."""
import itk
vertex_list = polyline_dict["vertexList"]
dimension = vertex_list.shape[1]
polyline = itk.PolyLineParametricPath[dimension].New()
polyline.SetObjectName(polyline_dict["name"])
for vertex in vertex_list:
polyline.AddVertex(vertex)
return polyline
def dict_from_polyline(polyline: "itkt.PolylineParametricPath") -> Dict:
"""Serialize a Python itk.PolylineParametricPath object to a pickable Python dictionary."""
import itk
vertex_list = polyline.GetVertexList()
vertex_list_array = itk.array_from_vector_container(vertex_list)
return dict(
name=polyline.GetObjectName(),
vertexList=vertex_list_array,
)
def dict_from_transform(
transform: Union["itkt.TransformBase", List["itkt.TransformBase"]],
) -> Union[List[Dict], Dict]:
"""Serialize a Python itk.Transform object to a pickable Python dictionary.
If the transform is a list of transforms, then a list of dictionaries is returned.
If the transform is a single, non-Composite transform, then a single dictionary is returned.
Composite transforms and nested composite transforms are flattened into a list of dictionaries.
"""
import itk
datatype_dict = {"double": itk.D, "float": itk.F}
def update_transform_dict(current_transform):
current_transform_type = current_transform.GetTransformTypeAsString()
current_transform_type_split = current_transform_type.split("_")
transform_type = dict()
transform_parameterization = current_transform_type_split[0].replace(
"Transform", ""
)
transform_type["transformParameterization"] = transform_parameterization
transform_type["parametersValueType"] = python_to_js(
datatype_dict[current_transform_type_split[1]]
)
transform_type["inputDimension"] = int(current_transform_type_split[2])
transform_type["outputDimension"] = int(current_transform_type_split[3])
transform_dict = dict()
transform_dict["transformType"] = transform_type
transform_dict["name"] = current_transform.GetObjectName()
transform_dict["inputSpaceName"] = current_transform.GetInputSpaceName()
transform_dict["outputSpaceName"] = current_transform.GetOutputSpaceName()
# To avoid copying the parameters for the Composite Transform
# as it is a copy of child transforms.
if "Composite" not in current_transform_type_split[0]:
p = np.array(current_transform.GetParameters())
transform_dict["parameters"] = p
fp = np.array(current_transform.GetFixedParameters())
transform_dict["fixedParameters"] = fp
transform_dict["numberOfParameters"] = p.shape[0]
transform_dict["numberOfFixedParameters"] = fp.shape[0]
return transform_dict
dict_array = []
multi = False
def add_transform_dict(transform):
transform_type = transform.GetTransformTypeAsString()
if "CompositeTransform" in transform_type:
# Add the transforms inside the composite transform
# range is over-ridden so using this hack to create a list
for i, _ in enumerate([0] * transform.GetNumberOfTransforms()):
current_transform = transform.GetNthTransform(i)
dict_array.append(update_transform_dict(current_transform))
return True
else:
dict_array.append(update_transform_dict(transform))
return False
if isinstance(transform, list):
multi = True
for t in transform:
add_transform_dict(t)
else:
multi = add_transform_dict(transform)
if multi:
return dict_array
else:
return dict_array[0]
def transform_from_dict(
transform_dict: Union[Dict, List[Dict]],
) -> "itkt.TransformBase":
"""Deserialize a dictionary representing an itk.Transform object.
If the dictionary represents a list of transforms, then a Composite Transform is returned.
"""
import itk
def set_parameters(
transform, transform_parameters, transform_fixed_parameters, data_type
):
# First set fixed parameters then parameters
o1 = itk.OptimizerParameters[data_type](list(transform_fixed_parameters))
transform.SetFixedParameters(o1)
o2 = itk.OptimizerParameters[data_type](list(transform_parameters))
transform.SetParameters(o2)
# For checking transforms which don't take additional parameters while instantiation
def special_transform_check(transform_name):
if "2D" in transform_name or "3D" in transform_name:
return True
check_list = ["VersorTransform", "QuaternionRigidTransform"]
for t in check_list:
if transform_name == t:
return True
return False
parametersValueType_dict = {"float32": itk.F, "float64": itk.D}
if not isinstance(transform_dict, list):
transform_dict = [transform_dict]
# Loop over all the transforms in the dictionary
transforms_list = []
for i, _ in enumerate(transform_dict):
transform_type = transform_dict[i]["transformType"]
data_type = parametersValueType_dict[transform_type["parametersValueType"]]
transform_parameterization = (
transform_type["transformParameterization"] + "Transform"
)
# No template parameter needed for transforms having 2D or 3D name
# Also for some selected transforms
if special_transform_check(transform_parameterization):
transform_template = getattr(itk, transform_parameterization)
transform = transform_template[data_type].New()
# Currently only BSpline Transform has 3 template parameters
# For future extensions the information will have to be encoded in
# the transformType variable. The transform object once added in a
# composite transform lose the information for other template parameters ex. BSpline.
# The Spline order is fixed as 3 here.
elif transform_parameterization == "BSplineTransform":
transform_template = getattr(itk, transform_parameterization)
transform = transform_template[
data_type, transform_type["inputDimension"], 3
].New()
else:
transform_template = getattr(itk, transform_parameterization)
if len(transform_template.items()[0][0]) > 2:
transform = transform_template[
data_type,
transform_type["inputDimension"],
transform_type["outputDimension"],
].New()
else:
transform = transform_template[
data_type, transform_type["inputDimension"]
].New()
transform.SetObjectName(transform_dict[i]["name"])
transform.SetInputSpaceName(transform_dict[i]["inputSpaceName"])
transform.SetOutputSpaceName(transform_dict[i]["outputSpaceName"])
set_parameters(
transform,
transform_dict[i]["parameters"],
transform_dict[i]["fixedParameters"],
data_type,
)
transforms_list.append(transform)
# If array has length more than 1 then it's a composite transform
if len(transforms_list) > 1:
# Create a Composite Transform object
# and add all the transforms in it.
data_type = parametersValueType_dict[
transform_dict[0]["transformType"]["parametersValueType"]
]
transform = itk.CompositeTransform[
data_type, transforms_list[0]["transformType"]["inputDimension"]
].New()
for current_transform in transforms_list:
transform.AddTransform(current_transform)
else:
transform = transforms_list[0]
return transform
def image_intensity_min_max(image_or_filter: "itkt.ImageOrImageSource"):
"""Return the minimum and maximum of values in a image of in the output image of a filter
The minimum and maximum values are returned in a tuple: (min, max)
image_intensity_min_max() take care of updating the pipeline
"""
import itk
img = itk.output(image_or_filter)
img.UpdateOutputInformation()
img.Update()
# don't put that calculator in the automatic pipeline
tmp_auto_pipeline = auto_pipeline.current
auto_pipeline.current = None
comp = itk.MinimumMaximumImageCalculator[img].New(Image=img)
auto_pipeline.current = tmp_auto_pipeline
comp.Compute()
return comp.GetMinimum(), comp.GetMaximum()
# range is a python function, and should not be overridden
# the current use of the function name "range" is for backward
# compatibility, but should be considered for removal in the future
def range(image_or_filter):
return image_intensity_min_max(image_or_filter)
def imwrite(
image_or_filter: "itkt.ImageOrImageSource",
filename: fileiotype,
compression: bool = False,
imageio: Optional["itkt.ImageIOBase"] = None,
) -> None:
"""Write a image or the output image of a filter to a file.
Parameters
----------
image_or_filter :
Image or filter that produces an image to write to the file.
filename :
Target output file path.
compression :
Use compression when writing if the format supports it.
imageio :
Use the provided itk.ImageIOBase derived instance to write the file.
The writer is instantiated with the image type of the image in
parameter (or, again, with the output image of the filter in parameter).
"""
import itk
img = itk.output(image_or_filter)
img.UpdateOutputInformation()
# don't put that writer in the automatic pipeline
tmp_auto_pipeline = auto_pipeline.current
auto_pipeline.current = None
writer = itk.ImageFileWriter[type(img)].New(
Input=img, FileName=f"{filename}", UseCompression=compression
)
auto_pipeline.current = tmp_auto_pipeline
if imageio:
writer.SetImageIO(imageio)
writer.Update()
def imread(
filename: Union[fileiotype, Sequence[Union[str, os.PathLike]]],
pixel_type: Optional["itkt.PixelTypes"] = None,
fallback_only: bool = False,
imageio: Optional["itkt.ImageIOBase"] = None,
series_uid: Optional[Union[int, str]] = None,
) -> "itkt.ImageBase":
"""Read an image from a file or series of files and return an itk.Image.
Parameters
----------
filename :
File path for a single file, a list of files for an image series, or a
directory for a DICOM image series.
pixel_type :
Image pixel type to cast to when loading.
fallback_only :
If true, first try to automatically deduce the image pixel type, and
only use the given `pixel_type` if automatic deduction fails.
imageio :
Use the provided itk.ImageIOBase derived instance to read the file.
series_uid :
If filename is a directory of DICOM files, and series_uid is a string, it will read the DICOM series that matches this series_uid name. If series_uid is an integer, it will read the series_uid'th DICOM series in the directory (using python notation, e.g., -1 is the last DICOM series in the directory). If unspecified, it will read the first series in the directory.
Returns
-------
image :
The resulting itk.Image.
The reader is instantiated with the image type of the image file if
`pixel_type` is not provided (default). The dimension of the image is
automatically deduced from the dimension stored on disk.
If the filename provided is a directory then the directory is assumed
to be for a DICOM series volume. If the argument series_uid is
specified, it will read that series from that directory. If the
specified series_uid doesn't exist, an error is thrown. If no
series_uid is given, the first series in that directory is read.
If the given filename is a list or a tuple of file names, the reader
will use an itk.ImageSeriesReader object to read the files.
If `fallback_only` is set to `True`, `imread()` will first try to
automatically deduce the image pixel_type, and only use the given
`pixel_type` if automatic deduction fails. Failures typically happen if
the pixel type is not supported (e.g. it is not currently wrapped).
"""
import itk
from itk.support.extras import TemplateTypeError
if fallback_only:
if pixel_type is None:
raise Exception(
"pixel_type must be set when using the fallback_only option"
)
try:
return imread(filename)
except (KeyError, TemplateTypeError):
pass
if type(filename) not in [list, tuple]:
import os
if os.path.isdir(filename) and imageio is None:
# read DICOM series of 1 image in a folder, refer to: https://github.com/RSIP-Vision/medio
names_generator = itk.GDCMSeriesFileNames.New()
names_generator.SetUseSeriesDetails(True)
names_generator.AddSeriesRestriction("0008|0021") # Series Date
names_generator.SetDirectory(f"{filename}")
series_uid_list = names_generator.GetSeriesUIDs()
if len(series_uid_list) == 0:
raise FileNotFoundError(f"no DICOMs in: {filename}.")
if series_uid != None:
if isinstance(series_uid, int):
try:
series_identifier = series_uid_list[series_uid]
except IndexError:
raise OSError(
f"cannot access DICOM series {series_uid} in the directory {filename}."
)
else:
if series_uid in series_uid_list:
series_identifier = series_uid
else:
raise OSError(
f"the directory {filename} does not contain the DICOM series {series_uid}."
)
else:
series_identifier = series_uid_list[0]
filename = names_generator.GetFileNames(series_identifier)
if type(filename) in [list, tuple]:
template_reader_type = itk.ImageSeriesReader
io_filename = f"{filename[0]}"
increase_dimension = True
kwargs = {"FileNames": [f"{str(f)}" for f in filename]}
else:
template_reader_type = itk.ImageFileReader
io_filename = f"{filename}"
increase_dimension = False
kwargs = {"FileName": f"{filename}"}
if imageio:
kwargs["ImageIO"] = imageio
if pixel_type:
image_IO = itk.ImageIOFactory.CreateImageIO(
io_filename, itk.CommonEnums.IOFileMode_ReadMode
)
if not image_IO:
if os.path.exists(io_filename):
raise RuntimeError("No ImageIO is registered to handle the given file.")
else:
raise FileNotFoundError(f"File {io_filename} does not exist.")
image_IO.SetFileName(io_filename)
image_IO.ReadImageInformation()
dimension = image_IO.GetNumberOfDimensions()
# Increase dimension if last dimension is not of size one.
if increase_dimension and image_IO.GetDimensions(dimension - 1) != 1:
dimension += 1
is_vlv = False
try:
is_vlv = itk.template(pixel_type)[0] is itk.VariableLengthVector
except KeyError:
pass
if is_vlv:
ImageType = itk.VectorImage[itk.template(pixel_type)[1][0], dimension]
else:
ImageType = itk.Image[pixel_type, dimension]
reader = template_reader_type[ImageType].New(**kwargs)
else:
reader = template_reader_type.New(**kwargs)
reader.Update()
return reader.GetOutput()
def meshwrite(
mesh: "itkt.Mesh", filename: fileiotype, compression: bool = False
) -> None:
"""Write a mesh to a file.
The writer is instantiated according to the type of the input mesh.
"""
import itk
mesh.UpdateOutputInformation()
# don't put that writer in the automatic pipeline
tmp_auto_pipeline = auto_pipeline.current
auto_pipeline.current = None
writer = itk.MeshFileWriter[type(mesh)].New(
Input=mesh, FileName=f"{filename}", UseCompression=compression
)
auto_pipeline.current = tmp_auto_pipeline
writer.Update()
def meshread(
filename: fileiotype,
pixel_type: Optional["itkt.PixelTypes"] = None,
fallback_only: bool = False,
) -> "itkt.Mesh":
"""Read a mesh from a file and return an itk.Mesh.
The reader is instantiated with the mesh type of the mesh file if
`pixel_type` is not provided (default). The dimension of the mesh is
automatically found.
If `fallback_only` is set to `True`, `meshread()` will first try to
automatically deduce the image pixel_type, and only use the given
`pixel_type` if automatic deduction fails. Failures typically
happen if the pixel type is not supported (e.g. it is not currently
wrapped).
"""
import itk
if fallback_only:
if pixel_type is None:
raise Exception(
"pixel_type must be set when using the fallback_only option"
)
try:
return meshread(filename)
except (KeyError, itk.TemplateTypeError):
pass
TemplateReaderType = itk.MeshFileReader
io_filename = f"{filename}"
increase_dimension = False
kwargs = {"FileName": f"{filename}"}
if pixel_type:
meshIO = itk.MeshIOFactory.CreateMeshIO(
io_filename, itk.CommonEnums.IOFileMode_ReadMode
)
if not meshIO:
raise RuntimeError("No MeshIO is registered to handle the given file.")
meshIO.SetFileName(io_filename)
meshIO.ReadMeshInformation()
dimension = meshIO.GetPointDimension()
# Increase dimension if last dimension is not of size one.
if increase_dimension and meshIO.GetDimensions(dimension - 1) != 1:
dimension += 1
MeshType = itk.Mesh[pixel_type, dimension]
reader = TemplateReaderType[MeshType].New(**kwargs)
else:
reader = TemplateReaderType.New(**kwargs)
reader.Update()
return reader.GetOutput()
def transformread(filename: fileiotype) -> List["itkt.TransformBase"]:
"""Read an itk Transform file.
Parameters
----------
filename:
Path to the transform file (typically a .h5 file).
Returns
-------
A Python list containing the transforms in the file.
"""
import itk
reader = itk.TransformFileReaderTemplate[itk.D].New()
reader.SetFileName(f"{filename}")
reader.Update()
transforms = []
transform_list = reader.GetModifiableTransformList()
while not transform_list.empty():
transform = transform_list.pop()
transforms.append(itk.down_cast(transform))
transforms.reverse()
return transforms
def transformwrite(
transforms: Union["itkt.TransformBase", List["itkt.TransformBase"]],
filename: fileiotype,
compression: bool = False,
) -> None:
"""Write an itk Transform file.
Parameters
----------
transforms: single transform or list of transforms
Single transform or Python list of itk.TransformBaseTemplate[itk.D]'s to write.
filename:
Path to the transform file (typically a .h5 file).
compression:
Use compression, if the file format supports it.
"""
import itk
writer = itk.TransformFileWriterTemplate[itk.D].New()
writer.SetFileName(f"{filename}")
writer.SetUseCompression(compression)
if not isinstance(transforms, list):
transforms = [transforms]
for transform in transforms:
writer.AddTransform(transform)
writer.Update()
def search(s: str, case_sensitive: bool = False) -> List[str]: # , fuzzy=True):
"""Search for a class name in the itk module."""
s = s.replace(" ", "")
if not case_sensitive:
s = s.lower()
import itk
names = sorted(dir(itk))
# exact match first
if case_sensitive:
res = [n for n in names if s == n]
else:
res = [n for n in names if s == n.lower()]
# then exact match inside the name
if case_sensitive:
res += [n for n in names if s in n and s != n]
else:
res += [n for n in names if s in n.lower() and s != n.lower()]
# if fuzzy:
# try:
# everything now requires editdist
# import editdist
# if case_sensitive:
# res.sort(key=lambda x: editdist.distance(x, s))
# else:
# res.sort(key=lambda x: (editdist.distance(x.lower(), s), x))
# except:
# pass
return res
def set_inputs(
new_itk_object,
inargs: Optional[Sequence[Any]] = None,
inkargs: Optional[Dict[str, Any]] = None,
):
"""Set the inputs of the given objects, according to the non named or the
named parameters in args and kargs
This function tries to assign all the non named parameters in the input of
the new_itk_object
- the first non named parameter in the first input, etc.
The named parameters are used by calling the method with the same name
prefixed by 'Set'.
set_inputs( obj, kargs={'Threshold': 10} ) calls obj.SetThreshold(10)
This is the function use in the enhanced New() method to manage the inputs.
It can be used to produce a similar behavior:
def SetInputs(self, *args, **kargs):
import itk
itk.set_inputs(self, *args, **kargs)
"""
# Fix bug with Mutable Default Arguments
# https://docs.python-guide.org/writing/gotchas/
args: List[Any] = inargs if inargs else []
kargs: Dict[str, Any] = inkargs if inkargs else {}
# try to get the images from the filters in args
args = [output(arg) for arg in args]
# args without name are filter used to set input image
#
# count SetInput calls to call SetInput, SetInput2, SetInput3, ...
# useful with filter which take 2 input (or more) like SubtractImageFiler
# Ex: subtract image2.png to image1.png and save the result in result.png
# r1 = itk.ImageFileReader.US2.New(FileName='image1.png')
# r2 = itk.ImageFileReader.US2.New(FileName='image2.png')
# s = itk.SubtractImageFilter.US2US2US2.New(r1, r2)
# itk.ImageFileWriter.US2.New(s, FileName='result.png').Update()
setInputNb: int = -1
try:
for setInputNb, arg in enumerate(args):
methodName = "SetInput%i" % (setInputNb + 1)
if methodName in dir(new_itk_object):
# first try to use methods called SetInput1, SetInput2, ...
# those method should have more chances to work in case of
# multiple input types
getattr(new_itk_object, methodName)(arg)
else:
# no method called SetInput?
# try with the standard SetInput(nb, input)
new_itk_object.SetInput(setInputNb, arg)
except TypeError as e:
# the exception have (at least) to possible reasons:
# + the filter don't take the input number as first argument
# + arg is an object of wrong type
#
# if it's not the first input, re-raise the exception
if setInputNb != 0:
raise e
# it's the first input, try to use the SetInput() method without input
# number
new_itk_object.SetInput(args[0])
# but raise an exception if there is more than 1 argument
if len(args) > 1:
raise TypeError("Object accepts only 1 input.")
except AttributeError:
# There is no SetInput() method, try SetImage
# but before, check the number of inputs
if len(args) > 1:
raise TypeError("Object accepts only 1 input.")
methodList = ["SetImage", "SetInputImage"]
methodName = None
for m in methodList:
if m in dir(new_itk_object):
methodName = m
if methodName:
getattr(new_itk_object, methodName)(args[0])
else:
raise AttributeError("No method found to set the input.")
# named args : name is the function name, value is argument(s)
for attribName, value in kargs.items():
# use Set as prefix. It allow to use a shorter and more intuitive
# call (Ex: itk.ImageFileReader.UC2.New(FileName='image.png')) than
# with the full name
# (Ex: itk.ImageFileReader.UC2.New(SetFileName='image.png'))
if attribName not in ["auto_progress", "template_parameters"]:
if attribName.islower():
attribName = snake_to_camel_case(attribName)
attrib = getattr(new_itk_object, "Set" + attribName)
# Do not use try-except mechanism as this leads to
# segfaults. Instead limit the number of types that are
# tested. The list of tested type could maybe be replaced by
# a test that would check for iterables.
import itk
if type(value) in [list, tuple]:
# Special case for FileNames which expects a container, not unpacked arguments.
# Most ITK Set* methods that take multiple values expect them as separate arguments
# (e.g., SetSpacing(x, y, z)), so lists are unpacked with *.
# However, SetFileNames expects a single container argument (vector<string>),
# so we pass the list directly without unpacking.
if attribName == "FileNames":
attrib(value)
else:
try:
output_value = [itk.output(x) for x in value]
attrib(*output_value)
except Exception:
attrib(itk.output(value))
else:
attrib(itk.output(value))
class templated_class:
"""This class is used to mimic the behavior of the templated C++ classes.
It is used this way:
class CustomClass:
# class definition here
CustomClass = templated_class(CustomClass)
customObject = CustomClass[template, parameters].New()
The template parameters are passed to the custom class constructor as a
named parameter 'template_parameters' in a tuple.
The custom class may implement a static method
check_template_parameters(parameters) which should raise an exception if
the template parameters provided are not suitable to instantiate the custom
class.
"""
def __init__(self, cls) -> None:
"""cls is the custom class"""
self.__cls__ = cls
self.__templates__ = {}
def New(self, *args, **kargs):
"""Use the parameters to infer the types of the template parameters."""
# extract the types from the arguments to instantiate the class
import itk
types = tuple(class_(o) for o in args)
return self[types].New(*args, **kargs)
def __getitem__(self, template_parameters):
"""Return a pair class-template parameters ready to be instantiated.
The template parameters may be validated if the custom class provide
the static method check_template_parameters(parameters).
"""
if not isinstance(template_parameters, tuple):
template_parameters = (template_parameters,)
return templated_class.__templated_class_and_parameters__(
self, template_parameters
)
def check_template_parameters(self, template_parameters) -> None:
"""Check the template parameters passed in parameter."""
# this method is there mainly to make possible to reuse it in the
# custom class constructor after having used templated_class().
# Without that, the following example doesn't work:
#
# class CustomClass:
# def __init__(self, *args, **kargs):
# template_parameters = kargs["template_parameters"]
# CustomClass.check_template_parameters(template_parameters)
# other init stuff
# def check_template_parameters(template_parameters):
# check, really
# pass
# CustomClass = templated_class(CustomClass)
#
self.__cls__.check_template_parameters(template_parameters)
def add_template(self, name: str, params):
if not isinstance(params, list) and not isinstance(params, tuple):
params = (params,)
params = tuple(params)
val = self[params]
self.__templates__[params] = val
setattr(self, name, val)
def add_image_templates(self, *args) -> None:
import itk
if not args:
return
combinations = [[t] for t in args[0]]
for types in args[1:]:
temp = []
for t in types:
for c in combinations:
temp.append(c + [t])
combinations = temp
for d in itk.DIMS:
for c in combinations:
parameters = []
name = ""
for t in c:
parameters.append(itk.Image[t, d])
name += "I" + t.short_name + str(d)
self.add_template(name, tuple(parameters))
class __templated_class_and_parameters__:
"""Inner class used to store the pair class-template parameters ready
to instantiate.
"""
def __init__(self, l_templated_class, l_template_parameters) -> None:
self.__templated_class__ = l_templated_class
self.__template_parameters__ = l_template_parameters
if "check_template_parameters" in dir(l_templated_class.__cls__):
l_templated_class.__cls__.check_template_parameters(
l_template_parameters
)
def New(self, *args, **kargs):
"""A New() method to mimic the ITK default behavior, even if the
class doesn't provide any New() method.
"""
kargs["template_parameters"] = self.__template_parameters__
if "New" in dir(self.__templated_class__.__cls__):
obj = self.__templated_class__.__cls__.New(*args, **kargs)
else:
obj = self.__templated_class__.__cls__(*args, **kargs)
setattr(obj, "__template_parameters__", self.__template_parameters__)
setattr(obj, "__templated_class__", self.__templated_class__)
return obj
def __call__(self, *args, **kargs):
return self.New(*args, **kargs)
def keys(self):
return self.__templates__.keys()
def values(self):
return list(self.__templates__.values())
def items(self):
return list(self.__templates__.items())
# everything after this comment is for dict interface
# and is a copy/paste from DictMixin
# only methods to edit dictionary are not there
def __iter__(self) -> str:
yield from self.keys()
def has_key(self, key: str):
return key in self.__templates__
def __contains__(self, key: str):
return key in self
def get(self, key: str, default: Optional[str] = None) -> Optional[str]:
return self.get(key, default)
def __len__(self):
return len(self.keys())
class pipeline:
"""A convenient class to store the reference to the filters of a pipeline
With this class, a method can create a pipeline of several filters and
return it without losing the references to the filters in this pipeline.
The pipeline object act almost like a filter (it has a GetOutput() method)
and thus can be simply integrated in another pipeline.
"""
def __init__(self, *args, **kargs) -> None:
self.clear()
self.input = None
self.filters: List[Any] = []
set_inputs(self, args, kargs)
def connect(self, l_filter) -> None:
"""Connect a new l_filter to the pipeline
The output of the first l_filter will be used as the input of this
one and the l_filter passed as parameter will be added to the list
"""
if self.GetOutput() is not None:
set_inputs(l_filter, [self.GetOutput()])
self.append(l_filter)
def append(self, l_filter) -> None:
"""Add a new l_filter to the pipeline
The new l_filter will not be connected. The user must connect it.
"""
self.filters.append(l_filter)
def clear(self) -> None:
"""Clear the filter list"""
self.filters = []
def GetOutput(self, l_index: int = 0):
"""Return the output of the pipeline
If another output is needed, use
pipeline.filters[-1].GetAnotherOutput() instead of this method,
subclass pipeline to implement another GetOutput() method, or use
expose()
"""
if len(self.filters) == 0:
return self.GetInput()
else:
l_filter = self.filters[-1]
if hasattr(l_filter, "__getitem__"):
return l_filter[l_index]
try:
return l_filter.GetOutput(l_index)
except Exception:
if l_index == 0:
return l_filter.GetOutput()
else:
raise ValueError("Index can only be 0 on that object")
def GetNumberOfOutputs(self) -> int:
"""Return the number of outputs"""
if len(self.filters) == 0:
return 1
else:
return self.filters[-1].GetNumberOfOutputs()
def SetInput(self, l_input) -> None:
"""Set the l_input of the pipeline"""
if len(self.filters) != 0:
set_inputs(self.filters[0], [l_input])
self.l_input = l_input
def GetInput(self):
"""Get the input of the pipeline"""
return self.input
def Update(self):
"""Update the pipeline"""
if len(self.filters) > 0:
return self.filters[-1].Update()
def UpdateLargestPossibleRegion(self):
"""Update the pipeline"""
if len(self.filters) > 0:
return self.filters[-1].UpdateLargestPossibleRegion()
def UpdateOutputInformation(self) -> None:
if "UpdateOutputInformation" in dir(self.filters[-1]):
self.filters[-1].UpdateOutputInformation()
else:
self.Update()
def __len__(self):
return self.GetNumberOfOutputs()
def __getitem__(self, item):
return self.GetOutput(item)
def __call__(self, *args, **kargs):
set_inputs(self, args, kargs)
self.UpdateLargestPossibleRegion()
return self
def expose(self, name: str, new_name: Optional[str] = None, position: int = -1):
"""Expose an attribute from a filter of the mini-pipeline.
Once called, the pipeline instance has a new Set/Get set of methods to
access directly the corresponding method of one of the filter of the
pipeline.
Ex: p.expose( "Radius" )
p.SetRadius( 5 )
p.GetRadius( 5 )
By default, the attribute usable on the pipeline instance has the same
name than the one of the filter, but it can be changed by providing a
value to new_name.
The last filter of the pipeline is used by default, but another one may
be used by giving its position.
Ex: p.expose("Radius", "SmoothingNeighborhood", 2)
p.GetSmoothingNeighborhood()
"""
if new_name is None:
new_name = name
src = self.filters[position]
ok: bool = False
set_name: str = "Set" + name
if set_name in dir(src):
setattr(self, "Set" + new_name, getattr(src, set_name))
ok = True
get_name = "Get" + name
if get_name in dir(src):
setattr(self, "Get" + new_name, getattr(src, get_name))
ok = True
if not ok:
raise RuntimeError(f"No attribute {name} at position {position}.")
class auto_pipeline(pipeline):
current = None
def __init__(self, *args, **kargs) -> None:
pipeline.__init__(self, *args, **kargs)
self.Start()
def Start(self) -> None:
auto_pipeline.current = self
@staticmethod
def Stop() -> None:
auto_pipeline.current = None
def down_cast(obj: "itkt.LightObject"):
"""Down cast an itk.LightObject (or a object of a subclass) to its most
specialized type.
"""
import itk
from itk.support.template_class import itkTemplate
class_name: str = obj.GetNameOfClass()
t = getattr(itk, class_name)
if isinstance(t, itkTemplate):
for c in t.values():
try:
return c.cast(obj)
except Exception:
# fail silently for now
pass
raise RuntimeError(f"Can't downcast to a specialization of {class_name}")
else:
return t.cast(obj)
def attribute_list(inputobject, name: str):
"""Returns a list of the specified attributes for the objects in the image.
i: the input LabelImage
name: the attribute name
"""
import itk
img = itk.output(inputobject)
relabel = itk.StatisticsRelabelLabelMapFilter[img].New(
img, Attribute=name, ReverseOrdering=True, InPlace=False
)
relabel.UpdateLargestPossibleRegion()
r = relabel.GetOutput()
l_list: List[Any] = []
# required because range is overloaded in this module
import sys
from builtins import range
for i in range(1, r.GetNumberOfLabelObjects() + 1):
l_list.append(r.GetLabelObject(i).__getattribute__("Get" + name)())
return l_list
def attributes_list(inputObject, names: List[str]):
"""Returns a list of the specified attributes for the objects in the image.
i: the input LabelImage
name: the attribute name
"""
import itk
img = itk.output(inputObject)
relabel = itk.StatisticsRelabelLabelMapFilter[img].New(
img, Attribute=names[0], ReverseOrdering=True, InPlace=False
)
relabel.UpdateLargestPossibleRegion()
r = relabel.GetOutput()
l_list: List[Any] = []
# required because range is overloaded in this module
from builtins import range
for i in range(1, r.GetNumberOfLabelObjects() + 1):
attrs = []
for name in names:
attrs.append(r.GetLabelObject(i).__getattribute__("Get" + name)())
l_list.append(tuple(attrs))
return l_list
def attribute_dict(inputobject, name: str):
"""Returns a dict with the attribute values in keys and a list of the
corresponding objects in value
i: the input LabelImage
name: the name of the attribute
"""
import itk
img = itk.output(inputobject)
relabel = itk.StatisticsRelabelLabelMapFilter[img].New(
img, Attribute=name, ReverseOrdering=True, InPlace=False
)
relabel.UpdateLargestPossibleRegion()
r = relabel.GetOutput()
d = {}
# required because range is overloaded in this module
from builtins import range
for i in range(1, r.GetNumberOfLabelObjects() + 1):
lo = r.GetLabelObject(i)
v = lo.__getattribute__("Get" + name)()
l_list = d.get(v, [])
l_list.append(lo)
d[v] = l_list
return d
def number_of_objects(image_or_filter) -> int:
"""Returns the number of objects in the image.
img: the input LabelImage
"""
import itk
image_or_filter.UpdateLargestPossibleRegion()
img = itk.output(image_or_filter)
return img.GetNumberOfLabelObjects()
def ipython_kw_matches(text: str):
"""Match named ITK object's named parameters"""
import IPython
import itk
import re
import inspect
from itk.support import template_class
regexp = re.compile(
r"""
'.*?' | # single quoted strings or
".*?" | # double quoted strings or
\w+ | # identifier
\S # other characters
""",
re.VERBOSE | re.DOTALL,
)
ip = IPython.get_ipython()
if "." in text: # a parameter cannot be dotted
return []
# 1. Find the nearest identifier that comes before an unclosed
# parenthesis e.g. for "foo (1+bar(x), pa", the candidate is "foo".
if ip.Completer.readline:
text_until_cursor = ip.Completer.readline.get_line_buffer()[
: ip.Completer.readline.get_endidx()
]
else:
# IPython >= 5.0.0, which is based on the Python Prompt Toolkit
text_until_cursor = ip.Completer.text_until_cursor
tokens = regexp.findall(text_until_cursor)
tokens.reverse()
iter_tokens = iter(tokens)
open_par = 0
for token in iter_tokens:
if token == ")":
open_par -= 1
elif token == "(":
open_par += 1
if open_par > 0:
# found the last unclosed parenthesis
break
else:
return []
# 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" )
ids = []
is_id = re.compile(r"\w+$").match
while True:
try:
ids.append(iter_tokens.next())
if not is_id(ids[-1]):
ids.pop()
break
if not iter_tokens.next() == ".":
break
except StopIteration:
break
# lookup the candidate callable matches either using global_matches
# or attr_matches for dotted names
if len(ids) == 1:
callable_matches = ip.Completer.global_matches(ids[0])
else:
callable_matches = ip.Completer.attr_matches(".".join(ids[::-1]))
arg_matches = []
for callable_match in callable_matches:
# drop the .New at this end, so we can search in the class members
if callable_match.endswith(".New"):
callable_match = callable_match[:-4]
elif not re.findall("([A-Z])", callable_match): # True if snake case
# Split at the last '.' occurrence
split_name_parts = callable_match.split(".")
namespace = split_name_parts[:-1]
function_name = split_name_parts[-1]
# Find corresponding object name
object_name = snake_to_camel_case(function_name)
# Check that this object actually exists
try:
object_callable_match = ".".join(namespace + [object_name])
eval(object_callable_match, ip.Completer.namespace)
# Reconstruct full object name
callable_match = object_callable_match
except AttributeError:
# callable_match is not a snake case function with a
# corresponding object.
pass
try:
l_object = eval(callable_match, ip.Completer.namespace)
if isinstance(l_object, template_class.itkTemplate):
# this is a template - lets grab the first entry to search for
# the methods
l_object = l_object.values()[0]
named_args = []
is_in: bool = isinstance(l_object, itk.LightObject)
if is_in or (
inspect.isclass(l_object) and issubclass(l_object, itk.LightObject)
):
named_args = [n[3:] for n in dir(l_object) if n.startswith("Set")]
except Exception as e:
print(e)
continue
for namedArg in named_args:
if namedArg.startswith(text):
arg_matches.append(f"{namedArg}=")
return arg_matches
def template(cl):
"""Return the template of a class (or of the class of an object) and
its parameters
template() returns a tuple with 2 elements:
- the first one is the itkTemplate object
- the second is a tuple containing the template parameters
"""
from itk.support.template_class import itkTemplateBase
return itkTemplateBase.__template_instantiations_object_to_name__[class_(cl)]
def ctype(s: str) -> "itkt.itkCType":
"""Return the c type corresponding to the string passed in parameter
The string can contain some extra spaces.
see also itkCType
"""
from itk.support.types import itkCType
ret = itkCType.GetCType(" ".join(s.split()))
if ret is None:
raise KeyError(f"Unrecognized C type '{s}'")
return ret
def class_(obj):
"""Return a class from an object
Often in itk, the __class__ is not what the user is expecting.
class_() should do a better job
"""
import inspect
if inspect.isclass(obj):
# obj is already a class !
return obj
else:
return obj.__class__
def python_type(object_ref) -> str:
"""Returns the Python type name of an object
The Python name corresponding to the given instantiated object is printed.
This includes both the Python name and the parameters of the object. A user
can copy and paste the printed value to instantiate a new object of the
same type."""
from itk.support.template_class import itkTemplate
from itk.support.types import itkCType
def in_itk(name):
import itk
# Remove "itk::" and "std::" from template name.
# Only happens for ITK objects.
shortname: str = name.split("::")[-1]
shortname = shortname.split("itk")[-1]
namespace = itk
# A type cannot be part of ITK if its name was not modified above. This
# check avoids having an input of type `list` and return `itk.list` that
# also exists.
likely_itk: bool = shortname != name or name[:3] == "vnl"
if likely_itk and hasattr(namespace, shortname):
return namespace.__name__ + "." + shortname # Prepend name with 'itk.'
else:
return name
def recursive(l_obj, level: int):
try:
type_name, param_list = template(l_obj)
name = in_itk(type_name.__name__)
parameters = []
for t in param_list:
parameters.append(recursive(t, level + 1))
return name + "[" + ",".join(parameters) + "]"
except KeyError:
if isinstance(l_obj, itkCType): # Handles CTypes differently
return "itk." + l_obj.short_name
elif hasattr(l_obj, "__name__"):
# This should be where most ITK types end up.
return in_itk(l_obj.__name__)
elif (
not isinstance(l_obj, type)
and type(l_obj) != itkTemplate
and level != 0
):
# l_obj should actually be considered a value, not a type,
# or it is already an itkTemplate type.
# A value can be an integer that is a template parameter.
# This does not happen at the first level of the recursion
# as it is not possible that this object would be a template
# parameter. Checking the level `0` allows e.g. to find the
# type of an object that is a `list` or an `int`.
return str(l_obj)
else:
return in_itk(type(l_obj).__name__)
return recursive(object_ref, 0)
class TemplateTypeError(TypeError):
def __init__(self, template_type, input_type):
def tuple_to_string_type(t):
if type(t) == tuple:
return ", ".join(python_type(x) for x in t)
else:
python_type(t)
import itk
# Special case for ITK readers: Add extra information.
extra_eg: str = ""
if template_type in [
itk.ImageFileReader,
itk.ImageSeriesReader,
itk.MeshFileReader,
]:
extra_eg = """
or
e.g.: image = itk.imread(my_input_filename, itk.F)
"""
python_template_type = python_type(template_type)
python_input_type = tuple_to_string_type(input_type)
type_list = "\n".join([python_type(x[0]) for x in template_type.keys()])
eg_type = ", ".join([python_type(x) for x in list(template_type.keys())[0]])
msg: str = (
"""{template_type} is not wrapped for input type `{input_type}`.
To limit the size of the package, only a limited number of
types are available in ITK Python. To print the supported
types, run the following command in your python environment:
{template_type}.GetTypes()
Possible solutions:
* If you are an application user:
** Convert your input image into a supported format (see below).
** Contact developer to report the issue.
* If you are an application developer, force input images to be
loaded in a supported pixel type.
e.g.: instance = {template_type}[{eg_type}].New(my_input){extra_eg}
* (Advanced) If you are an application developer, build ITK Python yourself and
turned to `ON` the corresponding CMake option to wrap the pixel type or image
dimension you need. When configuring ITK with CMake, you can set
`ITK_WRAP_${{type}}` (replace ${{type}} with appropriate pixel type such as
`double`). If you need to support images with 4 or 5 dimensions, you can add
these dimensions to the list of dimensions in the CMake variable
`ITK_WRAP_IMAGE_DIMS`.
Supported input types:
{type_list}
""".format(
template_type=python_template_type,
input_type=python_input_type,
type_list=type_list,
eg_type=eg_type,
extra_eg=extra_eg,
)
)
TypeError.__init__(self, msg)
# install progress callback and custom completer if we are in ipython
# interpreter
try:
import itkConfig
import IPython
if IPython.get_ipython():
IPython.get_ipython().Completer.matchers.insert(0, ipython_kw_matches)
# some cleanup
del itkConfig, IPython
except (ImportError, AttributeError):
# fail silently
pass
|