Skip to content

The method

hr2d is an implementation of 2D classification inspired by Hartigan k-means formulation. Assignments, in-plane angles and in-plane shifts are obtained such that they minimize the k-means loss. This algorithm is online, meaning it assigns images sequentially; in practice, for speed, images are moved in small batches (search.batch_size, 128 by default), and a separate minibatch schedule (minibatch.*) is also available. We implement the angle and shift search, frequency marching, and rotation operators by adapting the 2D classification available in RELION. For details on the derivations please see our paper.

The loss

We adapt the k-means loss to 2D classification, by considering that the images are unaligned and are blurred by the CTF. This gives us the following loss function $$ L(S, A) = \sum_{k=1}^{K} \sum_{i \in S_k} \lVert x_i - A_i\, \mu(S_k) \rVert^2. $$ Although this is the target loss of HR2D, evaluating it exactly for each query would require a pass through the data. Instead, when estimating the assignment and alignment of an image, we consider it as a new sample in the dataset, then we compute the difference between the loss before and after inserting the image into some class \(l\) with some alignment \(A\).

scripts/kmeans_loss.py evaluates \(L(S, A)\) exactly, with the class averages defined below; this is the loss used to compare methods (Analysis).

The class averages \(\mu(S_k)\) are computed by solving the least-squares problem $$ \mu(S_k) := \operatorname*{arg\,min}{\mu} \sum\, \mu|^2, $$ whose solution satisfies the normal equations $$ B_k\, \mu(S_k) = b_k, \qquad B_k := \sum_{i \in S_k} A_{i}^{\mathsf H} A_{i}, \qquad b_k := \sum_{i \in S_k} A_{i}^{\mathsf H} x_i, $$} |x_i - A_{i

where \(A^{\mathsf H}\) denotes the Hermitian transpose.

In our logs, we don't report this loss as it would require an additional pass through the data. Instead \(L_\text{approx}\), printed in run.log at every iteration, is reported for computational purposes. During the pose search, each particle's residual \(\lVert x_i - A_i\, \mu(S_{z_i}) \rVert^2\) is evaluated at the class and alignment it ends up with, against the class averages during the angle, shift and assignment search. These residuals are then added to produce

\[ L_\text{approx} = \sum_i \lVert x_i - A_i\, \mu^{(b_i)}(S_{z_i}) \rVert^2 , \]

where \(\mu^{(b_i)}\) are the class averages at the start of particle \(i\)'s batch. Although not exactly the target function, \(L_\text{approx}\) provides useful information regarding convergence and an approximation of the current fit to the class averages.

What is taken from RELION

  • Rotation: the reference is rotated, not the image, by bilinear interpolation on a 2×-oversampled Fourier grid (padding_factor, RELION's --pad).
  • Search grid: an exhaustive grid of in-plane angles (psi_step) × translations on a disk (offset_range, offset_step) around each image's previous offset.
  • Two passes: a coarse pass at a Fourier radius set by the particle diameter, then an oversampled fine pass (ψ ± step/4, shifts ± step/4) around the best candidates, at the current resolution.
  • Frequency marching: the Fourier radius is the SSNR resolution + incr_size pixels, starting from ini_high. It never decreases.
  • Optional extras: class centring (center_classes) and a soft reference mask (reference_mask: diameter). The class means are always plain least squares (no τ² prior); tau2_fudge only enters the resolution rule.
  • Conventions: RELION's CTF, ψ and origin conventions (Conventions).

What is new

  • Preprocessing: each image is normalised so that the noise in the box corners has unit power, multiplied by a soft circular window, and whitened by the noise spectrum of the corners, estimated once from 5,000 images. The CTF is whitened with the same filter.
  • Score: for image x, the difference in the loss caused by inserting it into class k and with angle and shift (ψ, s) is \(\Delta_H^2 = \lVert r \rVert^2 - r^H A (B_k + A^H A)^{-1} A^H r\). In our implementation we write this in its diagonal form \(S = \sum_j W_j \lvert T_s^H x_j - C_j \tilde\mu_j \rvert^2 - \sum_j \lvert x_j \rvert^2\) with \(W = \tilde h / (\tilde h + C^2)\). Here \(\tilde h\) is the class's information \(\beta_k\), interpolated at the rotated pixel. In this representation \(W\) does not depend on the shift, making this expression more practical for the angle and shift search.
  • Leave-one-out: the image's own class is always scored with the image removed. This comes from the formulation of Hartigan's algorithm and is not a heuristic.
  • Moves: each image is scored against its candidate classes, and against its own class at its current pose (it may stay), and moved to the argmin. The class sums are kept in double precision.
  • Batching: the angle and shift search is (optionally) batched. This allows for a more efficient implementation, at the cost of losing the pure mathematical formulation of Hartigan's algorithm. A batch of 128 is typically enough to achieve a performance similar to moving one image at a time (exact online Hartigan).
  • Lloyd: algorithm: lloyd uses the same search procedure with a different score: the plain residual \(\lVert x - A \mu(S_k) \rVert^2\) (\(W = 1\)), with no leave-one-out. The class averages are frozen during the sweep and rebuilt once per iteration (batch k-means).

Glossary: code names and the maths

code symbol meaning
images_fft \(x_i\) whitened Fourier coefficients of image i on the pixel list, (n_images, n_pixels)
ctfs \(C_i\) whitened CTF of image i on the same pixels
pixels (PixelList) \(\{k_j\}\) radius-sorted half-plane Fourier pixels; freq_x, freq_y = \(k_j\)
n_pixels, radius R pixels used up to the current Fourier radius (frequency marching)
n_pixels_coarse, coarse_radius \(R_c\) pixels used by the coarse search pass
assignments \(z_i\) class of each image
inplane_angles \(\psi_i\) in-plane angle (degrees); RELION's rlnAnglePsi = −ψ
shifts \(s_i\) particle centre in pixels; RELION's rlnOrigin = −s
padding_factor p oversampling of the class reference grid (RELION --pad)
grid_size G = pN side of the oversampled Fourier grid of a class
InterpolationTaps \(\omega_{jt}, g_{jt}\) bilinear neighbours and weights where pixel j reads the grid at angle ψ
ClassStats.backprojected_images \(b_k\) sum over members of \(R^H T^H C x\)
ClassStats.backprojected_ctf2 \(\beta_k\) sum over members of \(R^H C^2\) (diagonal of \(B_k\))
class_means \(\mu_k\) search template \(b_k / \beta_k\)
class_information \(h_k\) class information \(\beta_k\)
hartigan_weights() W \(h / (h + C^2)\)
score_all_shifts(), score_poses() S per-pixel Hartigan score
HartiganSweep.search_poses() coarse + fine RELION-style pose search
HartiganSweep.move_images() Hartigan moves of the whole batch at once
Driver iteration loop