Skip to content

Commit

Permalink
Binomsplit in 2 and 3 dimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
tmtenbrink committed Sep 16, 2021
0 parents commit 336c150
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
Cargo.lock
Binary file added Benton.tiff
Binary file not shown.
18 changes: 18 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "rustfrc"
version = "0.1.0"
edition = "2018"

[lib]
name = "rustfrc"
crate-type = ["cdylib"]

[dependencies]
numpy = "0.14"
ndarray = { version = "0.15", features = ["rayon"] }
ndarray-rand = "0.14"
rand = "0.8"

[dependencies.pyo3]
version = "0.14"
features = ["extension-module"]
100 changes: 100 additions & 0 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[tool.poetry]
name = "rustfrc"
version = "0.1.0"
description = "Package with some basic optimized Rust functions for use with FRC resolution determination for microscopy. TU Delft BEP 2021-2022."
authors = ["Tip ten Brink <[email protected]>"]

[tool.poetry.dependencies]
python = "^3.7"
numpy = "^1.18"

[tool.poetry.dev-dependencies]
maturin = "^0.11.3"
diplib = "3.0.0"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend ="maturin"
31 changes: 31 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use ndarray_rand::rand_distr::Binomial;
use ndarray_rand::rand::prelude::Distribution;
use ndarray_rand::rand::thread_rng;
use numpy::{IntoPyArray, PyArray3, PyReadonlyArray3, PyReadonlyArray2, PyArray2};
use pyo3::prelude::{pymodule, PyModule, PyResult, Python};

#[pymodule]
fn rustfrc(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
#[pyfn(m)]
fn binom_split_3d<'py>(py: Python<'py>, x: PyReadonlyArray3<'py, i32>) -> PyResult<&'py PyArray3<i32>> {
let mut x = x.as_array().to_owned();
x.par_mapv_inplace(|i| {
let mut rng = thread_rng();
Binomial::new(i as u64, 0.5).unwrap().sample(&mut rng) as i32
});
Ok(x.into_pyarray(py))
}

#[pyfn(m)]
fn binom_split_2d<'py>(py: Python<'py>, x: PyReadonlyArray2<'py, i32>) -> PyResult<&'py PyArray2<i32>> {
let mut x = x.as_array().to_owned();
x.par_mapv_inplace(|i| {
let mut rng = thread_rng();
Binomial::new(i as u64, 0.5).unwrap().sample(&mut rng) as i32
});
Ok(x.into_pyarray(py))
}
Ok(())
}


24 changes: 24 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import rustfrc as r
import diplib as dip
import numpy as np
import time

rng = np.random.default_rng()
img = dip.ImageRead('Benton')(0)[:388, :388]
x = np.array(img)
#x3d = np.repeat(x[:, :, np.newaxis], 1000, axis=2)
half_x = np.rint(x/2).astype(int)
# half_x = np.rint(x/2).astype(int)
# print(half_x)
start = time.time_ns()
#a = rng.binomial(half_x, 0.5)
a = r.binom_split_2d(half_x)
b = half_x - a
# for i in range(100):
# a = rng.binomial(half_x, 0.5)
# b = half_x - a
end_time = time.time_ns()
print(str(float(end_time - start)/1e9) + " s")

#

0 comments on commit 336c150

Please sign in to comment.