ImageD11.friedel_pairs module#
- class ImageD11.friedel_pairs.FriedelPairIndexer(cf, ds, tol_gv=0.02, tol_eta=0.5, tol_logI=inf, weights=None, n_steps=5)#
Bases:
objectMatch Friedel reflection pairs in a columnfile
Friedel relationships#
omega pairs : (h,k,l), -(h,k,l) reflexion 180 deg apart in omega: g -> -g, eta -> 180 - eta eta pairs : “entry” and “exit” (h,k,l),-(h,k,l) reflexions at omega +- theta: g -> -g, eta -> 180 + eta horizontal pairs : the same (h,k,l) at its two omega solutions, left and right of the beam:
g -> g, eta -> -eta
- param cf:
- type cf:
ImageD11 columnfile
- param ds:
- type ds:
ImageD11.sinograms.dataset object
- param tol_gv:
- type tol_gv:
float – g-vector tolerance
- param tol_eta:
- type tol_eta:
float – eta angle tolerance (degrees)
- param tol_logI:
- type tol_logI:
float – log10(intensity) tolerance (np.inf to ignore)
- param weights:
- type weights:
dict | None – User-defined weights for scaling search-space dimensions (Optional)
- param n_steps:
- type n_steps:
int – number of distance increments in iterative KDTree search
- param PeakSubsets:
- type PeakSubsets:
PeakSubsets object | None – paired subsets for friedel pair search by chunks
- estimate_search_scales(idx1, idx2, dist_cutoff, pair_type='omega')#
Pilot pairing on a small subset to estimate typical pair distances along each search-space dimension and derive normalised weights.
- Parameters:
idx1 (index arrays into self.cf (reference / partner subset))
idx2 (index arrays into self.cf (reference / partner subset))
dist_cutoff (float -- initial normalised cutoff for the pilot KDTree)
verbose (bool)
- Returns:
scales ((5,) array of median distances per dimension)
weights_dict (dict of normalised weights keyed by dimension name)
- initialize_peaksubsets(chunk_type='scans', n_eta_bins=None, reset=False)#
Initialization and checks for match_friedel_pairs_by_chunks():
- match_friedel_pairs(pair_type='omega', drop_unpaired=False, filter_mode='relaxed', doplot=True, timeout=120)#
Find Friedel pairs using a KDTree approach. The columnfile is split into two symmetric parts, and the symmetric partner is flipped to match with the reference subset. Two KDtrees are build with these subset and an iterative nearerst-neighbor search is performed to find the pairs within a certain distance defined by the tolerance thresholds.
Works for omega-pairs, eta-pairs and horizontal pairs. WARNING: do not use on large (>10M peaks) sanning-3DXRD peakfiles: very slow!
Use the chunk approach instead (self.match_omega_pairs_by_chunks)
- Parameters:
pair_type (str; 'omega', 'eta' or 'horizontal')
drop_unpaired (bool; If True non-paired peaks are filtered out from self.cf)
timeout (float; max execution time (s) before timeout for _run_pairing.)
filter_mode (str; 'strict' or 'relaxed') – controls how threshold filtering is performed during the search strict is slower but allows higher completeness for pair matching see also: self.run_pairing
- Returns:
adds new colum ‘<pair_type>_pair_id’ to self.cf
Returns self.cf with the new pair_id column
——-
- match_friedel_pairs_by_chunks(chunk_type='scans', filter_mode='relaxed', n_eta_bins=360, drop_unpaired=False, extended_bin_search=False, n_workers=-1, timeout=120, reset_psub=False, doplot=True)#
Find Friedel pairs in a large columnfile using a chunked KDTree approach. Instead of building KDTrees over the whole columnfile, pairs are searched independently within mirror subsets (mirror dty scans or omega-dty frames), then merged into the master columnfile.
Works only for scanning-3DXRD datasets with a dty column. Only search for omega-pairs. eta-pair search not implemented yet.
- Parameters:
chunk_type (str 'frames', 'scans', 'eta_bins', 'minus_eta_bins' ( default: 'scans')) – For omega pairs, use ‘frames’ or ‘scans’ for friedel pair search at different levels of chunking granularity. For eta pairs, use ‘eta_bins’ For horizontal pairs, use ‘minus_eta_bins’
n_eta_bins (int, even nb. number of bins for eta chunks.)
drop_unpaired (bool (default False))
reset_psub (bool (default False). reset self.Peaksubset if True.)
n_workers (int number of parallel worker processes.) – -1 = use all available cores
filter_mode (str; 'strict' or 'relaxed' (default strict)) – see also : self.match_friedel_pairs, self.run_pairing
- Returns:
adds new colum ‘<pair_type>_pair_id’ to self.cf
Returns self.cf with the new pair_id column
- match_quadruplets()#
Find quadruplets of peaks satisfying the full Friedel relation in both the omega and eta pairing directions.
A valid quadruplet is a 2x2 block in the (omega_pair_id, eta_pair_id) grid:
eta_pair A eta_pair B
omega_pair 0: peak p00 , peak p01 omega_pair 1: peak p10 , peak p11
Both match_omega_pairs() and match_eta_pairs() must have been called first so that ‘omega_pair_id’ and ‘eta_pair_id’ columns exist in self.cf.
- Writes a new column ‘quadruplet_id’ to self.cf:
- -1
: peak not part of any quadruplet
k : peak belongs to quadruplet k (each value appears exactly 4 times)
- Returns:
quadruplets – Each row is [p_om0_eta0, p_om0_eta1, p_om1_eta0, p_om1_eta1] with cf-level indices, arranged as the 2x2 block read row-major.
- Return type:
(M, 4) int array
- merge_outputs(cf_target=None, pair_type='omega', drop_broken=False, drop_unpaired=False, deduplicate=False)#
Merge all dicts of pair_ids in self.outputs into self.cf. For chunk-based matching, pair-id labels are made globally unique across chunks.
- Parameters:
cf_target (columnfile to write into (defaults to self.cf))
pair_type ('omega', 'eta' or 'horizontal')
drop_broken (bool -- if True, remove singleton pair_ids from cf)
drop_unpaired (bool -- if True, remove non-paired peaks from cf)
- reset_outputs(pair_id_col=None)#
- run_pairing(idx1, idx2, pair_type='omega', filter_mode='strict', weights=None, t_max=120)#
class-level launcher for _run_pairing
- set_peak_subsets(n_eta_bins=360, y0=None)#
- sort_peak_subsets(pair_type='omega')#
- ImageD11.friedel_pairs.Pair_dty#
alias of
Pair
- ImageD11.friedel_pairs.Pair_eta#
alias of
Pair
- ImageD11.friedel_pairs.Pair_omega_dty#
alias of
Pair
- class ImageD11.friedel_pairs.PeakSubsets(cf, ds, n_eta_bins=360, y0=None)#
Bases:
objectMake list of paired subsets (subset_1, subset_2) from whole dataset for matching Friedel pairs. Subsets are either:
pairs of mirror dty scans
pairs of mirror omega-dty frames (omega-pairs)
pairs of mirror eta bins (eta, eta+180 deg) (eta-pairs)
pairs of mirror eta bins (eta, -eta) (horizontal pairs)
Columnfile is sorted, either in sinogram order (dty, omega) for omega-pairs or in eta order for eta-pairs and subset indices are stored in lookup tables (LUT) for fast selection.
- check_symmetry()#
Plot N_peaks and total_intensity vs. position for mirror dty scans (y0+dy, y0-dy) or mirror eta bins (eta, eta+180 deg) and check their correlation.
If alignment is correct, values should match between mirror pairs. Significant mismatch suggests an incorrect y0 (for dty), sample movement, or a beam issue during scanning.
- find_valid_subsets()#
compute boolean arrays identifying paired subsets where both members contain at least one peak in corresponding lookup table (frames_LUT or scans_LUT). Sets new attributes self.valid_scans_subsets / self.valid_frames_subsets
- get_eta_bins_subsets(n_bins)#
- Find mirror pairs of eta bins:
bin lo : eta_lo bin hi : eta_lo + 180
Sets ds.eta_bins_subsets: list of Pair namedtuples (‘Pair’, [‘ei_hi’, ‘ei_lo’, ‘eta_hi’, ‘eta_lo’]) where ei_* are indices into self.ebincens.
- get_frames_subsets()#
- Find mirror pairs of frames (A, B) in omega and dty:
frame A : dty = y0 + dy , omega = w frame B : dty = y0 - dy , omega = (w + 180) mod 360
Sets ds.frames_subsets: list of Pair namedtuples (‘Pair’, [‘yi_A’,’yi_B’,’omi_A’,’omi_B’, ‘dty_A’,’dty_B’,’omega_A’,’omega_B’]) with indices yi*, omi* into ds.ybincens and ds.obincens
- get_minus_eta_bins_subsets()#
Find mirror pairs of eta bins for horizontal pairs, using the bins from get_eta_bins_subsets:
bin lo : eta_lo < 0 bin hi : -eta_lo
Sets self.minus_eta_bins_subsets: list of Pair namedtuples (‘Pair’, [‘ei_hi’, ‘ei_lo’, ‘eta_hi’, ‘eta_lo’]) where ei_* are indices into self.ebincens. Uses the eta_bins_LUT.
- get_scan_subsets()#
- Find mirror pairs of dty scans on each side of the central y0:
scan hi : dty = y0 + dy scan lo : dty = y0 - dy
Sets ds.scans_subsets: list of Pair namedtuples (‘Pair’, [‘yi_hi’, ‘yi_lo’, ‘dty_hi’, ‘dty_lo’]) where yi_* are indices into ds.ybincens.
- select_pair(pair_id, subset_type='scans', return_as='idx')#
select a pair of subsets from one of the pairs list
- sort_by_eta()#
Sort columnfile in eta bins order and build lookup table for quick peak selection
Sets self.eta_bins_LUT : dict { ei, slice(lo, hi), n_peaks, total_intensity) } ei indices match those in self.eta_bins_subsets
- sort_by_sinogram()#
Sort columnfile in sinogram space (dty primary, omega secondary) and build frame-level and scan-level lookup tables for quick peak selection by omega and dty bins.
Sets self.scans_LUT : dict { yi, slice(lo, hi), n_peaks, total_intensity) } self.frames_LUT : dict { (omi, yi), slice(lo, hi), n_peaks, total_intensity) }
y_i / om_y_i indices match those in self.scans_subsets and self.frames_subsets Frames / scans with zero peaks are absent from the dict.
- ImageD11.friedel_pairs.get_pair_distances(cf, pair_type='omega')#
Physical distance in g-vector norm, eta and log(intensity) between matched pairs.
- Returns:
distances (dict with keys ‘d_gvec’, ‘d_eta’, ‘d_logI’)
(idx1, idx2) (index positions of pairs in cf)
- ImageD11.friedel_pairs.get_pairs(cf, pair_type='omega')#
returns (idx1, idx2) : index positions of pairs in cf
- ImageD11.friedel_pairs.locate_eta_pairs(cf, pairs, ds=None, y0=0.0)#
Fit the centre of mass position of eta-pairs and write results back into cf as new columns ‘sx’ and ‘sy’. Works only for scanning-3DXRD. For Box-beam, the relocation problems has two more degrees of freedom and cannot be solved with only one pair.
- Parameters:
cf (ImageD11 columnfile)
pairs ((idx1, idx2) arrays of cf indices for each pair)
y0 (float, beam centre offset)
- ImageD11.friedel_pairs.locate_omega_pairs(cf, pairs, ds=None, y0=None)#
Fit the centre of mass position of omega-pairs and write results back into cf as new columns ‘sx’ and ‘sy’. Works only for scanning-3DXRD. For Box-beam, the relocation problems has two more degrees of freedom and cannot be solved with only one pair.
For omega-pairs the linear system used in ‘locate_eta_pairs’ is near-singular (paired peaks are ~180 degrees apart in omega), so the position is recovered from two-theta and dty: dx : x-shift from rotation centre, derived from tth asymmetry ylab: y-shift in lab frame, taken as dty - y0 Then rotate (dx, ylab) back into sample coordinates using omega. xlab, ylab and omega are averaged to make sure both peaks in each pair have the same (sx, sy) coordinates
- ImageD11.friedel_pairs.plot_pair_distances(cf, pair_type='omega', bins=50, log_scale=False, **kwargs)#
Plot distributions of pair distances along each physical dimension.
- Parameters:
cf (ImageD11 columnfile. Must contain a '{pair_type}_pair_id' column.)
pair_type ('omega', 'eta' or 'horizontal', used for column name lookup and title)
bins (number of histogram bins)
log_scale (bool, log y-axis)
- ImageD11.friedel_pairs.update_geometry_fpairs(cf, ds=None, add_xyz_lab=False, relocate_pairs=True)#
Update diffraction geometry for (scanning)-3DXRD data using Friedel-pairs (omega-pair) symmetry correction: similar to cf.updateGeometry() but uses Friedel-pairs to correct for the offset from rotation-center. Details in Jacob et al. 2024, https://doi.org/10.1107/S1600576724009634
Update the following columns in the ColumnFile: ‘tth’, ‘eta’, ‘ds’, ‘gx’, ‘gy’, ‘gz’ If
add_xyz_labTrue: ‘xl’, ‘yl’, ‘zl’ also updated Ifrelocate_pairsTrue: add ‘sx’ and ‘sy’ coordinates computed from omega pairscf : ImageD11 ColumnFile
ds : ImageD11.sinogram dataset (optional).
add_xyz_lab : bool, optional. Add corrected lab-frame coordinates to cf
relocate_fpairs