wsi-dicom-annotations 0.2.1

DICOM WSI ANN, SEG, SR, and Parametric Map interoperability
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
mod conversion;
mod document;
mod raster;
mod read;
mod runs;
mod write;

#[cfg(test)]
#[path = "seg/performance_tests.rs"]
mod performance_tests;
#[cfg(test)]
#[path = "seg/run_tests.rs"]
mod run_tests;
#[cfg(test)]
#[path = "seg/vectorization_tests.rs"]
mod vectorization_tests;

use crate::{Error, Result};

use super::context::DicomAnnotationContext;
use super::derived_object::DerivedObjectProducer;
use super::model::AnnotationGroup;
use super::model::{
    validate_polygon, validate_text, AlgorithmIdentification, DicomCode, FindingSemantics,
    GenerationType, InteroperabilityDiagnostic, Point2,
};

const MAX_SEG_FILE_BYTES: u64 = 1024 * 1024 * 1024;
const MAX_SEGMENTATION_FRAMES: usize = 65_536;
const MAX_SEGMENTATION_PIXELS: usize = 512 * 1024 * 1024;
const MAX_VECTORIZED_RUNS: usize = 1_000_000;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SegmentationKind {
    Binary,
    LabelMap,
    Fractional,
}

/// Controls whether SEG-to-ANN projection may discard nonrepresentable semantics.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SegToAnnConversionPolicy {
    RejectLoss,
    AllowLoss,
}

/// Editable ANN groups projected from a SEG, together with typed loss diagnostics.
#[derive(Debug, Clone, PartialEq)]
pub struct VectorizedAnnotations {
    groups: Vec<AnnotationGroup>,
    diagnostics: Vec<InteroperabilityDiagnostic>,
}

impl VectorizedAnnotations {
    #[must_use]
    pub fn groups(&self) -> &[AnnotationGroup] {
        &self.groups
    }

    #[must_use]
    pub fn diagnostics(&self) -> &[InteroperabilityDiagnostic] {
        &self.diagnostics
    }

    #[must_use]
    pub fn into_groups(self) -> Vec<AnnotationGroup> {
        self.groups
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct SegmentationSegment {
    source_segment_number: Option<u16>,
    label: String,
    description: String,
    finding: FindingSemantics,
    tracking_id: Option<String>,
    tracking_uid: Option<String>,
    outer_polygons: Vec<Vec<Point2>>,
    exclusion_polygons: Vec<Vec<Point2>>,
    // `Some` preserves GeoJSON MultiPolygon ownership: each entry contains
    // the holes belonging only to the outer polygon at the same index.
    component_holes: Option<Vec<Vec<Vec<Point2>>>>,
}

impl SegmentationSegment {
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        label: impl Into<String>,
        category: DicomCode,
        property_type: DicomCode,
        recommended_display_cielab: [u16; 3],
        outer_polygons: Vec<Vec<Point2>>,
        exclusion_polygons: Vec<Vec<Point2>>,
    ) -> Result<Self> {
        let label = label.into();
        if label.trim().is_empty() || label.len() > 64 || label.contains(['\\', '\0']) {
            return Err(Error::InvalidInput(
                "segment label must be 1..=64 bytes and contain no DICOM separator or NUL".into(),
            ));
        }
        if outer_polygons.is_empty() {
            return Err(Error::InvalidInput(
                "a binary segment needs at least one outer polygon".into(),
            ));
        }
        for polygon in outer_polygons.iter().chain(&exclusion_polygons) {
            validate_polygon(polygon)?;
        }
        Ok(Self {
            source_segment_number: None,
            label,
            description: String::new(),
            finding: FindingSemantics::manual(category, property_type, recommended_display_cielab),
            tracking_id: None,
            tracking_uid: None,
            outer_polygons,
            exclusion_polygons,
            component_holes: None,
        })
    }

    pub fn with_component_holes(mut self, component_holes: Vec<Vec<Vec<Point2>>>) -> Result<Self> {
        if component_holes.len() != self.outer_polygons.len() {
            return Err(Error::InvalidInput(
                "component hole groups must match the outer polygon count".into(),
            ));
        }
        for hole in component_holes.iter().flatten() {
            validate_polygon(hole)?;
        }
        self.exclusion_polygons = component_holes.iter().flatten().cloned().collect();
        self.component_holes = Some(component_holes);
        Ok(self)
    }

    pub fn with_description(mut self, description: impl Into<String>) -> Result<Self> {
        let description = description.into();
        if description.len() > 1_024 || description.contains('\0') {
            return Err(Error::InvalidInput(
                "segment description exceeds 1024 bytes or contains NUL".into(),
            ));
        }
        self.description = description;
        Ok(self)
    }

    pub fn with_generation(
        mut self,
        generation_type: GenerationType,
        algorithms: Vec<AlgorithmIdentification>,
    ) -> Result<Self> {
        self.finding = self.finding.with_generation(generation_type, algorithms)?;
        Ok(self)
    }

    #[must_use]
    pub fn with_property_type_modifiers(mut self, modifiers: Vec<DicomCode>) -> Self {
        self.finding = self.finding.with_property_type_modifiers(modifiers);
        self
    }

    pub fn with_tracking(
        mut self,
        tracking_id: impl Into<String>,
        tracking_uid: impl Into<String>,
    ) -> Result<Self> {
        let tracking_id = tracking_id.into();
        let tracking_uid = tracking_uid.into();
        validate_text("tracking ID", &tracking_id, 10_240)?;
        validate_text("tracking UID", &tracking_uid, 64)?;
        self.tracking_id = Some(tracking_id);
        self.tracking_uid = Some(tracking_uid);
        Ok(self)
    }

    #[must_use]
    pub fn with_anatomic_regions(mut self, regions: Vec<DicomCode>) -> Self {
        self.finding = self.finding.with_anatomic_regions(regions);
        self
    }

    #[must_use]
    pub fn with_primary_anatomic_structures(mut self, structures: Vec<DicomCode>) -> Self {
        self.finding = self.finding.with_primary_anatomic_structures(structures);
        self
    }

    pub(crate) fn with_finding_semantics(mut self, finding: FindingSemantics) -> Result<Self> {
        finding.validate()?;
        self.finding = finding;
        Ok(self)
    }

    #[must_use]
    pub fn label(&self) -> &str {
        &self.label
    }

    #[must_use]
    pub fn description(&self) -> &str {
        &self.description
    }

    #[must_use]
    pub const fn generation_type(&self) -> GenerationType {
        self.finding.generation_type()
    }

    #[must_use]
    pub fn algorithms(&self) -> &[AlgorithmIdentification] {
        self.finding.algorithms()
    }

    #[must_use]
    pub fn source_segment_number(&self) -> Option<u16> {
        self.source_segment_number
    }

    #[must_use]
    pub fn category(&self) -> &DicomCode {
        self.finding.category()
    }

    #[must_use]
    pub fn property_type(&self) -> &DicomCode {
        self.finding.property_type()
    }

    #[must_use]
    pub fn property_type_modifiers(&self) -> &[DicomCode] {
        self.finding.property_type_modifiers()
    }

    #[must_use]
    pub fn tracking_id(&self) -> Option<&str> {
        self.tracking_id.as_deref()
    }

    #[must_use]
    pub fn tracking_uid(&self) -> Option<&str> {
        self.tracking_uid.as_deref()
    }

    #[must_use]
    pub fn anatomic_regions(&self) -> &[DicomCode] {
        self.finding.anatomic_regions()
    }

    #[must_use]
    pub fn primary_anatomic_structures(&self) -> &[DicomCode] {
        self.finding.primary_anatomic_structures()
    }

    #[must_use]
    pub const fn recommended_display_cielab(&self) -> [u16; 3] {
        self.finding.recommended_display_cielab()
    }

    #[must_use]
    pub fn outer_polygons(&self) -> &[Vec<Point2>] {
        &self.outer_polygons
    }

    #[must_use]
    pub fn exclusion_polygons(&self) -> &[Vec<Point2>] {
        &self.exclusion_polygons
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BinarySegmentationFrame {
    segment_number: u16,
    tile_col: u32,
    tile_row: u32,
    width: u16,
    height: u16,
    mask: Vec<bool>,
}

impl BinarySegmentationFrame {
    #[must_use]
    pub const fn segment_number(&self) -> u16 {
        self.segment_number
    }

    #[must_use]
    pub const fn tile_col(&self) -> u32 {
        self.tile_col
    }

    #[must_use]
    pub const fn tile_row(&self) -> u32 {
        self.tile_row
    }

    #[must_use]
    pub const fn dimensions(&self) -> (u16, u16) {
        (self.width, self.height)
    }

    #[must_use]
    pub fn mask(&self) -> &[bool] {
        &self.mask
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FractionalSegmentationFrame {
    segment_number: u16,
    tile_col: u32,
    tile_row: u32,
    width: u16,
    height: u16,
    maximum_fractional_value: u16,
    values: Vec<u16>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BinaryMaskRun {
    segment_number: u16,
    row: u32,
    column_start: u32,
    length: u32,
}

impl BinaryMaskRun {
    #[must_use]
    pub const fn segment_number(&self) -> u16 {
        self.segment_number
    }

    #[must_use]
    pub const fn row(&self) -> u32 {
        self.row
    }

    #[must_use]
    pub const fn column_start(&self) -> u32 {
        self.column_start
    }

    #[must_use]
    pub const fn length(&self) -> u32 {
        self.length
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FractionalMaskRun {
    segment_number: u16,
    row: u32,
    column_start: u32,
    maximum_fractional_value: u16,
    values: Vec<u16>,
}

impl FractionalMaskRun {
    #[must_use]
    pub const fn segment_number(&self) -> u16 {
        self.segment_number
    }

    #[must_use]
    pub const fn row(&self) -> u32 {
        self.row
    }

    #[must_use]
    pub const fn column_start(&self) -> u32 {
        self.column_start
    }

    #[must_use]
    pub const fn maximum_fractional_value(&self) -> u16 {
        self.maximum_fractional_value
    }

    #[must_use]
    pub fn values(&self) -> &[u16] {
        &self.values
    }
}

impl FractionalSegmentationFrame {
    #[must_use]
    pub const fn segment_number(&self) -> u16 {
        self.segment_number
    }

    #[must_use]
    pub const fn tile_col(&self) -> u32 {
        self.tile_col
    }

    #[must_use]
    pub const fn tile_row(&self) -> u32 {
        self.tile_row
    }

    #[must_use]
    pub const fn dimensions(&self) -> (u16, u16) {
        (self.width, self.height)
    }

    #[must_use]
    pub const fn maximum_fractional_value(&self) -> u16 {
        self.maximum_fractional_value
    }

    #[must_use]
    pub fn values(&self) -> &[u16] {
        &self.values
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct SegmentationDocument {
    source: DicomAnnotationContext,
    kind: SegmentationKind,
    sop_instance_uid: String,
    series_instance_uid: String,
    content_label: String,
    content_description: String,
    content_creator_name: Option<String>,
    producer: DerivedObjectProducer,
    segments: Vec<SegmentationSegment>,
    imported_binary_frames: Option<Vec<BinarySegmentationFrame>>,
    imported_fractional_frames: Option<Vec<FractionalSegmentationFrame>>,
    diagnostics: Vec<InteroperabilityDiagnostic>,
}