API reference¶
Pipeline¶
- class probixi.Probixi¶
Self-calibrating probabilistic peak finder and indexer.
Observe the detector noise on a slice of seed frames, calibrate a probabilistic detector from it (peak/noise scale
kappa, peak prior, the pixel/radial/panel mean blend, a variance scale pinning the background toN(0, 1), and the matched-filter detection threshold), then stream frames to emit indexed, merge-ready results.- Parameters:
list_file (
Union[str,Path]) – CrystFEL.lstlist,.geomgeometry and.cellunit-cell inputs.cell_filemay be omitted for peak-only usegeometry_file (
Union[str,Path]) – CrystFEL.lstlist,.geomgeometry and.cellunit-cell inputs.cell_filemay be omitted for peak-only usecell_file (
Union[str,Path,None]) – CrystFEL.lstlist,.geomgeometry and.cellunit-cell inputs.cell_filemay be omitted for peak-only usenoise_mode (
Literal['per_frame','online']) – Whether the running noise model keeps updating per frame (“online”, tracking slow drift) or resets each frame.warmup_frames (
int) – Frames observed before the dead-pixel mask is committed.finder_kappa (
float) – Pre-calibration detector defaults;calibrateoverrideskappaand the peak prior with learned values.posterior_threshold (
float) – Pre-calibration detector defaults;calibrateoverrideskappaand the peak prior with learned values.candidate_threshold (
Optional[float]) – Pre-calibration detector defaults;calibrateoverrideskappaand the peak prior with learned values.matched_filter (
bool) – Use the multi-scale matched filter (the recommended operating point).mf_scales (
tuple[float,...]) – Matched-filter kernel scales and the fallback threshold (learned bycalibratewhentarget_noise_peaksis set).mf_threshold (
float) – Matched-filter kernel scales and the fallback threshold (learned bycalibratewhentarget_noise_peaksis set).flux_variance (
bool) – Replace the frozen variance floor with a learned photon-transfer curve so dim/low-flux shots are whitened against their own Poisson noise. Opt-in; intended for XFEL/SFX or jet-intensity-variable data.seed (
Optional[SeedConfig]) – Optional indexer configuration objects.refine (
Optional[RefineConfig]) – Optional indexer configuration objects.cell_match (
Optional[CellMatchConfig]) – Optional indexer configuration objects.integrate (
Optional[IntegrateConfig]) – Optional indexer configuration objects.device (
Optional[device]) – Torch device and frame dtype.dtype (
dtype) – Torch device and frame dtype.
- threshold_calibration¶
The fitted matched-filter threshold result, set by
calibrate.- Type:
ThresholdCalibration or None
- property metadata: Metadata¶
Parsed run metadata (frames, geometry, cell).
- property target_cell: CellParams¶
Target unit cell that accepted orientations must match.
- property noise: NoiseModel¶
The live noise model (built lazily on the first frame seen).
- property finder: PeakFinder¶
The peak finder over the noise model (built lazily on the first frame).
- frames(start=None, stop=None, batch_size=1, prefetch=2)¶
Stream frames off disk as device tensors, prefetching reads.
- Parameters:
start (
Optional[int]) – Half-open absolute frame range over the run; full run if omitted.stop (
Optional[int]) – Half-open absolute frame range over the run; full run if omitted.batch_size (
int) – Frames stacked per yielded tensor (1 yields single frames).prefetch (
int) – Batches read ahead on a background thread to overlap I/O.
- Returns:
Frames on the configured device and dtype.
- Return type:
Iterator[Tensor]
- show_frame(frame, *, path, peaks=True, predictions=True, update_noise=False, **render_kwargs)¶
Render one frame with its detected peaks and indexed reflections.
Resolves
frameto an absolute index, reads it off disk, runs peak finding (and indexing, when a target cell is configured) on that single frame, and writes an overlay image. Callcalibrate()first for production-quality detection.- Parameters:
frame (
Union[int,str,tuple]) – Absolute frame index, an"image_filename//event"string, or a(filename, event)pair.peaks (
bool) – Overlay detected peaks.predictions (
bool) – Overlay the predicted lattice when the frame indexes.update_noise (
bool) – Fold this frame into the live noise model (off by default so recalling a frame does not perturb pipeline state).**render_kwargs – Forwarded to
probixi.io.render_frame()(title,cmap,vmax_pct, …).
- Returns:
The written image path.
- Return type:
- fit_noise(frames)¶
Observe
framesto warm the noise model (builds it on the first).- Returns:
Self, for chaining.
- Return type:
- noise_diagnostics(path, *, frames=None, start=None, stop=None, batch_size=15, **kwargs)¶
Write a noise-model diagnostic GIF, driving the live model.
Streams
frames(or a[start, stop)slice of the run) through the pipeline’s live noise model, snapshotting after everybatch_sizeframes, and animates the running mean background, its radial profile, and the per-batch drift. The model is built lazily (with the geometry-aware bad-pixel mask) if it has not been seeded yet.Because it drives the live model, every frame is folded into the running stats: run it before
calibrateto watch warmup, or on a freshProbixito inspect a run’s drift. One mean-image snapshot is retained per batch, so prefer a bounded slice for long runs.- Parameters:
frames (
Optional[Iterable[Tensor]]) – Explicit frames; defaults to the[start, stop)run slice.start (
Optional[int]) – Half-open frame range whenframesis not given.stop (
Optional[int]) – Half-open frame range whenframesis not given.batch_size (
int) – Frames folded in between animation snapshots.**kwargs – Forwarded to the noise model’s diagnostics (
fps,cmap,dpi,max_radius,figsize).
- Returns:
The written GIF path.
- Return type:
- calibrate(n_seed=32, seed_frames=None, eigen_modes=0, target_noise_peaks=5.0, threshold_opts=None, **opts)¶
Calibrate the detector on seed frames, then freeze the learned params.
Warms the noise model, then learns the peak/noise scale
kappaand peak prior (EM mixture) and the mean blend + variance scale that pin the backgroundztoN(0, 1). Withtarget_noise_peaksset (the default), also calibrates the matched-filter threshold so a signal-free frame yields at most that many noise blobs, and installs it on the finder. Withflux_varianceenabled on the pipeline, also fits the photon-transfer curve.- Parameters:
n_seed (
int) – Leading frames to calibrate on whenseed_framesis not given.seed_frames (
Optional[Iterable[Tensor]]) – Explicit calibration frames; overridesn_seed.eigen_modes (
int) – If > 0, also fit this many low-rank background modes (XFEL/SFX).target_noise_peaks (
Optional[float]) – Matched-filter operating point (expected noise blobs per signal-free frame).Noneskips threshold calibration. 5 ~mf_threshold5.5.threshold_opts (
Optional[dict]) – Extra keyword arguments forwarded tocalibrate_threshold.**opts – Extra keyword arguments forwarded to
calibrate_noise.
- Returns:
The applied noise calibration.
- Return type:
CalibrationResult
- property beamstop_min_res: float | None¶
Effective low-resolution cutoff (A) excluding the beam center, or None.
- peak_stream(frames, start_index=0, update_noise=True, estimate_scale=True)¶
Open a lazy stream of per-frame peak results.
- Parameters:
frames (
Iterable[Tensor]) – Frames to search; the first triggers lazy model construction.start_index (
int) – Absolute index assigned to the first frame (so results carry true run indices when starting partway through).update_noise (
bool) – Fold each frame into the running noise model as it passes.estimate_scale (
bool) – Estimate the per-frame fluence scale (when a calibration reference exists) soindex_streamcan attach it to each solution. SetFalsefor peak-only use to skip the per-frame regression and avoid accumulating scales that are never consumed.
- Returns:
Lazy, composable stream of
PeakResult(torch-resident).- Return type:
PeakStream
- index_stream(frames, batch_size=8, start_index=0, update_noise=True)¶
Open a lazy stream of indexing solutions over
frames.Runs the full pipeline per frame: detect peaks, lift to reciprocal space, seed and refine an orientation whose cell matches the target, then predict and integrate the lattice.
- Parameters:
- Returns:
Lazy stream of
IndexResult, one per indexed frame.- Return type:
- scale_stream(frames, start_index=0)¶
Infer the per-frame relative intensity scale, frame by frame.
Each frame is regressed against the background reference frozen at
calibrate(so online drift-tracking does not contaminate the scale), yieldingg_fand its uncertainty – the shot-to-shot fluence scale that dominates at an XFEL and that initializes downstream per-pattern scaling. Requirescalibrateto have been run.
- __init__(list_file, geometry_file, cell_file=None, noise_mode='online', warmup_frames=16, finder_kappa=10.0, posterior_threshold=0.5, candidate_threshold=None, matched_filter=True, mf_scales=(1.0, 1.6, 2.4), mf_threshold=5.0, flux_variance=False, flux_var_floor=0.15, seed=None, refine=None, cell_match=None, integrate=None, device=None, dtype=torch.float32)¶
Streams and results¶
- class probixi.indexer.IndexStream¶
Lazy, composable stream of per-frame :class:`IndexResult`s.
A torch-iterable produced by
Indexer.index_stream(). Operators (map/filter/tap) compose lazily; terminals (collect,to_stream,count, …) drive the underlying generator once.The
statsfunnel (frames/hits/indexed) is shared across composed operators, so counts stay live no matter how the stream is wrapped.- __init__(source, stats=None)¶
- map(fn)¶
- Return type:
- filter(predicate)¶
- Return type:
- tap(fn)¶
- Return type:
- enrich_gate(alpha=0.001)¶
Drop solutions whose predicted spots are not backed by image signal.
- Parameters:
alpha (
float) – Max chance probability to accept a frame (a per-frame false-discovery level). Smaller is stricter.- Returns:
Filtered stream.
- Return type:
- to_db(path, *, geometry, cell=None, geometry_file=None, files=None, panel='0')¶
Drain the stream into a DuckDB database at
path.Convenience terminal wrapping
DuckDBOffloader: opens the database, writes thegeometry/panels/cellmetadata tables, and streams every result into theframes/reflections/peakstables.- Parameters:
path (str or Path) – Destination
.duckdbfile (overwritten if present).geometry (
dict) – Indexer geometry dict (beam_center,clen, …).cell (
Optional[CellParams]) – Target unit cell, written to thecelltable.geometry_file (str or Path, optional) – Geometry file whose text is stored verbatim.
files (
Optional[dict]) – Loader file map; enables the non-indexed frame backfill.panel (
str) – Fallback panel name for out-of-panel peaks/reflections.
- Returns:
Number of indexed frames written.
- Return type:
- collect()¶
- Return type:
- class probixi.indexer.IndexResult¶
Indexing solution for a single frame.
- A¶
(3, 3) reciprocal-to-lab matrix,
A = U @ B.- Type:
Tensor
- U¶
(3, 3) crystal orientation (rotation).
- Type:
Tensor
- B¶
(3, 3) cell-only reciprocal basis.
- Type:
Tensor
- cell¶
Unit cell recovered from
B.- Type:
CellParams
- indexed_mask¶
(N,) bool mask of which peaks were indexed.
- Type:
Tensor
- hkl¶
(N, 3) integer Miller indices assigned to each peak.
- Type:
Tensor
- positions¶
(N, 2) observed peak
(row, col)positions, aligned withhkl.- Type:
Tensor
- intensities¶
(N,) integrated peak intensity, aligned with
positions.- Type:
Tensor
- sigmas¶
(N,) 1-sigma uncertainty on each intensity, aligned with
positions.- Type:
Tensor
- loss_history¶
Per-iteration refinement loss (excluded from repr).
- Type:
Tensor
- predicted_hkl¶
(M, 3) Miller indices of all reflections predicted to diffract on this frame, or None if prediction/integration was not run.
- Type:
Tensor, optional
- predicted_positions¶
(M, 2) predicted detector
(row, col)positions.- Type:
Tensor, optional
- predicted_intensities¶
(M,) box-integrated intensity per predicted reflection (snapped to the observed centroid where a peak coincides).
- Type:
Tensor, optional
- predicted_sigmas¶
(M,) 1-sigma uncertainty on each predicted intensity.
- Type:
Tensor, optional
- predicted_peak¶
(M,) max background-subtracted pixel in each box (spot height; the CrystFEL stream’s
peakcolumn).- Type:
Tensor, optional
- predicted_background¶
(M,) mean per-pixel noise background under each box (the CrystFEL stream’s
backgroundcolumn; informational only).- Type:
Tensor, optional
- diffraction_limit¶
Per-crystal resolution limit (nm^-1) from integrated I/sigma; reflections beyond it are dropped and it is written as the stream’s
diffraction_resolution_limit.- Type:
float, optional
- enrichment¶
Bright-rate of predicted spots over the background bright-rate (see
spot_enrichment); ~1 is a noise indexing, >>1 a real lattice.- Type:
float, optional
- enrich_p¶
Chance probability of this many bright predicted spots under the noise-null (
spot_enrichment); the gate keeps frames with smallenrich_p. Self-calibrating: no enrichment cut to choose.- Type:
float, optional
- scale¶
Per-frame relative intensity scale (background fluence vs the calibration reference), if inferred; an initialization for downstream per-pattern scaling/merging.
- Type:
float, optional
- __init__(frame_index, n_peaks, n_indexed, rmsd, A, U, B, cell, indexed_mask, hkl, positions, intensities, sigmas, loss_history, predicted_hkl=None, predicted_positions=None, predicted_intensities=None, predicted_sigmas=None, predicted_peak=None, predicted_background=None, diffraction_limit=None, enrichment=None, n_bright=None, enrich_p=None, scale=None, scale_sigma=None, mosaicity=None, profile_radius=None)¶
Output writers¶
- class probixi.DuckDBOffloader¶
Write ``IndexResult``s to a DuckDB database.
A relational alternative to the CrystFEL
.stream: run metadata lands in smallgeometry/panels/celltables, every file-event becomes a row inframes(flagged indexed or not, with its per-frame statistics), and the integratedreflectionsand searchedpeaksare stored keyed by the frame’sframe_id().Same interface as
DataOffloader, so it drops into the same driver loop:with DuckDBOffloader(out, geometry=geom, cell=cell, files=files) as off: stream.to_stream(off)
or, more directly, via
to_db().When
filesis supplied every file-event is enumerated, so frames that never indexed are recorded withindexed = FALSEand null statistics; the indexed rate is thenAVG(indexed::INT)overframes. Withoutfilesonly indexed frames are written.- Parameters:
path (
Union[str,Path]) – Destination.duckdbfile. Overwritten if it exists.geometry (
dict) – Indexer geometry (beam_center,clen,pixel_size,wavelength,adu_per_photon, and – when available –panels).cell (
Optional[CellParams]) – Target unit cell; written to thecelltable and used for any symmetry labels.geometry_file (
Union[str,Path,None]) – Geometry file whose text is stored verbatim ingeometry.geometry_file.files (
Optional[dict]) – Loader file map, used both to resolve a global frame index to its source file/event and to enumerate the non-indexed frames.frame_range (
Optional[tuple[int,int]]) – Half-open[lo, hi)global-frame-index range this writer is responsible for. When set, the non-indexed backfill is restricted to this range – required when only a sub-range is processed (--start/--stop) or when several writers each cover a disjoint block (the multi-GPU path), so the whole dataset is not marked non-indexed by every writer.Nonebackfills every file-event.indexer_name (
str) – Recorded for provenance parity with the stream writer (unused in the DB).panel (
str) – Fallback panel name for peaks/reflections outside every geometry panel.
- __init__(path, geometry, *, cell=None, geometry_file=None, files=None, frame_range=None, indexer_name='probixi', panel='0')¶
- class probixi.DataOffloader¶
Write
IndexResult``s to a CrystFEL-style ``.streamfile.Use as a context manager; the instance is itself the per-frame writer expected by
IndexStream.to_stream(__call__forwards towrite):with DataOffloader(out, geometry=geom, cell=cell, files=files) as off: stream.to_stream(off)
Please be aware that this functionality may be deprecated in the future.
- Parameters:
geometry (
dict) – Indexer geometry (beam_center,clen,pixel_size,wavelength, and – when available – the parsedpanels).cell (
Optional[CellParams]) – Target unit cell; supplies the symmetry labels (lattice_type, centering, unique_axis). Per-crystal cell parameters are recovered from each solution’s orientation matrix.geometry_file (
Union[str,Path,None]) – Geometry file embedded verbatim in the stream header when present.files (
Optional[dict]) – Loader file map, used to resolve a global frame index back to its source file and within-file event number.indexer_name (
str) – Value written forGenerated by. The per-crystalindexed_byis alwaysfromfileso CrystFEL parses it as a known indexing method.panel (
str) – Fallback panel name when a peak falls outside every geometry panel (or when the geometry carries no panel definitions).
- __init__(path, geometry, *, cell=None, geometry_file=None, files=None, indexer_name='probixi', panel='0')¶
- class probixi.PeakOffloader¶
Write peak-search results as CrystFEL-readable CXI files.
Exports peaks in the CXI layout so that
indexamajig --peaks=cxican re-index probixi’s peaks with its own engine. One.cxiis written per source HDF5 file; each external-links the raw image stack and stores the peak arrays under/entry_1/result_1indexed by event.Alongside the
.cxifiles it writespeaks.lstand a companion geometry file (the input geometry pluspeak_list/peak_list_type), so the output directory is drop-in for indexamajig.Use as a context manager; the instance is the per-frame writer expected by
PeakStream.for_each(__call__forwards towrite()):with PeakOffloader(out_dir, geometry=geom, geometry_file=gf, files=meta.files) as off: for result in pipeline.peak_stream(frames): off.write(result)
- Parameters:
- __init__(path, *, geometry_file=None, files=None)¶
Multi-GPU¶
- probixi.run_data_parallel(list_file, geometry_file, cell_file, output, *, devices=None, start=None, stop=None, batch_size=8, seed_frames=32, target_noise_peaks=5.0, noise_mode='online', warmup_frames=16, flux_variance=False, flux_var_floor=0.15, panel='0', enrich_gate=False, enrich_alpha=0.001, threads_per_worker=None, keep_parts=False, quiet=False)¶
Run indexing across several devices on one node and merge the outputs.
- Return type: