coseda.centerfinding package

Submodules

coseda.centerfinding.findcenters_beamstop module

Beamstop-based center finding with Friedel-pair clustering and drift fitting.

coseda.centerfinding.findcenters_beamstop.divide_into_batches(total_indices, batch_size, logfile_path)[source]

Split surviving frame indices into batches, enforcing at least four batches.

coseda.centerfinding.findcenters_beamstop.find_friedel_pairs(peak_pos, center, tol, block=2048, max_pairs=None)[source]

Vectorised Friedel-pair search that bounds memory to ~32MB at block=2048.

Shifts peaks relative to the current center and searches for pairs whose sum-norm is below tol using blockwise matrix operations.

Parameters:
  • peak_pos (numpy.ndarray)

  • center (numpy.ndarray)

  • tol (float)

  • block (int)

  • max_pairs (int | None)

Return type:

numpy.ndarray

coseda.centerfinding.findcenters_beamstop.fit_gaussian_to_largest_cluster(deviations, min_samples_fraction, dbscan_eps=5.0)[source]

Cluster deviations with DBSCAN and fit a 1-component GMM to the largest cluster.

coseda.centerfinding.findcenters_beamstop.get_frame_stack_shape(h5file, framepath)[source]

Return (num_frames, frame_height, frame_width, resolved_dataset_path).

Parameters:
  • h5file (h5py.File)

  • framepath (str)

coseda.centerfinding.findcenters_beamstop.get_pixels_per_meter(config, logfile_path)[source]

Retrieve the ‘pixels_per_meter’ value from the configuration. If ‘pixels_per_meter’ is not available, compute it using ‘pixel_width’ and ‘pixel_unit’.

coseda.centerfinding.findcenters_beamstop.iterative_gaussian_fitting_on_subset(h5file_path, initial_center, subset_indices, tolerance, min_peaks, resolution_limit, batch_index, graphicsfolder_path, logfile_path, min_samples_fraction, stop_event=None, detector_geometry=None, max_pairs_per_frame=None, dbscan_eps=5.0)[source]

Iteratively shift the center toward the mean Friedel-pair deviation for one batch.

Stops when deviations fall below 0.4 px in both axes or after breakcriterion iterations; saves per-iteration diagnostics to disk.

coseda.centerfinding.findcenters_beamstop.plot_batch_centers(frame_positions, updated_x, updated_y, graphicsfolder_path)[source]
coseda.centerfinding.findcenters_beamstop.plot_deviation_cloud(deviations, mean, current_center, batch_index, itcount, graphicsfolder_path)[source]

Scatter the Friedel-pair deviation cloud with the current mean overlaid.

coseda.centerfinding.findcenters_beamstop.plot_linear_fit(batch_positions, batch_centers, frame_positions, updated_x, updated_y, graphicsfolder_path)[source]
coseda.centerfinding.findcenters_beamstop.process_batch(h5file_path, initial_center, subset_indices, tolerance, min_peaks, resolution_limit, batch_index, graphicsfolder_path, logfile_path, min_samples_fraction, detector_geometry=None, max_pairs_per_frame=None, dbscan_eps=5.0)

Delayed wrapper to run iterative fitting for one batch (used by Dask).

coseda.centerfinding.findcenters_beamstop.quartering(configfile, usedask, batch_size, progress_callback=None, stop_event=None)[source]

Main beamstop center-finding workflow for a single INI/HDF5 pair.

  • Validates required parameters

  • Maps surviving frames (post-strip) to batches

  • Runs Friedel-pair iterative fitting per batch (optionally via Dask)

  • Tries linear drift fit, falls back to batch means if poor

coseda.centerfinding.findcenters_beamstop.quartering_batch(input_path, usedask=False, batch_size=10000, progress_callback=None, stop_event=None)[source]

Run beamstop center finding for each INI in a folder/file/list input.

coseda.centerfinding.findcenters_beamstop.refine_center_with_friedel(h5file_path, current_center, tolerance, min_peaks, resolution_limit, subset_indices, batch_index, logfile_path=None, detector_geometry=None, max_pairs_per_frame=None)[source]

Extract Friedel-pair deviations for a batch of frames and return concatenated deltas.

  • Skips frames with fewer than min_peaks

  • Applies a radial cutoff (resolution_limit) around the current center

  • Validates shapes before appending to the deviation list

coseda.centerfinding.findcenters_beamstop.resolve_frame_dataset(h5file, framepath)[source]

Resolve a frame path to a concrete HDF5 dataset.

Accepts either a dataset path (e.g. /entry/data/images) or a group path (e.g. /entry/data/image_data NXdata), where the image dataset is resolved via signal, then common child names, then a single 2D/3D child dataset.

Parameters:
  • h5file (h5py.File)

  • framepath (str)

coseda.centerfinding.findcenters_beamstop.set_center_based_on_batch(h5file_path, batch_centers, graphicsfolder_path, logfile_path, framepath, batch_size, total_frames, pixels_per_meter, orig_positions=None, detector_geometry=None)[source]

Fallback: write per-batch mean centers to all frames in that batch and persist.

coseda.centerfinding.findcenters_beamstop.set_center_based_on_line_fit(h5file_path, batch_centers, graphicsfolder_path, logfile_path, framesize, framepath, batch_size, total_frames, pixels_per_meter, force_linear_fit=False, detector_geometry=None)[source]

Fit a linear drift to batch centers, write per-frame centers to HDF5, and log QA.

Saves fit parameters for GUI display, warns on poor R^2, and optionally falls back if the fit is weak (unless forced).

coseda.centerfinding.findcenters_beamstop.update_detector_shifts(h5file_path, logfile_path, framepath, pixels_per_meter, detector_geometry=None)[source]

Compute detector shifts (mm) from center arrays and save to HDF5.

coseda.centerfinding.findcenters_direct module

Direct beam center finding via quadrant evenness and optional auto-thresholding.

coseda.centerfinding.findcenters_direct.compute_auto_significance_threshold(h5file_path, framepath, sample_count, patch_size, bin_size)[source]

Sample frames, bin to patch_size patches, and compute mean brightest/corner ratio.

Picks sample_count random frames, bins them coarsely, and for each compares the brightest patch with the three darkest corners. Returns the mean ratio to serve as an automatic significance threshold for beam detection.

coseda.centerfinding.findcenters_direct.find_primary_beam_direct(frame, box_size, search_radius, bin_size, threshold)[source]

Locate the beam by coarse binning, quadrant-evenness refinement, and significance test.

Uses an integral image to find the brightest bin, sweeps a search window to minimize quadrant variance, then rejects the candidate if its local mean is not sufficiently brighter than the image corners. Returns (row, col) or None.

coseda.centerfinding.findcenters_direct.interpolate_centers(centers)[source]

Linearly interpolate missing centers; returns arrays of x and y positions.

If fewer than two valid points are present, fills NaNs with zeros.

coseda.centerfinding.findcenters_direct.is_within_bounds(r, c, shape, box)[source]

Return True if a box of size box at (r, c) fits entirely inside an array of shape shape.

coseda.centerfinding.findcenters_direct.process_centers(h5file_path, framepath, interval, box_size, search_radius, bin_size, threshold, outputfolder_path, pixels_per_meter, logfile_path=None, progress_callback=None, stop_event=None)[source]

Detect beam centers on a subsampled set of frames, fit linear drift, and write results.

  • Samples original frames by interval honoring stripped index mapping

  • Runs Dask-parallel find_primary_beam_direct

  • Saves raw detections, fit parameters, and writes center_x/y + detector shifts

coseda.centerfinding.findcenters_direct.run_findcenters_direct(configfile, progress_callback=None, stop_event=None)[source]

Entry point for direct beam finding given a single dataset INI.

coseda.centerfinding.findcenters_direct.write_centerfinderdirectsettings(input_path, interval, box_size, search_radius, bin_size, auto_sample_count=None, auto_patch_size=None, auto_factor=None)[source]

Update INI files under [Parameters] with direct center-finding settings.

Module contents

Centerfinding workflows.