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

88% Statements 22/25
62.5% Branches 5/8
100% Functions 3/3
88% Lines 22/25

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 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                                                        8x   8x   8x 8x     8x   8x       8x         8x           8x 8x     8x         8x         8x   8x 8x   8x               8x   8x               8x                     8x                 8x   8x        
import { utilities, getWebWorkerManager } from '@cornerstonejs/core';
import { triggerWorkerProgress } from './utilsForWorker';
import { WorkerTypes } from '../../enums';
import type {
  LabelmapSegmentationDataStack,
  NamedStatistics,
} from '../../types';
import { registerComputeWorker } from '../registerComputeWorker';
import createMergedLabelmapForIndex from './createMergedLabelmapForIndex';
import { getSegmentation } from '../../stateManagement/segmentation/getSegmentation';
import getOrCreateSegmentationVolume from './getOrCreateSegmentationVolume';
import { getReferenceVolumeForSegmentation } from './getReferenceVolumeForSegmentation';
 
/**
 * Get the TMTV for a segmentation.
 *
 * @param segmentationIds - The segmentation IDs. If there are multiple, a merged
 * labelmap will get created first for that segmentIndex
 * @param segmentIndex - The segment index to get TMTV for.
 * @returns The TMTV.
 */
async function computeMetabolicStats({
  segmentationIds,
  segmentIndex,
}: {
  segmentationIds: string[];
  segmentIndex: number;
}): Promise<NamedStatistics | { [segmentIndex: number]: NamedStatistics }> {
  registerComputeWorker();
 
  triggerWorkerProgress(WorkerTypes.COMPUTE_STATISTICS, 0);
 
  const segmentation = getSegmentation(segmentationIds[0]);
  const { imageIds: segImageIds } = segmentation.representationData
    .Labelmap as LabelmapSegmentationDataStack;
 
  const isValidVolume = utilities.isValidVolume(segImageIds);
 
  Iif (!isValidVolume) {
    throw new Error('Invalid volume - TMTV cannot be calculated');
  }
 
  const stats = await calculateForVolume({
    segmentationIds,
    segmentIndex,
  });
 
  return stats;
}
 
async function calculateForVolume({ segmentationIds, segmentIndex }) {
  // create volume from segmentationIds
 
  const labelmapVolumes = segmentationIds.map((id) => {
    return getOrCreateSegmentationVolume(id);
  });
 
  const mergedLabelmap = createMergedLabelmapForIndex(
    labelmapVolumes,
    segmentIndex
  );
 
  Iif (!mergedLabelmap) {
    throw new Error('Invalid volume - TMTV cannot be calculated');
  }
 
  const { imageData, dimensions, direction, origin, voxelManager } =
    mergedLabelmap;
 
  const spacing = imageData.getSpacing();
  const segmentationScalarData = voxelManager.getCompleteScalarDataArray();
 
  const segmentationInfo = {
    scalarData: segmentationScalarData,
    dimensions,
    spacing,
    origin,
    direction,
  };
 
  const referenceVolume = getReferenceVolumeForSegmentation(segmentationIds[0]);
 
  const imageInfo = {
    dimensions: referenceVolume.dimensions,
    spacing: referenceVolume.spacing,
    origin: referenceVolume.origin,
    direction: referenceVolume.direction,
    scalarData: referenceVolume.voxelManager.getCompleteScalarDataArray(),
  };
 
  Iif (
    imageInfo.scalarData.length === 0 ||
    segmentationInfo.scalarData.length === 0
  ) {
    return {
      [segmentIndex]: {
        name: 'TMTV',
        value: 0,
      },
    };
  }
  const stats = await getWebWorkerManager().executeTask(
    'compute',
    'computeMetabolicStats',
    {
      segmentationInfo,
      imageInfo,
    }
  );
 
  triggerWorkerProgress(WorkerTypes.COMPUTE_STATISTICS, 100);
 
  return stats;
}
 
export { computeMetabolicStats };