Segmentation output writers¶
Utilities for exporting nnU-Net segmentations to disk and DICOM SEG.
This module provides the SegWriter abstraction and supporting helpers to
convert raw prediction arrays into persistent representations, including
NIfTI-like formats and DICOM SEG objects with appropriate coding, colors,
and metadata derived from the input images and model configuration.
SegWriter
dataclass
¶
Helper class to manage DICOM Segmentation (SEG) and RT-Struct export.
This class handles the conversion of prediction masks into standardized DICOM formats, including metadata management for algorithms and segments.
Attributes:
| Name | Type | Description |
|---|---|---|
algorithm_name |
str
|
Name of the segmentation algorithm. |
segment_names |
list[str | dict[str, str]]
|
Names or dicts describing the segments. |
segment_descriptions |
list[SegmentDescription]
|
High-level segment descriptions. |
algorithm_version |
str
|
Version string of the algorithm. |
algorithm_family |
Code
|
DICOM code for the algorithm family. |
algorithm_type |
SegmentAlgorithmTypeValues
|
DICOM algorithm type. |
instance_number |
int
|
DICOM instance number. |
series_number |
int
|
DICOM series number. |
manufacturer |
str
|
Manufacturer name for DICOM metadata. |
manufacturer_model_name |
str
|
Model name for DICOM metadata. |
series_description |
str
|
Description for the DICOM series. |
clinical_trial_series_id |
str
|
Clinical trial series ID. |
clinical_trial_time_point_id |
str
|
Clinical trial time point ID. |
body_part_examined |
str
|
Body part examined for DICOM metadata. |
validate |
bool
|
Whether to perform validation on init. |
Source code in src/nnunet_serve/seg_writers.py
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 | |
init_from_dcmqi_metadata_file(metadata_file, algorithm_version='v1.0', manufacturer='Algorithm', manufacturer_model_name='AlgorithmModel')
staticmethod
¶
Uses a DCMQI metadata file generated using [1] to initialize a SegWriter.
[1] https://qiicr.org/dcmqi/#/seg
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metadata_file
|
str
|
path to the DCMQI metadata file. |
required |
algorithm_version
|
str
|
algorithm version. |
'v1.0'
|
manufacturer
|
str
|
manufacturer. |
'Algorithm'
|
manufacturer_model_name
|
str
|
manufacturer model name. |
'AlgorithmModel'
|
Returns:
| Name | Type | Description |
|---|---|---|
SegWriter |
initialized SegWriter. |
Source code in src/nnunet_serve/seg_writers.py
init_from_metadata_dict(metadata, validate=False)
staticmethod
¶
Initializes a SegWriter from a metadata dictionary. It automatically detects the type of metadata and calls the appropriate initialization method. If there is a "path" key, it is assumed to be a DCMQI metadata file and the init_from_dcmqi_metadata_file method is called. Otherwise, the constructor is called directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metadata
|
dict[str, str]
|
metadata dictionary. |
required |
validate
|
bool
|
whether to validate the metadata. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
SegWriter |
initialized SegWriter. |
Source code in src/nnunet_serve/seg_writers.py
make_compliant(f)
¶
Tries to smooth some potential missing fields.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Dataset
|
a pydicom Dataset. |
required |
Returns:
| Type | Description |
|---|---|
|
A compliant pydicom.Dataset. |
Source code in src/nnunet_serve/seg_writers.py
to_array_if_necessary(mask)
¶
Converts a mask to a numpy array if the mask is a sitk.Image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask
|
ndarray | Image
|
the mask to convert. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: the mask as a numpy array. |
Source code in src/nnunet_serve/seg_writers.py
write_dicom_rtstruct(mask_array, source_files, output_path)
¶
Routine to write a DICOM RTstruct object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask_array
|
ndarray | Image
|
mask image. |
required |
source_files
|
list[str]
|
list of source files. |
required |
output_path
|
str
|
output path. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
"success" if the operation was successful. |
Source code in src/nnunet_serve/seg_writers.py
write_dicom_seg(mask_array, source_files, output_path, is_fractional=False, is_fractional_compliant=False, class_idx=None)
¶
Writes a DICOM segmentation file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask_array
|
ndarray | Image
|
the mask array or sitk image. |
required |
source_files
|
list[str]
|
the list of DICOM source files (as returned
by |
required |
output_path
|
str
|
the output path. |
required |
is_fractional
|
bool
|
whether the mask is fractional. Defaults to False. |
False
|
is_fractional_compliant
|
bool
|
whether the probability mask
should be converted to a map with |
False
|
class_idx
|
int | None
|
index used for selecting specific
segment descriptions when saving probability maps. Only used when
either |
None
|
Source code in src/nnunet_serve/seg_writers.py
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 | |
close_match(a, b, ratio=0.8)
¶
Returns True if the ratio of matching characters between strings a and b is greater than the specified ratio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
str
|
first string. |
required |
b
|
str
|
second string. |
required |
ratio
|
float
|
ratio of matching characters. |
0.8
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the ratio of matching characters between strings a and b is greater than the specified ratio. |
Source code in src/nnunet_serve/seg_writers.py
export_predictions(masks, output_dir, volumes=None, proba_maps=None, good_file_paths=None, suffix=None, is_dicom=False, seg_writers=None, save_proba_map=False, save_nifti_inputs=False, save_rt_struct_output=False, class_idx=None)
¶
Export stage-wise nnUNet outputs to NIfTI and optional DICOM artifacts.
This function writes one output subdirectory per stage (stage_<i>) under
output_dir and stores prediction masks for all stages. Depending on the
flags, it can also export probability maps, input NIfTI volumes, DICOM SEG,
DICOM RTSTRUCT, and DICOM fractional SEG outputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
masks
|
list[Image]
|
Predicted segmentation masks, one |
required |
output_dir
|
str
|
Base output directory. |
required |
volumes
|
list[list[Image]] | None
|
Optional stage-wise input volumes used to export NIfTI inputs
when |
None
|
proba_maps
|
list[list[Image]] | None
|
Optional stage-wise probability maps used when
|
None
|
good_file_paths
|
list[str] | None
|
Source DICOM file paths used to create DICOM outputs. |
None
|
suffix
|
str | None
|
Optional suffix appended to exported file names. |
None
|
is_dicom
|
bool
|
Whether to export DICOM-derived outputs (SEG/RTSTRUCT). |
False
|
seg_writers
|
SegWriter | list[SegWriter] | None
|
A |
None
|
save_proba_map
|
bool
|
If |
False
|
save_nifti_inputs
|
bool
|
If |
False
|
save_rt_struct_output
|
bool
|
If |
False
|
class_idx
|
int | None
|
Optional class index metadata for probability export workflows. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Mapping of artifact type to exported paths. Keys may include: |
|
|
||
|
||
|
||
|
||
|
||
|
Source code in src/nnunet_serve/seg_writers.py
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 | |
get_empty_segment_description(algorithm_type, algorithm_identification, tracking_id)
¶
Returns an empty segment description.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
algorithm_type
|
str
|
algorithm type. |
required |
algorithm_identification
|
str
|
algorithm identification. |
required |
tracking_id
|
str
|
tracking ID. |
required |
Returns:
| Type | Description |
|---|---|
|
hd.seg.SegmentDescription: empty segment description. |
Source code in src/nnunet_serve/seg_writers.py
get_segment_type_code(segment, i)
¶
Resolve and build the DICOM coded concept for a segment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
segment
|
dict | str
|
Segment specification. If a dict, the following keys are supported:
If a string is provided, it is interpreted as |
required |
i
|
int
|
1-based segment number. |
required |
Returns:
| Type | Description |
|---|---|
Code
|
|
Code
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/nnunet_serve/seg_writers.py
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 | |
one_hot_encode(arr, n_labels)
¶
Converts a numpy array to a one-hot encoded numpy array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arr
|
ndarray
|
numpy array to be converted. |
required |
n_labels
|
int
|
Number of labels for encoding. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: one-hot encoded numpy array of shape (*arr.shape, n_labels). |
Source code in src/nnunet_serve/seg_writers.py
process_name(name)
¶
random_color_generator()
¶
Returns a random color as a tuple of RGB values.
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
tuple of RGB values. |
Source code in src/nnunet_serve/seg_writers.py
save_mask_as_rtstruct(img_data, dcm_reference_file, output_path, segment_info)
¶
Converts a numpy array to an RT (radiotherapy) struct object. Could be a
multi-class object (each n > 0 corresponds to a class). The number of
classes corresponds to np.unique(img_data).shape[0] - 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img_data
|
ndarray
|
numpy array with n non-zero unique values, each of which corresponds to a class. |
required |
dcm_reference_file
|
str
|
reference DICOM files. |
required |
output_path
|
str
|
output file for RT struct file. |
required |
segment_info
|
tuple[str, list[int]]
|
segment information. Should be a list with size equal to the number of classes, and each element should be a tuple whose first element is the segment description and the second element a list of RGB values. |
required |
Source code in src/nnunet_serve/seg_writers.py
strip_laterality(name)
¶
Strips laterality indicators (left/right) from a string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The string to process. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The string without laterality indicators. |