use crate::error::Error;
use crate::gaussian as gaussian_rs;
use crate::gaussian::{BackgroundSubtraction, ModalAnalysis, ModalAnalysisResult};
use numpy::{AllowTypeChange, IntoPyArray, PyArrayDyn, PyArrayLike1, PyArrayLikeDyn};
use pyo3::prelude::*;
use pyo3_stub_gen::derive::*;
#[gen_stub_pyfunction(module = "smoll.gaussian")]
#[pyfunction]
pub(crate) fn gaussian<'py>(
py: Python<'py>,
#[gen_stub(override_type(type_repr="numpy.typing.ArrayLike", imports=("numpy.typing")))] x: Vec<
PyArrayLike1<f64, AllowTypeChange>,
>,
#[gen_stub(override_type(type_repr="numpy.typing.ArrayLike", imports=("numpy.typing")))]
p: PyArrayLike1<f64, AllowTypeChange>,
) -> PyResult<Bound<'py, PyArrayDyn<f64>>> {
let x = x
.into_iter()
.map(|i| i.as_array().to_owned())
.collect::<Vec<_>>();
let xv = x.iter().map(|i| i.view()).collect::<Vec<_>>();
Ok(if let Ok(p) = p.as_slice() {
gaussian_rs::gaussian(xv, p)?
} else {
let p = p.as_array().to_vec();
gaussian_rs::gaussian(xv, &p)?
}
.into_pyarray(py))
}
#[gen_stub_pyfunction(module = "smoll.gaussian")]
#[pyfunction]
pub(crate) fn gaussian_pixel_int<'py>(
py: Python<'py>,
#[gen_stub(override_type(type_repr="numpy.typing.ArrayLike", imports=("numpy.typing")))] x: Vec<
PyArrayLike1<f64, AllowTypeChange>,
>,
#[gen_stub(override_type(type_repr="numpy.typing.ArrayLike", imports=("numpy.typing")))]
p: PyArrayLike1<f64, AllowTypeChange>,
) -> PyResult<Bound<'py, PyArrayDyn<f64>>> {
let x = x
.into_iter()
.map(|i| i.as_array().to_owned())
.collect::<Vec<_>>();
let xv = x.iter().map(|i| i.view()).collect::<Vec<_>>();
Ok(if let Ok(p) = p.as_slice() {
gaussian_rs::gaussian_pixel_int(xv, p)?
} else {
let p = p.as_array().to_vec();
gaussian_rs::gaussian_pixel_int(xv, &p)?
}
.into_pyarray(py))
}
#[gen_stub_pyclass]
#[pyclass(
subclass,
from_py_object,
name = "BackgroundSubtraction",
module = "smoll.gaussian"
)]
#[derive(Debug, Clone, PartialEq, Copy)]
pub(crate) struct PyBackgroundSubtraction {
inner: BackgroundSubtraction,
}
#[gen_stub_pymethods]
#[pymethods]
impl PyBackgroundSubtraction {
#[new]
fn new(t: String) -> PyResult<Self> {
Ok(Self {
inner: t.parse().map_err(Error::from)?,
})
}
fn __getnewargs__(&self) -> String {
format!("{:?}", self.inner)
}
fn __repr__(&self) -> String {
format!("{:?}", self.inner)
}
#[staticmethod]
fn none() -> Self {
Self {
inner: BackgroundSubtraction::None,
}
}
#[staticmethod]
fn constant() -> Self {
Self {
inner: BackgroundSubtraction::Constant,
}
}
#[staticmethod]
fn tilted() -> Self {
Self {
inner: BackgroundSubtraction::Tilted,
}
}
}
#[gen_stub_pyclass]
#[pyclass(
subclass,
from_py_object,
name = "ModelAnalysisResult",
module = "smoll.gaussian"
)]
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct PyModalAnalysisResult {
inner: ModalAnalysisResult,
}
#[gen_stub_pymethods]
#[pymethods]
impl PyModalAnalysisResult {
#[new]
fn new(
iterations: usize,
integrated_intensity: f64,
position: Vec<f64>,
sigma: Vec<f64>,
background: Vec<f64>,
) -> Self {
Self {
inner: ModalAnalysisResult::new(
iterations,
integrated_intensity,
position,
sigma,
background,
),
}
}
fn __repr__(&self) -> String {
format!("{:?}", self.inner)
}
fn __getnewargs__(&self) -> (&usize, &f64, &Vec<f64>, &Vec<f64>, &Vec<f64>) {
(
self.inner.iterations(),
self.inner.integrated_intensity(),
self.inner.position(),
self.inner.sigma(),
self.inner.background(),
)
}
#[getter]
fn iterations(&self) -> &usize {
self.inner.iterations()
}
#[getter]
fn integrated_intensity(&self) -> &f64 {
self.inner.integrated_intensity()
}
#[gen_stub(override_return_type(type_repr = "list[float]"))]
#[getter]
fn position(&self) -> &[f64] {
self.inner.position()
}
#[gen_stub(override_return_type(type_repr = "list[float]"))]
#[getter]
fn sigma(&self) -> &[f64] {
self.inner.sigma()
}
#[gen_stub(override_return_type(type_repr = "list[float]"))]
#[getter]
fn background(&self) -> &[f64] {
self.inner.background()
}
}
#[gen_stub_pyclass]
#[pyclass(
subclass,
from_py_object,
name = "ModelAnalysis",
module = "smoll.gaussian"
)]
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct PyModalAnalysis {
inner: ModalAnalysis,
}
#[gen_stub_pymethods]
#[pymethods]
impl PyModalAnalysis {
#[new]
#[pyo3(signature = (sigma, window_shape, max_iterations=20, tolerance=1e-2, background_subtraction=None, fix_location=false))]
fn new(
sigma: Vec<f64>,
window_shape: Vec<usize>,
max_iterations: usize,
tolerance: f64,
background_subtraction: Option<Bound<PyAny>>,
fix_location: bool,
) -> PyResult<Self> {
let background_subtraction = if let Some(background_subtraction) = background_subtraction {
if let Ok(background_subtraction) =
background_subtraction.extract::<PyBackgroundSubtraction>()
{
background_subtraction.inner
} else {
background_subtraction
.call_method0("__str__")?
.extract::<String>()?
.parse()
.map_err(Error::from)?
}
} else {
BackgroundSubtraction::None
};
Ok(Self {
inner: ModalAnalysis {
sigma,
window_shape,
max_iterations,
tolerance,
background_subtraction,
fix_location,
},
})
}
fn __repr__(&self) -> String {
format!("{:?}", self.inner)
}
fn __getnewargs__(&self) -> (&Vec<f64>, &Vec<usize>, &usize, &f64, String, &bool) {
(
&self.inner.sigma,
&self.inner.window_shape,
&self.inner.max_iterations,
&self.inner.tolerance,
format!("{:?}", self.inner.background_subtraction),
&self.inner.fix_location,
)
}
fn fit(
&self,
py: Python,
#[gen_stub(override_type(type_repr="numpy.typing.ArrayLike", imports=("numpy.typing")))]
g: PyArrayLikeDyn<f64, AllowTypeChange>,
#[gen_stub(override_type(type_repr="numpy.typing.ArrayLike", imports=("numpy.typing")))]
x: PyArrayLike1<f64, AllowTypeChange>,
) -> PyResult<PyModalAnalysisResult> {
let g = g.as_array();
let x = x.as_array();
let result = if let Some(x) = x.as_slice() {
py.detach(|| self.inner.fit(g, x))?
} else {
let x = x.to_vec();
py.detach(|| self.inner.fit(g, &x))?
};
Ok(PyModalAnalysisResult { inner: result })
}
fn with_sigma<'py>(
mut slf: PyRefMut<'py, Self>,
#[gen_stub(override_type(type_repr="numpy.typing.ArrayLike", imports=("numpy.typing")))]
sigma: PyArrayLike1<f64, AllowTypeChange>,
) -> PyRefMut<'py, Self> {
slf.inner.sigma = sigma.as_array().to_vec();
slf
}
#[gen_stub(override_return_type(type_repr = "list[float]"))]
#[getter]
fn get_sigma(&self) -> &[f64] {
self.inner.sigma.as_slice()
}
#[setter]
fn set_sigma(
&mut self,
#[gen_stub(override_type(type_repr="numpy.typing.ArrayLike", imports=("numpy.typing")))]
sigma: PyArrayLike1<f64, AllowTypeChange>,
) {
self.inner.sigma = sigma.as_array().to_vec();
}
fn with_window_shape<'py>(
mut slf: PyRefMut<'py, Self>,
#[gen_stub(override_type(type_repr="numpy.typing.ArrayLike", imports=("numpy.typing")))]
window_shape: PyArrayLike1<usize, AllowTypeChange>,
) -> PyRefMut<'py, Self> {
slf.inner.window_shape = window_shape.as_array().to_vec();
slf
}
#[gen_stub(override_return_type(type_repr = "list[int]"))]
#[getter]
fn get_window_shape(&self) -> &[usize] {
self.inner.window_shape.as_slice()
}
#[setter]
fn set_window_shape(
&mut self,
#[gen_stub(override_type(type_repr="numpy.typing.ArrayLike", imports=("numpy.typing")))]
window_shape: PyArrayLike1<usize, AllowTypeChange>,
) {
self.inner.window_shape = window_shape.as_array().to_vec();
}
fn with_max_iterations(
mut slf: PyRefMut<'_, Self>,
max_iterations: usize,
) -> PyRefMut<'_, Self> {
slf.inner.max_iterations = max_iterations;
slf
}
#[getter]
fn get_max_iterations(&self) -> usize {
self.inner.max_iterations
}
#[setter]
fn set_max_iterations(&mut self, max_iterations: usize) {
self.inner.max_iterations = max_iterations;
}
fn with_tolerance(mut slf: PyRefMut<'_, Self>, tolerance: f64) -> PyRefMut<'_, Self> {
slf.inner.tolerance = tolerance;
slf
}
#[getter]
fn get_tolerance(&self) -> f64 {
self.inner.tolerance
}
#[setter]
fn set_tolerance(&mut self, tolerance: f64) {
self.inner.tolerance = tolerance;
}
fn with_background_subtraction<'py>(
mut slf: PyRefMut<'py, Self>,
background_subtraction: Bound<'py, PyAny>,
) -> PyResult<PyRefMut<'py, Self>> {
if let Ok(background_subtraction) =
background_subtraction.extract::<PyBackgroundSubtraction>()
{
slf.inner.background_subtraction = background_subtraction.inner;
} else {
slf.inner.background_subtraction = background_subtraction
.call_method0("__str__")?
.extract::<String>()?
.parse()
.map_err(Error::from)?;
};
Ok(slf)
}
#[getter]
fn get_background_subtraction(&self) -> PyBackgroundSubtraction {
PyBackgroundSubtraction {
inner: self.inner.background_subtraction,
}
}
#[setter]
fn set_background_subtraction(
&mut self,
background_subtraction: Bound<'_, PyAny>,
) -> PyResult<()> {
if let Ok(background_subtraction) =
background_subtraction.extract::<PyBackgroundSubtraction>()
{
self.inner.background_subtraction = background_subtraction.inner;
} else {
self.inner.background_subtraction = background_subtraction
.call_method0("__str__")?
.extract::<String>()?
.parse()
.map_err(Error::from)?;
}
Ok(())
}
fn with_fix_location(mut slf: PyRefMut<'_, Self>, fix_location: bool) -> PyRefMut<'_, Self> {
slf.inner.fix_location = fix_location;
slf
}
#[getter]
fn get_fix_location(&self) -> bool {
self.inner.fix_location
}
#[setter]
fn set_fix_location(&mut self, fix_location: bool) {
self.inner.fix_location = fix_location;
}
}