wsi-annotation-tests 0.1.3

DICOM Annotation Test Suite for WSI ANN, SEG, SR, and Parametric Map
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
use serde_json::{Value, json};
use sha2::{Digest, Sha256};

const STUDY_UID: &str = "2.25.100000000000000000000000000000001";
const SOURCE_SERIES_UID: &str = "2.25.100000000000000000000000000000002";
const SOURCE_SOP_UID: &str = "2.25.100000000000000000000000000000003";
const FRAME_OF_REFERENCE_UID: &str = "2.25.100000000000000000000000000000004";
const PYRAMID_SOURCE_SERIES_UID: &str = "2.25.100000000000000000000000000000009";
const PYRAMID_SOURCE_SOP_UID: &str = "2.25.100000000000000000000000000000010";
const WSI_SOP_CLASS_UID: &str = "1.2.840.10008.5.1.4.1.1.77.1.6";

type Point = (f64, f64);

#[derive(Clone, Copy)]
enum MaskKind {
    Binary,
    Labelmap,
    Fractional,
}

impl MaskKind {
    const fn label(self) -> &'static str {
        match self {
            Self::Binary => "binary",
            Self::Labelmap => "labelmap",
            Self::Fractional => "fractional",
        }
    }

    const fn digest_tag(self) -> u8 {
        match self {
            Self::Binary => 0,
            Self::Labelmap => 1,
            Self::Fractional => 2,
        }
    }
}

enum MaskRun {
    Binary {
        segment_number: u16,
        row: u32,
        column_start: u32,
        length: u32,
    },
    Fractional {
        segment_number: u16,
        row: u32,
        column_start: u32,
        maximum_fractional_value: u16,
        values: Vec<u16>,
    },
}

impl MaskRun {
    fn as_json(&self) -> Value {
        match self {
            Self::Binary {
                segment_number,
                row,
                column_start,
                length,
            } => json!({
                "segment_number": segment_number,
                "row": row,
                "column_start": column_start,
                "length": length,
            }),
            Self::Fractional {
                segment_number,
                row,
                column_start,
                maximum_fractional_value,
                values,
            } => json!({
                "segment_number": segment_number,
                "row": row,
                "column_start": column_start,
                "maximum_fractional_value": maximum_fractional_value,
                "values": values,
            }),
        }
    }
}

/// Build the deterministic, implementation-independent core study oracle.
#[must_use]
pub fn build_core_ground_truth() -> Value {
    json!({
        "schema_version": 1,
        "cases": {
            "ann-2d-volume": ann_truth("2D_VOLUME", 1, 0),
            "ann-2d-frame": ann_truth("2D_FRAME", 2, 0),
            "ann-3d-common-z": ann_truth("3D_COMMON_Z", 3, 0),
            "ann-3d-xyz": ann_truth("3D_XYZ", 4, 0),
            "ann-2d-volume-level1": ann_truth("2D_VOLUME", 5, 1),
            "seg-binary": seg_truth(MaskKind::Binary, 1),
            "seg-labelmap": seg_truth(MaskKind::Labelmap, 2),
            "seg-fractional": seg_truth(MaskKind::Fractional, 3),
            "seg-binary-reordered": seg_truth(MaskKind::Binary, 4),
        },
    })
}

fn ann_truth(form: &str, index: u8, source_level: u8) -> Value {
    let is_3d = form.starts_with("3D");
    let pixel_origin = if is_3d {
        Value::Null
    } else if form == "2D_FRAME" {
        json!("FRAME")
    } else {
        json!("VOLUME")
    };
    let referenced_frame = (form == "2D_FRAME").then_some(1);
    let groups: Vec<_> = graphics()
        .into_iter()
        .enumerate()
        .map(|(offset, (graphic_type, annotations))| {
            ann_group_truth(offset + 1, graphic_type, &annotations, form, source_level)
        })
        .collect();

    json!({
        "sop_instance_uid": format!("2.25.2100000000000000000000000000000{index}"),
        "series_instance_uid": format!("2.25.2000000000000000000000000000000{index}"),
        "coordinate_type": if is_3d { "3D" } else { "2D" },
        "pixel_origin_interpretation": pixel_origin,
        "referenced_frame_number": referenced_frame,
        "content": {
            "label": format!("ANN{index}"),
            "description": format!("Deterministic {form} annotation fixture"),
            "creator_name": null,
        },
        "source": source_truth(source_level),
        "groups": groups,
    })
}

fn ann_group_truth(
    number: usize,
    graphic_type: &str,
    annotations: &[Vec<Point>; 2],
    form: &str,
    source_level: u8,
) -> Value {
    let native_dimensions = usize::from(form == "3D_XYZ") + 2;
    let canonical_dimensions = if form.starts_with("3D") { 3 } else { 2 };
    let mut native_coordinates = Vec::new();
    let mut canonical_coordinates = Vec::new();
    let mut ordinal = 0_u32;
    for annotation in annotations {
        for &(x, y) in annotation {
            if form.starts_with("3D") {
                let z = if form == "3D_COMMON_Z" {
                    0.01
                } else {
                    0.01 + f64::from(ordinal) * 0.0001
                };
                native_coordinates.extend([x * 0.001, y * 0.001]);
                if form == "3D_XYZ" {
                    native_coordinates.push(z);
                }
                canonical_coordinates.extend([x, y, z]);
                ordinal += 1;
            } else {
                native_coordinates.extend([x, y]);
                let scale = if source_level == 1 { 2.0 } else { 1.0 };
                canonical_coordinates.extend([x * scale, y * scale]);
            }
        }
    }
    let primitive_point_indices = if matches!(graphic_type, "POLYLINE" | "POLYGON") {
        vec![1, 1 + annotations[0].len() * native_dimensions]
    } else {
        Vec::new()
    };

    json!({
        "uid": format!("2.25.3000000000000000000000000000000{number}"),
        "label": format!("{graphic_type} GROUP"),
        "description": format!("Two deterministic {graphic_type} annotations"),
        "generation_type": "AUTOMATIC",
        "algorithms": [algorithm("Synthetic annotation algorithm", Some("threshold=0.5"))],
        "category": code("MORPH", "99WSI", "Morphologically abnormal structure", Some("1"), "short"),
        "property_type": code("TUMOR", "99WSI", "Tumor", None, "short"),
        "property_type_modifiers": [code("urn:wsi-interop:viable", "99WSI", "Viable", None, "urn")],
        "anatomic_regions": [code("LUNG", "99WSI", "Lung", None, "short")],
        "primary_anatomic_structures": [code("BRONCHUS", "99WSI", "Bronchus", None, "short")],
        "applies_to_all_optical_paths": false,
        "referenced_optical_paths": ["1"],
        "applies_to_all_z_planes": !form.starts_with("3D"),
        "common_z_coordinates_mm": if form == "3D_COMMON_Z" { vec![0.01] } else { Vec::new() },
        "recommended_display_cielab": [39321, 38036, 35466],
        "graphic_type": graphic_type,
        "annotation_count": 2,
        "measurements": [{
            "concept": code("AREA", "99WSI", "Synthetic area", None, "short"),
            "units": code("mm2", "UCUM", "square millimeter", None, "short"),
            "values": [1.0],
            "annotation_indices": [1],
        }],
        "geometry": {
            "mode": "Full",
            "native_dimensions": native_dimensions,
            "canonical_dimensions": canonical_dimensions,
            "native_coordinates": native_coordinates,
            "canonical_level0_coordinates": canonical_coordinates,
            "primitive_point_indices": primitive_point_indices,
        },
    })
}

fn seg_truth(kind: MaskKind, index: u8) -> Value {
    let mut segments = vec![segment_truth(1)];
    if !matches!(kind, MaskKind::Fractional) {
        segments.push(segment_truth(2));
    }
    if matches!(kind, MaskKind::Labelmap) {
        segments.insert(0, background_segment_truth());
    }
    let runs = match kind {
        MaskKind::Binary => binary_runs(),
        MaskKind::Labelmap => labelmap_runs(),
        MaskKind::Fractional => fractional_runs(),
    };
    let mode = if matches!(kind, MaskKind::Fractional) {
        "FullFractional"
    } else {
        "FullBinary"
    };

    json!({
        "sop_instance_uid": format!("2.25.5100000000000000000000000000000{index}"),
        "series_instance_uid": format!("2.25.5000000000000000000000000000000{index}"),
        "segmentation_kind": kind.label(),
        "content": {
            "label": format!("SEG{index}"),
            "description": format!("Deterministic {} segmentation fixture", kind.label().to_uppercase()),
            "creator_name": null,
        },
        "source": source_truth(0),
        "segments": segments,
        "masks": {
            "mode": mode,
            "sha256": mask_digest(kind, &runs),
            "runs": runs.iter().map(MaskRun::as_json).collect::<Vec<_>>(),
        },
    })
}

fn segment_truth(number: u8) -> Value {
    json!({
        "number": number,
        "label": format!("Segment {number}"),
        "description": "",
        "generation_type": "AUTOMATIC",
        "algorithms": [algorithm("Synthetic segmentation algorithm", None)],
        "category": code("MORPH", "99WSI", "Morphology", None, "short"),
        "property_type": code(&format!("TUMOR{number}"), "99WSI", &format!("Tumor {number}"), None, "short"),
        "property_type_modifiers": [],
        "tracking_id": format!("lesion-{number}"),
        "tracking_uid": format!("2.25.4000000000000000000000000000000{number}"),
        "anatomic_regions": [code("LUNG", "99WSI", "Lung", None, "short")],
        "primary_anatomic_structures": [],
        "recommended_display_cielab": [if number == 1 { 33423 } else { 34078 }, 35466, 34181],
    })
}

fn background_segment_truth() -> Value {
    let background = code("125040", "DCM", "Background", None, "short");
    json!({
        "number": 0,
        "label": "Background",
        "description": "",
        "generation_type": "AUTOMATIC",
        "algorithms": [],
        "category": background,
        "property_type": background,
        "property_type_modifiers": [],
        "tracking_id": null,
        "tracking_uid": null,
        "anatomic_regions": [],
        "primary_anatomic_structures": [],
        "recommended_display_cielab": [0, 32896, 32896],
    })
}

pub(crate) fn source_truth(level: u8) -> Value {
    let canonical = level == 0;
    json!({
        "sop_class_uid": WSI_SOP_CLASS_UID,
        "sop_instance_uid": if canonical { SOURCE_SOP_UID } else { PYRAMID_SOURCE_SOP_UID },
        "series_instance_uid": if canonical { SOURCE_SERIES_UID } else { PYRAMID_SOURCE_SERIES_UID },
        "study_instance_uid": STUDY_UID,
        "frame_of_reference_uid": FRAME_OF_REFERENCE_UID,
        "total_pixel_matrix_columns": if canonical { 16 } else { 8 },
        "total_pixel_matrix_rows": if canonical { 16 } else { 8 },
        "tile_columns": 4,
        "tile_rows": 4,
        "pixel_spacing": if canonical { vec![0.001, 0.001] } else { vec![0.002, 0.002] },
        "canonical_total_pixel_matrix_columns": 16,
        "canonical_total_pixel_matrix_rows": 16,
        "canonical_pixel_spacing": [0.001, 0.001],
    })
}

pub(crate) fn code(
    value: &str,
    scheme: &str,
    meaning: &str,
    coding_scheme_version: Option<&str>,
    value_kind: &str,
) -> Value {
    json!({
        "value": value,
        "value_kind": value_kind,
        "scheme": scheme,
        "coding_scheme_version": coding_scheme_version,
        "meaning": meaning,
        "context_identifier": null,
        "context_uid": null,
        "mapping_resource": null,
        "mapping_resource_uid": null,
        "context_group_version": null,
        "context_group_local_version": null,
        "context_group_extension": null,
        "context_group_extension_creator_uid": null,
    })
}

fn algorithm(name: &str, parameters: Option<&str>) -> Value {
    json!({
        "family": code("AI", "99WSI", "Artificial intelligence", None, "short"),
        "name_code": null,
        "name": name,
        "version": "1.0.0",
        "parameters": parameters,
        "source": "wsi-annotation-interop",
    })
}

fn graphics() -> [(&'static str, [Vec<Point>; 2]); 5] {
    [
        ("POINT", [vec![(0.5, 0.5)], vec![(2.5, 2.5)]]),
        (
            "POLYLINE",
            [
                vec![(0.5, 0.5), (1.5, 0.75), (2.5, 1.0)],
                vec![(0.5, 2.5), (1.5, 2.0), (2.5, 2.5)],
            ],
        ),
        (
            "POLYGON",
            [
                vec![(0.5, 0.5), (1.5, 0.5), (1.5, 1.5), (0.5, 1.5)],
                vec![(2.0, 2.0), (3.0, 2.0), (3.0, 3.0), (2.0, 3.0)],
            ],
        ),
        (
            "ELLIPSE",
            [
                vec![(0.5, 1.0), (2.5, 1.0), (1.5, 0.5), (1.5, 1.5)],
                vec![(1.0, 2.5), (3.0, 2.5), (2.0, 2.0), (2.0, 3.0)],
            ],
        ),
        (
            "RECTANGLE",
            [
                vec![(0.5, 0.5), (1.5, 0.5), (1.5, 1.5), (0.5, 1.5)],
                vec![(2.0, 2.0), (3.0, 2.0), (3.0, 3.0), (2.0, 3.0)],
            ],
        ),
    ]
}

fn binary_runs() -> Vec<MaskRun> {
    let mut planes = [[[false; 16]; 16]; 2];
    fill(&mut planes[0], 1..9, 1..9, true);
    fill(&mut planes[0], 4..6, 4..6, false);
    fill(&mut planes[0], 12..14, 1..3, true);
    fill(&mut planes[1], 6..14, 6..14, true);
    fill(&mut planes[1], 9..11, 9..11, false);
    planes
        .iter()
        .enumerate()
        .flat_map(|(index, plane)| plane_runs(plane, small_u16(index + 1)))
        .collect()
}

fn labelmap_runs() -> Vec<MaskRun> {
    let mut plane = [[0_u8; 16]; 16];
    fill(&mut plane, 1..7, 1..7, 1);
    fill(&mut plane, 9..15, 9..15, 2);
    (1..=2)
        .flat_map(|segment_number| {
            let mask = plane.map(|row| row.map(|value| value == segment_number));
            plane_runs(&mask, u16::from(segment_number))
        })
        .collect()
}

fn fractional_runs() -> Vec<MaskRun> {
    let mut plane = [[0_u16; 16]; 16];
    fill(&mut plane, 1..7, 1..7, 64);
    fill(&mut plane, 9..15, 9..15, 191);
    let mut runs = Vec::new();
    for (row, values) in plane.iter().enumerate() {
        let mut column = 0;
        while column < values.len() {
            if values[column] == 0 {
                column += 1;
                continue;
            }
            let start = column;
            while column < values.len() && values[column] != 0 {
                column += 1;
            }
            runs.push(MaskRun::Fractional {
                segment_number: 1,
                row: small_u32(row),
                column_start: small_u32(start),
                maximum_fractional_value: 255,
                values: values[start..column].to_vec(),
            });
        }
    }
    runs
}

fn plane_runs(plane: &[[bool; 16]; 16], segment_number: u16) -> Vec<MaskRun> {
    let mut runs = Vec::new();
    for (row, values) in plane.iter().enumerate() {
        let mut column = 0;
        while column < values.len() {
            if !values[column] {
                column += 1;
                continue;
            }
            let start = column;
            while column < values.len() && values[column] {
                column += 1;
            }
            runs.push(MaskRun::Binary {
                segment_number,
                row: small_u32(row),
                column_start: small_u32(start),
                length: small_u32(column - start),
            });
        }
    }
    runs
}

fn mask_digest(kind: MaskKind, runs: &[MaskRun]) -> String {
    let mut digest = Sha256::new();
    digest.update(b"dicom-viewer-seg-runs-v1\0");
    digest.update(16_u32.to_le_bytes());
    digest.update(16_u32.to_le_bytes());
    digest.update([kind.digest_tag()]);
    for run in runs {
        match run {
            MaskRun::Binary {
                segment_number,
                row,
                column_start,
                length,
            } => {
                digest.update(segment_number.to_le_bytes());
                digest.update(row.to_le_bytes());
                digest.update(column_start.to_le_bytes());
                digest.update(length.to_le_bytes());
            }
            MaskRun::Fractional {
                segment_number,
                row,
                column_start,
                maximum_fractional_value,
                values,
            } => {
                digest.update(segment_number.to_le_bytes());
                digest.update(row.to_le_bytes());
                digest.update(column_start.to_le_bytes());
                digest.update(maximum_fractional_value.to_le_bytes());
                digest.update(small_u32(values.len()).to_le_bytes());
                for value in values {
                    digest.update(value.to_le_bytes());
                }
            }
        }
    }
    format!("{:x}", digest.finalize())
}

#[allow(clippy::cast_possible_truncation)]
fn small_u32(value: usize) -> u32 {
    debug_assert!(
        u32::try_from(value).is_ok(),
        "fixture coordinate exceeds u32"
    );
    value as u32
}

#[allow(clippy::cast_possible_truncation)]
fn small_u16(value: usize) -> u16 {
    debug_assert!(u16::try_from(value).is_ok(), "fixture segment exceeds u16");
    value as u16
}

fn fill<T: Copy>(
    plane: &mut [[T; 16]; 16],
    rows: std::ops::Range<usize>,
    columns: std::ops::Range<usize>,
    value: T,
) {
    for row in rows {
        for column in columns.clone() {
            plane[row][column] = value;
        }
    }
}