torchref.refinement.targets.xray.least_squares module

class torchref.refinement.targets.xray.least_squares.LeastSquaresXrayTarget(data=None, model=None, scaler=None, weighting='sigma', use_work_set=True, sigma_mode='raw', verbose=0)[source]

Bases: XrayTarget

Least Squares target function. L_LS = Σ w_i * (|F_obs| - k * |F_calc|

__init__(data=None, model=None, scaler=None, weighting='sigma', use_work_set=True, sigma_mode='raw', verbose=0)[source]

Initialize X-ray target.

Parameters:
  • data (ReflectionData, optional) – Reference to the ReflectionData object. Required for forward().

  • model (Model or ModelFT, optional) – Reference to Model object for F_calc computation. If None, fcalc must be provided to forward().

  • scaler (Scaler, optional) – Reference to the Scaler object.

  • use_work_set (bool, optional) – If True, compute loss on work set; if False, on test set. Default is True.

  • sigma_mode (str, optional) –

    Which sigma to use in the likelihood. Options:

    • 'raw' (default): use the raw experimental sigmas from the data file. Empirically gives the best Rfree across the mid-resolution regime (1.5-3.0 A) when paired with appropriate group weights.

    • 'effective': use per-shell effective sigmas estimated from scaling residuals (capped SIGMAA-style correction). Opt-in for high-resolution refinement (< 1.5 A) or datasets with known sigma miscalibration. Note: Scaler.estimate_sigma_eff is always called so the estimates are available regardless of which mode the target uses.

  • verbose (int, optional) – Verbosity level. Default is 0.

forward(fcalc=None)[source]

Compute least squares loss.

Parameters:

fcalc (torch.Tensor, optional) – Pre-computed structure factors. If provided, uses these instead of computing from model.

Returns:

Mean weighted least squares loss.

Return type:

torch.Tensor