All files / packages/streaming-image-volume-loader/src/helpers splitImageIdsBy4DTags.ts

0.99% Statements 1/101
0% Branches 0/34
0% Functions 0/22
1.06% Lines 1/94

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                                      1x                                                                                                                                                                                                                                                                                                                                                                                                                                    
import { metaData } from '@cornerstonejs/core';
 
// TODO: Test remaining implemented tags
// Supported 4D Tags
//   (0018,1060) Trigger Time                   [Implemented, not tested]
//   (0018,0081) Echo Time                      [Implemented, not tested]
//   (0018,0086) Echo Number                    [Implemented, not tested]
//   (0020,0100) Temporal Position Identifier   [OK]
//   (0054,1300) FrameReferenceTime             [OK]
//   (0018,9087) Diffusion B Value              [OK]
//   (2001,1003) Philips Diffusion B-factor     [OK]
//   (0019,100c) Siemens Diffusion B Value      [Implemented, not tested]
//   (0043,1039) GE Diffusion B Value           [OK]
 
interface MappedIPP {
  imageId: string;
  imagePositionPatient;
}
 
const groupBy = (array, key) => {
  return array.reduce((rv, x) => {
    (rv[x[key]] = rv[x[key]] || []).push(x);
    return rv;
  }, {});
};
 
function getIPPGroups(imageIds: string[]): { [id: string]: Array<MappedIPP> } {
  const ippMetadata: Array<MappedIPP> = imageIds.map((imageId) => {
    const { imagePositionPatient } = metaData.get('imagePlaneModule', imageId);
    return { imageId, imagePositionPatient };
  });
 
  if (!ippMetadata.every((item) => item.imagePositionPatient)) {
    // Fail if any instances don't provide a position
    return null;
  }
 
  const positionGroups = groupBy(ippMetadata, 'imagePositionPatient');
  const positions = Object.keys(positionGroups);
  const frame_count = positionGroups[positions[0]].length;
  if (frame_count === 1) {
    // Single frame indicates 3D volume
    return null;
  }
  const frame_count_equal = positions.every(
    (k) => positionGroups[k].length === frame_count
  );
  if (!frame_count_equal) {
    // Differences in number of frames per position group --> not a valid MV
    return null;
  }
  return positionGroups;
}
 
function test4DTag(
  IPPGroups: { [id: string]: Array<MappedIPP> },
  value_getter: (imageId: string) => number
) {
  const frame_groups = {};
  let first_frame_value_set: number[] = [];
 
  const positions = Object.keys(IPPGroups);
  for (let i = 0; i < positions.length; i++) {
    const frame_value_set: Set<number> = new Set<number>();
    const frames = IPPGroups[positions[i]];
 
    for (let j = 0; j < frames.length; j++) {
      const frame_value = value_getter(frames[j].imageId) || 0;
 
      frame_groups[frame_value] = frame_groups[frame_value] || [];
      frame_groups[frame_value].push({ imageId: frames[j].imageId });
 
      frame_value_set.add(frame_value);
      if (frame_value_set.size - 1 < j) {
        return undefined;
      }
    }
 
    if (i == 0) {
      first_frame_value_set = Array.from(frame_value_set);
    } else if (!setEquals(first_frame_value_set, frame_value_set)) {
      return undefined;
    }
  }
  return frame_groups;
}
 
function getTagValue(imageId: string, tag: string): number {
  const value = metaData.get(tag, imageId);
  try {
    return parseFloat(value);
  } catch {
    return undefined;
  }
}
 
function getPhilipsPrivateBValue(imageId: string) {
  // Philips Private Diffusion B-factor tag (2001, 1003)
  // Private creator: Philips Imaging DD 001, VR=FL, VM=1
  const value = metaData.get('20011003', imageId);
  try {
    const { InlineBinary } = value;
    if (InlineBinary) {
      const value_bytes = atob(InlineBinary);
      const ary_buf = new ArrayBuffer(value_bytes.length);
      const dv = new DataView(ary_buf);
      for (let i = 0; i < value_bytes.length; i++) {
        dv.setUint8(i, value_bytes.charCodeAt(i));
      }
      //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      // For WebGL Buffers, can skip Float32Array,
      // just return ArrayBuffer is all that's needed.
      return new Float32Array(ary_buf)[0];
    }
 
    return parseFloat(value);
  } catch {
    return undefined;
  }
}
 
function getSiemensPrivateBValue(imageId: string) {
  // Siemens Private Diffusion B-factor tag (0019, 100c)
  // Private creator: SIEMENS MR HEADER, VR=IS, VM=1
  let value = metaData.get('0019100c', imageId);
 
  try {
    const { InlineBinary } = value;
    if (InlineBinary) {
      value = atob(InlineBinary);
    }
    return parseFloat(value);
  } catch {
    return undefined;
  }
}
 
function getGEPrivateBValue(imageId: string) {
  // GE Private Diffusion B-factor tag (0043, 1039)
  // Private creator: GEMS_PARM_01, VR=IS, VM=4
  let value = metaData.get('00431039', imageId);
 
  try {
    const { InlineBinary } = value;
    if (InlineBinary) {
      value = atob(InlineBinary).split('//');
    }
    return parseFloat(value[0]) % 100000;
  } catch {
    return undefined;
  }
}
 
function setEquals(set_a: number[], set_b: Set<number>): boolean {
  if (set_a.length != set_b.size) {
    return false;
  }
  for (let i = 0; i < set_a.length; i++) {
    if (!set_b.has(set_a[i])) {
      return false;
    }
  }
  return true;
}
 
function getPetFrameReferenceTime(imageId) {
  const moduleInfo = metaData.get('petImageModule', imageId);
  return moduleInfo ? moduleInfo['frameReferenceTime'] : 0;
}
 
/**
 * Split the imageIds array by 4D tags into groups. Each group must have the
 * same number of imageIds or the same imageIds array passed in is returned.
 * @param imageIds - array of imageIds
 * @returns imageIds grouped by 4D tags
 */
function splitImageIdsBy4DTags(imageIds: string[]): {
  imageIdsGroups: string[][];
  splittingTag: string | null;
} {
  const positionGroups = getIPPGroups(imageIds);
  if (!positionGroups) {
    // When no position groups are found, return the original array wrapped and indicate no tag was used
    return { imageIdsGroups: [imageIds], splittingTag: null };
  }
 
  const tags = [
    'TemporalPositionIdentifier',
    'DiffusionBValue',
    'TriggerTime',
    'EchoTime',
    'EchoNumber',
    'PhilipsPrivateBValue',
    'SiemensPrivateBValue',
    'GEPrivateBValue',
    'PetFrameReferenceTime',
  ];
 
  const fncList2 = [
    (imageId) => getTagValue(imageId, tags[0]),
    (imageId) => getTagValue(imageId, tags[1]),
    (imageId) => getTagValue(imageId, tags[2]),
    (imageId) => getTagValue(imageId, tags[3]),
    (imageId) => getTagValue(imageId, tags[4]),
    getPhilipsPrivateBValue,
    getSiemensPrivateBValue,
    getGEPrivateBValue,
    getPetFrameReferenceTime,
  ];
 
  for (let i = 0; i < fncList2.length; i++) {
    const frame_groups = test4DTag(positionGroups, fncList2[i]);
    if (frame_groups) {
      const sortedKeys = Object.keys(frame_groups)
        .map(Number.parseFloat)
        .sort((a, b) => a - b);
 
      const imageIdsGroups = sortedKeys.map((key) =>
        frame_groups[key].map((item) => item.imageId)
      );
      return { imageIdsGroups, splittingTag: tags[i] };
    }
  }
 
  // Return the same imagesIds for non-4D volumes and indicate no tag was used
  return { imageIdsGroups: [imageIds], splittingTag: null };
}
 
export default splitImageIdsBy4DTags;