https://github.com/tmtenbrink/rustfrc
rustfrc is a Python package with some fast Rust functions useful for FRC (Fourier Ring Correlation).
https://github.com/tmtenbrink/rustfrc
ndarray pyo3 python rust rust-ndarray
Last synced: about 1 month ago
JSON representation
rustfrc is a Python package with some fast Rust functions useful for FRC (Fourier Ring Correlation).
- Host: GitHub
- URL: https://github.com/tmtenbrink/rustfrc
- Owner: tmtenbrink
- License: apache-2.0
- Created: 2021-09-16T21:36:28.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-06-13T16:16:04.000Z (11 months ago)
- Last Synced: 2025-02-28T22:51:53.393Z (2 months ago)
- Topics: ndarray, pyo3, python, rust, rust-ndarray
- Language: Rust
- Homepage:
- Size: 1.66 MB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
- Citation: CITATION.cff
Awesome Lists containing this project
README
# rustfrc

rustfrc is a Python package with some fast Rust functions that are useful when performing Fourier Ring Correlation (FRC) computations for resolution determination in microscopy (specifically optical nanoscopy). It was originally developed for use in a Bachelor end project for the TU Delft in the period 2021-2022. See the Python package [`frc`](https://github.com/tmtenbrink/frc) for examples of its usage. The `test_split.py` file in the `tests` directory also holds some examples.
Since rustfrc contains compiled (Rust) extensions and is not pure Python, it is not available for all platforms, but only for those with available compiled wheels or a Rust toolchain and `maturin` support (see below). They are available for Windows (x86_64), macOS (x86_64 and universal2, which includes Apple Silicon) and Linux (x86_64). However, since Rust and Python are supported on many platforms, it is not difficult to compile for other platforms (see below).
## Features
rustfrc has only a few features. The primary one is `binom_split(x: ndarray) -> ndarray` which samples binomial _(n, 0.5)_ with n as the array element value. The operation is fully parallelized and somewhere between 3-10x faster than sampling using NumPy.
Furthermore, there are also (since version 1.1) `sqr_abs(a: ndarray) -> ndarray` and `pois_gen(lam: float, shape: tuple[int, ...]) -> ndarray`.
`sqr_abs` computes the element-wise norm and square of a complex array, while `pois_gen` generates an array of the specified size using the Poisson distribution and a single parameter λ.
## Requirements
* Python 3.8-3.12
* NumPy 1.18+ (exact version might depend on Python version, e.g. Python 3.12 requires NumPy 1.26)## Performance
On an i7-8750H, a decently high performance 6-core chip from 2017, I measured the following speeds:
- `binom_split`: ~210 ms on a 4000x4000 array, with each element Poisson-generated with a mean of 200
- `pois_gen`: ~420 ms to generate a 4000x4000 array with mean 200
- `sqr_abs`: ~40 ms on a 4000x4000 array, where each element is a complex number with both the real and imaginary parts having a mean of 200Take this with a grain of salt, but it should provide a decent order of magnitude for larger images.
## Installation
You can most easily install [rustfrc](https://pypi.org/project/rustfrc/) as follows:
```shell
pip install rustfrc
```However, for an optimal Python experience, use [poetry](https://github.com/python-poetry/poetry) and install it using `poetry add rustfrc`.
### From source (using maturin)
rustfrc uses [poetry](https://github.com/python-poetry/poetry) as its Python dependency manager. For best results, create a `poetry` virtualenv (be sure to install virtualenv as a systems package) with the `pyproject.toml` and run `poetry install` to install the required packages. I recommend installing [maturin](https://pypi.org/project/maturin/) as a global tool (e.g. with `cargo binstall`).
Build a wheel file like this (if using poetry, append `poetry run` before the command) from the project directory:
```shell
maturin build --release
```If you want to choose which versions of Python to build for, you can write e.g. `maturin build --release -i python3.9 python3.8 python3.7`. Here, for example '`python3.7`' should be an available Python command installed on your computer.
This generates `.whl` files in `/target/wheels`. Then, create a Python environment of your choosing (with `numpy ^1.18` and `python ^3.7`), drop the `.whl` file in it and run `pip install <.whl filename>`, for example: `pip install rustfrc-0.1.0-cp39-none-win_amd64.whl`. Then, use `import rustfrc` in your Python script to be able to use the Rust functions. This should be generally valid for all platforms. The only real requirement is the availability of a Rust toolchain and Python for your platform.
Take a look at [PyO3](https://github.com/PyO3/pyo3) for other installation options as the only true requirement for building is using a tool that understands PyO3 bindings, as those are used in the Rust code.
#### Manylinux
If you want to build .whl files that are compatible with a wide range of Linux distributions and can be uploaded to PyPI, take a look at the GitHub Actions files and [pyo3/maturin-action](https://github.com/PyO3/maturin-action).
## Development
### Poetry
First, install Poetry. Since we aim for compatibility with Python 3.8+, it's recommended to install Python 3.8 and create a virtual environment with:
`poetry env use `
Then, do `poetry shell` to activate the virtual environment.
Next, run `poetry install` to install the (development) dependencies.
Finally, build the Rust project using `maturin develop` (it's recommended to install `maturin` globally using `cargo binstall maturin` or `pipx install maturin`).
Run the tests using `pytest`.
Note that type information is not available for the Rust functions, you will have to look at the Rust source code. Maturin builds a package structures as follows:
- root `rustfrc` package
- `_internal`: this includes `binom_split_py`, etc.
- `split`: this is the Python source code in `python/rustfrc/split.py`