Skip to content

2D classification

A 2D classification is run from the command line with a YAML config file:

hr2d run config.yaml

Any option of the config file can be overridden on the command line. We also include an option to resume an interrupted run:

hr2d run config.yaml --set n_classes=20 --set search.psi_step=6
hr2d run config.yaml --image_sign dark-on-light      # same as --set image_sign=dark-on-light
hr2d run config.yaml --resume                        # continue from out_dir/checkpoint.pt

Input data

hr2d reads a RELION particle STAR file (RELION 3.0, or 3.1 and later with an optics table) and the .mrcs with the particle images. Image paths in the STAR file are relative to project_dir.

Only the CTF is read

hr2d reads only the CTF parameters from the STAR file (defocus, astigmatism, phase shift, voltage, spherical aberration, amplitude contrast, pixel size). Classes, in-plane angles and offsets all start from scratch: every particle starts in a class assigned round robin after a random shuffle, at a random in-plane angle and at zero offset. Set use_star_offsets: true to start the offsets from the STAR file's rlnOriginX/YAngst instead.

The particles are expected light on a dark background (RELION's convention). If they are dark on a light background, set image_sign: dark-on-light and they will be inverted on loading. Check the contrast on the raw pixels: a wrong sign gives classes that look like noise.

To classify at a smaller box than the stacks, set preprocess.downsample_box: the images are Fourier-cropped when they are loaded, and the original stacks are not changed.

Input format

A sample config file with every option at its default value is shown below. Only star is required.

# hr2d run config: every option, at its default value.
# Parameters marked with (*) are optional. Any option can also be set on the command line:
#   hr2d run config.yaml --set n_classes=20 --set search.psi_step=6

star: /path/to/particles.star          # RELION 3.0 or 3.1+ particle STAR file (only its CTF is read)
project_dir: /path/to/relion/project   # (*) image paths in the STAR file are relative to it
out_dir: runs/run                      # (*) created if missing

n_classes: 50                          # (*) K
iterations: 25                         # (*) full passes over the particles
algorithm: hartigan                    # (*) "hartigan" or "lloyd" (batch k-means, same search)
seed: 0                                # (*) initial classes, angles and visiting order

# Particle contrast in the raw images: "light-on-dark" (light particles on a dark background,
# RELION's convention) or "dark-on-light" (inverted on loading, as RELION's --invert_contrast).
# Check it on the raw pixels: a wrong sign gives classes that look like noise.
image_sign: light-on-dark              # (*)

particle_diameter: null                # (*) Å; default (box - 5) * pixel size, as RELION.
                                       #     Sets the Fourier radius of the coarse search pass
ini_high: null                         # (*) Å; first resolution, default box * apix / round(0.07 box)
incr_size: 10                          # (*) Fourier pixels added beyond the estimated resolution
tau2_fudge: 2.0                        # (*) T of the SSNR resolution rule (RELION --tau2_fudge)
full_resolution: false                 # (*) true: no frequency marching, always up to Nyquist

padding_factor: 2                      # (*) oversampling of the class reference grid (RELION --pad)
interpolation: bilinear                # (*) "bilinear" or "nearest"
gridding_correction: false             # (*) RELION's griddingCorrect on the class means
reference_mask: box                    # (*) "box" (circle at the box radius), "diameter"
                                       #     (soft circle of particle_diameter) or "none"
reference_mask_edge: 5.0               # (*) soft edge (px) of the "diameter" mask
center_classes: true                   # (*) move each class's centre of mass to the box centre

use_star_offsets: false                # (*) true: start the offsets from the STAR file's rlnOrigin
max_shift_fraction: 0.2                # (*) |shift| <= this fraction of the box size

half_set_resolution: true              # (*) per-class half-set FRC resolutions and a class gallery
                                       #     every iteration

# GPU memory. "auto" keeps every particle on the GPU when they fit in gpu_memory_fraction of the
# free GPU memory; otherwise particles are streamed in chunks, kept in host memory ("cpu") or
# re-read from the stacks every pass ("lazy"). Nothing is written to disk.
particles_on_gpu: auto                 # (*) "auto" or a number of particles per chunk
particle_storage: auto                 # (*) "auto", "gpu", "cpu" or "lazy"
gpu_memory_fraction: 0.4               # (*)
cpu_memory_fraction: 0.5               # (*)
device: cuda                           # (*)

preprocess:
  downsample_box: null                 # (*) Fourier-crop the images to this box (null: keep)
  window_inner: 0.85                   # (*) soft circular window: starts at this fraction of the
  window_outer: 0.99                   # (*)     box radius and reaches zero here
  whiten: true                         # (*) whiten with the noise spectrum of the box corners
  normalize: true                      # (*) rescale each image to unit corner-noise power
  max_images_for_noise: 5000           # (*) images used to estimate the noise spectrum
  chunk_size: 2048                     # (*) images preprocessed at once
  image_padding: 1                     # (*) zero-pad the images by this factor before the FFT

search:
  pose_search: relion                  # (*) "relion" or "hierarchical" (see below)
  psi_step: 12.0                       # (*) coarse in-plane angle step, degrees (RELION --psi_step)
  offset_range: 5.0                    # (*) translation search radius around the previous offset,
                                       #     pixels (RELION --offset_range)
  offset_step: 2.0                     # (*) translation step, pixels (RELION --offset_step)
  oversampling: 1                      # (*) fine pass: psi and shifts at step / 2 (0: no fine pass)
  n_candidate_poses: 2                 # (*) coarse poses kept per class for the fine pass
  n_candidate_classes: 5               # (*) classes refined in the fine pass (plus the own class)
  batch_size: 128                      # (*) images searched and moved at once (1: exact online
                                       #     Hartigan, one image at a time)
  images_per_block: 8                  # (*) images per matrix-product block in the coarse pass
  # pose_search: hierarchical only
  hierarchical_base_level: 2           # (*) base grid of 6 * 2**level angles (2: 24 angles, 15°)
  hierarchical_n_rounds: 5             # (*) refinements of the angle grid after the first
  hierarchical_n_angles_kept: 6        # (*) angles kept per class and level
  hierarchical_shift_range: null       # (*) px, square window around 0 (null: the max shift)

# cryoSPARC-style schedule, off by default (online_iterations: 0). When on, the run has
# online_iterations + full_iterations iterations and `iterations` is ignored.
minibatch:
  online_iterations: 0                 # (*) cryoSPARC's "number of online-EM iterations" is 20
  particles_per_class: 100             # (*) minibatch size = particles_per_class * n_classes
  full_iterations: 1                   # (*) full passes at the end

The options that mirror a RELION flag keep RELION's name and meaning: psi_step, offset_range, offset_step, oversampling, ini_high, incr_size, tau2_fudge, particle_diameter and padding_factor (--pad). The defaults are those of RELION's 2D classification GUI.

Choosing the algorithm and the schedule

  • algorithm: hartigan (default): 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.
  • algorithm: lloyd: Lloyd (batch) k-means with the same pose search and model. In this case assignments and alignments are decided by comparing to the current class averages through Euclidean distance. The class means are rebuilt once per iteration.
  • search.pose_search: relion (default): RELION's local search. A coarse pass over the in-plane angle grid and the translations within offset_range of each particle's previous offset, then an oversampled fine pass around the best candidates.
  • search.pose_search: hierarchical: a coarse-to-fine in-plane angle tree (24 angles at 15° down to 0.23°) and an exhaustive whole-pixel shift search around the box centre. Finer poses, at about twice the cost.
  • minibatch.online_iterations: 20: a cryoSPARC-style schedule. Online iterations each visit one minibatch of particles_per_class × n_classes particles, taken in a random order; only the particles already visited are in the classes. Then full_iterations full passes. This is several times faster than 25 full passes, at a higher final loss. With so few particles in the classes a class can empty; this schedule then refills it with a random half of the largest class (ordinary runs leave an empty class empty, as RELION's EM classification does).

Outputs

Every iteration NNN writes, in out_dir:

  • run_itNNN_classes.mrcs: the class means, one image per class, at the box and pixel size of the classification.
  • run_itNNN_data.star: the particles, with rlnClassNumber (1-based), rlnAnglePsi and rlnOriginX/YAngst in RELION's conventions, and the input CTF columns. It can be read by RELION, or imported into cryoSPARC.
  • run_itNNN_model.star: RELION's model file (class distribution, current resolution).
  • run_itNNN_class_resolution.csv, run_itNNN_class_frc.npy and run_itNNN_classes.png (with half_set_resolution: true): each class's population and half-set FRC resolutions (0.5 and 0.143 thresholds), and a gallery of the classes sorted by size.

and, once per run:

  • config.json: the full configuration of the run, defaults included.
  • checkpoint.pt: the state after the last finished iteration, for --resume.
  • run.log: the command line, the preprocessing, and one line per iteration:
    it   3 R=30 Rc=15 moved=7 L_approx 598519.5 com=0.42px nextres=8.3A classes=4 0.4s
    
    R and Rc are the Fourier radii of the fine and coarse passes, moved the particles that changed class, L_approx the loss of the iteration, com the largest class re-centring, nextres the resolution of the next iteration, and the last number the wall time of the iteration. For computational purposes, L_approx is an approximation of the loss \(L(S, A)\) collected during the pose search, at no extra cost (see The loss); the exact \(L(S, A)\) is computed by scripts/kmeans_loss.py.

Conventions

hr2d writes RELION's conventions: rlnAnglePsi = -ψ and rlnOriginX/YAngst = -s × pixel size, where ψ and s are hr2d's in-plane angle and particle centre. See Conventions for these and the CTF.