#![allow(clippy::many_single_char_names)]
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use super::segment as sg;
use super::{Grid, Spread, Target, inside, read, spread, usage};
use crate::error::{Error, Result};
use crate::model::ImageInfo;
use crate::region::Region;
pub const DEFAULT_MAX_OBJECTS: usize = 100;
pub const LOW_FIT: f64 = 0.5;
const ONE_PIECE: u64 = 1 << 23;
const TILE: u32 = 2048;
pub const MAX_OBJECT_PIXELS: u64 = 1 << 26;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "lowercase")]
pub enum Kind {
#[default]
Nuclei,
Cells,
Spots,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "lowercase")]
pub enum Polarity {
#[default]
Auto,
Bright,
Dark,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "lowercase")]
pub enum ThresholdMethod {
#[default]
Auto,
Otsu,
Triangle,
Local,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "lowercase")]
pub enum CountConfidence {
#[default]
High,
Low,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "lowercase")]
pub enum Stain {
#[default]
Auto,
Hematoxylin,
Eosin,
Dab,
Red,
Green,
Blue,
Luminance,
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct ObjectsRequest {
#[serde(default)]
pub kind: Kind,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub diameter_um: Option<f64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub diameter_px: Option<f64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub threshold: Option<f64>,
#[serde(default)]
pub method: ThresholdMethod,
#[serde(default)]
pub polarity: Polarity,
#[serde(default)]
pub stain: Stain,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub split: Option<bool>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub min_area_um2: Option<f64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max_area_um2: Option<f64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max_objects: Option<usize>,
#[serde(skip)]
#[schemars(skip)]
pub outlines: bool,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
pub struct Object {
pub id: u32,
pub x: f64,
pub y: f64,
pub area_px: u64,
#[serde(skip_serializing_if = "Option::is_none")]
pub area_um2: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub diameter_um: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub solidity: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub height: Option<f64>,
pub mean: Vec<f64>,
#[serde(skip_serializing_if = "std::ops::Not::not")]
pub edge: bool,
#[serde(skip)]
#[schemars(skip)]
pub outline: Vec<[f64; 2]>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
pub struct Positive {
pub threshold: f64,
pub method: String,
pub count: usize,
pub fraction: f64,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
pub struct ObjectChannel {
pub c: u32,
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
pub background: f64,
#[serde(skip_serializing_if = "Option::is_none")]
pub mean: Option<Spread>,
#[serde(skip_serializing_if = "Option::is_none")]
pub positive: Option<Positive>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
pub struct Objects {
pub kind: Kind,
pub measured: String,
pub region: Region,
#[serde(skip_serializing_if = "Option::is_none")]
pub searched_um2: Option<f64>,
pub expected_diameter_px: f64,
#[serde(skip_serializing_if = "Option::is_none")]
pub expected_diameter_um: Option<f64>,
pub expected_diameter_from: String,
pub polarity: Polarity,
pub threshold: f64,
pub threshold_method: String,
pub split: bool,
pub count: usize,
pub confidence: CountConfidence,
#[serde(skip_serializing_if = "Option::is_none")]
pub fit: Option<f64>,
pub touching_edge: usize,
#[serde(skip_serializing_if = "Option::is_none")]
pub density_per_mm2: Option<f64>,
pub area_fraction: f64,
pub large: usize,
#[serde(skip_serializing_if = "Option::is_none")]
pub area_px: Option<Spread>,
#[serde(skip_serializing_if = "Option::is_none")]
pub area_um2: Option<Spread>,
#[serde(skip_serializing_if = "Option::is_none")]
pub diameter_um: Option<Spread>,
pub channels: Vec<ObjectChannel>,
pub objects: Vec<Object>,
pub truncated: bool,
}
#[derive(Debug, Clone)]
pub(crate) struct Signal {
pub stain: Option<Stain>,
pub i0: [f32; 3],
pub dark: bool,
pub flip: f32,
}
impl Signal {
fn deconvolved(&self) -> bool {
matches!(
self.stain,
Some(Stain::Hematoxylin | Stain::Eosin | Stain::Dab)
)
}
fn of(&self, comps: &[Vec<f32>]) -> Vec<f32> {
let n = comps.first().map_or(0, Vec::len);
let base: Vec<f32> = match self.stain {
None => comps[0].clone(),
Some(Stain::Red) => comps[0].clone(),
Some(Stain::Green) => comps[1.min(comps.len() - 1)].clone(),
Some(Stain::Blue) => comps[2.min(comps.len() - 1)].clone(),
Some(Stain::Luminance) => {
let k = comps.len().min(3);
(0..n)
.map(|i| comps[..k].iter().map(|c| c[i]).sum::<f32>() / k as f32)
.collect()
}
Some(Stain::Auto) => (0..n)
.map(|i| comps.iter().take(3).map(|c| c[i]).fold(f32::MIN, f32::max))
.collect(),
Some(s @ (Stain::Hematoxylin | Stain::Eosin | Stain::Dab)) => {
return stains(comps, self.i0)[stain_index(s)].clone();
}
};
if self.dark {
base.iter().map(|v| self.flip - v).collect()
} else {
base
}
}
}
fn stain_index(s: Stain) -> usize {
match s {
Stain::Eosin => 1,
Stain::Dab => 2,
_ => 0,
}
}
fn stains(comps: &[Vec<f32>], i0: [f32; 3]) -> [Vec<f32>; 3] {
let n = comps[0].len();
let inv = sg::unmixing([sg::HEMATOXYLIN, sg::EOSIN, sg::DAB]).unwrap_or([[0.0; 3]; 3]);
let mut out = [vec![0f32; n], vec![0f32; n], vec![0f32; n]];
for i in 0..n {
let od = [
f64::from(sg::optical_density(comps[0][i], i0[0])),
f64::from(sg::optical_density(comps[1][i], i0[1])),
f64::from(sg::optical_density(comps[2][i], i0[2])),
];
for (j, o) in out.iter_mut().enumerate() {
o[i] = (od[0] * inv[0][j] + od[1] * inv[1][j] + od[2] * inv[2][j]).max(0.0) as f32;
}
}
out
}
fn stain_misfit(comps: &[Vec<f32>], i0: [f32; 3]) -> f64 {
let Some(inv) = sg::unmixing([sg::HEMATOXYLIN, sg::EOSIN, sg::DAB]) else {
return 0.0;
};
let (mut stained, mut bad) = (0usize, 0usize);
for i in (0..comps[0].len()).step_by(7) {
let od = [0, 1, 2].map(|k| f64::from(sg::optical_density(comps[k][i], i0[k])));
if od.iter().sum::<f64>() < 0.3 {
continue;
}
stained += 1;
let worst = (0..3)
.map(|j| od[0] * inv[0][j] + od[1] * inv[1][j] + od[2] * inv[2][j])
.fold(f64::INFINITY, f64::min);
if worst < -0.15 {
bad += 1;
}
}
if stained == 0 {
0.0
} else {
bad as f64 / stained as f64
}
}
fn looks_dark(v: &[f32]) -> bool {
let n = v.len().max(1) as f64;
let (Some(p1), Some(p50), Some(p99), Some(top)) = (
sg::quantile(v, 0.01),
sg::quantile(v, 0.5),
sg::quantile(v, 0.99),
sg::quantile(v, 0.999f64.max(1.0 - 20.0 / n)),
) else {
return false;
};
(p50 - p1) > 1.5 * (p99 - p50) || (top > 0.0 && p1 > 0.12 * top && p50 > 0.5 * top)
}
fn signal_for(
comps: &[Vec<f32>],
o: &ObjectsRequest,
notes: &mut Vec<String>,
) -> Result<(Signal, String)> {
let rgb = comps.len() >= 3;
if !rgb && o.stain != Stain::Auto {
return Err(usage(
"stain is for RGB images; this image has one value per pixel (choose a channel with select c=N)",
));
}
if rgb {
let lum: Vec<f32> = (0..comps[0].len())
.map(|i| (comps[0][i] + comps[1][i] + comps[2][i]) / 3.0)
.collect();
let brightfield = match o.polarity {
Polarity::Auto => {
looks_dark(&lum)
|| match (sg::quantile(&lum, 0.5), sg::quantile(&lum, 0.999)) {
(Some(mid), Some(top)) => top > 0.0 && mid > 0.3 * top,
_ => false,
}
}
p => p == Polarity::Dark,
};
let i0 = [0, 1, 2].map(|k| sg::quantile(&comps[k], 0.99).unwrap_or(255.0).max(1.0));
let blue_nuclei = !brightfield
&& o.kind == Kind::Nuclei
&& i0[2] >= 32.0
&& i0[2] >= 0.25 * i0[0].max(i0[1]);
let stain = match (o.stain, brightfield) {
(Stain::Auto, true) => Stain::Hematoxylin,
(Stain::Auto, false) if blue_nuclei => {
notes.push(
"nuclei counted in the blue component (where DAPI and Hoechst are drawn); stain=red|green picks another"
.into(),
);
Stain::Blue
}
(s, _) => s,
};
let deconvolved = matches!(stain, Stain::Hematoxylin | Stain::Eosin | Stain::Dab);
if deconvolved {
let misfit = stain_misfit(comps, i0);
if misfit > 0.3 {
notes.push(format!(
"{:.0}% of the stained pixels have colours that hematoxylin, eosin and DAB do not explain (another stain, such as a trichrome?): the {} amounts may be wrong; check a picture (preview marks=objects), or count in one colour with stain=red|green|blue",
misfit * 100.0,
serde_json::to_value(stain)
.ok()
.and_then(|v| v.as_str().map(str::to_string))
.unwrap_or_default()
));
}
}
let dark = brightfield && !deconvolved;
let name = match stain {
Stain::Auto => "brightest component".into(),
s => serde_json::to_value(s)
.ok()
.and_then(|v| v.as_str().map(str::to_string))
.unwrap_or_default(),
};
let flip = if dark {
sg::quantile(&lum, 0.999).unwrap_or(255.0)
} else {
0.0
};
return Ok((
Signal {
stain: Some(stain),
i0,
dark,
flip,
},
name,
));
}
let dark = match o.polarity {
Polarity::Auto => looks_dark(&comps[0]),
p => p == Polarity::Dark,
};
Ok((
Signal {
stain: None,
i0: [0.0; 3],
dark,
flip: if dark {
sg::quantile(&comps[0], 0.999).unwrap_or(0.0)
} else {
0.0
},
},
String::new(),
))
}
fn measure_diameter(
s: &[f32],
w: usize,
h: usize,
kind: Kind,
signal: &Signal,
range: (f64, f64),
typical: Option<f64>,
) -> Option<(f64, bool)> {
let background = !signal.deconvolved();
let plausible = |d: &f64| *d >= range.0.max(4.0) && *d <= range.1.min((w.min(h) as f64) / 3.0);
let distinct = |cands: &[f64], d: f64| cands.iter().all(|c| (c / d).max(d / c) > 1.3);
let mut cands: Vec<f64> = Vec::new();
{
let mut add = |d: Option<f64>| {
if let Some(d) = d.filter(plausible)
&& distinct(&cands, d)
{
cands.push(d);
}
};
let (compact, peaks) = mask_sizes(s, w, h);
add(compact.or(peaks));
add(sg::blob_diameter(s, w, h));
if !signal.dark {
let k = (w.min(h) / 128).max(2);
let (small, sw, sh) = sg::shrink(s, w, h, k);
let sm = sg::gaussian(&small, sw, sh, 1.5);
let (compact, peaks) = mask_sizes(&sm, sw, sh);
add(peaks.map(|d| d * k as f64));
add(compact.map(|d| d * k as f64));
}
}
let measured = cands.len();
if let Some(t) = typical.filter(|t| *t <= w.min(h) as f64)
&& distinct(&cands, t)
{
cands.push(t);
}
if cands.len() <= 1 {
return cands.pop().map(|d| (d, measured == 0));
}
let q = |d: f64| {
let mut plan = Plan::new(kind, d, background, ThresholdMethod::Otsu);
let (ws, ww, wh, k) = working(s, w, h, &plan);
let Some((t, _)) = sg::otsu(&scaled(&plan, k).flatten(&ws, ww, wh)) else {
return 0.0;
};
plan.threshold = t;
quality(s, w, h, &plan)
};
let mut best = (q(cands[0]), 0usize);
for (i, &d) in cands.iter().enumerate().skip(1) {
let qd = q(d);
if qd > best.0 {
best = (qd, i);
}
}
Some((cands[best.1], best.1 >= measured))
}
fn quality(s: &[f32], w: usize, h: usize, plan: &Plan) -> f64 {
let side = (8.0 * plan.d).max(768.0) as usize;
let (cw, ch) = (w.min(side), h.min(side));
let (x0, y0) = ((w - cw) / 2, (h - ch) / 2);
let crop: Vec<f32> = (0..ch)
.flat_map(|y| s[(y0 + y) * w + x0..(y0 + y) * w + x0 + cw].iter().copied())
.collect();
let (lab, _) = label_tile(&crop, cw, ch, plan);
fit(&shapes(&lab, cw, ch), plan.d)
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) enum Doubt {
Shapes(f64),
Specks,
Crowded,
}
impl Doubt {
fn note(self, count: usize, d: f64, kind: Kind) -> String {
let why = match self {
Doubt::Shapes(f) => format!(
"only {:.0}% of the objects' area is in compact objects about {d:.0} px across; this image may not suit counting {} (cytoplasm, another stain or debris counted, or touching objects merged)",
f * 100.0,
kind_name(kind)
),
Doubt::Specks => format!(
"most of the {count} objects are under a third of the area of a {d:.0} px disc: the threshold probably cut background noise or texture into specks"
),
Doubt::Crowded => format!(
"{count} objects about {d:.0} px across would need more than the area searched: the threshold probably cut background noise or texture into pieces"
),
};
format!(
"low confidence: {why}. Do not quote the count without checking a picture (preview marks=objects); set diameter_um, threshold, another channel or stain if the picture shows why"
)
}
}
pub(crate) fn assess(
kind: Kind,
shapes: &[(f64, f64)],
d: f64,
searched: f64,
) -> (Option<f64>, Option<Doubt>) {
if kind == Kind::Spots || shapes.is_empty() {
return (None, None);
}
let f = fit(shapes, d);
let expected = std::f64::consts::PI * d * d / 4.0;
let mut areas: Vec<f64> = shapes.iter().map(|s| s.0).collect();
let doubt = if f < LOW_FIT {
Some(Doubt::Shapes(f))
} else if shapes.len() >= 10 && median(&mut areas).is_some_and(|a| a < expected / 3.0) {
Some(Doubt::Specks)
} else if shapes.len() as f64 * expected > searched {
Some(Doubt::Crowded)
} else {
None
};
(Some(f), doubt)
}
fn fit(shapes: &[(f64, f64)], d: f64) -> f64 {
let expected = std::f64::consts::PI * d * d / 4.0;
let (mut good, mut all) = (0.0, 0.0);
for &(area, solidity) in shapes {
all += area;
let r = area / expected;
if solidity >= 0.85 && (0.3..=3.0).contains(&r) {
good += area;
}
}
if all > 0.0 { good / all } else { 0.0 }
}
fn shapes(lab: &[u32], w: usize, h: usize) -> Vec<(f64, f64)> {
let n = lab.iter().copied().max().unwrap_or(0) as usize;
let mut area = vec![0usize; n + 1];
let mut pts: Vec<Vec<[f64; 2]>> = vec![Vec::new(); n + 1];
for (i, &l) in lab.iter().enumerate() {
if l == 0 {
continue;
}
area[l as usize] += 1;
let (x, y) = (i % w, i / w);
let rim = x == 0
|| y == 0
|| x + 1 == w
|| y + 1 == h
|| lab[i - 1] != l
|| lab[i + 1] != l
|| lab[i - w] != l
|| lab[i + w] != l;
if rim {
let (fx, fy) = (x as f64, y as f64);
pts[l as usize].extend([
[fx, fy],
[fx + 1.0, fy],
[fx, fy + 1.0],
[fx + 1.0, fy + 1.0],
]);
}
}
(1..=n)
.filter(|l| area[*l] > 0)
.map(|l| {
let hull = sg::hull_area(&mut pts[l]);
let a = area[l] as f64;
(a, if hull > 0.0 { (a / hull).min(1.0) } else { 1.0 })
})
.collect()
}
fn mask_sizes(s: &[f32], w: usize, h: usize) -> (Option<f64>, Option<f64>) {
let sm = sg::gaussian(s, w, h, 1.0);
let Some((t, _)) = sg::otsu(&sm) else {
return (None, None);
};
let mut fg: Vec<bool> = sm.iter().map(|v| *v > t).collect();
sg::fill_holes(&mut fg, w, h, (w * h) / 100);
let (lab, n) = sg::components(&fg, w, h);
if n == 0 {
return (None, None);
}
let mut compact = Vec::new();
let mut all = 0usize;
for (area, solidity) in shapes(&lab, w, h) {
if area < 12.0 {
continue;
}
all += 1;
if solidity >= 0.85 {
compact.push(2.0 * (area / std::f64::consts::PI).sqrt());
}
}
let compact = if compact.len() >= 5 && compact.len() * 3 >= all {
median(&mut compact)
} else {
None
};
let dist = sg::distance(&fg, w, h);
let (seeds, k) = sg::seeds(&dist, &fg, w, h, 3, 2.0);
let mut depth = vec![0f32; k as usize + 1];
for (i, &l) in seeds.iter().enumerate() {
if l > 0 {
depth[l as usize] = depth[l as usize].max(dist[i]);
}
}
let mut d: Vec<f64> = depth
.iter()
.skip(1)
.map(|r| 2.0 * f64::from(*r) * 1.1)
.collect();
(compact, median(&mut d))
}
fn median(v: &mut [f64]) -> Option<f64> {
if v.is_empty() {
return None;
}
v.sort_by(f64::total_cmp);
Some(super::median_sorted(v))
}
#[derive(Debug, Clone)]
pub(crate) struct Plan {
pub kind: Kind,
pub d: f64,
pub split: bool,
pub threshold: f32,
pub method: ThresholdMethod,
pub min_area: usize,
pub max_area: usize,
pub background: bool,
}
impl Plan {
fn new(kind: Kind, d: f64, background: bool, method: ThresholdMethod) -> Plan {
let expected = std::f64::consts::PI * d * d / 4.0;
Plan {
kind,
d,
split: kind != Kind::Spots,
threshold: 0.0,
method,
min_area: if kind == Kind::Spots {
1
} else {
(expected / 5.0).max(1.0) as usize
},
max_area: usize::MAX,
background,
}
}
pub(crate) fn flatten(&self, s: &[f32], w: usize, h: usize) -> Vec<f32> {
if self.kind == Kind::Spots {
let sm = sg::gaussian(s, w, h, spot_sigma(self.d).min(1.0));
let bg = sg::background(&sm, w, h, (self.d * 3.0).max(6.0));
return sm.iter().zip(&bg).map(|(a, b)| a - b).collect();
}
let sm = sg::gaussian(s, w, h, (self.d / 20.0).max(0.6));
if !self.background {
return sm;
}
let bg = sg::background(&sm, w, h, self.d);
sm.iter().zip(&bg).map(|(a, b)| a - b).collect()
}
}
fn spot_sigma(d: f64) -> f64 {
(d / (2.0 * std::f64::consts::SQRT_2)).max(0.7)
}
const LARGE_OBJECTS: f64 = 64.0;
const MAX_WORKING_FACTOR: usize = 1 << 12;
fn working_factor(p: &Plan) -> usize {
if p.kind == Kind::Spots || p.d < LARGE_OBJECTS {
1
} else {
((p.d / 32.0).floor().max(1.0) as usize).min(MAX_WORKING_FACTOR)
}
}
fn working(s: &[f32], w: usize, h: usize, p: &Plan) -> (Vec<f32>, usize, usize, usize) {
let f = working_factor(p);
if f == 1 {
return (s.to_vec(), w, h, 1);
}
let (small, sw, sh) = sg::shrink(s, w, h, f);
(small, sw, sh, f)
}
fn scaled(p: &Plan, k: usize) -> Plan {
let mut q = p.clone();
if k > 1 {
q.d = p.d / k as f64;
q.min_area = (p.min_area / k.saturating_mul(k)).max(1);
}
q
}
pub(crate) fn label_tile(s: &[f32], w: usize, h: usize, p: &Plan) -> (Vec<u32>, Vec<f32>) {
if p.kind == Kind::Spots {
return spots(s, w, h, p);
}
let (ws, sw, sh, f) = working(s, w, h, p);
if f == 1 {
return (label_objects(&ws, w, h, p), Vec::new());
}
let lab_s = label_objects(&ws, sw, sh, &scaled(p, f));
let mut lab = vec![0u32; w * h];
for y in 0..h {
for x in 0..w {
lab[y * w + x] = lab_s[(y / f) * sw + x / f];
}
}
(lab, Vec::new())
}
fn label_objects(s: &[f32], w: usize, h: usize, p: &Plan) -> Vec<u32> {
let f = p.flatten(s, w, h);
let mut fg = foreground(&f, w, h, p);
let expected = std::f64::consts::PI * p.d * p.d / 4.0;
sg::fill_holes(&mut fg, w, h, (expected * 0.5) as usize);
let mut lab = if p.split {
let r = ((p.d * 0.1).round() as usize).max(1);
let closed = sg::rank_filter(&sg::rank_filter(&f, w, h, r, true), w, h, r, false);
let t = p.threshold.abs().max(f32::EPSILON);
let fall: Vec<f32> = closed
.iter()
.zip(&f)
.map(|(c, v)| (c - v) / c.max(t))
.collect();
let texture = texture(&fall, &sg::distance(&fg, w, h), p.d);
let seam = SEAM_FALL.max(texture + SEAM_OVER_TEXTURE);
let cut: Vec<bool> = fg.iter().zip(&fall).map(|(g, x)| *g && *x < seam).collect();
let dist = sg::distance(&cut, w, h);
let m = sg::gaussian(&dist, w, h, (p.d * 0.08).max(0.7));
let radius = ((p.d * 0.25).round() as usize).max(1);
let floor = (p.d * 0.05) as f32;
let (seeds, _) = sg::seeds(&m, &fg, w, h, radius, floor);
let mut lab = sg::watershed(&m, &fg, &seeds, w, h);
let rule = sg::JoinRule {
small: (expected * 0.4) as usize,
seam: (texture + SEAM_OVER_TEXTURE).max(0.2),
convexity: 0.08,
};
sg::join_small_pieces(&mut lab, w, h, &f, &rule);
lab
} else {
sg::components(&fg, w, h).0
};
sg::tidy_labels(&mut lab, w, h, p.min_area);
lab
}
const SEAM_FALL: f32 = 0.4;
const SEAM_OVER_TEXTURE: f32 = 0.2;
fn texture(fall: &[f32], depth: &[f32], d: f64) -> f32 {
let inner: Vec<f32> = fall
.iter()
.zip(depth)
.filter(|(_, z)| f64::from(**z) > d / 8.0)
.map(|(x, _)| *x)
.collect();
sg::quantile(&inner, 0.95).unwrap_or(0.0).clamp(0.0, 1.0)
}
fn foreground(f: &[f32], w: usize, h: usize, p: &Plan) -> Vec<bool> {
if p.method == ThresholdMethod::Local {
let local = sg::gaussian(f, w, h, p.d);
return f
.iter()
.zip(&local)
.map(|(v, l)| *v > l.max(0.0) + 0.5 * p.threshold.max(0.0) && *v > 0.5 * p.threshold)
.collect();
}
f.iter().map(|v| *v > p.threshold).collect()
}
fn spots(s: &[f32], w: usize, h: usize, p: &Plan) -> (Vec<u32>, Vec<f32>) {
let resp = sg::log_filter(s, w, h, spot_sigma(p.d));
let height = p.flatten(s, w, h);
let fg = vec![true; w * h];
let r = (p.d / 2.0).ceil().max(1.0) as usize;
let (peaks, n) = sg::seeds(&resp, &fg, w, h, r, f32::MIN_POSITIVE);
let mut first = vec![usize::MAX; n as usize + 1];
for (i, &l) in peaks.iter().enumerate() {
if l > 0 && first[l as usize] == usize::MAX {
first[l as usize] = i;
}
}
let mut lab = vec![0u32; w * h];
let mut heights = Vec::new();
let rr = (p.d / 2.0).max(1.0);
for &i in first.iter().skip(1) {
if i == usize::MAX || height[i] < p.threshold {
continue;
}
let id = heights.len() as u32 + 1;
heights.push(height[i]);
let (cx, cy) = ((i % w) as isize, (i / w) as isize);
let ri = rr.ceil() as isize;
for dy in -ri..=ri {
for dx in -ri..=ri {
let (x, y) = (cx + dx, cy + dy);
if x < 0 || y < 0 || x >= w as isize || y >= h as isize {
continue;
}
if ((dx * dx + dy * dy) as f64) <= rr * rr + 0.5 {
let j = y as usize * w + x as usize;
if lab[j] == 0 {
lab[j] = id;
}
}
}
}
}
(lab, heights)
}
fn flat() -> Error {
usage(
"the area has no contrast (one value once the background is removed), so there is nothing to count: check the channel, z plane and region with preview",
)
}
fn sparse_threshold(sample: &[f32]) -> Option<f32> {
let n = sample.iter().filter(|v| v.is_finite()).count();
let med = sg::quantile(sample, 0.5)?;
let hi = sg::quantile(sample, 0.999)?;
let top = sg::quantile(sample, 0.999f64.max(1.0 - 20.0 / n.max(1) as f64))?;
let dev: Vec<f32> = sample.iter().map(|v| (v - med).abs()).collect();
let sd = 1.4826 * sg::quantile(&dev, 0.5)?;
if top <= hi + 2.0 * (hi - med).max(sd) {
return None;
}
if let Some((t, _)) = sg::otsu(sample)
&& (t - med).abs() >= 3.0 * sd
{
return None;
}
let noise_top = med + 5.0 * sd;
let bright: Vec<f32> = sample.iter().copied().filter(|v| *v > noise_top).collect();
let objects = sg::quantile(&bright, 0.5)?;
Some(noise_top.max(med + 0.5 * (objects - med)))
}
fn settle_threshold(
probe: &[f32],
w: usize,
h: usize,
plan: &Plan,
sample: &[f32],
) -> Option<(f32, &'static str)> {
if plan.kind == Kind::Spots {
let med = sg::quantile(sample, 0.5)?;
let dev: Vec<f32> = sample.iter().map(|v| (v - med).abs()).collect();
let mad = sg::quantile(&dev, 0.5)?;
let sd = (1.4826 * mad).max(f32::EPSILON);
return Some((med + 5.0 * sd, "noise"));
}
if let Some(t) = sparse_threshold(sample) {
let name = if plan.method == ThresholdMethod::Local {
"local"
} else {
"sparse"
};
return Some((t, name));
}
match plan.method {
ThresholdMethod::Otsu => sg::otsu(sample).map(|t| (t.0, "otsu")),
ThresholdMethod::Local => sg::otsu(sample).map(|t| (t.0, "local")),
ThresholdMethod::Triangle => sg::triangle(sample).map(|t| (t, "triangle")),
ThresholdMethod::Auto => {
let (t, _) = sg::otsu(sample)?;
let k = 0.7f32;
let med = sg::quantile(sample, 0.5)?;
let mut cands: Vec<(f32, &'static str)> = vec![(med + k * (t - med), "otsu")];
if let Some((lo, hi)) = sg::otsu3(sample)
&& hi > t
{
cands.push((lo + k * (hi - lo), "otsu-3"));
}
if cands.len() == 1 {
return cands.pop();
}
let mut best = (f64::NEG_INFINITY, cands[0]);
for c in cands {
let mut p = plan.clone();
p.threshold = c.0;
let q = quality(probe, w, h, &p);
if q > best.0 + 0.2 {
best = (q, c);
}
}
Some(best.1)
}
}
}
fn channel_label(tg: &Target<'_>, c: u32) -> Option<String> {
tg.im
.channels
.iter()
.find(|ch| ch.index == c)
.and_then(|ch| ch.name.clone().or_else(|| ch.fluorophore.clone()))
}
pub fn default_channel(im: &ImageInfo, kind: Kind) -> u32 {
const DNA: [&str; 14] = [
"dapi", "hoechst", "draq5", "draq7", "nucblue", "sir-dna", "sirdna", "spy-dna", "spydna",
"nuclear", "nuclei", "h2b", "to-pro", "topro",
];
if kind != Kind::Nuclei {
return 0;
}
im.channels
.iter()
.find(|ch| {
[&ch.name, &ch.fluorophore].iter().any(|n| {
n.as_deref()
.is_some_and(|n| DNA.iter().any(|d| n.to_ascii_lowercase().contains(d)))
})
})
.map_or(0, |ch| ch.index)
}
#[derive(Default, Clone)]
struct Acc {
n: u64,
sx: f64,
sy: f64,
sums: Vec<f64>,
edge: bool,
hull: Vec<[f64; 2]>,
height: Option<f64>,
outline: Vec<[f64; 2]>,
}
struct TileData {
comps: Vec<Vec<f32>>,
measured: Vec<Vec<f32>>,
}
fn grid_f32(g: &Grid) -> Vec<Vec<f32>> {
g.comps
.iter()
.map(|c| c.iter().map(|v| *v as f32).collect())
.collect()
}
pub(crate) fn objects(
ds: &mut dyn crate::reader::Dataset,
tg: &Target<'_>,
req: &super::MeasureRequest,
o: &ObjectsRequest,
notes: &mut Vec<String>,
) -> Result<Objects> {
validate(o)?;
let poly = req.polygon.as_deref();
let r = match (req.region, poly) {
(Some(r), _) => {
r.check_within(
tg.level_w,
tg.level_h,
&format!("image {} level {}", tg.im.index, req.level),
)?;
r
}
(None, Some(p)) => super::bounds(p, 0.0, tg.level_w, tg.level_h)?,
(None, None) => Region::full(tg.level_w, tg.level_h),
};
let total = u64::from(r.width) * u64::from(r.height);
if total > MAX_OBJECT_PIXELS {
let coarser = (1..tg.im.pyramid_levels.max(1))
.find(|l| total >> (2 * l) <= MAX_OBJECT_PIXELS)
.map(|l| format!(" (level {})", req.level + l))
.unwrap_or_default();
return Err(Error::unsupported(
"stats",
format!("an object count over {} x {} pixels", r.width, r.height),
format!(
"Count objects in at most {MAX_OBJECT_PIXELS} pixels: pass a region (or polygon) inside it, or a coarser level{coarser}; coordinates are then that level's pixels."
),
));
}
let selected_c = !req.select.iter().all(|s| !s.contains('c'));
let c = if selected_c {
tg.channels.first().copied().unwrap_or(0)
} else {
default_channel(tg.im, o.kind)
};
let counted = c;
let all_channels: Vec<u32> = (0..tg.im.size_c.max(1)).take(8).collect();
if tg.im.size_c > 1 && !selected_c {
notes.push(format!(
"objects were found in channel {c}{}; select c=N for another",
channel_label(tg, c)
.map(|n| format!(" ({n})"))
.unwrap_or_default()
));
}
let pixel_um = tg.pixel_um;
let area_px_um2 = pixel_um.map(|p| p * tg.pixel_um_y.unwrap_or(p));
let probe = centre_tile(r);
let g = read(ds, tg, req.level, c, probe)?;
let comps = grid_f32(&g);
let rgb = comps.len() >= 3;
let (signal, stain_name) = signal_for(&comps, o, notes)?;
let measured = if rgb {
stain_name
} else {
format!(
"c={c}{}",
channel_label(tg, c)
.map(|n| format!(" {n}"))
.unwrap_or_default()
)
};
let s = signal.of(&comps);
let background = !signal.deconvolved();
let (d, from) = diameter(o, &s, probe, r, pixel_um, &signal, notes)?;
if d < 3.0 && o.kind != Kind::Spots {
notes.push(format!(
"objects are about {d:.1} px across at this level: too small to separate; read a finer level"
));
}
let to_px2 = |um2: f64| area_px_um2.map(|a| um2 / a);
let expected = std::f64::consts::PI * d * d / 4.0;
let min_area = match (o.min_area_um2, to_px2(1.0)) {
(Some(a), Some(_)) => to_px2(a).unwrap_or(1.0),
(Some(_), None) => {
return Err(usage(
"min_area_um2 needs the pixel size, which this image does not record; set diameter_px instead (objects under a fifth of its area are dropped)",
));
}
(None, _) if o.kind == Kind::Spots => 1.0,
(None, _) => expected / 5.0,
}
.max(1.0) as usize;
let max_area = match (o.max_area_um2, to_px2(1.0)) {
(Some(a), Some(_)) => to_px2(a).unwrap_or(f64::MAX),
(Some(_), None) => {
return Err(usage(
"max_area_um2 needs the pixel size, which this image does not record",
));
}
(None, _) => f64::MAX,
};
let max_area = max_area as usize;
if max_area < min_area {
return Err(usage(format!(
"max_area_um2 is below the smallest object kept ({:.3} µm², a fifth of the expected area): raise it, or set min_area_um2 too",
min_area as f64 * area_px_um2.unwrap_or(1.0)
)));
}
let tiles = tiles(r, d);
let mut plan = Plan::new(o.kind, d, background, o.method);
plan.split &= o.split.unwrap_or(true);
plan.min_area = min_area;
plan.max_area = max_area;
let method_name;
if let Some(t) = o.threshold {
plan.threshold = t as f32;
method_name = "given".to_string();
} else {
let mut sample: Vec<f32> = Vec::new();
let single = tiles.len() == 1;
for t in &tiles {
let s = if single && t.read == probe {
s.clone()
} else {
let g = read(ds, tg, req.level, c, t.read)?;
signal.of(&grid_f32(&g))
};
let (w, h) = (t.read.width as usize, t.read.height as usize);
let (ws, sw, _, k) = working(&s, w, h, &plan);
let f = scaled(&plan, k).flatten(&ws, sw, ws.len() / sw.max(1));
let stride = if single { 1 } else { 3 };
let core = t.core_in_read();
let k32 = k as u32;
for y in (core.y / k32..(core.y + core.height) / k32).step_by(stride) {
for x in (core.x / k32..(core.x + core.width) / k32).step_by(stride) {
let i = y as usize * sw + x as usize;
let (gx, gy) = (t.read.x + x * k32, t.read.y + y * k32);
if i < f.len()
&& poly.is_none_or(|p| inside(p, f64::from(gx) + 0.5, f64::from(gy) + 0.5))
{
sample.push(f[i]);
}
}
}
}
let (pw, ph) = (probe.width as usize, probe.height as usize);
let (t, m) = settle_threshold(&s, pw, ph, &plan, &sample).ok_or_else(flat)?;
if o.kind != Kind::Spots
&& let Some((_, sep)) = sg::otsu(&sample)
&& sep < 0.5
{
notes.push(format!(
"weak contrast between objects and background (Otsu separability {sep:.2}): the count may be unreliable; check a picture (preview marks=objects)"
));
}
plan.threshold = t;
method_name = m.to_string();
}
let n_meas = if rgb { 3 } else { all_channels.len() };
let mut accs: Vec<Acc> = Vec::new();
let mut bg_samples: Vec<Vec<f32>> = vec![Vec::new(); n_meas];
let mut covered = 0u64;
let mut searched = 0u64;
for t in &tiles {
let data = tile_data(ds, tg, req.level, c, &all_channels, t.read, &signal)?;
let (w, h) = (t.read.width as usize, t.read.height as usize);
let s = signal.of(&data.comps);
let (lab, heights) = label_tile(&s, w, h, &plan);
let n = lab.iter().copied().max().unwrap_or(0) as usize;
let mut a: Vec<Acc> = vec![
Acc {
sums: vec![0.0; n_meas],
..Acc::default()
};
n + 1
];
let mut first = vec![usize::MAX; n + 1];
for (i, &l) in lab.iter().enumerate() {
let (x, y) = (i % w, i / w);
let (gx, gy) = (t.read.x + x as u32, t.read.y + y as u32);
let in_core = t.core.contains_px(gx, gy);
let in_poly = poly.is_none_or(|p| inside(p, f64::from(gx) + 0.5, f64::from(gy) + 0.5));
if in_core && in_poly {
searched += 1;
}
if l == 0 {
if in_core && in_poly && (x + y * 7) % 5 == 0 {
for (k, m) in data.measured.iter().enumerate() {
bg_samples[k].push(m[i]);
}
}
continue;
}
let l = l as usize;
if first[l] == usize::MAX {
first[l] = i;
}
let e = &mut a[l];
e.n += 1;
e.sx += x as f64;
e.sy += y as f64;
for (k, m) in data.measured.iter().enumerate() {
e.sums[k] += f64::from(m[i]);
}
e.edge |= gx == r.x
|| gy == r.y
|| gx + 1 == r.x + r.width
|| gy + 1 == r.y + r.height
|| poly.is_some() && !in_poly;
let (fx, fy) = (gx as f64, gy as f64);
let at_rim = x == 0
|| y == 0
|| x + 1 == w
|| y + 1 == h
|| lab[i - 1] != lab[i]
|| lab[i + 1] != lab[i]
|| lab[i - w] != lab[i]
|| lab[i + w] != lab[i];
if at_rim {
e.hull.extend([
[fx, fy],
[fx + 1.0, fy],
[fx, fy + 1.0],
[fx + 1.0, fy + 1.0],
]);
}
}
for (l, e) in a.iter_mut().enumerate().skip(1) {
if e.n == 0 {
continue;
}
let (cx, cy) = (
f64::from(t.read.x) + e.sx / e.n as f64 + 0.5,
f64::from(t.read.y) + e.sy / e.n as f64 + 0.5,
);
if !t.core.contains_px(cx as u32, cy as u32) {
continue;
}
if poly.is_some_and(|p| !inside(p, cx, cy)) {
continue;
}
if (e.n as usize) > plan.max_area {
continue;
}
covered += e.n;
e.sx = cx;
e.sy = cy;
e.height = heights.get(l - 1).map(|h| f64::from(*h));
if o.outlines {
let block = working_factor(&plan);
let mut ol = sg::outline(&lab, w, h, l as u32, first[l], 0.4 + 0.6 * block as f64);
if block > 1 {
ol = sg::chaikin(&ol, 2);
}
for p in &mut ol {
p[0] += f64::from(t.read.x);
p[1] += f64::from(t.read.y);
}
e.outline = ol;
}
accs.push(std::mem::take(e));
}
}
let band = d.max(1.0);
accs.sort_by(|a, b| {
((a.sy / band).floor())
.total_cmp(&(b.sy / band).floor())
.then(a.sx.total_cmp(&b.sx))
});
let stained = rgb && (signal.dark || signal.deconvolved());
let channels_meta: Vec<(u32, Option<String>)> = if stained {
vec![
(0, Some("hematoxylin".into())),
(1, Some("eosin".into())),
(2, Some("dab".into())),
]
} else if rgb {
vec![
(0, Some("R".into())),
(1, Some("G".into())),
(2, Some("B".into())),
]
} else {
all_channels
.iter()
.map(|c| (*c, channel_label(tg, *c)))
.collect()
};
let mut all: Vec<Object> = accs
.into_iter()
.enumerate()
.map(|(i, mut e)| {
let solidity = (o.kind != Kind::Spots)
.then(|| {
let hull = sg::hull_area(&mut e.hull);
(hull > 0.0).then(|| (e.n as f64 / hull).min(1.0))
})
.flatten();
let area_um2 = area_px_um2.map(|a| e.n as f64 * a);
Object {
id: i as u32 + 1,
x: (e.sx * 10.0).round() / 10.0,
y: (e.sy * 10.0).round() / 10.0,
area_px: e.n,
area_um2: area_um2.map(round4),
diameter_um: area_um2.map(|a| round4(2.0 * (a / std::f64::consts::PI).sqrt())),
solidity: solidity.map(|s| (s * 100.0).round() / 100.0),
height: e.height.map(round4),
mean: e.sums.iter().map(|s| round4(s / e.n as f64)).collect(),
edge: e.edge,
outline: e.outline,
}
})
.collect();
let count = all.len();
let touching = all.iter().filter(|o| o.edge).count();
let inner: Vec<&Object> = if touching < count {
all.iter().filter(|o| !o.edge).collect()
} else {
all.iter().collect()
};
let areas: Vec<f64> = inner.iter().map(|o| o.area_px as f64).collect();
let area_px = spread(areas.clone());
let med = area_px.as_ref().map_or(0.0, |s| s.median);
let large = if o.kind == Kind::Spots {
0
} else {
all.iter().filter(|o| o.area_px as f64 > 2.0 * med).count()
};
let area_um2 = area_px_um2.and_then(|a| spread(areas.iter().map(|x| x * a).collect()));
let diameter_um_spread = area_px_um2.and_then(|a| {
spread(
areas
.iter()
.map(|x| 2.0 * (x * a / std::f64::consts::PI).sqrt())
.collect(),
)
});
let searched_um2 = area_px_um2.map(|a| searched as f64 * a);
let density = searched_um2
.filter(|a| *a > 0.0)
.map(|a| round4((count as f64 - touching as f64 / 2.0) / (a / 1e6)));
let channels = channels_meta
.into_iter()
.enumerate()
.map(|(k, (c, name))| {
let means: Vec<f64> = all.iter().map(|o| o.mean[k]).collect();
let bg = &bg_samples[k];
let background = sg::quantile(bg, 0.5).map_or(f64::NAN, f64::from);
let marker = if stained { k == 2 } else { rgb || c != counted };
let positive = marker.then(|| positive(&means, bg)).flatten();
ObjectChannel {
c,
name,
background: round4(background),
mean: spread(means).map(round_spread),
positive,
}
})
.collect();
let shapes: Vec<(f64, f64)> = all
.iter()
.map(|o| (o.area_px as f64, o.solidity.unwrap_or(1.0)))
.collect();
let (fit, doubt) = assess(o.kind, &shapes, d, searched as f64);
let fit = fit.map(round4);
let confidence = if doubt.is_some() {
CountConfidence::Low
} else {
CountConfidence::High
};
if let Some(doubt) = doubt {
notes.push(doubt.note(count, d, o.kind));
} else if o.kind != Kind::Spots && count >= 10 {
let mut sol: Vec<f64> = all.iter().filter_map(|o| o.solidity).collect();
let irregular = median(&mut sol).is_some_and(|s| s < 0.8);
if large * 4 > count || irregular {
notes.push(format!(
"{} of {count} objects are over twice the median size{}: touching objects were not all separated, or the stain marks more than {}; check a picture (preview marks=objects) and set diameter_um or another channel or stain",
large,
if irregular { " and most are irregular" } else { "" },
kind_name(o.kind)
));
}
}
if count == 0 {
notes.push(format!(
"no {} found above the threshold; check a picture, or set diameter_um, threshold or polarity",
kind_name(o.kind)
));
}
let max_list = o.max_objects.unwrap_or(DEFAULT_MAX_OBJECTS);
let truncated = count > max_list;
all.truncate(max_list);
let d_um = pixel_um.map(|p| round4(d * p));
Ok(Objects {
kind: o.kind,
measured,
region: r,
searched_um2: searched_um2.map(round4),
expected_diameter_px: round4(d),
expected_diameter_um: d_um,
expected_diameter_from: from.into(),
polarity: if signal.dark || signal.deconvolved() {
Polarity::Dark
} else {
Polarity::Bright
},
threshold: round4(f64::from(plan.threshold)),
threshold_method: method_name,
split: plan.split,
count,
confidence,
fit,
touching_edge: touching,
density_per_mm2: density,
area_fraction: round4(covered as f64 / searched.max(1) as f64),
large,
area_px: area_px.map(round_spread),
area_um2: area_um2.map(round_spread),
diameter_um: diameter_um_spread.map(round_spread),
channels,
objects: all,
truncated,
})
}
fn round4(v: f64) -> f64 {
if !v.is_finite() || v == 0.0 {
return v;
}
let d = 4 - 1 - v.abs().log10().floor() as i32;
if d <= 0 {
return v.round();
}
let m = 10f64.powi(d.min(12));
(v * m).round() / m
}
fn round_spread(s: Spread) -> Spread {
Spread {
min: round4(s.min),
median: round4(s.median),
mean: round4(s.mean),
max: round4(s.max),
}
}
fn positive(means: &[f64], bg: &[f32]) -> Option<Positive> {
if means.is_empty() {
return None;
}
let m32: Vec<f32> = means.iter().map(|v| *v as f32).collect();
let bg_med = sg::quantile(bg, 0.5)?;
let dev: Vec<f32> = bg.iter().map(|v| (v - bg_med).abs()).collect();
let bg_sd = 1.4826 * sg::quantile(&dev, 0.5)?;
let top = sg::quantile(&m32, 0.95)?;
let floor = f64::from(bg_med + (5.0 * bg_sd).max(0.1 * (top - bg_med)).max(f32::EPSILON));
let (threshold, method) = match sg::otsu(&m32) {
Some((t, sep)) if sep >= 0.75 && means.len() >= 8 && f64::from(t) > floor => {
(f64::from(t), "otsu")
}
_ => (floor, "background"),
};
let count = means.iter().filter(|m| **m > threshold).count();
Some(Positive {
threshold: round4(threshold),
method: if count == 0 {
"none".into()
} else {
method.into()
},
count,
fraction: round4(count as f64 / means.len() as f64),
})
}
fn tile_data(
ds: &mut dyn crate::reader::Dataset,
tg: &Target<'_>,
level: u32,
c: u32,
all: &[u32],
r: Region,
signal: &Signal,
) -> Result<TileData> {
let g = read(ds, tg, level, c, r)?;
let comps = grid_f32(&g);
let measured = if comps.len() >= 3 {
if signal.dark
|| matches!(
signal.stain,
Some(Stain::Hematoxylin | Stain::Eosin | Stain::Dab)
)
{
let i0 = if signal.i0.iter().all(|v| *v <= 0.0) {
[255.0; 3]
} else {
signal.i0
};
stains(&comps, i0).to_vec()
} else {
comps.iter().take(3).cloned().collect()
}
} else {
let mut m = Vec::with_capacity(all.len());
for &k in all {
if k == c {
m.push(comps[0].clone());
} else {
m.push(grid_f32(&read(ds, tg, level, k, r)?).swap_remove(0));
}
}
m
};
Ok(TileData { comps, measured })
}
const MAX_SPOT_PX: f64 = 64.0;
pub(crate) fn validate(o: &ObjectsRequest) -> Result<()> {
let positive = [
("diameter_um", o.diameter_um),
("diameter_px", o.diameter_px),
("min_area_um2", o.min_area_um2),
("max_area_um2", o.max_area_um2),
];
for (name, v) in positive {
if let Some(v) = v
&& !(v.is_finite() && v > 0.0)
{
return Err(usage(format!(
"{name} must be a positive number (got {v}); leave it out for the default"
)));
}
}
if let Some(t) = o.threshold
&& !(t.is_finite() && t.abs() < f64::from(f32::MAX))
{
return Err(usage(format!(
"threshold must be a finite number (got {t}); leave it out to choose one from the image"
)));
}
if let (Some(lo), Some(hi)) = (o.min_area_um2, o.max_area_um2)
&& hi < lo
{
return Err(usage(format!(
"max_area_um2 ({hi}) is below min_area_um2 ({lo}): no object could be kept"
)));
}
Ok(())
}
fn largest_diameter(kind: Kind, w: usize, h: usize) -> f64 {
let side = w.min(h).max(1) as f64;
if kind == Kind::Spots {
side.min(MAX_SPOT_PX)
} else {
side
}
}
fn diameter(
o: &ObjectsRequest,
s: &[f32],
probe: Region,
area: Region,
pixel_um: Option<f64>,
signal: &Signal,
notes: &mut Vec<String>,
) -> Result<(f64, &'static str)> {
let cap = largest_diameter(o.kind, area.width as usize, area.height as usize);
let given = |d: f64, what: &str| -> Result<(f64, &'static str)> {
if d <= cap {
return Ok((d, "given"));
}
Err(usage(if o.kind == Kind::Spots && cap == MAX_SPOT_PX {
format!(
"{what} makes spots {d:.0} px across; spots are at most {MAX_SPOT_PX} px: for larger objects use kind=nuclei or cells, or read a coarser level"
)
} else {
format!(
"{what} makes objects {d:.0} px across, more than the area searched ({} x {} px) holds: give a smaller diameter or a larger region",
area.width, area.height
)
}))
};
if let Some(d) = o.diameter_um {
let Some(p) = pixel_um else {
return Err(usage(
"diameter_um needs the pixel size, which this image does not record; give diameter_px instead",
));
};
return given(d / p, "diameter_um");
}
if let Some(d) = o.diameter_px {
return given(d, "diameter_px");
}
let derived = |d: f64, notes: &mut Vec<String>| -> f64 {
if d <= cap {
return d;
}
notes.push(format!(
"the recorded pixel size ({} µm) makes typical {} {d:.0} px across, more than the area searched allows; used {cap:.0} px (set diameter_px if the pixel size is wrong)",
pixel_um.map_or_else(String::new, |p| format!("{p:.3e}")),
kind_name(o.kind)
));
cap
};
let (typical, lo, hi) = match o.kind {
Kind::Nuclei => (10.0, 3.0, 30.0),
Kind::Cells => (20.0, 6.0, 150.0),
Kind::Spots => (0.5, 0.1, 3.0),
};
if o.kind == Kind::Spots {
return Ok(match pixel_um {
Some(p) => (derived((typical / p).max(2.0), notes), "default"),
None => (3.0f64.min(cap), "default"),
});
}
let (w, h) = (probe.width as usize, probe.height as usize);
let (range, typical_px) = match pixel_um {
Some(p) => ((lo / p, hi / p), Some(typical / p)),
None => ((0.0, f64::INFINITY), None),
};
match (
measure_diameter(s, w, h, o.kind, signal, range, typical_px),
pixel_um,
) {
(Some((d, false)), _) => Ok((d, "measured")),
(Some((d, true)), _) => Ok((derived(d, notes), "default")),
(None, Some(p)) => {
notes.push(format!(
"could not measure the {} in the image; used the typical {typical} µm (set diameter_um to override)",
kind_name(o.kind)
));
Ok((derived(typical / p, notes), "default"))
}
(None, None) => {
notes.push("could not measure the object size and the image has no pixel size; used 20 px (set diameter_px)".into());
Ok((20f64.min(cap), "default"))
}
}
}
fn kind_name(k: Kind) -> &'static str {
match k {
Kind::Nuclei => "nuclei",
Kind::Cells => "cells",
Kind::Spots => "spots",
}
}
fn centre_tile(r: Region) -> Region {
if u64::from(r.width) * u64::from(r.height) <= ONE_PIECE {
return r;
}
let w = r.width.min(TILE);
let h = r.height.min(TILE);
Region::new(r.x + (r.width - w) / 2, r.y + (r.height - h) / 2, w, h)
}
#[derive(Debug, Clone, Copy)]
struct Tile {
core: Region,
read: Region,
}
impl Tile {
fn core_in_read(&self) -> Region {
Region::new(
self.core.x - self.read.x,
self.core.y - self.read.y,
self.core.width,
self.core.height,
)
}
}
trait ContainsPx {
fn contains_px(&self, x: u32, y: u32) -> bool;
}
impl ContainsPx for Region {
fn contains_px(&self, x: u32, y: u32) -> bool {
x >= self.x && y >= self.y && x - self.x < self.width && y - self.y < self.height
}
}
fn tiles(r: Region, d: f64) -> Vec<Tile> {
if u64::from(r.width) * u64::from(r.height) <= ONE_PIECE {
return vec![Tile { core: r, read: r }];
}
let m = (3.0 * d).ceil().clamp(16.0, 1024.0) as u32;
let mut out = Vec::new();
let mut y = r.y;
while y < r.y + r.height {
let th = TILE.min(r.y + r.height - y);
let mut x = r.x;
while x < r.x + r.width {
let tw = TILE.min(r.x + r.width - x);
let core = Region::new(x, y, tw, th);
let rx0 = x.saturating_sub(m).max(r.x);
let ry0 = y.saturating_sub(m).max(r.y);
let rx1 = (x + tw + m).min(r.x + r.width);
let ry1 = (y + th + m).min(r.y + r.height);
out.push(Tile {
core,
read: Region::new(rx0, ry0, rx1 - rx0, ry1 - ry0),
});
x += tw;
}
y += th;
}
out
}
#[doc(hidden)]
pub fn label_plane(
comps: &[Vec<f32>],
w: usize,
h: usize,
pixel_um: Option<f64>,
o: &ObjectsRequest,
) -> Result<(Vec<u32>, f64, f64, CountConfidence)> {
if comps.is_empty() || w == 0 || h == 0 || comps.iter().any(|c| c.len() != w * h) {
return Err(usage("label_plane: each component needs w × h values"));
}
validate(o)?;
let mut notes = Vec::new();
let (signal, _) = signal_for(comps, o, &mut notes)?;
let s = signal.of(comps);
let background = !signal.deconvolved();
let full = Region::full(w as u32, h as u32);
let (d, _) = diameter(o, &s, full, full, pixel_um, &signal, &mut notes)?;
let mut plan = Plan::new(o.kind, d, background, o.method);
plan.split &= o.split.unwrap_or(true);
plan.threshold = if let Some(t) = o.threshold {
t as f32
} else {
let (ws, sw, _, k) = working(&s, w, h, &plan);
let f = scaled(&plan, k).flatten(&ws, sw, ws.len() / sw.max(1));
settle_threshold(&s, w, h, &plan, &f).ok_or_else(flat)?.0
};
let lab = label_tile(&s, w, h, &plan).0;
let (f, doubt) = assess(o.kind, &shapes(&lab, w, h), d, (w * h) as f64);
let confidence = if doubt.is_some() {
CountConfidence::Low
} else {
CountConfidence::High
};
Ok((lab, d, f.unwrap_or(0.0), confidence))
}
#[cfg(test)]
pub(crate) mod tests {
use super::*;
use crate::measure::{MeasureRequest, measure};
use crate::model::{ChannelInfo, CheckReport, FileInfo, ImageInfo, LsEntry, PhysicalSize};
use crate::pixel::Plane;
use crate::reader::{Dataset, PlaneIndex};
use crate::{PixelType, ProvenanceMap};
struct Lcg(u64);
impl Lcg {
fn next(&mut self) -> f32 {
self.0 = self
.0
.wrapping_mul(6_364_136_223_846_793_005)
.wrapping_add(1_442_695_040_888_963_407);
((self.0 >> 40) as f32) / (1u64 << 24) as f32 - 0.5
}
}
fn discs(
w: usize,
h: usize,
d: &[(f32, f32, f32, f32)],
background: f32,
noise: f32,
) -> Vec<f32> {
let mut rng = Lcg(7);
(0..w * h)
.map(|i| {
let (x, y) = ((i % w) as f32 + 0.5, (i / w) as f32 + 0.5);
let mut v = background;
for &(cx, cy, r, val) in d {
if (x - cx).hypot(y - cy) <= r {
v = val;
}
}
v + noise * rng.next()
})
.collect()
}
fn count(lab: &[u32]) -> usize {
let mut v: Vec<u32> = lab.iter().copied().filter(|l| *l > 0).collect();
v.sort_unstable();
v.dedup();
v.len()
}
fn nuclei() -> Vec<(f32, f32, f32, f32)> {
let mut d = Vec::new();
for (i, cx) in [30.0, 80.0, 130.0].into_iter().enumerate() {
d.push((cx, 30.0, 10.0, 200.0 + 20.0 * i as f32));
d.push((cx, 80.0, 10.0, 150.0));
}
d.push((40.0, 140.0, 10.0, 180.0));
d.push((59.0, 140.0, 10.0, 190.0));
d.push((78.0, 141.0, 10.0, 170.0));
d
}
#[test]
fn counts_and_splits_bright_nuclei() {
let (w, h) = (180, 180);
let plane = discs(w, h, &nuclei(), 20.0, 10.0);
for o in [
ObjectsRequest::default(),
ObjectsRequest {
diameter_px: Some(20.0),
..ObjectsRequest::default()
},
] {
let (lab, d, _, _) = label_plane(std::slice::from_ref(&plane), w, h, None, &o).unwrap();
assert!((12.0..30.0).contains(&d), "{d}");
assert_eq!(count(&lab), 9, "diameter {d}");
}
let o = ObjectsRequest {
diameter_px: Some(20.0),
split: Some(false),
..ObjectsRequest::default()
};
assert_eq!(count(&label_plane(&[plane], w, h, None, &o).unwrap().0), 7);
}
#[test]
fn finds_dark_nuclei_on_a_bright_background() {
let (w, h) = (180, 180);
let d: Vec<_> = nuclei()
.into_iter()
.map(|(x, y, r, v)| (x, y, r, 250.0 - v))
.collect();
let plane = discs(w, h, &d, 230.0, 8.0);
let (lab, _, _, _) = label_plane(&[plane], w, h, None, &ObjectsRequest::default()).unwrap();
assert_eq!(count(&lab), 9);
}
#[test]
fn finds_hematoxylin_nuclei_in_stained_tissue() {
let (w, h) = (180, 180);
let mut r = vec![0f32; w * h];
let mut g = vec![0f32; w * h];
let mut b = vec![0f32; w * h];
let inside = discs(
w,
h,
&nuclei()
.iter()
.map(|n| (n.0, n.1, n.2, 1.0))
.collect::<Vec<_>>(),
0.0,
0.0,
);
let mut rng = Lcg(3);
for i in 0..w * h {
let (cr, cg, cb) = if i / w < 8 {
(245.0, 245.0, 245.0)
} else if inside[i] > 0.5 {
(90.0, 60.0, 150.0)
} else {
(235.0, 150.0, 200.0)
};
r[i] = cr + 6.0 * rng.next();
g[i] = cg + 6.0 * rng.next();
b[i] = cb + 6.0 * rng.next();
}
let o = ObjectsRequest::default();
let mut notes = Vec::new();
let (sig, name) = signal_for(&[r.clone(), g.clone(), b.clone()], &o, &mut notes).unwrap();
assert_eq!(name, "hematoxylin");
assert!(notes.is_empty(), "{notes:?}");
assert!(sig.deconvolved());
let (lab, _, _, _) = label_plane(&[r, g, b], w, h, None, &o).unwrap();
assert_eq!(count(&lab), 9);
}
#[test]
fn finds_spots_above_the_noise() {
let (w, h) = (120, 120);
let mut d = Vec::new();
for i in 0..5 {
for j in 0..4 {
d.push((15.0 + 22.0 * i as f32, 15.0 + 28.0 * j as f32, 1.5, 300.0));
}
}
let plane = discs(w, h, &d, 100.0, 20.0);
let o = ObjectsRequest {
kind: Kind::Spots,
..ObjectsRequest::default()
};
let (lab, _, _, _) = label_plane(&[plane], w, h, None, &o).unwrap();
assert_eq!(count(&lab), 20);
}
fn textured(w: usize, h: usize, nuclei: &[(f32, f32, f32)], gap: Option<f32>) -> Vec<f32> {
let mut rng = Lcg(11);
let spots: Vec<(f32, f32)> = (0..40)
.map(|_| (1.6 * rng.next(), 1.6 * rng.next()))
.filter(|(x, y)| x.hypot(*y) < 0.8)
.collect();
(0..w * h)
.map(|i| {
let (x, y) = ((i % w) as f32 + 0.5, (i / w) as f32 + 0.5);
let mut v = 20.0;
for (k, &(cx, cy, r)) in nuclei.iter().enumerate() {
if (x - cx).hypot(y - cy) > r {
continue;
}
v = 120.0;
if spots
.iter()
.any(|(sx, sy)| (x - cx - sx * r).hypot(y - cy - sy * r) < 0.1 * r)
{
v = 240.0;
}
let a = 0.7 * k as f32;
if ((x - cx) * a.sin() - (y - cy) * a.cos()).abs() < 0.05 * r {
v = 60.0;
}
}
if gap.is_some_and(|g| (x - g).abs() < 1.5) && v > 20.0 {
v = 25.0;
}
v + 8.0 * rng.next()
})
.collect()
}
#[test]
fn texture_does_not_cut_nuclei() {
let (w, h) = (300, 200);
let mut n = Vec::new();
for cx in [50.0, 150.0, 250.0] {
for cy in [50.0, 150.0] {
n.push((cx, cy, 30.0));
}
}
let plane = textured(w, h, &n, None);
for o in [
ObjectsRequest::default(),
ObjectsRequest {
diameter_px: Some(60.0),
..ObjectsRequest::default()
},
] {
let (lab, d, _, _) = label_plane(std::slice::from_ref(&plane), w, h, None, &o).unwrap();
assert_eq!(count(&lab), 6, "diameter {d}");
}
}
#[test]
fn touching_textured_nuclei_are_cut_apart() {
let (w, h) = (300, 200);
let n = [
(50.0, 60.0, 30.0),
(108.0, 60.0, 30.0),
(180.0, 140.0, 30.0),
(232.0, 140.0, 30.0),
];
let plane = textured(w, h, &n, Some(79.0));
let o = ObjectsRequest {
diameter_px: Some(60.0),
..ObjectsRequest::default()
};
let (lab, _, _, _) = label_plane(&[plane], w, h, None, &o).unwrap();
assert_eq!(count(&lab), 4);
let at = |x: usize, y: usize| lab[y * w + x];
for (a, b) in [((50, 60), (108, 60)), ((180, 140), (232, 140))] {
assert_ne!(at(a.0, a.1), 0);
assert_ne!(at(a.0, a.1), at(b.0, b.1));
}
}
#[test]
fn irregular_objects_have_low_fit() {
let (w, h) = (160, 160);
let plane: Vec<f32> = (0..w * h)
.map(|i| if (i % w) % 16 < 2 { 200.0 } else { 10.0 })
.collect();
let o = ObjectsRequest {
diameter_px: Some(12.0),
..ObjectsRequest::default()
};
let (_, _, f, c) = label_plane(&[plane], w, h, None, &o).unwrap();
assert!(f < LOW_FIT, "{f}");
assert_eq!(c, CountConfidence::Low);
}
fn sparse(w: usize, n: usize, r: f32, height: f32) -> Vec<f32> {
let d: Vec<_> = (0..n)
.map(|i| (100.0 + 180.0 * i as f32, w as f32 / 2.0, r, 100.0 + height))
.collect();
discs(w, w, &d, 100.0, 35.0)
}
#[test]
fn sparse_objects_on_noise_are_counted() {
let w = 1024;
let plane = sparse(w, 5, 6.0, 1000.0);
for method in [
ThresholdMethod::Auto,
ThresholdMethod::Otsu,
ThresholdMethod::Triangle,
] {
let o = ObjectsRequest {
diameter_px: Some(12.0),
method,
..ObjectsRequest::default()
};
let (lab, _, _, c) = label_plane(std::slice::from_ref(&plane), w, w, None, &o).unwrap();
assert_eq!(count(&lab), 5, "{method:?}");
assert_eq!(c, CountConfidence::High, "{method:?}");
}
let (lab, d, _, _) = label_plane(
std::slice::from_ref(&plane),
w,
w,
None,
&ObjectsRequest::default(),
)
.unwrap();
assert_eq!(count(&lab), 5, "measured diameter {d}");
let one = discs(512, 512, &[(256.0, 256.0, 5.0, 1000.0)], 0.0, 0.0);
let o = ObjectsRequest {
diameter_px: Some(10.0),
..ObjectsRequest::default()
};
let (lab, _, _, _) = label_plane(&[one], 512, 512, None, &o).unwrap();
assert_eq!(count(&lab), 1);
let noise = discs(512, 512, &[], 100.0, 35.0);
let (lab, _, _, c) = label_plane(&[noise], 512, 512, None, &o).unwrap();
assert!(
count(&lab) <= 1 || c == CountConfidence::Low,
"{}",
count(&lab)
);
}
#[test]
fn a_threshold_inside_the_noise_has_low_confidence() {
let w = 512;
let plane = sparse(w, 2, 6.0, 1000.0);
let o = ObjectsRequest {
diameter_px: Some(12.0),
threshold: Some(0.0),
..ObjectsRequest::default()
};
let (lab, _, _, c) = label_plane(&[plane], w, w, None, &o).unwrap();
assert!(count(&lab) > 10, "{}", count(&lab));
assert_eq!(c, CountConfidence::Low);
let specks = vec![(35.0, 0.9); 20];
assert_eq!(
assess(Kind::Nuclei, &specks, 12.0, 1e6).1,
Some(Doubt::Specks)
);
let crowded = vec![(113.0, 0.95); 100];
assert_eq!(
assess(Kind::Nuclei, &crowded, 12.0, 5000.0).1,
Some(Doubt::Crowded)
);
assert_eq!(assess(Kind::Nuclei, &crowded, 12.0, 1e6).1, None);
}
#[test]
fn object_options_must_be_finite_and_positive() {
let plane = discs(64, 64, &[(32.0, 32.0, 6.0, 100.0)], 0.0, 1.0);
let bad = [
ObjectsRequest {
diameter_px: Some(f64::NAN),
..ObjectsRequest::default()
},
ObjectsRequest {
diameter_px: Some(-3.0),
..ObjectsRequest::default()
},
ObjectsRequest {
max_area_um2: Some(f64::NAN),
..ObjectsRequest::default()
},
ObjectsRequest {
max_area_um2: Some(0.0),
..ObjectsRequest::default()
},
ObjectsRequest {
min_area_um2: Some(f64::INFINITY),
..ObjectsRequest::default()
},
ObjectsRequest {
min_area_um2: Some(50.0),
max_area_um2: Some(10.0),
..ObjectsRequest::default()
},
ObjectsRequest {
threshold: Some(f64::NAN),
..ObjectsRequest::default()
},
ObjectsRequest {
threshold: Some(1e300),
..ObjectsRequest::default()
},
ObjectsRequest {
diameter_um: Some(f64::NEG_INFINITY),
..ObjectsRequest::default()
},
];
for o in bad {
let e = label_plane(std::slice::from_ref(&plane), 64, 64, Some(0.5), &o).unwrap_err();
assert!(matches!(e, Error::Usage(_)), "{o:?} {e}");
}
let mut ds = TwoChannels::new();
let info = ds.info().unwrap();
let req = MeasureRequest {
objects: Some(ObjectsRequest {
max_area_um2: Some(0.01),
..ObjectsRequest::default()
}),
..MeasureRequest::default()
};
assert!(matches!(
measure(&mut ds, &info, &req).unwrap_err(),
Error::Usage(_)
));
}
#[test]
fn huge_diameters_are_refused_or_capped_quickly() {
let plane = discs(64, 64, &[(32.0, 32.0, 2.0, 1000.0)], 0.0, 1.0);
for kind in [Kind::Spots, Kind::Nuclei, Kind::Cells] {
for d in [65.0, 500.0, 2000.0, 1e13, f64::MAX] {
let o = ObjectsRequest {
kind,
diameter_px: Some(d),
..ObjectsRequest::default()
};
let t = std::time::Instant::now();
let e = label_plane(std::slice::from_ref(&plane), 64, 64, None, &o).unwrap_err();
assert!(matches!(e, Error::Usage(_)), "{kind:?} {d} {e}");
assert!(t.elapsed().as_secs() < 5);
}
}
let big = discs(300, 300, &[(150.0, 150.0, 2.0, 1000.0)], 0.0, 1.0);
let o = ObjectsRequest {
kind: Kind::Spots,
diameter_px: Some(100.0),
..ObjectsRequest::default()
};
assert!(label_plane(std::slice::from_ref(&big), 300, 300, None, &o).is_err());
for (kind, p) in [
(Kind::Spots, 1e-9),
(Kind::Nuclei, 1e-9),
(Kind::Spots, 1e9),
(Kind::Nuclei, 1e9),
] {
let o = ObjectsRequest {
kind,
..ObjectsRequest::default()
};
let t = std::time::Instant::now();
let r = label_plane(std::slice::from_ref(&big), 300, 300, Some(p), &o);
if let Ok((_, d, _, _)) = r {
assert!(d <= 300.0, "{kind:?} {p} {d}");
}
assert!(t.elapsed().as_secs() < 20, "{kind:?} {p}");
}
let p = Plan::new(Kind::Nuclei, 1e13, true, ThresholdMethod::Auto);
let q = scaled(&p, working_factor(&p));
assert!(q.min_area >= 1);
}
#[test]
fn odd_planes_fail_cleanly() {
let kinds = [
ObjectsRequest::default(),
ObjectsRequest {
kind: Kind::Spots,
..ObjectsRequest::default()
},
ObjectsRequest {
diameter_px: Some(2.0),
..ObjectsRequest::default()
},
ObjectsRequest {
method: ThresholdMethod::Local,
..ObjectsRequest::default()
},
];
for (w, h) in [(1, 1000), (1000, 1), (1, 1), (2, 2), (3, 300), (5, 5)] {
let ramp: Vec<f32> = (0..w * h).map(|i| (i % 7) as f32).collect();
let nan = vec![f32::NAN; w * h];
let inf = vec![f32::INFINITY; w * h];
let mixed: Vec<f32> = ramp
.iter()
.enumerate()
.map(|(i, v)| match i % 3 {
0 => f32::NAN,
1 => f32::NEG_INFINITY,
_ => *v,
})
.collect();
for plane in [ramp, nan, inf, mixed] {
for o in &kinds {
let _ = label_plane(std::slice::from_ref(&plane), w, h, None, o);
}
}
}
}
#[test]
fn flat_and_tiny_planes_fail_cleanly() {
let o = ObjectsRequest::default();
assert!(label_plane(&[vec![5.0; 100]], 10, 10, None, &o).is_err());
assert!(label_plane(&[vec![1.0]], 1, 1, None, &o).is_err());
assert!(label_plane(&[vec![1.0; 3]], 2, 2, None, &o).is_err());
assert!(label_plane(&[], 0, 0, None, &o).is_err());
let mut nan = discs(40, 40, &[(20.0, 20.0, 6.0, 100.0)], 0.0, 1.0);
nan[5] = f32::NAN;
let _ = label_plane(&[nan], 40, 40, None, &o);
}
#[test]
fn requests_reject_unknown_options() {
let e = serde_json::from_str::<ObjectsRequest>(r#"{"min_area_px": 4}"#);
assert!(e.is_err());
let o: ObjectsRequest =
serde_json::from_str(r#"{"kind": "spots", "diameter_um": 0.4}"#).unwrap();
assert_eq!(o.kind, Kind::Spots);
}
pub(crate) struct TwoChannels {
pub w: usize,
pub h: usize,
pub planes: [Vec<f32>; 2],
}
impl TwoChannels {
pub(crate) fn new() -> TwoChannels {
let (w, h) = (180, 180);
let n = nuclei();
let dapi = discs(w, h, &n, 100.0, 20.0);
let marked: Vec<_> = n
.iter()
.enumerate()
.map(|(i, d)| (d.0, d.1, d.2, if i % 2 == 0 { 3000.0 } else { 200.0 }))
.collect();
let marker = discs(w, h, &marked, 200.0, 30.0);
TwoChannels {
w,
h,
planes: [marker, dapi],
}
}
fn file_info(&self) -> FileInfo {
let mut im = ImageInfo::new(0, self.w as u32, self.h as u32, PixelType::Uint16);
im.size_c = 2;
im.physical_size = PhysicalSize::micrometres(Some(0.5), Some(0.5), None);
im.channels = vec![
ChannelInfo {
index: 0,
name: Some("Ki67".into()),
..ChannelInfo::default()
},
ChannelInfo {
index: 1,
name: Some("DAPI".into()),
..ChannelInfo::default()
},
];
FileInfo {
path: "two.tif".into(),
size_bytes: 1,
format: crate::model::FormatDescriptor {
id: "tiff".into(),
name: "TIFF".into(),
vendor: String::new(),
extensions: vec![],
family: "microscopy".into(),
can_read: true,
can_write: false,
confidence: crate::provenance::Confidence::High,
known_gaps: vec![],
},
format_version: None,
images: vec![im.finish()],
tables: vec![],
spectra: vec![],
traces: vec![],
plane_count: 2,
notes: vec![],
}
}
}
impl Dataset for TwoChannels {
fn info(&self) -> Result<FileInfo> {
Ok(self.file_info())
}
fn vendor_metadata(&self) -> Result<serde_json::Value> {
Ok(serde_json::Value::Null)
}
fn provenance(&self) -> ProvenanceMap {
ProvenanceMap::new()
}
fn entries(&self) -> Result<Vec<LsEntry>> {
Ok(vec![])
}
fn read_plane(&mut self, _: u32, i: PlaneIndex) -> Result<Plane> {
let data = self.planes[i.c as usize]
.iter()
.flat_map(|v| (v.clamp(0.0, 65535.0) as u16).to_le_bytes())
.collect();
Ok(Plane {
width: self.w as u32,
height: self.h as u32,
pixel_type: PixelType::Uint16,
samples_per_pixel: 1,
data,
})
}
fn check(&mut self) -> Result<CheckReport> {
Ok(CheckReport::new("tiff", "two.tif"))
}
}
#[test]
fn one_call_counts_nuclei_and_marker_positive_cells() {
let mut ds = TwoChannels::new();
let info = ds.info().unwrap();
let req = MeasureRequest {
objects: Some(ObjectsRequest::default()),
..MeasureRequest::default()
};
let out = measure(&mut ds, &info, &req).unwrap();
let o = out.objects.unwrap();
assert_eq!(o.measured, "c=1 DAPI");
assert_eq!(o.count, 9);
assert_eq!(o.confidence, CountConfidence::High, "fit {:?}", o.fit);
assert_eq!(o.objects.len(), 9);
assert_eq!(o.channels.len(), 2);
let ki67 = o.channels[0].positive.as_ref().unwrap();
assert_eq!(ki67.count, 5, "{ki67:?}");
assert!(
o.channels[1].positive.is_none(),
"the counted channel has no positive call"
);
let d = o.diameter_um.unwrap().median;
assert!((8.0..12.0).contains(&d), "{d}");
assert!(o.density_per_mm2.unwrap() > 0.0);
assert!(o.objects.windows(2).all(|p| p[0].y <= p[1].y + 12.0));
let req = MeasureRequest {
polygon: Some(vec![[0.0, 0.0], [180.0, 0.0], [180.0, 55.0], [0.0, 55.0]]),
objects: Some(ObjectsRequest {
diameter_um: Some(10.0),
..ObjectsRequest::default()
}),
..MeasureRequest::default()
};
let o = measure(&mut ds, &info, &req).unwrap().objects.unwrap();
assert_eq!(o.count, 3);
assert_eq!(o.expected_diameter_from, "given");
}
#[test]
fn tiles_count_each_object_once() {
let r = Region::new(0, 0, 5000, 3000);
let t = tiles(r, 20.0);
assert_eq!(t.len(), 6);
let covered: u64 = t
.iter()
.map(|t| u64::from(t.core.width) * u64::from(t.core.height))
.sum();
assert_eq!(covered, 5000 * 3000);
assert!(
t.iter()
.all(|t| t.read.x <= t.core.x && t.read.width >= t.core.width)
);
assert_eq!(tiles(Region::new(0, 0, 2000, 2000), 20.0).len(), 1);
}
}