- added ome_xml method
- some pyo3 methods
This commit is contained in:
49
src/py.rs
49
src/py.rs
@@ -1,14 +1,51 @@
|
||||
use crate::{Frame, Reader};
|
||||
use numpy::{IntoPyArray, PyArrayMethods, ToPyArray};
|
||||
use pyo3::prelude::*;
|
||||
use pyo3::BoundObject;
|
||||
use std::path::PathBuf;
|
||||
|
||||
/// Formats the sum of two numbers as string.
|
||||
#[pyfunction]
|
||||
fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
|
||||
Ok((a + b).to_string())
|
||||
#[pyclass(subclass)]
|
||||
#[pyo3(name = "Reader")]
|
||||
#[derive(Debug)]
|
||||
struct PyReader {
|
||||
path: PathBuf,
|
||||
series: i32,
|
||||
}
|
||||
|
||||
#[pymethods]
|
||||
impl PyReader {
|
||||
#[new]
|
||||
fn new(path: &str, series: usize) -> PyResult<Self> {
|
||||
Ok(PyReader {
|
||||
path: PathBuf::from(path),
|
||||
series: series as i32,
|
||||
})
|
||||
}
|
||||
|
||||
fn get_frame<'py>(
|
||||
&self,
|
||||
py: Python<'py>,
|
||||
c: usize,
|
||||
z: usize,
|
||||
t: usize,
|
||||
) -> PyResult<Bound<'py, PyAny>> {
|
||||
let reader = Reader::new(&self.path, self.series)?; // TODO: prevent making a new Reader each time
|
||||
Ok(match reader.get_frame(c, z, t)? {
|
||||
Frame::INT8(arr) => arr.to_pyarray(py).into_any(),
|
||||
Frame::UINT8(arr) => arr.to_pyarray(py).into_any(),
|
||||
Frame::INT16(arr) => arr.to_pyarray(py).into_any(),
|
||||
Frame::UINT16(arr) => arr.to_pyarray(py).into_any(),
|
||||
Frame::INT32(arr) => arr.to_pyarray(py).into_any(),
|
||||
Frame::UINT32(arr) => arr.to_pyarray(py).into_any(),
|
||||
Frame::FLOAT(arr) => arr.to_pyarray(py).into_any(),
|
||||
Frame::DOUBLE(arr) => arr.to_pyarray(py).into_any(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// A Python module implemented in Rust.
|
||||
#[pymodule]
|
||||
#[pyo3(name = "ndbioimage_rs")]
|
||||
fn ndbioimage_rs(m: &Bound<'_, PyModule>) -> PyResult<()> {
|
||||
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
|
||||
m.add_class::<PyReader>()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user