- some workarounds to get jars and shared libs in the right place for python

- add most ndbioimage python code and use rs code as bfread
This commit is contained in:
Wim Pomp
2025-02-16 23:02:40 +01:00
parent fefdd6448b
commit 83ea9722f6
19 changed files with 3036 additions and 24 deletions

View File

@@ -10,11 +10,51 @@ thread_local! {
/// Ensure 1 jvm per thread
fn jvm() -> Rc<Jvm> {
JVM.with(|cell| {
cell.get_or_init(move || Rc::new(JvmBuilder::new().build().expect("Failed to build JVM")))
.clone()
cell.get_or_init(move || {
#[cfg(feature = "python")]
let path = crate::py::ndbioimage_file().unwrap();
#[cfg(not(feature = "python"))]
let path = std::env::current_exe()
.unwrap()
.parent()
.unwrap()
.to_path_buf();
let class_path = path.parent().unwrap();
Rc::new(
JvmBuilder::new()
.with_base_path(class_path.to_str().unwrap())
.build()
.expect("Failed to build JVM"),
)
})
.clone()
})
}
#[cfg(feature = "python")]
pub(crate) fn download_bioformats(gpl_formats: bool) -> Result<()> {
let path = crate::py::ndbioimage_file().unwrap();
let class_path = path.parent().unwrap();
let jvm = JvmBuilder::new()
.with_base_path(class_path.to_str().unwrap())
.with_maven_settings(j4rs::MavenSettings::new(vec![
j4rs::MavenArtifactRepo::from(
"openmicroscopy::https://artifacts.openmicroscopy.org/artifactory/ome.releases",
),
]))
.build()?;
jvm.deploy_artifact(&j4rs::MavenArtifact::from("ome:bioformats_package:8.1.0"))?;
if gpl_formats {
jvm.deploy_artifact(&j4rs::MavenArtifact::from("ome:formats-gpl:8.1.0"))?;
}
Ok(())
}
macro_rules! method_return {
($R:ty$(|c)?) => { Result<$R> };
() => { Result<()> };
@@ -113,7 +153,7 @@ impl ImageReader {
Ok(jvm()
.chain(&mds)?
.cast("loci.formats.ome.OMEPyramidStore")?
.invoke("dumpXML", &[])?
.invoke("dumpXML", InvocationArg::empty())?
.to_rust()?)
}

View File

@@ -215,7 +215,7 @@ impl Reader {
}
/// Get ome metadata as xml string
pub fn ome_xml(&self) -> Result<String> {
pub fn get_ome_xml(&self) -> Result<String> {
self.image_reader.ome_xml()
}
@@ -396,7 +396,7 @@ mod tests {
fn ome_xml() -> Result<()> {
let file = "Experiment-2029.czi";
let reader = open(file)?;
let xml = reader.ome_xml()?;
let xml = reader.get_ome_xml()?;
println!("{}", xml);
Ok(())
}

View File

@@ -1,7 +1,7 @@
use crate::bioformats::download_bioformats;
use crate::{Frame, Reader};
use numpy::{IntoPyArray, PyArrayMethods, ToPyArray};
use numpy::ToPyArray;
use pyo3::prelude::*;
use pyo3::BoundObject;
use std::path::PathBuf;
#[pyclass(subclass)]
@@ -41,11 +41,35 @@ impl PyReader {
Frame::DOUBLE(arr) => arr.to_pyarray(py).into_any(),
})
}
fn get_ome_xml(&self) -> PyResult<String> {
let reader = Reader::new(&self.path, self.series)?; // TODO: prevent making a new Reader each time
Ok(reader.get_ome_xml()?)
}
}
pub(crate) fn ndbioimage_file() -> anyhow::Result<PathBuf> {
let file = Python::with_gil(|py| {
py.import("ndbioimage")
.unwrap()
.filename()
.unwrap()
.to_string()
});
Ok(PathBuf::from(file))
}
#[pymodule]
#[pyo3(name = "ndbioimage_rs")]
fn ndbioimage_rs(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<PyReader>()?;
#[pyfn(m)]
#[pyo3(name = "download_bioformats")]
fn py_download_bioformats(gpl_formats: bool) -> PyResult<()> {
download_bioformats(gpl_formats)?;
Ok(())
}
Ok(())
}