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
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
use super::*;

#[test]
fn segmentation_import_enforces_pixel_budget_for_every_representation() {
    let directory = tempfile::tempdir().unwrap();
    let source = directory.path().join("source.dcm");
    let input = directory.path().join("seg.dcm");
    write_source_wsi(&source, 4, 4, 4, 4);
    write_fractional_seg(&input, [128; 16]);
    let context = DicomAnnotationContext::from_source(&source).unwrap();
    let original = dicom_object::open_file(&input).unwrap();

    for kind in ["BINARY", "FRACTIONAL", "LABELMAP"] {
        let mut object = original.clone();
        object.put(DataElement::new(tags::SEGMENTATION_TYPE, VR::CS, kind));
        for tag in [tags::ROWS, tags::COLUMNS] {
            object.put(DataElement::new(
                tag,
                VR::US,
                PrimitiveValue::from(32768_u16),
            ));
        }
        object.write_to_file(&input).unwrap();

        let error = SegmentationDocument::read_seg(&input, &context).unwrap_err();
        assert!(
            error.to_string().contains("pixel import limit"),
            "{kind} must reject excessive decoded pixels before reading the payload: {error}"
        );
    }
}

#[test]
fn segmentation_import_preserves_shared_segment_identification() {
    let directory = tempfile::tempdir().unwrap();
    let source = directory.path().join("source.dcm");
    let input = directory.path().join("seg.dcm");
    write_source_wsi(&source, 4, 4, 4, 4);
    write_fractional_seg(&input, [128; 16]);
    let context = DicomAnnotationContext::from_source(&source).unwrap();
    let mut object = dicom_object::open_file(&input).unwrap();
    let mut segments = object.take(tags::SEGMENT_SEQUENCE).unwrap();
    segments.update_value(|value| {
        let items = value.items_mut().unwrap();
        let mut second = items[0].clone();
        second.put(DataElement::new(
            tags::SEGMENT_NUMBER,
            VR::US,
            PrimitiveValue::from(2_u16),
        ));
        items.push(second);
    });
    object.put(segments);
    let mut frames = object
        .take(tags::PER_FRAME_FUNCTIONAL_GROUPS_SEQUENCE)
        .unwrap();
    let mut identification = None;
    frames.update_value(|value| {
        let mut sequence = value.items_mut().unwrap()[0]
            .take(tags::SEGMENT_IDENTIFICATION_SEQUENCE)
            .unwrap();
        sequence.update_value(|value| {
            value.items_mut().unwrap()[0].put(DataElement::new(
                tags::REFERENCED_SEGMENT_NUMBER,
                VR::US,
                PrimitiveValue::from(2_u16),
            ));
        });
        identification = Some(sequence);
    });
    object.put(frames);
    let mut shared = object
        .take(tags::SHARED_FUNCTIONAL_GROUPS_SEQUENCE)
        .unwrap();
    shared.update_value(|value| {
        value.items_mut().unwrap()[0].put(identification.take().unwrap());
    });
    object.put(shared);
    object.write_to_file(&input).unwrap();

    let imported = SegmentationDocument::read_seg(&input, &context).unwrap();
    let frames = imported.fractional_frames().unwrap();
    assert_eq!(frames.len(), 1);
    assert_eq!(frames[0].segment_number(), 2);
    assert_eq!(frames[0].values(), &[128; 16]);
}

#[test]
fn segmentation_import_rejects_missing_or_unknown_segment_identification() {
    let directory = tempfile::tempdir().unwrap();
    let source = directory.path().join("source.dcm");
    let input = directory.path().join("seg.dcm");
    write_source_wsi(&source, 4, 4, 4, 4);
    write_fractional_seg(&input, [128; 16]);
    let context = DicomAnnotationContext::from_source(&source).unwrap();
    let original = dicom_object::open_file(&input).unwrap();

    for number in [None, Some(0_u16), Some(2_u16)] {
        let mut object = original.clone();
        let mut frames = object
            .take(tags::PER_FRAME_FUNCTIONAL_GROUPS_SEQUENCE)
            .unwrap();
        frames.update_value(|value| {
            let frame = &mut value.items_mut().unwrap()[0];
            let mut identification = frame.take(tags::SEGMENT_IDENTIFICATION_SEQUENCE).unwrap();
            if let Some(number) = number {
                identification.update_value(|value| {
                    value.items_mut().unwrap()[0].put(DataElement::new(
                        tags::REFERENCED_SEGMENT_NUMBER,
                        VR::US,
                        PrimitiveValue::from(number),
                    ));
                });
                frame.put(identification);
            }
        });
        object.put(frames);
        object.write_to_file(&input).unwrap();

        assert!(
            SegmentationDocument::read_seg(&input, &context).is_err(),
            "invalid segment reference {number:?} was accepted"
        );
    }
}

#[test]
fn segmentation_import_rejects_unaligned_frame_positions() {
    let directory = tempfile::tempdir().unwrap();
    let source = directory.path().join("source.dcm");
    let input = directory.path().join("seg.dcm");
    write_source_wsi(&source, 4, 4, 4, 4);
    write_fractional_seg(&input, [128; 16]);
    let context = DicomAnnotationContext::from_source(&source).unwrap();
    let original = dicom_object::open_file(&input).unwrap();

    for tag in [
        tags::COLUMN_POSITION_IN_TOTAL_IMAGE_PIXEL_MATRIX,
        tags::ROW_POSITION_IN_TOTAL_IMAGE_PIXEL_MATRIX,
    ] {
        let mut object = original.clone();
        let mut frames = object
            .take(tags::PER_FRAME_FUNCTIONAL_GROUPS_SEQUENCE)
            .unwrap();
        frames.update_value(|value| {
            let frame = &mut value.items_mut().unwrap()[0];
            let mut plane = frame.take(tags::PLANE_POSITION_SLIDE_SEQUENCE).unwrap();
            plane.update_value(|value| {
                value.items_mut().unwrap()[0].put(DataElement::new(
                    tag,
                    VR::SL,
                    PrimitiveValue::from(2_i32),
                ));
            });
            frame.put(plane);
        });
        object.put(frames);
        object.write_to_file(&input).unwrap();

        assert!(SegmentationDocument::read_seg(&input, &context).is_err());
    }
}

#[test]
fn segmentation_import_rejects_zero_frame_dimensions() {
    let directory = tempfile::tempdir().unwrap();
    let source = directory.path().join("source.dcm");
    let input = directory.path().join("seg.dcm");
    write_source_wsi(&source, 4, 4, 4, 4);
    write_fractional_seg(&input, [128; 16]);
    let context = DicomAnnotationContext::from_source(&source).unwrap();
    let original = dicom_object::open_file(&input).unwrap();

    for tiled_full in [false, true] {
        for tag in [tags::ROWS, tags::COLUMNS] {
            let mut object = original.clone();
            if tiled_full {
                object
                    .take(tags::PER_FRAME_FUNCTIONAL_GROUPS_SEQUENCE)
                    .unwrap();
                object.put(DataElement::new(
                    tags::DIMENSION_ORGANIZATION_TYPE,
                    VR::CS,
                    "TILED_FULL",
                ));
                for dimension in [
                    tags::TOTAL_PIXEL_MATRIX_ROWS,
                    tags::TOTAL_PIXEL_MATRIX_COLUMNS,
                ] {
                    object.put(DataElement::new(
                        dimension,
                        VR::UL,
                        PrimitiveValue::from(4_u32),
                    ));
                }
            }
            object.put(DataElement::new(tag, VR::US, PrimitiveValue::from(0_u16)));
            object.write_to_file(&input).unwrap();

            assert!(SegmentationDocument::read_seg(&input, &context).is_err());
        }
    }
}

#[test]
fn sparse_binary_segmentation_preserves_a_hole_and_omits_empty_tiles() {
    let dir = tempfile::tempdir().unwrap();
    let source = dir.path().join("source.dcm");
    let output = dir.path().join("mask.dcm");
    write_source_wsi(&source, 12, 8, 4, 4);
    let source_before = std::fs::read(&source).unwrap();
    let context = DicomAnnotationContext::from_source(&source).unwrap();
    let segment = SegmentationSegment::new(
        "Viable tumor",
        code("MORPH", "Morphologically abnormal structure"),
        code("VIABLE_TUMOR", "Viable tumor"),
        [48_000, 40_000, 20_000],
        vec![vec![
            Point2::new(0.0, 0.0),
            Point2::new(8.0, 0.0),
            Point2::new(8.0, 8.0),
            Point2::new(0.0, 8.0),
        ]],
        vec![vec![
            Point2::new(2.0, 2.0),
            Point2::new(6.0, 2.0),
            Point2::new(6.0, 6.0),
            Point2::new(2.0, 6.0),
        ]],
    )
    .unwrap();
    let segmentation = SegmentationDocument::binary(context.clone(), vec![segment]).unwrap();

    let frames = segmentation.rasterized_frames().unwrap();

    assert_eq!(
        frames.len(),
        4,
        "the empty right-most tile column must be omitted"
    );
    assert!(frames
        .iter()
        .all(|frame| frame.mask().iter().any(|value| *value)));
    assert!(
        !frames
            .iter()
            .find(|frame| frame.tile_col() == 0 && frame.tile_row() == 0)
            .unwrap()
            .mask()[2 * 4 + 2]
    );

    segmentation.write_seg(&output).unwrap();
    let imported = SegmentationDocument::read_seg(&output, &context).unwrap();
    assert_eq!(imported.rasterized_frames().unwrap(), frames);
    assert_eq!(
        dicom_object::open_file(&output)
            .unwrap()
            .element(tags::DIMENSION_ORGANIZATION_TYPE)
            .unwrap()
            .to_str()
            .unwrap(),
        "TILED_SPARSE"
    );
    assert_eq!(std::fs::read(&source).unwrap(), source_before);
}

#[test]
fn segmentation_row_runs_merge_tile_boundaries_and_have_a_stable_digest() {
    let directory = tempfile::tempdir().unwrap();
    let source = directory.path().join("source.dcm");
    let output = directory.path().join("segmentation.dcm");
    write_source_wsi(&source, 8, 4, 4, 4);
    let context = DicomAnnotationContext::from_source(&source).unwrap();
    let segmentation = SegmentationDocument::binary(
        context.clone(),
        vec![SegmentationSegment::new(
            "Tumor",
            code("MORPH", "Morphologically abnormal structure"),
            code("TUMOR", "Tumor"),
            [0, 0, 0],
            vec![vec![
                Point2::new(0.0, 0.0),
                Point2::new(8.0, 0.0),
                Point2::new(8.0, 4.0),
                Point2::new(0.0, 4.0),
            ]],
            Vec::new(),
        )
        .unwrap()],
    )
    .unwrap();

    let runs = segmentation.binary_runs().unwrap();
    assert_eq!(runs.len(), 4);
    assert!(runs.iter().enumerate().all(|(row, run)| {
        run.segment_number() == 1
            && run.row() == u32::try_from(row).unwrap()
            && run.column_start() == 0
            && run.length() == 8
    }));
    let digest = segmentation.mask_digest().unwrap();
    segmentation.write_seg(&output).unwrap();
    let imported = SegmentationDocument::read_seg(&output, &context).unwrap();
    assert_eq!(imported.binary_runs().unwrap(), runs);
    assert_eq!(imported.mask_digest().unwrap(), digest);
    assert_ne!(
        imported.revised().sop_instance_uid(),
        imported.sop_instance_uid()
    );
    assert_eq!(
        imported.revised().series_instance_uid(),
        imported.series_instance_uid()
    );
}

#[test]
fn fractional_segmentation_rewrite_fails_without_creating_output() {
    let directory = tempfile::tempdir().unwrap();
    let source = directory.path().join("source.dcm");
    let input = directory.path().join("fractional.dcm");
    let output = directory.path().join("must-not-exist.dcm");
    write_source_wsi(&source, 4, 4, 4, 4);
    write_fractional_seg(&input, [1; 16]);
    let context = DicomAnnotationContext::from_source(&source).unwrap();
    let segmentation = SegmentationDocument::read_seg(&input, &context).unwrap();

    assert!(segmentation.write_seg(&output).is_err());
    assert!(!output.exists());
    assert!(!segmentation.fractional_runs().unwrap().is_empty());
}

#[test]
fn segmentation_import_requires_an_exact_source_sop_reference() {
    let directory = tempfile::tempdir().unwrap();
    let source = directory.path().join("source.dcm");
    let segmentation_path = directory.path().join("segmentation.dcm");
    write_source_wsi(&source, 4, 4, 4, 4);
    write_fractional_seg(&segmentation_path, [1; 16]);
    let mut object = dicom_object::open_file(&segmentation_path).unwrap();
    let mut shared = object
        .take(tags::SHARED_FUNCTIONAL_GROUPS_SEQUENCE)
        .unwrap();
    shared.update_value(|value| {
        let shared = &mut value.items_mut().unwrap()[0];
        let mut derivations = shared.take(tags::DERIVATION_IMAGE_SEQUENCE).unwrap();
        derivations.update_value(|value| {
            let derivation = &mut value.items_mut().unwrap()[0];
            let mut sources = derivation.take(tags::SOURCE_IMAGE_SEQUENCE).unwrap();
            sources.update_value(|value| {
                value.items_mut().unwrap()[0].put(DataElement::new(
                    tags::REFERENCED_SOP_INSTANCE_UID,
                    VR::UI,
                    "2.25.999",
                ));
            });
            derivation.put(sources);
        });
        shared.put(derivations);
    });
    object.put(shared);
    object.write_to_file(&segmentation_path).unwrap();
    let context = DicomAnnotationContext::from_source(&source).unwrap();

    assert!(SegmentationDocument::read_seg(&segmentation_path, &context).is_err());
}

#[test]
fn sparse_segmentation_work_is_bounded_by_annotated_tiles_not_slide_area() {
    let directory = tempfile::tempdir().unwrap();
    let source = directory.path().join("large-source.dcm");
    write_source_wsi(&source, 1_000_000, 1_000_000, 256, 256);
    let context = DicomAnnotationContext::from_source(&source).unwrap();
    let polygon = vec![
        Point2::new(8.0, 8.0),
        Point2::new(24.0, 8.0),
        Point2::new(24.0, 24.0),
        Point2::new(8.0, 24.0),
    ];
    let segmentation = SegmentationDocument::binary(
        context,
        vec![SegmentationSegment::new(
            "Small focus",
            code("MORPH", "Morphologically abnormal structure"),
            code("FOCUS", "Small focus"),
            [48_000, 40_000, 20_000],
            vec![polygon],
            Vec::new(),
        )
        .unwrap()],
    )
    .unwrap();

    let frames = segmentation.rasterized_frames().unwrap();

    assert_eq!(frames.len(), 1);
    assert_eq!((frames[0].tile_col(), frames[0].tile_row()), (0, 0));
    assert_eq!(frames[0].segment_number(), 1);
    assert_eq!(frames[0].dimensions(), (256, 256));
    assert_eq!(frames[0].mask().iter().filter(|value| **value).count(), 256);
}

#[test]
fn fractional_segmentation_import_preserves_raster_values_as_read_only() {
    let directory = tempfile::tempdir().unwrap();
    let source = directory.path().join("source.dcm");
    let sidecar = directory.path().join("fractional-seg.dcm");
    write_source_wsi(&source, 4, 4, 4, 4);
    write_fractional_seg(
        &sidecar,
        [
            0, 32, 64, 96, 128, 160, 192, 224, 255, 224, 192, 160, 128, 96, 64, 32,
        ],
    );
    let context = DicomAnnotationContext::from_source(&source).unwrap();

    let imported = SegmentationDocument::read_seg(&sidecar, &context).unwrap();

    assert_eq!(imported.kind(), crate::SegmentationKind::Fractional);
    assert!(!imported.editable());
    let frames = imported.fractional_frames().unwrap();
    assert_eq!(frames.len(), 1);
    assert_eq!(frames[0].segment_number(), 1);
    assert_eq!(frames[0].dimensions(), (4, 4));
    assert_eq!(frames[0].maximum_fractional_value(), 255);
    assert_eq!(frames[0].values()[8], 255);
    assert_eq!((frames[0].tile_col(), frames[0].tile_row()), (0, 0));
    let runs = imported.fractional_runs().unwrap();
    assert_eq!(runs.len(), 4);
    assert_eq!(runs[0].segment_number(), 1);
    assert_eq!(runs[0].row(), 0);
    assert_eq!(runs[0].column_start(), 1);
    assert_eq!(runs[0].maximum_fractional_value(), 255);
    assert_eq!(runs[0].values(), &[32, 64, 96]);
    assert_eq!(imported.mask_digest().unwrap().len(), 64);
    assert!(imported.binary_runs().is_err());
    assert!(imported.rasterized_frames().is_err());
    assert!(imported
        .vectorized_annotations(crate::SegToAnnConversionPolicy::RejectLoss)
        .is_err());
    assert!(imported
        .write_seg(directory.path().join("fractional-copy.dcm"))
        .is_err());
    assert!(imported.segment_for_number(1).is_some());
    assert!(imported.segment_for_number(2).is_none());
    assert_ne!(
        imported.revised().sop_instance_uid(),
        imported.sop_instance_uid()
    );
    let discovered = discover_sidecars(&context).unwrap();
    assert_eq!(discovered.len(), 1);
    assert_eq!(discovered[0].kind(), SidecarKind::FractionalSegmentation);
}

#[test]
fn tiled_full_binary_segmentation_derives_frame_positions() {
    let directory = tempfile::tempdir().unwrap();
    let source = directory.path().join("source.dcm");
    let segmentation_path = directory.path().join("tiled-full-seg.dcm");
    write_source_wsi(&source, 8, 4, 4, 4);
    let context = DicomAnnotationContext::from_source(&source).unwrap();
    let segmentation = SegmentationDocument::binary(
        context.clone(),
        vec![SegmentationSegment::new(
            "Tumor",
            code("MORPH", "Morphology"),
            code("TUMOR", "Tumor"),
            [0, 0, 0],
            vec![vec![
                Point2::new(0.0, 0.0),
                Point2::new(8.0, 0.0),
                Point2::new(8.0, 4.0),
                Point2::new(0.0, 4.0),
            ]],
            Vec::new(),
        )
        .unwrap()],
    )
    .unwrap();
    let expected_runs = segmentation.binary_runs().unwrap();
    segmentation.write_seg(&segmentation_path).unwrap();
    let mut object = dicom_object::open_file(&segmentation_path).unwrap();
    object
        .take(tags::PER_FRAME_FUNCTIONAL_GROUPS_SEQUENCE)
        .unwrap();
    object.put(DataElement::new(
        tags::DIMENSION_ORGANIZATION_TYPE,
        VR::CS,
        "TILED_FULL",
    ));
    object.write_to_file(&segmentation_path).unwrap();

    let imported = SegmentationDocument::read_seg(&segmentation_path, &context).unwrap();

    assert_eq!(imported.binary_runs().unwrap(), expected_runs);
}

#[test]
fn tiled_full_fractional_segmentation_derives_frame_positions() {
    let directory = tempfile::tempdir().unwrap();
    let source = directory.path().join("source.dcm");
    let segmentation_path = directory.path().join("tiled-full-fractional.dcm");
    write_source_wsi(&source, 4, 4, 4, 4);
    write_fractional_seg(&segmentation_path, [128; 16]);
    let mut object = dicom_object::open_file(&segmentation_path).unwrap();
    object
        .take(tags::PER_FRAME_FUNCTIONAL_GROUPS_SEQUENCE)
        .unwrap();
    for element in [
        DataElement::new(tags::DIMENSION_ORGANIZATION_TYPE, VR::CS, "TILED_FULL"),
        DataElement::new(
            tags::TOTAL_PIXEL_MATRIX_ROWS,
            VR::UL,
            PrimitiveValue::from(4_u32),
        ),
        DataElement::new(
            tags::TOTAL_PIXEL_MATRIX_COLUMNS,
            VR::UL,
            PrimitiveValue::from(4_u32),
        ),
    ] {
        object.put(element);
    }
    object.write_to_file(&segmentation_path).unwrap();
    let context = DicomAnnotationContext::from_source(&source).unwrap();

    let imported = SegmentationDocument::read_seg(&segmentation_path, &context).unwrap();

    assert_eq!(imported.fractional_frames().unwrap().len(), 1);
    assert_eq!(
        imported.fractional_frames().unwrap()[0].values(),
        &[128; 16]
    );
}

#[test]
fn labelmap_background_segment_zero_is_inspectable_but_not_a_mask() {
    let directory = tempfile::tempdir().unwrap();
    let source = directory.path().join("source.dcm");
    let segmentation_path = directory.path().join("labelmap.dcm");
    write_source_wsi(&source, 4, 4, 4, 4);
    write_fractional_seg(&segmentation_path, [0; 16]);
    let mut object = dicom_object::open_file(&segmentation_path).unwrap();
    object
        .take(tags::PER_FRAME_FUNCTIONAL_GROUPS_SEQUENCE)
        .unwrap();
    object.take(tags::SEGMENTATION_FRACTIONAL_TYPE).unwrap();
    object.take(tags::MAXIMUM_FRACTIONAL_VALUE).unwrap();
    let mut segments = object.take(tags::SEGMENT_SEQUENCE).unwrap();
    segments.update_value(|value| {
        value.items_mut().unwrap()[0].put(DataElement::new(
            tags::SEGMENT_NUMBER,
            VR::US,
            PrimitiveValue::from(0_u16),
        ));
    });
    object.put(segments);
    for element in [
        DataElement::new(tags::SEGMENTATION_TYPE, VR::CS, "LABELMAP"),
        DataElement::new(tags::DIMENSION_ORGANIZATION_TYPE, VR::CS, "TILED_FULL"),
        DataElement::new(
            tags::TOTAL_PIXEL_MATRIX_ROWS,
            VR::UL,
            PrimitiveValue::from(4_u32),
        ),
        DataElement::new(
            tags::TOTAL_PIXEL_MATRIX_COLUMNS,
            VR::UL,
            PrimitiveValue::from(4_u32),
        ),
    ] {
        object.put(element);
    }
    object.write_to_file(&segmentation_path).unwrap();
    let context = DicomAnnotationContext::from_source(&source).unwrap();

    let imported = SegmentationDocument::read_seg(&segmentation_path, &context).unwrap();

    assert_eq!(imported.kind(), crate::SegmentationKind::LabelMap);
    assert_eq!(imported.segments()[0].source_segment_number(), Some(0));
    assert!(imported.binary_runs().unwrap().is_empty());
}