All files / extensions/dicom-microscopy/src DicomMicroscopyANNSopClassHandler.js

0% Statements 0/31
0% Branches 0/4
0% Functions 0/8
0% Lines 0/31

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                                                                                                                                                                                                                                   
import OHIF, { DicomMetadataStore } from '@ohif/core';
import loadAnnotation from './utils/loadAnnotation';
import getSourceDisplaySet from './utils/getSourceDisplaySet';
 
const { utils } = OHIF;
 
const SOP_CLASS_UIDS = {
  MICROSCOPY_BULK_SIMPLE_ANNOTATION: '1.2.840.10008.5.1.4.1.1.91.1',
};
 
const SOPClassHandlerId =
  '@ohif/extension-dicom-microscopy.sopClassHandlerModule.DicomMicroscopyANNSopClassHandler';
 
function _getDisplaySetsFromSeries(instances, servicesManager, extensionManager) {
  // If the series has no instances, stop here
  if (!instances || !instances.length) {
    throw new Error('No instances were provided');
  }
 
  const { displaySetService, microscopyService } = servicesManager.services;
 
  // Sort instances by date/time in ascending order (oldest first)
  const sortedInstances = [...instances].sort((a, b) => {
    const dateA = `${a.ContentDate}${a.ContentTime}`;
    const dateB = `${b.ContentDate}${b.ContentTime}`;
    return dateA.localeCompare(dateB);
  });
 
  // Get the most recent instance (last in the sorted array)
  const instance = sortedInstances[sortedInstances.length - 1];
 
  const naturalizedDataset = DicomMetadataStore.getSeries(
    instance.StudyInstanceUID,
    instance.SeriesInstanceUID
  ).instances[0];
 
  const {
    SeriesDescription,
    ContentDate,
    ContentTime,
    SeriesNumber,
    StudyInstanceUID,
    SeriesInstanceUID,
    SOPInstanceUID,
    SOPClassUID,
  } = instance;
 
  const displaySet = {
    isOverlayDisplaySet: true,
    plugin: 'microscopy',
    Modality: 'ANN',
    thumbnailSrc: null,
    altImageText: 'Microscopy Annotation',
    displaySetInstanceUID: utils.uuidv4(),
    SOPInstanceUID,
    SeriesInstanceUID,
    StudyInstanceUID,
    SOPClassHandlerId,
    SOPClassUID,
    SeriesDescription,
    // Map the content date/time to the series date/time, these are only used for filtering.
    SeriesDate: ContentDate,
    SeriesTime: ContentTime,
    SeriesNumber,
    instance,
    metadata: naturalizedDataset,
    isDerived: true,
    isLoading: false,
    isLoaded: false,
    loadError: false,
  };
 
  displaySet.load = function () {
    return loadAnnotation({
      microscopyService,
      displaySet,
      extensionManager,
      servicesManager,
    }).catch(error => {
      displaySet.isLoaded = false;
      displaySet.loadError = true;
      throw new Error(error);
    });
  };
 
  displaySet.getSourceDisplaySet = function () {
    let allDisplaySets = [];
    const studyMetadata = DicomMetadataStore.getStudy(StudyInstanceUID);
    studyMetadata.series.forEach(series => {
      const displaySets = displaySetService.getDisplaySetsForSeries(series.SeriesInstanceUID);
      allDisplaySets = allDisplaySets.concat(displaySets);
    });
    const ds = getSourceDisplaySet(allDisplaySets, displaySet);
    return ds;
  };
 
  return [displaySet];
}
 
export default function getDicomMicroscopyANNSopClassHandler({
  servicesManager,
  extensionManager,
}) {
  const getDisplaySetsFromSeries = instances => {
    return _getDisplaySetsFromSeries(instances, servicesManager, extensionManager);
  };
 
  return {
    name: 'DicomMicroscopyANNSopClassHandler',
    sopClassUids: [SOP_CLASS_UIDS.MICROSCOPY_BULK_SIMPLE_ANNOTATION],
    getDisplaySetsFromSeries,
  };
}