Engines¶
BaseEngine
¶
Bases: object
Base class providing shared functionality for PtyLab reconstruction engines.
BaseEngine coordinates operations that are common to different
reconstruction algorithms, including wave propagation, intensity
projection, error evaluation, reconstruction constraints, position
correction, monitoring, and CPU/GPU data transfer.
The engine operates on shared Reconstruction, ExperimentalData,
Params, and Monitor objects. These objects are referenced directly
rather than copied, so updates performed by the engine are reflected in
the associated reconstruction state.
Specific reconstruction engines inherit from this class and implement
their algorithm-specific reconstruction and update steps. BaseEngine
is therefore generally not intended to be instantiated directly.
Many methods in BaseEngine implement functionality controlled by user-facing
Params switches. The Params API documentation describes when and why these
options are used, while the corresponding BaseEngine methods document their
implementation details.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reconstruction
|
Reconstruction
|
Mutable reconstruction state containing the current object, probe, geometry, and reconstruction results. |
required |
experimentalData
|
ExperimentalData
|
Experimental diffraction data and acquisition geometry. |
required |
params
|
Params
|
Reconstruction parameters controlling propagation, constraints, correction methods, GPU usage, and other algorithm settings. |
required |
monitor
|
Monitor
|
Monitor used to visualize and report reconstruction progress. |
required |
Attributes:
betaObject (float):
Object update weight used by shared object-update routines.
Initialized to ``0.25``.
Source code in PtyLab/Engines/BaseEngine.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 | |
_initialProbePowerCorrection()
¶
Scale the initial probe to the measured diffraction power.
When probePowerCorrectionSwitch is enabled, rescale the complex probe
\(P\) using the measured amplitude scale \(P_{\max}\):
The sum in the normalization includes every element of the probe array. The amplitude scale is derived from the brightest measured diffraction pattern:
Here, \(I_j(x,y)\) is the measured intensity at detector pixel \((x,y)\) in frame \(j\). The correction preserves the probe shape and phase while placing its total power on the scale of the experimental data, reducing large amplitude corrections at the beginning of reconstruction.
Source code in PtyLab/Engines/BaseEngine.py
_probeWindow()
¶
Create the spatial window used by probe-boundary constraints.
For absorbingProbeBoundary, a smooth super-Gaussian window is defined as
where
For probeBoundary, a circular support window is generated from the
entrance pupil diameter.
The generated window is stored in probeWindow and applied later by
applyConstraints().
Source code in PtyLab/Engines/BaseEngine.py
update_data(experimentalData, reconstruction=None)
¶
Update the data objects referenced by the engine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
experimentalData
|
ExperimentalData
|
Experimental dataset to use for subsequent reconstruction steps. |
required |
reconstruction
|
Reconstruction
|
Replacement reconstruction state. If None, the current reconstruction object is retained. |
None
|
Source code in PtyLab/Engines/BaseEngine.py
setPositionOrder()
¶
Set the order in which scan positions are processed.
The ordering is controlled by params.positionOrder:
"sequential"processes frames in their original order."random"uses the original order during the first two iterations and randomly shuffles the positions afterwards."NA"sorts positions by their distance from the scan center, processing central positions first. This ordering is intended for FPM, where central illumination angles correspond to bright-field data.
The resulting frame indices are stored in positionIndices.
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in PtyLab/Engines/BaseEngine.py
convert2single()
¶
Configure single-precision data types for reconstruction arrays.
This method sets the target complex and real data types to
numpy.complex64 and numpy.float32 and delegates the actual
conversion to dtype-matching helpers.
Source code in PtyLab/Engines/BaseEngine.py
object2detector(esw=None)
¶
Propagate the exit surface wave from the object plane to the detector plane.
If esw is not provided, reconstruction.esw is used. The propagation
is performed by the operator selected through the reconstruction
parameters, and the propagated detector-plane field is stored in
reconstruction.ESW.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
esw
|
ndarray
|
Object-plane exit surface wave. If None, use
|
None
|
Source code in PtyLab/Engines/BaseEngine.py
detector2object(ESW=None)
¶
Propagate the detector-plane field back to the object plane.
If ESW is not provided, reconstruction.ESW is used. The
back-propagated exit surface wave is stored in reconstruction.esw,
together with the corresponding update field in
reconstruction.eswUpdate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ESW
|
ndarray
|
Detector-plane wavefield. If None, use
|
None
|
Source code in PtyLab/Engines/BaseEngine.py
getBeamWidth()
¶
Estimate the probe beam width from the second moment of its intensity.
The probe intensity is summed over the non-spatial dimensions and normalized as
The intensity-weighted centroid and variance are then calculated, for example along \(x\) as
and converted to a Gaussian-equivalent full width at half maximum:
The same calculation is applied along \(y\).
Returns:
| Type | Description |
|---|---|
tuple
|
|
Notes
For non-Gaussian probe profiles, the returned values should be interpreted as second-moment Gaussian-equivalent beam widths rather than direct half-maximum widths.
Source code in PtyLab/Engines/BaseEngine.py
getOverlap(ind1, ind2)
¶
Estimate the probe overlap between two scan positions.
The physical displacement between the two positions is calculated from their pixel-coordinate difference and the probe-plane pixel size:
The linear overlap is estimated from the radial scan displacement and the smaller of the reconstructed probe widths:
where \(w_x\) and \(w_y\) are the Gaussian-equivalent probe widths returned
by getBeamWidth().
The area overlap is calculated from the normalized autocorrelation of the probe amplitude. Let
and
Using the Fourier correlation theorem, the normalized area overlap is
This definition is invariant to the absolute centering of the probe and yields an overlap of one for zero displacement.
The current implementation uses the first probe mode and the last slice when evaluating the area overlap.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ind1
|
int
|
Index of the first scan position. |
required |
ind2
|
int
|
Index of the second scan position. |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
|
Notes
The calculated values are also stored in
reconstruction.linearOverlap and reconstruction.areaOverlap.
Source code in PtyLab/Engines/BaseEngine.py
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 | |
getErrorMetrics()
¶
Compute the normalized reconstruction error for the current iteration.
For each scan position \(j\), the detector-domain error is summed over all detector pixels:
where \(E_j(x,y)\) is the absolute difference between measured and estimated
detector intensities. If FourierMaskSwitch is enabled, the detector error
is weighted by the Fourier mask \(W\):
The error at each scan position is normalized by the measured diffraction energy:
The total error for the current iteration is then
and is appended to reconstruction.error.
Notes
If saveMemory is enabled, the per-position errors are accumulated
during the reconstruction loop instead of storing the full detector
error array.
Source code in PtyLab/Engines/BaseEngine.py
getRMSD(positionIndex)
¶
Compute the detector-domain intensity error for one scan position.
The pixel-wise detector error is defined as
If saveMemory is disabled, the full detector-error map is stored in
reconstruction.detectorError.
If saveMemory is enabled, the detector error is reduced immediately to
a per-position scalar:
When FourierMaskSwitch is enabled, the masked error is used instead:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
positionIndex
|
int
|
Index of the current scan position. |
required |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If |
Notes
Despite its name, this method does not currently compute a root mean square deviation. It computes an absolute detector-intensity difference.
Source code in PtyLab/Engines/BaseEngine.py
intensityProjection(positionIndex)
¶
Apply the detector-plane intensity constraint for one scan position.
The current exit surface wave is first propagated to the detector plane. The estimated intensity is calculated from the propagated field as
For the standard intensity constraint, the detector-plane field is scaled by
so that
Alternative projection rules are selected through
params.intensityConstraint. The current implementation supports
"standard", "fluctuation", "exponential",
"poisson", and "interferometric".
Depending on the active parameters, the method may additionally apply constrained-pixel-sum decompression, adaptive denoising, Fourier masking, background estimation, or an interferometric reference update.
The constrained detector-plane field is finally propagated back to the object plane.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
positionIndex
|
int
|
Index of the current diffraction frame. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in PtyLab/Engines/BaseEngine.py
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 | |
decompressionProjection(positionIndex)
¶
Construct a high-resolution measured intensity for the
constrained-pixel-sum projection.
The current high-resolution estimated intensity is divided into blocks
of size \(s \times s\), where \(s\) is CPSCupsamplingFactor.
For each detector block, the predicted low-resolution intensity is
A block-wise correction factor is calculated from the measured low-resolution diffraction intensity:
The factor is expanded over the corresponding high-resolution block and used to construct an effective high-resolution measured intensity:
This preserves the current estimate of the sub-pixel intensity distribution while enforcing that the summed intensity in each high-resolution block matches the experimentally measured low-resolution pixel intensity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
positionIndex
|
int
|
Index of the current diffraction frame. |
required |
Notes
The generated high-resolution intensity is stored in
reconstruction.Imeasured. When FourierMaskSwitch is enabled,
the CPSC correction is restricted by experimentalData.W after
the first five reconstruction iterations.
Source code in PtyLab/Engines/BaseEngine.py
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 | |
showReconstruction(loop, force=False)
¶
Update reconstruction monitoring and optional iteration output.
The object, probe, reconstruction error, propagation distance, mode
purities, scan positions, and beam width are sent to the active monitor
at intervals defined by monitor.figureUpdateFrequency.
For Fourier ptychography, the object is transformed to real space before
visualization. When the monitor verbosity is set to "high", the
current measured and estimated diffraction intensities and the estimated
scan overlap are also sent, and the last iteration prints a summary of
the final error and overlap.
If object dumping is enabled, the current reconstructed object is written to disk for each iteration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loop
|
int
|
Current reconstruction iteration. |
required |
force
|
bool
|
Force an immediate monitor update regardless of
|
False
|
Source code in PtyLab/Engines/BaseEngine.py
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 | |
positionCorrection(objectPatch, positionIndex, sy, sx)
¶
Estimate the position correction for one scan position.
The current object patch is compared with the corresponding region of the reconstructed object using cross-correlation. For small search radii, shifted object patches are evaluated directly. For larger search radii, the cross-correlation is calculated in the Fourier domain.
A correlation-weighted displacement estimate is obtained from the tested shifts. Conceptually,
and similarly for \(g_y\), where \(C_k\) is the correlation obtained for the candidate shift \((\Delta y_k,\Delta x_k)\).
The estimated correction is scaled by the position-correction feedback
factor and accumulated into the position search direction used by
positionCorrectionUpdate().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objectPatch
|
ndarray
|
Current reconstructed object patch for the scan position. |
required |
positionIndex
|
int
|
Index of the current scan position. |
required |
sy
|
slice
|
Row slice locating the current object patch in the full object. |
required |
sx
|
slice
|
Column slice locating the current object patch in the full object. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Estimated position correction |
Source code in PtyLab/Engines/BaseEngine.py
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 | |
position_update_to_change_in_z(loop)
¶
Map the global scaling of corrected scan positions to an update of the propagation distance.
The corrected and original encoder positions are centered and their relative spatial scale is estimated as
A corresponding target propagation distance is estimated as
Rather than jumping to \(z_{\mathrm{target}}\), the distance moves a fixed fraction \(\alpha = 0.2\) of the way there on each call, so that noisy position estimates do not make it jump:
After updating the propagation distance, the corrected scan positions are rescaled around their center so that the global scale change is transferred from the position correction to the propagation distance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loop
|
int
|
Current reconstruction iteration. |
required |
Notes
This method is used together with position correction and is called
periodically when map_position_to_z_change is enabled.
Source code in PtyLab/Engines/BaseEngine.py
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 | |
positionCorrectionUpdate()
¶
Apply the accumulated position corrections to the scan coordinates.
Position corrections estimated by positionCorrection() are stored in
D in pixel units and are applied after the position-correction warm-up.
For conventional ptychography, the corrected encoder coordinates are updated as
where \(\alpha\) is the adaptive step size and \(d_{xo}\) is the object-plane pixel size. The corrected scan grid is then recentered to the mean of the original encoder positions.
For Fourier ptychography, the corrected Fourier-space positions are converted back to illumination coordinates using the inverse FPM illumination geometry:
with
Notes
The method updates reconstruction.encoder_corrected in place and is
called when position correction is enabled.
Source code in PtyLab/Engines/BaseEngine.py
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 | |
applyConstraints(loop)
¶
Apply enabled reconstruction constraints after an iteration.
This method acts as the central dispatcher for object, probe, position,
and autofocus constraints selected through Params. Depending on the
active switches, it may apply regularization, probe normalization,
modal orthogonalization, probe-boundary constraints, smoothing,
amplitude constraints, spectral coupling, position correction, or
autofocus updates.
Constraints are applied sequentially in the order defined by this method, so enabling multiple constraints may cause later operations to act on the result of earlier ones.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loop
|
int
|
Current reconstruction iteration. |
required |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If PSD estimation is enabled. |
Source code in PtyLab/Engines/BaseEngine.py
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 | |
orthogonalization()
¶
Orthogonalize mixed-state probe or object modes.
For multiple probe modes, the reconstructed modes are transformed to an orthogonal modal basis independently for each wavelength and slice. Conceptually, the transformed modes are
where \(U\) is the modal transformation returned by orthogonalizeModes().
The normalized modal eigenvalues are used to calculate the probe purity:
where
are the normalized modal eigenvalues. A purity of one corresponds to a single dominant mode, while lower values indicate a stronger mixed-state contribution.
If momentum acceleration is enabled, the same modal transformation is applied to the corresponding momentum and buffer arrays so that all reconstruction state variables remain expressed in the same modal basis.
If only multiple object modes are present, the analogous procedure is
applied to the object modes and purityObject is updated.
Notes
Probe modes are orthogonalized independently for each wavelength and
slice. The resulting probe purity is stored in
reconstruction.purityProbe and appended to
reconstruction.purityProbeHist.
Source code in PtyLab/Engines/BaseEngine.py
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 | |
comStabilization()
¶
Stabilize the probe center of mass to suppress translational drift.
Ptychographic reconstruction contains a translational ambiguity that can allow the reconstructed probe and object to drift within their numerical arrays without strongly affecting the data consistency. This method estimates the probe center relative to the center of the reconstruction window and shifts the reconstruction when the displacement exceeds approximately one pixel.
The current probe center is estimated as
and
where \(A(x,y)\) is the probe amplitude and \(d_{xp}\) is the probe-plane pixel size. The resulting coordinates are rounded to integer pixel shifts.
If the probe center is displaced by more than approximately one pixel,
both the probe and object are shifted by (-yc, -xc) to recenter the
reconstruction while preserving their relative spatial registration.
When momentum acceleration is enabled, the corresponding probe and object momentum and buffer arrays are shifted by the same amount.
Notes
For multislice reconstruction, the last probe slice is used to estimate the center.
Source code in PtyLab/Engines/BaseEngine.py
2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 | |
modulusEnforcedProbe()
¶
Constrain the reconstructed probe using a measured empty-beam intensity.
The current probe is propagated to the detector plane, where its
estimated intensity is compared with experimentalData.emptyBeam.
The detector-plane probe is scaled by
and updated as
so that the measured detector-plane amplitude is enforced while the current phase estimate is retained.
The constrained field is then propagated back to the probe plane and
stored in reconstruction.probe.
If FourierMaskSwitch is enabled, the modulus constraint is applied only
inside the active Fourier mask, while the detector-plane field outside
the mask is left unchanged.
Notes
This constraint requires experimentalData.emptyBeam, containing a
measured detector intensity of the illumination without the sample.
Source code in PtyLab/Engines/BaseEngine.py
2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 | |
adaptiveDenoising()
¶
Apply an adaptive amplitude-domain noise-floor correction.
The measured and estimated detector intensities are converted to amplitudes.
A global noise level is estimated from their mean amplitude difference:
The estimated noise floor is subtracted from the measured amplitude and negative values are clipped to zero:
The corrected measured intensity is then reconstructed as
Notes
This method modifies reconstruction.Imeasured in place and is
applied before the detector-plane intensity constraint.
Source code in PtyLab/Engines/BaseEngine.py
objectPatchUpdate_TV(objectPatch, DELTA)
¶
Apply total-variation regularization to the engine-specific object update.
The data-driven object update is first calculated using the current
engine's objectPatchUpdate() implementation:
where \(U_{\mathrm{engine}}\) denotes the object-update rule implemented by
the active reconstruction engine. For example, ePIE uses the standard
ePIE update, while mPIE uses its regularized mPIE/rPIE update.
A total-variation regularization term is then added:
where \(G_{\mathrm{TV}}(O_j)\) is calculated by grad_TV() using
epsilon=1e-2, \(\beta_O\) is the engine's object update step size, and
\(\lambda\) is controlled by params.objectTVregStepSize.
This design keeps the TV regularization independent of the underlying reconstruction engine, allowing different engines to retain their native object-update rules while sharing the same TV regularizer.
The activation and application frequency of this update are controlled by
params.objectTVregSwitch and params.objectTVfreq in the reconstruction
loop.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objectPatch
|
ndarray
|
Current object patch at the active scan position. |
required |
DELTA
|
ndarray
|
Exit-wave correction, typically
|
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Engine-specific object update with the additional TV regularization term. |
Source code in PtyLab/Engines/BaseEngine.py
2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 | |
ePIE
¶
Bases: BaseEngine
Extended Ptychographical Iterative Engine (ePIE).
ePIE jointly reconstructs the complex object and illumination probe by iterating over overlapping scan positions.1 For each position \(j\), the exit surface wave is formed as
where \(O_j\) is the object patch illuminated by the probe \(P\).
After propagation to the detector plane and application of the measured intensity constraint, the corrected exit wave \(\Psi'_j\) is propagated back to the object plane. The resulting exit-wave difference is
In the classical single-mode ePIE formulation, the object and probe are updated according to
and
where \(\beta_O\) and \(\beta_P\) control the object and probe update step sizes.
The PtyLab implementation generalizes these updates to its multidimensional reconstruction representation by summing the corresponding contributions over the relevant wavelength, mode, and slice dimensions.
The default ePIE settings are betaObject = 0.25,
betaProbe = 0.25, and numIterations = 50.
-
A. M. Maiden and J. M. Rodenburg, "An improved ptychographical phase retrieval algorithm for diffractive imaging," Ultramicroscopy 109, 1256-1262 (2009). https://doi.org/10.1016/j.ultramic.2009.05.012 ↩
Source code in PtyLab/Engines/ePIE.py
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 | |
initializeReconstructionParams()
¶
Initialize ePIE-specific reconstruction parameters.
Defaults
betaObject (float): Object update step size. Default is 0.25. betaProbe (float): Probe update step size. Default is 0.25. numIterations (int): Number of reconstruction iterations. Default is 50.
Source code in PtyLab/Engines/ePIE.py
reconstruct(experimentalData=None)
¶
Run the ePIE reconstruction to completion.
This method consumes the generator returned by reconstruct_stepwise()
until all reconstruction iterations and scan positions have been
processed.
Use reconstruct_stepwise() when custom operations need to be inserted
between individual scan-position updates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
experimentalData
|
ExperimentalData
|
Experimental dataset to use for the reconstruction. If provided, it replaces the currently attached experimental data. |
None
|
Source code in PtyLab/Engines/ePIE.py
reconstruct_stepwise(experimentalData=None)
¶
Run the ePIE reconstruction one scan-position update at a time.
For each reconstruction iteration, the scan positions are visited in the
order selected by params.positionOrder. At each position \(j\), the
corresponding object patch is extracted and combined with the current
probe to form the exit surface wave:
The exit wave is propagated to the detector plane, constrained by the
measured diffraction intensity through intensityProjection(), and
propagated back to obtain an updated exit wave \(\Psi'_j\).
The exit-wave correction is
By default, the object is updated using the standard ePIE rule implemented
by objectPatchUpdate().
If params.objectTVregSwitch is enabled, the TV-regularized update
objectPatchUpdate_TV() is used every params.objectTVfreq iterations.
The standard ePIE object update is retained and an additional TV
regularization term is added with strength controlled by
params.objectTVregStepSize.
The probe is updated using probeUpdate() after each object update.
If params.OPRP is enabled, position-dependent probe estimates are
retrieved from reconstruction.probe_storage before each scan-position
update and stored again after the probe update. Without OPRP, a shared
probe estimate is updated sequentially across all scan positions.
After all scan positions in an iteration have been processed,
getErrorMetrics() evaluates the reconstruction error,
applyConstraints() applies the enabled reconstruction constraints and
showReconstruction() updates the monitor.
The method yields after every scan-position update, allowing custom code to be interleaved with the reconstruction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
experimentalData
|
ExperimentalData
|
Experimental dataset to use for the reconstruction. If provided, it replaces the currently attached experimental data. |
None
|
Yields:
| Type | Description |
|---|---|
tuple
|
|
Source code in PtyLab/Engines/ePIE.py
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 | |
objectPatchUpdate(objectPatch, DELTA)
¶
Update the object patch using the ePIE object-update rule.
For the classical single-mode case, the probe weighting is
and the object patch is updated according to
where \(O_j\) is the current object patch, \(\Delta\Psi_j\) is the
exit-wave correction obtained from the detector-plane intensity
constraint, and \(\beta_O\) is betaObject.
In the multidimensional PtyLab representation, contributions from the relevant wavelength, probe-mode, and slice dimensions are summed before updating the object patch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objectPatch
|
ndarray
|
Current object patch at the active scan position. |
required |
DELTA
|
ndarray
|
Exit-wave correction
|
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Updated object patch. |
Source code in PtyLab/Engines/ePIE.py
probeUpdate(objectPatch, DELTA)
¶
Update the probe using the ePIE probe-update rule.
For the classical single-mode case, the object weighting is
and the probe is updated according to
where \(O_j\) is the current object patch, \(\Delta\Psi_j\) is the
exit-wave correction obtained from the detector-plane intensity
constraint, and \(\beta_P\) is betaProbe.
In the multidimensional PtyLab representation, the implemented update
sums the corresponding correction over axes (0, 1, 3) while preserving
the probe-mode dimension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objectPatch
|
ndarray
|
Current object patch at the active scan position. |
required |
DELTA
|
ndarray
|
Exit-wave correction
|
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Updated probe. |
Source code in PtyLab/Engines/ePIE.py
mPIE
¶
Bases: BaseEngine
Momentum-accelerated ptychographic iterative engine (mPIE).
mPIE extends the standard ePIE reconstruction by combining regularized PIE (rPIE) object/probe updates with momentum acceleration.1
As in ePIE, the exit-wave correction at scan position \(j\) is
For conventional ptychography, the object update is regularized as
where \(P_{\max}=\max |P|^2\), \(\beta_O\) controls the object update step size, and \(\alpha_O\) controls the spatial regularization.
The probe is updated analogously:
where \(O_{\max}=\max |O_j|^2\), and \(\alpha_P\) and \(\beta_P\) control probe regularization and update strength.
Compared with ePIE, these denominators retain stronger updates in moderately illuminated regions while suppressing unstable updates where the corresponding probe or object intensity is small.
Momentum acceleration is periodically applied to both object and probe. In the PtyLab implementation, the momentum state is updated as
followed by
where \(X\) denotes the object or probe, \(\eta\) is frictionM, and
\(\gamma\) is feedbackM. The current implementation applies these
momentum updates stochastically during the scan-position loop.
The default mPIE parameters are betaObject = 0.25,
betaProbe = 0.25, alphaObject = 0.1, alphaProbe = 0.1,
feedbackM = 0.3, and frictionM = 0.7.
-
A. M. Maiden, D. Johnson, and P. Li, "Further improvements to the ptychographical iterative engine," Optica 4, 736-745 (2017). https://doi.org/10.1364/OPTICA.4.000736 ↩
Attributes:
| Name | Type | Description |
|---|---|---|
keepPatches |
bool
|
If enabled, store the reconstructed object patch associated with each scan position for debugging or detailed analysis. This option can require substantial additional memory. |
See Also:
ePIE
Baseline ePIE reconstruction without rPIE regularization or
momentum acceleration.
Source code in PtyLab/Engines/mPIE.py
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 | |
keepPatches
property
writable
¶
Whether to store the reconstructed object patch for every scan position.
Enable with engine.keepPatches = True and disable with engine.keepPatches = False.
This option is intended for debugging or detailed analysis and may require a large amount of additional memory.
initializeReconstructionParams()
¶
Initialize mPIE-specific reconstruction parameters and momentum state.
The default mPIE parameters are:
betaObject = 0.25: object update step size.betaProbe = 0.25: probe update step size.alphaObject = 0.1: object-update regularization parameter.alphaProbe = 0.1: probe-update regularization parameter.feedbackM = 0.3: momentum feedback strength.frictionM = 0.7: momentum memory coefficient.numIterations = 50: number of reconstruction iterations.
Object and probe momentum arrays are initialized together with corresponding buffers that store the reconstruction state used by the momentum updates.
Source code in PtyLab/Engines/mPIE.py
reconstruct(experimentalData=None, reconstruction=None, vis_after_each_iteration=None)
¶
Run the mPIE reconstruction to completion.
The reconstruction follows the standard ptychographic position loop. At each scan position \(j\), the current object patch and probe form the exit surface wave
which is propagated to the detector plane and constrained by the measured
diffraction intensity through intensityProjection(). The corresponding
exit-wave correction is
The object and probe are then updated using the mPIE/rPIE update rules
implemented by objectPatchUpdate() and probeUpdate().
If params.objectTVregSwitch is enabled, the TV-regularized object update
objectPatchUpdate_TV() is used every params.objectTVfreq iterations.
The native mPIE object update is retained and an additional TV
regularization term is added with strength controlled by
params.objectTVregStepSize.
If params.weigh_probe_updates_by_intensity is enabled, the probe update
is scaled by the relative intensity of the current diffraction frame.
If params.positionCorrectionSwitch is enabled, scan-position correction
is applied after the object and probe updates.
Momentum acceleration is applied stochastically during the scan-position
loop. In the current implementation, each position update has
approximately a 5% probability of triggering objectMomentumUpdate() and
probeMomentumUpdate().
If keepPatches is enabled, the reconstructed patch associated with each
scan position is additionally stored in self.patches for diagnostics or
further analysis.
After all scan positions in an iteration have been processed,
getErrorMetrics() evaluates the reconstruction error,
applyConstraints() applies the enabled reconstruction constraints, and
showReconstruction() updates the reconstruction monitor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
experimentalData
|
ExperimentalData
|
Experimental dataset to use for the reconstruction. If provided, it replaces the currently attached experimental data. |
None
|
reconstruction
|
Reconstruction
|
Reconstruction state to optimize. If provided, it replaces the currently attached reconstruction object. |
None
|
vis_after_each_iteration
|
callable
|
Callback executed after each reconstruction iteration as
|
None
|
Notes
The object and probe momentum buffers are reset after
_prepareReconstruction() so that they remain synchronized with any
initialization changes applied before reconstruction starts.
Source code in PtyLab/Engines/mPIE.py
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 | |
objectMomentumUpdate()
¶
Apply the mPIE momentum update to the reconstructed object.
The change in the object since the previous momentum update is estimated from the stored object buffer:
The object momentum is updated according to
where \(\eta\) is frictionM.
The accumulated momentum is then fed back into the object estimate:
where \(\gamma\) is feedbackM.
After the momentum correction, objectBuffer is updated with the current
object estimate for the next momentum step.
Notes
This update is triggered stochastically from reconstruct() rather
than after every scan-position update.
Source code in PtyLab/Engines/mPIE.py
probeMomentumUpdate()
¶
Apply the mPIE momentum update to the reconstructed probe. Similar to objectMomentumUpdate().
See Also
objectMomentumUpdate
Equivalent momentum update applied to the reconstructed object.
Source code in PtyLab/Engines/mPIE.py
objectPatchUpdate(objectPatch, DELTA)
¶
Update the object patch using the regularized mPIE/rPIE object-update rule.
The probe intensity is first evaluated and its maximum value is used as a global normalization scale:
For conventional ptychography, the probe weighting is
and the object patch is updated according to
where \(\Delta\Psi_j\) is the exit-wave correction, \(\beta_O\) is
betaObject, and \(\alpha_O\) is alphaObject.
The parameter alphaObject controls the balance between global
normalization by the maximum probe intensity and local normalization by
the spatially varying probe intensity.
For Fourier ptychography (operationMode == "FPM"), an additional
probe-amplitude weighting is applied:
In the multidimensional PtyLab representation, the object correction is summed over the probe-mode axis before being added to the current object patch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objectPatch
|
ndarray
|
Current object patch at the active scan position. |
required |
DELTA
|
ndarray
|
Exit-wave correction
|
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Updated object patch. |
Source code in PtyLab/Engines/mPIE.py
probeUpdate(objectPatch, DELTA, weight)
¶
Update the probe using the regularized mPIE/rPIE probe-update rule.
The object intensity is first evaluated and its maximum value is used as a global normalization scale:
The object weighting is then calculated as
and the probe is updated according to
where \(\Delta\Psi_j\) is the exit-wave correction, \(\beta_P\) is
betaProbe, \(\alpha_P\) is alphaProbe, and \(w\) is the optional
intensity-dependent update weight.
The parameter alphaProbe controls the balance between global
normalization by the maximum object intensity and local normalization by
the spatially varying object intensity.
By default, \(w=1\). If params.weigh_probe_updates_by_intensity is
enabled in reconstruct(), \(w\) is set to the relative intensity of the
current diffraction frame.
In the current multidimensional PtyLab representation, the probe
correction is summed over axis 1 before being added to the current
probe estimate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objectPatch
|
ndarray
|
Current object patch at the active scan position. |
required |
DELTA
|
ndarray
|
Exit-wave correction
|
required |
weight
|
float
|
Multiplicative weight applied to the probe update. Typically |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Updated probe. |
Source code in PtyLab/Engines/mPIE.py
pcPIE
¶
Bases: mPIE
Backward-compatible wrapper for mPIE.
Position correction is now provided by mPIE through
params.positionCorrectionSwitch. This class is retained only for
compatibility with existing code using Engines.pcPIE.
Source code in PtyLab/Engines/mPIE.py
purityPIE
¶
Bases: mPIE
Purity-based axial-distance and wavelength self-calibration for ptychographic reconstruction.
purityPIE extends mPIE by estimating either the sample-to-detector
axial distance or the illumination wavelength from the reconstructed
probe purity.1
In mixed-state ptychography, the measured diffraction intensity at scan position \(j\) is modeled as an incoherent sum over multiple probe modes:
where \(P_m\) denotes probe mode \(m\), \(O\) is the object transmission function, and \(\mathcal{F}\) denotes propagation to the detector plane.
The mutual intensity of the reconstructed probe is
and can be decomposed into orthonormal coherent modes as
where \(\lambda_m\) represents the power contribution of the corresponding orthonormal mode.
The probe purity is defined as
$$ \mu = \frac{\sqrt{\sum_m \lambda_m^2}}{\sum_m \lambda_m} $$ with \(\mu \in [0,1]\).
An incorrect axial distance or illumination wavelength introduces a mismatch in the diffraction geometry connecting the object and detector planes. This mismatch can produce geometric deformation and resolution degradation in the reconstructed object and probe and is reflected by a reduction in the reconstructed probe purity.
The calibrated parameter is therefore obtained by maximizing the reconstructed probe purity.
For each candidate value, purityPIE creates an independent
reconstruction with the corresponding geometry, performs an mPIE
reconstruction, orthogonalizes the reconstructed probe modes, and
evaluates the resulting probe purity. The candidate yielding the highest
purity is retained as the calibrated reconstruction.
Two calibration targets are supported:
-
Axial-distance calibration: optimizes the sample-to-detector distance
z. -
Wavelength calibration: optimizes the illumination wavelength
λ.
The calibration target is selected through
params.purityCalibrationTarget, which can be set to "z" or
"wavelength".
For either calibration target, two search strategies are available:
-
Fixed-grid search: evaluates uniformly spaced candidate values over a predefined range. Axial-distance scans are controlled by
params.purityZScanRangeandparams.purityZScanPoints, while wavelength scans are controlled byparams.purityWavelengthScanRangeandparams.purityWavelengthScanPoints. -
Adaptive search: starts from the current parameter estimate, determines the direction of increasing probe purity, expands the search step while the purity continues to improve, and then refines the search around the best candidate using progressively smaller steps. Adaptive search is enabled with
params.purityAdaptive = True.
Purity-based calibration requires at least two reconstructed probe modes,
i.e. reconstruction.npsm >= 2.
-
C. Liu, W. Eschen, D. S. Penagos Molina, L. Licht, M. Abdelaal, and J. Rothhardt, "Purity-based self-calibration in ptychography," Opt. Lett. 50, 1581-1584 (2025). https://doi.org/10.1364/OL.543865 ↩
See Also
mPIE
Momentum-accelerated reconstruction engine used for each
candidate calibration value.
Source code in PtyLab/Engines/purityPIE.py
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 | |
reconstruct(experimentalData=None, reconstruction=None)
¶
Run purity-based self-calibration.
The reconstruction first prepares the current object, probe, geometry,
constraints, and optional GPU state using the standard mPIE setup.
Each candidate parameter value is evaluated using an independent mPIE
reconstruction. The reconstructed probe modes are orthogonalized, the
corresponding probe purity is evaluated, and the candidate yielding the
highest purity is retained.
After calibration, the reconstruction state corresponding to the optimal
candidate is restored to the original Reconstruction object.
With the matplotlib Monitor, purity and reconstruction error vs the
calibrated parameter are plotted live during the scan, and the final plot
shades the scan resolution around the maximum. A warning is logged if the
maximum lies at the edge of the scanned range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
experimentalData
|
ExperimentalData
|
Experimental dataset to use for calibration. If provided, it replaces the dataset currently attached to the engine. |
None
|
reconstruction
|
Reconstruction
|
Reconstruction state to calibrate. If provided, it replaces the reconstruction currently attached to the engine. |
None
|
Returns:
| Type | Description |
|---|---|
tuple
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in PtyLab/Engines/purityPIE.py
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 | |
purityZScan()
¶
Perform a fixed-grid purity-based axial calibration.
The scan evaluates a uniformly spaced set of candidate sample-to-detector
distances centered on the current value reconstruction.zo. The scan range
and number of candidate positions are defined by
params.purityZScanRange and params.purityZScanPoints.
Each candidate distance is evaluated using an independent mPIE
reconstruction. The reconstructed probe modes are orthogonalized, and the
candidate yielding the highest probe purity is retained as the calibrated
axial distance.
The reconstruction error for each candidate is recorded together with the
purity metric. After the scan, the reconstruction state corresponding to
the highest-purity candidate is restored to the original Reconstruction
object.
The evaluated distances, purity values, and reconstruction errors are stored
in purityZValues, purityZMetrics, and purityZErrors, respectively.
Returns:
| Type | Description |
|---|---|
tuple
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than two probe modes are reconstructed
( |
RuntimeError
|
If no valid candidate reconstruction produces a purity value. |
Source code in PtyLab/Engines/purityPIE.py
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 | |
adaptivePurityZScan()
¶
Perform an adaptive purity-based axial calibration.
The search starts from the current sample-to-detector distance
reconstruction.zo and evaluates the initial position together with two
neighboring candidates separated by params.purityZInitialStep.
If the reconstructed probe purity increases toward either side, the search continues in that direction while progressively increasing the step size according to
where g = params.purityZStepGrowth.
Once the purity no longer improves significantly, the search switches to a local refinement around the best candidate. The step size is progressively reduced according to
where s = params.purityZStepShrink.
Refinement continues until the step size falls below
params.purityZMinStep or the maximum number of candidate evaluations
defined by params.purityMaxEvaluations is reached. Previously evaluated
axial positions are cached and reused.
Each candidate distance is evaluated using an independent mPIE
reconstruction. The candidate yielding the highest reconstructed probe
purity is retained, and its reconstruction state is restored to the original
Reconstruction object.
The evaluated distances, purity values, and reconstruction errors are stored
in purityZValues, purityZMetrics, and purityZErrors, respectively.
Returns:
| Type | Description |
|---|---|
tuple
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than two probe modes are reconstructed
( |
RuntimeError
|
If no valid candidate reconstruction produces a purity value. |
Source code in PtyLab/Engines/purityPIE.py
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 | |
purityWavelengthScan()
¶
Perform a fixed-grid purity-based wavelength calibration.
Candidate wavelengths are evaluated over a uniformly spaced range defined
by params.purityWavelengthScanRange and
params.purityWavelengthScanPoints. The wavelength yielding the highest
reconstructed probe purity is retained.
Returns:
| Type | Description |
|---|---|
tuple
|
|
See Also
purityZScan
Fixed-grid purity-based axial calibration.
Source code in PtyLab/Engines/purityPIE.py
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 | |
adaptivePurityWavelengthScan()
¶
Perform an adaptive purity-based wavelength calibration.
The search starts from the current wavelength, expands the step in the direction of increasing probe purity, and then refines around the best candidate using progressively smaller steps.
Returns:
| Type | Description |
|---|---|
tuple
|
|
See Also
adaptivePurityZScan
adaptive stepsize purity-based axial distance calibration.
Source code in PtyLab/Engines/purityPIE.py
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 | |
zPIE
¶
Bases: BaseEngine
Ptychographic iterative engine with axial-distance refinement.
zPIE extends the standard PIE reconstruction by jointly refining the object/probe estimate and the axial propagation distance \(z\).1
During reconstruction, a set of candidate axial offsets is generated around the current propagation distance:
For each candidate offset, either the reconstructed object or probe is
propagated to the corresponding defocus plane using angular-spectrum
propagation. The quantity used for axial optimization is selected through
focusObject.
A total-variation-based focus metric is evaluated from the propagated complex field:
where \(U_k\) is the propagated field at axial offset \(\Delta z_k\) and \(\epsilon\) is a small numerical stabilization term.
The axial feedback is calculated from the merit values as
and accumulated using a momentum-like update:
followed by
where \(\gamma\) is zPIEfriction and \(\eta\) is
zPIEgradientStepSize.
The updated axial distance is stored in reconstruction.zo. For
propagation schemes whose sampling depends explicitly on distance, the
corresponding propagation coordinates are updated after each axial
correction.
The object and probe are subsequently updated using the standard PIE object and probe update rules.
Parameters specific to zPIE include:
DoF: depth-of-field scale used to define the axial search range.zPIEgradientStepSize: strength of the axial feedback update.zPIEfriction: momentum retention factor for axial refinement.focusObject: ifTrue, evaluate the focus metric on the object; otherwise evaluate it on the probe.zMomentun: accumulated axial momentum.
-
L. Loetgering, M. Du, K. S. E. Eikema, and S. Witte, "zPIE: an autofocusing algorithm for ptychography," Optics Letters 45, 2030-2033 (2020). https://doi.org/10.1364/OL.389492 ↩
Source code in PtyLab/Engines/zPIE.py
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 | |
initializeReconstructionParams()
¶
Initialize zPIE-specific reconstruction parameters.
The default parameters are:
-
betaProbe = 0.25: Probe update step size used during the PIE reconstruction. -
betaObject = 0.25: Object update step size used during the PIE reconstruction. -
numIterations = 50: Number of reconstruction iterations. -
DoF = reconstruction.DoF: Depth-of-field scale used to define the axial search interval. -
zPIEgradientStepSize = 100: Scaling factor applied to the axial feedback term when updating the sample-to-detector distance. -
zPIEfriction = 0.7: Momentum retention factor used for axial-distance refinement. -
focusObject = True: IfTrue, evaluate the axial focus metric on the reconstructed object. IfFalse, evaluate it on the reconstructed probe. -
zMomentun = 0: Initial accumulated axial momentum used for iterative z refinement.
These values can be modified after creating the zPIE engine and before
calling reconstruct().
Source code in PtyLab/Engines/zPIE.py
show_defocus(viewer=None, scanrange_times_dof=1000, N_points=10)
¶
Visualize a defocus stack of the reconstructed object.
A set of axial offsets is generated symmetrically around the current reconstruction plane:
where s is given by scanrange_times_dof and DoF is taken from
reconstruction.DoF.
For each axial offset, the current reconstructed object is propagated
using angular-spectrum propagation (aspw). The propagated intensity
is calculated as the squared magnitude of the complex field and collected
into a defocus stack.
The resulting stack is displayed in a napari viewer. If no viewer is provided, a new napari viewer is created automatically.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
viewer
|
Viewer
|
Existing napari viewer to which the defocus stack is added.
If |
None
|
scanrange_times_dof
|
float
|
Half-width of the axial scan expressed in units of the
reconstruction depth of field. Default is |
1000
|
N_points
|
int
|
Number of equally spaced axial positions in the defocus stack.
Default is |
10
|
Raises:
| Type | Description |
|---|---|
ImportError
|
If |
Notes
This function is intended as a diagnostic visualization tool and does not modify the reconstructed object or the current axial distance.
Source code in PtyLab/Engines/zPIE.py
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 | |
reconstruct(experimentalData=None, reconstruction=None)
¶
Run the zPIE reconstruction with iterative axial-distance refinement.
The reconstruction jointly updates the object, probe, and sample-to-detector propagation distance. Before the iterative loop starts, the experimental data and reconstruction state are updated if new objects are supplied, followed by the standard reconstruction preparation.
During each zPIE iteration, a set of candidate axial offsets is generated around the current propagation distance:
using 11 equally spaced candidate positions.
Depending on focusObject, either the reconstructed object or probe is
propagated to each candidate defocus plane using angular-spectrum
propagation. A total-variation-based merit value is calculated for each
propagated field.
The axial feedback is then obtained from the weighted candidate offsets:
where \(M_k\) is the focus merit evaluated at candidate offset \(\Delta z_k\).
The feedback is accumulated through the axial momentum update
where \(\gamma\) is zPIEfriction and \(\eta\) is
zPIEgradientStepSize. The propagation distance is then updated as
After the axial update, a standard PIE reconstruction loop is performed over all scan positions. The exit surface wave is calculated from the current object patch and probe, followed by the intensity constraint and object/probe updates.
The current axial distance is stored during reconstruction in
reconstruction.zHistory. The total-variation merit values and candidate
axial offsets are also stored in reconstruction.merit and
reconstruction.dz for diagnostic use.
For propagation schemes whose object-plane sampling depends explicitly on
the propagation distance, the sampling reconstruction.dxp is updated
after each axial correction.
At the end of each iteration, reconstruction error metrics and enabled constraints are evaluated, followed by an update of the reconstruction monitor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
experimentalData
|
ExperimentalData
|
Experimental dataset to use. If provided, it replaces the dataset currently attached to the engine. |
None
|
reconstruction
|
Reconstruction
|
Reconstruction state to optimize. If provided, it replaces the reconstruction currently attached to the engine. |
None
|
See Also
show_defocus
Visualize the reconstructed object over a range of axial defocus
positions.
objectPatchUpdate
Update the reconstructed object patch during the PIE loop.
probeUpdate
Update the reconstructed probe during the PIE loop.
Source code in PtyLab/Engines/zPIE.py
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 | |
objectPatchUpdate(objectPatch, DELTA)
¶
Update the object patch using the standard ePIE object-update rule.
This implementation is identical to ePIE.objectPatchUpdate().
See Also
ePIE.objectPatchUpdate
Standard ePIE object update.
Source code in PtyLab/Engines/zPIE.py
probeUpdate(objectPatch, DELTA)
¶
Update the probe using the standard ePIE probe-update rule.
This implementation is identical to ePIE.probeUpdate().
See Also
ePIE.probeUpdate
Standard ePIE probe update.
Source code in PtyLab/Engines/zPIE.py
aPIE
¶
Bases: BaseEngine
Angle-corrected ptychographic iterative engine (aPIE).
aPIE extends the standard PIE reconstruction by jointly refining the
object, probe, and detector tilt angle. The angular correction is based on
a Luus-Jaakola-type stochastic search combined with momentum
acceleration.1
The detector tilt is described by the angle reconstruction.theta. For
each reconstruction iteration, two candidate angles are evaluated: the
current angle and a randomly perturbed angle within the current search
radius. The diffraction data are transformed into the corresponding
detector coordinates for each candidate, followed by a standard PIE
object/probe reconstruction.
The candidate producing the lower reconstruction error is retained. The angular update is then combined with a momentum term according to
where f = feedback, \gamma = aPIEfriction, and \Delta\theta_k is the
accepted angular change.
The angular search radius is gradually reduced from
thetaSearchRadiusMax to thetaSearchRadiusMin over the reconstruction
iterations, allowing coarse angular exploration at the beginning and
progressively finer correction toward convergence.
The evolution of the estimated detector angle is stored in
reconstruction.thetaHistory.
Angle correction requires an initial value of reconstruction.theta.
With thetaSearch = "scan", the angle is found with thetaScan() instead:
one independent reconstruction per candidate angle on a grid, refined
around the best one.
-
A. de Beurs, L. Loetgering, M. Herczog, M. Du, K. S. E. Eikema, and S. Witte, "aPIE: an angle calibration algorithm for reflection ptychography," Opt. Lett. 47, 1949-1952 (2022). https://doi.org/10.1364/OL.453655 ↩
Source code in PtyLab/Engines/aPIE.py
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 | |
initializeReconstructionParams()
¶
Initialize parameters specific to the aPIE reconstruction.
This method sets the object and probe update strengths, angular momentum parameters, reconstruction length, and angular search-radius limits. It also initializes the detector-angle momentum and history if they are not already present.
A copy of the original ptychogram is stored in
self.ptychogramUntransformed for repeated angle-dependent coordinate
transformations during reconstruction.
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in PtyLab/Engines/aPIE.py
doReconstruction()
¶
Deprecated alias of reconstruct().
resampleDetector(theta)
¶
Resample the measured ptychogram onto the detector grid of a tilt angle.
Each pixel \((x_d, y_d)\) of the regular grid takes the measured intensity at
\(x = T^{-1}(x_d, y_d)\) (see T_inv), interpolated linearly along the rows.
Points that fall outside the detector are set to zero.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
theta
|
float
|
Tilt angle in degrees. |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
The resampled ptychogram |
Source code in PtyLab/Engines/aPIE.py
reconstruct(experimentalData=None, reconstruction=None)
¶
Run the aPIE reconstruction with iterative detector-angle correction.
The reconstruction starts from the current detector angle
reconstruction.theta and gradually reduces the angular search radius
from thetaSearchRadiusMax to thetaSearchRadiusMin over the requested
number of iterations.
At each iteration, two candidate detector angles are evaluated: the current angle and a randomly perturbed angle within the current search radius, both including the accumulated angular momentum.
For each candidate angle, the original diffraction data stored in
ptychogramUntransformed are resampled onto the transformed detector
coordinates with resampleDetector().
After the angle-dependent coordinate correction, a standard PIE
reconstruction is performed over all scan positions. The scan order is
prepared with setPositionOrder(). For each position, the exit surface
wave is formed from the current object patch and probe, and
intensityProjection() applies the measured-intensity constraint in the
detector plane.
The difference between the updated and current exit surface waves is used
to update the object and probe through objectPatchUpdate() and
probeUpdate(), respectively. Reconstruction errors are evaluated with
getErrorMetrics(), and the reconstruction constraints are applied with
applyConstraints().
The two candidate angles are compared using their reconstruction errors.
The candidate producing the lower error is retained together with its
corresponding object and probe. The accepted angular change is then used to
update reconstruction.thetaMomentum.
The accepted detector angle is stored in
reconstruction.thetaHistory, while showReconstruction() is used to
update the reconstruction monitor during the optimization.
If GPU acceleration is enabled, the reconstruction data are returned to CPU memory after completion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
experimentalData
|
ExperimentalData
|
Experimental dataset to use. If provided, it replaces the dataset currently attached to the engine. |
None
|
reconstruction
|
Reconstruction
|
Reconstruction state to optimize. If provided, it replaces the reconstruction currently attached to the engine. |
None
|
Source code in PtyLab/Engines/aPIE.py
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 | |
thetaScan()
¶
Find the tilt angle with a grid scan of independent reconstructions.
Each candidate angle \(\theta_k\) gets its own mPIE reconstruction
(numIterations iterations, betaObject, betaProbe) on the data
resampled with resampleDetector(). All candidates start from the same
object, probe and random state, so they differ only in \(\theta_k\).
Each candidate is scored by rawDetectorError(), the error against the
measured frames before resampling, so that the score does not depend on
how much the resampling smooths the data.
The first grid has thetaScanPoints angles in
\(\theta \pm\) thetaScanRange. Each of the thetaScanRefinements
further grids spans one grid step on either side of the best angle so
far, with the same number of points, so it is \((N - 1)/2\) times finer
for \(N\) = thetaScanPoints; use \(N \ge 5\) for the refinement to help.
Afterwards reconstruction.theta, the object and the probe are those of
the best candidate, and experimentalData.ptychogram holds the data
resampled at that angle, so another engine can continue from there.
The scanned angles and errors are stored in thetaScanValues and
thetaScanErrors, and in reconstruction.extraResults["thetaScan"].
Returns:
| Type | Description |
|---|---|
tuple
|
|
Source code in PtyLab/Engines/aPIE.py
rawDetectorError(theta, scale=1.0)
¶
Error of the current model against the measured, unresampled frames.
The model intensity \(I_j\) of frame \(j\) lives on the corrected detector
grid. The measured pixel at \(x'\) corresponds to the corrected point
\(T(x', y')\) (see T), so the model is sampled there and compared with
the measured frame \(M_j\):
with \(s\) the factor the resampled data were scaled by. Only the smooth model is interpolated, never the noisy data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
theta
|
float
|
Tilt angle in degrees. |
required |
scale
|
float
|
The scale \(s\) of the resampled data. |
1.0
|
Returns:
| Type | Description |
|---|---|
float
|
The normalized error \(e\). |
Source code in PtyLab/Engines/aPIE.py
objectPatchUpdate(objectPatch, DELTA)
¶
Update the object patch using the standard ePIE object-update rule.
This implementation is identical to ePIE.objectPatchUpdate().
See Also
ePIE.objectPatchUpdate
Standard ePIE object update.
Source code in PtyLab/Engines/aPIE.py
probeUpdate(objectPatch, DELTA)
¶
Update the probe using the standard ePIE probe-update rule.
This implementation is identical to ePIE.probeUpdate().
See Also
ePIE.probeUpdate
Standard ePIE probe update.
Source code in PtyLab/Engines/aPIE.py
e3PIE
¶
Bases: BaseEngine
Multislice ptychographic iterative engine for thick-sample reconstruction.
e3PIE extends conventional PIE reconstruction to samples represented by
multiple axial object slices. The illumination is propagated sequentially
through the object slices using an angular-spectrum transfer function,
allowing wave propagation within a finite-thickness sample to be included
in the forward model.
For each scan position, the probe interacts with the first object slice and is then propagated through the remaining slices. At slice \(s\),
where \(O_s\) is the transmission function of slice \(s\) and
\(\mathcal{P}_{\Delta z}\) denotes propagation over the inter-slice distance
reconstruction.dz.
After applying the measured-intensity constraint at the detector, the resulting exit-wave correction is propagated backward through the sample. The object slices are updated sequentially from the last slice to the first, while the corresponding local probe fields are updated and back-propagated between slices.
The inter-slice propagation transfer function is precomputed using
aspw() and stored in reconstruction.H. The reconstruction therefore
requires reconstruction.nslice > 1 to represent a multislice object.
The product of all reconstructed object slices is stored in
reconstruction.objectProd as an effective projected object.
Source code in PtyLab/Engines/e3PIE.py
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 | |
initializeReconstructionParams()
¶
Initialize parameters specific to the e3PIE reconstruction.
This method sets the object and probe update strengths and the default number of reconstruction iterations.
It also precomputes the angular-spectrum transfer function used to
propagate the wavefield between adjacent object slices. The propagation
distance is given by reconstruction.dz, and the effective wavelength is
reconstruction.wavelength / reconstruction.refrIndex.
Inter-slice propagation is performed in Fourier space as
where \(\Psi_s\) is the exit wave from slice \(s\) and \(H\) is the
angular-spectrum transfer function generated by aspw().
The transfer function is stored in reconstruction.H and shifted with
ifftshift() so that it can be applied directly during FFT-based
propagation between object slices.
Source code in PtyLab/Engines/e3PIE.py
reconstruct()
¶
Run the multislice e3PIE reconstruction.
For each scan position, the corresponding object patch is extracted and the incident probe is propagated sequentially through all object slices.
At the first slice, the exit wave is formed as
For each subsequent slice, the exit wave from the previous slice is
propagated using the precomputed angular-spectrum transfer function
reconstruction.H and multiplied by the local object transmission:
where
After propagation through the full object, intensityProjection() applies
the measured diffraction-intensity constraint at the detector plane and
returns the updated exit wave.
The resulting correction term is initialized at the final object slice as
The object is then updated sequentially from the last slice back to the
first using objectPatchUpdate(). The corresponding local probe field is
updated with probeUpdate() and the correction is back-propagated to the
preceding slice using the conjugated transfer function H.conj().
After all scan positions have been processed, the product of all object
slices is stored in reconstruction.objectProd. Reconstruction errors are
evaluated with getErrorMetrics(), constraints are applied with
applyConstraints(), and showReconstruction() updates the reconstruction
monitor.
If GPU acceleration is enabled, the reconstruction data are returned to CPU memory after completion.
Source code in PtyLab/Engines/e3PIE.py
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 | |
objectPatchUpdate(objectPatch, DELTA, localProbe)
¶
Update one object slice using the local ePIE correction.
The object patch is updated from the exit-wave correction DELTA using
the probe field incident on the current object slice.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objectPatch
|
ndarray
|
Object transmission function of the current slice. |
required |
DELTA
|
ndarray
|
Exit-wave correction propagated to the current slice. |
required |
localProbe
|
ndarray
|
Probe field incident on the current object slice. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Updated object patch. |
See Also
ePIE.probeUpdate
Standard ePIE probe update.
Source code in PtyLab/Engines/e3PIE.py
probeUpdate(objectPatch, DELTA, localProbe, beth)
¶
Update the local probe field using the ePIE correction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objectPatch
|
ndarray
|
Object transmission function of the current slice. |
required |
DELTA
|
ndarray
|
Exit-wave correction at the current slice. |
required |
localProbe
|
ndarray
|
Probe field incident on the current object slice. |
required |
beth
|
float
|
Probe-update step size. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Updated local probe field. |
See Also
ePIE.probeUpdate
Standard ePIE probe update.
Source code in PtyLab/Engines/e3PIE.py
OPR
¶
Bases: BaseEngine
Orthogonal Probe Relaxation (OPR) for position-dependent probe reconstruction.
OPR extends conventional ptychographic reconstruction by allowing the
illumination probe to vary between scan positions while constraining these
variations to a low-dimensional orthogonal probe subspace.1
For scan position \(j\), the exit surface wave is formed as
where \(O_j\) is the object patch illuminated at position \(j\) and \(P_j\) is the position-dependent probe.
Instead of enforcing a single identical probe for all diffraction frames,
OPR maintains a probe estimate for each scan position. The set of
position-dependent probes is stored in reconstruction.probe_stack.
At initialization, the selected probe modes defined by params.OPR_modes
are copied to all scan positions. During reconstruction, the probe
corresponding to the current diffraction frame is loaded from the probe
stack, updated by the PIE step, and written back to the same scan position.
Allowing an independent probe at every scan position introduces many additional degrees of freedom. OPR therefore constrains the probe variations to a low-dimensional subspace. For one selected probe mode, the position-dependent probes are arranged as columns of the matrix
where each probe is flattened into a vector and \(N\) is the number of scan positions.
A truncated singular-value decomposition is used to approximate the probe stack as
where \(K =\) params.OPR_subspace is the retained subspace
dimension. Equivalently, the probe at scan position \(j\) can be represented
as a linear combination of a small number of orthogonal probe basis
functions,
thereby allowing systematic probe variations while suppressing unconstrained frame-to-frame fluctuations.
The low-rank probe estimate is blended with the current probe stack using
\(\alpha =\) params.OPR_alpha,
The truncated decomposition can be computed using the method selected by
params.OPR_tsvd_type. The current implementation supports standard SVD,
randomized SVD, and a Gram-matrix-based truncated SVD.
OPR regularization acts at two complementary levels. Despite of a low-rank
constraint on the position-dependent probe stack, if params.OPR_neighbor_constraint is enabled, the subspace
coefficients \(c_{k,j}\) are additionally averaged over neighboring scan
positions. This imposes smoothness along the scan sequence and suppresses
abrupt changes in the contribution of each probe basis mode. The two constraints therefore regularize different aspects of the probe
variation: the low-rank constraint limits which spatial variations are
allowed, while the neighbor constraint limits how rapidly their coefficients
may change between successive scan positions.
OPR can also be combined with mixed-state ptychography.2 Multiple mutually incoherent probe modes are used to describe partial coherence within each diffraction frame, while OPR accounts for systematic changes of these probe modes between scan positions.
These two descriptions address different probe degrees of freedom. The mixed-state model represents the illumination as an incoherent sum of probe modes,
where \(m\) indexes the mutually incoherent probe modes. OPR additionally allows the individual probe modes \(P_{m,j}\) to vary with scan position \(j\) while constraining their position dependence to a low-dimensional orthogonal subspace.
In the current implementation, the probe modes included in OPR are selected
through params.OPR_modes. If params.OPR_orthogonalize_modes is enabled,
orthogonalizeIncoherentModes() orthogonalizes the incoherent probe modes
independently at each scan position before the position-dependent probe stack
is projected onto the OPR subspace.
If params.OPR_tv is enabled, total-variation object updates are applied
at the interval specified by params.OPR_tv_freq.
-
M. Odstrčil, P. Baksh, S. A. Boden, R. Card, J. E. Chad, J. G. Frey, and W. S. Brocklesby, "Ptychographic coherent diffractive imaging with orthogonal probe relaxation," Opt. Express 24, 8360-8369 (2016). https://doi.org/10.1364/OE.24.008360 ↩
-
W. Eschen, L. Loetgering, V. Schuster, R. Klas, A. Kirsche, L. Berthold, M. Steinert, T. Pertsch, H. Gross, M. Krause, J. Limpert, and J. Rothhardt, "Material-specific high-resolution table-top extreme ultraviolet microscopy," Light Sci. Appl. 11, 117 (2022). https://doi.org/10.1038/s41377-022-00797-6 ↩
Implementation Notes
The current OPR implementation requires CuPy and GPU execution. Several probe-stack operations are implemented directly with CuPy rather than through the NumPy/CuPy abstraction used elsewhere in PtyLab. CPU-only execution is therefore not currently supported.
Source code in PtyLab/Engines/OPR.py
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 | |
initializeReconstructionParams()
¶
Initialize parameters specific to the OPR reconstruction.
The OPR relaxation strength, selected probe modes, and retained
position-dependent probe subspace dimension are read from
params.OPR_alpha, params.OPR_modes, and params.OPR_subspace,
respectively.
This method also sets the default object and probe update strengths and the number of reconstruction iterations. In addition, it defines the working-memory budget used by the batched incoherent-mode orthogonalization routine.
The main OPR parameters initialized here are:
-
alpha: Blending factor between the current probe stack and its low-rank OPR approximation. -
OPR_modes: Probe-mode indices for which position-dependent probe relaxation is applied. -
n_subspace: Number of dominant OPR subspace components retained during the truncated singular-value decomposition.
Source code in PtyLab/Engines/OPR.py
reconstruct()
¶
Run the Orthogonal Probe Relaxation reconstruction.
For each reconstruction iteration, a position-dependent probe is loaded
from reconstruction.probe_stack for the current scan position. The exit
surface wave is then formed from the local object patch and the selected
probe, followed by intensityProjection() to apply the measured
diffraction-intensity constraint.
The resulting exit-wave correction
is used to update the object and probe through objectPatchUpdate() and
probeUpdate(). If params.OPR_tv is enabled, the object update is
periodically replaced by objectPatchUpdate_TV() according to
params.OPR_tv_freq.
After each probe update, the selected probe modes are written back to
reconstruction.probe_stack at the corresponding scan position.
Once all scan positions have been processed, the position-dependent probe
stack is regularized. If params.OPR_orthogonalize_modes is enabled,
orthogonalizeIncoherentModes() first orthogonalizes the incoherent probe
modes independently at each scan position.
The complete probe stack is then projected onto a low-dimensional
position-dependent subspace using orthogonalizeProbeStack(), with the
retained subspace dimension given by params.OPR_subspace.
Finally, reconstruction errors are evaluated with getErrorMetrics(),
the remaining reconstruction constraints are applied with
applyConstraints(), and showReconstruction() updates the monitor.
If GPU acceleration is enabled, the reconstruction data are returned to CPU memory after completion.
Source code in PtyLab/Engines/OPR.py
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 | |
orthogonalizeIncoherentModes()
¶
Orthogonalize the incoherent probe modes at each scan position.
For every position in reconstruction.probe_stack, the selected probe
modes are reshaped into a two-dimensional matrix with one row per
incoherent mode and one column per probe pixel.
An SVD is then performed,
and the probe modes are replaced by
which provides an equivalent orthogonal representation of the same mixed-state probe subspace.
The orthogonalization is applied independently at every scan position and does not alter the position-dependent OPR subspace itself.
If params.OPR_fast_orthogonalization is enabled, the batched
Gram-matrix implementation _orthogonalizeIncoherentModes_batched() is
used instead of the frame-by-frame SVD. For a probe-mode matrix \(P\), the left singular vectors of \(P\) are also
the eigenvectors of the much smaller Gram matrix
Since the number of incoherent probe modes is typically much smaller than
the number of probe pixels, \(G\) has shape
(nModes, nModes) and can be factorized much more efficiently than the
full probe matrix. The orthogonal modes are then obtained from
which is equivalent to \(S V^\dagger\).
The batched implementation performs this operation for multiple scan positions simultaneously and processes the probe stack in chunks to limit temporary memory usage.
Source code in PtyLab/Engines/OPR.py
average(arr)
¶
Smooth a one-dimensional array by averaging neighboring values.
Each interior element is replaced by the average of itself and its two nearest neighbors,
while the first and last elements are averaged only with their single available neighbor.
In OPR, this operation is used to smooth the position-dependent subspace
coefficients when params.OPR_neighbor_constraint is enabled, thereby
suppressing abrupt probe variations between successive scan positions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arr
|
array - like
|
One-dimensional array of position-dependent coefficients. |
required |
Returns:
| Type | Description |
|---|---|
array - like
|
Smoothed array with the same shape as the input. |
Notes
This implementation currently operates on CuPy arrays and therefore requires GPU support.
Source code in PtyLab/Engines/OPR.py
svd(P)
¶
Compute the reduced singular-value decomposition of a matrix.
The decomposition
is evaluated with CuPy when P is stored on the GPU and with NumPy
otherwise. In both cases, full_matrices=False is used to return the
reduced SVD.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
P
|
array - like
|
Input matrix to decompose. |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
|
Source code in PtyLab/Engines/OPR.py
rsvd(P, n_dim)
¶
Compute a randomized truncated singular-value decomposition.
This method delegates to PtyLab.utils.fsvd.rsvd() and returns a
low-rank approximation of the input matrix using the requested subspace
dimension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
P
|
array - like
|
Input matrix to decompose. |
required |
n_dim
|
int
|
Number of singular components to retain. |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
|
Source code in PtyLab/Engines/OPR.py
gram_tsvd(A, n_dim)
staticmethod
¶
Compute a truncated SVD through the Gram matrix.
For a tall matrix \(A\), the method forms the smaller Gram matrix
and uses
to recover the truncated singular-value decomposition of A. The retained
left singular vectors are reconstructed from
using the first n_dim components.
The Gram matrix is formed in double precision for improved numerical stability, and division by numerically zero singular values is guarded.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
A
|
array - like
|
Input matrix. |
required |
n_dim
|
int
|
Number of singular components to retain. |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
|
Source code in PtyLab/Engines/OPR.py
orthogonalizeProbeStack(probe_stack, n_dim)
¶
Project the position-dependent probe stack onto a low-dimensional subspace.
For each probe mode selected by params.OPR_modes, the probes from all
scan positions are reshaped into a matrix
whose columns contain the flattened probe estimates at individual scan positions.
A truncated singular-value decomposition is then computed using the method
selected by params.OPR_tsvd_type. Only n_dim dominant components are
retained, yielding a low-rank approximation of the position-dependent
probe stack.
If params.OPR_neighbor_constraint is enabled, the position-dependent
subspace coefficients are smoothed over neighboring scan positions before
the probe stack is reconstructed.
The low-rank approximation is blended with the current probe stack using
where alpha = params.OPR_alpha.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
probe_stack
|
array - like
|
Position-dependent probe stack. |
required |
n_dim
|
int
|
Number of OPR subspace components to retain. |
required |
Returns:
| Type | Description |
|---|---|
array - like
|
Regularized probe stack with the same shape as the input. |
Source code in PtyLab/Engines/OPR.py
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 | |
objectPatchUpdate(objectPatch, DELTA)
¶
Update the object patch using the ePIE correction.
The object update uses the current probe and exit-wave correction DELTA
with normalization by the maximum probe intensity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objectPatch
|
ndarray
|
Current object patch at the scan position. |
required |
DELTA
|
ndarray
|
Exit-wave correction after the intensity projection. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Updated object patch. |
Source code in PtyLab/Engines/OPR.py
probeUpdate(objectPatch, DELTA, weight, gimmel=0.1)
¶
Update the probe using the ePIE correction.
The probe update is computed from the current object patch and exit-wave
correction DELTA. The normalization includes the regularization term
gimmel to avoid excessively large updates when the object intensity is
small.
The update can additionally be scaled by weight. In the current OPR
reconstruction workflow, weight is set to 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objectPatch
|
ndarray
|
Current object patch at the scan position. |
required |
DELTA
|
ndarray
|
Exit-wave correction after the intensity projection. |
required |
weight
|
float
|
Multiplicative weight applied to the probe update. |
required |
gimmel
|
float
|
Small positive regularization term added to the normalization
denominator. Default is |
0.1
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Updated probe. |
Source code in PtyLab/Engines/OPR.py
qNewton
¶
Bases: BaseEngine
Experimental quasi-Newton-inspired ptychographic reconstruction engine.
qNewton provides an alternative to the standard ePIE and mPIE update
rules by using locally weighted, regularized object and probe corrections.
Its purpose is to adapt the update strength to the local illumination or
object amplitude instead of applying the same normalization everywhere.
For the object update, the current implementation uses a factor of the form
while the probe update uses the corresponding object-dependent expression,
where regObject and regProbe provide the regularization terms.
The inverse-intensity factors reduce the update in strongly illuminated regions and prevent excessively large corrections where the local probe or object intensity becomes small. The additional amplitude weighting further modulates the contribution of weakly illuminated or weak-object regions.
This type of locally scaled update may be useful when the illumination or object amplitude varies strongly across the reconstruction and a standard globally normalized PIE update becomes poorly balanced.
The remaining reconstruction workflow follows the standard PIE structure:
the exit surface wave is formed from the object and probe,
intensityProjection() applies the measured diffraction constraint, and
the resulting exit-wave correction is used to update the object and probe.
Notes
This engine is retained as an experimental algorithm implementation. Its normalization and theoretical formulation should be validated against the original method or reference before further development.
Source code in PtyLab/Engines/qNewton.py
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 | |
initializeReconstructionParams()
¶
Initialize parameters specific to the experimental qNewton reconstruction.
This method sets the default object and probe update strengths, regularization parameters, and number of reconstruction iterations.
The object and probe updates are controlled by betaObject and
betaProbe, while regObject and regProbe regularize the local
inverse-intensity factors used in the qNewton-inspired update rules.
Source code in PtyLab/Engines/qNewton.py
reconstruct(experimentalData=None)
¶
Run the qNewton ptychographic reconstruction.
The reconstruction follows the standard PIE workflow: each scan position
is processed sequentially, intensityProjection() applies the measured
diffraction constraint, and the resulting exit-wave correction is used to
update the object and probe.
The qNewton-specific behavior is provided by objectPatchUpdate() and
probeUpdate(), which apply the locally weighted and regularized update
rules defined by this engine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
experimentalData
|
ExperimentalData
|
Experimental dataset to use for the reconstruction. If provided, it replaces the dataset currently attached to the engine. |
None
|
Source code in PtyLab/Engines/qNewton.py
objectPatchUpdate(objectPatch, DELTA)
¶
Update the object patch using the qNewton-inspired correction.
The update is weighted by the current probe according to
where regObject provides the regularization term \(\lambda_O\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objectPatch
|
ndarray
|
Current object patch at the scan position. |
required |
DELTA
|
ndarray
|
Exit-wave correction after the intensity projection. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Updated object patch. |
Source code in PtyLab/Engines/qNewton.py
probeUpdate(objectPatch, DELTA)
¶
Update the probe using the qNewton-inspired correction.
The update is weighted by the current object patch according to
where regProbe provides the regularization term \(\lambda_P\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objectPatch
|
ndarray
|
Current object patch at the scan position. |
required |
DELTA
|
ndarray
|
Exit-wave correction after the intensity projection. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Updated probe. |
Source code in PtyLab/Engines/qNewton.py
mqNewton
¶
Bases: BaseEngine
Experimental momentum-accelerated quasi-Newton-inspired ptychographic
reconstruction engine.
mqNewton extends qNewton by combining the same locally regularized,
curvature-like object and probe updates with adaptive momentum
acceleration.
The current implementation supports three momentum schemes:
"momentum", "ADAM", and "NADAM". Object and probe momentum updates
are applied after each scan-position update. These methods should be interpreted as adaptive
momentum variants unless further theoretical validation establishes a more
specific correspondence.
The local object and probe updates are inherited conceptually from the
qNewton formulation, while the momentum contribution is applied through
objectMomentumUpdate() and probeMomentumUpdate().
Notes
This engine is retained as an experimental algorithm implementation. The qNewton normalization, momentum formulation, and theoretical basis should be validated against the original method or reference before further development.
Source code in PtyLab/Engines/mqNewton.py
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 | |
initializeReconstructionParams()
¶
Initialize parameters specific to the experimental mqNewton reconstruction.
This method sets the qNewton-style object and probe update strengths and regularization parameters, together with the parameters used by the momentum-acceleration scheme.
beta1 and beta2 control the first- and second-moment estimates used by
the adaptive momentum methods. betaObject_m and betaProbe_m determine
the strength of the resulting momentum corrections.
The momentum strategy is selected through momentum_method, with the
current implementation supporting "momentum", "ADAM", and "NADAM".
Source code in PtyLab/Engines/mqNewton.py
initializeAdaptiveMomentum()
¶
Initialize the selected momentum-acceleration scheme.
The update function is selected from momentum, ADAM, or NADAM
according to momentum_method and stored in momentum_engine.
For the adaptive "ADAM" and "NADAM" variants, additional second-moment
buffers are initialized for both the object and probe momentum terms.
These buffers are stored in objectMomentum_v and probeMomentum_v.
Raises:
| Type | Description |
|---|---|
AttributeError
|
If |
Source code in PtyLab/Engines/mqNewton.py
reconstruct(experimentalData=None)
¶
Run the momentum-accelerated qNewton reconstruction.
The reconstruction follows the standard PIE workflow using the
qNewton-specific object and probe update rules. After each scan-position
update, additional object and probe momentum corrections are applied
through objectMomentumUpdate() and probeMomentumUpdate().
The momentum update scheme is selected by momentum_method and may use
standard momentum, ADAM, or NADAM. If
params.positionCorrectionSwitch is enabled, position correction is also
applied during the scan loop.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
experimentalData
|
ExperimentalData
|
Experimental dataset to use for the reconstruction. If provided, it replaces the dataset currently attached to the engine. |
None
|
Source code in PtyLab/Engines/mqNewton.py
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 | |
ADAM(grad, mt, vt, itr)
¶
Compute an ADAM-inspired adaptive momentum update.
ADAM (Adaptive Moment Estimation) combines momentum with adaptive step scaling. It tracks both the running average of the gradient direction and a running estimate of the gradient magnitude, allowing the update direction and effective step size to adapt during optimization.
The first-moment estimate is updated as
while the second-moment estimate uses the global squared L2 norm of the gradient,
Bias-corrected estimates are then formed as
and the normalized momentum update is
with \(\epsilon = 10^{-8}\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grad
|
array - like
|
Current object or probe gradient. |
required |
mt
|
array - like
|
Previous first-moment estimate. |
required |
vt
|
array - like
|
Previous second-moment estimate. |
required |
itr
|
int
|
Current iteration index used for bias correction. |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
|
Source code in PtyLab/Engines/mqNewton.py
NADAM(grad, mt, vt, itr)
¶
Compute a NADAM-inspired adaptive momentum update.
NADAM combines ADAM-style adaptive moment estimation with a Nesterov-type momentum correction. Compared with ADAM, the update includes an additional contribution from the current gradient, giving the momentum step a more anticipatory character.
The first- and second-moment estimates are updated as in ADAM(), using the current gradient g_t.
The NADAM-style update is then computed as
with \(\epsilon = 10^{-8}\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grad
|
array - like
|
Current object or probe gradient. |
required |
mt
|
array - like
|
Previous first-moment estimate. |
required |
vt
|
array - like
|
Previous second-moment estimate. |
required |
itr
|
int
|
Current iteration index used for bias correction. |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
|
Source code in PtyLab/Engines/mqNewton.py
momentum(grad, mt, vt, itr)
¶
Compute a standard momentum update.
The current gradient is combined with the previous momentum according to
where frictionM defines the momentum retention factor \(\gamma\).
The resulting momentum is used directly as the update direction. The
second-moment variable vt is not modified and is returned unchanged so
that this method shares the same calling interface as ADAM() and
NADAM().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grad
|
array - like
|
Current object or probe gradient. |
required |
mt
|
array - like
|
Previous momentum estimate. |
required |
vt
|
array - like
|
Second-moment buffer. It is not used by standard momentum and is returned unchanged. |
required |
itr
|
int
|
Iteration index. It is not used by standard momentum but is accepted for a common interface with the adaptive momentum methods. |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
|
Source code in PtyLab/Engines/mqNewton.py
objectMomentumUpdate(loop)
¶
Apply the selected momentum update to the reconstructed object.
The momentum gradient is defined from the difference between the buffered object state and the current reconstruction,
The selected momentum method (momentum, ADAM, or NADAM) is then
used to compute the update direction and refresh the corresponding
momentum buffers.
The reconstructed object is updated according to
where betaObject_m controls the strength of the momentum correction
and \(u_t\) is the update returned by the selected momentum method.
After the update, the object buffer is replaced by the new object state for use in the next momentum step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loop
|
int
|
Current reconstruction iteration index. |
required |
Source code in PtyLab/Engines/mqNewton.py
probeMomentumUpdate(loop)
¶
Apply the selected momentum update to the reconstructed probe.
The momentum gradient is defined from the difference between the buffered probe state and the current reconstruction,
The selected momentum method (momentum, ADAM, or NADAM) is used
to compute the probe update direction and refresh the corresponding
momentum buffers.
The probe is updated according to
where betaProbe_m controls the strength of the momentum correction.
After the update, the probe buffer is replaced by the new probe state for use in the next momentum step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loop
|
int
|
Current reconstruction iteration index. |
required |
Source code in PtyLab/Engines/mqNewton.py
objectPatchUpdate(objectPatch, DELTA)
¶
Update the object patch using the qNewton-inspired correction.
The update is weighted by the current probe according to
where regObject provides the regularization term \(\lambda_O\).
This local qNewton-style update is applied before the additional momentum
correction performed by objectMomentumUpdate().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objectPatch
|
ndarray
|
Current object patch at the scan position. |
required |
DELTA
|
ndarray
|
Exit-wave correction after the intensity projection. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Updated object patch. |
Source code in PtyLab/Engines/mqNewton.py
probeUpdate(objectPatch, DELTA)
¶
Update the probe using the qNewton-inspired correction.
The update is weighted by the current object patch according to
where regProbe provides the regularization term \(\lambda_P\).
This local qNewton-style update is applied before the additional momentum
correction performed by probeMomentumUpdate().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objectPatch
|
ndarray
|
Current object patch at the scan position. |
required |
DELTA
|
ndarray
|
Exit-wave correction after the intensity projection. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Updated probe. |