use crate::detection;
use crate::detection::{AbsoluteThreshold, Detector, DoG, Filter, LoG, StdThreshold};
use numpy::{AllowTypeChange, IntoPyArray, PyArray2, PyArrayDyn, PyArrayLikeDyn};
use pyo3::prelude::*;
use pyo3_stub_gen::derive::*;
#[derive(Debug, Clone, PartialEq)]
enum Filters {
DoG(DoG),
LoG(LoG),
}
#[gen_stub_pyclass]
#[pyclass(subclass, from_py_object, name = "Filter", module = "smoll.detection")]
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct PyFilter {
inner: Filters,
}
#[gen_stub_pymethods]
#[pymethods]
impl PyFilter {
#[staticmethod]
fn dog(lower: Vec<f64>, upper: Vec<f64>) -> Self {
Self {
inner: Filters::DoG(DoG::new(lower, upper)),
}
}
#[staticmethod]
fn log() -> Self {
Self {
inner: Filters::LoG(LoG::new()),
}
}
fn filter<'py>(
&self,
py: Python<'py>,
#[gen_stub(override_type(type_repr="numpy.typing.ArrayLike", imports=("numpy.typing")))]
array: PyArrayLikeDyn<f64, AllowTypeChange>,
) -> PyResult<Bound<'py, PyArrayDyn<f64>>> {
let array = array.as_array();
Ok(match self.inner {
Filters::DoG(ref dog) => dog.filter_array(array)?,
Filters::LoG(ref log) => log.filter_array(array)?,
}
.into_pyarray(py))
}
}
#[gen_stub_pyfunction(module = "smoll.detection")]
#[pyfunction]
pub(crate) fn local_maxima<'py>(
py: Python<'py>,
#[gen_stub(override_type(type_repr="numpy.typing.ArrayLike", imports=("numpy.typing")))]
array: PyArrayLikeDyn<f64, AllowTypeChange>,
) -> Bound<'py, PyArray2<u64>> {
detection::local_maxima(array.as_array())
.mapv(|i| i as u64)
.into_pyarray(py)
}
#[derive(Debug, Clone, PartialEq)]
enum Detectors {
Absolute(AbsoluteThreshold),
Std(StdThreshold),
}
#[gen_stub_pyclass]
#[pyclass(
subclass,
from_py_object,
name = "Detector",
module = "smoll.detection"
)]
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct PyDetector {
inner: Detectors,
}
#[gen_stub_pymethods]
#[pymethods]
impl PyDetector {
#[staticmethod]
fn abs(threshold: f64) -> Self {
Self {
inner: Detectors::Absolute(AbsoluteThreshold { threshold }),
}
}
#[staticmethod]
fn std(threshold: f64) -> Self {
Self {
inner: Detectors::Std(StdThreshold { threshold }),
}
}
fn detect<'py>(
&self,
py: Python<'py>,
#[gen_stub(override_type(type_repr="numpy.typing.ArrayLike", imports=("numpy.typing")))]
array: PyArrayLikeDyn<f64, AllowTypeChange>,
) -> PyResult<Bound<'py, PyArray2<u64>>> {
let array = array.as_array();
Ok(match self.inner {
Detectors::Absolute(ref abs) => abs.detect(array)?,
Detectors::Std(ref std) => std.detect(array)?,
}
.mapv(|i| i as u64)
.into_pyarray(py))
}
}