Package: rustfrc
rustfrcrustfrc is a Python package with some fast Rust functions that are using when performing Fourier Ring Correlation (FRC) computations for resolution determination in microscopy (specifically optical nanoscopy). It is in development for use in a Bachelor end project for the TU Delft in the period 2021-2022.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 andmaturinsupport (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).FeaturesCurrently, rustfrc does not have many features. The primary one isbinom_split(x: ndarray) -> ndarraywhich 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) -> ndarrayandpois_gen(lam: float, shape: tuple[int, ...]) -> ndarray.sqr_abscomputes the element-wise norm and square of a complex array, whilepois_gengenerates an array of the specified size using the Poisson distribution and a single parameter λ.RequirementsPython 3.7 or greaterNumPy 1.18 or greaterInstallationYou can most easily installrustfrcas follows:pipinstallrustfrcHowever, for an optimal Python experience, usepoetryand install it usingpoetry add rustfrc.From source (using maturin)rustfrc usespoetryas its Python dependency manager. For best results, create apoetryvirtualenv (be sure to install virtualenv as a systems package) with thepyproject.tomland runpoetry updateto install the required packages.
Installingmaturinmanually should also work. It is also necessary to have a Rust toolchain installed on your computer. Rust can be easily installed usingrustup.Build a wheel file like this (if using poetry, appendpoetry runbefore the command) from the project directory:maturinbuild--releaseIf 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.whlfiles in/target/wheels. Then, create a Python environment of your choosing (withnumpy ^1.18andpython ^3.7), drop the.whlfile in it and runpip install <.whl filename>, for example:pip install rustfrc-0.1.0-cp39-none-win_amd64.whl. Then, useimport rustfrcin 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 atPyO3for 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.ManylinuxIf you want to build .whl files that are compatible with a wide range of Linux distributions and can be uploaded to PyPI, using amanylinuxcontainer is necessary.This example assumes a manylinux2014 (x86_64) target, using other docker base images should work to compile for other targets. Go into therustfrc/dockerdirectory and run:dockerpullquay.io/pypa/manylinux2014_x86_64
dockerbuild--no-cache-ttmtenbrink/manylinux2014-rustfrc--build-argPY_ABI=cp39-cp39.After the image is built, run it as a container:dockerrun-ittmtenbrink/manylinux2014-rustfrcThe repository should now be installed at/opt/rustfrc, with its dependencies andmaturininstalled. Then follow the same steps as above, but when the wheels are built run the following, which should resolve any issues:find.-name'*.whl'-execauditwheelrepair{}\;The complete wheel files can now be found in a/wheelhousedirectory.Take a look at the GitHub Actions for this project for an idea of how to automate this.