All files / tools/src/utilities/segmentation validateLabelmap.ts

0% Statements 0/15
0% Branches 0/10
0% Functions 0/3
0% Lines 0/15

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 64 65 66 67 68 69 70 71 72                                                                                                                                               
import { cache } from '@cornerstonejs/core';
import type { SegmentationPublicInput } from '../../types/SegmentationStateTypes';
import type {
  LabelmapSegmentationData,
  LabelmapSegmentationDataStack,
  LabelmapSegmentationDataVolume,
} from '../../types/LabelmapTypes';
 
function validateRepresentationData(
  segmentationRepresentationData: LabelmapSegmentationData
): void {
  if ('volumeId' in segmentationRepresentationData) {
    segmentationRepresentationData =
      segmentationRepresentationData as LabelmapSegmentationDataVolume;
 
    const cachedVolume = cache.getVolume(
      segmentationRepresentationData.volumeId
    );
 
    if (!cachedVolume) {
      throw new Error(
        `volumeId of ${segmentationRepresentationData.volumeId} not found in cache, you should load and cache volume before adding segmentation`
      );
    }
  } else if ('imageIds' in segmentationRepresentationData) {
    segmentationRepresentationData =
      segmentationRepresentationData as LabelmapSegmentationDataStack;
 
    if (!segmentationRepresentationData.imageIds) {
      throw new Error(
        'The segmentationInput.representationData.imageIds is undefined, please provide a valid representationData.imageIds for stack data'
      );
    }
  } else {
    throw new Error(
      'The segmentationInput.representationData is undefined, please provide a valid representationData'
    );
  }
}
 
/**
 * Validates the public segmentation input.
 * Throws an error if the segmentation input is invalid.
 *
 * @param segmentationInput - The segmentation input to validate.
 */
export function validatePublic(
  segmentationInput: SegmentationPublicInput
): void {
  if (!segmentationInput.representation.data) {
    throw new Error(
      'The segmentationInput.representationData.data is undefined, please provide a valid representationData.data'
    );
  }
 
  const representationData = segmentationInput.representation
    .data as LabelmapSegmentationData;
 
  validateRepresentationData(representationData);
}
 
/**
 * Validates the given segmentation representation data.
 *
 * @param segmentationRepresentationData The segmentation representation data to validate.
 */
export function validate(
  segmentationRepresentationData: LabelmapSegmentationData
) {
  validateRepresentationData(segmentationRepresentationData);
}