Metadata-Version: 2.5
Name: hr2d
Version: 0.1.0
Summary: Hartigan 2D classification for cryo-EM with RELION-style pose search (PyTorch/GPU)
Project-URL: repository, https://github.com/Lederman-Group/HR2D/
Author: Roey Yadgar
Author-email: David Silva Sanchez <david.silva@yale.edu>, "Roy R. Lederman" <roy.lederman@yale.edu>
License-Expression: GPL-3.0-or-later
License-File: LICENSE
Requires-Python: >=3.10
Requires-Dist: matplotlib
Requires-Dist: mrcfile
Requires-Dist: numpy
Requires-Dist: pandas
Requires-Dist: psutil
Requires-Dist: pyyaml
Requires-Dist: scipy
Requires-Dist: starfile
Requires-Dist: torch>=2.1
Requires-Dist: tqdm
Provides-Extra: dev
Requires-Dist: matplotlib; extra == 'dev'
Requires-Dist: pre-commit; extra == 'dev'
Requires-Dist: pytest; extra == 'dev'
Requires-Dist: ruff; extra == 'dev'
Requires-Dist: scikit-learn; extra == 'dev'
Provides-Extra: docs
Requires-Dist: mkdocs; extra == 'docs'
Requires-Dist: mkdocs-material; extra == 'docs'
Description-Content-Type: text/markdown

<h1 align='center'>Hartigan 2D classification of cryo-EM particle images with a RELION-style pose search</h1>


## Summary

`hr2d` is a GPU (PyTorch) program for cryo-EM 2D classification. It classifies particle images with an algorithm inspired by Hartigan's k-means algorithm. Our strategy is inspired by features in RELION's 2D classification. Specifically, we adapt their angle and shift search, frequency marching, and rotation operators; in addition to their starfile I/O interface and CTF definition.

Details and results are available in our paper (**link coming soon**). Instructions for reproducing
the paper results are [below](#reproducing-paper-results).

Full documentation is available at
[https://lederman-group.github.io/HR2D/](https://lederman-group.github.io/HR2D/).

## Installation

HR2D has been tested on Linux (RHEL 8) with NVIDIA V100, A100 and Quadro RTX 8000 GPUs, Python
3.11, PyTorch 2.5.1 and CUDA 12.4. An NVIDIA GPU is required for real datasets.

We recommend a python environment, with PyTorch installed first for your CUDA
version. For example, with `mamba`:

```bash
mamba create -n hr2d python==3.11
mamba activate hr2d
pip install torch --index-url https://download.pytorch.org/whl/cu124   # insert your CUDA version!
pip install "hr2d @ git+https://github.com/Lederman-Group/HR2D.git"
```

Choose a PyTorch CUDA build compatible with your NVIDIA driver using the PyTorch installation instructions (https://pytorch.org/get-started/locally/). The command above uses CUDA 12.4.

## Quick start

```bash
hr2d run config.yaml                                 # a RELION STAR file in, RELION outputs out
hr2d run config.yaml --set n_classes=20 --set search.psi_step=6
hr2d run config.yaml --resume                        # continue from out_dir/checkpoint.pt
```

A minimal `config.yaml`:

```yaml
star: /path/to/particles.star          # RELION 3.0 or 3.1+ particle STAR file
project_dir: /path/to/relion/project   # image paths in the STAR file are relative to it
out_dir: ./hr2d_run
n_classes: 50
iterations: 25
image_sign: light-on-dark              # or dark-on-light: particles darker than the background
```

## Tutorial

The [tutorial](docs/tutorial.md) classifies a small simulated dataset (four shapes at SNR 0.1,
26 MB) in under a minute.

## 2D classification input

See the [input documentation](docs/classification.md): every option of the YAML file, the outputs,
restarting, the cryoSPARC-style minibatch schedule and SLURM.

## Analysis and comparison with other programs

See the [analysis documentation](docs/analysis.md): converting RELION, cryoSPARC and Xmipp results to
one convention, and scoring every method with the same k-means loss, half-set resolutions and NMI.

## The method

See the [method page](docs/method.md): what is taken from RELION, what is Hartigan, and a glossary
from the code's names to the paper's symbols. The angle, origin and CTF conventions are in
[docs/relion_conventions.md](docs/relion_conventions.md).

## Reproducing paper results

All the input STAR files, run scripts and config files, every method's results, the simulated
datasets, the analysis scripts and the instructions to reproduce our results are available on
[Zenodo](https://doi.org/10.5281/zenodo.24563010) (DOI 10.5281/zenodo.24563010). The experimental
particle images are public on [EMPIAR](https://www.ebi.ac.uk/empiar/) (EMPIAR-10028, 10076, 10096
and 10180). These results were obtained with the version of HR2D tagged `paper-runs`.

## LLM disclosure

Claude was used to assist in writing the HR2D code, adapting our preliminary implementation in JAX and reimplementing select RELION strategies.

## License and copyright

Copyright (c) 2026 David Silva-Sánchez, Roy R. Lederman and Roey Yadgar.
Upstream portions retain their respective copyright notices.

HR2D is free software, distributed under the GNU General Public License,
version 3 or (at your option) any later version. See [LICENSE](LICENSE).

HR2D reimplements selected RELION strategies in Python/PyTorch, including
angle and shift search, rotation operators, CTF calculations, frequency
marching and STAR-file conventions. RELION's source code was consulted
during development.

When redistributing this code or modified versions, retain the applicable
HR2D and upstream copyright, license and warranty notices, and comply
with the GNU GPL.

The original RELION notice is reproduced below:

```text
Author: "Sjors H.W. Scheres"
MRC Laboratory of Molecular Biology

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

This complete copyright notice must be included in any revised version of the
source code. Additional authorship citations may be added, but existing
author citations must be preserved.
```

The CTF code (`src/hr2d/fourier/ctf.py`) also carries a copyright notice from Xmipp (CSIC)
authors. The complete notices for each adapted file are in its header.

## Citation

If you use `hr2d`, please cite our paper:

```bibtex
@unpublished{hr2d,
  title  = {The Case for Greed in Cryo-EM 2D Classification},
  author = {Silva-S{\'a}nchez, David and Yadgar, Roey and Lederman, Roy R.},
  note   = {Under review},
  year   = {2026},
}
```

## Contact

Please submit any bug reports, feature requests, or general feedback as a GitHub issue or
discussion.

## Contributing

If you are contributing to this project, please install the package with the following command:

```bash
pip install -e ".[dev]"
```

This installs the development dependencies, the most important being `ruff`, `pre-commit` and
`pytest`. After installation, activate your environment and install the `pre-commit` hooks:

```bash
pre-commit install
```

Make sure that your code is formatted and passes the tests before opening a pull request:

```bash
pre-commit run --all-files
pytest tests
```
