All files / extensions/cornerstone-dicom-seg/src/utils labelmapLayerSegmentIndices.ts

73.68% Statements 14/19
50% Branches 2/4
75% Functions 3/4
72.22% Lines 13/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                              156x 156x 40894464x 40894464x 19366x     156x     156x 3x 3x                                   3x     3x 3x     3x    
/** The map from a label value in the voxels of a labelmap layer to the segment index. */
export type LabelToSegmentIndex = { [labelValue: number]: number };
 
/**
 * Gets the label values of one labelmap slice that the SEG export includes, in
 * one pass. The result is the `segmentsOnLabelmap` of a labelmap2D, which the
 * SEG adapter reads as label values.
 *
 * cs3D stores a segment in a private overlap layer under a label value that is
 * not its segment index (usually 1), and keeps the map in `labelToSegmentIndex`
 * of each layer. A label value that has no entry in that map belongs to no
 * segment: it is not returned, and it is added to `unboundLabels`. Without a
 * map, every label value is its own segment index.
 *
 * @param pixelData - The voxel values of the labelmap slice.
 * @param labelToSegmentIndex - The label map of the layer, or undefined.
 * @param unboundLabels - Gets each label value that has no entry in the map.
 */
export function getExportedLabelValues(
  pixelData,
  labelToSegmentIndex?: LabelToSegmentIndex,
  unboundLabels?: Set<number>
): number[] {
  const labelValues = new Set<number>();
  for (let i = 0; i < pixelData.length; i++) {
    const labelValue = pixelData[i];
    if (labelValue !== 0) {
      labelValues.add(labelValue);
    }
  }
  Iif (!labelToSegmentIndex) {
    return Array.from(labelValues);
  }
  return Array.from(labelValues).filter(labelValue => {
    if (labelToSegmentIndex[labelValue]) {
      return true;
    }
    unboundLabels?.add(labelValue);
    return false;
  });
}
 
/**
 * Gets the `labelToSegmentIndex` of a labelmap3D for the SEG adapter.
 *
 * The adapter reads a label value that is not in the map as its own segment
 * index. The export must not include a label value that has no segment, so the
 * map gives segment index 0 (no segment) to each such label value.
 */
export function getExportLabelToSegmentIndex(
  labelToSegmentIndex: LabelToSegmentIndex | undefined,
  unboundLabels: Set<number>
): LabelToSegmentIndex | undefined {
  Iif (!labelToSegmentIndex) {
    return undefined;
  }
  const exportMap = { ...labelToSegmentIndex };
  unboundLabels.forEach(labelValue => {
    exportMap[labelValue] = 0;
  });
  return exportMap;
}