coseda.centerrefinement package¶
Submodules¶
coseda.centerrefinement.cancellation module¶
Shared helpers for cooperative cancellation of long-running refinement jobs.
- exception coseda.centerrefinement.cancellation.RefinementCancelled[source]¶
Bases:
ExceptionRaised when a user requests to stop an in-flight refinement.
- add_note()¶
Exception.add_note(note) – add a note to the exception
- args¶
- with_traceback()¶
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- coseda.centerrefinement.cancellation.check_cancel(stop_event, where='')[source]¶
Raise RefinementCancelled if the provided stop_event is set.
- Parameters:
stop_event (threading.Event or None) – Event used to request cancellation. If None, no check is performed.
where (str) – Optional string describing the current phase, helpful for logging.
coseda.centerrefinement.refinecenters_beamstop module¶
- coseda.centerrefinement.refinecenters_beamstop.output(indices, deviations, graphicsfolder_path, logfile_path, itcount, index_length=None)[source]¶
Save deviations for GUI.
- coseda.centerrefinement.refinecenters_beamstop.perform_lowess(indices, deviations, frac=0.1)[source]¶
Perform LOWESS smoothing on deviations.
- coseda.centerrefinement.refinecenters_beamstop.plot_deviations(indices, deviations, graphicsfolder_path, itcount, lowess_frac=0.1)[source]¶
Plot deviations with LOWESS fit.
- coseda.centerrefinement.refinecenters_beamstop.plot_lowess_and_centers(lowess_fit_x, lowess_fit_y, original_center_x, original_center_y, existing_indices, itcount, graphicsfolder_path)[source]¶
Plot LOWESS fits and original centers.
- coseda.centerrefinement.refinecenters_beamstop.refine(configfile, client, batch_size=500, stop_event=None)[source]¶
Main center refinement function.
- coseda.centerrefinement.refinecenters_beamstop.refine_center_with_friedel_and_update(h5file_path, min_peaks, tolerance, resolution_limit, itcount, convergence_threshold, converged, graphicsfolder_path, logfile_path, client, lowess_frac=0.1, lowess_window=None, batch_size=500, stop_event=None, detector_geometry=None, max_pairs_per_frame=None, deviation_aggregation='median')[source]¶
Refine centers using Friedel pairs and update the HDF5 file.
- coseda.centerrefinement.refinecenters_beamstop.refine_center_with_friedel_batch(h5file_path, min_peaks, tolerance, resolution_limit, indices_subset, centers_x, centers_y, index_map, detector_geometry=None, max_pairs_per_frame=None)[source]¶
Process a subset of frames (indices_subset, which are original frame numbers) to find deviations.
coseda.centerrefinement.refinecenters_direct module¶
- coseda.centerrefinement.refinecenters_direct.lowess_xunit(x, y, xout, span)[source]¶
Custom LOESS smoothing using a fixed window span in x-units. x, y: original data arrays. xout: points at which to evaluate the fit (subset of x). span: half-window width in the same units as x. Returns array of shape (len(xout), 2) with columns (xout, smoothed y).
- coseda.centerrefinement.refinecenters_direct.output_direct(indices, coarse, fine, outputfolder_path, logfile_path, loess_span=None, loess_frac=None, total_frames=None, iteration=0)[source]¶
Outputs direct refinement results to numpy files.
- Parameters:
loess_span (float)
loess_frac (float)
total_frames (int)
iteration (int)
- coseda.centerrefinement.refinecenters_direct.quadrant_var_cost(img, xc, yc, half, dxs, dys)[source]¶
- coseda.centerrefinement.refinecenters_direct.quadrant_variance_numba(img, cx, cy, half, deltas_x, deltas_y)¶
- coseda.centerrefinement.refinecenters_direct.refine_and_update_centers(h5file_path, sigma_x, sigma_y, threshold_frac, box1_half, step1, step2, batch_size=500, R_min=None, R_max=None, debug_plot_dir=None, logfile_path=None, loess_span=None, loess_frac=None, usedask=False, convergence_threshold=None, skip_fit=False, stop_event=None)[source]¶
Runs refine_direct_all over all frames, updates center_x and center_y datasets with the new fine centers (for all valid frames).
- Parameters:
h5file_path (str)
sigma_x (float)
sigma_y (float)
threshold_frac (float)
box1_half (int)
step1 (float)
step2 (float)
batch_size (int)
R_min (float)
R_max (float)
debug_plot_dir (str)
logfile_path (str)
loess_span (float)
loess_frac (float)
usedask (bool)
convergence_threshold (float)
skip_fit (bool)
- coseda.centerrefinement.refinecenters_direct.refine_direct_all(h5file_path, all_indices, sigma_x, sigma_y, threshold_frac, box1_half, step1, step2, batch_size=500, R_min=None, R_max=None, debug_plot_dir=None, usedask=False, stop_event=None)[source]¶
Driver: divides frames into batches, optionally calls refine_direct_batch for each batch in parallel with Dask. Collects and returns all valid results.
- Parameters:
h5file_path (str)
all_indices (numpy.ndarray)
sigma_x (float)
sigma_y (float)
threshold_frac (float)
box1_half (int)
step1 (float)
step2 (float)
batch_size (int)
R_min (float)
R_max (float)
debug_plot_dir (str)
usedask (bool)
- coseda.centerrefinement.refinecenters_direct.refine_direct_batch(h5file_path, frame_indices, sigma_x, sigma_y, threshold_frac, box1_half, step1, step2, R_min=None, R_max=None, debug_plot_dir=None, stop_event=None)[source]¶
- Parameters:
h5file_path (str)
frame_indices (numpy.ndarray)
sigma_x (float)
sigma_y (float)
threshold_frac (float)
box1_half (int)
step1 (float)
step2 (float)
R_min (float)
R_max (float)
debug_plot_dir (str)
- coseda.centerrefinement.refinecenters_direct.refine_direct_single_frame(frameindex, frame, x0, y0, sigma_x, sigma_y, threshold_frac, box1_half, step1, step2, R_min=None, R_max=None, debug_plot_path=None)[source]¶
Perform adaptive two-stage quadrant-variance refinement on a single frame. Returns a dict with keys ‘coarse’ and ‘fine’ containing (x,y) tuples.
- Parameters:
frameindex (int)
frame (numpy.ndarray)
x0 (float)
y0 (float)
sigma_x (float)
sigma_y (float)
threshold_frac (float)
box1_half (int)
step1 (float)
step2 (float)
R_min (float)
R_max (float)
debug_plot_path (str)
- Return type:
dict
- coseda.centerrefinement.refinecenters_direct.refinecenters_direct_batch(input_path, usedask=True, batch_size=50, stop_event=None)[source]¶
Process all configuration files for center refinement.
- coseda.centerrefinement.refinecenters_direct.refinecenters_direct_file(configfile, client=None, batch_size=50, stop_event=None)[source]¶
- coseda.centerrefinement.refinecenters_direct.resolve_loess_span(loess_span, loess_frac, total_frames, default_frac=0.1)[source]¶
Determine the LOESS span (in frame units) from either an explicit span or a fraction. Returns a tuple (span, fraction_used). If both are missing and default_frac is None or <=0, span will be None to indicate smoothing should be skipped.
- coseda.centerrefinement.refinecenters_direct.write_centerrefinement_direct_settings(input_path, threshold_frac, box1_half, step1, step2, sigma_x, sigma_y, loess_span=None, loess_frac=None, batch_size=None, cycles=None, convergence_threshold=None)[source]¶
Update .ini files to set parameters for direct center refinement.
Module contents¶
Center refinement workflows.