Skip to content

_bayfai

Classes for geometry optimization tasks.

Classes:

Name Description
BayFAIOpt

optimize LCLS1 detector geometry using PyFAI coupled with Bayesian Optimization.

Functions:

Name Description
- _build_ai

Build a pyFAI AzimuthalIntegrator from a detector, geometry parameters, and wavelength.

BayFAIOpt

Class to run BayFAI optimization on a powder image.

Parameters

exp : str Experiment name run : int Run number

Source code in lute/tasks/_bayfai.py
 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
class BayFAIOpt:
    """
    Class to run BayFAI optimization on a powder image.

    Parameters
    ----------
    exp : str
        Experiment name
    run : int
        Run number
    """

    min_points = 0  # score() returns 0 below this many control points

    def __init__(
        self,
        exp: str,
        run: int,
    ):
        self.exp = exp
        self.run = run
        self.comm = MPI.COMM_WORLD
        self.rank = self.comm.Get_rank()
        self.size = self.comm.Get_size()
        if self.rank == 0:
            logger.info(f"Getting {self.size} processes for BayFAIOpt task")

    @staticmethod
    def UCB(X, gp_model, visited_idx, beta=1.96, n_cand=None):
        """Return the index into X of the best unvisited UCB point, among n_cand random ones if set."""
        idx = np.delete(np.arange(len(X)), visited_idx)
        if n_cand and len(idx) > n_cand:
            idx = np.random.choice(idx, n_cand, replace=False)
        y_pred, y_std = gp_model.predict(X[idx], return_std=True)
        return idx[np.argmax(y_pred + beta * y_std)]

    @staticmethod
    def q_UCB(X, gp_model, q, visited_idx, beta=1.96):
        y_pred, y_std = gp_model.predict(X, return_std=True)
        ucb = y_pred + beta * y_std
        ucb[visited_idx] = -np.inf
        top_next = np.argsort(ucb)[-q:]
        return top_next

    def setup(
        self,
        detname: str,
        h5: str,
        Imin: float,
        calibrant: str,
        fixed: list,
        parallelized: Optional[str] = None,
        wavelength: float = 1e-10,
    ):
        """
        Setup the BayFAI optimization.

        Parameters
        ----------
        detname : str
            Name of the detector
        h5 : str
            Path to the smalldata h5 file to use for calibration
        Imin : float
            Minimum intensity percentile threshold for Bragg peak detection
        calibrant : PyFAI.Calibrant
            PyFAI calibrant object
        fixed : list
            List of parameters to keep fixed during optimization
        parallelized : str, optional
            Name of the parameter to distribute across MPI ranks as a
            sliding window. If None, ranks instead run full BO differentiated
            only by seed.
        wavelength : float, optional
            X-ray wavelength in meters. If provided (non-default 1e-10),
            overrides the value read from the h5 file.

        Returns
        -------
        Imin : float
            Minimum intensity value for identifying Bragg peaks
        """
        self.detector = self.build_detector(detname)
        # twoThetaArray/chiArray recompute full-grid pixel positions (geometry-independent) on every
        # BO evaluation via fromfunction index arrays; pyFAI callers copy (p1 - poni1), never mutate.
        pos, calc = (
            self.detector.calc_cartesian_positions(),
            self.detector.calc_cartesian_positions,
        )
        for a in pos:
            if a is not None:
                a.flags.writeable = False
        self.detector.calc_cartesian_positions = (
            lambda d1=None, d2=None, center=True, use_cython=True: (
                pos
                if center
                and all(
                    d is None or np.shape(d) == self.detector.shape for d in (d1, d2)
                )
                else calc(d1, d2, center, use_cython)
            )
        )
        self.powder = self.generate_powder(h5, detname, Imin)
        self.calibrant = self.define_calibrant(calibrant, h5, wavelength)
        self.set_search_space(parallelized, fixed)
        # pyFAI's poni setters reset engines and call gc.collect() on every geometry build;
        # freezing the post-import/setup heap keeps those collections from rescanning it.
        gc.freeze()

    def extract_powder(self, powder_path: str, detname: str) -> npt.NDArray[np.float64]:
        """
        Extract a powder image from smalldata analysis.

        Parameters
        ----------
        powder_path : str
            Path to the h5 or npy file containing the powder data.

        Returns
        -------
        powder : npt.NDArray[np.float64]
            The extracted powder image.
        """
        if powder_path.endswith(".npy"):
            powder = np.load(powder_path)
            return powder
        else:
            with h5py.File(powder_path) as h5:
                try:
                    powder = h5[f"Sums/{detname}_calib_max"][()]
                except KeyError:
                    logger.warning(
                        f"Cannot find {detname} Max powder in {powder_path}, defaulting to {detname} Sum instead."
                    )
                    try:
                        powder = h5[f"Sums/{detname}_calib"][()]
                    except KeyError:
                        logger.error(
                            f"Cannot find {detname} Sum powder in {powder_path}. Exiting..."
                        )
                        raise
            return powder

    def preprocess_powder(
        self,
        powder: npt.NDArray[np.float64],
        mask: npt.NDArray[np.integer],
        Imin: float = 95,
    ) -> npt.NDArray[np.float64]:
        """
        Preprocess extracted powder for enhancing optimization

        Parameters
        ----------
        powder : npt.NDArray[np.float64]
            Powder image to use for calibration
        mask : npt.NDArray[np.integer]
            Pixel mask to apply to the powder image
        Imin : float, optional
            Minimum intensity percentile threshold for Bragg peak detection.
        """
        powder = np.asarray(powder, dtype=np.float64).copy()
        good = mask != 0
        powder[~good] = 0
        powder[powder < 0] = 0
        self.powder = powder
        self.stacked_powder = np.reshape(self.powder, self.detector.shape)
        non_zero_pixels = self.powder[self.powder > 0]
        self.Imin = np.percentile(non_zero_pixels, Imin)
        return powder

    def assemble_image(
        self, powder: npt.NDArray[np.float64]
    ) -> npt.NDArray[np.float64]:
        """
        Assemble the powder image from modules to full detector shape.

        Parameters
        ----------
        powder : npt.NDArray[np.float64]
            Powder image to use for calibration
        """
        pixel_index_map = self.detector.pixel_index_map
        max_rows = np.max(pixel_index_map[..., 0]) + 1
        max_cols = np.max(pixel_index_map[..., 1]) + 1
        assembled_powder = np.zeros((max_rows, max_cols))
        for p in range(pixel_index_map.shape[0]):
            i = pixel_index_map[p, ..., 0]
            j = pixel_index_map[p, ..., 1]
            assembled_powder[i, j] = powder[p]
        return assembled_powder

    def generate_powder(
        self,
        powder_path: str,
        detname: str,
        Imin: float = 95,
    ) -> npt.NDArray[np.float64]:
        """
        Generate a preprocessed powder image from smalldata reduction.

        Parameters
        ----------
        powder_path : str
            Path to the h5 or npy file containing the powder data.
        detname : str
            Name of the detector
        Imin : float, optional
            Minimum intensity percentile threshold for Bragg peak detection.
        """
        mask = self.detector.geo.get_pixel_mask(mbits=3)
        if mask.shape[0] == 1 and mask.ndim > 3:
            mask = np.squeeze(mask, axis=0)
        powder = self.extract_powder(powder_path, detname)
        powder = self.preprocess_powder(powder, mask, Imin)
        self.assembled_powder = self.assemble_image(powder)
        return powder

    def build_detector(self, detname: str) -> pyFAI.detectors.Detector:
        """
        Read the metrology data and build a pyFAI detector object.

        Parameters
        ----------
        detname : str
            Name of the detector

        Returns
        -------
        pyFAI.Detector
            Configured pyFAI detector object
        """
        metrology = get_geometry(detname)
        detector = PsanaToPyFAI.convert(in_file=metrology, detname=detname)
        return detector

    def update_geometry(self, out_file: str, detname: str) -> pyFAI.detectors.Detector:
        """
        Update the geometry and write a new .poni, .geom and .data file

        Parameters
        ----------
        optimizer : BayesGeomOpt
            Optimizer object
        out_file : str
            Path to the output file
        """
        path = os.path.dirname(out_file)
        poni_file = os.path.join(path, f"r{self.run:0>4}_{detname}.poni")
        self.gr.save(poni_file)
        pyfai_to_image_poni(
            detector=self.detector,
            in_file=poni_file,
            out_file=os.path.join(path, f"r{self.run:0>4}_{detname}_image.poni"),
        )
        PyFAIToPsana.convert(
            in_file=poni_file,
            detector=self.detector,
            out_file=out_file,
        )
        geom_file = os.path.join(path, f"r{self.run:0>4}_{detname}.geom")
        PyFAIToCrystFEL.convert(
            in_file=poni_file,
            detector=self.detector,
            out_file=geom_file,
        )

    def define_calibrant(
        self,
        calibrant_name: str,
        h5: str,
        wavelength: float = 1e-10,
    ) -> pyFAI.calibrant.Calibrant:
        """
        Define calibrant for optimization with appropriate wavelength.

        Parameters
        ----------
        calibrant_name : str
            Name of the calibrant
        h5 : str
            Path to the smalldata h5 file containing the photon energy.
        wavelength : float, optional
            X-ray wavelength in meters.
        """
        self.calibrant_name = calibrant_name
        calibrant = CALIBRANT_FACTORY(calibrant_name)
        if wavelength != 1e-10:
            logger.info(f"Using user-provided wavelength {wavelength} m")
        else:
            try:
                with h5py.File(h5) as f:
                    for key in PHOTON_ENERGY_KEYS:
                        if key not in f:
                            continue
                        energies = np.asarray(f[key][()], dtype=float)
                        energies = energies[np.isfinite(energies) & (energies > 0)]
                        if energies.size == 0:
                            continue
                        photon_energy = float(np.mean(energies))
                        wavelength = 1.23984193e-6 / photon_energy
                        logger.info(
                            f"Read {photon_energy:.2f} eV from {key} "
                            f"-> wavelength {wavelength:.4e} m"
                        )
                        break
                    else:
                        raise KeyError(
                            f"None of {PHOTON_ENERGY_KEYS} hold usable photon "
                            f"energies in the h5 file"
                        )
            except Exception as e:
                logger.warning(
                    f"Could not read photon energy from {h5} due to {e}, defaulting to provided wavelength {wavelength} m"
                )
        calibrant.wavelength = wavelength
        return calibrant

    def set_search_space(self, parallelized: Optional[str], fixed: list) -> None:
        """
        Define the search space for the free parameters.

        Parameters
        ----------
        parallelized : str, optional
            Name of the parameter to be distributed across MPI ranks as a
            sliding window. Stays part of the free search space.
        fixed : list
            List of parameters to keep fixed during optimization
        """
        if parallelized and parallelized in fixed:
            raise ValueError(
                f"Parameter '{parallelized}' cannot be both parallelized and fixed."
            )
        self.parallelized = parallelized
        self.fixed = fixed
        self.space = []
        self.order = ["dist", "poni1", "poni2", "rot1", "rot2", "rot3"]
        for p in self.order:
            if p not in fixed:
                self.space.append(p)

    def distribute_parallelized(self, bounds, res, dist_spacing="geometric"):
        """
        Split the parallelized parameter's absolute bounds into one window per MPI rank.

        dist with dist_spacing 'geometric': rank r gets [b_r, b_r+1] of
        b = low*(high/low)**(arange(size+1)/size), searched on a 2-point grid at the
        window edges. Otherwise ('linear', or any other parallelized parameter):
        size equal windows of width w = (high-low)/size, grid step w.
        Sets res[p] to this rank's window width (refinement range +-step*w).

        Parameters
        ----------
        bounds : dict
            Absolute bounds per parameter, format: {param: (low, high)}
        res : dict
            Resolution per parameter (updated in place for the parallelized one)
        dist_spacing : str
            'geometric' or 'linear' split of bounds['dist'] when dist is parallelized

        Returns
        -------
        value : float, optional
            Center of this rank's window, or None if no parameter is parallelized.
        """
        self.param_values = None
        self.window = None
        if not self.parallelized:
            return None
        p = self.parallelized
        lo, hi = sorted(bounds[p])
        n = self.size
        if p == "dist" and dist_spacing == "geometric":
            b = lo * (hi / lo) ** (np.arange(n + 1) / n)
            lo_k, hi_k = b[self.rank], b[self.rank + 1]
            w = hi_k - lo_k
            value = (lo_k + hi_k) / 2
            self.param_values = (b[:-1] + b[1:]) / 2
            self.window = np.array([lo_k, hi_k])
        else:
            # ponytail: rounding to 9 decimals makes w the decimal of the config (e.g. 0.0058), so
            # the windows reproduce the old center+k*res sweep bit for bit.
            w = round((hi - lo) / n, 9)
            self.param_values = np.round(lo + (np.arange(n) + 0.5) * w, 6)
            value = self.param_values[self.rank]
            self.window = np.arange(value - w / 2, value + w / 2 + w, w)
        res[p] = self.window_width = w
        logger.info(
            f"tiling rank={self.rank} {p}={value:.6f} lo={self.window[0]:.6f} "
            f"hi={self.window[-1]:.6f} w={w:.6f}"
        )
        return value

    def create_search_space(self, value, center, bounds, res):
        """
        Discretize the search space for the free parameters.

        Parameters
        ----------
        value : float, optional
            Window center of the parallelized parameter on this MPI rank
        center : dict
            Center values for each parameter
        bounds : dict
            Absolute bounds for each parameter, format: {param: (low, high)}
        res : dict
            Resolution per parameter

        Returns
        -------
        X : np.ndarray
            Full 6D geometry space (cartesian product)
        X_norm : np.ndarray
            Normalized search space (between-1 and 1)
        """
        if self.parallelized is not None and value is not None:
            center[self.parallelized] = value
        full_params = {}
        search_params = {}
        for p in self.order:
            if p == self.parallelized and value is not None:
                full_params[p] = self.window
                search_params[p] = full_params[p]
            elif p in self.space:
                low, high = bounds[p]
                if high < low:
                    low, high = high, low
                step = res[p]
                full_params[p] = np.arange(low, high + step, step)
                search_params[p] = full_params[p]
            else:
                full_params[p] = np.array([center[p]])

        X = np.array(np.meshgrid(*[full_params[p] for p in self.order])).T.reshape(
            -1, len(self.order)
        )
        X_search = np.array(
            np.meshgrid(*[search_params[p] for p in self.space])
        ).T.reshape(-1, len(self.space))
        self.mins = np.min(X_search, axis=0)
        self.maxs = np.max(X_search, axis=0)
        X_norm = 2 * (X_search - self.mins) / (self.maxs - self.mins) - 1
        return X, X_norm

    def sample_initial_points(self, X, X_norm, center, bounds, n_samples, prior):
        """
        Sample initial points from the search space.

        Parameters
        ----------
        X : np.ndarray
            Search space
        X_norm : np.ndarray
            Normalized search space
        center : dict
            Center values for each parameter
        bounds : dict
            Bounds for each parameter
        n_samples : int
            Number of samples to draw
        prior : bool
            Use prior information for sampling

        Returns
        -------
        np.ndarray
            Sampled points
        """
        if prior:
            means = [center[p] for p in self.space]
            # ponytail: builtin round(), not np.round: c+b - (c-b) differs from 2b in the last bit for
            # most centers (0.28 - 0.26 != 0.02), and the multivariate_normal draws would change.
            widths = [
                (
                    self.window_width
                    if p == self.parallelized
                    else round(float(abs(bounds[p][1] - bounds[p][0])), 12)
                )
                for p in self.space
            ]
            cov = np.diag([(wd / 5) ** 2 for wd in widths])
            X_free = np.random.multivariate_normal(means, cov, n_samples)
            X_free = np.clip(X_free, self.mins, self.maxs)
            X_norm_samples = 2 * (X_free - self.mins) / (self.maxs - self.mins) - 1
            X_samples = np.tile([center[p] for p in self.order], (n_samples, 1))
            for i, p in enumerate(self.space):
                j = self.order.index(p)
                X_samples[:, j] = X_free[:, i]
            return X_samples, X_norm_samples
        else:
            idx_samples = np.random.choice(X.shape[0], n_samples)
            X_samples = X[idx_samples]
            X_norm_samples = X_norm[idx_samples]
            return X_samples, X_norm_samples

    @functools.cached_property
    def _massif(self):
        """The powder's Massif, built once: peaks_from_area only reads its data, mask and interpolator."""
        return _Massif(
            self.stacked_powder, self.detector.dynamic_mask(self.stacked_powder)
        )

    @functools.cached_property
    def _pos64(self):
        """float64 buffers for p1 - poni1, p2 - poni2 and p3: calc_tth copies other dtypes to float64."""
        p1, p2, p3 = self.detector.calc_cartesian_positions()
        return (
            np.empty(p1.shape),
            np.empty(p2.shape),
            None if p3 is None else p3.astype(np.float64),
        )

    def _fast_maps(self, sg):
        """Serve extract_cp's Massif and 2theta/chi maps from per-rank caches (same cython kernels).

        chiArray is only read at ring-contour pixels, so chi is computed per indexed pixel instead of
        over the full grid; both skip pyFAI's fromfunction index grids. The maps are left alone unless
        the inputs match.
        """
        # Each extract_cp otherwise allocates and frees ~1 GiB on jungfrau16M; on a node whose memory is
        # full of page cache, re-faulting those pages costs more kernel time than the scoring itself.
        sg.massif = self._massif
        gr = sg.geometry_refinement
        if not (
            self.stacked_powder.shape == self.detector.shape
            and gr._parallax is None
            and gr.chiDiscAtPi is True
            and gr.detector is self.detector
        ):
            return
        p1, p2, p3 = self.detector.calc_cartesian_positions()
        b1, b2, b3 = self._pos64
        # dtype=p1.dtype: the difference is still taken in the positions' precision, then widened.
        gr.twoThetaArray = lambda shape=None: _geometry.calc_tth(
            L=gr._dist,
            rot1=gr._rot1,
            rot2=gr._rot2,
            rot3=gr._rot3,
            pos1=np.subtract(p1, gr._poni1, out=b1, dtype=p1.dtype),
            pos2=np.subtract(p2, gr._poni2, out=b2, dtype=p2.dtype),
            pos3=b3,
        )

        class ChiAt:
            def __getitem__(_, idx):
                return _geometry.calc_chi(
                    L=gr._dist,
                    rot1=gr._rot1,
                    rot2=gr._rot2,
                    rot3=gr._rot3,
                    pos1=p1[idx] - gr._poni1,
                    pos2=p2[idx] - gr._poni2,
                    pos3=None if p3 is None else p3[idx],
                    orientation=gr.detector.orientation,
                    chi_discontinuity_at_pi=gr.chiDiscAtPi,
                )

        gr.chiArray = lambda shape=None: ChiAt()

    def extract_data(self, sample, Imin, max_rings, pts_per_deg):
        """
        Extract data points for a given sampled geometry.

        Parameters
        ----------
        sample : list
            Geometry parameters
        Imin : float
            Minimum intensity threshold
        max_rings : int
            Maximum number of rings to consider
        pts_per_deg : int
            Number of points per degree for extraction

        Returns
        -------
        data : np.ndarray
            Extracted data points
        """
        dist, poni1, poni2, rot1, rot2, rot3 = sample
        geom_sample = Geometry(
            dist=dist,
            poni1=poni1,
            poni2=poni2,
            rot1=rot1,
            rot2=rot2,
            rot3=rot3,
            detector=self.detector,
            wavelength=self.calibrant.wavelength,
        )
        sg = _SingleGeometry(
            "Data Extraction Geometry",
            self.stacked_powder,
            calibrant=self.calibrant,
            detector=self.detector,
            geometry=geom_sample,
        )
        self._fast_maps(sg)
        with _fast_extract_cp(max_rings):
            sg.extract_cp(max_rings=max_rings, pts_per_deg=pts_per_deg, Imin=Imin)
        return sg.geometry_refinement.data

    def score(self, sample, Imin, max_rings, pts_per_deg):
        """
        Evaluate score at a given sampled geometry based on the residual between predicted and observed Bragg peak positions.

        Parameters
        ----------
        sample : list
            Geometry parameters
        Imin : float
            Minimum intensity threshold
        max_rings : int
            Maximum number of rings to consider

        Returns
        -------
        score : float
            Scalar score for Bayesian optimization
        """
        dist, poni1, poni2, rot1, rot2, rot3 = sample
        geom_sample = Geometry(
            dist=dist,
            poni1=poni1,
            poni2=poni2,
            rot1=rot1,
            rot2=rot2,
            rot3=rot3,
            detector=self.detector,
            wavelength=self.calibrant.wavelength,
        )
        sg = _SingleGeometry(
            "Score Geometry",
            self.stacked_powder,
            calibrant=self.calibrant,
            detector=self.detector,
            geometry=geom_sample,
        )
        self._fast_maps(sg)
        with _fast_extract_cp(max_rings):
            sg.extract_cp(max_rings=max_rings, pts_per_deg=pts_per_deg, Imin=Imin)
        data = sg.geometry_refinement.data

        # A handful of points can fit any geometry, so too few points is a failed geometry.
        if data is None or len(data) == 0 or len(data) < self.min_points:
            return 0.0

        ix = data[:, 0]
        iy = data[:, 1]
        ring = data[:, 2].astype(np.int32)
        score = -np.log(
            sg.geometry_refinement.residu2(sample, ix, iy, ring) / len(data)
        )
        return score

    def estimate_uncertainty(self, refinement, rel_eps=1e-3, abs_eps=1e-4):
        """
        Estimate parameter uncertainties from the Hessian matrix.

        Parameters
        ----------
        refinement : GeometryRefinement
            pyFAI refinement object after refine3.
        rel_eps : float
            Relative step for finite differences.
        abs_eps : float
            Absolute step for finite differences.

        Returns
        -------
        sigmas : np.ndarray
            Estimated uncertainties for each parameter
        is_min : bool
            True if a local minimum was found
        """
        param0 = np.array(
            [
                refinement.dist,
                refinement.poni1,
                refinement.poni2,
                refinement.rot1,
                refinement.rot2,
                refinement.rot3,
            ],
            dtype=np.float64,
        )
        param_names = ["dist", "poni1", "poni2", "rot1", "rot2"]
        size = len(param_names)

        d1 = refinement.data[:, 0]
        d2 = refinement.data[:, 1]
        ring = refinement.data[:, 2].astype(np.int32)
        f_min = refinement.residu2(param0, d1, d2, ring)
        hessian = np.zeros((size, size), dtype=np.float64)
        dof = max(len(refinement.data) - size, 1)

        delta = np.maximum(rel_eps * np.abs(param0), abs_eps)
        for i in range(size):
            deltai = delta[i]
            param = param0.copy()
            param[i] += deltai
            f_plus = refinement.residu2(param, d1, d2, ring)
            param = param0.copy()
            param[i] -= deltai
            f_minus = refinement.residu2(param, d1, d2, ring)
            hessian[i, i] = (f_plus + f_minus - 2.0 * f_min) / (deltai**2)

            for j in range(i + 1, size):
                deltaj = delta[j]
                param = param0.copy()
                param[i] += deltai
                param[j] += deltaj
                f_pp = refinement.residu2(param, d1, d2, ring)
                param = param0.copy()
                param[i] -= deltai
                param[j] -= deltaj
                f_mm = refinement.residu2(param, d1, d2, ring)
                param = param0.copy()
                param[i] += deltai
                param[j] -= deltaj
                f_pm = refinement.residu2(param, d1, d2, ring)
                param = param0.copy()
                param[i] -= deltai
                param[j] += deltaj
                f_mp = refinement.residu2(param, d1, d2, ring)
                hessian[j, i] = hessian[i, j] = (f_pp + f_mm - f_pm - f_mp) / (
                    4.0 * deltai * deltaj
                )

        eigs, _ = np.linalg.eigh(hessian)
        if np.any(eigs <= 0):
            sigmas = [np.inf] * size
            penalty = 0.0
            return sigmas, penalty

        cov = np.linalg.inv(hessian)
        sigmas = f_min * np.diag(cov) / dof
        sigmas = np.sqrt(sigmas)
        penalty = -np.log(np.linalg.det(cov))
        return sigmas, penalty

    def gradient_descent(self, sample, data, resolutions, step=5):
        """
        Run gradient descent refinement on the given sample parameters using pyFAI.

        Parameters
        ----------
        sample : list
            Sample parameters for the refinement
        data : np.ndarray
            Data used for the refinement
        resolutions : dict
            Resolution per parameter for restricted refinement
        step : int
            Size of the refinement space around best parameters

        Returns
        -------
        score : float
            Negative log of the residual after refinement
        sigma : np.ndarray
            Estimated uncertainties for each parameter
        penalty : float
            Penalty from uncertainty estimation
        params : list
            Refined parameters
        """
        dist, poni1, poni2, rot1, rot2, rot3 = sample

        if data is None or len(data) == 0 or len(data) < self.min_points:
            return 0.0, [np.inf] * 5, 0.0, sample

        gr = GeometryRefinement(
            data=data,
            calibrant=self.calibrant,
            dist=dist,
            poni1=poni1,
            poni2=poni2,
            rot1=rot1,
            rot2=rot2,
            rot3=rot3,
            detector=self.detector,
            wavelength=self.calibrant.wavelength,
        )
        gr.set_dist_min(dist - step * resolutions["dist"])
        gr.set_dist_max(dist + step * resolutions["dist"])
        gr.set_poni1_min(poni1 - step * resolutions["poni1"])
        gr.set_poni1_max(poni1 + step * resolutions["poni1"])
        gr.set_poni2_min(poni2 - step * resolutions["poni2"])
        gr.set_poni2_max(poni2 + step * resolutions["poni2"])
        gr.set_rot1_min(rot1 - step * resolutions["rot1"])
        gr.set_rot1_max(rot1 + step * resolutions["rot1"])
        gr.set_rot2_min(rot2 - step * resolutions["rot2"])
        gr.set_rot2_max(rot2 + step * resolutions["rot2"])
        fix = ["rot3", "wavelength"]
        score = -np.log(gr.refine3(fix=fix))
        sigma, penalty = self.estimate_uncertainty(gr)
        params = gr.param
        self.gr = gr
        return score, sigma, penalty, params

    @ignore_warnings(category=ConvergenceWarning)
    def bayes_opt_rank(
        self,
        value,
        center,
        bounds,
        res,
        n_samples,
        n_iterations,
        Imin,
        max_rings,
        pts_per_deg,
        beta=1.96,
        prior=True,
        step=5,
        seed=None,
        n_cand=None,
        gp_restarts=10,
    ):
        """
        Run Bayesian Optimization on this MPI rank's search space.

        Parameters
        ----------
        value : float, optional
            Shifted center for the parallelized parameter on this MPI rank,
            or None if no parameter is parallelized.
        center : dict
            Dictionary of center values for each parameter
        bounds : dict
            Dictionary of absolute (low, high) bounds for each parameter
        res : dict
            Dictionary of resolution for each parameter
        n_samples : int
            Number of samples to initialize the Gaussian Process
        n_iterations : int
            Number of iterations of Bayesian Optimization
        Imin : float
            Minimum intensity threshold for identifying Bragg peaks
        max_rings : int
            Maximum number of rings to search for Bragg peaks
        pts_per_deg : float
            Number of Bragg peaks to extract per azimuthal degree
        beta : float
            Exploration-exploitation trade-off parameter for UCB acquisition function
        prior : bool
            Whether to sample initial points around the center or randomly
        step : int
            Step size for the gradient descent refinement
        seed : optional, int
            Random seed for reproducibility. Offset by this rank's index so
            that every rank is independently seeded.
        gp_restarts : int
            n_restarts_optimizer of the GP kernel-hyperparameter fit
        """
        if seed is not None:
            np.random.seed(seed + self.rank)

        # 1. Create the search space
        X, X_norm = self.create_search_space(value, center, bounds, res)
        center[self.parallelized] = value

        # 2. Sample initial points
        X_samples, X_norm_samples = self.sample_initial_points(
            X, X_norm, center, bounds, n_samples, prior
        )

        # 3. Evaluate the initial points
        bo_history = {"params": [], "scores": []}
        y = np.zeros((n_samples))
        for i in range(n_samples):
            y[i] = self.score(X_samples[i], Imin, max_rings, pts_per_deg)
            bo_history["params"].append(X_samples[i])
            bo_history["scores"].append(y[i])

        if np.all(y == 0.0):
            result = {
                "bo_history": bo_history,
                "params": np.array(
                    [
                        center[k]
                        for k in ("dist", "poni1", "poni2", "rot1", "rot2", "rot3")
                    ]
                ),
                "score": 0.0,
                "sigma": [np.inf] * 5,
                "penalty": 0.0,
                "best_idx": 0,
            }
            if self.rank == 0:
                logger.warning(
                    "Skipping Bayesian Optimization because all initial scores are zero."
                )
                logger.warning(
                    "Initial geometry guess is too far from optimum. Please refine the search space."
                )
            return result

        if np.std(y) != 0:
            y_norm = (y - np.mean(y)) / np.std(y)
        else:
            y_norm = y - np.mean(y)

        # 4. Initialize the Gaussian Process model
        kernel = _Matern(
            length_scale=0.3, length_scale_bounds=(0.2, 0.4), nu=2.5
        ) * _ConstantKernel(
            constant_value=1.0, constant_value_bounds=(0.5, 1.5)
        ) + _WhiteKernel(
            noise_level=0.001, noise_level_bounds="fixed"
        )
        gp_model = GaussianProcessRegressor(
            kernel=kernel, n_restarts_optimizer=gp_restarts, random_state=0
        )
        gp_model.fit(X_norm_samples, y_norm)
        visited_idx = list([])

        # 5. Run the Bayesian Optimization loop
        for i in range(n_iterations):
            # 6. Select the next point to evaluate
            next = self.UCB(X_norm, gp_model, visited_idx, beta, n_cand)
            next_sample = X[next]
            visited_idx.append(next)

            # 7. Compute the score of the next point
            score = self.score(next_sample, Imin, max_rings, pts_per_deg)
            y = np.append(y, [score], axis=0)
            bo_history["params"].append(next_sample)
            bo_history["scores"].append(score)
            X_samples = np.append(X_samples, [X[next]], axis=0)
            X_norm_samples = np.append(X_norm_samples, [X_norm[next]], axis=0)
            if np.std(y) != 0:
                y_norm = (y - np.mean(y)) / np.std(y)
            else:
                y_norm = y - np.mean(y)

            # 8. Update the Gaussian Process model
            if i < n_iterations - 1:  # the final refit is never read
                gp_model.fit(X_norm_samples, y_norm)

        # 9. Gather results
        best_idx = np.argmax(y)
        best_param = X_samples[best_idx]
        data = self.extract_data(best_param, Imin, max_rings, pts_per_deg)
        score, sigma, penalty, params = self.gradient_descent(
            best_param, data, res, step
        )
        logger.info(f"Rank {self.rank}: score={score:3e}, penalty={penalty:3e}")
        result = {
            "bo_history": bo_history,
            "params": params,
            "score": score,
            "sigma": sigma,
            "penalty": penalty,
            "best_idx": best_idx,
        }
        return result

    def bayfai_opt(
        self,
        center,
        bounds,
        res,
        n_samples,
        n_iterations,
        Imin,
        max_rings,
        pts_per_deg,
        beta=1.96,
        prior=True,
        step=5,
        lbda=0.1,
        seed=None,
        n_cand=None,
        dist_spacing="geometric",
        gp_restarts=10,
        min_points=None,
    ):
        """
        Run BayFAI optimization.
        Distribute BO across MPI ranks for the parallelized parameter.
        Run Bayesian Optimization on each rank with fixed search space.
        Perform pyFAI least-squares refinement for each rank's best geometry.
        Optimal geometry is chosen based on the lowest residual among ranks.

        Parameters
        ----------
        center : dict
            Dictionary of center values for each parameter
        bounds : dict
            Dictionary of absolute (low, high) bounds for each parameter
        res : dict
            Dictionary of resolution for each parameter
        n_samples : int
            Number of samples to initialize the Gaussian Process
        n_iterations : int
            Number of iterations of Bayesian Optimization
        Imin : float
            Minimum intensity threshold for identifying Bragg peaks
        max_rings : int
            Maximum number of rings to consider
        pts_per_deg: float
            Number of Bragg peaks to extract per azimuthal degree
        beta : float
            Exploration-exploitation trade-off parameter for UCB acquisition function
        step : int
            Size of the refinement space around best parameters
        prior : bool
            Whether to sample initial points around the center or randomly
        seed : optional, int
            Random seed for reproducibility
        n_cand : optional, int
            Number of random grid candidates scored by UCB per step (None/0 = whole grid)
        dist_spacing : str
            'geometric' (default) or 'linear' split of bounds['dist'] across ranks when
            parallelized == 'dist'
        gp_restarts : int
            n_restarts_optimizer of the GP kernel-hyperparameter fit
        min_points : int, optional
            Geometries with fewer extracted control points score 0 (0 = off; None = 100
            when parallelized == 'dist', else 0)
        """
        # Distance sweeps reach geometries where a few stray points fit; MEC boxes do not.
        if min_points is None:
            min_points = 100 if self.parallelized == "dist" else 0
        self.min_points = min_points
        # Split the parallelized parameter's absolute bounds into one window per MPI rank
        res = dict(res)
        value = self.distribute_parallelized(bounds, res, dist_spacing)
        if self.rank == 0:
            if self.parallelized is not None:
                logger.info(
                    f"Running BayFAI parallelizing {self.size} ranks over '{self.parallelized}'"
                )
            else:
                logger.info(f"Running BayFAI on {self.size} ranks")

        bayfai_hyperparams = {
            "n_samples": n_samples,
            "n_iterations": n_iterations,
            "Imin": Imin,
            "max_rings": max_rings,
            "pts_per_deg": pts_per_deg,
            "beta": beta,
            "step": step,
            "prior": prior,
            "seed": seed,
            "n_cand": n_cand,
            "gp_restarts": gp_restarts,
        }

        # Run BO on this rank's search space
        results = self.bayes_opt_rank(
            value,
            center,
            bounds,
            res,
            **bayfai_hyperparams,
        )

        if self.rank == 0:
            # Rank 0 mostly waits at the Barrier for slower ranks: hide its plotting imports there.
            # They leave numpy's global RNG state untouched, so later pyFAI draws are unchanged.
            import matplotlib.pyplot  # type: ignore # noqa: F401
            import bokeh.plotting  # type: ignore # noqa: F401
            import panel  # type: ignore # noqa: F401

        # Gather BayFAI results from all ranks
        self.comm.Barrier()
        self.scan = {}
        self.scan["bo_history"] = self.comm.gather(results["bo_history"], root=0)
        self.scan["params"] = self.comm.gather(results["params"], root=0)
        self.scan["score"] = self.comm.gather(results["score"], root=0)
        self.scan["sigma"] = self.comm.gather(results["sigma"], root=0)
        self.scan["penalty"] = self.comm.gather(results["penalty"], root=0)
        self.scan["best_idx"] = self.comm.gather(results["best_idx"], root=0)

        # Winner selection
        if self.rank == 0:
            for key in self.scan.keys():
                self.scan[key] = np.array([item for item in self.scan[key]])

            self.final_score = self.scan["score"] + lbda * self.scan["penalty"]
            self.index = np.argmax(self.final_score)
            self.bo_history = self.scan["bo_history"][self.index]
            self.params = self.scan["params"][self.index]
            self.neglog_score = self.scan["score"][self.index]
            self.sigma = self.scan["sigma"][self.index]
            self.penalty = self.scan["penalty"][self.index]
            self.best_idx = self.scan["best_idx"][self.index]
            self.gr = GeometryRefinement(
                calibrant=self.calibrant,
                dist=self.params[0],
                poni1=self.params[1],
                poni2=self.params[2],
                rot1=self.params[3],
                rot2=self.params[4],
                rot3=self.params[5],
                detector=self.detector,
                wavelength=self.calibrant.wavelength,
            )
            # One integrator for all rank-0 plots: its 2theta/q arrays and integration engines are cached.
            self.ai = _build_ai(self.detector, self.params, self.calibrant.wavelength)

    def plot_radial_integration(self, result1d, calibrant, ax=None):
        """
        Plot the radial integration of a powder image

        Parameters
        ----------
        result1d : pyFAI.containers.Integrate1dResult
            Result from AzimuthalIntegrator.integrate1d, containing .radial (q in Å⁻¹) and .intensity.
        calibrant : Calibrant
            Calibrant object
        ax : plt.Axes
            Matplotlib axes
        """
        import matplotlib.pyplot as plt  # type: ignore
        from matplotlib import lines  # type: ignore

        if ax is None:
            fig, ax = plt.subplots()

        unit = RADIAL_UNITS["q_A^-1"]
        ax.plot(result1d.radial, result1d.intensity, color="black", linewidth=0.8)

        x_values = calibrant.get_peaks(unit)
        if x_values is not None:
            for x in x_values:
                line = lines.Line2D(
                    [x, x],
                    ax.axis()[2:4],
                    color="red",
                    linestyle="--",
                    linewidth=0.8,
                    alpha=0.7,
                )
                ax.add_line(line)

        ax.set_title("Radial Profile", fontsize=6)
        if unit:
            ax.set_xlabel(unit.label, fontsize=6)
        ax.set_ylabel("Intensity", fontsize=6)
        ax.tick_params(axis="x", labelsize=4)
        ax.tick_params(axis="y", labelsize=4)

    def plot_2d_integration(self, result2d, calibrant, ax=None):
        """
        Plot the 2D azimuthal integration (cake plot) of a powder image.

        Parameters
        ----------
        result2d : pyFAI.containers.Integrate2dResult
            Result from AzimuthalIntegrator.integrate2d, containing .intensity,
            .radial (q in Å⁻¹), and .azimuthal (χ in °).
        calibrant : pyFAI.calibrant.Calibrant
            Calibrant object for Bragg peak overlay.
        ax : plt.Axes, optional
            Matplotlib axes.
        """
        import matplotlib.pyplot as plt  # type: ignore

        if ax is None:
            _fig, ax = plt.subplots()

        cake = result2d.intensity
        non_zero = cake[cake > 0]
        vmin = np.percentile(non_zero, 5) if len(non_zero) > 0 else 0
        vmax = np.percentile(non_zero, 95) if len(non_zero) > 0 else 1

        ax.imshow(
            cake,
            extent=[
                result2d.radial.min(),
                result2d.radial.max(),
                result2d.azimuthal.min(),
                result2d.azimuthal.max(),
            ],
            aspect="auto",
            origin="lower",
            vmin=vmin,
            vmax=vmax,
            cmap="viridis",
        )

        unit = RADIAL_UNITS["q_A^-1"]
        peaks = calibrant.get_peaks(unit)
        if peaks is not None:
            for q_peak in peaks:
                ax.axvline(
                    q_peak, color="red", linestyle="--", linewidth=0.8, alpha=0.7
                )

        ax.set_xlabel(unit.label, fontsize=6)
        ax.set_ylabel(r"$\chi$ (°)", fontsize=6)
        ax.tick_params(axis="x", labelsize=4)
        ax.tick_params(axis="y", labelsize=4)
        ax.set_title("2D Azimuthal Integration", fontsize=6)

    def plot_bo_history(self, ax):
        """
        Plot the Bayesian Optimization history across all ranks

        Parameters
        ----------
        bo_history : list
            List of all the BO histories with keys 'params' and 'scores' for each rank-distance
        ax : plt.Axes
            Matplotlib axes
        """
        bo_history = self.scan["bo_history"]
        iters = np.arange(len(bo_history[self.index]["scores"]))
        if self.param_values is not None:
            label = f"Best {self.parallelized}: {self.param_values[self.index]:.3f}"
        else:
            label = f"Best rank: {self.index}"
        ax.plot(
            iters,
            bo_history[self.index]["scores"],
            marker="o",
            markersize=3,
            linestyle="--",
            linewidth=0.8,
            color="black",
            markerfacecolor="red",
            markeredgecolor="black",
            label=label,
        )
        ax.legend(fontsize=6)
        ax.set_xlabel("Iteration", fontsize=6)
        ax.set_ylabel("Score", fontsize=6)
        ax.yaxis.get_offset_text().set_fontsize(6)
        ax.tick_params(axis="x", labelsize=6)
        ax.tick_params(axis="y", labelsize=6)
        ax.set_title("Bayesian Optimization History", fontsize=6)

    def plot_score_distance_scan(self, ax):
        """
        Plot the score scan over distance

        Parameters
        ----------
        ax : plt.Axes
            Matplotlib axes
        """
        x = self.param_values if self.param_values is not None else np.arange(self.size)
        xlabel = self.parallelized if self.parallelized is not None else "Rank"
        ax.plot(x, self.scan["score"], linewidth=0.8, color="k")
        ax.set_xlabel(xlabel, fontsize=6)
        ax.set_ylabel(
            r"$-\log\left(\frac{1}{N}\sum (2\theta_g - 2\theta_c)^2\right)$", fontsize=6
        )
        ax.yaxis.get_offset_text().set_fontsize(6)
        ax.tick_params(axis="x", labelsize=6)
        ax.tick_params(axis="y", labelsize=6)
        ax.set_title(
            f"Score vs {xlabel}",
            fontsize=6,
        )

    def plot_residual_distance_scan(self, ax):
        """
        Plot the residual scan over distance

        Parameters
        ----------
        ax : plt.Axes
            Matplotlib axes
        """
        x = self.param_values if self.param_values is not None else np.arange(self.size)
        xlabel = self.parallelized if self.parallelized is not None else "Rank"
        ax.plot(x, self.final_score, linewidth=0.8, color="k")
        ax.scatter(
            x[self.index],
            self.final_score[self.index],
            color="red",
            s=50,
            marker="*",
        )
        ax.set_xlabel(xlabel, fontsize=6)
        ax.set_ylabel(
            r"$-\log\left(\frac{1}{N}\sum (2\theta_g - 2\theta_c)^2\right)$", fontsize=6
        )
        ax.yaxis.get_offset_text().set_fontsize(6)
        ax.tick_params(axis="x", labelsize=6)
        ax.tick_params(axis="y", labelsize=6)
        ax.set_title(
            f"Penalized Score vs {xlabel}",
            fontsize=6,
        )

    def plot_intensity_hist(self, powder, Imin, ax):
        """
        Plot histogram of pixel intensities in the powder image

        Parameters
        ----------
        powder : np.ndarray
            Powder image
        exp : str
            Experiment name
        run : int
            Run number
        Imin : float
            Minimum intensity threshold for identifying Bragg peaks
        ax : plt.Axes
            Matplotlib axes
        """
        mean = np.mean(powder)
        std_dev = np.std(powder)
        nice_pix = powder[np.where(powder < mean + 2 * std_dev)]
        _ = ax.hist(
            nice_pix.ravel(),
            bins=100,
            color="skyblue",
            edgecolor="black",
            alpha=0.7,
            label="Pixel Intensities",
        )
        ax.axvline(
            mean,
            color="red",
            linestyle="--",
            label=f"Mean ({mean:.2f})",
        )
        ax.axvline(
            mean + std_dev,
            color="orange",
            linestyle="--",
            label=f"Mean + Std Dev ({mean + std_dev:.2f})",
        )
        ax.axvline(
            mean + 2 * std_dev,
            color="green",
            linestyle="--",
            label=f"Mean + 2 Std Dev ({mean + 2 * std_dev:.2f})",
        )
        ax.axvline(
            Imin,
            color="purple",
            linestyle=":",
            linewidth=2,
            label=f"Minimum Intensity ({Imin:.2f})",
        )
        ax.set_xlim([0, mean + 2 * std_dev])
        ax.set_xlabel("Pixel Intensity", fontsize=6)
        ax.set_ylabel("Frequency", fontsize=6)
        ax.set_xticks([])
        ax.set_xticklabels([])
        ax.tick_params(axis="y", labelsize=4)
        ax.set_title(
            f"Histogram of Pixel Intensities \n for {self.exp} run {self.run}",
            fontsize=6,
        )
        ax.legend(fontsize=6)

    def plot_powder_and_resolution(self, ax=None):
        """
        Plot the powder image with calibrated overlapping 2θ rings.

        Parameters
        ----------
        ax : plt.Axes, optional
            Matplotlib axes
        """
        import matplotlib.pyplot as plt  # type: ignore

        if ax is None:
            _fig, ax = plt.subplots()

        # Physical coordinates powder plot
        p1, p2, _ = self.detector.calc_cartesian_positions()

        ax.imshow(
            self.assembled_powder,
            extent=[p2.min(), p2.max(), p1.min(), p1.max()],
            vmin=np.percentile(self.powder, 5),
            vmax=np.percentile(self.powder, 95),
        )

        # Ring overlay
        ai = self.ai
        tth = np.array(self.calibrant.get_2th())
        ttha = ai.twoThetaArray().reshape(self.detector.calib_shape)
        p1_3d = p1.reshape(self.detector.calib_shape)
        p2_3d = p2.reshape(self.detector.calib_shape)
        for m in range(self.detector.n_modules):
            ax.contour(
                p2_3d[m],
                p1_3d[m],
                ttha[m],
                levels=tth,
                cmap="autumn",
                linewidths=1,
                linestyles="dashed",
            )

        # Control points overlay
        if self.gr.data is not None and len(self.gr.data) > 0:
            d1_idx = self.gr.data[:, 0].astype(int)
            d2_idx = self.gr.data[:, 1].astype(int)
            rings = self.gr.data[:, 2].astype(int)
            cp_x = p2[d1_idx, d2_idx]
            cp_y = p1[d1_idx, d2_idx]
            cmap = plt.get_cmap("tab10")
            for ring_id in np.unique(rings):
                mask = rings == ring_id
                ax.scatter(
                    cp_x[mask],
                    cp_y[mask],
                    s=10,
                    color=cmap(ring_id % 10),
                    alpha=0.6,
                    label=f"Ring {ring_id}",
                    zorder=3,
                )
            ax.legend(
                fontsize=5,
                markerscale=2,
                title=f"N={len(self.gr.data)}",
                title_fontsize=5,
            )

        # Resolution overlay
        radii = ai.rArray()
        q_array = ai.qArray() / 10  # nm⁻¹ → Å⁻¹

        d1c, d2c = np.unravel_index(np.argmin(radii), p1.shape)
        closest_resol = 2 * np.pi / q_array[d1c, d2c]
        ax.text(
            p2[d1c, d2c],
            p1[d1c, d2c],
            f"{closest_resol:.3f} Å",
            color="red",
            fontsize=10,
            bbox=dict(facecolor="white", alpha=0.6, edgecolor="none", pad=1),
        )

        d1f, d2f = np.unravel_index(np.argmax(radii), p1.shape)
        furthest_resol = 2 * np.pi / q_array[d1f, d2f]
        ax.text(
            p2[d1f, d2f],
            p1[d1f, d2f],
            f"{furthest_resol:.3f} Å",
            color="red",
            fontsize=10,
            bbox=dict(facecolor="white", alpha=0.6, edgecolor="none", pad=1),
        )

        ax.set_xlabel("X-axis (m)", fontsize=8)
        ax.set_ylabel("Y-axis (m)", fontsize=8)
        ax.tick_params(axis="x", labelsize=6)
        ax.tick_params(axis="y", labelsize=6)
        ax.set_title(
            f"Run {self.run} - {self.detector.detname} - {self.calibrant_name}",
            fontsize=8,
        )
        ax.set_aspect("equal")

    def create_interactive_powder(
        self,
    ):
        """
        Create an interactive powder image with calibrated overlapping 2θ rings.
        """
        from bokeh.plotting import figure  # type: ignore
        from bokeh.models import LinearColorMapper, HoverTool  # type: ignore
        from bokeh.palettes import Viridis256, Category10  # type: ignore
        from bokeh.models.annotations import Label  # type: ignore

        p1, p2, _ = self.detector.calc_cartesian_positions()
        # d1 = vertical (bottom→top), d2 = horizontal (left→right)
        y = np.reshape(p1, self.detector.calib_shape)
        x = np.reshape(p2, self.detector.calib_shape)
        xmin, xmax = x.min(), x.max()
        ymin, ymax = y.min(), y.max()

        p = figure(
            title=f"Run {self.run} - {self.detector.detname} - {self.calibrant_name}",
            x_axis_label="X-axis (m)",
            y_axis_label="Y-axis (m)",
            width=1200,
            height=1200,
            match_aspect=True,
            x_range=(xmin, xmax),
            y_range=(ymin, ymax),
        )

        vmin, vmax = (
            np.percentile(self.stacked_powder, 5),
            np.percentile(self.stacked_powder, 95),
        )
        color_mapper = LinearColorMapper(palette=Viridis256, low=vmin, high=vmax)

        p.image(
            image=[self.assembled_powder[::-1, :]],
            x=xmin,
            y=ymin,
            dw=(xmax - xmin),
            dh=(ymax - ymin),
            color_mapper=color_mapper,
        )

        ai = self.ai
        tth = np.array(self.calibrant.get_2th())
        ttha = ai.twoThetaArray().reshape(self.detector.calib_shape)
        for i in range(self.detector.n_modules):
            p.contour(
                x=x[i],
                y=y[i],
                z=ttha[i],
                levels=tth,
                line_color="red",
                line_width=3,
                line_dash="dashed",
            )

        if self.gr.data is not None and len(self.gr.data) > 0:
            d1_idx = self.gr.data[:, 0].astype(int)
            d2_idx = self.gr.data[:, 1].astype(int)
            rings = self.gr.data[:, 2].astype(int)
            cp_x = p2[d1_idx, d2_idx]
            cp_y = p1[d1_idx, d2_idx]
            n_rings = len(np.unique(rings))
            palette = Category10[max(3, min(10, n_rings))]
            for ring_id in np.unique(rings):
                mask = rings == ring_id
                p.circle(
                    cp_x[mask].tolist(),
                    cp_y[mask].tolist(),
                    size=10,
                    color=palette[ring_id % len(palette)],
                    alpha=0.6,
                    legend_label=f"Ring {ring_id}",
                )
            p.legend.title = f"Control points (N={len(self.gr.data)})"
            p.legend.label_text_font_size = "8pt"
            p.legend.click_policy = "hide"

        radii = ai.rArray().reshape(self.detector.calib_shape)
        q_array = ai.qArray().reshape(self.detector.calib_shape) / 10  # nm⁻¹ → Å⁻¹
        closest_pixel_index = np.argmin(radii)
        closest_pixel = (
            x.flatten()[closest_pixel_index],
            y.flatten()[closest_pixel_index],
        )
        closest_q = q_array.flatten()[closest_pixel_index]
        closest_resol = 2 * np.pi / closest_q

        furthest_pixel_index = np.argmax(radii)
        furthest_pixel = (
            x.flatten()[furthest_pixel_index],
            y.flatten()[furthest_pixel_index],
        )
        furthest_q = q_array.flatten()[furthest_pixel_index]
        furthest_resol = 2 * np.pi / furthest_q

        pixel_lvls = np.array([closest_pixel, furthest_pixel])
        resol_lvls = np.array([closest_resol, furthest_resol])
        for pixel, resol in zip(pixel_lvls, resol_lvls):
            label_annotation = Label(
                x=pixel[0],
                y=pixel[1],
                text=f"{resol:.3f} Å",
                text_color="red",
                text_font_size="16pt",
            )
            p.add_layout(label_annotation)

        hover = HoverTool(
            tooltips=[
                ("x", "@x{0.000}"),
                ("y", "@y{0.000}"),
                ("Intensity", "@intensity{0.0}"),
            ]
        )
        p.add_tools(hover)
        p.title.text_font_size = "12pt"
        p.xaxis.axis_label_text_font_size = "10pt"
        p.yaxis.axis_label_text_font_size = "10pt"
        p.xaxis.major_label_text_font_size = "8pt"
        p.yaxis.major_label_text_font_size = "8pt"

        qs = {
            "closest": closest_q,
            "furthest": furthest_q,
        }
        resolutions = {
            "closest": closest_resol,
            "furthest": furthest_resol,
        }
        return p, qs, resolutions

    def create_diagnostics_panel(
        self,
        plot="",
    ):
        """
        Create a diagnostics panel with the results of the Bayesian Optimization.

        Parameters
        ----------
        plot : str
            Path to save plot
        """
        import matplotlib.pyplot as plt  # type: ignore
        import matplotlib.patches as patches  # type: ignore

        fig = plt.figure(figsize=(8, 9), dpi=100)
        nrow, ncol = 3, 2

        # (0,0) Summary text
        ax1 = plt.subplot2grid((nrow, ncol), (0, 0))
        rect = patches.Rectangle(
            (0, 0),
            1,
            1,
            transform=ax1.transAxes,
            color="lightgrey",
            alpha=0.3,
        )
        ax1.add_patch(rect)
        ax1.text(
            0.05,
            0.9,
            f"Experiment {self.exp}",
            ha="left",
            va="center",
            fontsize=8,
        )
        ax1.text(0.05, 0.8, f"Run {self.run}", ha="left", va="center", fontsize=8)
        ax1.text(
            0.05,
            0.7,
            f"Detector {self.detector.detname}",
            ha="left",
            va="center",
            fontsize=8,
        )
        ax1.text(
            0.05,
            0.6,
            f"Calibrant {self.calibrant_name}",
            ha="left",
            va="center",
            fontsize=8,
        )
        ax1.text(
            0.05,
            0.5,
            f"Distance = {1000 * self.params[0]:.3f} ± {1000 * self.sigma[0]:.3f} mm",
            ha="left",
            va="center",
            fontsize=8,
        )
        ax1.text(
            0.05,
            0.4,
            f"X-shift = {1000 * self.params[1]:.3f} ± {1000 * self.sigma[1]:.3f} mm",
            ha="left",
            va="center",
            fontsize=8,
        )
        ax1.text(
            0.05,
            0.3,
            f"Y-shift = {1000 * self.params[2]:.3f} ± {1000 * self.sigma[2]:.3f} mm",
            ha="left",
            va="center",
            fontsize=8,
        )
        ax1.text(
            0.05,
            0.2,
            f"RotX = {self.params[3]:.3f} ± {self.sigma[3]:.6f} rad",
            ha="left",
            va="center",
            fontsize=8,
        )
        ax1.text(
            0.05,
            0.1,
            f"RotY = {self.params[4]:.3f} ± {self.sigma[4]:.6f} rad",
            ha="left",
            va="center",
            fontsize=8,
        )
        ax1.axis("off")

        # (0,1) Pixel histogram
        ax2 = plt.subplot2grid((nrow, ncol), (0, 1))
        self.plot_intensity_hist(self.powder, self.Imin, ax2)

        # Compute 1D azimuthal integration via pyFAI
        ai = self.ai
        result1d = ai.integrate1d(self.stacked_powder, npt=256, unit="q_A^-1")

        # (1,0) 1D integration, spanning two cols
        ax3 = plt.subplot2grid((nrow, ncol), (1, 0), colspan=2)
        self.plot_radial_integration(result1d, self.calibrant, ax=ax3)

        # (2,0) Score vs distance
        ax4 = plt.subplot2grid((nrow, ncol), (2, 0))
        self.plot_score_distance_scan(ax4)

        # (2,1) Penalized score vs distance
        ax5 = plt.subplot2grid((nrow, ncol), (2, 1))
        self.plot_residual_distance_scan(ax5)

        fig.tight_layout()

        if plot != "":
            fig.savefig(plot, dpi=100)
        return fig

    def create_summary_plot(
        self,
        plot="",
    ):
        """
        Create a summary plot with the results of the Bayesian Optimization.

        Parameters
        ----------
        plot : str
            Path to save plot
        """
        import matplotlib.pyplot as plt  # type: ignore
        import matplotlib.patches as patches  # type: ignore

        fig = plt.figure(figsize=(9, 12), dpi=100)
        nrow, ncol = 4, 3

        # Labelling experiment and run number
        ax1 = plt.subplot2grid((nrow, ncol), (0, 0))
        rect = patches.Rectangle(
            (0, 0),
            1,
            1,
            transform=ax1.transAxes,
            color="lightgrey",
            alpha=0.3,
        )
        ax1.add_patch(rect)
        ax1.text(
            0.05,
            0.9,
            f"Experiment {self.exp}",
            ha="left",
            va="center",
            fontsize=8,
        )
        ax1.text(0.05, 0.8, f"Run {self.run}", ha="left", va="center", fontsize=8)
        ax1.text(
            0.05,
            0.7,
            f"Detector {self.detector.detname}",
            ha="left",
            va="center",
            fontsize=8,
        )
        ax1.text(
            0.05,
            0.6,
            f"Calibrant {self.calibrant_name}",
            ha="left",
            va="center",
            fontsize=8,
        )
        ax1.text(
            0.05,
            0.5,
            f"Distance = {1000 * self.params[0]:.3f} ± {1000 * self.sigma[0]:.3f} mm",
            ha="left",
            va="center",
            fontsize=8,
        )
        ax1.text(
            0.05,
            0.4,
            f"ShiftX = {1000 * self.params[1]:.3f} ± {1000 * self.sigma[1]:.3f} mm",
            ha="left",
            va="center",
            fontsize=8,
        )
        ax1.text(
            0.05,
            0.3,
            f"ShiftY = {1000 * self.params[2]:.3f} ± {1000 * self.sigma[2]:.3f} mm",
            ha="left",
            va="center",
            fontsize=8,
        )
        ax1.text(
            0.05,
            0.2,
            f"RotX = {self.params[3]:.3f} ± {self.sigma[3]:.6f} rad",
            ha="left",
            va="center",
            fontsize=8,
        )
        ax1.text(
            0.05,
            0.1,
            f"RotY = {self.params[4]:.3f} ± {self.sigma[4]:.6f} rad",
            ha="left",
            va="center",
            fontsize=8,
        )
        ax1.axis("off")

        # Compute azimuthal integrations via pyFAI
        ai = self.ai
        q_max = float(
            (ai.qArray().reshape(self.detector.calib_shape) / 10).max()
        )  # nm⁻¹ → Å⁻¹, detector-limited q maximum
        result1d = ai.integrate1d(
            self.stacked_powder, npt=256, unit="q_A^-1", radial_range=(0, q_max)
        )
        result2d = ai.integrate2d(
            self.stacked_powder,
            npt_rad=256,
            npt_azim=360,
            unit="q_A^-1",
            radial_range=(0, q_max),
        )

        # (0,1) Pixel histogram, spanning two cols
        ax2 = plt.subplot2grid((nrow, ncol), (0, 1), colspan=2)
        self.plot_intensity_hist(self.powder, self.Imin, ax2)

        # (1,0) Powder rings overlay, spanning two cols and two rows
        ax3 = plt.subplot2grid((nrow, ncol), (1, 0), rowspan=2, colspan=2)
        self.plot_powder_and_resolution(ax=ax3)

        # (1,2) Score vs distance
        ax4 = plt.subplot2grid((nrow, ncol), (1, 2))
        self.plot_score_distance_scan(ax4)

        # (2,2) Penalized score vs distance
        ax5 = plt.subplot2grid((nrow, ncol), (2, 2))
        self.plot_residual_distance_scan(ax5)

        # (3,0) 1D integration, spanning two cols
        ax6 = plt.subplot2grid((nrow, ncol), (3, 0), colspan=2)
        self.plot_radial_integration(result1d, self.calibrant, ax=ax6)

        # (3,2) 2D integration
        ax7 = plt.subplot2grid((nrow, ncol), (3, 2))
        self.plot_2d_integration(result2d, self.calibrant, ax7)

        fig.tight_layout()

        if plot != "":
            fig.savefig(plot, dpi=100)
        return fig

_massif cached property

The powder's Massif, built once: peaks_from_area only reads its data, mask and interpolator.

_pos64 cached property

float64 buffers for p1 - poni1, p2 - poni2 and p3: calc_tth copies other dtypes to float64.

UCB(X, gp_model, visited_idx, beta=1.96, n_cand=None) staticmethod

Return the index into X of the best unvisited UCB point, among n_cand random ones if set.

Source code in lute/tasks/_bayfai.py
351
352
353
354
355
356
357
358
@staticmethod
def UCB(X, gp_model, visited_idx, beta=1.96, n_cand=None):
    """Return the index into X of the best unvisited UCB point, among n_cand random ones if set."""
    idx = np.delete(np.arange(len(X)), visited_idx)
    if n_cand and len(idx) > n_cand:
        idx = np.random.choice(idx, n_cand, replace=False)
    y_pred, y_std = gp_model.predict(X[idx], return_std=True)
    return idx[np.argmax(y_pred + beta * y_std)]

_fast_maps(sg)

Serve extract_cp's Massif and 2theta/chi maps from per-rank caches (same cython kernels).

chiArray is only read at ring-contour pixels, so chi is computed per indexed pixel instead of over the full grid; both skip pyFAI's fromfunction index grids. The maps are left alone unless the inputs match.

Source code in lute/tasks/_bayfai.py
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
def _fast_maps(self, sg):
    """Serve extract_cp's Massif and 2theta/chi maps from per-rank caches (same cython kernels).

    chiArray is only read at ring-contour pixels, so chi is computed per indexed pixel instead of
    over the full grid; both skip pyFAI's fromfunction index grids. The maps are left alone unless
    the inputs match.
    """
    # Each extract_cp otherwise allocates and frees ~1 GiB on jungfrau16M; on a node whose memory is
    # full of page cache, re-faulting those pages costs more kernel time than the scoring itself.
    sg.massif = self._massif
    gr = sg.geometry_refinement
    if not (
        self.stacked_powder.shape == self.detector.shape
        and gr._parallax is None
        and gr.chiDiscAtPi is True
        and gr.detector is self.detector
    ):
        return
    p1, p2, p3 = self.detector.calc_cartesian_positions()
    b1, b2, b3 = self._pos64
    # dtype=p1.dtype: the difference is still taken in the positions' precision, then widened.
    gr.twoThetaArray = lambda shape=None: _geometry.calc_tth(
        L=gr._dist,
        rot1=gr._rot1,
        rot2=gr._rot2,
        rot3=gr._rot3,
        pos1=np.subtract(p1, gr._poni1, out=b1, dtype=p1.dtype),
        pos2=np.subtract(p2, gr._poni2, out=b2, dtype=p2.dtype),
        pos3=b3,
    )

    class ChiAt:
        def __getitem__(_, idx):
            return _geometry.calc_chi(
                L=gr._dist,
                rot1=gr._rot1,
                rot2=gr._rot2,
                rot3=gr._rot3,
                pos1=p1[idx] - gr._poni1,
                pos2=p2[idx] - gr._poni2,
                pos3=None if p3 is None else p3[idx],
                orientation=gr.detector.orientation,
                chi_discontinuity_at_pi=gr.chiDiscAtPi,
            )

    gr.chiArray = lambda shape=None: ChiAt()

assemble_image(powder)

Assemble the powder image from modules to full detector shape.

Parameters

powder : npt.NDArray[np.float64] Powder image to use for calibration

Source code in lute/tasks/_bayfai.py
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
def assemble_image(
    self, powder: npt.NDArray[np.float64]
) -> npt.NDArray[np.float64]:
    """
    Assemble the powder image from modules to full detector shape.

    Parameters
    ----------
    powder : npt.NDArray[np.float64]
        Powder image to use for calibration
    """
    pixel_index_map = self.detector.pixel_index_map
    max_rows = np.max(pixel_index_map[..., 0]) + 1
    max_cols = np.max(pixel_index_map[..., 1]) + 1
    assembled_powder = np.zeros((max_rows, max_cols))
    for p in range(pixel_index_map.shape[0]):
        i = pixel_index_map[p, ..., 0]
        j = pixel_index_map[p, ..., 1]
        assembled_powder[i, j] = powder[p]
    return assembled_powder

bayes_opt_rank(value, center, bounds, res, n_samples, n_iterations, Imin, max_rings, pts_per_deg, beta=1.96, prior=True, step=5, seed=None, n_cand=None, gp_restarts=10)

Run Bayesian Optimization on this MPI rank's search space.

Parameters

value : float, optional Shifted center for the parallelized parameter on this MPI rank, or None if no parameter is parallelized. center : dict Dictionary of center values for each parameter bounds : dict Dictionary of absolute (low, high) bounds for each parameter res : dict Dictionary of resolution for each parameter n_samples : int Number of samples to initialize the Gaussian Process n_iterations : int Number of iterations of Bayesian Optimization Imin : float Minimum intensity threshold for identifying Bragg peaks max_rings : int Maximum number of rings to search for Bragg peaks pts_per_deg : float Number of Bragg peaks to extract per azimuthal degree beta : float Exploration-exploitation trade-off parameter for UCB acquisition function prior : bool Whether to sample initial points around the center or randomly step : int Step size for the gradient descent refinement seed : optional, int Random seed for reproducibility. Offset by this rank's index so that every rank is independently seeded. gp_restarts : int n_restarts_optimizer of the GP kernel-hyperparameter fit

Source code in lute/tasks/_bayfai.py
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
@ignore_warnings(category=ConvergenceWarning)
def bayes_opt_rank(
    self,
    value,
    center,
    bounds,
    res,
    n_samples,
    n_iterations,
    Imin,
    max_rings,
    pts_per_deg,
    beta=1.96,
    prior=True,
    step=5,
    seed=None,
    n_cand=None,
    gp_restarts=10,
):
    """
    Run Bayesian Optimization on this MPI rank's search space.

    Parameters
    ----------
    value : float, optional
        Shifted center for the parallelized parameter on this MPI rank,
        or None if no parameter is parallelized.
    center : dict
        Dictionary of center values for each parameter
    bounds : dict
        Dictionary of absolute (low, high) bounds for each parameter
    res : dict
        Dictionary of resolution for each parameter
    n_samples : int
        Number of samples to initialize the Gaussian Process
    n_iterations : int
        Number of iterations of Bayesian Optimization
    Imin : float
        Minimum intensity threshold for identifying Bragg peaks
    max_rings : int
        Maximum number of rings to search for Bragg peaks
    pts_per_deg : float
        Number of Bragg peaks to extract per azimuthal degree
    beta : float
        Exploration-exploitation trade-off parameter for UCB acquisition function
    prior : bool
        Whether to sample initial points around the center or randomly
    step : int
        Step size for the gradient descent refinement
    seed : optional, int
        Random seed for reproducibility. Offset by this rank's index so
        that every rank is independently seeded.
    gp_restarts : int
        n_restarts_optimizer of the GP kernel-hyperparameter fit
    """
    if seed is not None:
        np.random.seed(seed + self.rank)

    # 1. Create the search space
    X, X_norm = self.create_search_space(value, center, bounds, res)
    center[self.parallelized] = value

    # 2. Sample initial points
    X_samples, X_norm_samples = self.sample_initial_points(
        X, X_norm, center, bounds, n_samples, prior
    )

    # 3. Evaluate the initial points
    bo_history = {"params": [], "scores": []}
    y = np.zeros((n_samples))
    for i in range(n_samples):
        y[i] = self.score(X_samples[i], Imin, max_rings, pts_per_deg)
        bo_history["params"].append(X_samples[i])
        bo_history["scores"].append(y[i])

    if np.all(y == 0.0):
        result = {
            "bo_history": bo_history,
            "params": np.array(
                [
                    center[k]
                    for k in ("dist", "poni1", "poni2", "rot1", "rot2", "rot3")
                ]
            ),
            "score": 0.0,
            "sigma": [np.inf] * 5,
            "penalty": 0.0,
            "best_idx": 0,
        }
        if self.rank == 0:
            logger.warning(
                "Skipping Bayesian Optimization because all initial scores are zero."
            )
            logger.warning(
                "Initial geometry guess is too far from optimum. Please refine the search space."
            )
        return result

    if np.std(y) != 0:
        y_norm = (y - np.mean(y)) / np.std(y)
    else:
        y_norm = y - np.mean(y)

    # 4. Initialize the Gaussian Process model
    kernel = _Matern(
        length_scale=0.3, length_scale_bounds=(0.2, 0.4), nu=2.5
    ) * _ConstantKernel(
        constant_value=1.0, constant_value_bounds=(0.5, 1.5)
    ) + _WhiteKernel(
        noise_level=0.001, noise_level_bounds="fixed"
    )
    gp_model = GaussianProcessRegressor(
        kernel=kernel, n_restarts_optimizer=gp_restarts, random_state=0
    )
    gp_model.fit(X_norm_samples, y_norm)
    visited_idx = list([])

    # 5. Run the Bayesian Optimization loop
    for i in range(n_iterations):
        # 6. Select the next point to evaluate
        next = self.UCB(X_norm, gp_model, visited_idx, beta, n_cand)
        next_sample = X[next]
        visited_idx.append(next)

        # 7. Compute the score of the next point
        score = self.score(next_sample, Imin, max_rings, pts_per_deg)
        y = np.append(y, [score], axis=0)
        bo_history["params"].append(next_sample)
        bo_history["scores"].append(score)
        X_samples = np.append(X_samples, [X[next]], axis=0)
        X_norm_samples = np.append(X_norm_samples, [X_norm[next]], axis=0)
        if np.std(y) != 0:
            y_norm = (y - np.mean(y)) / np.std(y)
        else:
            y_norm = y - np.mean(y)

        # 8. Update the Gaussian Process model
        if i < n_iterations - 1:  # the final refit is never read
            gp_model.fit(X_norm_samples, y_norm)

    # 9. Gather results
    best_idx = np.argmax(y)
    best_param = X_samples[best_idx]
    data = self.extract_data(best_param, Imin, max_rings, pts_per_deg)
    score, sigma, penalty, params = self.gradient_descent(
        best_param, data, res, step
    )
    logger.info(f"Rank {self.rank}: score={score:3e}, penalty={penalty:3e}")
    result = {
        "bo_history": bo_history,
        "params": params,
        "score": score,
        "sigma": sigma,
        "penalty": penalty,
        "best_idx": best_idx,
    }
    return result

bayfai_opt(center, bounds, res, n_samples, n_iterations, Imin, max_rings, pts_per_deg, beta=1.96, prior=True, step=5, lbda=0.1, seed=None, n_cand=None, dist_spacing='geometric', gp_restarts=10, min_points=None)

Run BayFAI optimization. Distribute BO across MPI ranks for the parallelized parameter. Run Bayesian Optimization on each rank with fixed search space. Perform pyFAI least-squares refinement for each rank's best geometry. Optimal geometry is chosen based on the lowest residual among ranks.

Parameters

center : dict Dictionary of center values for each parameter bounds : dict Dictionary of absolute (low, high) bounds for each parameter res : dict Dictionary of resolution for each parameter n_samples : int Number of samples to initialize the Gaussian Process n_iterations : int Number of iterations of Bayesian Optimization Imin : float Minimum intensity threshold for identifying Bragg peaks max_rings : int Maximum number of rings to consider pts_per_deg: float Number of Bragg peaks to extract per azimuthal degree beta : float Exploration-exploitation trade-off parameter for UCB acquisition function step : int Size of the refinement space around best parameters prior : bool Whether to sample initial points around the center or randomly seed : optional, int Random seed for reproducibility n_cand : optional, int Number of random grid candidates scored by UCB per step (None/0 = whole grid) dist_spacing : str 'geometric' (default) or 'linear' split of bounds['dist'] across ranks when parallelized == 'dist' gp_restarts : int n_restarts_optimizer of the GP kernel-hyperparameter fit min_points : int, optional Geometries with fewer extracted control points score 0 (0 = off; None = 100 when parallelized == 'dist', else 0)

Source code in lute/tasks/_bayfai.py
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
def bayfai_opt(
    self,
    center,
    bounds,
    res,
    n_samples,
    n_iterations,
    Imin,
    max_rings,
    pts_per_deg,
    beta=1.96,
    prior=True,
    step=5,
    lbda=0.1,
    seed=None,
    n_cand=None,
    dist_spacing="geometric",
    gp_restarts=10,
    min_points=None,
):
    """
    Run BayFAI optimization.
    Distribute BO across MPI ranks for the parallelized parameter.
    Run Bayesian Optimization on each rank with fixed search space.
    Perform pyFAI least-squares refinement for each rank's best geometry.
    Optimal geometry is chosen based on the lowest residual among ranks.

    Parameters
    ----------
    center : dict
        Dictionary of center values for each parameter
    bounds : dict
        Dictionary of absolute (low, high) bounds for each parameter
    res : dict
        Dictionary of resolution for each parameter
    n_samples : int
        Number of samples to initialize the Gaussian Process
    n_iterations : int
        Number of iterations of Bayesian Optimization
    Imin : float
        Minimum intensity threshold for identifying Bragg peaks
    max_rings : int
        Maximum number of rings to consider
    pts_per_deg: float
        Number of Bragg peaks to extract per azimuthal degree
    beta : float
        Exploration-exploitation trade-off parameter for UCB acquisition function
    step : int
        Size of the refinement space around best parameters
    prior : bool
        Whether to sample initial points around the center or randomly
    seed : optional, int
        Random seed for reproducibility
    n_cand : optional, int
        Number of random grid candidates scored by UCB per step (None/0 = whole grid)
    dist_spacing : str
        'geometric' (default) or 'linear' split of bounds['dist'] across ranks when
        parallelized == 'dist'
    gp_restarts : int
        n_restarts_optimizer of the GP kernel-hyperparameter fit
    min_points : int, optional
        Geometries with fewer extracted control points score 0 (0 = off; None = 100
        when parallelized == 'dist', else 0)
    """
    # Distance sweeps reach geometries where a few stray points fit; MEC boxes do not.
    if min_points is None:
        min_points = 100 if self.parallelized == "dist" else 0
    self.min_points = min_points
    # Split the parallelized parameter's absolute bounds into one window per MPI rank
    res = dict(res)
    value = self.distribute_parallelized(bounds, res, dist_spacing)
    if self.rank == 0:
        if self.parallelized is not None:
            logger.info(
                f"Running BayFAI parallelizing {self.size} ranks over '{self.parallelized}'"
            )
        else:
            logger.info(f"Running BayFAI on {self.size} ranks")

    bayfai_hyperparams = {
        "n_samples": n_samples,
        "n_iterations": n_iterations,
        "Imin": Imin,
        "max_rings": max_rings,
        "pts_per_deg": pts_per_deg,
        "beta": beta,
        "step": step,
        "prior": prior,
        "seed": seed,
        "n_cand": n_cand,
        "gp_restarts": gp_restarts,
    }

    # Run BO on this rank's search space
    results = self.bayes_opt_rank(
        value,
        center,
        bounds,
        res,
        **bayfai_hyperparams,
    )

    if self.rank == 0:
        # Rank 0 mostly waits at the Barrier for slower ranks: hide its plotting imports there.
        # They leave numpy's global RNG state untouched, so later pyFAI draws are unchanged.
        import matplotlib.pyplot  # type: ignore # noqa: F401
        import bokeh.plotting  # type: ignore # noqa: F401
        import panel  # type: ignore # noqa: F401

    # Gather BayFAI results from all ranks
    self.comm.Barrier()
    self.scan = {}
    self.scan["bo_history"] = self.comm.gather(results["bo_history"], root=0)
    self.scan["params"] = self.comm.gather(results["params"], root=0)
    self.scan["score"] = self.comm.gather(results["score"], root=0)
    self.scan["sigma"] = self.comm.gather(results["sigma"], root=0)
    self.scan["penalty"] = self.comm.gather(results["penalty"], root=0)
    self.scan["best_idx"] = self.comm.gather(results["best_idx"], root=0)

    # Winner selection
    if self.rank == 0:
        for key in self.scan.keys():
            self.scan[key] = np.array([item for item in self.scan[key]])

        self.final_score = self.scan["score"] + lbda * self.scan["penalty"]
        self.index = np.argmax(self.final_score)
        self.bo_history = self.scan["bo_history"][self.index]
        self.params = self.scan["params"][self.index]
        self.neglog_score = self.scan["score"][self.index]
        self.sigma = self.scan["sigma"][self.index]
        self.penalty = self.scan["penalty"][self.index]
        self.best_idx = self.scan["best_idx"][self.index]
        self.gr = GeometryRefinement(
            calibrant=self.calibrant,
            dist=self.params[0],
            poni1=self.params[1],
            poni2=self.params[2],
            rot1=self.params[3],
            rot2=self.params[4],
            rot3=self.params[5],
            detector=self.detector,
            wavelength=self.calibrant.wavelength,
        )
        # One integrator for all rank-0 plots: its 2theta/q arrays and integration engines are cached.
        self.ai = _build_ai(self.detector, self.params, self.calibrant.wavelength)

build_detector(detname)

Read the metrology data and build a pyFAI detector object.

Parameters

detname : str Name of the detector

Returns

pyFAI.Detector Configured pyFAI detector object

Source code in lute/tasks/_bayfai.py
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
def build_detector(self, detname: str) -> pyFAI.detectors.Detector:
    """
    Read the metrology data and build a pyFAI detector object.

    Parameters
    ----------
    detname : str
        Name of the detector

    Returns
    -------
    pyFAI.Detector
        Configured pyFAI detector object
    """
    metrology = get_geometry(detname)
    detector = PsanaToPyFAI.convert(in_file=metrology, detname=detname)
    return detector

create_diagnostics_panel(plot='')

Create a diagnostics panel with the results of the Bayesian Optimization.

Parameters

plot : str Path to save plot

Source code in lute/tasks/_bayfai.py
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
def create_diagnostics_panel(
    self,
    plot="",
):
    """
    Create a diagnostics panel with the results of the Bayesian Optimization.

    Parameters
    ----------
    plot : str
        Path to save plot
    """
    import matplotlib.pyplot as plt  # type: ignore
    import matplotlib.patches as patches  # type: ignore

    fig = plt.figure(figsize=(8, 9), dpi=100)
    nrow, ncol = 3, 2

    # (0,0) Summary text
    ax1 = plt.subplot2grid((nrow, ncol), (0, 0))
    rect = patches.Rectangle(
        (0, 0),
        1,
        1,
        transform=ax1.transAxes,
        color="lightgrey",
        alpha=0.3,
    )
    ax1.add_patch(rect)
    ax1.text(
        0.05,
        0.9,
        f"Experiment {self.exp}",
        ha="left",
        va="center",
        fontsize=8,
    )
    ax1.text(0.05, 0.8, f"Run {self.run}", ha="left", va="center", fontsize=8)
    ax1.text(
        0.05,
        0.7,
        f"Detector {self.detector.detname}",
        ha="left",
        va="center",
        fontsize=8,
    )
    ax1.text(
        0.05,
        0.6,
        f"Calibrant {self.calibrant_name}",
        ha="left",
        va="center",
        fontsize=8,
    )
    ax1.text(
        0.05,
        0.5,
        f"Distance = {1000 * self.params[0]:.3f} ± {1000 * self.sigma[0]:.3f} mm",
        ha="left",
        va="center",
        fontsize=8,
    )
    ax1.text(
        0.05,
        0.4,
        f"X-shift = {1000 * self.params[1]:.3f} ± {1000 * self.sigma[1]:.3f} mm",
        ha="left",
        va="center",
        fontsize=8,
    )
    ax1.text(
        0.05,
        0.3,
        f"Y-shift = {1000 * self.params[2]:.3f} ± {1000 * self.sigma[2]:.3f} mm",
        ha="left",
        va="center",
        fontsize=8,
    )
    ax1.text(
        0.05,
        0.2,
        f"RotX = {self.params[3]:.3f} ± {self.sigma[3]:.6f} rad",
        ha="left",
        va="center",
        fontsize=8,
    )
    ax1.text(
        0.05,
        0.1,
        f"RotY = {self.params[4]:.3f} ± {self.sigma[4]:.6f} rad",
        ha="left",
        va="center",
        fontsize=8,
    )
    ax1.axis("off")

    # (0,1) Pixel histogram
    ax2 = plt.subplot2grid((nrow, ncol), (0, 1))
    self.plot_intensity_hist(self.powder, self.Imin, ax2)

    # Compute 1D azimuthal integration via pyFAI
    ai = self.ai
    result1d = ai.integrate1d(self.stacked_powder, npt=256, unit="q_A^-1")

    # (1,0) 1D integration, spanning two cols
    ax3 = plt.subplot2grid((nrow, ncol), (1, 0), colspan=2)
    self.plot_radial_integration(result1d, self.calibrant, ax=ax3)

    # (2,0) Score vs distance
    ax4 = plt.subplot2grid((nrow, ncol), (2, 0))
    self.plot_score_distance_scan(ax4)

    # (2,1) Penalized score vs distance
    ax5 = plt.subplot2grid((nrow, ncol), (2, 1))
    self.plot_residual_distance_scan(ax5)

    fig.tight_layout()

    if plot != "":
        fig.savefig(plot, dpi=100)
    return fig

create_interactive_powder()

Create an interactive powder image with calibrated overlapping 2θ rings.

Source code in lute/tasks/_bayfai.py
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
def create_interactive_powder(
    self,
):
    """
    Create an interactive powder image with calibrated overlapping 2θ rings.
    """
    from bokeh.plotting import figure  # type: ignore
    from bokeh.models import LinearColorMapper, HoverTool  # type: ignore
    from bokeh.palettes import Viridis256, Category10  # type: ignore
    from bokeh.models.annotations import Label  # type: ignore

    p1, p2, _ = self.detector.calc_cartesian_positions()
    # d1 = vertical (bottom→top), d2 = horizontal (left→right)
    y = np.reshape(p1, self.detector.calib_shape)
    x = np.reshape(p2, self.detector.calib_shape)
    xmin, xmax = x.min(), x.max()
    ymin, ymax = y.min(), y.max()

    p = figure(
        title=f"Run {self.run} - {self.detector.detname} - {self.calibrant_name}",
        x_axis_label="X-axis (m)",
        y_axis_label="Y-axis (m)",
        width=1200,
        height=1200,
        match_aspect=True,
        x_range=(xmin, xmax),
        y_range=(ymin, ymax),
    )

    vmin, vmax = (
        np.percentile(self.stacked_powder, 5),
        np.percentile(self.stacked_powder, 95),
    )
    color_mapper = LinearColorMapper(palette=Viridis256, low=vmin, high=vmax)

    p.image(
        image=[self.assembled_powder[::-1, :]],
        x=xmin,
        y=ymin,
        dw=(xmax - xmin),
        dh=(ymax - ymin),
        color_mapper=color_mapper,
    )

    ai = self.ai
    tth = np.array(self.calibrant.get_2th())
    ttha = ai.twoThetaArray().reshape(self.detector.calib_shape)
    for i in range(self.detector.n_modules):
        p.contour(
            x=x[i],
            y=y[i],
            z=ttha[i],
            levels=tth,
            line_color="red",
            line_width=3,
            line_dash="dashed",
        )

    if self.gr.data is not None and len(self.gr.data) > 0:
        d1_idx = self.gr.data[:, 0].astype(int)
        d2_idx = self.gr.data[:, 1].astype(int)
        rings = self.gr.data[:, 2].astype(int)
        cp_x = p2[d1_idx, d2_idx]
        cp_y = p1[d1_idx, d2_idx]
        n_rings = len(np.unique(rings))
        palette = Category10[max(3, min(10, n_rings))]
        for ring_id in np.unique(rings):
            mask = rings == ring_id
            p.circle(
                cp_x[mask].tolist(),
                cp_y[mask].tolist(),
                size=10,
                color=palette[ring_id % len(palette)],
                alpha=0.6,
                legend_label=f"Ring {ring_id}",
            )
        p.legend.title = f"Control points (N={len(self.gr.data)})"
        p.legend.label_text_font_size = "8pt"
        p.legend.click_policy = "hide"

    radii = ai.rArray().reshape(self.detector.calib_shape)
    q_array = ai.qArray().reshape(self.detector.calib_shape) / 10  # nm⁻¹ → Å⁻¹
    closest_pixel_index = np.argmin(radii)
    closest_pixel = (
        x.flatten()[closest_pixel_index],
        y.flatten()[closest_pixel_index],
    )
    closest_q = q_array.flatten()[closest_pixel_index]
    closest_resol = 2 * np.pi / closest_q

    furthest_pixel_index = np.argmax(radii)
    furthest_pixel = (
        x.flatten()[furthest_pixel_index],
        y.flatten()[furthest_pixel_index],
    )
    furthest_q = q_array.flatten()[furthest_pixel_index]
    furthest_resol = 2 * np.pi / furthest_q

    pixel_lvls = np.array([closest_pixel, furthest_pixel])
    resol_lvls = np.array([closest_resol, furthest_resol])
    for pixel, resol in zip(pixel_lvls, resol_lvls):
        label_annotation = Label(
            x=pixel[0],
            y=pixel[1],
            text=f"{resol:.3f} Å",
            text_color="red",
            text_font_size="16pt",
        )
        p.add_layout(label_annotation)

    hover = HoverTool(
        tooltips=[
            ("x", "@x{0.000}"),
            ("y", "@y{0.000}"),
            ("Intensity", "@intensity{0.0}"),
        ]
    )
    p.add_tools(hover)
    p.title.text_font_size = "12pt"
    p.xaxis.axis_label_text_font_size = "10pt"
    p.yaxis.axis_label_text_font_size = "10pt"
    p.xaxis.major_label_text_font_size = "8pt"
    p.yaxis.major_label_text_font_size = "8pt"

    qs = {
        "closest": closest_q,
        "furthest": furthest_q,
    }
    resolutions = {
        "closest": closest_resol,
        "furthest": furthest_resol,
    }
    return p, qs, resolutions

create_search_space(value, center, bounds, res)

Discretize the search space for the free parameters.

Parameters

value : float, optional Window center of the parallelized parameter on this MPI rank center : dict Center values for each parameter bounds : dict Absolute bounds for each parameter, format: {param: (low, high)} res : dict Resolution per parameter

Returns

X : np.ndarray Full 6D geometry space (cartesian product) X_norm : np.ndarray Normalized search space (between-1 and 1)

Source code in lute/tasks/_bayfai.py
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
def create_search_space(self, value, center, bounds, res):
    """
    Discretize the search space for the free parameters.

    Parameters
    ----------
    value : float, optional
        Window center of the parallelized parameter on this MPI rank
    center : dict
        Center values for each parameter
    bounds : dict
        Absolute bounds for each parameter, format: {param: (low, high)}
    res : dict
        Resolution per parameter

    Returns
    -------
    X : np.ndarray
        Full 6D geometry space (cartesian product)
    X_norm : np.ndarray
        Normalized search space (between-1 and 1)
    """
    if self.parallelized is not None and value is not None:
        center[self.parallelized] = value
    full_params = {}
    search_params = {}
    for p in self.order:
        if p == self.parallelized and value is not None:
            full_params[p] = self.window
            search_params[p] = full_params[p]
        elif p in self.space:
            low, high = bounds[p]
            if high < low:
                low, high = high, low
            step = res[p]
            full_params[p] = np.arange(low, high + step, step)
            search_params[p] = full_params[p]
        else:
            full_params[p] = np.array([center[p]])

    X = np.array(np.meshgrid(*[full_params[p] for p in self.order])).T.reshape(
        -1, len(self.order)
    )
    X_search = np.array(
        np.meshgrid(*[search_params[p] for p in self.space])
    ).T.reshape(-1, len(self.space))
    self.mins = np.min(X_search, axis=0)
    self.maxs = np.max(X_search, axis=0)
    X_norm = 2 * (X_search - self.mins) / (self.maxs - self.mins) - 1
    return X, X_norm

create_summary_plot(plot='')

Create a summary plot with the results of the Bayesian Optimization.

Parameters

plot : str Path to save plot

Source code in lute/tasks/_bayfai.py
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
def create_summary_plot(
    self,
    plot="",
):
    """
    Create a summary plot with the results of the Bayesian Optimization.

    Parameters
    ----------
    plot : str
        Path to save plot
    """
    import matplotlib.pyplot as plt  # type: ignore
    import matplotlib.patches as patches  # type: ignore

    fig = plt.figure(figsize=(9, 12), dpi=100)
    nrow, ncol = 4, 3

    # Labelling experiment and run number
    ax1 = plt.subplot2grid((nrow, ncol), (0, 0))
    rect = patches.Rectangle(
        (0, 0),
        1,
        1,
        transform=ax1.transAxes,
        color="lightgrey",
        alpha=0.3,
    )
    ax1.add_patch(rect)
    ax1.text(
        0.05,
        0.9,
        f"Experiment {self.exp}",
        ha="left",
        va="center",
        fontsize=8,
    )
    ax1.text(0.05, 0.8, f"Run {self.run}", ha="left", va="center", fontsize=8)
    ax1.text(
        0.05,
        0.7,
        f"Detector {self.detector.detname}",
        ha="left",
        va="center",
        fontsize=8,
    )
    ax1.text(
        0.05,
        0.6,
        f"Calibrant {self.calibrant_name}",
        ha="left",
        va="center",
        fontsize=8,
    )
    ax1.text(
        0.05,
        0.5,
        f"Distance = {1000 * self.params[0]:.3f} ± {1000 * self.sigma[0]:.3f} mm",
        ha="left",
        va="center",
        fontsize=8,
    )
    ax1.text(
        0.05,
        0.4,
        f"ShiftX = {1000 * self.params[1]:.3f} ± {1000 * self.sigma[1]:.3f} mm",
        ha="left",
        va="center",
        fontsize=8,
    )
    ax1.text(
        0.05,
        0.3,
        f"ShiftY = {1000 * self.params[2]:.3f} ± {1000 * self.sigma[2]:.3f} mm",
        ha="left",
        va="center",
        fontsize=8,
    )
    ax1.text(
        0.05,
        0.2,
        f"RotX = {self.params[3]:.3f} ± {self.sigma[3]:.6f} rad",
        ha="left",
        va="center",
        fontsize=8,
    )
    ax1.text(
        0.05,
        0.1,
        f"RotY = {self.params[4]:.3f} ± {self.sigma[4]:.6f} rad",
        ha="left",
        va="center",
        fontsize=8,
    )
    ax1.axis("off")

    # Compute azimuthal integrations via pyFAI
    ai = self.ai
    q_max = float(
        (ai.qArray().reshape(self.detector.calib_shape) / 10).max()
    )  # nm⁻¹ → Å⁻¹, detector-limited q maximum
    result1d = ai.integrate1d(
        self.stacked_powder, npt=256, unit="q_A^-1", radial_range=(0, q_max)
    )
    result2d = ai.integrate2d(
        self.stacked_powder,
        npt_rad=256,
        npt_azim=360,
        unit="q_A^-1",
        radial_range=(0, q_max),
    )

    # (0,1) Pixel histogram, spanning two cols
    ax2 = plt.subplot2grid((nrow, ncol), (0, 1), colspan=2)
    self.plot_intensity_hist(self.powder, self.Imin, ax2)

    # (1,0) Powder rings overlay, spanning two cols and two rows
    ax3 = plt.subplot2grid((nrow, ncol), (1, 0), rowspan=2, colspan=2)
    self.plot_powder_and_resolution(ax=ax3)

    # (1,2) Score vs distance
    ax4 = plt.subplot2grid((nrow, ncol), (1, 2))
    self.plot_score_distance_scan(ax4)

    # (2,2) Penalized score vs distance
    ax5 = plt.subplot2grid((nrow, ncol), (2, 2))
    self.plot_residual_distance_scan(ax5)

    # (3,0) 1D integration, spanning two cols
    ax6 = plt.subplot2grid((nrow, ncol), (3, 0), colspan=2)
    self.plot_radial_integration(result1d, self.calibrant, ax=ax6)

    # (3,2) 2D integration
    ax7 = plt.subplot2grid((nrow, ncol), (3, 2))
    self.plot_2d_integration(result2d, self.calibrant, ax7)

    fig.tight_layout()

    if plot != "":
        fig.savefig(plot, dpi=100)
    return fig

define_calibrant(calibrant_name, h5, wavelength=1e-10)

Define calibrant for optimization with appropriate wavelength.

Parameters

calibrant_name : str Name of the calibrant h5 : str Path to the smalldata h5 file containing the photon energy. wavelength : float, optional X-ray wavelength in meters.

Source code in lute/tasks/_bayfai.py
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
def define_calibrant(
    self,
    calibrant_name: str,
    h5: str,
    wavelength: float = 1e-10,
) -> pyFAI.calibrant.Calibrant:
    """
    Define calibrant for optimization with appropriate wavelength.

    Parameters
    ----------
    calibrant_name : str
        Name of the calibrant
    h5 : str
        Path to the smalldata h5 file containing the photon energy.
    wavelength : float, optional
        X-ray wavelength in meters.
    """
    self.calibrant_name = calibrant_name
    calibrant = CALIBRANT_FACTORY(calibrant_name)
    if wavelength != 1e-10:
        logger.info(f"Using user-provided wavelength {wavelength} m")
    else:
        try:
            with h5py.File(h5) as f:
                for key in PHOTON_ENERGY_KEYS:
                    if key not in f:
                        continue
                    energies = np.asarray(f[key][()], dtype=float)
                    energies = energies[np.isfinite(energies) & (energies > 0)]
                    if energies.size == 0:
                        continue
                    photon_energy = float(np.mean(energies))
                    wavelength = 1.23984193e-6 / photon_energy
                    logger.info(
                        f"Read {photon_energy:.2f} eV from {key} "
                        f"-> wavelength {wavelength:.4e} m"
                    )
                    break
                else:
                    raise KeyError(
                        f"None of {PHOTON_ENERGY_KEYS} hold usable photon "
                        f"energies in the h5 file"
                    )
        except Exception as e:
            logger.warning(
                f"Could not read photon energy from {h5} due to {e}, defaulting to provided wavelength {wavelength} m"
            )
    calibrant.wavelength = wavelength
    return calibrant

distribute_parallelized(bounds, res, dist_spacing='geometric')

Split the parallelized parameter's absolute bounds into one window per MPI rank.

dist with dist_spacing 'geometric': rank r gets [b_r, b_r+1] of b = low(high/low)**(arange(size+1)/size), searched on a 2-point grid at the window edges. Otherwise ('linear', or any other parallelized parameter): size equal windows of width w = (high-low)/size, grid step w. Sets res[p] to this rank's window width (refinement range +-stepw).

Parameters

bounds : dict Absolute bounds per parameter, format: {param: (low, high)} res : dict Resolution per parameter (updated in place for the parallelized one) dist_spacing : str 'geometric' or 'linear' split of bounds['dist'] when dist is parallelized

Returns

value : float, optional Center of this rank's window, or None if no parameter is parallelized.

Source code in lute/tasks/_bayfai.py
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
def distribute_parallelized(self, bounds, res, dist_spacing="geometric"):
    """
    Split the parallelized parameter's absolute bounds into one window per MPI rank.

    dist with dist_spacing 'geometric': rank r gets [b_r, b_r+1] of
    b = low*(high/low)**(arange(size+1)/size), searched on a 2-point grid at the
    window edges. Otherwise ('linear', or any other parallelized parameter):
    size equal windows of width w = (high-low)/size, grid step w.
    Sets res[p] to this rank's window width (refinement range +-step*w).

    Parameters
    ----------
    bounds : dict
        Absolute bounds per parameter, format: {param: (low, high)}
    res : dict
        Resolution per parameter (updated in place for the parallelized one)
    dist_spacing : str
        'geometric' or 'linear' split of bounds['dist'] when dist is parallelized

    Returns
    -------
    value : float, optional
        Center of this rank's window, or None if no parameter is parallelized.
    """
    self.param_values = None
    self.window = None
    if not self.parallelized:
        return None
    p = self.parallelized
    lo, hi = sorted(bounds[p])
    n = self.size
    if p == "dist" and dist_spacing == "geometric":
        b = lo * (hi / lo) ** (np.arange(n + 1) / n)
        lo_k, hi_k = b[self.rank], b[self.rank + 1]
        w = hi_k - lo_k
        value = (lo_k + hi_k) / 2
        self.param_values = (b[:-1] + b[1:]) / 2
        self.window = np.array([lo_k, hi_k])
    else:
        # ponytail: rounding to 9 decimals makes w the decimal of the config (e.g. 0.0058), so
        # the windows reproduce the old center+k*res sweep bit for bit.
        w = round((hi - lo) / n, 9)
        self.param_values = np.round(lo + (np.arange(n) + 0.5) * w, 6)
        value = self.param_values[self.rank]
        self.window = np.arange(value - w / 2, value + w / 2 + w, w)
    res[p] = self.window_width = w
    logger.info(
        f"tiling rank={self.rank} {p}={value:.6f} lo={self.window[0]:.6f} "
        f"hi={self.window[-1]:.6f} w={w:.6f}"
    )
    return value

estimate_uncertainty(refinement, rel_eps=0.001, abs_eps=0.0001)

Estimate parameter uncertainties from the Hessian matrix.

Parameters

refinement : GeometryRefinement pyFAI refinement object after refine3. rel_eps : float Relative step for finite differences. abs_eps : float Absolute step for finite differences.

Returns

sigmas : np.ndarray Estimated uncertainties for each parameter is_min : bool True if a local minimum was found

Source code in lute/tasks/_bayfai.py
 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
def estimate_uncertainty(self, refinement, rel_eps=1e-3, abs_eps=1e-4):
    """
    Estimate parameter uncertainties from the Hessian matrix.

    Parameters
    ----------
    refinement : GeometryRefinement
        pyFAI refinement object after refine3.
    rel_eps : float
        Relative step for finite differences.
    abs_eps : float
        Absolute step for finite differences.

    Returns
    -------
    sigmas : np.ndarray
        Estimated uncertainties for each parameter
    is_min : bool
        True if a local minimum was found
    """
    param0 = np.array(
        [
            refinement.dist,
            refinement.poni1,
            refinement.poni2,
            refinement.rot1,
            refinement.rot2,
            refinement.rot3,
        ],
        dtype=np.float64,
    )
    param_names = ["dist", "poni1", "poni2", "rot1", "rot2"]
    size = len(param_names)

    d1 = refinement.data[:, 0]
    d2 = refinement.data[:, 1]
    ring = refinement.data[:, 2].astype(np.int32)
    f_min = refinement.residu2(param0, d1, d2, ring)
    hessian = np.zeros((size, size), dtype=np.float64)
    dof = max(len(refinement.data) - size, 1)

    delta = np.maximum(rel_eps * np.abs(param0), abs_eps)
    for i in range(size):
        deltai = delta[i]
        param = param0.copy()
        param[i] += deltai
        f_plus = refinement.residu2(param, d1, d2, ring)
        param = param0.copy()
        param[i] -= deltai
        f_minus = refinement.residu2(param, d1, d2, ring)
        hessian[i, i] = (f_plus + f_minus - 2.0 * f_min) / (deltai**2)

        for j in range(i + 1, size):
            deltaj = delta[j]
            param = param0.copy()
            param[i] += deltai
            param[j] += deltaj
            f_pp = refinement.residu2(param, d1, d2, ring)
            param = param0.copy()
            param[i] -= deltai
            param[j] -= deltaj
            f_mm = refinement.residu2(param, d1, d2, ring)
            param = param0.copy()
            param[i] += deltai
            param[j] -= deltaj
            f_pm = refinement.residu2(param, d1, d2, ring)
            param = param0.copy()
            param[i] -= deltai
            param[j] += deltaj
            f_mp = refinement.residu2(param, d1, d2, ring)
            hessian[j, i] = hessian[i, j] = (f_pp + f_mm - f_pm - f_mp) / (
                4.0 * deltai * deltaj
            )

    eigs, _ = np.linalg.eigh(hessian)
    if np.any(eigs <= 0):
        sigmas = [np.inf] * size
        penalty = 0.0
        return sigmas, penalty

    cov = np.linalg.inv(hessian)
    sigmas = f_min * np.diag(cov) / dof
    sigmas = np.sqrt(sigmas)
    penalty = -np.log(np.linalg.det(cov))
    return sigmas, penalty

extract_data(sample, Imin, max_rings, pts_per_deg)

Extract data points for a given sampled geometry.

Parameters

sample : list Geometry parameters Imin : float Minimum intensity threshold max_rings : int Maximum number of rings to consider pts_per_deg : int Number of points per degree for extraction

Returns

data : np.ndarray Extracted data points

Source code in lute/tasks/_bayfai.py
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
def extract_data(self, sample, Imin, max_rings, pts_per_deg):
    """
    Extract data points for a given sampled geometry.

    Parameters
    ----------
    sample : list
        Geometry parameters
    Imin : float
        Minimum intensity threshold
    max_rings : int
        Maximum number of rings to consider
    pts_per_deg : int
        Number of points per degree for extraction

    Returns
    -------
    data : np.ndarray
        Extracted data points
    """
    dist, poni1, poni2, rot1, rot2, rot3 = sample
    geom_sample = Geometry(
        dist=dist,
        poni1=poni1,
        poni2=poni2,
        rot1=rot1,
        rot2=rot2,
        rot3=rot3,
        detector=self.detector,
        wavelength=self.calibrant.wavelength,
    )
    sg = _SingleGeometry(
        "Data Extraction Geometry",
        self.stacked_powder,
        calibrant=self.calibrant,
        detector=self.detector,
        geometry=geom_sample,
    )
    self._fast_maps(sg)
    with _fast_extract_cp(max_rings):
        sg.extract_cp(max_rings=max_rings, pts_per_deg=pts_per_deg, Imin=Imin)
    return sg.geometry_refinement.data

extract_powder(powder_path, detname)

Extract a powder image from smalldata analysis.

Parameters

powder_path : str Path to the h5 or npy file containing the powder data.

Returns

powder : npt.NDArray[np.float64] The extracted powder image.

Source code in lute/tasks/_bayfai.py
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
def extract_powder(self, powder_path: str, detname: str) -> npt.NDArray[np.float64]:
    """
    Extract a powder image from smalldata analysis.

    Parameters
    ----------
    powder_path : str
        Path to the h5 or npy file containing the powder data.

    Returns
    -------
    powder : npt.NDArray[np.float64]
        The extracted powder image.
    """
    if powder_path.endswith(".npy"):
        powder = np.load(powder_path)
        return powder
    else:
        with h5py.File(powder_path) as h5:
            try:
                powder = h5[f"Sums/{detname}_calib_max"][()]
            except KeyError:
                logger.warning(
                    f"Cannot find {detname} Max powder in {powder_path}, defaulting to {detname} Sum instead."
                )
                try:
                    powder = h5[f"Sums/{detname}_calib"][()]
                except KeyError:
                    logger.error(
                        f"Cannot find {detname} Sum powder in {powder_path}. Exiting..."
                    )
                    raise
        return powder

generate_powder(powder_path, detname, Imin=95)

Generate a preprocessed powder image from smalldata reduction.

Parameters

powder_path : str Path to the h5 or npy file containing the powder data. detname : str Name of the detector Imin : float, optional Minimum intensity percentile threshold for Bragg peak detection.

Source code in lute/tasks/_bayfai.py
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
def generate_powder(
    self,
    powder_path: str,
    detname: str,
    Imin: float = 95,
) -> npt.NDArray[np.float64]:
    """
    Generate a preprocessed powder image from smalldata reduction.

    Parameters
    ----------
    powder_path : str
        Path to the h5 or npy file containing the powder data.
    detname : str
        Name of the detector
    Imin : float, optional
        Minimum intensity percentile threshold for Bragg peak detection.
    """
    mask = self.detector.geo.get_pixel_mask(mbits=3)
    if mask.shape[0] == 1 and mask.ndim > 3:
        mask = np.squeeze(mask, axis=0)
    powder = self.extract_powder(powder_path, detname)
    powder = self.preprocess_powder(powder, mask, Imin)
    self.assembled_powder = self.assemble_image(powder)
    return powder

gradient_descent(sample, data, resolutions, step=5)

Run gradient descent refinement on the given sample parameters using pyFAI.

Parameters

sample : list Sample parameters for the refinement data : np.ndarray Data used for the refinement resolutions : dict Resolution per parameter for restricted refinement step : int Size of the refinement space around best parameters

Returns

score : float Negative log of the residual after refinement sigma : np.ndarray Estimated uncertainties for each parameter penalty : float Penalty from uncertainty estimation params : list Refined parameters

Source code in lute/tasks/_bayfai.py
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
def gradient_descent(self, sample, data, resolutions, step=5):
    """
    Run gradient descent refinement on the given sample parameters using pyFAI.

    Parameters
    ----------
    sample : list
        Sample parameters for the refinement
    data : np.ndarray
        Data used for the refinement
    resolutions : dict
        Resolution per parameter for restricted refinement
    step : int
        Size of the refinement space around best parameters

    Returns
    -------
    score : float
        Negative log of the residual after refinement
    sigma : np.ndarray
        Estimated uncertainties for each parameter
    penalty : float
        Penalty from uncertainty estimation
    params : list
        Refined parameters
    """
    dist, poni1, poni2, rot1, rot2, rot3 = sample

    if data is None or len(data) == 0 or len(data) < self.min_points:
        return 0.0, [np.inf] * 5, 0.0, sample

    gr = GeometryRefinement(
        data=data,
        calibrant=self.calibrant,
        dist=dist,
        poni1=poni1,
        poni2=poni2,
        rot1=rot1,
        rot2=rot2,
        rot3=rot3,
        detector=self.detector,
        wavelength=self.calibrant.wavelength,
    )
    gr.set_dist_min(dist - step * resolutions["dist"])
    gr.set_dist_max(dist + step * resolutions["dist"])
    gr.set_poni1_min(poni1 - step * resolutions["poni1"])
    gr.set_poni1_max(poni1 + step * resolutions["poni1"])
    gr.set_poni2_min(poni2 - step * resolutions["poni2"])
    gr.set_poni2_max(poni2 + step * resolutions["poni2"])
    gr.set_rot1_min(rot1 - step * resolutions["rot1"])
    gr.set_rot1_max(rot1 + step * resolutions["rot1"])
    gr.set_rot2_min(rot2 - step * resolutions["rot2"])
    gr.set_rot2_max(rot2 + step * resolutions["rot2"])
    fix = ["rot3", "wavelength"]
    score = -np.log(gr.refine3(fix=fix))
    sigma, penalty = self.estimate_uncertainty(gr)
    params = gr.param
    self.gr = gr
    return score, sigma, penalty, params

plot_2d_integration(result2d, calibrant, ax=None)

Plot the 2D azimuthal integration (cake plot) of a powder image.

Parameters

result2d : pyFAI.containers.Integrate2dResult Result from AzimuthalIntegrator.integrate2d, containing .intensity, .radial (q in Å⁻¹), and .azimuthal (χ in °). calibrant : pyFAI.calibrant.Calibrant Calibrant object for Bragg peak overlay. ax : plt.Axes, optional Matplotlib axes.

Source code in lute/tasks/_bayfai.py
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
def plot_2d_integration(self, result2d, calibrant, ax=None):
    """
    Plot the 2D azimuthal integration (cake plot) of a powder image.

    Parameters
    ----------
    result2d : pyFAI.containers.Integrate2dResult
        Result from AzimuthalIntegrator.integrate2d, containing .intensity,
        .radial (q in Å⁻¹), and .azimuthal (χ in °).
    calibrant : pyFAI.calibrant.Calibrant
        Calibrant object for Bragg peak overlay.
    ax : plt.Axes, optional
        Matplotlib axes.
    """
    import matplotlib.pyplot as plt  # type: ignore

    if ax is None:
        _fig, ax = plt.subplots()

    cake = result2d.intensity
    non_zero = cake[cake > 0]
    vmin = np.percentile(non_zero, 5) if len(non_zero) > 0 else 0
    vmax = np.percentile(non_zero, 95) if len(non_zero) > 0 else 1

    ax.imshow(
        cake,
        extent=[
            result2d.radial.min(),
            result2d.radial.max(),
            result2d.azimuthal.min(),
            result2d.azimuthal.max(),
        ],
        aspect="auto",
        origin="lower",
        vmin=vmin,
        vmax=vmax,
        cmap="viridis",
    )

    unit = RADIAL_UNITS["q_A^-1"]
    peaks = calibrant.get_peaks(unit)
    if peaks is not None:
        for q_peak in peaks:
            ax.axvline(
                q_peak, color="red", linestyle="--", linewidth=0.8, alpha=0.7
            )

    ax.set_xlabel(unit.label, fontsize=6)
    ax.set_ylabel(r"$\chi$ (°)", fontsize=6)
    ax.tick_params(axis="x", labelsize=4)
    ax.tick_params(axis="y", labelsize=4)
    ax.set_title("2D Azimuthal Integration", fontsize=6)

plot_bo_history(ax)

Plot the Bayesian Optimization history across all ranks

Parameters

bo_history : list List of all the BO histories with keys 'params' and 'scores' for each rank-distance ax : plt.Axes Matplotlib axes

Source code in lute/tasks/_bayfai.py
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
def plot_bo_history(self, ax):
    """
    Plot the Bayesian Optimization history across all ranks

    Parameters
    ----------
    bo_history : list
        List of all the BO histories with keys 'params' and 'scores' for each rank-distance
    ax : plt.Axes
        Matplotlib axes
    """
    bo_history = self.scan["bo_history"]
    iters = np.arange(len(bo_history[self.index]["scores"]))
    if self.param_values is not None:
        label = f"Best {self.parallelized}: {self.param_values[self.index]:.3f}"
    else:
        label = f"Best rank: {self.index}"
    ax.plot(
        iters,
        bo_history[self.index]["scores"],
        marker="o",
        markersize=3,
        linestyle="--",
        linewidth=0.8,
        color="black",
        markerfacecolor="red",
        markeredgecolor="black",
        label=label,
    )
    ax.legend(fontsize=6)
    ax.set_xlabel("Iteration", fontsize=6)
    ax.set_ylabel("Score", fontsize=6)
    ax.yaxis.get_offset_text().set_fontsize(6)
    ax.tick_params(axis="x", labelsize=6)
    ax.tick_params(axis="y", labelsize=6)
    ax.set_title("Bayesian Optimization History", fontsize=6)

plot_intensity_hist(powder, Imin, ax)

Plot histogram of pixel intensities in the powder image

Parameters

powder : np.ndarray Powder image exp : str Experiment name run : int Run number Imin : float Minimum intensity threshold for identifying Bragg peaks ax : plt.Axes Matplotlib axes

Source code in lute/tasks/_bayfai.py
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
def plot_intensity_hist(self, powder, Imin, ax):
    """
    Plot histogram of pixel intensities in the powder image

    Parameters
    ----------
    powder : np.ndarray
        Powder image
    exp : str
        Experiment name
    run : int
        Run number
    Imin : float
        Minimum intensity threshold for identifying Bragg peaks
    ax : plt.Axes
        Matplotlib axes
    """
    mean = np.mean(powder)
    std_dev = np.std(powder)
    nice_pix = powder[np.where(powder < mean + 2 * std_dev)]
    _ = ax.hist(
        nice_pix.ravel(),
        bins=100,
        color="skyblue",
        edgecolor="black",
        alpha=0.7,
        label="Pixel Intensities",
    )
    ax.axvline(
        mean,
        color="red",
        linestyle="--",
        label=f"Mean ({mean:.2f})",
    )
    ax.axvline(
        mean + std_dev,
        color="orange",
        linestyle="--",
        label=f"Mean + Std Dev ({mean + std_dev:.2f})",
    )
    ax.axvline(
        mean + 2 * std_dev,
        color="green",
        linestyle="--",
        label=f"Mean + 2 Std Dev ({mean + 2 * std_dev:.2f})",
    )
    ax.axvline(
        Imin,
        color="purple",
        linestyle=":",
        linewidth=2,
        label=f"Minimum Intensity ({Imin:.2f})",
    )
    ax.set_xlim([0, mean + 2 * std_dev])
    ax.set_xlabel("Pixel Intensity", fontsize=6)
    ax.set_ylabel("Frequency", fontsize=6)
    ax.set_xticks([])
    ax.set_xticklabels([])
    ax.tick_params(axis="y", labelsize=4)
    ax.set_title(
        f"Histogram of Pixel Intensities \n for {self.exp} run {self.run}",
        fontsize=6,
    )
    ax.legend(fontsize=6)

plot_powder_and_resolution(ax=None)

Plot the powder image with calibrated overlapping 2θ rings.

Parameters

ax : plt.Axes, optional Matplotlib axes

Source code in lute/tasks/_bayfai.py
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
def plot_powder_and_resolution(self, ax=None):
    """
    Plot the powder image with calibrated overlapping 2θ rings.

    Parameters
    ----------
    ax : plt.Axes, optional
        Matplotlib axes
    """
    import matplotlib.pyplot as plt  # type: ignore

    if ax is None:
        _fig, ax = plt.subplots()

    # Physical coordinates powder plot
    p1, p2, _ = self.detector.calc_cartesian_positions()

    ax.imshow(
        self.assembled_powder,
        extent=[p2.min(), p2.max(), p1.min(), p1.max()],
        vmin=np.percentile(self.powder, 5),
        vmax=np.percentile(self.powder, 95),
    )

    # Ring overlay
    ai = self.ai
    tth = np.array(self.calibrant.get_2th())
    ttha = ai.twoThetaArray().reshape(self.detector.calib_shape)
    p1_3d = p1.reshape(self.detector.calib_shape)
    p2_3d = p2.reshape(self.detector.calib_shape)
    for m in range(self.detector.n_modules):
        ax.contour(
            p2_3d[m],
            p1_3d[m],
            ttha[m],
            levels=tth,
            cmap="autumn",
            linewidths=1,
            linestyles="dashed",
        )

    # Control points overlay
    if self.gr.data is not None and len(self.gr.data) > 0:
        d1_idx = self.gr.data[:, 0].astype(int)
        d2_idx = self.gr.data[:, 1].astype(int)
        rings = self.gr.data[:, 2].astype(int)
        cp_x = p2[d1_idx, d2_idx]
        cp_y = p1[d1_idx, d2_idx]
        cmap = plt.get_cmap("tab10")
        for ring_id in np.unique(rings):
            mask = rings == ring_id
            ax.scatter(
                cp_x[mask],
                cp_y[mask],
                s=10,
                color=cmap(ring_id % 10),
                alpha=0.6,
                label=f"Ring {ring_id}",
                zorder=3,
            )
        ax.legend(
            fontsize=5,
            markerscale=2,
            title=f"N={len(self.gr.data)}",
            title_fontsize=5,
        )

    # Resolution overlay
    radii = ai.rArray()
    q_array = ai.qArray() / 10  # nm⁻¹ → Å⁻¹

    d1c, d2c = np.unravel_index(np.argmin(radii), p1.shape)
    closest_resol = 2 * np.pi / q_array[d1c, d2c]
    ax.text(
        p2[d1c, d2c],
        p1[d1c, d2c],
        f"{closest_resol:.3f} Å",
        color="red",
        fontsize=10,
        bbox=dict(facecolor="white", alpha=0.6, edgecolor="none", pad=1),
    )

    d1f, d2f = np.unravel_index(np.argmax(radii), p1.shape)
    furthest_resol = 2 * np.pi / q_array[d1f, d2f]
    ax.text(
        p2[d1f, d2f],
        p1[d1f, d2f],
        f"{furthest_resol:.3f} Å",
        color="red",
        fontsize=10,
        bbox=dict(facecolor="white", alpha=0.6, edgecolor="none", pad=1),
    )

    ax.set_xlabel("X-axis (m)", fontsize=8)
    ax.set_ylabel("Y-axis (m)", fontsize=8)
    ax.tick_params(axis="x", labelsize=6)
    ax.tick_params(axis="y", labelsize=6)
    ax.set_title(
        f"Run {self.run} - {self.detector.detname} - {self.calibrant_name}",
        fontsize=8,
    )
    ax.set_aspect("equal")

plot_radial_integration(result1d, calibrant, ax=None)

Plot the radial integration of a powder image

Parameters

result1d : pyFAI.containers.Integrate1dResult Result from AzimuthalIntegrator.integrate1d, containing .radial (q in Å⁻¹) and .intensity. calibrant : Calibrant Calibrant object ax : plt.Axes Matplotlib axes

Source code in lute/tasks/_bayfai.py
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
def plot_radial_integration(self, result1d, calibrant, ax=None):
    """
    Plot the radial integration of a powder image

    Parameters
    ----------
    result1d : pyFAI.containers.Integrate1dResult
        Result from AzimuthalIntegrator.integrate1d, containing .radial (q in Å⁻¹) and .intensity.
    calibrant : Calibrant
        Calibrant object
    ax : plt.Axes
        Matplotlib axes
    """
    import matplotlib.pyplot as plt  # type: ignore
    from matplotlib import lines  # type: ignore

    if ax is None:
        fig, ax = plt.subplots()

    unit = RADIAL_UNITS["q_A^-1"]
    ax.plot(result1d.radial, result1d.intensity, color="black", linewidth=0.8)

    x_values = calibrant.get_peaks(unit)
    if x_values is not None:
        for x in x_values:
            line = lines.Line2D(
                [x, x],
                ax.axis()[2:4],
                color="red",
                linestyle="--",
                linewidth=0.8,
                alpha=0.7,
            )
            ax.add_line(line)

    ax.set_title("Radial Profile", fontsize=6)
    if unit:
        ax.set_xlabel(unit.label, fontsize=6)
    ax.set_ylabel("Intensity", fontsize=6)
    ax.tick_params(axis="x", labelsize=4)
    ax.tick_params(axis="y", labelsize=4)

plot_residual_distance_scan(ax)

Plot the residual scan over distance

Parameters

ax : plt.Axes Matplotlib axes

Source code in lute/tasks/_bayfai.py
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
def plot_residual_distance_scan(self, ax):
    """
    Plot the residual scan over distance

    Parameters
    ----------
    ax : plt.Axes
        Matplotlib axes
    """
    x = self.param_values if self.param_values is not None else np.arange(self.size)
    xlabel = self.parallelized if self.parallelized is not None else "Rank"
    ax.plot(x, self.final_score, linewidth=0.8, color="k")
    ax.scatter(
        x[self.index],
        self.final_score[self.index],
        color="red",
        s=50,
        marker="*",
    )
    ax.set_xlabel(xlabel, fontsize=6)
    ax.set_ylabel(
        r"$-\log\left(\frac{1}{N}\sum (2\theta_g - 2\theta_c)^2\right)$", fontsize=6
    )
    ax.yaxis.get_offset_text().set_fontsize(6)
    ax.tick_params(axis="x", labelsize=6)
    ax.tick_params(axis="y", labelsize=6)
    ax.set_title(
        f"Penalized Score vs {xlabel}",
        fontsize=6,
    )

plot_score_distance_scan(ax)

Plot the score scan over distance

Parameters

ax : plt.Axes Matplotlib axes

Source code in lute/tasks/_bayfai.py
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
def plot_score_distance_scan(self, ax):
    """
    Plot the score scan over distance

    Parameters
    ----------
    ax : plt.Axes
        Matplotlib axes
    """
    x = self.param_values if self.param_values is not None else np.arange(self.size)
    xlabel = self.parallelized if self.parallelized is not None else "Rank"
    ax.plot(x, self.scan["score"], linewidth=0.8, color="k")
    ax.set_xlabel(xlabel, fontsize=6)
    ax.set_ylabel(
        r"$-\log\left(\frac{1}{N}\sum (2\theta_g - 2\theta_c)^2\right)$", fontsize=6
    )
    ax.yaxis.get_offset_text().set_fontsize(6)
    ax.tick_params(axis="x", labelsize=6)
    ax.tick_params(axis="y", labelsize=6)
    ax.set_title(
        f"Score vs {xlabel}",
        fontsize=6,
    )

preprocess_powder(powder, mask, Imin=95)

Preprocess extracted powder for enhancing optimization

Parameters

powder : npt.NDArray[np.float64] Powder image to use for calibration mask : npt.NDArray[np.integer] Pixel mask to apply to the powder image Imin : float, optional Minimum intensity percentile threshold for Bragg peak detection.

Source code in lute/tasks/_bayfai.py
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
def preprocess_powder(
    self,
    powder: npt.NDArray[np.float64],
    mask: npt.NDArray[np.integer],
    Imin: float = 95,
) -> npt.NDArray[np.float64]:
    """
    Preprocess extracted powder for enhancing optimization

    Parameters
    ----------
    powder : npt.NDArray[np.float64]
        Powder image to use for calibration
    mask : npt.NDArray[np.integer]
        Pixel mask to apply to the powder image
    Imin : float, optional
        Minimum intensity percentile threshold for Bragg peak detection.
    """
    powder = np.asarray(powder, dtype=np.float64).copy()
    good = mask != 0
    powder[~good] = 0
    powder[powder < 0] = 0
    self.powder = powder
    self.stacked_powder = np.reshape(self.powder, self.detector.shape)
    non_zero_pixels = self.powder[self.powder > 0]
    self.Imin = np.percentile(non_zero_pixels, Imin)
    return powder

sample_initial_points(X, X_norm, center, bounds, n_samples, prior)

Sample initial points from the search space.

Parameters

X : np.ndarray Search space X_norm : np.ndarray Normalized search space center : dict Center values for each parameter bounds : dict Bounds for each parameter n_samples : int Number of samples to draw prior : bool Use prior information for sampling

Returns

np.ndarray Sampled points

Source code in lute/tasks/_bayfai.py
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
def sample_initial_points(self, X, X_norm, center, bounds, n_samples, prior):
    """
    Sample initial points from the search space.

    Parameters
    ----------
    X : np.ndarray
        Search space
    X_norm : np.ndarray
        Normalized search space
    center : dict
        Center values for each parameter
    bounds : dict
        Bounds for each parameter
    n_samples : int
        Number of samples to draw
    prior : bool
        Use prior information for sampling

    Returns
    -------
    np.ndarray
        Sampled points
    """
    if prior:
        means = [center[p] for p in self.space]
        # ponytail: builtin round(), not np.round: c+b - (c-b) differs from 2b in the last bit for
        # most centers (0.28 - 0.26 != 0.02), and the multivariate_normal draws would change.
        widths = [
            (
                self.window_width
                if p == self.parallelized
                else round(float(abs(bounds[p][1] - bounds[p][0])), 12)
            )
            for p in self.space
        ]
        cov = np.diag([(wd / 5) ** 2 for wd in widths])
        X_free = np.random.multivariate_normal(means, cov, n_samples)
        X_free = np.clip(X_free, self.mins, self.maxs)
        X_norm_samples = 2 * (X_free - self.mins) / (self.maxs - self.mins) - 1
        X_samples = np.tile([center[p] for p in self.order], (n_samples, 1))
        for i, p in enumerate(self.space):
            j = self.order.index(p)
            X_samples[:, j] = X_free[:, i]
        return X_samples, X_norm_samples
    else:
        idx_samples = np.random.choice(X.shape[0], n_samples)
        X_samples = X[idx_samples]
        X_norm_samples = X_norm[idx_samples]
        return X_samples, X_norm_samples

score(sample, Imin, max_rings, pts_per_deg)

Evaluate score at a given sampled geometry based on the residual between predicted and observed Bragg peak positions.

Parameters

sample : list Geometry parameters Imin : float Minimum intensity threshold max_rings : int Maximum number of rings to consider

Returns

score : float Scalar score for Bayesian optimization

Source code in lute/tasks/_bayfai.py
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
def score(self, sample, Imin, max_rings, pts_per_deg):
    """
    Evaluate score at a given sampled geometry based on the residual between predicted and observed Bragg peak positions.

    Parameters
    ----------
    sample : list
        Geometry parameters
    Imin : float
        Minimum intensity threshold
    max_rings : int
        Maximum number of rings to consider

    Returns
    -------
    score : float
        Scalar score for Bayesian optimization
    """
    dist, poni1, poni2, rot1, rot2, rot3 = sample
    geom_sample = Geometry(
        dist=dist,
        poni1=poni1,
        poni2=poni2,
        rot1=rot1,
        rot2=rot2,
        rot3=rot3,
        detector=self.detector,
        wavelength=self.calibrant.wavelength,
    )
    sg = _SingleGeometry(
        "Score Geometry",
        self.stacked_powder,
        calibrant=self.calibrant,
        detector=self.detector,
        geometry=geom_sample,
    )
    self._fast_maps(sg)
    with _fast_extract_cp(max_rings):
        sg.extract_cp(max_rings=max_rings, pts_per_deg=pts_per_deg, Imin=Imin)
    data = sg.geometry_refinement.data

    # A handful of points can fit any geometry, so too few points is a failed geometry.
    if data is None or len(data) == 0 or len(data) < self.min_points:
        return 0.0

    ix = data[:, 0]
    iy = data[:, 1]
    ring = data[:, 2].astype(np.int32)
    score = -np.log(
        sg.geometry_refinement.residu2(sample, ix, iy, ring) / len(data)
    )
    return score

set_search_space(parallelized, fixed)

Define the search space for the free parameters.

Parameters

parallelized : str, optional Name of the parameter to be distributed across MPI ranks as a sliding window. Stays part of the free search space. fixed : list List of parameters to keep fixed during optimization

Source code in lute/tasks/_bayfai.py
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
def set_search_space(self, parallelized: Optional[str], fixed: list) -> None:
    """
    Define the search space for the free parameters.

    Parameters
    ----------
    parallelized : str, optional
        Name of the parameter to be distributed across MPI ranks as a
        sliding window. Stays part of the free search space.
    fixed : list
        List of parameters to keep fixed during optimization
    """
    if parallelized and parallelized in fixed:
        raise ValueError(
            f"Parameter '{parallelized}' cannot be both parallelized and fixed."
        )
    self.parallelized = parallelized
    self.fixed = fixed
    self.space = []
    self.order = ["dist", "poni1", "poni2", "rot1", "rot2", "rot3"]
    for p in self.order:
        if p not in fixed:
            self.space.append(p)

setup(detname, h5, Imin, calibrant, fixed, parallelized=None, wavelength=1e-10)

Setup the BayFAI optimization.

Parameters

detname : str Name of the detector h5 : str Path to the smalldata h5 file to use for calibration Imin : float Minimum intensity percentile threshold for Bragg peak detection calibrant : PyFAI.Calibrant PyFAI calibrant object fixed : list List of parameters to keep fixed during optimization parallelized : str, optional Name of the parameter to distribute across MPI ranks as a sliding window. If None, ranks instead run full BO differentiated only by seed. wavelength : float, optional X-ray wavelength in meters. If provided (non-default 1e-10), overrides the value read from the h5 file.

Returns

Imin : float Minimum intensity value for identifying Bragg peaks

Source code in lute/tasks/_bayfai.py
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
def setup(
    self,
    detname: str,
    h5: str,
    Imin: float,
    calibrant: str,
    fixed: list,
    parallelized: Optional[str] = None,
    wavelength: float = 1e-10,
):
    """
    Setup the BayFAI optimization.

    Parameters
    ----------
    detname : str
        Name of the detector
    h5 : str
        Path to the smalldata h5 file to use for calibration
    Imin : float
        Minimum intensity percentile threshold for Bragg peak detection
    calibrant : PyFAI.Calibrant
        PyFAI calibrant object
    fixed : list
        List of parameters to keep fixed during optimization
    parallelized : str, optional
        Name of the parameter to distribute across MPI ranks as a
        sliding window. If None, ranks instead run full BO differentiated
        only by seed.
    wavelength : float, optional
        X-ray wavelength in meters. If provided (non-default 1e-10),
        overrides the value read from the h5 file.

    Returns
    -------
    Imin : float
        Minimum intensity value for identifying Bragg peaks
    """
    self.detector = self.build_detector(detname)
    # twoThetaArray/chiArray recompute full-grid pixel positions (geometry-independent) on every
    # BO evaluation via fromfunction index arrays; pyFAI callers copy (p1 - poni1), never mutate.
    pos, calc = (
        self.detector.calc_cartesian_positions(),
        self.detector.calc_cartesian_positions,
    )
    for a in pos:
        if a is not None:
            a.flags.writeable = False
    self.detector.calc_cartesian_positions = (
        lambda d1=None, d2=None, center=True, use_cython=True: (
            pos
            if center
            and all(
                d is None or np.shape(d) == self.detector.shape for d in (d1, d2)
            )
            else calc(d1, d2, center, use_cython)
        )
    )
    self.powder = self.generate_powder(h5, detname, Imin)
    self.calibrant = self.define_calibrant(calibrant, h5, wavelength)
    self.set_search_space(parallelized, fixed)
    # pyFAI's poni setters reset engines and call gc.collect() on every geometry build;
    # freezing the post-import/setup heap keeps those collections from rescanning it.
    gc.freeze()

update_geometry(out_file, detname)

Update the geometry and write a new .poni, .geom and .data file

Parameters

optimizer : BayesGeomOpt Optimizer object out_file : str Path to the output file

Source code in lute/tasks/_bayfai.py
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
def update_geometry(self, out_file: str, detname: str) -> pyFAI.detectors.Detector:
    """
    Update the geometry and write a new .poni, .geom and .data file

    Parameters
    ----------
    optimizer : BayesGeomOpt
        Optimizer object
    out_file : str
        Path to the output file
    """
    path = os.path.dirname(out_file)
    poni_file = os.path.join(path, f"r{self.run:0>4}_{detname}.poni")
    self.gr.save(poni_file)
    pyfai_to_image_poni(
        detector=self.detector,
        in_file=poni_file,
        out_file=os.path.join(path, f"r{self.run:0>4}_{detname}_image.poni"),
    )
    PyFAIToPsana.convert(
        in_file=poni_file,
        detector=self.detector,
        out_file=out_file,
    )
    geom_file = os.path.join(path, f"r{self.run:0>4}_{detname}.geom")
    PyFAIToCrystFEL.convert(
        in_file=poni_file,
        detector=self.detector,
        out_file=geom_file,
    )

_CachedKernelNames

Cache, per kernel class, the names sklearn recomputes on every theta access.

Stock leaf kernels run inspect.signature (get_params) and dir() (hyperparameters) on each L-BFGS objective call; only the names are cached, values are always read.

Source code in lute/tasks/_bayfai.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
class _CachedKernelNames:
    """Cache, per kernel class, the names sklearn recomputes on every theta access.

    Stock leaf kernels run inspect.signature (get_params) and dir() (hyperparameters)
    on each L-BFGS objective call; only the names are cached, values are always read.
    """

    def get_params(self, deep=True):
        cls = type(self)
        names = cls.__dict__.get("_param_names")
        if names is None:
            names = cls._param_names = tuple(super().get_params(deep=False))
        return {name: getattr(self, name) for name in names}

    @property
    def hyperparameters(self):
        cls = type(self)
        names = cls.__dict__.get("_hp_names")
        if names is None:
            names = cls._hp_names = sorted(
                a for a in dir(cls) if a.startswith("hyperparameter_")
            )
        return [getattr(self, a) for a in names]

_Massif

Bases: Massif

Massif whose peaks_from_area scans only the mask's row band (same points, order, RNG draws).

Source code in lute/tasks/_bayfai.py
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
class _Massif(pyFAI.massif.Massif):
    """Massif whose peaks_from_area scans only the mask's row band (same points, order, RNG draws)."""

    def peaks_from_area(
        self,
        mask,
        Imin=np.finfo(np.float64).min,
        keep=1000,
        dmin=0.0,
        seed=None,
        **kwarg,
    ):
        """Verbatim pyFAI 2024.9 Massif.peaks_from_area except for the all_points and shuffle lines."""
        res = []
        cnt = 0
        dmin2 = dmin * dmin

        # ponytail: same intp values in row-major order as np.where(mask), np.where over the rows that hold points only.
        rows = np.flatnonzero(mask.any(axis=1))
        if rows.size:
            w = np.nonzero(mask[rows[0] : rows[-1] + 1])
            all_points = np.vstack((w[0] + rows[0], w[1])).T
        else:
            all_points = np.vstack(np.where(mask)).T
        # ponytail: legacy RandomState.permutation(n) is arange(n) plus the same Fisher-Yates random_interval(i),
        # i = n-1..1, as shuffle's multi-dim path: same draws, same rows, without the per-row bounce buffer.
        if len(all_points) > 0:
            all_points = all_points[np.random.permutation(len(all_points))]

        if seed:
            seeds = np.array(list(seed))
            if len(seeds) > 0:
                seeds = seeds[np.random.permutation(len(seeds))]
            all_points = np.concatenate((seeds, all_points))

        msg = "[ %3i, %3i ] -> [ %.1f, %.1f ]"
        for idx in all_points:
            out = self.nearest_peak(idx)
            if out is not None:

                pyFAI.massif.logger.debug(msg, idx[1], idx[0], out[1], out[0])
                p0, p1 = int(round(out[0])), int(round(out[1]))
                if mask[p0, p1]:
                    if (self.data[p0, p1] > Imin) and pyFAI.massif.is_far_from_group(
                        out, res, dmin2
                    ):
                        res.append(out)
                        cnt = 0
            if len(res) >= keep or cnt > keep:
                break
            else:
                cnt += 1
        return res

peaks_from_area(mask, Imin=np.finfo(np.float64).min, keep=1000, dmin=0.0, seed=None, **kwarg)

Verbatim pyFAI 2024.9 Massif.peaks_from_area except for the all_points and shuffle lines.

Source code in lute/tasks/_bayfai.py
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
def peaks_from_area(
    self,
    mask,
    Imin=np.finfo(np.float64).min,
    keep=1000,
    dmin=0.0,
    seed=None,
    **kwarg,
):
    """Verbatim pyFAI 2024.9 Massif.peaks_from_area except for the all_points and shuffle lines."""
    res = []
    cnt = 0
    dmin2 = dmin * dmin

    # ponytail: same intp values in row-major order as np.where(mask), np.where over the rows that hold points only.
    rows = np.flatnonzero(mask.any(axis=1))
    if rows.size:
        w = np.nonzero(mask[rows[0] : rows[-1] + 1])
        all_points = np.vstack((w[0] + rows[0], w[1])).T
    else:
        all_points = np.vstack(np.where(mask)).T
    # ponytail: legacy RandomState.permutation(n) is arange(n) plus the same Fisher-Yates random_interval(i),
    # i = n-1..1, as shuffle's multi-dim path: same draws, same rows, without the per-row bounce buffer.
    if len(all_points) > 0:
        all_points = all_points[np.random.permutation(len(all_points))]

    if seed:
        seeds = np.array(list(seed))
        if len(seeds) > 0:
            seeds = seeds[np.random.permutation(len(seeds))]
        all_points = np.concatenate((seeds, all_points))

    msg = "[ %3i, %3i ] -> [ %.1f, %.1f ]"
    for idx in all_points:
        out = self.nearest_peak(idx)
        if out is not None:

            pyFAI.massif.logger.debug(msg, idx[1], idx[0], out[1], out[0])
            p0, p1 = int(round(out[0])), int(round(out[1]))
            if mask[p0, p1]:
                if (self.data[p0, p1] > Imin) and pyFAI.massif.is_far_from_group(
                    out, res, dmin2
                ):
                    res.append(out)
                    cnt = 0
        if len(res) >= keep or cnt > keep:
            break
        else:
            cnt += 1
    return res

_SingleGeometry

Bases: SingleGeometry

SingleGeometry whose extract_cp does the per-ring mask ops on the ring's row band only.

Source code in lute/tasks/_bayfai.py
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
class _SingleGeometry(SingleGeometry):
    """SingleGeometry whose extract_cp does the per-ring mask ops on the ring's row band only."""

    def extract_cp(self, max_rings=None, pts_per_deg=1.0, Imin=0):
        """Verbatim pyFAI 2024.9 SingleGeometry.extract_cp except for the ring-loop mask ops."""
        G = pyFAI.goniometer
        if self.massif is None:
            if self.detector:
                mask = self.detector.dynamic_mask(self.image)
            else:
                mask = None
            self.massif = G.Massif(self.image, mask)

        tth = np.array([i for i in self.calibrant.get_2th() if i is not None])
        tth = np.unique(tth)
        tth_min = np.zeros_like(tth)
        tth_max = np.zeros_like(tth)
        delta = (tth[1:] - tth[:-1]) / 4.0
        tth_max[:-1] = delta
        tth_max[-1] = delta[-1]
        tth_min[1:] = -delta
        tth_min[0] = -delta[0]
        tth_max += tth
        tth_min += tth
        shape = self.image.shape
        ttha = self.geometry_refinement.twoThetaArray(shape)
        chia = self.geometry_refinement.chiArray(shape)
        rings = 0
        cp = G.ControlPoints(calibrant=self.calibrant)
        if max_rings is None:
            max_rings = tth.size

        qmask, count = G.build_qmask(
            ttha, tth_min, tth_max, self.geometry_refinement.detector.mask
        )
        # ponytail: identical because ring i only lives in rows with lo <= i <= hi; outside them stock's
        # qmask == i and mask2 are all False (mask2 kept zero there), and row-major order is preserved.
        mask2 = np.zeros(qmask.shape, dtype=bool)
        hi = qmask.max(axis=1)
        lo = qmask.min(axis=1, initial=count.size, where=qmask >= 0)
        prev = []
        ms = G.marchingsquares.MarchingSquaresMergeImpl(
            ttha, mask=self.geometry_refinement.detector.mask, use_minmax_cache=True
        )
        for i in range(tth.size):
            if rings >= max_rings:
                break
            if count[i]:
                rings += 1
                runs = _row_runs(np.flatnonzero((lo <= i) & (hi >= i)))
                for s in prev:
                    mask2[s] = False
                prev = runs
                masks = [qmask[s] == i for s in runs]
                parts = [self.image[s][m] for s, m in zip(runs, masks)]
                sub_data = parts[0] if len(parts) == 1 else np.concatenate(parts)
                mean = sub_data.mean(dtype=np.float64)
                std = sub_data.std(dtype=np.float64)
                upper_limit = mean + std
                for s, m in zip(runs, masks):
                    np.logical_and(self.image[s] > upper_limit, m, out=mask2[s])
                size2 = sum(int(mask2[s].sum(dtype=int)) for s in runs)
                if size2 < 1000:
                    upper_limit = mean
                    for s, m in zip(runs, masks):
                        np.logical_and(self.image[s] > upper_limit, m, out=mask2[s])
                    size2 = sum(int(mask2[s].sum()) for s in runs)
                # length of the arc:
                points = ms.find_pixels(tth[i])
                seeds = set((i[0], i[1]) for i in points if mask2[i[0], i[1]])
                # max number of points: 360 points for a full circle
                azimuthal = chia[
                    points[:, 0].clip(0, shape[0]), points[:, 1].clip(0, shape[1])
                ]
                nb_deg_azim = np.unique(np.rad2deg(azimuthal).round()).size
                keep = int(nb_deg_azim * pts_per_deg)
                if keep == 0:
                    continue
                dist_min = len(seeds) / 2.0 / keep
                # why 3.0, why not ?

                G.logger.info(
                    "Extracting datapoint for ring %s (2theta = %.2f deg); "
                    + "searching for %i pts out of %i with I>%.1f, dmin=%.1f",
                    i,
                    np.degrees(tth[i]),
                    keep,
                    size2,
                    upper_limit,
                    dist_min,
                )
                res = self.massif.peaks_from_area(
                    mask2, Imin=Imin, keep=keep, dmin=dist_min, seed=seeds, ring=i
                )
                cp.append(res, i)
        self.control_points = cp
        self.geometry_refinement.data = np.asarray(cp.getList(), dtype=np.float64)
        return cp

extract_cp(max_rings=None, pts_per_deg=1.0, Imin=0)

Verbatim pyFAI 2024.9 SingleGeometry.extract_cp except for the ring-loop mask ops.

Source code in lute/tasks/_bayfai.py
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
def extract_cp(self, max_rings=None, pts_per_deg=1.0, Imin=0):
    """Verbatim pyFAI 2024.9 SingleGeometry.extract_cp except for the ring-loop mask ops."""
    G = pyFAI.goniometer
    if self.massif is None:
        if self.detector:
            mask = self.detector.dynamic_mask(self.image)
        else:
            mask = None
        self.massif = G.Massif(self.image, mask)

    tth = np.array([i for i in self.calibrant.get_2th() if i is not None])
    tth = np.unique(tth)
    tth_min = np.zeros_like(tth)
    tth_max = np.zeros_like(tth)
    delta = (tth[1:] - tth[:-1]) / 4.0
    tth_max[:-1] = delta
    tth_max[-1] = delta[-1]
    tth_min[1:] = -delta
    tth_min[0] = -delta[0]
    tth_max += tth
    tth_min += tth
    shape = self.image.shape
    ttha = self.geometry_refinement.twoThetaArray(shape)
    chia = self.geometry_refinement.chiArray(shape)
    rings = 0
    cp = G.ControlPoints(calibrant=self.calibrant)
    if max_rings is None:
        max_rings = tth.size

    qmask, count = G.build_qmask(
        ttha, tth_min, tth_max, self.geometry_refinement.detector.mask
    )
    # ponytail: identical because ring i only lives in rows with lo <= i <= hi; outside them stock's
    # qmask == i and mask2 are all False (mask2 kept zero there), and row-major order is preserved.
    mask2 = np.zeros(qmask.shape, dtype=bool)
    hi = qmask.max(axis=1)
    lo = qmask.min(axis=1, initial=count.size, where=qmask >= 0)
    prev = []
    ms = G.marchingsquares.MarchingSquaresMergeImpl(
        ttha, mask=self.geometry_refinement.detector.mask, use_minmax_cache=True
    )
    for i in range(tth.size):
        if rings >= max_rings:
            break
        if count[i]:
            rings += 1
            runs = _row_runs(np.flatnonzero((lo <= i) & (hi >= i)))
            for s in prev:
                mask2[s] = False
            prev = runs
            masks = [qmask[s] == i for s in runs]
            parts = [self.image[s][m] for s, m in zip(runs, masks)]
            sub_data = parts[0] if len(parts) == 1 else np.concatenate(parts)
            mean = sub_data.mean(dtype=np.float64)
            std = sub_data.std(dtype=np.float64)
            upper_limit = mean + std
            for s, m in zip(runs, masks):
                np.logical_and(self.image[s] > upper_limit, m, out=mask2[s])
            size2 = sum(int(mask2[s].sum(dtype=int)) for s in runs)
            if size2 < 1000:
                upper_limit = mean
                for s, m in zip(runs, masks):
                    np.logical_and(self.image[s] > upper_limit, m, out=mask2[s])
                size2 = sum(int(mask2[s].sum()) for s in runs)
            # length of the arc:
            points = ms.find_pixels(tth[i])
            seeds = set((i[0], i[1]) for i in points if mask2[i[0], i[1]])
            # max number of points: 360 points for a full circle
            azimuthal = chia[
                points[:, 0].clip(0, shape[0]), points[:, 1].clip(0, shape[1])
            ]
            nb_deg_azim = np.unique(np.rad2deg(azimuthal).round()).size
            keep = int(nb_deg_azim * pts_per_deg)
            if keep == 0:
                continue
            dist_min = len(seeds) / 2.0 / keep
            # why 3.0, why not ?

            G.logger.info(
                "Extracting datapoint for ring %s (2theta = %.2f deg); "
                + "searching for %i pts out of %i with I>%.1f, dmin=%.1f",
                i,
                np.degrees(tth[i]),
                keep,
                size2,
                upper_limit,
                dist_min,
            )
            res = self.massif.peaks_from_area(
                mask2, Imin=Imin, keep=keep, dmin=dist_min, seed=seeds, ring=i
            )
            cp.append(res, i)
    self.control_points = cp
    self.geometry_refinement.data = np.asarray(cp.getList(), dtype=np.float64)
    return cp

_build_ai(detector, params, wavelength)

Build a pyFAI AzimuthalIntegrator from a detector, geometry parameters, and wavelength.

Parameters

detector : pyFAI.detectors.Detector PyFAI detector object. params : list 6 geometry parameters: [dist, poni1, poni2, rot1, rot2, rot3] wavelength : float X-ray wavelength in meters.

Returns

AzimuthalIntegrator Configured pyFAI AzimuthalIntegrator.

Source code in lute/tasks/_bayfai.py
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
def _build_ai(
    detector: pyFAI.detectors.Detector,
    params: list,
    wavelength: float,
) -> AzimuthalIntegrator:
    """
    Build a pyFAI AzimuthalIntegrator from a detector, geometry parameters, and wavelength.

    Parameters
    ----------
    detector : pyFAI.detectors.Detector
        PyFAI detector object.
    params : list
        6 geometry parameters: [dist, poni1, poni2, rot1, rot2, rot3]
    wavelength : float
        X-ray wavelength in meters.

    Returns
    -------
    AzimuthalIntegrator
        Configured pyFAI AzimuthalIntegrator.
    """
    dist, poni1, poni2, rot1, rot2, rot3 = params
    return AzimuthalIntegrator(
        dist=dist,
        poni1=poni1,
        poni2=poni2,
        rot1=rot1,
        rot2=rot2,
        rot3=rot3,
        detector=detector,
        wavelength=wavelength,
    )

_fast_extract_cp(max_rings)

Patches active around sg.extract_cp(max_rings=...); outputs and global RNG draws are unchanged.

Source code in lute/tasks/_bayfai.py
161
162
163
164
165
166
167
168
169
170
171
172
@contextlib.contextmanager
def _fast_extract_cp(max_rings):
    """Patches active around sg.extract_cp(max_rings=...); outputs and global RNG draws are unchanged."""
    with (
        mock.patch.object(
            pyFAI.goniometer,
            "build_qmask",
            functools.partial(_qmask_first, max_rings=max_rings),
        ),
        mock.patch.object(pyFAI.goniometer, "Massif", _Massif),
    ):
        yield

_qmask_first(ttha, tth_min, tth_max, mask, max_rings)

build_qmask over the first max_rings+2 2theta intervals only; full call if too few are non-empty.

Source code in lute/tasks/_bayfai.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
def _qmask_first(ttha, tth_min, tth_max, mask, max_rings):
    """build_qmask over the first max_rings+2 2theta intervals only; full call if too few are non-empty."""
    # ponytail: identical because the intervals do not overlap and extract_cp's ring loop breaks before index K.
    n = len(tth_min)
    K = min(n, max_rings + 2)
    if K == n:
        return build_qmask(ttha, tth_min, tth_max, mask)
    qmask, count = build_qmask(ttha, tth_min[:K], tth_max[:K], mask)
    if np.count_nonzero(count) < max_rings:
        return build_qmask(ttha, tth_min, tth_max, mask)
    return qmask, np.concatenate((count, np.zeros(n - K, dtype=count.dtype)))

_row_runs(rows)

Contiguous runs of the sorted row indices, as slices in row order.

Source code in lute/tasks/_bayfai.py
175
176
177
178
179
def _row_runs(rows):
    """Contiguous runs of the sorted row indices, as slices in row order."""
    cut = np.flatnonzero(np.diff(rows) > 1)
    starts, ends = np.r_[rows[0], rows[cut + 1]], np.r_[rows[cut], rows[-1]]
    return [slice(int(a), int(b) + 1) for a, b in zip(starts, ends)]