Skip to content

Tutorial: four shapes at SNR 0.1

This tutorial classifies a small simulated dataset: 400 particles, each one of four filled shapes (rectangle, star, pentagon, triangle) at a random in-plane angle and shift, with a CTF and noise at a signal-to-noise ratio of 0.1. It runs in under a minute on one GPU.

Eight raw particles of the tutorial dataset

1. Download the data

Download tutorial.zip (26 MB) from the paper's Zenodo record and unzip it:

unzip tutorial.zip && cd tutorial

It contains:

  • dataset_geometric_01/img_00000.mrcs: the particles (128 px, 1.5 Å/px), light on a dark background.
  • dataset_geometric_01/starfile.star: their CTF parameters. It also holds the true angles and origins, which hr2d does not read: classes, angles and offsets all start from scratch.
  • dataset_geometric_01/true_labels.npy: the true shape of every particle, to check the result.
  • config_geometric.yaml: the config below.

2. The config

# hr2d tutorial: the simulated geometric set at SNR 0.1. Run from this folder: hr2d run config_geometric.yaml
star: dataset_geometric_01/starfile.star
project_dir: dataset_geometric_01
out_dir: hr2d_geometric
n_classes: 4
iterations: 20
image_sign: light-on-dark

Everything else is at its default value: RELION's GUI pose search (12° in-plane steps refined to 6°, offsets within 5 px of each particle's previous offset), frequency marching, and Hartigan moves in batches of 128 particles. See 2D classification for every option.

3. Run

hr2d run config_geometric.yaml

The log prints one line per iteration (the format is explained in 2D classification):

preprocessed 400 particles, box 128, apix 1.500 (0.4s); diameter 184.5 A, ini_high 21.3 A
it   1 R=19 Rc=15 moved=311 L_approx 572530.8 com=5.00px nextres=17.5A classes=4 0.7s
it   2 R=21 Rc=15 moved=178 L_approx 392330.3 com=3.39px nextres=9.6A classes=4 0.3s
it   3 R=30 Rc=15 moved=7 L_approx 598519.5 com=0.42px nextres=8.3A classes=4 0.4s
...
it  20 R=35 Rc=15 moved=0 L_approx 766454.5 com=0.00px nextres=7.7A classes=4 0.5s

After two iterations almost every particle has found its class. From iteration 8 on, no particle changes class, and the remaining iterations only refine the poses. The loss L_approx falls while the radius R stays the same; it rises when R grows (iterations 3 to 5), because more Fourier pixels are compared. Each iteration takes under a second on a Quadro RTX 4000.

4. The result

hr2d_geometric/run_it020_classes.png shows the four classes, sorted by size, with their populations and half-set resolutions:

The four classes after 20 iterations

To compare the classes with the true shapes (needs scikit-learn):

import numpy as np
import starfile
from sklearn.metrics import normalized_mutual_info_score

truth = np.load("dataset_geometric_01/true_labels.npy")
classes = starfile.read("hr2d_geometric/run_it020_data.star")["particles"]["rlnClassNumber"]
print(normalized_mutual_info_score(truth, classes))  # 1.0: every particle in its true class

run_it020_data.star holds every particle's class, in-plane angle and offset in RELION's conventions, so it can be opened in RELION or imported into cryoSPARC.

Next steps

  • Change the number of classes: --set n_classes=8 --set out_dir=hr2d_k8.
  • Compare with Lloyd k-means on the same search: --set algorithm=lloyd --set out_dir=lloyd.
  • On real data, start from the installation notes on SLURM and the config reference.