#![allow(clippy::many_single_char_names)]
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::error::{Error, Result};
use crate::model::{FileInfo, ImageInfo};
use crate::pixel::Plane;
use crate::reader::{Dataset, PlaneIndex};
use crate::region::Region;
use crate::select::Selection;
mod lanes;
mod objects;
mod segment;
pub use lanes::{
Band, LANE_PROFILE_VALUES, LadderRequest, Lane, Lanes, LanesRequest, MAX_BANDS,
MAX_LANE_PIXELS, MAX_LANES, lanes_of_bytes,
};
pub use objects::{
CountConfidence, DEFAULT_MAX_OBJECTS, Kind, LOW_FIT, MAX_OBJECT_PIXELS, Object, ObjectChannel,
Objects, ObjectsRequest, Polarity, Positive, Stain, ThresholdMethod, default_channel,
label_plane,
};
pub const MAX_PROFILE_SAMPLES: usize = 20_000;
pub const PROFILE_VALUES: usize = 200;
pub const MAX_MEASURE_PIXELS: u64 = 1 << 23;
#[derive(Debug, Clone, Default)]
pub struct MeasureRequest {
pub image: Option<u32>,
pub select: Vec<String>,
pub level: u32,
pub region: Option<Region>,
pub line: Option<[f64; 4]>,
pub line_width: Option<u32>,
pub polygon: Option<Vec<[f64; 2]>>,
pub objects: Option<ObjectsRequest>,
pub lanes: Option<LanesRequest>,
}
impl MeasureRequest {
pub fn any(&self) -> bool {
self.line.is_some()
|| self.polygon.is_some()
|| self.objects.is_some()
|| self.lanes.is_some()
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
pub struct ProfileChannel {
pub c: u32,
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
pub min: f64,
pub max: f64,
pub mean: f64,
pub max_at_px: f64,
#[serde(skip_serializing_if = "Option::is_none")]
pub max_at_um: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub fwhm_px: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub fwhm_um: Option<f64>,
pub values: Vec<f64>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
pub struct Profile {
pub line: [f64; 4],
pub width_px: u32,
pub length_px: f64,
#[serde(skip_serializing_if = "Option::is_none")]
pub length_um: Option<f64>,
pub step_px: f64,
#[serde(skip_serializing_if = "Option::is_none")]
pub step_um: Option<f64>,
pub samples: usize,
pub channels: Vec<ProfileChannel>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
pub struct RoiChannel {
pub c: u32,
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
pub min: f64,
pub max: f64,
pub mean: f64,
pub std: f64,
pub median: f64,
pub sum: f64,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
pub struct Roi {
pub polygon: Vec<[f64; 2]>,
pub area_px: u64,
#[serde(skip_serializing_if = "Option::is_none")]
pub area_um2: Option<f64>,
pub channels: Vec<RoiChannel>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
pub struct Spread {
pub min: f64,
pub median: f64,
pub mean: f64,
pub max: f64,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
pub struct MeasureOutput {
pub path: String,
pub format: String,
pub image: u32,
pub level: u32,
pub downsample: f64,
pub z: u32,
pub t: u32,
#[serde(skip_serializing_if = "Option::is_none")]
pub pixel_size_um: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub profile: Option<Profile>,
#[serde(skip_serializing_if = "Option::is_none")]
pub roi: Option<Roi>,
#[serde(skip_serializing_if = "Option::is_none")]
pub objects: Option<Objects>,
#[serde(skip_serializing_if = "Option::is_none")]
pub lanes: Option<Lanes>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub notes: Vec<String>,
}
struct Grid {
x0: u32,
y0: u32,
w: usize,
h: usize,
comps: Vec<Vec<f64>>,
}
impl Grid {
fn from_plane(p: &Plane, r: Region) -> Result<Grid> {
let spp = p.samples_per_pixel.max(1) as usize;
let bps = p.pixel_type.bytes_per_sample();
let (w, h) = (p.width as usize, p.height as usize);
if (p.width, p.height) != (r.width, r.height) || w == 0 || h == 0 {
return Err(Error::corrupt(
"stats",
format!(
"the reader returned {w} x {h} pixels for a {} x {} region",
r.width, r.height
),
));
}
let need = w
.checked_mul(h)
.and_then(|n| n.checked_mul(spp))
.and_then(|n| n.checked_mul(bps))
.unwrap_or(usize::MAX);
if p.data.len() < need {
return Err(Error::corrupt(
"stats",
format!("plane holds {} bytes, {w} x {h} needs {need}", p.data.len()),
));
}
let mut comps = vec![Vec::with_capacity(w * h); spp];
for (i, s) in p
.data
.chunks_exact(bps)
.take(w * h * spp)
.map(|c| p.pixel_type.sample_f64(c))
.enumerate()
{
comps[i % spp].push(s);
}
Ok(Grid {
x0: r.x,
y0: r.y,
w,
h,
comps,
})
}
fn at(&self, k: usize, x: f64, y: f64) -> Option<f64> {
let fx = x - 0.5 - f64::from(self.x0);
let fy = y - 0.5 - f64::from(self.y0);
if !(fx >= -0.5 && fy >= -0.5 && fx <= self.w as f64 - 0.5 && fy <= self.h as f64 - 0.5)
|| self.w == 0
|| self.h == 0
{
return None;
}
let fx = fx.clamp(0.0, (self.w - 1) as f64);
let fy = fy.clamp(0.0, (self.h - 1) as f64);
let (ix, iy) = (fx.floor() as usize, fy.floor() as usize);
let (jx, jy) = ((ix + 1).min(self.w - 1), (iy + 1).min(self.h - 1));
let (ax, ay) = (fx - ix as f64, fy - iy as f64);
let c = &self.comps[k];
let v = |x: usize, y: usize| c[y * self.w + x];
Some(
v(ix, iy) * (1.0 - ax) * (1.0 - ay)
+ v(jx, iy) * ax * (1.0 - ay)
+ v(ix, jy) * (1.0 - ax) * ay
+ v(jx, jy) * ax * ay,
)
}
}
fn usage(m: impl Into<String>) -> Error {
Error::Usage(m.into())
}
struct Target<'a> {
im: &'a ImageInfo,
z: u32,
t: u32,
channels: Vec<u32>,
level_w: u32,
level_h: u32,
downsample: f64,
pixel_um: Option<f64>,
pixel_um_y: Option<f64>,
}
fn target<'a>(info: &'a FileInfo, req: &MeasureRequest) -> Result<Target<'a>> {
let Some(im) = (match req.image {
Some(i) => info.images.iter().find(|im| im.index == i),
None => info.images.first(),
}) else {
return Err(if info.images.is_empty() {
Error::unsupported(
"stats",
format!("image measurements of a {} file", info.format.name),
"This file holds no images; line, polygon and objects measure image planes.",
)
} else {
usage(format!(
"image {} does not exist (the file has {} images)",
req.image.unwrap_or(0),
info.images.len()
))
});
};
let sel = Selection::parse(&req.select)?;
let one = |axis: &[u32], size: u32, default: u32, name: &str| -> Result<u32> {
match axis {
[] => Ok(default),
[v] if *v < size => Ok(*v),
[v] => Err(usage(format!(
"{name}={v} is out of range (image {} has {size})",
im.index
))),
_ => Err(usage(format!(
"measurements read one plane: select one {name} (e.g. {name}={default})"
))),
}
};
let z = one(&sel.z, im.size_z.max(1), im.size_z.max(1) / 2, "z")?;
let t = one(&sel.t, im.size_t.max(1), 0, "t")?;
let channels: Vec<u32> = if sel.c.is_empty() {
(0..im.size_c.max(1)).collect()
} else {
if let Some(c) = sel.c.iter().find(|c| **c >= im.size_c.max(1)) {
return Err(usage(format!(
"c={c} is out of range (image {} has {} channels)",
im.index, im.size_c
)));
}
sel.c.clone()
};
let (level_w, level_h) = crate::region::level_size(im, req.level)?;
let downsample = if level_w > 0 {
f64::from(im.size_x) / f64::from(level_w)
} else {
1.0
};
let usable = |p: &f64| p.is_finite() && *p > 0.0;
let pixel_um = im.physical_size.x.map(|p| p * downsample).filter(usable);
let pixel_um_y = im
.physical_size
.y
.map(|p| p * f64::from(im.size_y) / f64::from(level_h.max(1)))
.filter(usable)
.filter(|py| pixel_um.is_some_and(|px| (py - px).abs() > px * 1e-6));
Ok(Target {
im,
z,
t,
channels,
level_w,
level_h,
downsample,
pixel_um,
pixel_um_y,
})
}
fn bounds(points: &[[f64; 2]], pad: f64, w: u32, h: u32) -> Result<Region> {
let (mut x0, mut y0, mut x1, mut y1) = (f64::MAX, f64::MAX, f64::MIN, f64::MIN);
for p in points {
if !(p[0].is_finite() && p[1].is_finite()) {
return Err(usage("coordinates must be finite numbers"));
}
x0 = x0.min(p[0]);
y0 = y0.min(p[1]);
x1 = x1.max(p[0]);
y1 = y1.max(p[1]);
}
let cx0 = (x0 - pad - 1.0).floor().max(0.0);
let cy0 = (y0 - pad - 1.0).floor().max(0.0);
let cx1 = (x1 + pad + 1.0).ceil().min(f64::from(w));
let cy1 = (y1 + pad + 1.0).ceil().min(f64::from(h));
if cx1 <= cx0 || cy1 <= cy0 {
return Err(usage(format!(
"the shape lies outside the plane, which is {w} x {h} pixels at this level"
)));
}
Ok(Region::new(
cx0 as u32,
cy0 as u32,
(cx1 - cx0) as u32,
(cy1 - cy0) as u32,
))
}
fn check_size(r: Region, what: &str) -> Result<()> {
let n = u64::from(r.width) * u64::from(r.height);
if n > MAX_MEASURE_PIXELS {
return Err(Error::unsupported(
"stats",
format!("{what} over {} x {} pixels", r.width, r.height),
format!(
"Measure at most {MAX_MEASURE_PIXELS} pixels: pass a smaller region, or a coarser level (coordinates are then that level's pixels)."
),
));
}
Ok(())
}
fn read(ds: &mut dyn Dataset, tg: &Target<'_>, level: u32, c: u32, r: Region) -> Result<Grid> {
let idx = PlaneIndex {
c,
z: tg.z,
t: tg.t,
};
let p = if r.is_full(tg.level_w, tg.level_h) && level == 0 {
ds.read_plane(tg.im.index, idx)?
} else {
ds.read_region(tg.im.index, idx, level, r)?
};
Grid::from_plane(&p, r)
}
fn labels(tg: &Target<'_>, c: u32, spp: usize) -> Vec<(usize, u32, Option<String>)> {
let name = tg
.im
.channels
.iter()
.find(|ch| ch.index == c)
.and_then(|ch| ch.name.clone().or_else(|| ch.fluorophore.clone()));
if spp == 1 {
return vec![(0, c, name)];
}
["R", "G", "B", "A"]
.iter()
.take(spp)
.enumerate()
.map(|(k, n)| (k, k as u32, Some((*n).to_string())))
.collect()
}
pub fn measure(
ds: &mut dyn Dataset,
info: &FileInfo,
req: &MeasureRequest,
) -> Result<MeasureOutput> {
let tg = target(info, req)?;
let mut out = MeasureOutput {
path: info.path.clone(),
format: info.format.id.clone(),
image: tg.im.index,
level: req.level,
downsample: tg.downsample,
z: tg.z,
t: tg.t,
pixel_size_um: tg.pixel_um,
..MeasureOutput::default()
};
if tg.im.size_z > 1 && !req.select.iter().any(|s| s.contains('z')) {
out.notes.push(format!(
"read z={} (the middle of {}); select z=N for another",
tg.z, tg.im.size_z
));
}
if let Some(line) = req.line {
out.profile = Some(profile(
ds,
&tg,
req.level,
line,
req.line_width.unwrap_or(1),
)?);
}
if let Some(poly) = &req.polygon {
out.roi = Some(roi(ds, &tg, req.level, poly)?);
}
if let Some(o) = &req.objects {
out.objects = Some(objects::objects(ds, &tg, req, o, &mut out.notes)?);
}
if let Some(l) = &req.lanes {
out.lanes = Some(lanes::lanes(
ds,
&tg,
req.level,
req.region,
l,
&mut out.notes,
)?);
}
Ok(out)
}
fn profile(
ds: &mut dyn Dataset,
tg: &Target<'_>,
level: u32,
line: [f64; 4],
width: u32,
) -> Result<Profile> {
let [x0, y0, x1, y1] = line;
let width = width.clamp(1, 1000);
let len = (x1 - x0).hypot(y1 - y0);
if !(len.is_finite() && len > 0.0) {
return Err(usage(
"line needs two different end points [x0, y0, x1, y1]",
));
}
let (pw, ph) = (f64::from(tg.level_w), f64::from(tg.level_h));
if [x0, x1].iter().any(|x| !(0.0..=pw).contains(x))
|| [y0, y1].iter().any(|y| !(0.0..=ph).contains(y))
{
return Err(usage(format!(
"the line {line:?} runs off the plane, which is {} x {} pixels at this level: keep x within 0..={} and y within 0..={}",
tg.level_w, tg.level_h, tg.level_w, tg.level_h
)));
}
let r = bounds(
&[[x0, y0], [x1, y1]],
f64::from(width) / 2.0 + 1.0,
tg.level_w,
tg.level_h,
)?;
check_size(r, "a profile")?;
let n = (len.ceil() as usize)
.saturating_add(1)
.clamp(2, MAX_PROFILE_SAMPLES);
let step = len / (n - 1) as f64;
let (ux, uy) = ((x1 - x0) / len, (y1 - y0) / len);
let offsets: Vec<f64> = (0..width)
.map(|k| f64::from(k) - f64::from(width - 1) / 2.0)
.collect();
let um_per_px = tg.pixel_um.map(|px| {
let py = tg.pixel_um_y.unwrap_or(px);
((x1 - x0) * px).hypot((y1 - y0) * py) / len
});
let k_out = n.min(PROFILE_VALUES);
let out_step = len / (k_out - 1) as f64;
let mut channels = Vec::new();
for &c in &tg.channels {
let g = read(ds, tg, level, c, r)?;
for (k, label, name) in labels(tg, c, g.comps.len()) {
let values: Vec<f64> = (0..n)
.map(|i| {
let d = step * i as f64;
let px = (x0 + ux * d).clamp(0.0, pw);
let py = (y0 + uy * d).clamp(0.0, ph);
let vals: Vec<f64> = offsets
.iter()
.filter_map(|o| g.at(k, px - uy * o, py + ux * o))
.collect();
if vals.is_empty() {
g.at(k, px, py).unwrap_or(f64::NAN)
} else {
vals.iter().sum::<f64>() / vals.len() as f64
}
})
.collect();
let fin: Vec<(usize, f64)> = values
.iter()
.copied()
.enumerate()
.filter(|(_, v)| v.is_finite())
.collect();
let Some(&(imax, max)) = fin.iter().max_by(|a, b| a.1.total_cmp(&b.1)) else {
return Err(usage("the line lies outside the plane"));
};
let min = fin.iter().map(|p| p.1).fold(f64::INFINITY, f64::min);
let mean = fin.iter().map(|p| p.1).sum::<f64>() / fin.len() as f64;
let fwhm_px = fwhm(&values, imax, min).map(|w| w * step);
channels.push(ProfileChannel {
c: label,
name,
min,
max,
mean,
max_at_px: imax as f64 * step,
max_at_um: um_per_px.map(|p| imax as f64 * step * p),
fwhm_px,
fwhm_um: fwhm_px.zip(um_per_px).map(|(w, p)| w * p),
values: resample(&values, k_out)
.iter()
.map(|v| round_sig(*v))
.collect(),
});
}
}
Ok(Profile {
line,
width_px: width,
length_px: len,
length_um: um_per_px.map(|p| p * len),
step_px: out_step,
step_um: um_per_px.map(|p| p * out_step),
samples: n,
channels,
})
}
fn resample(v: &[f64], k: usize) -> Vec<f64> {
if v.len() <= k || k < 2 {
return v.to_vec();
}
let end = v.len() - 1;
let last = end as f64;
let step = last / (k - 1) as f64;
(0..k)
.map(|i| {
let c = (i as f64 * step).min(last);
let half = (step / 2.0).min(c).min(last - c).max(0.0);
let lo = ((c - half).ceil().max(0.0) as usize).min(end);
let hi = ((c + half).floor().max(0.0) as usize).min(end);
let f: Vec<f64> = v[lo..=hi.max(lo)]
.iter()
.copied()
.filter(|x| x.is_finite())
.collect();
if f.is_empty() {
f64::NAN
} else {
f.iter().sum::<f64>() / f.len() as f64
}
})
.collect()
}
fn fwhm(v: &[f64], imax: usize, base: f64) -> Option<f64> {
let half = base + (v[imax] - base) / 2.0;
if v[imax] <= base {
return None;
}
let mut left = None;
for i in (0..imax).rev() {
if !v[i].is_finite() {
return None;
}
if v[i] < half {
let f = (half - v[i]) / (v[i + 1] - v[i]);
left = Some(i as f64 + f);
break;
}
}
let mut right = None;
for i in imax + 1..v.len() {
if !v[i].is_finite() {
return None;
}
if v[i] < half {
let f = (v[i - 1] - half) / (v[i - 1] - v[i]);
right = Some((i - 1) as f64 + f);
break;
}
}
Some(right? - left?)
}
fn round_sig(v: f64) -> f64 {
if !v.is_finite() || v == 0.0 {
return v;
}
let d = 6 - 1 - v.abs().log10().floor() as i32;
if d <= 0 {
return v.round();
}
let m = 10f64.powi(d.min(15));
(v * m).round() / m
}
fn inside(poly: &[[f64; 2]], x: f64, y: f64) -> bool {
let mut c = false;
let mut j = poly.len() - 1;
for i in 0..poly.len() {
let (xi, yi) = (poly[i][0], poly[i][1]);
let (xj, yj) = (poly[j][0], poly[j][1]);
if (yi > y) != (yj > y) && x < (xj - xi) * (y - yi) / (yj - yi) + xi {
c = !c;
}
j = i;
}
c
}
fn roi(ds: &mut dyn Dataset, tg: &Target<'_>, level: u32, poly: &[[f64; 2]]) -> Result<Roi> {
if poly.len() < 3 {
return Err(usage("polygon needs at least three vertices [[x, y], ...]"));
}
let r = bounds(poly, 0.0, tg.level_w, tg.level_h)?;
check_size(r, "a polygon")?;
let mut mask = Vec::new();
for y in 0..r.height {
for x in 0..r.width {
let (cx, cy) = (f64::from(r.x + x) + 0.5, f64::from(r.y + y) + 0.5);
if inside(poly, cx, cy) {
mask.push((y * r.width + x) as usize);
}
}
}
if mask.is_empty() {
return Err(usage("the polygon holds no pixel centre"));
}
let mut channels = Vec::new();
for &c in &tg.channels {
let g = read(ds, tg, level, c, r)?;
for (k, label, name) in labels(tg, c, g.comps.len()) {
let mut v: Vec<f64> = mask.iter().map(|&i| g.comps[k][i]).collect();
let n = v.len() as f64;
let sum: f64 = v.iter().sum();
let mean = sum / n;
let var = v.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / (n - 1.0).max(1.0);
v.sort_by(f64::total_cmp);
channels.push(RoiChannel {
c: label,
name,
min: v[0],
max: v[v.len() - 1],
mean,
std: var.sqrt(),
median: median_sorted(&v),
sum,
});
}
}
let area = mask.len() as u64;
Ok(Roi {
polygon: poly.to_vec(),
area_px: area,
area_um2: tg
.pixel_um
.map(|p| area as f64 * p * tg.pixel_um_y.unwrap_or(p)),
channels,
})
}
fn median_sorted(v: &[f64]) -> f64 {
let n = v.len();
if n == 0 {
return f64::NAN;
}
if n % 2 == 1 {
v[n / 2]
} else {
f64::midpoint(v[n / 2 - 1], v[n / 2])
}
}
fn spread(mut v: Vec<f64>) -> Option<Spread> {
if v.is_empty() {
return None;
}
v.sort_by(f64::total_cmp);
Some(Spread {
min: v[0],
median: median_sorted(&v),
mean: v.iter().sum::<f64>() / v.len() as f64,
max: v[v.len() - 1],
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn fwhm_of_a_triangle() {
let v = [0.0, 0.0, 1.0, 2.0, 3.0, 4.0, 3.0, 2.0, 1.0, 0.0, 0.0];
assert!((fwhm(&v, 5, 0.0).unwrap() - 4.0).abs() < 1e-9);
}
#[test]
fn polygon_inside() {
let sq = [[0.0, 0.0], [4.0, 0.0], [4.0, 4.0], [0.0, 4.0]];
assert!(inside(&sq, 2.0, 2.0));
assert!(!inside(&sq, 5.0, 2.0));
}
#[test]
fn rounding_keeps_six_digits() {
assert_eq!(round_sig(1234.56789), 1234.57);
assert_eq!(round_sig(0.000_123_456_78), 0.000_123_457);
assert_eq!(round_sig(65535.0), 65535.0);
}
#[test]
fn resampling_never_reads_past_the_end() {
for len in 2..1200u32 {
let v: Vec<f64> = (0..len).map(f64::from).collect();
for k in [2, 3, 7, 199, 200, 201] {
let b = resample(&v, k);
assert!(b.iter().all(|x| x.is_finite()), "{len} {k}");
assert_eq!(*b.last().unwrap(), f64::from(len - 1), "{len} {k}");
}
}
}
fn ramp(w: usize, h: usize) -> super::objects::tests::TwoChannels {
let mut ds = super::objects::tests::TwoChannels::new();
ds.w = w;
ds.h = h;
ds.planes = [
(0..w * h).map(|i| (i % w) as f32).collect(),
(0..w * h).map(|i| 1000.0 + (i / w) as f32).collect(),
];
ds
}
fn line(ds: &mut dyn Dataset, info: &FileInfo, l: [f64; 4], width: u32) -> Result<Profile> {
let req = MeasureRequest {
line: Some(l),
line_width: Some(width),
select: vec!["c=0".into()],
..MeasureRequest::default()
};
measure(ds, info, &req).map(|o| o.profile.unwrap_or_default())
}
#[test]
fn profiles_reach_the_edges_of_the_plane() {
for (w, h) in [(256, 100), (1, 1), (1, 40), (40, 1), (3, 3)] {
let mut ds = ramp(w, h);
let info = ds.info().unwrap();
let (fw, fh) = (w as f64, h as f64);
for l in [
[0.0, fh / 2.0, fw, fh / 2.0],
[0.0, 0.0, fw, 0.0],
[0.0, fh, fw, fh],
[0.0, 0.0, fw, fh],
[fw, fh, 0.0, 0.0],
] {
for width in [1, 3, 1000] {
let p = line(&mut ds, &info, l, width)
.unwrap_or_else(|e| panic!("{w}x{h} {l:?} {e}"));
let c = &p.channels[0];
assert!(
c.values.iter().all(|v| v.is_finite()),
"{w}x{h} {l:?} {:?}",
c.values
);
assert!(c.min.is_finite() && c.max.is_finite() && c.mean.is_finite());
let json = serde_json::to_string(&p).unwrap();
assert!(!json.contains("null"), "{json}");
}
}
}
let mut ds = ramp(256, 100);
let info = ds.info().unwrap();
let p = line(&mut ds, &info, [0.0, 50.0, 256.0, 50.0], 1).unwrap();
let c = &p.channels[0];
assert_eq!(c.values[0], 0.0);
assert_eq!(*c.values.last().unwrap(), 255.0);
assert!((c.max_at_px - 256.0).abs() < 1e-9, "{}", c.max_at_px);
}
#[test]
fn profiles_off_the_plane_are_usage_errors() {
let mut ds = ramp(64, 64);
let info = ds.info().unwrap();
for l in [
[-1.0, 5.0, 10.0, 5.0],
[0.0, 5.0, 64.5, 5.0],
[0.0, 65.0, 10.0, 65.0],
[f64::NAN, 0.0, 1.0, 1.0],
[0.0, 0.0, f64::INFINITY, 1.0],
[3.0, 3.0, 3.0, 3.0],
[1e300, 0.0, -1e300, 0.0],
] {
let e = line(&mut ds, &info, l, 1).unwrap_err();
assert!(matches!(e, Error::Usage(_)), "{l:?} {e}");
}
}
#[test]
fn odd_polygons_and_planes_do_not_panic() {
for (w, h) in [(1, 1), (1, 50), (50, 1), (7, 3)] {
let mut ds = ramp(w, h);
let info = ds.info().unwrap();
let (fw, fh) = (w as f64, h as f64);
let polys: Vec<Vec<[f64; 2]>> = vec![
vec![[0.0, 0.0], [fw, 0.0], [fw, fh], [0.0, fh]],
vec![
[0.0, 0.0],
[0.0, 0.0],
[fw, 0.0],
[fw, 0.0],
[fw, fh],
[0.0, fh],
],
vec![[0.0, 0.0], [fw / 2.0, 0.0], [fw, 0.0]],
vec![[0.0, 0.0], [fw, fh], [fw, 0.0], [0.0, fh]],
vec![[-100.0, -100.0], [1e6, -100.0], [1e6, 1e6], [-100.0, 1e6]],
vec![[1e300, 1e300], [2e300, 1e300], [2e300, 2e300]],
vec![[f64::NAN, 0.0], [1.0, 0.0], [1.0, 1.0]],
vec![[0.0, 0.0], [1.0, f64::INFINITY], [1.0, 1.0]],
];
for poly in polys {
let req = MeasureRequest {
polygon: Some(poly.clone()),
..MeasureRequest::default()
};
match measure(&mut ds, &info, &req) {
Ok(o) => {
let roi = o.roi.unwrap();
assert!(roi.area_px > 0);
assert!(roi.channels.iter().all(|c| c.mean.is_finite()));
}
Err(e) => assert!(matches!(e, Error::Usage(_)), "{w}x{h} {poly:?} {e}"),
}
}
}
let mut ds = ramp(10, 4);
let info = ds.info().unwrap();
let req = MeasureRequest {
polygon: Some(vec![[0.0, 0.0], [10.0, 0.0], [10.0, 4.0], [0.0, 4.0]]),
..MeasureRequest::default()
};
let roi = measure(&mut ds, &info, &req).unwrap().roi.unwrap();
assert_eq!(roi.area_px, 40);
}
#[test]
fn profile_values_are_evenly_spaced() {
let v: Vec<f64> = (0..1001).map(f64::from).collect();
let b = resample(&v, 201);
assert_eq!(b.len(), 201);
assert!((b[0] - 0.0).abs() < 1e-9, "{}", b[0]);
assert!((b[1] - 5.0).abs() < 1e-9, "{}", b[1]);
assert!((b[200] - 1000.0).abs() < 1e-9, "{}", b[200]);
assert_eq!(resample(&v[..10], 200).len(), 10);
}
}