Converter Script Fixes#

Issues Fixed#

The converter script had several issues that could cause empty label NIFTI files:

1. Data Type Mismatch#

  • Problem: The original code created all_organ as np.uint8 but the input segmentation might have different data types (e.g., np.int16, np.int32).

  • Fix: Now uses the same data type as the input segmentation for the all_organ array.

2. In-place Operations#

  • Problem: The original code used orig_seg directly in comparisons, which could cause issues if orig_seg was modified in-place.

  • Fix: Now converts to a numpy array (orig_seg_array) and uses explicit masks for operations.

3. Missing Debugging Information#

  • Problem: No way to understand why the conversion failed or what label values were present.

  • Fix: Added comprehensive debugging output including:

    • Original segmentation properties (shape, data type, value range)

    • Label analysis comparing expected vs found labels

    • Voxel counts for each expected label

    • Final result validation

4. No Input Validation#

  • Problem: No checks for empty or corrupted input files.

  • Fix: Added input file validation and warnings for empty segmentations.

New Features#

1. Label Analysis Function#

1analyze_label_values(segmentation_array)

This function provides detailed analysis of label values in your segmentation:

  • Lists all unique label values found

  • Compares with expected label values from the LABELS dictionary

  • Identifies missing and unexpected labels

  • Counts voxels for each expected label

2. Label Remapping Function#

1remap_labels(segmentation_array, label_mapping)

This function helps remap label values if they don’t match the expected ones:

1# Example: if your file uses labels 1,2,3 but expects 115,3,4
2mapping = {1: 115, 2: 3, 3: 4}
3remapped_array = remap_labels(original_array, mapping)

3. Debug Script#

python debug_labels.py /path/to/your/label.nii.gz

This script provides comprehensive analysis of your label file using both SimpleITK and MONAI.

How to Use#

1. Debug Your Label File#

First, analyze your label file to understand what’s in it:

python utils/debug_labels.py /path/to/your/label.nii.gz

2. Check the Output#

The debug script will show you:

  • File properties (size, spacing, origin)

  • Data type and value range

  • All unique label values

  • Comparison with expected labels

  • Voxel counts for each expected label

3. Identify Issues#

Common issues and solutions:

Issue: “No labels were found in the segmentation!”

  • Cause: Label values don’t match expected values

  • Solution: Use the remap_labels function or check your label file

Issue: “Missing expected labels”

  • Cause: Your segmentation doesn’t contain all expected organs

  • Solution: This is normal if your dataset doesn’t have all organs

Issue: “Unexpected labels found”

  • Cause: Your segmentation has labels not in the expected set

  • Solution: Either ignore them or add them to the LABELS dictionary

4. Run the Converter#

After understanding your data, run the converter:

python utils/converter.py /path/to/your/label.nii.gz

The converter will now provide detailed output showing:

  • What labels were found

  • How many voxels were processed for each label

  • Whether the final result is valid

Expected Label Values#

The converter expects these label values (defined in LABELS dictionary):

  • Liver: 1

  • Spleen: 3

  • Pancreas: 4

  • Heart: 115

  • Body: 200

  • Gallbladder: 10

  • Stomach: 12

  • Small_bowel: 19

  • Colon: 62

  • Kidney: Right=5, Left=14

  • Veins: Various values (6, 7, 17, 58, 59, 60, 61, 119, 123, 124, 125, 109, 110, 111, 112, 113)

  • Lungs: Various lobes (28, 29, 30, 31, 32)

  • Spine: Various vertebrae (131, 33-56, 97, 127)

  • Ribs: Various ribs (63-86, 114, 122)

  • Shoulders: Scapula and clavicle (89-92)

  • Hips: Left=95, Right=96

  • Back_muscles: Various muscles (98-107)

If your labels don’t match these values, you’ll need to remap them or modify the LABELS dictionary.