- all tests passing

This commit is contained in:
w.pomp
2026-07-28 11:51:04 +02:00
parent 9f8764ee00
commit 0f315e66ca
5 changed files with 20 additions and 92 deletions
+1 -7
View File
@@ -112,7 +112,7 @@ where
mod tests { mod tests {
use super::*; use super::*;
use crate::julia_image; use crate::julia_image;
use ndarray::{Ix1, Ix2, array}; use ndarray::{Ix1, array};
use tiffwrite::IJTiffFile; use tiffwrite::IJTiffFile;
#[test] #[test]
@@ -142,12 +142,6 @@ mod tests {
Ok(()) Ok(())
} }
#[test]
fn gaussian_kernel_test2() -> Result<(), Box<dyn std::error::Error>> {
let k = gaussian_kernel::<Ix2>(&[60, 80], &[16.0, 16.0])?;
ndarray_npy::write_npy("/home/wim/tmp/kernel.npy", &k)?;
Ok(())
}
#[test] #[test]
fn fft_test() -> Result<(), Box<dyn std::error::Error>> { fn fft_test() -> Result<(), Box<dyn std::error::Error>> {
+6 -73
View File
@@ -167,15 +167,13 @@ impl Sampling {
fn index(&self) -> Vec<Vec<f64>> { fn index(&self) -> Vec<Vec<f64>> {
match self { match self {
Self::Fixed(index) => index.clone(), Self::Fixed(index) => index.clone(),
Self::Random((n_samples, shape, _cached)) => { Self::Random((n_samples, shape, _cached)) => rand::rng()
rand::rng()
.random_iter::<f64>() .random_iter::<f64>()
.take(n_samples * shape.len()) .take(n_samples * shape.len())
.chunks(shape.len()) .chunks(shape.len())
.into_iter() .into_iter()
.map(|c| c.zip_eq(shape.iter()).map(|(i, s)| i * s - 0.5).collect()) .map(|c| c.zip_eq(shape.iter()).map(|(i, s)| i * s - 0.5).collect())
.collect() .collect(),
}
} }
} }
} }
@@ -208,7 +206,7 @@ where
shape: Vec<f64>, shape: Vec<f64>,
center: Vec<f64>, center: Vec<f64>,
fixed: BSpline<0, D>, fixed: BSpline<0, D>,
moving: BSpline<3, D>, moving: BSpline<1, D>,
fixed_mu: FixedMu, fixed_mu: FixedMu,
minmax: [f64; 2], minmax: [f64; 2],
sampling: Sampling, sampling: Sampling,
@@ -226,7 +224,7 @@ where
{ {
pub fn new( pub fn new(
fixed: BSpline<0, D>, fixed: BSpline<0, D>,
moving: BSpline<3, D>, moving: BSpline<1, D>,
sampling: SamplingArg, sampling: SamplingArg,
n_bins: usize, n_bins: usize,
edge: f64, edge: f64,
@@ -523,8 +521,6 @@ mod tests {
use algos::optimization::{bfgs_minimize, gradient_descent_minimize}; use algos::optimization::{bfgs_minimize, gradient_descent_minimize};
use ndarray::{MeshIndex, array, meshgrid, stack}; use ndarray::{MeshIndex, array, meshgrid, stack};
use ndarray_npy::NpzWriter; use ndarray_npy::NpzWriter;
use num::integer::Roots;
use num::traits::FloatConst;
use std::fs::File; use std::fs::File;
#[test] #[test]
@@ -546,69 +542,6 @@ mod tests {
Ok(()) Ok(())
} }
#[test]
fn grad() -> Result<(), Box<dyn std::error::Error>> {
let mut rng = rand::rng();
let u1 = (&mut rng)
.random_iter::<f64>()
.take(1000)
.collect::<Vec<_>>();
let u2 = (&mut rng)
.random_iter::<f64>()
.take(1000)
.collect::<Vec<_>>();
let f = u1
.iter()
.zip_eq(u2.iter())
.map(|(i, j)| (-2.0 * i.ln()).sqrt() * (2.0 * f64::PI() * j).cos() - 1.0)
.collect::<Vec<_>>();
let m = u1
.iter()
.zip_eq(u2.iter())
.map(|(i, j)| (-2.0 * i.ln()).sqrt() * (2.0 * f64::PI() * j).sin() + 1.0)
.collect::<Vec<_>>();
let s = f.len().sqrt();
let n_bins = s.max(11);
let e = 2.0 / (n_bins - 3) as f64;
let first_bin = -5.0 - e;
let last_bin = 5.0 + e;
// println!("f: {:?}, m: {:?}", f, m);
// println!("first_bin: {}, last_bin: {}, n_bins: {}, e: {}", first_bin, last_bin, n_bins, e);
let w = vec![1.0; f.len()];
let dw = vec![vec![0.0]; f.len()];
let (jpdf, d_jpdf_m) = parzen(
1.0,
&[1.0],
&f,
&m,
&[],
&w,
&dw,
first_bin,
last_bin,
n_bins,
);
let pdf_f = jpdf.rows().into_iter().map(|i| i.sum()).collect::<Vec<_>>();
let pdf_m = jpdf
.columns()
.into_iter()
.map(|i| i.sum())
.collect::<Vec<_>>();
let d_pdf_m = d_jpdf_m
.columns()
.into_iter()
.map(|i| i.sum())
.collect::<Vec<_>>();
println!(
"pdf_f = {:?}\npdf_m = {:?}\nd_pdf_m = {:?}",
pdf_f.to_vec(),
pdf_m.to_vec(),
d_pdf_m.to_vec()
);
Ok(())
}
#[test] #[test]
fn metric() -> Result<(), Box<dyn std::error::Error>> { fn metric() -> Result<(), Box<dyn std::error::Error>> {
let im_a = array![0.0, 0.0, 1.0, 0.0, 1.0, 2.0, 1.0, 0.0, 1.0, 0.0, 0.0]; let im_a = array![0.0, 0.0, 1.0, 0.0, 1.0, 2.0, 1.0, 0.0, 1.0, 0.0, 0.0];
@@ -837,7 +770,7 @@ mod tests {
let a = array![0.0, 0.0, 1.0, 0.0, 1.0, 2.0, 1.0, 0.0, 1.0, 0.0, 0.0]; let a = array![0.0, 0.0, 1.0, 0.0, 1.0, 2.0, 1.0, 0.0, 1.0, 0.0, 0.0];
let b = array![0.0, 0.0, 1.0, 0.0, 1.0, 2.0, 1.0, 0.0, 1.0, 0.0, 0.0]; let b = array![0.0, 0.0, 1.0, 0.0, 1.0, 2.0, 1.0, 0.0, 1.0, 0.0, 0.0];
let fixed = BSpline::<0, _>::new(a.view()); let fixed = BSpline::<0, _>::new(a.view());
let moving = BSpline::<3, _>::new(b.view()); let moving = BSpline::<1, _>::new(b.view());
let mus = vec![0.0, 0.001]; let mus = vec![0.0, 0.001];
let mut npz = NpzWriter::new(File::create( let mut npz = NpzWriter::new(File::create(
@@ -918,7 +851,7 @@ mod tests {
let a = array![0.0, 0.0, 1.0, 0.0, 1.0, 2.0, 1.0, 0.0, 1.0, 0.0, 0.0]; let a = array![0.0, 0.0, 1.0, 0.0, 1.0, 2.0, 1.0, 0.0, 1.0, 0.0, 0.0];
let b = array![0.0, 0.0, 1.0, 0.0, 1.0, 2.0, 1.0, 0.0, 1.0, 0.0, 0.0]; let b = array![0.0, 0.0, 1.0, 0.0, 1.0, 2.0, 1.0, 0.0, 1.0, 0.0, 0.0];
let fixed = BSpline::<0, _>::new(a.view()); let fixed = BSpline::<0, _>::new(a.view());
let moving = BSpline::<3, _>::new(b.view()); let moving = BSpline::<1, _>::new(b.view());
let mus = Array1::linspace(-10.0, 10.0, 500); let mus = Array1::linspace(-10.0, 10.0, 500);
let s = a.len() as f64; let s = a.len() as f64;
+1 -1
View File
@@ -208,7 +208,7 @@ impl<'a, T> BaseIndexedIterMut<'a, T> {
} }
} }
/// An parallel iterator over array indices and mutable values. /// A parallel iterator over array indices and mutable values.
pub struct IndexedIterMut<'a, T>(BaseIndexedIterMut<'a, T>); pub struct IndexedIterMut<'a, T>(BaseIndexedIterMut<'a, T>);
/// An iterator over array indices and mutable values. /// An iterator over array indices and mutable values.
+4 -3
View File
@@ -64,6 +64,7 @@ impl RegistrationStep {
/// **NOT downsampled**. See: `itkMultiResolutionGaussianSmoothingPyramidImageFilter.hxx` /// **NOT downsampled**. See: `itkMultiResolutionGaussianSmoothingPyramidImageFilter.hxx`
/// ///
/// Schedule [8, 4, 2, 1] means σ = [4.0, 2.0, 1.0, 0.5] (spacing=1). /// Schedule [8, 4, 2, 1] means σ = [4.0, 2.0, 1.0, 0.5] (spacing=1).
/// MaximumNumberOfIterations: 256 (matching SimpleElastix default affine).
pub fn default_steps(ndim: usize, n: usize) -> Vec<Self> { pub fn default_steps(ndim: usize, n: usize) -> Vec<Self> {
let sigma_schedule: Vec<f64> = vec![4.0, 2.0, 1.0, 0.5]; let sigma_schedule: Vec<f64> = vec![4.0, 2.0, 1.0, 0.5];
let nlevels = sigma_schedule.len(); let nlevels = sigma_schedule.len();
@@ -81,7 +82,7 @@ impl RegistrationStep {
32, 32,
1e-6, 1e-6,
0.05, 0.05,
512, 256,
1.0, 1.0,
)); ));
} }
@@ -309,7 +310,7 @@ impl<D: Dimension> Registration<D> {
continue; continue;
} }
let bf = BSpline::<0, _>::new(f.view()); let bf = BSpline::<0, _>::new(f.view());
let bm = BSpline::<3, _>::new(m.view()); let bm = BSpline::<1, _>::new(m.view());
let metric = MattesMetric::new(bf, bm, samples, n_bins, edge)? let metric = MattesMetric::new(bf, bm, samples, n_bins, edge)?
.with_fixed_mu(self.fixed_mu.clone()); .with_fixed_mu(self.fixed_mu.clone());
@@ -409,7 +410,7 @@ impl<D: Dimension> Registration<D> {
} }
let bf = BSpline::<0, _>::new(f.view()); let bf = BSpline::<0, _>::new(f.view());
let bm = BSpline::<3, _>::new(m.view()); let bm = BSpline::<1, _>::new(m.view());
let n_samples = match &samples { let n_samples = match &samples {
SamplingArg::Fixed(n) => *n, SamplingArg::Fixed(n) => *n,
SamplingArg::Random(n) => *n, SamplingArg::Random(n) => *n,
+3 -3
View File
@@ -879,7 +879,7 @@ mod tests {
let f = gaussian_smooth(im_a.view(), &[*sigma_val; 2])?; let f = gaussian_smooth(im_a.view(), &[*sigma_val; 2])?;
let m = gaussian_smooth(im_b.view(), &[*sigma_val; 2])?; let m = gaussian_smooth(im_b.view(), &[*sigma_val; 2])?;
let bf = BSpline::<0, _>::new(f.view()); let bf = BSpline::<0, _>::new(f.view());
let bm = BSpline::<3, _>::new(m.view()); let bm = BSpline::<1, _>::new(m.view());
let metric = MattesMetric::new(bf, bm, SamplingArg::Random(3000), 128, edge)?; let metric = MattesMetric::new(bf, bm, SamplingArg::Random(3000), 128, edge)?;
let mi_id = metric.evaluate(&identity); let mi_id = metric.evaluate(&identity);
@@ -907,7 +907,7 @@ mod tests {
let f4 = gaussian_smooth(im_a.view(), &[4.0, 4.0])?; let f4 = gaussian_smooth(im_a.view(), &[4.0, 4.0])?;
let m4 = gaussian_smooth(im_b.view(), &[4.0, 4.0])?; let m4 = gaussian_smooth(im_b.view(), &[4.0, 4.0])?;
let bf4 = BSpline::<0, _>::new(f4.view()); let bf4 = BSpline::<0, _>::new(f4.view());
let bm4 = BSpline::<3, _>::new(m4.view()); let bm4 = BSpline::<1, _>::new(m4.view());
let metric4 = MattesMetric::new(bf4, bm4, SamplingArg::Random(5000), 128, edge)?; let metric4 = MattesMetric::new(bf4, bm4, SamplingArg::Random(5000), 128, edge)?;
let mi_p_coarse = metric4.evaluate(&p); let mi_p_coarse = metric4.evaluate(&p);
let mi_qinv_coarse = metric4.evaluate(&q_inv); let mi_qinv_coarse = metric4.evaluate(&q_inv);
@@ -951,7 +951,7 @@ mod tests {
let m = gaussian_smooth(im_b.view(), &[4.0, 4.0])?; let m = gaussian_smooth(im_b.view(), &[4.0, 4.0])?;
let metric = MattesMetric::new( let metric = MattesMetric::new(
BSpline::<0, _>::new(f.view()), BSpline::<0, _>::new(f.view()),
BSpline::<3, _>::new(m.view()), BSpline::<1, _>::new(m.view()),
SamplingArg::Random(3000), SamplingArg::Random(3000),
128, 128,
edge, edge,