openreadout-core 0.4.0

Core traits, normalized metadata model, provenance tags, JSON output and exit codes for OpenReadout
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
//! Measurements on one image plane: an intensity profile along a line, statistics inside a
//! polygon, and objects found by a threshold (connected components with their sizes), so that
//! "how bright is the membrane", "what is in this region" and "how many nuclei" each take one
//! call.
//!
//! Coordinates are pixels of the resolution level read (level 0 = full resolution), the same
//! as `stats` regions and the rulers `preview` draws at level 0. Only the bounding rectangle
//! of what is measured is read, so a line across a whole-slide image costs the tiles it
//! crosses.
//!
//! Definitions (`book/src/reference/commands/stats.md`, "Measurements"):
//!
//! - **profile**: values sampled every pixel along the line by bilinear interpolation, each
//!   the mean over `width` pixels across the line (at most [`MAX_PROFILE_SAMPLES`] samples;
//!   longer lines are sampled more coarsely). The statistics, peak and FWHM are of those
//!   samples; `values` returns at most [`PROFILE_VALUES`] of them, evenly spaced from the start
//!   to the end, each the mean of the samples within half a step of it.
//! - **polygon**: a pixel is inside when its centre is (even-odd rule).
//! - **objects**: see `objects.rs` and `segment.rs`.

// x, y, w, h, c, k are the natural names in image arithmetic.
#![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,
};

/// Most samples taken along a profile line.
pub const MAX_PROFILE_SAMPLES: usize = 20_000;
/// Most profile values returned per channel.
pub const PROFILE_VALUES: usize = 200;
/// Most pixels a measurement reads (a 2896 × 2896 region).
pub const MAX_MEASURE_PIXELS: u64 = 1 << 23;

/// What to measure. Coordinates are pixels of `level`.
#[derive(Debug, Clone, Default)]
pub struct MeasureRequest {
    /// Image (default: the first).
    pub image: Option<u32>,
    /// Plane selection: channels (default all for profile and polygon, the first for objects),
    /// one z (default the middle) and one t (default 0).
    pub select: Vec<String>,
    /// Pyramid level.
    pub level: u32,
    /// The rectangle objects are counted in (default the whole plane, or the polygon).
    pub region: Option<Region>,
    /// Profile line `[x0, y0, x1, y1]`.
    pub line: Option<[f64; 4]>,
    /// Profile width in pixels (default 1).
    pub line_width: Option<u32>,
    /// Polygon vertices `[[x, y], ...]`.
    pub polygon: Option<Vec<[f64; 2]>>,
    /// Count objects.
    pub objects: Option<ObjectsRequest>,
    /// Find the lanes and bands of a gel or blot.
    pub lanes: Option<LanesRequest>,
}

impl MeasureRequest {
    /// True when the request asks for any measurement.
    pub fn any(&self) -> bool {
        self.line.is_some()
            || self.polygon.is_some()
            || self.objects.is_some()
            || self.lanes.is_some()
    }
}

/// One channel of a profile.
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
pub struct ProfileChannel {
    /// Channel index (`c`), or the RGB component for RGB images.
    pub c: u32,
    /// Channel or component name.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// Smallest value along the line.
    pub min: f64,
    /// Largest value along the line.
    pub max: f64,
    /// Mean value along the line.
    pub mean: f64,
    /// Distance of the largest value from the start, in pixels.
    pub max_at_px: f64,
    /// The same in µm, when the pixel size is recorded.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_at_um: Option<f64>,
    /// Full width at half maximum of the highest peak above the line's minimum, in pixels.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fwhm_px: Option<f64>,
    /// The same in µm.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fwhm_um: Option<f64>,
    /// The profile, one value every `step_px` from the start (at most 200; each the mean of the
    /// samples it covers).
    pub values: Vec<f64>,
}

/// An intensity profile along a line.
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
pub struct Profile {
    /// `[x0, y0, x1, y1]` in pixels of the level read.
    pub line: [f64; 4],
    /// Pixels averaged across the line.
    pub width_px: u32,
    /// Line length in pixels.
    pub length_px: f64,
    /// Line length in µm, when the pixel size is recorded.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub length_um: Option<f64>,
    /// Distance between the returned values in pixels (the first is at the start, the last
    /// at the end).
    pub step_px: f64,
    /// Distance between the returned values in µm.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub step_um: Option<f64>,
    /// Samples taken along the line (one per pixel up to 20 000).
    pub samples: usize,
    /// One entry per channel.
    pub channels: Vec<ProfileChannel>,
}

/// Statistics of one channel inside a polygon.
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
pub struct RoiChannel {
    /// Channel index (`c`), or the RGB component.
    pub c: u32,
    /// Channel or component name.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// Smallest value.
    pub min: f64,
    /// Largest value.
    pub max: f64,
    /// Mean value.
    pub mean: f64,
    /// Standard deviation.
    pub std: f64,
    /// Median value.
    pub median: f64,
    /// Sum of the values (integrated intensity).
    pub sum: f64,
}

/// Statistics inside a polygon.
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
pub struct Roi {
    /// The vertices, in pixels of the level read.
    pub polygon: Vec<[f64; 2]>,
    /// Pixels inside.
    pub area_px: u64,
    /// Area in µm², when the pixel size is recorded.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub area_um2: Option<f64>,
    /// One entry per channel.
    pub channels: Vec<RoiChannel>,
}

/// Minimum, median, mean and maximum.
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
pub struct Spread {
    /// Smallest.
    pub min: f64,
    /// Median.
    pub median: f64,
    /// Mean.
    pub mean: f64,
    /// Largest.
    pub max: f64,
}

/// Measurements on one plane: a line profile, statistics inside a polygon or an object count.
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
pub struct MeasureOutput {
    /// The file.
    pub path: String,
    /// Format id.
    pub format: String,
    /// Image index.
    pub image: u32,
    /// Pyramid level read; coordinates are its pixels.
    pub level: u32,
    /// Level-0 pixels per pixel of this level (1 at level 0).
    pub downsample: f64,
    /// The z plane read.
    pub z: u32,
    /// The time point read.
    pub t: u32,
    /// Pixel size at this level in µm, when recorded.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pixel_size_um: Option<f64>,
    /// Profile along `line`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub profile: Option<Profile>,
    /// Statistics inside `polygon`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub roi: Option<Roi>,
    /// Objects.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub objects: Option<Objects>,
    /// Lanes and bands.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub lanes: Option<Lanes>,
    /// What was assumed.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub notes: Vec<String>,
}

/// The values of one plane as `f64`, one `Vec` per component (1 for grayscale, 3 for RGB).
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,
        })
    }

    /// Bilinear value of component `k` at level coordinates (x, y) (pixel centres at
    /// integer + 0.5); `None` outside. The plane's edges (x = 0 and x = width) are inside: the
    /// half pixel between the last centre and the edge takes the edge pixel's value.
    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())
}

/// The image, z, t and channels a request reads.
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 height in µm when it differs from the width.
    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
    };
    // a zero, negative or missing pixel size is no pixel size
    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,
    })
}

/// The rectangle of the level that holds the points, grown by `pad` pixels and clipped.
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(())
}

/// Read channel `c` of the target in rectangle `r`.
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)
}

/// The channels to report: (component grid index, label) per selected channel and component.
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()
}

/// Measure one plane.
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]",
        ));
    }
    // both ends on the plane (its edges included), so every sample along the line has a value
    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();
    // µm per pixel along this line (pixels may be taller than wide)
    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;
                    // on the plane, which holds both ends (rounding aside)
                    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() {
                        // every sample across the line is off the plane (a wide line at a
                        // corner): the value on the line itself
                        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,
    })
}

/// `k` values evenly spaced from the first sample of `v` to the last, each the mean of the
/// finite samples within half a step of it (or as close to the ends as it is).
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| {
            // i · step can land a rounding error past the last sample
            let c = (i as f64 * step).min(last);
            // a window centred on the value, narrower at the ends of the line
            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()
}

/// Width (in samples) at half height of the peak at `imax` above `base`, by linear
/// interpolation; `None` when the profile does not fall below half height on both sides.
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?)
}

/// Six significant digits keep the profile short without losing anything a sensor measures.
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];
        // half height 2 at index 3 and 7
        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() {
        // i · step rounds past the last sample for some lengths (a 256-pixel line panicked)
        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}");
            }
        }
    }

    /// A plane whose value in channel 0 is its column (in channel 1, 1000 + its row).
    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);
            // edge to edge across the middle, along the top and bottom edges, and diagonals
            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}");
                }
            }
        }
        // across the ramp: the ends take the edge pixels' values, and the peak is at the end
        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]],
                // repeated and collinear vertices
                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);
        // each value is centred on its position, the ends included
        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);
    }
}