Skip to content

sfx_find_peaks

Classes for peak finding tasks in SFX.

Classes:

Name Description
CxiWriter

utility class for writing peak finding results to CXI files.

FindPeaksSFX

peak finding using psana's PyAlgos algorithm or peakfinder8. Optional data compression and decompression with libpressio for data reduction tests.

CxiWriter

Source code in lute/tasks/sfx_find_peaks.py
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
class CxiWriter:

    def __init__(
        self,
        outdir: str,
        rank: int,
        exp: str,
        run: int,
        n_events: int,
        det_shape: Tuple[int, ...],
        raw_det_shape: Tuple[int, ...],
        min_peaks: int,
        max_peaks: int,
        i_x: Any,  # Not typed becomes it comes from psana
        i_y: Any,  # Not typed becomes it comes from psana
        ipx: Any,  # Not typed becomes it comes from psana
        ipy: Any,  # Not typed becomes it comes from psana
        tag: str,
        algo: Literal["Peakfinder8", "Peakfinder8_v2", "PyAlgos"] = "Peakfinder8",
    ):
        """
        Set up the CXI files to which peak finding results will be saved.

        Parameters:

            outdir (str): Output directory for cxi file.

            rank (int): MPI rank of the caller.

            exp (str): Experiment string.

            run (int): Experimental run.

            n_events (int): Number of events to process.

            det_shape (Tuple[int, int]): Shape of the numpy array storing the detector
                data. This must be aCheetah-stile 2D array.

            raw_det_shape (Tuple[int, ...]): Shape of the numpy array storing the
                detector in raw unassembled form. Length = 2, 3, or 4.

            min_peaks (int): Minimum number of peaks per image.

            max_peaks (int): Maximum number of peaks per image.

            i_x (Any): Array of pixel indexes along x

            i_y (Any): Array of pixel indexes along y

            ipx (Any): Pixel indexes with respect to detector origin (x component)

            ipy (Any): Pixel indexes with respect to detector origin (y component)

            tag (str): Tag to append to cxi file names.

            algo (str): The algorithm being used - either Peakfinder8 or PyAlgos.
        """
        self._det_shape: Tuple[int, ...] = det_shape
        self._raw_det_shape: Tuple[int, ...] = raw_det_shape
        self._i_x: Any = i_x
        self._i_y: Any = i_y
        self._ipx: Any = ipx
        self._ipy: Any = ipy
        self._index: int = 0

        # Create and open the HDF5 file
        fname: str = f"{exp}_r{run:0>4}_{rank}{tag}.cxi"
        Path(outdir).mkdir(exist_ok=True)
        self._outh5: Any = h5py.File(Path(outdir) / fname, "w")

        # Entry_1 entry for processing with CrystFEL
        entry_1: Any = self._outh5.create_group("entry_1")

        keys: List[str]
        if "Peakfinder8" in algo:
            keys = [
                "nPeaks",
                "peakXPosRaw",
                "peakYPosRaw",
                "peakNPixels",
                "peakTotalIntensity",
                "peakMaxIntensity",
                "peakSNR",
            ]
            if algo == "Peakfinder8_v2":
                keys.append("peakPanelNumRaw")
        else:
            keys = [
                "nPeaks",
                "peakXPosRaw",
                "peakYPosRaw",
                "rcent",
                "ccent",
                "rmin",
                "rmax",
                "cmin",
                "cmax",
                "peakTotalIntensity",
                "peakMaxIntensity",
                "peakRadius",
            ]
        ds_expId: Any = entry_1.create_dataset(
            "experimental_identifier", (n_events,), maxshape=(None,), dtype=int
        )
        ds_expId.attrs["axes"] = "experiment_identifier"
        data_1: Any = entry_1.create_dataset(
            "/entry_1/data_1/data",
            (n_events, det_shape[0], det_shape[1]),
            chunks=(1, det_shape[0], det_shape[1]),
            maxshape=(None, det_shape[0], det_shape[1]),
            dtype=np.float32,
        )
        data_1.attrs["axes"] = "experiment_identifier"

        key: str
        for key in ["powderHits", "powderMisses", "mask"]:
            entry_1.create_dataset(
                f"/entry_1/data_1/{key}",
                det_shape,
                chunks=det_shape,
                maxshape=det_shape,
                dtype=float,
            )

        # Peak-related entries
        ds_x: Any
        for key in keys:
            if key == "nPeaks":
                ds_x = self._outh5.create_dataset(
                    f"/entry_1/result_1/{key}",
                    (n_events,),
                    maxshape=(None,),
                    dtype=int,
                )
                ds_x.attrs["minPeaks"] = min_peaks
                ds_x.attrs["maxPeaks"] = max_peaks
            else:
                ds_x = self._outh5.create_dataset(
                    f"/entry_1/result_1/{key}",
                    (n_events, max_peaks),
                    maxshape=(None, max_peaks),
                    chunks=(1, max_peaks),
                    dtype=float,
                )
            ds_x.attrs["axes"] = "experiment_identifier:peaks"

        # Timestamp entries
        lcls_1: Any = self._outh5.create_group("LCLS")
        keys = [
            "eventNumber",
            "machineTime",
            "machineTimeNanoSeconds",
            "fiducial",
            "photon_energy_eV",
        ]
        for key in keys:
            if key == "photon_energy_eV":
                ds_x = lcls_1.create_dataset(
                    f"{key}", (n_events,), maxshape=(None,), dtype=float
                )
            else:
                ds_x = lcls_1.create_dataset(
                    f"{key}", (n_events,), maxshape=(None,), dtype=int
                )
            ds_x.attrs["axes"] = "experiment_identifier"

        ds_x = self._outh5.create_dataset(
            "/LCLS/detector_1/EncoderValue", (n_events,), maxshape=(None,), dtype=float
        )
        ds_x.attrs["axes"] = "experiment_identifier"

    def write_event(
        self,
        img: npt.NDArray[np.float32],
        peaks: Any,  # Not typed becomes it comes from psana
        timestamp_seconds: int,
        timestamp_nanoseconds: int,
        timestamp_fiducials: int,
        photon_energy: float,
        clen: float,
        algo: Literal["Peakfinder8", "Peakfinder8_v2", "PyAlgos"] = "Peakfinder8",
    ):
        """
        Write peak finding results for an event into the HDF5 file.

        Parameters:

            img (npt.NDArray[np.float32]): Detector data for the event

            peaks: (Any): Peak information for the event, as recovered from the PyAlgos
                algorithm

            timestamp_seconds (int): Second part of the event's timestamp information

            timestamp_nanoseconds (int): Nanosecond part of the event's timestamp
                information

            timestamp_fiducials (int): Fiducials part of the event's timestamp
                information

            photon_energy (float): Photon energy for the event

            clen (float): Camera length/detector distance.
        """
        if algo == "PyAlgos":
            self._write_event_pyalgos(
                img=img,
                peaks=peaks,
                timestamp_seconds=timestamp_seconds,
                timestamp_nanoseconds=timestamp_nanoseconds,
                timestamp_fiducials=timestamp_fiducials,
                photon_energy=photon_energy,
                clen=clen,
            )
        elif algo not in ("Peakfinder8", "Peakfinder8_v2"):
            raise ValueError(f"Unsupported peakfinding algorithm {algo}!")
        else:
            v2: bool = False
            if algo == "Peakfinder8":
                v2 = False
            elif algo == "Peakfinder8_v2":
                v2 = True

            self._write_event_peakfinder8(
                img=img,
                peaks=peaks,
                timestamp_seconds=timestamp_seconds,
                timestamp_nanoseconds=timestamp_nanoseconds,
                timestamp_fiducials=timestamp_fiducials,
                photon_energy=photon_energy,
                clen=clen,
                v2=v2,
            )

    def _write_event_peakfinder8(
        self,
        img: npt.NDArray[np.float32],
        peaks: Union[Peakfinder8PeakList, Peakfinder8_v2PeakList],
        timestamp_seconds: int,
        timestamp_nanoseconds: int,
        timestamp_fiducials: int,
        photon_energy: float,
        clen: float,
        v2: bool = False,
    ) -> None:
        if self._outh5["/entry_1/data_1/data"].shape[0] <= self._index:
            self._outh5["entry_1/data_1/data"].resize(self._index + 1, axis=0)
            ds_key: str
            for ds_key in self._outh5["/entry_1/result_1"].keys():
                self._outh5[f"/entry_1/result_1/{ds_key}"].resize(
                    self._index + 1, axis=0
                )
            for ds_key in (
                "machineTime",
                "machineTimeNanoSeconds",
                "fiducial",
                "photon_energy_eV",
                "detector_1/EncoderValue",
            ):
                self._outh5[f"/LCLS/{ds_key}"].resize(self._index + 1, axis=0)

        # Entry_1 entry for processing with CrystFEL
        self._outh5["/entry_1/data_1/data"][self._index, :, :] = img.reshape(
            -1, img.shape[-1]
        )
        num_peaks: int = peaks["num_peaks"]
        self._outh5["/entry_1/result_1/nPeaks"][self._index] = num_peaks
        self._outh5["/entry_1/result_1/peakXPosRaw"][self._index, :num_peaks] = (
            np.array(peaks["fs"], dtype=np.float32)
        )
        self._outh5["/entry_1/result_1/peakYPosRaw"][self._index, :num_peaks] = (
            np.array(peaks["ss"], dtype=np.float32)
        )
        if v2:
            peaks = cast(Peakfinder8_v2PeakList, peaks)
            self._outh5["/entry_1/result_1/peakPanelNumRaw"][
                self._index, :num_peaks
            ] = np.array(peaks["panel_number"], dtype=np.float32)
        self._outh5["/entry_1/result_1/peakNPixels"][self._index, :num_peaks] = (
            np.array(peaks["num_pixels"], dtype=np.float32)
        )
        self._outh5["/entry_1/result_1/peakTotalIntensity"][self._index, :num_peaks] = (
            np.array(peaks["intensity"], dtype=np.float32)
        )
        self._outh5["/entry_1/result_1/peakMaxIntensity"][self._index, :num_peaks] = (
            np.array(peaks["max_pixel_intensity"], dtype=np.float32)
        )
        self._outh5["/entry_1/result_1/peakSNR"][self._index, :num_peaks] = np.array(
            peaks["snr"], dtype=np.float32
        )

        # LCLS entry dataset
        self._outh5["/LCLS/machineTime"][self._index] = timestamp_seconds
        self._outh5["/LCLS/machineTimeNanoSeconds"][self._index] = timestamp_nanoseconds
        self._outh5["/LCLS/fiducial"][self._index] = timestamp_fiducials
        self._outh5["/LCLS/photon_energy_eV"][self._index] = photon_energy

        # Add clen distance
        self._outh5["/LCLS/detector_1/EncoderValue"][self._index] = clen
        self._index += 1

    def _write_event_pyalgos(
        self,
        img: npt.NDArray[np.float32],
        peaks: Any,  # Not typed becomes it comes from psana
        timestamp_seconds: int,
        timestamp_nanoseconds: int,
        timestamp_fiducials: int,
        photon_energy: float,
        clen: float,
    ) -> None:
        ch_rows: npt.NDArray[np.float64] = (
            peaks[:, 0] * self._raw_det_shape[-2] + peaks[:, 1]
        )
        ch_cols: npt.NDArray[np.float64] = peaks[:, 2]

        if self._outh5["/entry_1/data_1/data"].shape[0] <= self._index:
            self._outh5["entry_1/data_1/data"].resize(self._index + 1, axis=0)
            ds_key: str
            for ds_key in self._outh5["/entry_1/result_1"].keys():
                self._outh5[f"/entry_1/result_1/{ds_key}"].resize(
                    self._index + 1, axis=0
                )
            for ds_key in (
                "machineTime",
                "machineTimeNanoSeconds",
                "fiducial",
                "photon_energy_eV",
                "detector_1/EncoderValue",
            ):
                self._outh5[f"/LCLS/{ds_key}"].resize(self._index + 1, axis=0)

        # Entry_1 entry for processing with CrystFEL
        self._outh5["/entry_1/data_1/data"][self._index, :, :] = img.reshape(
            -1, img.shape[-1]
        )
        self._outh5["/entry_1/result_1/nPeaks"][self._index] = peaks.shape[0]
        self._outh5["/entry_1/result_1/peakXPosRaw"][self._index, : peaks.shape[0]] = (
            ch_cols.astype("int")
        )
        self._outh5["/entry_1/result_1/peakYPosRaw"][self._index, : peaks.shape[0]] = (
            ch_rows.astype("int")
        )
        self._outh5["/entry_1/result_1/rcent"][self._index, : peaks.shape[0]] = peaks[
            :, 6
        ]
        self._outh5["/entry_1/result_1/ccent"][self._index, : peaks.shape[0]] = peaks[
            :, 7
        ]
        self._outh5["/entry_1/result_1/rmin"][self._index, : peaks.shape[0]] = peaks[
            :, 10
        ]
        self._outh5["/entry_1/result_1/rmax"][self._index, : peaks.shape[0]] = peaks[
            :, 11
        ]
        self._outh5["/entry_1/result_1/cmin"][self._index, : peaks.shape[0]] = peaks[
            :, 12
        ]
        self._outh5["/entry_1/result_1/cmax"][self._index, : peaks.shape[0]] = peaks[
            :, 13
        ]
        self._outh5["/entry_1/result_1/peakTotalIntensity"][
            self._index, : peaks.shape[0]
        ] = peaks[:, 5]
        self._outh5["/entry_1/result_1/peakMaxIntensity"][
            self._index, : peaks.shape[0]
        ] = peaks[:, 4]

        # Calculate and write pixel radius
        peaks_cenx: npt.NDArray[np.float64] = (
            self._i_x[
                np.array(peaks[:, 0], dtype=np.int64),
                np.array(peaks[:, 1], dtype=np.int64),
                np.array(peaks[:, 2], dtype=np.int64),
            ]
            + 0.5
            - self._ipx
        )
        peaks_ceny: npt.NDArray[np.float64] = (
            self._i_y[
                np.array(peaks[:, 0], dtype=np.int64),
                np.array(peaks[:, 1], dtype=np.int64),
                np.array(peaks[:, 2], dtype=np.int64),
            ]
            + 0.5
            - self._ipy
        )
        peak_radius: npt.NDArray[np.float64] = np.sqrt(
            (peaks_cenx**2) + (peaks_ceny**2)
        )
        self._outh5["/entry_1/result_1/peakRadius"][
            self._index, : peaks.shape[0]
        ] = peak_radius

        # LCLS entry dataset
        self._outh5["/LCLS/machineTime"][self._index] = timestamp_seconds
        self._outh5["/LCLS/machineTimeNanoSeconds"][self._index] = timestamp_nanoseconds
        self._outh5["/LCLS/fiducial"][self._index] = timestamp_fiducials
        self._outh5["/LCLS/photon_energy_eV"][self._index] = photon_energy

        # Add clen distance
        self._outh5["/LCLS/detector_1/EncoderValue"][self._index] = clen
        self._index += 1

    def write_non_event_data(
        self,
        powder_hits: npt.NDArray[np.float64],
        powder_misses: npt.NDArray[np.float64],
        mask: npt.NDArray[np.uint8],
    ):
        """
        Write to the file data that is not related to a specific event (masks, powders)

        Parameters:

            powder_hits (npt.NDArray[np.float64]): Virtual powder pattern from hits

            powder_misses (npt.NDArray[np.float64]): Virtual powder pattern from hits

            mask: (npt.NDArray[np.uint16]): Pixel ask to write into the file

        """
        # Add powders and mask to files, reshaping them to match the crystfel
        # convention
        self._outh5["/entry_1/data_1/powderHits"][:] = powder_hits
        self._outh5["/entry_1/data_1/powderMisses"][:] = powder_misses
        self._outh5["/entry_1/data_1/mask"][:] = (1 - mask).reshape(
            -1, mask.shape[-1]
        )  # Crystfel expects inverted values

    def optimize_and_close_file(
        self,
        num_hits: int,
        max_peaks: int,
        algo: Literal["PyAlgos", "Peakfinder8", "Peakfinder8_v2"] = "Peakfinder8",
    ):
        """
        Resize data blocks and write additional information to the file

        Parameters:

            num_hits (int): Number of hits for which information has been saved to the
                file

            max_peaks (int): Maximum number of peaks (per event) for which information
                can be written into the file

            algo (Literal["PyAlgos", "Peakfinder8", "Peakfinder8_v2"]): The algorithm
                being used for peakfinding. This affects the exact keys that are
                saved.
        """

        # Resize the entry_1 entry
        data_shape: Tuple[int, ...] = self._outh5["/entry_1/data_1/data"].shape
        self._outh5["/entry_1/data_1/data"].resize(
            (num_hits, data_shape[1], data_shape[2])
        )
        self._outh5["/entry_1/result_1/nPeaks"].resize((num_hits,))

        keys: List[str]
        if "Peakfinder8" in algo:
            keys = [
                "peakXPosRaw",
                "peakYPosRaw",
                "peakNPixels",
                "peakTotalIntensity",
                "peakMaxIntensity",
                "peakSNR",
            ]
            if algo == "Peakfinder8_v2":
                keys.append("peakPanelNumRaw")
        else:
            keys = [
                "peakXPosRaw",
                "peakYPosRaw",
                "rcent",
                "ccent",
                "rmin",
                "rmax",
                "cmin",
                "cmax",
                "peakTotalIntensity",
                "peakMaxIntensity",
                "peakRadius",
            ]

        key: str
        for key in keys:
            self._outh5[f"/entry_1/result_1/{key}"].resize((num_hits, max_peaks))

        # Resize LCLS entry
        for key in [
            "eventNumber",
            "machineTime",
            "machineTimeNanoSeconds",
            "fiducial",
            "detector_1/EncoderValue",
            "photon_energy_eV",
        ]:
            self._outh5[f"/LCLS/{key}"].resize((num_hits,))
        self._outh5.close()

__init__(outdir, rank, exp, run, n_events, det_shape, raw_det_shape, min_peaks, max_peaks, i_x, i_y, ipx, ipy, tag, algo='Peakfinder8')

Set up the CXI files to which peak finding results will be saved.

Parameters:

outdir (str): Output directory for cxi file.

rank (int): MPI rank of the caller.

exp (str): Experiment string.

run (int): Experimental run.

n_events (int): Number of events to process.

det_shape (Tuple[int, int]): Shape of the numpy array storing the detector
    data. This must be aCheetah-stile 2D array.

raw_det_shape (Tuple[int, ...]): Shape of the numpy array storing the
    detector in raw unassembled form. Length = 2, 3, or 4.

min_peaks (int): Minimum number of peaks per image.

max_peaks (int): Maximum number of peaks per image.

i_x (Any): Array of pixel indexes along x

i_y (Any): Array of pixel indexes along y

ipx (Any): Pixel indexes with respect to detector origin (x component)

ipy (Any): Pixel indexes with respect to detector origin (y component)

tag (str): Tag to append to cxi file names.

algo (str): The algorithm being used - either Peakfinder8 or PyAlgos.
Source code in lute/tasks/sfx_find_peaks.py
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
def __init__(
    self,
    outdir: str,
    rank: int,
    exp: str,
    run: int,
    n_events: int,
    det_shape: Tuple[int, ...],
    raw_det_shape: Tuple[int, ...],
    min_peaks: int,
    max_peaks: int,
    i_x: Any,  # Not typed becomes it comes from psana
    i_y: Any,  # Not typed becomes it comes from psana
    ipx: Any,  # Not typed becomes it comes from psana
    ipy: Any,  # Not typed becomes it comes from psana
    tag: str,
    algo: Literal["Peakfinder8", "Peakfinder8_v2", "PyAlgos"] = "Peakfinder8",
):
    """
    Set up the CXI files to which peak finding results will be saved.

    Parameters:

        outdir (str): Output directory for cxi file.

        rank (int): MPI rank of the caller.

        exp (str): Experiment string.

        run (int): Experimental run.

        n_events (int): Number of events to process.

        det_shape (Tuple[int, int]): Shape of the numpy array storing the detector
            data. This must be aCheetah-stile 2D array.

        raw_det_shape (Tuple[int, ...]): Shape of the numpy array storing the
            detector in raw unassembled form. Length = 2, 3, or 4.

        min_peaks (int): Minimum number of peaks per image.

        max_peaks (int): Maximum number of peaks per image.

        i_x (Any): Array of pixel indexes along x

        i_y (Any): Array of pixel indexes along y

        ipx (Any): Pixel indexes with respect to detector origin (x component)

        ipy (Any): Pixel indexes with respect to detector origin (y component)

        tag (str): Tag to append to cxi file names.

        algo (str): The algorithm being used - either Peakfinder8 or PyAlgos.
    """
    self._det_shape: Tuple[int, ...] = det_shape
    self._raw_det_shape: Tuple[int, ...] = raw_det_shape
    self._i_x: Any = i_x
    self._i_y: Any = i_y
    self._ipx: Any = ipx
    self._ipy: Any = ipy
    self._index: int = 0

    # Create and open the HDF5 file
    fname: str = f"{exp}_r{run:0>4}_{rank}{tag}.cxi"
    Path(outdir).mkdir(exist_ok=True)
    self._outh5: Any = h5py.File(Path(outdir) / fname, "w")

    # Entry_1 entry for processing with CrystFEL
    entry_1: Any = self._outh5.create_group("entry_1")

    keys: List[str]
    if "Peakfinder8" in algo:
        keys = [
            "nPeaks",
            "peakXPosRaw",
            "peakYPosRaw",
            "peakNPixels",
            "peakTotalIntensity",
            "peakMaxIntensity",
            "peakSNR",
        ]
        if algo == "Peakfinder8_v2":
            keys.append("peakPanelNumRaw")
    else:
        keys = [
            "nPeaks",
            "peakXPosRaw",
            "peakYPosRaw",
            "rcent",
            "ccent",
            "rmin",
            "rmax",
            "cmin",
            "cmax",
            "peakTotalIntensity",
            "peakMaxIntensity",
            "peakRadius",
        ]
    ds_expId: Any = entry_1.create_dataset(
        "experimental_identifier", (n_events,), maxshape=(None,), dtype=int
    )
    ds_expId.attrs["axes"] = "experiment_identifier"
    data_1: Any = entry_1.create_dataset(
        "/entry_1/data_1/data",
        (n_events, det_shape[0], det_shape[1]),
        chunks=(1, det_shape[0], det_shape[1]),
        maxshape=(None, det_shape[0], det_shape[1]),
        dtype=np.float32,
    )
    data_1.attrs["axes"] = "experiment_identifier"

    key: str
    for key in ["powderHits", "powderMisses", "mask"]:
        entry_1.create_dataset(
            f"/entry_1/data_1/{key}",
            det_shape,
            chunks=det_shape,
            maxshape=det_shape,
            dtype=float,
        )

    # Peak-related entries
    ds_x: Any
    for key in keys:
        if key == "nPeaks":
            ds_x = self._outh5.create_dataset(
                f"/entry_1/result_1/{key}",
                (n_events,),
                maxshape=(None,),
                dtype=int,
            )
            ds_x.attrs["minPeaks"] = min_peaks
            ds_x.attrs["maxPeaks"] = max_peaks
        else:
            ds_x = self._outh5.create_dataset(
                f"/entry_1/result_1/{key}",
                (n_events, max_peaks),
                maxshape=(None, max_peaks),
                chunks=(1, max_peaks),
                dtype=float,
            )
        ds_x.attrs["axes"] = "experiment_identifier:peaks"

    # Timestamp entries
    lcls_1: Any = self._outh5.create_group("LCLS")
    keys = [
        "eventNumber",
        "machineTime",
        "machineTimeNanoSeconds",
        "fiducial",
        "photon_energy_eV",
    ]
    for key in keys:
        if key == "photon_energy_eV":
            ds_x = lcls_1.create_dataset(
                f"{key}", (n_events,), maxshape=(None,), dtype=float
            )
        else:
            ds_x = lcls_1.create_dataset(
                f"{key}", (n_events,), maxshape=(None,), dtype=int
            )
        ds_x.attrs["axes"] = "experiment_identifier"

    ds_x = self._outh5.create_dataset(
        "/LCLS/detector_1/EncoderValue", (n_events,), maxshape=(None,), dtype=float
    )
    ds_x.attrs["axes"] = "experiment_identifier"

optimize_and_close_file(num_hits, max_peaks, algo='Peakfinder8')

Resize data blocks and write additional information to the file

Parameters:

num_hits (int): Number of hits for which information has been saved to the
    file

max_peaks (int): Maximum number of peaks (per event) for which information
    can be written into the file

algo (Literal["PyAlgos", "Peakfinder8", "Peakfinder8_v2"]): The algorithm
    being used for peakfinding. This affects the exact keys that are
    saved.
Source code in lute/tasks/sfx_find_peaks.py
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
def optimize_and_close_file(
    self,
    num_hits: int,
    max_peaks: int,
    algo: Literal["PyAlgos", "Peakfinder8", "Peakfinder8_v2"] = "Peakfinder8",
):
    """
    Resize data blocks and write additional information to the file

    Parameters:

        num_hits (int): Number of hits for which information has been saved to the
            file

        max_peaks (int): Maximum number of peaks (per event) for which information
            can be written into the file

        algo (Literal["PyAlgos", "Peakfinder8", "Peakfinder8_v2"]): The algorithm
            being used for peakfinding. This affects the exact keys that are
            saved.
    """

    # Resize the entry_1 entry
    data_shape: Tuple[int, ...] = self._outh5["/entry_1/data_1/data"].shape
    self._outh5["/entry_1/data_1/data"].resize(
        (num_hits, data_shape[1], data_shape[2])
    )
    self._outh5["/entry_1/result_1/nPeaks"].resize((num_hits,))

    keys: List[str]
    if "Peakfinder8" in algo:
        keys = [
            "peakXPosRaw",
            "peakYPosRaw",
            "peakNPixels",
            "peakTotalIntensity",
            "peakMaxIntensity",
            "peakSNR",
        ]
        if algo == "Peakfinder8_v2":
            keys.append("peakPanelNumRaw")
    else:
        keys = [
            "peakXPosRaw",
            "peakYPosRaw",
            "rcent",
            "ccent",
            "rmin",
            "rmax",
            "cmin",
            "cmax",
            "peakTotalIntensity",
            "peakMaxIntensity",
            "peakRadius",
        ]

    key: str
    for key in keys:
        self._outh5[f"/entry_1/result_1/{key}"].resize((num_hits, max_peaks))

    # Resize LCLS entry
    for key in [
        "eventNumber",
        "machineTime",
        "machineTimeNanoSeconds",
        "fiducial",
        "detector_1/EncoderValue",
        "photon_energy_eV",
    ]:
        self._outh5[f"/LCLS/{key}"].resize((num_hits,))
    self._outh5.close()

write_event(img, peaks, timestamp_seconds, timestamp_nanoseconds, timestamp_fiducials, photon_energy, clen, algo='Peakfinder8')

Write peak finding results for an event into the HDF5 file.

Parameters:

img (npt.NDArray[np.float32]): Detector data for the event

peaks: (Any): Peak information for the event, as recovered from the PyAlgos
    algorithm

timestamp_seconds (int): Second part of the event's timestamp information

timestamp_nanoseconds (int): Nanosecond part of the event's timestamp
    information

timestamp_fiducials (int): Fiducials part of the event's timestamp
    information

photon_energy (float): Photon energy for the event

clen (float): Camera length/detector distance.
Source code in lute/tasks/sfx_find_peaks.py
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
def write_event(
    self,
    img: npt.NDArray[np.float32],
    peaks: Any,  # Not typed becomes it comes from psana
    timestamp_seconds: int,
    timestamp_nanoseconds: int,
    timestamp_fiducials: int,
    photon_energy: float,
    clen: float,
    algo: Literal["Peakfinder8", "Peakfinder8_v2", "PyAlgos"] = "Peakfinder8",
):
    """
    Write peak finding results for an event into the HDF5 file.

    Parameters:

        img (npt.NDArray[np.float32]): Detector data for the event

        peaks: (Any): Peak information for the event, as recovered from the PyAlgos
            algorithm

        timestamp_seconds (int): Second part of the event's timestamp information

        timestamp_nanoseconds (int): Nanosecond part of the event's timestamp
            information

        timestamp_fiducials (int): Fiducials part of the event's timestamp
            information

        photon_energy (float): Photon energy for the event

        clen (float): Camera length/detector distance.
    """
    if algo == "PyAlgos":
        self._write_event_pyalgos(
            img=img,
            peaks=peaks,
            timestamp_seconds=timestamp_seconds,
            timestamp_nanoseconds=timestamp_nanoseconds,
            timestamp_fiducials=timestamp_fiducials,
            photon_energy=photon_energy,
            clen=clen,
        )
    elif algo not in ("Peakfinder8", "Peakfinder8_v2"):
        raise ValueError(f"Unsupported peakfinding algorithm {algo}!")
    else:
        v2: bool = False
        if algo == "Peakfinder8":
            v2 = False
        elif algo == "Peakfinder8_v2":
            v2 = True

        self._write_event_peakfinder8(
            img=img,
            peaks=peaks,
            timestamp_seconds=timestamp_seconds,
            timestamp_nanoseconds=timestamp_nanoseconds,
            timestamp_fiducials=timestamp_fiducials,
            photon_energy=photon_energy,
            clen=clen,
            v2=v2,
        )

write_non_event_data(powder_hits, powder_misses, mask)

Write to the file data that is not related to a specific event (masks, powders)

Parameters:

powder_hits (npt.NDArray[np.float64]): Virtual powder pattern from hits

powder_misses (npt.NDArray[np.float64]): Virtual powder pattern from hits

mask: (npt.NDArray[np.uint16]): Pixel ask to write into the file
Source code in lute/tasks/sfx_find_peaks.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 write_non_event_data(
    self,
    powder_hits: npt.NDArray[np.float64],
    powder_misses: npt.NDArray[np.float64],
    mask: npt.NDArray[np.uint8],
):
    """
    Write to the file data that is not related to a specific event (masks, powders)

    Parameters:

        powder_hits (npt.NDArray[np.float64]): Virtual powder pattern from hits

        powder_misses (npt.NDArray[np.float64]): Virtual powder pattern from hits

        mask: (npt.NDArray[np.uint16]): Pixel ask to write into the file

    """
    # Add powders and mask to files, reshaping them to match the crystfel
    # convention
    self._outh5["/entry_1/data_1/powderHits"][:] = powder_hits
    self._outh5["/entry_1/data_1/powderMisses"][:] = powder_misses
    self._outh5["/entry_1/data_1/mask"][:] = (1 - mask).reshape(
        -1, mask.shape[-1]
    )  # Crystfel expects inverted values

FindPeaksSFX

Bases: Task

Task that performs peak finding using the PyAlgos peak finding algorithms and writes the peak information to CXI files.

Source code in lute/tasks/sfx_find_peaks.py
 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
class FindPeaksSFX(Task):
    """
    Task that performs peak finding using the PyAlgos peak finding algorithms and
    writes the peak information to CXI files.
    """

    def __init__(
        self, *, params: FindPeaksSFXParameters, use_mpi: bool = True, row_ids=None
    ) -> None:
        self._task_parameters: FindPeaksSFXParameters = params
        super().__init__(params=params, use_mpi=use_mpi, row_ids=row_ids)

        self._algo: Literal["PyAlgos", "Peakfinder8", "Peakfinder8_v2"] = (
            self._task_parameters.algorithm
        )

    def get_radius_map(self) -> npt.NDArray[np.float64]:
        from lute.tasks.util.geometry import (
            CrystfelDetectorGeometry,
            PixelMaps,
            crystfel_to_pixel_map,
            parse_crystfel_geometry_file,
        )

        if self._task_parameters.geometry_file is not None:
            geom_desc: CrystfelDetectorGeometry = parse_crystfel_geometry_file(
                file_path=self._task_parameters.geometry_file
            )
            pixel_maps: PixelMaps = crystfel_to_pixel_map(geometry_desc=geom_desc)
            return pixel_maps["radius_map"]
        else:
            raise RuntimeError("Need a geometry file to compute radius map!")

    def _compute_num_radial_bins(
        self, indices: Tuple[slice, ...], radius_map: npt.NDArray[np.float64]
    ) -> int:
        return int(np.ceil(radius_map[indices].max()) + 1)

    def compute_radial_statistics(
        self,
        radius_map: npt.NDArray[np.float64],
        shape: Tuple[int, ...],
        num_bins: int = 100,
    ) -> RadialStatistics:
        radius_map_int: npt.NDArray[np.int8] = np.rint(radius_map).astype(int).ravel()
        peak_index: List[int] = []
        radius: List[int] = []
        for idx in np.split(
            np.argsort(radius_map_int, kind="mergesort"),
            np.cumsum(np.bincount(radius_map_int)[:-1]),
        ):
            if len(idx) < num_bins:
                peak_index.extend(idx)
                radius.extend(radius_map_int[(idx,)])
            else:
                idx_sample: List[int] = random.sample(list(idx), num_bins)
                peak_index.extend(idx_sample)
                radius.extend(radius_map_int[(idx_sample,)])

        rstats_pixel_index: npt.NDArray[np.int32] = np.array(peak_index).astype(
            np.int32
        )
        rstats_radius: npt.NDArray[np.int32] = np.array(radius).astype(np.int32)

        logger.debug(
            f"Computed radial statistics for {num_bins} bins. "
            f"rstats_num_pix: {rstats_pixel_index.size}"
        )

        return {
            "rstats_pixel_index": rstats_pixel_index,
            "rstats_radius": rstats_radius,
            "rstats_num_pix": rstats_pixel_index.size,
            "radial_map": radius_map.reshape(shape),
        }

    def _retrieve_psana_detector_data(self, lcls2: bool = True) -> DetectorGeomInfo:
        exp: str = self._task_parameters.lute_config.experiment
        run_num: int = int(self._task_parameters.lute_config.run)

        if lcls2:
            import psana  # type: ignore

            ds2: psana.psexp.mpi_ds.ParallelDataSource = psana.DataSource(
                exp=exp,
                run=run_num,
            )
            run2: psana.psexp.mpi_ds.RunParallel = next(ds2.runs())
            det2 = run2.Detector(self._task_parameters.det_name)

            i_x, i_y = det2.raw._pixel_coord_indexes()

            ipx, ipy = det2.raw._pixel_xy_at_z()
            return DetectorGeomInfo(i_x=i_x, i_y=i_y, ipx=ipx, ipy=ipy)
        else:
            from psana import Detector, MPIDataSource  # type: ignore

            ds: Any = MPIDataSource(f"exp={exp}:run={run_num}:smd")  # noqa

            det: Any = Detector(self._task_parameters.det_name)
            det.do_reshape_2d_to_3d(flag=True)
            i_x = det.indexes_x(self._task_parameters.lute_config.run).astype(np.int64)
            i_y = det.indexes_y(self._task_parameters.lute_config.run).astype(np.int64)
            ipx, ipy = det.point_indexes(
                self._task_parameters.lute_config.run, pxy_um=(0, 0)
            )

            return DetectorGeomInfo(i_x=i_x, i_y=i_y, ipx=ipx, ipy=ipy)

    def _event_generator(
        self, lcls2: bool = True
    ) -> Generator[Union[EventMaskData, EventData], None, None]:
        exp: str = self._task_parameters.lute_config.experiment
        run_num: int = int(self._task_parameters.lute_config.run)

        first_event: bool = True
        if lcls2:
            import psana  # type: ignore

            kwargs: Dict[str, Any] = {
                "exp": exp,
                "run": run_num,
            }
            if self._task_parameters.n_events != 0:
                kwargs["max_events"] = self._task_parameters.n_events

            ds2: psana.psexp.mpi_ds.ParallelDataSource = psana.DataSource(**kwargs)
            run2: psana.psexp.mpi_ds.RunParallel = next(ds2.runs())
            timing2 = run2.Detector("timing")

            det2 = run2.Detector(self._task_parameters.det_name)

            # TODO: Need to handle other BLD?
            ebeamh = run2.Detector("ebeamh")

            if first_event:
                mask2: Optional[npt.NDArray[np.uint8]] = None
            for evt in run2.events():
                data: Optional[npt.NDArray[np.float32]] = det2.raw.calib(evt)

                raw_timestamp: Optional[int] = timing2.raw.timestamp(evt)

                # Fiducial only makes sense if actually on LCLS1 timing
                raw_fiducial2: Optional[int] = None
                seconds2: Optional[int] = None
                nanoseconds2: Optional[int] = None
                if raw_timestamp:
                    raw_fiducial2 = raw_timestamp & 0x1FFFF
                    nanoseconds2 = raw_timestamp & 0xFFFFFFFF
                    seconds2 = raw_timestamp >> 32

                photon_energy2: Optional[float] = None
                try:
                    photon_energy2 = ebeamh.raw.ebeamPhotonEnergy(evt)
                    if photon_energy2 and np.isinf(photon_energy2):
                        raise ValueError

                except (AttributeError, ValueError):
                    ebeam_raw: Optional[float] = run2.Detector("SIOC:SYS0:ML00:AO192")(
                        evt
                    )
                    if ebeam_raw:
                        photon_energy2 = (1.23984197386209e-06 / ebeam_raw) * 1e9

                if isinstance(self._task_parameters.pv_camera_length, float):
                    clen2: float = self._task_parameters.pv_camera_length
                else:
                    clen2 = run2.Detector(self._task_parameters.pv_camera_length)(evt)

                if data is not None:
                    det2_shape: Tuple[int, ...] = data.shape
                    if len(det2_shape) == 3:
                        det2_shape = (det2_shape[0] * det2_shape[1], det2_shape[2])
                    else:
                        det2_shape = data.shape
                    if first_event:
                        if hasattr(det2.raw, "_mask"):
                            mask2 = det2.raw._mask()
                        else:
                            mask2 = np.ones(det2_shape, dtype=np.uint8)

                        first_event = False
                        yield EventMaskData(
                            data,
                            mask2,
                            seconds2,
                            nanoseconds2,
                            raw_fiducial2,
                            clen2,
                            photon_energy2,
                        )
                    else:
                        yield EventData(
                            data,
                            seconds2,
                            nanoseconds2,
                            raw_fiducial2,
                            clen2,
                            photon_energy2,
                        )
        else:
            from psana import Detector, EventId, MPIDataSource  # type: ignore

            ds: Any = MPIDataSource(f"exp={exp}:run={run_num}:smd")
            det: Any = Detector(self._task_parameters.det_name)
            det.do_reshape_2d_to_3d(flag=True)
            evr: Any = Detector(self._task_parameters.event_receiver)

            if self._task_parameters.n_events != 0:
                ds.break_after(self._task_parameters.n_events)

            for evt in ds.events():
                evt_id: Any = evt.get(EventId)
                seconds: int = evt_id.time()[0]
                nanoseconds: int = evt_id.time()[1]
                fiducials: int = evt_id.fiducials()
                event_codes: Any = evr.eventCodes(evt)

                if isinstance(self._task_parameters.pv_camera_length, float):
                    clen: float = self._task_parameters.pv_camera_length
                else:
                    clen = (
                        ds.env()
                        .epicsStore()
                        .value(self._task_parameters.pv_camera_length)
                    )

                if self._task_parameters.event_logic:
                    if self._task_parameters.event_code not in event_codes:
                        continue

                photon_energy: float
                try:
                    photon_energy = Detector("EBeam").get(evt).ebeamPhotonEnergy()
                    if np.isinf(photon_energy):
                        raise ValueError
                except (AttributeError, ValueError):
                    photon_energy = (
                        1.23984197386209e-06
                        / ds.env().epicsStore().value("SIOC:SYS0:ML00:AO192")
                    ) * 1e9

                data = det.calib(evt)
                if first_event:
                    mask: Optional[npt.NDArray[np.uint8]] = None
                if data:
                    det_shape: Tuple[int, ...] = data.shape
                    if len(det_shape) == 3:
                        det_shape = (det_shape[0] * det_shape[1], det_shape[2])
                    else:
                        det_shape = data.shape

                        if first_event:
                            mask = np.ones(det_shape, dtype=np.uint8)
                            psana_mask = det.mask(
                                self._task_parameters.lute_config.run,
                                calib=False,
                                status=True,
                                edges=False,
                                centra=False,
                                unbond=False,
                                unbondnbrs=False,
                            ).astype(np.uint8)
                            if psana_mask:
                                mask = psana_mask

                            first_event = False

                            yield EventMaskData(
                                data,
                                mask,
                                seconds,
                                nanoseconds,
                                fiducials,
                                clen,
                                photon_energy,
                            )
                        else:
                            yield EventData(
                                data,
                                seconds,
                                nanoseconds,
                                fiducials,
                                clen,
                                photon_energy,
                            )

    def _run(self) -> None:
        rank: int = COMM_WORLD.Get_rank()
        size: int = COMM_WORLD.Get_size()

        i_x, i_y, ipx, ipy = self._retrieve_psana_detector_data()

        alg: Optional[
            Union[
                PyAlgos, _peakfinders_ext.peakfinder_8, _peakfinders_ext.peakfinder_8_v2
            ]
        ] = None
        num_hits: int = 0
        num_events: int = 0
        num_empty_images: int = 0
        tag: str = self._task_parameters.tag

        if (tag != "") and (tag[0] != "_"):
            tag = "_" + tag

        first_event: bool = True
        mask: Optional[npt.NDArray[np.uint8]] = None
        # Need to make this None.... psana2 ranks don't all get into this loop...
        file_writer: Optional[CxiWriter] = None
        powder_hits: Optional[npt.NDArray[np.float64]] = None
        powder_misses: Optional[npt.NDArray[np.float64]] = None
        for event_data in self._event_generator():
            if first_event:
                assert isinstance(event_data, EventMaskData)
                (
                    img,
                    mask,
                    timestamp_seconds,
                    timestamp_nanoseconds,
                    timestamp_fiducials,
                    clen,
                    photon_energy,
                ) = event_data

                first_event = False
            else:
                assert isinstance(event_data, EventData)
                (
                    img,
                    timestamp_seconds,
                    timestamp_nanoseconds,
                    timestamp_fiducials,
                    clen,
                    photon_energy,
                ) = event_data
            if img is None:
                num_empty_images += 1
                continue

            det_shape: Tuple[int, ...] = img.shape
            if len(det_shape) == 3:
                det_shape = (det_shape[0] * det_shape[1], det_shape[2])
            else:
                det_shape = img.shape

            shape: Tuple[int, ...] = img.shape
            img_reshaped: npt.NDArray[np.float32]
            if len(shape) == 2 or self._algo == "Peakfinder8_v2":
                img_reshaped = img
            else:
                img_reshaped = img.reshape(shape[0] * shape[1], shape[2])

            radial_stats: Optional[RadialStatistics] = None
            base_peakfinder_options: Dict[str, Any]
            if alg is None:
                # Mask should only come on first event
                assert mask is not None

                mask_reshaped: npt.NDArray[np.uint8]
                if len(shape) == 2 or self._algo == "Peakfinder8_v2":
                    mask_reshaped = mask
                else:
                    mask_reshaped = mask.reshape(shape[0] * shape[1], shape[2])

                if "Peakfinder8" in self._algo:
                    radius_map: npt.NDArray[np.float64] = self.get_radius_map()
                    adc_thresh = (
                        self._task_parameters.amax_thr
                    )  # Minimum ADC threshold for peak detection
                    hitfinder_min_snr = self._task_parameters.son_min
                    hitfinder_min_pix_count = self._task_parameters.npix_min
                    hitfinder_max_pix_count = self._task_parameters.npix_max
                    local_bg_radius = self._task_parameters.r0

                    if self._algo == "Peakfinder8":
                        alg = _peakfinders_ext.peakfinder_8

                        num_radial_bins: int = self._compute_num_radial_bins(
                            indices=tuple(slice(None, dim) for dim in radius_map.shape),
                            radius_map=radius_map,
                        )
                        radial_stats = self.compute_radial_statistics(
                            radius_map=radius_map,
                            shape=det_shape,
                            num_bins=num_radial_bins,
                        )

                        asic_nx: int  # Size in the fs axis (1 panel/asic)
                        asic_ny: int  # Size in the ss axis (1 panel/asic)
                        nasics_x: int  # Number of asics/panels in the fs axis
                        nasics_y: int  # Number of asics/pnaels in the ss axis
                        if len(shape) > 2:
                            asic_nx = img.shape[2]  # fs size
                            asic_ny = img.shape[1]  # ss size
                            nasics_x = 1  # Number of panels in fs dim
                            nasics_y = img.shape[0]  # Number of panels in ss dim
                        else:
                            asic_nx = img.shape[1]
                            asic_ny = img.shape[0]
                            nasics_x = 1
                            nasics_y = 1
                        base_peakfinder_options = {
                            "max_num_peaks": self._task_parameters.max_peaks,
                            "data": img_reshaped,  # Must be updated each time afterwards
                            "mask": mask_reshaped.astype(np.int8),
                            "pix_r": radial_stats["radial_map"],
                            "rstats_num_pix": radial_stats["rstats_num_pix"],
                            "rstats_pidx": radial_stats["rstats_pixel_index"],
                            "rstats_radius": radial_stats["rstats_radius"],
                            "fast": 1,
                            "asic_nx": asic_nx,
                            "asic_ny": asic_ny,
                            "nasics_x": nasics_x,
                            "nasics_y": nasics_y,
                            "adc_thresh": adc_thresh,
                            "hitfinder_min_snr": hitfinder_min_snr,
                            "hitfinder_min_pix_count": hitfinder_min_pix_count,
                            "hitfinder_max_pix_count": hitfinder_max_pix_count,
                            "hitfinder_local_bg_radius": int(
                                local_bg_radius
                            ),  # Must be int
                        }
                    else:
                        alg = _peakfinders_ext.peakfinder_8_v2

                        base_peakfinder_options = {
                            "max_num_peaks": self._task_parameters.max_peaks,
                            "data": img,  # Must be updated each time afterwards
                            "mask": mask.astype(np.int8),
                            "pix_r": radius_map,
                            "adc_thresh": adc_thresh,
                            "hitfinder_min_snr": hitfinder_min_snr,
                            "hitfinder_min_pix_count": hitfinder_min_pix_count,
                            "hitfinder_max_pix_count": hitfinder_max_pix_count,
                            "hitfinder_local_bg_radius": int(
                                local_bg_radius
                            ),  # Must be int
                        }

                        # Reset the det_shape since we don't use the "slab" in v2
                        det_shape = img.shape
                else:
                    alg = PyAlgos(mask=mask, pbits=0)  # pbits controls verbosity
                    base_peakfinder_options = {
                        "rank": self._task_parameters.peak_rank,
                        "r0": self._task_parameters.r0,
                        "dr": self._task_parameters.dr,
                        "nsigm": self._task_parameters.nsigm,
                    }
                    assert alg is not None
                    alg.set_peak_selection_pars(
                        npix_min=self._task_parameters.npix_min,
                        npix_max=self._task_parameters.npix_max,
                        amax_thr=self._task_parameters.amax_thr,
                        atot_thr=self._task_parameters.atot_thr,
                        son_min=self._task_parameters.son_min,
                    )
                hdffh: Any
                if self._task_parameters.mask_file is not None:
                    with h5py.File(self._task_parameters.mask_file, "r") as hdffh:
                        loaded_mask: npt.NDArray[np.int64] = hdffh[
                            "entry_1/data_1/mask"
                        ][:]
                        mask *= loaded_mask.astype(np.uint8)

                file_writer = CxiWriter(
                    outdir=self._task_parameters.outdir,
                    rank=rank,
                    exp=self._task_parameters.lute_config.experiment,
                    run=int(self._task_parameters.lute_config.run),
                    n_events=self._task_parameters.n_events,
                    det_shape=det_shape,
                    raw_det_shape=img.shape,
                    i_x=i_x,
                    i_y=i_y,
                    ipx=ipx,
                    ipy=ipy,
                    min_peaks=self._task_parameters.min_peaks,
                    max_peaks=self._task_parameters.max_peaks,
                    tag=tag,
                    algo=self._algo,
                )

                libpressio_config: Optional[Dict[str, Any]] = None
                if self._task_parameters.compression is not None:
                    libpressio_config = generate_libpressio_configuration(
                        compressor=self._task_parameters.compression.compressor,
                        roi_window_size=self._task_parameters.compression.roi_window_size,
                        bin_size=self._task_parameters.compression.bin_size,
                        abs_error=self._task_parameters.compression.abs_error,
                        libpressio_mask=(
                            mask_reshaped if self._algo == "Peakfinder8" else mask
                        ),
                    )

                powder_hits = np.zeros(det_shape)
                powder_misses = np.zeros(det_shape)

            peaks: Union[Peakfinder8PeakList, Any]
            num_peaks: int = 0
            assert alg
            if self._algo == "Peakfinder8":
                shape = img.shape
                if len(img.shape) == 2:
                    img_reshaped = img
                else:
                    img_reshaped = img.reshape(shape[0] * shape[1], shape[2])

                # Update the options each time
                base_peakfinder_options["data"] = img_reshaped
                raw_pf8_lists: Tuple[
                    List[float],
                    List[float],
                    List[float],
                    List[int],
                    List[float],
                    List[float],
                    List[float],
                    List[float],
                ] = alg(**base_peakfinder_options)

                pf8_peaks: Peakfinder8PeakList = {
                    "num_peaks": len(raw_pf8_lists[0]),
                    "fs": raw_pf8_lists[0],
                    "ss": raw_pf8_lists[1],
                    "intensity": raw_pf8_lists[2],
                    "num_pixels": raw_pf8_lists[4],
                    "max_pixel_intensity": raw_pf8_lists[5],
                    "snr": raw_pf8_lists[6],
                }

                num_peaks = pf8_peaks["num_peaks"]
                peaks = pf8_peaks
            elif self._algo == "Peakfinder8_v2":
                # Update the options each time
                base_peakfinder_options["data"] = img
                raw_pf8_v2_lists: Tuple[
                    List[float],
                    List[float],
                    List[float],
                    List[int],
                    List[float],
                    List[float],
                    List[float],
                    List[float],
                    List[int],
                ] = alg(**base_peakfinder_options)
                pf8_v2_peaks: Peakfinder8_v2PeakList = {
                    "num_peaks": len(raw_pf8_v2_lists[0]),
                    "fs": raw_pf8_v2_lists[0],
                    "ss": raw_pf8_v2_lists[1],
                    "intensity": raw_pf8_v2_lists[2],
                    "num_pixels": raw_pf8_v2_lists[4],
                    "max_pixel_intensity": raw_pf8_v2_lists[5],
                    "snr": raw_pf8_v2_lists[6],
                    "panel_number": raw_pf8_v2_lists[8],
                }

                num_peaks = pf8_v2_peaks["num_peaks"]
                peaks = pf8_v2_peaks
            else:
                raw_pyalgos_peaks = alg(img, **base_peakfinder_options)
                num_peaks = raw_pyalgos_peaks.shape[0]

                peaks = raw_pyalgos_peaks

            num_events += 1
            if (num_peaks >= self._task_parameters.min_peaks) and (
                num_peaks <= self._task_parameters.max_peaks
            ):

                if self._task_parameters.compression is not None:
                    from libpressio import PressioCompressor  # type: ignore

                    assert libpressio_config
                    libpressio_config_with_peaks = (
                        add_peaks_to_libpressio_configuration(
                            lp_json=libpressio_config,
                            peaks=peaks,
                            algo=self._algo,
                        )
                    )
                    compressor = PressioCompressor.from_config(
                        libpressio_config_with_peaks
                    )
                    # Currently have a bug in libpressio so need 4D array
                    original_shape: Tuple[int, ...]
                    new_shape: Tuple[int, ...]
                    data_for_libpressio: npt.NDArray[np.float32]
                    if self._algo in ("Peakfinder8_v2", "PyAlgos"):
                        original_shape = img.shape
                        if img.ndim < 4:
                            new_shape = ((1,) * (4 - img.ndim)) + img.shape
                            data_for_libpressio = img.reshape(new_shape)
                        else:
                            data_for_libpressio = img
                    else:
                        original_shape = img_reshaped.shape
                        new_shape = (
                            1,
                            1,
                        ) + img_reshaped.shape
                        data_for_libpressio = img_reshaped.reshape(new_shape)

                    compressed_img: bytes = compressor.encode(data_for_libpressio)
                    decompressed_img: npt.NDArray[np.float32] = np.zeros_like(
                        data_for_libpressio
                    )
                    _ = compressor.decode(compressed_img, decompressed_img)
                    if self._algo in ("Peakfinder8_v2", "PyAlgos"):
                        img = decompressed_img.reshape(original_shape)
                    else:
                        img_reshaped = decompressed_img.reshape(original_shape)

                assert file_writer is not None
                file_writer.write_event(
                    img=img_reshaped if self._algo == "Peakfinder8" else img,
                    peaks=peaks,
                    timestamp_seconds=timestamp_seconds,
                    timestamp_nanoseconds=timestamp_nanoseconds,
                    timestamp_fiducials=timestamp_fiducials,
                    photon_energy=photon_energy,
                    clen=clen,
                    algo=self._algo,
                )
                num_hits += 1

            if num_peaks >= self._task_parameters.min_peaks:
                assert powder_hits is not None
                powder_hits = np.maximum(
                    powder_hits,
                    img.reshape(det_shape),
                )
            else:
                assert powder_misses is not None
                powder_misses = np.maximum(
                    powder_misses,
                    img.reshape(det_shape),
                )

        if num_empty_images != 0:
            msg: Message = Message(
                contents=f"Rank {rank} encountered {num_empty_images} empty images."
            )
            self._report_to_executor(msg)

        if file_writer is not None:
            assert mask is not None
            assert powder_hits is not None
            assert powder_misses is not None
            file_writer.write_non_event_data(
                powder_hits=powder_hits,
                powder_misses=powder_misses,
                mask=mask,
            )

            file_writer.optimize_and_close_file(
                num_hits=num_hits,
                max_peaks=self._task_parameters.max_peaks,
                algo=self._algo,
            )

        COMM_WORLD.Barrier()

        num_hits_per_rank: List[int] = cast(
            List[int], COMM_WORLD.gather(num_hits, root=0)
        )
        num_hits_total: int = cast(int, COMM_WORLD.reduce(num_hits, SUM))
        num_events_total: int = cast(int, COMM_WORLD.reduce(num_events, SUM))

        if rank == 0:
            master_fname: Path = write_master_file(
                mpi_size=size,
                outdir=self._task_parameters.outdir,
                exp=self._task_parameters.lute_config.experiment,
                run=int(self._task_parameters.lute_config.run),
                tag=tag,
                n_hits_per_rank=num_hits_per_rank,
                n_hits_total=num_hits_total,
            )

            # Write final summary file
            f: Union[TextIO, h5py.File]
            with open(
                Path(self._task_parameters.outdir) / f"peakfinding{tag}.summary", "w"
            ) as f:
                print(f"Number of events processed: {num_events_total}", file=f)
                print(f"Number of hits found: {num_hits_total}", file=f)
                print(
                    "Fractional hit rate: " f"{(num_hits_total/num_events_total):.2f}",
                    file=f,
                )
                print(f"No. hits per rank: {num_hits_per_rank}", file=f)

            with h5py.File(master_fname, "r") as f:
                final_powder_hits: npt.NDArray[np.float64] = f[
                    "entry_1/data_1/powderHits"
                ][:]
                final_powder_misses: npt.NDArray[np.float64] = f[
                    "entry_1/data_1/powderMisses"
                ][:]
                f.close()

            text_summary: Dict[str, str] = {
                "Number of events processed": str(num_events_total),
                "Number of hits found": str(num_hits_total),
                "Fractional hit rate": f"{num_hits_total/num_events_total:.2f}",
            }

            task_summary: Union[Dict[str, str], Tuple[Dict[str, str], ElogSummaryPlots]]
            if self._task_parameters.make_powder_plots:
                powder_plots: pn.Tabs = self._create_powder_plots(
                    powder_hits=final_powder_hits.astype(np.float64),
                    powder_misses=final_powder_misses.astype(np.float64),
                    i_x=i_x,
                    i_y=i_y,
                )
                task_summary = (
                    text_summary,
                    ElogSummaryPlots(
                        f"r{self._task_parameters.lute_config.run}/powders",
                        powder_plots,
                    ),
                )
            else:
                task_summary = text_summary

            self._result.summary = task_summary
            with open(Path(self._task_parameters.out_file), "w") as f:
                print(f"{master_fname}", file=f)

    def _post_run(self) -> None:
        super()._post_run()
        self._result.task_status = TaskStatus.COMPLETED

    def _assemble_image(
        self,
        img: npt.NDArray[np.float64],
        i_x: npt.NDArray[np.uint64],
        i_y: npt.NDArray[np.uint64],
    ) -> npt.NDArray[np.float64]:
        """Assemble an image based on psana geometry.

        Args:
            img (np.ndarray[np.float64]): The image to assemble. Should
                generally be of shape (n_panels, ss, fs)

            i_x (Any): Array of pixel indexes along x

            i_y (Any): Array of pixel indexes along y

        Returns:
            assembled_img(np.ndarray[np.float64]): Assembled 2D image.
        """
        img_reshaped: npt.NDArray[np.float64] = img.reshape(i_x.shape)
        idx_max_y: int = int(np.max(i_x) + 1)
        idx_max_x: int = int(np.max(i_y) + 1)
        assembled_img: npt.NDArray[np.float64] = np.zeros(
            (idx_max_y, idx_max_x), dtype=np.float64
        )
        assembled_img[i_x, i_y] = img_reshaped

        return assembled_img

    def _create_powder_plots(
        self,
        powder_hits: npt.NDArray[np.float64],
        powder_misses: npt.NDArray[np.float64],
        i_x: npt.NDArray[np.uint64],
        i_y: npt.NDArray[np.uint64],
    ) -> pn.Tabs:
        """Create a tabbed display of hits and misses 'powder' plots.

        Args:
            powder_hits (np.ndarray[np.float64]): Total max/sum projection of
                hits across the run.

            powder_misses (np.ndarray[np.float64]): Total max/sum projection of
                misses across the run.

            i_x (Any): Array of pixel indexes along x

            i_y (Any): Array of pixel indexes along y

        Returns:
            tabs (pn.Tabs): Tabbed display of the image plots.
        """
        assembled_powder_hits: npt.NDArray[np.float64] = self._assemble_image(
            img=powder_hits, i_x=i_x, i_y=i_y
        )
        assembled_powder_misses: npt.NDArray[np.float64] = self._assemble_image(
            img=powder_misses, i_x=i_x, i_y=i_y
        )

        grid_hits: pn.GridSpec = pn.GridSpec(
            sizing_mode="stretch_both",
            max_width=700,
            name=f"{self._task_parameters.det_name} - Hits",
        )
        dim: hv.Dimension = hv.Dimension(
            ("image", "Hits"),
            range=(
                np.nanpercentile(assembled_powder_hits, 1),
                np.nanpercentile(assembled_powder_hits, 99),
            ),
        )
        grid_hits[0, 0] = pn.Row(
            hv.Image(assembled_powder_hits, vdims=[dim], name=dim.label).options(
                colorbar=True, cmap="rainbow"
            )
        )

        grid_misses: pn.GridSpec = pn.GridSpec(
            sizing_mode="stretch_both",
            max_width=700,
            name=f"{self._task_parameters.det_name} - Misses",
        )
        dim = hv.Dimension(
            ("image", "Misses"),
            range=(
                np.nanpercentile(assembled_powder_misses, 1),
                np.nanpercentile(assembled_powder_misses, 99),
            ),
        )
        grid_misses[0, 0] = pn.Row(
            hv.Image(assembled_powder_misses, vdims=[dim], name=dim.label).options(
                colorbar=True, cmap="rainbow"
            )
        )

        tabs: pn.Tabs = pn.Tabs(grid_hits)
        tabs.append(grid_misses)
        return tabs

_assemble_image(img, i_x, i_y)

Assemble an image based on psana geometry.

Parameters:

Name Type Description Default
img ndarray[float64]

The image to assemble. Should generally be of shape (n_panels, ss, fs)

required
i_x Any

Array of pixel indexes along x

required
i_y Any

Array of pixel indexes along y

required

Returns:

Name Type Description
assembled_img ndarray[float64]

Assembled 2D image.

Source code in lute/tasks/sfx_find_peaks.py
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
def _assemble_image(
    self,
    img: npt.NDArray[np.float64],
    i_x: npt.NDArray[np.uint64],
    i_y: npt.NDArray[np.uint64],
) -> npt.NDArray[np.float64]:
    """Assemble an image based on psana geometry.

    Args:
        img (np.ndarray[np.float64]): The image to assemble. Should
            generally be of shape (n_panels, ss, fs)

        i_x (Any): Array of pixel indexes along x

        i_y (Any): Array of pixel indexes along y

    Returns:
        assembled_img(np.ndarray[np.float64]): Assembled 2D image.
    """
    img_reshaped: npt.NDArray[np.float64] = img.reshape(i_x.shape)
    idx_max_y: int = int(np.max(i_x) + 1)
    idx_max_x: int = int(np.max(i_y) + 1)
    assembled_img: npt.NDArray[np.float64] = np.zeros(
        (idx_max_y, idx_max_x), dtype=np.float64
    )
    assembled_img[i_x, i_y] = img_reshaped

    return assembled_img

_create_powder_plots(powder_hits, powder_misses, i_x, i_y)

Create a tabbed display of hits and misses 'powder' plots.

Parameters:

Name Type Description Default
powder_hits ndarray[float64]

Total max/sum projection of hits across the run.

required
powder_misses ndarray[float64]

Total max/sum projection of misses across the run.

required
i_x Any

Array of pixel indexes along x

required
i_y Any

Array of pixel indexes along y

required

Returns:

Name Type Description
tabs Tabs

Tabbed display of the image plots.

Source code in lute/tasks/sfx_find_peaks.py
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
def _create_powder_plots(
    self,
    powder_hits: npt.NDArray[np.float64],
    powder_misses: npt.NDArray[np.float64],
    i_x: npt.NDArray[np.uint64],
    i_y: npt.NDArray[np.uint64],
) -> pn.Tabs:
    """Create a tabbed display of hits and misses 'powder' plots.

    Args:
        powder_hits (np.ndarray[np.float64]): Total max/sum projection of
            hits across the run.

        powder_misses (np.ndarray[np.float64]): Total max/sum projection of
            misses across the run.

        i_x (Any): Array of pixel indexes along x

        i_y (Any): Array of pixel indexes along y

    Returns:
        tabs (pn.Tabs): Tabbed display of the image plots.
    """
    assembled_powder_hits: npt.NDArray[np.float64] = self._assemble_image(
        img=powder_hits, i_x=i_x, i_y=i_y
    )
    assembled_powder_misses: npt.NDArray[np.float64] = self._assemble_image(
        img=powder_misses, i_x=i_x, i_y=i_y
    )

    grid_hits: pn.GridSpec = pn.GridSpec(
        sizing_mode="stretch_both",
        max_width=700,
        name=f"{self._task_parameters.det_name} - Hits",
    )
    dim: hv.Dimension = hv.Dimension(
        ("image", "Hits"),
        range=(
            np.nanpercentile(assembled_powder_hits, 1),
            np.nanpercentile(assembled_powder_hits, 99),
        ),
    )
    grid_hits[0, 0] = pn.Row(
        hv.Image(assembled_powder_hits, vdims=[dim], name=dim.label).options(
            colorbar=True, cmap="rainbow"
        )
    )

    grid_misses: pn.GridSpec = pn.GridSpec(
        sizing_mode="stretch_both",
        max_width=700,
        name=f"{self._task_parameters.det_name} - Misses",
    )
    dim = hv.Dimension(
        ("image", "Misses"),
        range=(
            np.nanpercentile(assembled_powder_misses, 1),
            np.nanpercentile(assembled_powder_misses, 99),
        ),
    )
    grid_misses[0, 0] = pn.Row(
        hv.Image(assembled_powder_misses, vdims=[dim], name=dim.label).options(
            colorbar=True, cmap="rainbow"
        )
    )

    tabs: pn.Tabs = pn.Tabs(grid_hits)
    tabs.append(grid_misses)
    return tabs

add_peaks_to_libpressio_configuration(lp_json, peaks, algo)

Add peak infromation to libpressio configuration

Parameters:

lp_json: Dictionary storing the configuration JSON structure for the libpressio
    library.

peaks (Any): Peak information as returned by psana.

Returns:

lp_json: Updated configuration JSON structure for the libpressio library.
Source code in lute/tasks/sfx_find_peaks.py
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
def add_peaks_to_libpressio_configuration(
    lp_json: Dict[str, Any],
    peaks: Union[Peakfinder8PeakList, Peakfinder8_v2PeakList, Any],
    algo: Literal["PyAlgos", "Peakfinder8", "Peakfinder8_v2"],
) -> Dict[str, Any]:
    """
    Add peak infromation to libpressio configuration

    Parameters:

        lp_json: Dictionary storing the configuration JSON structure for the libpressio
            library.

        peaks (Any): Peak information as returned by psana.

    Returns:

        lp_json: Updated configuration JSON structure for the libpressio library.
    """
    if "Peakfinder8" in algo:
        return _libpressio_config_pf8(lp_json=lp_json, peaks=peaks)
    else:
        return _libpressio_config_pyalgos(lp_json=lp_json, peaks=peaks)

generate_libpressio_configuration(compressor, roi_window_size, bin_size, abs_error, libpressio_mask)

Create the configuration JSON for the libpressio library

Parameters:

compressor (Literal["sz3", "qoz"]): Compression algorithm to use
    ("qoz" or "sz3").

abs_error (float): Bound value for the absolute error.

bin_size (int): Bining Size.

roi_window_size (int): Default size of the ROI window.

libpressio_mask (npt.NDArray): mask to be applied to the data.

Returns:

lp_json (Dict[str, Any]): Dictionary storing the JSON configuration structure
for the libpressio library
Source code in lute/tasks/sfx_find_peaks.py
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
def generate_libpressio_configuration(
    compressor: Literal["sz3", "qoz"],
    roi_window_size: int,
    bin_size: int,
    abs_error: float,
    libpressio_mask,
) -> Dict[str, Any]:
    """
    Create the configuration JSON for the libpressio library

    Parameters:

        compressor (Literal["sz3", "qoz"]): Compression algorithm to use
            ("qoz" or "sz3").

        abs_error (float): Bound value for the absolute error.

        bin_size (int): Bining Size.

        roi_window_size (int): Default size of the ROI window.

        libpressio_mask (npt.NDArray): mask to be applied to the data.

    Returns:

        lp_json (Dict[str, Any]): Dictionary storing the JSON configuration structure
        for the libpressio library
    """

    if compressor == "qoz":
        pressio_opts: Dict[str, Any] = {
            "pressio:abs": abs_error,
            "qoz": {"qoz:stride": 8},
        }
    elif compressor == "sz3":
        pressio_opts = {"pressio:abs": abs_error}

    ndims: int = len(libpressio_mask.shape)

    # Add padding for non-row/col dims
    other_dims: List[int] = [1] * (ndims - 2)
    roi_size: List[int] = other_dims * (ndims - 2) + [roi_window_size, roi_window_size]
    bin_dims: List[int] = other_dims * (ndims - 2) + [bin_size, bin_size]

    # Libpressio bug requires 4D data
    if len(roi_size) < 4:
        other_dims = [1] * (4 - len(roi_size))
        roi_size = other_dims + roi_size
        bin_dims = other_dims + bin_dims

    lp_json: Dict[str, Any] = {
        "compressor_id": "pressio",
        "early_config": {
            "pressio": {
                "pressio:compressor": "roibin",
                "roibin": {
                    "roibin:metric": "composite",
                    "roibin:background": "mask_binning",
                    "roibin:roi": "fpzip",
                    "background": {
                        "binning:compressor": "pressio",
                        "mask_binning:compressor": "pressio",
                        "pressio": {"pressio:compressor": compressor},
                    },
                    "composite": {
                        "composite:plugins": [
                            "size",
                            "time",
                            "input_stats",
                            "error_stat",
                        ]
                    },
                },
            }
        },
        "compressor_config": {
            "pressio": {
                "roibin": {
                    "roibin:roi_size": roi_size,
                    "roibin:centers": None,
                    "roibin:roi_strategy": "coordinates",
                    "roibin:nthreads": 1,
                    "roi": {"fpzip:prec": 0},
                    "background": {
                        "mask_binning:mask": None,
                        "mask_binning:shape": bin_dims,
                        "mask_binning:nthreads": 4,
                        "pressio": pressio_opts,
                    },
                }
            }
        },
        "name": "pressio",
    }

    # Need 4D array for libpressio bug
    reshaped_mask: npt.NDArray[np.uint8]
    if libpressio_mask.ndim < 4:
        new_shape: Tuple[int, ...] = (
            (1,) * (4 - libpressio_mask.ndim)
        ) + libpressio_mask.shape
        reshaped_mask = libpressio_mask.reshape(new_shape)
    else:
        reshaped_mask = libpressio_mask

    lp_json["compressor_config"]["pressio"]["roibin"]["background"][
        "mask_binning:mask"
    ] = (1 - reshaped_mask)

    return lp_json

write_master_file(mpi_size, outdir, exp, run, tag, n_hits_per_rank, n_hits_total)

Generate a virtual dataset to map all individual files for this run.

Parameters:

mpi_size (int): Number of ranks in the MPI pool.

outdir (str): Output directory for cxi file.

exp (str): Experiment string.

run (int): Experimental run.

tag (str): Tag to append to cxi file names.

n_hits_per_rank (List[int]): Array containing the number of hits found on each
    node processing data.

n_hits_total (int): Total number of hits found across all nodes.

Returns:

The path to the the written master file
Source code in lute/tasks/sfx_find_peaks.py
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
def write_master_file(
    mpi_size: int,
    outdir: str,
    exp: str,
    run: int,
    tag: str,
    n_hits_per_rank: List[int],
    n_hits_total: int,
) -> Path:
    """
    Generate a virtual dataset to map all individual files for this run.

    Parameters:

        mpi_size (int): Number of ranks in the MPI pool.

        outdir (str): Output directory for cxi file.

        exp (str): Experiment string.

        run (int): Experimental run.

        tag (str): Tag to append to cxi file names.

        n_hits_per_rank (List[int]): Array containing the number of hits found on each
            node processing data.

        n_hits_total (int): Total number of hits found across all nodes.

    Returns:

        The path to the the written master file
    """
    # Retrieve paths to the files containing data
    fnames: List[Path] = []
    # Need to keep track of these for indexing later during master file creation
    ranks_with_hits: List[int] = []
    for fi in range(mpi_size):
        if n_hits_per_rank[fi] > 0:
            fnames.append(Path(outdir) / f"{exp}_r{run:0>4}_{fi}{tag}.cxi")
            ranks_with_hits.append(fi)
    if len(fnames) == 0:
        sys.exit("No hits found")

    # Retrieve list of entries to populate in the virtual hdf5 file
    dname_list, key_list, shape_list, dtype_list = [], [], [], []
    datasets = ["/entry_1/result_1", "/LCLS/detector_1", "/LCLS", "/entry_1/data_1"]
    f = h5py.File(fnames[0], "r")
    for dname in datasets:
        dset = f[dname]
        for key in dset.keys():
            if f"{dname}/{key}" not in datasets:
                dname_list.append(dname)
                key_list.append(key)
                shape_list.append(dset[key].shape)
                dtype_list.append(dset[key].dtype)
    f.close()

    # Compute cumulative powder hits and misses for all files
    # Copy mask as well
    mask: Optional[npt.NDArray[np.uint8]] = None
    powder_hits: Optional[npt.NDArray[np.float64]] = None
    powder_misses: Optional[npt.NDArray[np.float64]] = None
    for fn in fnames:
        f = h5py.File(fn, "r")
        if mask is None:
            mask = f["entry_1/data_1/mask"][:].copy()
        if powder_hits is None:
            powder_hits = f["entry_1/data_1/powderHits"][:].copy()
            powder_misses = f["entry_1/data_1/powderMisses"][:].copy()
        else:
            assert powder_misses is not None
            powder_hits = np.maximum(
                powder_hits, f["entry_1/data_1/powderHits"][:].copy()
            )
            powder_misses = np.maximum(
                powder_misses, f["entry_1/data_1/powderMisses"][:].copy()
            )
        f.close()

    vfname: Path = Path(outdir) / f"{exp}_r{run:0>4}{tag}.cxi"
    with h5py.File(vfname, "w") as vdf:
        # Write the virtual hdf5 file
        for dnum in range(len(dname_list)):
            dname = f"{dname_list[dnum]}/{key_list[dnum]}"
            if key_list[dnum] not in ["mask", "powderHits", "powderMisses"]:
                layout = h5py.VirtualLayout(
                    shape=(n_hits_total,) + shape_list[dnum][1:], dtype=dtype_list[dnum]
                )
                cursor = 0
                for i, fn in enumerate(fnames):
                    # Use the correct rank hit count or you get a malformed VDS
                    rank_idx: int = ranks_with_hits[i]
                    vsrc = h5py.VirtualSource(
                        fn,
                        dname,
                        shape=(n_hits_per_rank[rank_idx],) + shape_list[dnum][1:],
                    )

                    if len(shape_list[dnum]) == 1:
                        layout[cursor : cursor + n_hits_per_rank[rank_idx]] = vsrc
                    else:
                        layout[cursor : cursor + n_hits_per_rank[rank_idx], :] = vsrc
                    cursor += n_hits_per_rank[rank_idx]
                vdf.create_virtual_dataset(dname, layout, fillvalue=-1)

        vdf["entry_1/data_1/powderHits"] = powder_hits
        vdf["entry_1/data_1/powderMisses"] = powder_misses
        vdf["entry_1/data_1/mask"] = mask

    return vfname