All files / extensions/cornerstone-dynamic-volume/src/actions updateSegmentationsChartDisplaySet.ts

0% Statements 0/107
0% Branches 0/34
0% Functions 0/15
0% Lines 0/100

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 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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
import { DicomMetadataStore, utils } from '@ohif/core';
 
import * as cs from '@cornerstonejs/core';
import * as csTools from '@cornerstonejs/tools';
 
const CHART_MODALITY = 'CHT';
const SEG_CHART_INSTANCE_UID = utils.guid();
 
// Private SOPClassUid for chart data
const ChartDataSOPClassUid = '1.9.451.13215.7.3.2.7.6.1';
 
const { utilities: csToolsUtils } = csTools;
 
function _getDateTimeStr() {
  const now = new Date();
  const date =
    now.getFullYear() + ('0' + now.getUTCMonth()).slice(-2) + ('0' + now.getUTCDate()).slice(-2);
  const time =
    ('0' + now.getUTCHours()).slice(-2) +
    ('0' + now.getUTCMinutes()).slice(-2) +
    ('0' + now.getUTCSeconds()).slice(-2);
 
  return { date, time };
}
 
function _getTimePointsDataByTagName(volume, timePointsTag) {
  const uniqueTimePoints = volume.imageIds.reduce((timePoints, imageId) => {
    const instance = DicomMetadataStore.getInstanceByImageId(imageId);
    const timePointValue = instance[timePointsTag];
 
    if (timePointValue !== undefined) {
      timePoints.add(timePointValue);
    }
 
    return timePoints;
  }, new Set());
 
  return Array.from(uniqueTimePoints).sort((a: number, b: number) => a - b);
}
 
function _convertTimePointsUnit(timePoints, timePointsUnit) {
  const validUnits = ['ms', 's', 'm', 'h'];
  const divisors = [1000, 60, 60];
  const currentUnitIndex = validUnits.indexOf(timePointsUnit);
  let divisor = 1;
 
  if (currentUnitIndex !== -1) {
    for (let i = currentUnitIndex; i < validUnits.length - 1; i++) {
      const newDivisor = divisor * divisors[i];
      const greaterThanDivisorCount = timePoints.filter(timePoint => timePoint > newDivisor).length;
 
      // Change the scale only if more than 50% of the time points are
      // greater than the new divisor.
      if (greaterThanDivisorCount <= timePoints.length / 2) {
        break;
      }
 
      divisor = newDivisor;
      timePointsUnit = validUnits[i + 1];
    }
 
    if (divisor > 1) {
      timePoints = timePoints.map(timePoint => timePoint / divisor);
    }
  }
 
  return { timePoints, timePointsUnit };
}
 
// It currently supports only one tag but a few other will be added soon
// Supported 4D Tags
//   (0018,1060) Trigger Time                   [NOK]
//   (0018,0081) Echo Time                      [NOK]
//   (0018,0086) Echo Number                    [NOK]
//   (0020,0100) Temporal Position Identifier   [NOK]
//   (0054,1300) FrameReferenceTime             [OK]
function _getTimePointsData(volume) {
  const timePointsTags = {
    FrameReferenceTime: {
      unit: 'ms',
    },
  };
 
  const timePointsTagNames = Object.keys(timePointsTags);
  let timePoints;
  let timePointsUnit;
 
  for (let i = 0; i < timePointsTagNames.length; i++) {
    const tagName = timePointsTagNames[i];
    const curTimePoints = _getTimePointsDataByTagName(volume, tagName);
 
    if (curTimePoints.length) {
      timePoints = curTimePoints;
      timePointsUnit = timePointsTags[tagName].unit;
      break;
    }
  }
 
  if (!timePoints.length) {
    const concatTagNames = timePointsTagNames.join(', ');
 
    throw new Error(`Could not extract time points data for the following tags: ${concatTagNames}`);
  }
 
  const convertedTimePoints = _convertTimePointsUnit(timePoints, timePointsUnit);
 
  timePoints = convertedTimePoints.timePoints;
  timePointsUnit = convertedTimePoints.timePointsUnit;
 
  return { timePoints, timePointsUnit };
}
 
function _getSegmentationData(
  segmentation,
  volumesTimePointsCache,
  { servicesManager }: { servicesManager: AppTypes.ServicesManager }
) {
  const { displaySetService, segmentationService, viewportGridService } = servicesManager.services;
  const displaySets = displaySetService.getActiveDisplaySets();
 
  const dynamic4DDisplaySet = displaySets.find(displaySet => {
    const anInstance = displaySet.instances?.[0];
 
    if (anInstance) {
      return (
        anInstance.FrameReferenceTime !== undefined || anInstance.NumberOfTimeSlices !== undefined
      );
    }
 
    return false;
  });
 
  // const referencedDynamicVolume = cs.cache.getVolume(dynamic4DDisplaySet.displaySetInstanceUID);
  let volumeCacheKey: string | undefined;
  const volumeId = dynamic4DDisplaySet.displaySetInstanceUID;
 
  for (const [key] of cs.cache._volumeCache) {
    if (key.includes(volumeId)) {
      volumeCacheKey = key;
      break;
    }
  }
 
  let referencedDynamicVolume;
  if (volumeCacheKey) {
    referencedDynamicVolume = cs.cache.getVolume(volumeCacheKey);
  }
 
  const { StudyInstanceUID, StudyDescription } = DicomMetadataStore.getInstanceByImageId(
    referencedDynamicVolume.imageIds[0]
  );
 
  const segmentationVolume = segmentationService.getLabelmapVolume(segmentation.segmentationId);
  const maskVolumeId = segmentationVolume?.volumeId;
 
  const [timeData, _] = csToolsUtils.dynamicVolume.getDataInTime(referencedDynamicVolume, {
    maskVolumeId,
  }) as number[][];
 
  const pixelCount = timeData.length;
 
  if (pixelCount === 0) {
    return [];
  }
 
  // Todo: this is useless we should be able to grab color with just segRepUID and segmentIndex
  // const color = csTools.segmentation.config.color.getSegmentIndexColor(
  //   segmentationRepresentationUID,
  //   1 // segmentIndex
  // );
  const viewportId = viewportGridService.getActiveViewportId();
  const color = segmentationService.getSegmentColor(viewportId, segmentation.segmentationId, 1);
 
  const hexColor = cs.utilities.color.rgbToHex(color[0], color[1], color[2]);
  let timePointsData = volumesTimePointsCache.get(referencedDynamicVolume);
 
  if (!timePointsData) {
    timePointsData = _getTimePointsData(referencedDynamicVolume);
    volumesTimePointsCache.set(referencedDynamicVolume, timePointsData);
  }
 
  const { timePoints, timePointsUnit } = timePointsData;
 
  if (timePoints.length !== timeData[0].length) {
    throw new Error('Invalid number of time points returned');
  }
 
  const timepointsCount = timePoints.length;
  const chartSeriesData = new Array(timepointsCount);
 
  for (let i = 0; i < timepointsCount; i++) {
    const average = timeData.reduce((acc, cur) => acc + cur[i] / pixelCount, 0);
 
    chartSeriesData[i] = [timePoints[i], average];
  }
 
  return {
    StudyInstanceUID,
    StudyDescription,
    chartData: {
      series: {
        label: segmentation.label,
        points: chartSeriesData,
        color: hexColor,
      },
      axis: {
        x: {
          label: `Time (${timePointsUnit})`,
        },
        y: {
          label: `Vl (Bq/ml)`,
        },
      },
    },
  };
}
 
function _getInstanceFromSegmentations(segmentations, { servicesManager }) {
  if (!segmentations.length) {
    return;
  }
 
  const volumesTimePointsCache = new WeakMap();
  const segmentationsData = segmentations.map(segmentation =>
    _getSegmentationData(segmentation, volumesTimePointsCache, { servicesManager })
  );
 
  const { date: seriesDate, time: seriesTime } = _getDateTimeStr();
  const series = segmentationsData.reduce((allSeries, curSegData) => {
    return [...allSeries, curSegData.chartData.series];
  }, []);
 
  const instance = {
    SOPClassUID: ChartDataSOPClassUid,
    Modality: CHART_MODALITY,
    SOPInstanceUID: utils.guid(),
    SeriesDate: seriesDate,
    SeriesTime: seriesTime,
    SeriesInstanceUID: SEG_CHART_INSTANCE_UID,
    StudyInstanceUID: segmentationsData[0].StudyInstanceUID,
    StudyDescription: segmentationsData[0].StudyDescription,
    SeriesNumber: 100,
    SeriesDescription: 'Segmentation chart series data',
    chartData: {
      series,
      axis: { ...segmentationsData[0].chartData.axis },
    },
  };
 
  const seriesMetadata = {
    StudyInstanceUID: instance.StudyInstanceUID,
    StudyDescription: instance.StudyDescription,
    SeriesInstanceUID: instance.SeriesInstanceUID,
    SeriesDescription: instance.SeriesDescription,
    SeriesNumber: instance.SeriesNumber,
    SeriesTime: instance.SeriesTime,
    SOPClassUID: instance.SOPClassUID,
    Modality: instance.Modality,
  };
 
  return { seriesMetadata, instance };
}
 
function updateSegmentationsChartDisplaySet({ servicesManager }: withAppTypes): void {
  const { segmentationService } = servicesManager.services;
  const segmentations = segmentationService.getSegmentations();
  const { seriesMetadata, instance } =
    _getInstanceFromSegmentations(segmentations, { servicesManager }) ?? {};
 
  if (seriesMetadata && instance) {
    // An event is triggered after adding the instance and the displaySet is created
    DicomMetadataStore.addSeriesMetadata([seriesMetadata], true);
    DicomMetadataStore.addInstances([instance], true);
  }
}
 
export { updateSegmentationsChartDisplaySet as default };