torchref.base.metrics.rfactor module

R-factor calculation functions.

Functions for computing crystallographic R-factors and related metrics.

torchref.base.metrics.rfactor.get_rfactor_torch(F_obs, F_calc)[source]

Calculate R-factor between observed and calculated structure factors (PyTorch version).

Parameters:
  • F_obs (torch.Tensor) – Observed structure factor amplitudes.

  • F_calc (torch.Tensor) – Calculated structure factor amplitudes.

Returns:

R-factor value.

Return type:

torch.Tensor

torchref.base.metrics.rfactor.get_rfactor(F_obs, F_calc)[source]

Calculate the R-factor between observed and calculated structure factors (NumPy version).

The R-factor is a measure of agreement between observed and calculated structure factor amplitudes, defined as sum(|F_obs - F_calc|) / sum(F_obs).

Parameters:
  • F_obs (numpy.ndarray) – Observed structure factor amplitudes.

  • F_calc (numpy.ndarray) – Calculated structure factor amplitudes.

Returns:

R-factor value between 0 and 1.

Return type:

float

torchref.base.metrics.rfactor.rfactor(F_obs, F_calc)[source]

Calculate R-factor between observed and calculated structure factors.

Parameters:
  • F_obs (torch.Tensor) – Observed structure factor amplitudes of shape (N,).

  • F_calc (torch.Tensor) – Calculated structure factor amplitudes of shape (N,).

Returns:

R-factor value.

Return type:

float

torchref.base.metrics.rfactor.get_rfactors(F_obs, F_calc, rfree)[source]

Get R-factors for working and test sets.

Parameters:
  • F_obs (torch.Tensor) – Observed structure factor amplitudes of shape (N,).

  • F_calc (torch.Tensor) – Calculated structure factor amplitudes of shape (N,).

  • rfree (torch.Tensor) – Boolean mask indicating R-free reflections of shape (N,). 1 is Working set, 0 is Test set.

Returns:

(r_work, r_test) where r_work is the R-factor for the working set and r_test is the R-factor for the test set.

Return type:

tuple

torchref.base.metrics.rfactor.bin_wise_rfactors(F_obs, F_calc, rfree, bins)[source]

Calculate bin-wise R-factors between observed and calculated structure factors.

Parameters:
Returns:

  • r_work_bins (torch.Tensor) – R-factors for working set (per bin).

  • r_test_bins (torch.Tensor) – R-factors for test set (per bin).

Return type:

tuple

torchref.base.metrics.rfactor.calc_outliers(F_obs, F_calc, z)[source]

Identify outlier reflections based on deviation from expected values (PyTorch version).

Parameters:
  • F_obs (torch.Tensor) – Observed structure factor amplitudes.

  • F_calc (torch.Tensor) – Calculated structure factor amplitudes.

  • z (float) – Number of standard deviations for outlier threshold.

Returns:

Boolean mask where True indicates outlier reflections.

Return type:

torch.Tensor

torchref.base.metrics.rfactor.calc_outliers_numpy(F_obs, F_calc, z)[source]

Identify outlier reflections based on structure factor differences (NumPy version).

Detects reflections where the normalized difference between observed and calculated structure factors exceeds z standard deviations.

Parameters:
  • F_obs (numpy.ndarray) – Observed structure factor amplitudes.

  • F_calc (numpy.ndarray) – Calculated structure factor amplitudes.

  • z (float) – Number of standard deviations for outlier threshold.

Returns:

Boolean array where True indicates an outlier reflection.

Return type:

numpy.ndarray