All files / tools/src/utilities/math/basic BasicStatsCalculator.ts

83.33% Statements 85/102
60% Branches 45/75
78.94% Functions 15/19
82.65% Lines 81/98

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 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367          428x                                             756x                                                   11680x                             11680x     11680x 11680x     11680x 11680x 11680x 11680x     11680x 11680x     11680x       11680x 11680x 11680x 11680x     11680x 11680x     11680x           11680x     11680x   11680x 696x 696x 696x 696x       11680x 1424x 1424x 1424x 1424x               168x 8x       55028x 160x   160x     160x                 168x 168x 168x   504x     168x 168x 168x     168x       168x 168x 168x     168x       168x   168x                                                                                                                                                   168x                       168x 168x       168x 168x   168x 168x 168x 168x 168x 168x 168x 168x 168x 168x 168x 168x 168x 168x 168x   168x             428x     160x 160x   160x     428x                 11680x     428x     168x                                                                                              
import { utilities } from '@cornerstonejs/core';
import type { NamedStatistics } from '../../../types';
import { Calculator, InstanceCalculator } from './Calculator';
import type { Types } from '@cornerstonejs/core';
 
const { PointsManager } = utilities;
 
// Define an interface for the internal state used by the basic stats calculators
interface BasicStatsState {
  max: number[];
  min: number[];
  sum: number[];
  count: number;
  maxIJK: Types.Point3 | null;
  maxLPS: Types.Point3 | null;
  minIJK: Types.Point3 | null;
  minLPS: Types.Point3 | null;
  runMean: number[];
  m2: number[];
  m3: number[]; // For skewness calculation
  m4: number[]; // For kurtosis calculation
  allValues: number[][]; // Store all values for median calculation
  pointsInShape?: Types.IPointsManager<Types.Point3> | null;
  sumLPS: Types.Point3; // To calculate the center point
}
 
// Helper function to create a new state
function createBasicStatsState(storePointData: boolean): BasicStatsState {
  return {
    max: [-Infinity],
    min: [Infinity],
    sum: [0],
    count: 0,
    maxIJK: null,
    maxLPS: null,
    minIJK: null,
    minLPS: null,
    runMean: [0],
    m2: [0],
    m3: [0],
    m4: [0],
    allValues: [[]],
    pointsInShape: storePointData ? PointsManager.create3(1024) : null,
    sumLPS: [0, 0, 0],
  };
}
 
// Shared logic for updating stats from a callback
function basicStatsCallback(
  state: BasicStatsState,
  newValue: number | Types.RGB,
  pointLPS: Types.Point3 | null = null,
  pointIJK: Types.Point3 | null = null
): void {
  Iif (
    Array.isArray(newValue) &&
    newValue.length > 1 &&
    state.max.length === 1
  ) {
    state.max.push(state.max[0], state.max[0]);
    state.min.push(state.min[0], state.min[0]);
    state.sum.push(state.sum[0], state.sum[0]);
    state.runMean.push(0, 0);
    state.m2.push(state.m2[0], state.m2[0]);
    state.m3.push(state.m3[0], state.m3[0]);
    state.m4.push(state.m4[0], state.m4[0]);
    state.allValues.push([], []);
  }
 
  Iif (state?.pointsInShape && pointLPS) {
    state.pointsInShape.push(pointLPS);
  }
  const newArray = Array.isArray(newValue) ? newValue : [newValue];
  state.count += 1;
 
  // Accumulate LPS sum if pointLPS is provided
  Eif (pointLPS) {
    state.sumLPS[0] += pointLPS[0];
    state.sumLPS[1] += pointLPS[1];
    state.sumLPS[2] += pointLPS[2];
  }
 
  state.max.forEach((it, idx) => {
    const value = newArray[idx];
 
    // Store value for median calculation
    state.allValues[idx].push(value);
 
    // Calculate running statistics using Welford's online algorithm
    // extended for higher moments (skewness and kurtosis)
    const n = state.count;
    const delta = value - state.runMean[idx];
    const delta_n = delta / n;
    const term1 = delta * delta_n * (n - 1);
 
    // Update mean
    state.sum[idx] += value;
    state.runMean[idx] += delta_n;
 
    // For kurtosis, must be updated before m3 and m2
    state.m4[idx] +=
      term1 * delta_n * delta_n * (n * n - 3 * n + 3) +
      6 * delta_n * delta_n * state.m2[idx] -
      4 * delta_n * state.m3[idx];
 
    // For skewness, must be updated before m2
    state.m3[idx] += term1 * delta_n * (n - 2) - 3 * delta_n * state.m2[idx];
 
    // For variance
    state.m2[idx] += term1;
 
    if (value < state.min[idx]) {
      state.min[idx] = value;
      Eif (idx === 0) {
        state.minIJK = pointIJK ? [...pointIJK] : null;
        state.minLPS = pointLPS ? [...pointLPS] : null;
      }
    }
 
    if (value > state.max[idx]) {
      state.max[idx] = value;
      Eif (idx === 0) {
        state.maxIJK = pointIJK ? [...pointIJK] : null;
        state.maxLPS = pointLPS ? [...pointLPS] : null;
      }
    }
  });
}
 
// Helper function to calculate median
function calculateMedian(values: number[]): number {
  if (values.length === 0) {
    return 0;
  }
 
  // Sort values
  const sorted = [...values].sort((a, b) => a - b);
  const mid = Math.floor(sorted.length / 2);
 
  Iif (sorted.length % 2 === 0) {
    return (sorted[mid - 1] + sorted[mid]) / 2;
  } else {
    return sorted[mid];
  }
}
 
// Shared logic for computing the statistics
function basicGetStatistics(
  state: BasicStatsState,
  unit?: string
): NamedStatistics {
  const mean = state.sum.map((sum) => sum / state.count);
  const stdDev = state.m2.map((squaredDiffSum) =>
    Math.sqrt(squaredDiffSum / state.count)
  );
  const center = state.sumLPS.map((sum) => sum / state.count) as Types.Point3;
 
  // Calculate skewness
  const skewness = state.m3.map((m3, idx) => {
    const variance = state.m2[idx] / state.count;
    Iif (variance === 0) {
      return 0;
    }
    return m3 / (state.count * Math.pow(variance, 1.5));
  });
 
  // Calculate kurtosis (excess kurtosis: normal distribution would be 0)
  const kurtosis = state.m4.map((m4, idx) => {
    const variance = state.m2[idx] / state.count;
    Iif (variance === 0) {
      return 0;
    }
    return m4 / (state.count * variance * variance) - 3;
  });
 
  // Calculate median for each channel
  const median = state.allValues.map((values) => calculateMedian(values));
 
  const named: NamedStatistics = {
    max: {
      name: 'max',
      label: 'Max Pixel',
      value: state.max.length === 1 ? state.max[0] : state.max,
      unit,
      pointIJK: state.maxIJK ? [...state.maxIJK] : null,
      pointLPS: state.maxLPS ? [...state.maxLPS] : null,
    },
    min: {
      name: 'min',
      label: 'Min Pixel',
      value: state.min.length === 1 ? state.min[0] : state.min,
      unit,
      pointIJK: state.minIJK ? [...state.minIJK] : null,
      pointLPS: state.minLPS ? [...state.minLPS] : null,
    },
    mean: {
      name: 'mean',
      label: 'Mean Pixel',
      value: mean.length === 1 ? mean[0] : mean,
      unit,
    },
    stdDev: {
      name: 'stdDev',
      label: 'Standard Deviation',
      value: stdDev.length === 1 ? stdDev[0] : stdDev,
      unit,
    },
    count: {
      name: 'count',
      label: 'Voxel Count',
      value: state.count,
      unit: null,
    },
    median: {
      name: 'median',
      label: 'Median',
      value: median.length === 1 ? median[0] : median,
      unit,
    },
    skewness: {
      name: 'skewness',
      label: 'Skewness',
      value: skewness.length === 1 ? skewness[0] : skewness,
      unit: null,
    },
    kurtosis: {
      name: 'kurtosis',
      label: 'Kurtosis',
      value: kurtosis.length === 1 ? kurtosis[0] : kurtosis,
      unit: null,
    },
    maxLPS: {
      name: 'maxLPS',
      label: 'Max LPS',
      value: state.maxLPS ? Array.from(state.maxLPS) : null,
      unit: null,
    },
    minLPS: {
      name: 'minLPS',
      label: 'Min LPS',
      value: state.minLPS ? Array.from(state.minLPS) : null,
      unit: null,
    },
    pointsInShape: state.pointsInShape,
    center: {
      name: 'center',
      label: 'Center',
      value: center ? [...center] : null,
      unit: null,
    },
    array: [],
  };
  named.array.push(
    named.min,
    named.max,
    named.mean,
    named.stdDev,
    named.median,
    named.skewness,
    named.kurtosis,
    named.count,
    named.maxLPS,
    named.minLPS
  );
  Eif (named.center.value) {
    named.array.push(named.center);
  }
 
  // Reset state for next computation
  const store = state.pointsInShape !== null;
  const freshState = createBasicStatsState(store);
  // Copy fresh state into the provided state object
  state.max = freshState.max;
  state.min = freshState.min;
  state.sum = freshState.sum;
  state.count = freshState.count;
  state.maxIJK = freshState.maxIJK;
  state.maxLPS = freshState.maxLPS;
  state.minIJK = freshState.minIJK;
  state.minLPS = freshState.minLPS;
  state.runMean = freshState.runMean;
  state.m2 = freshState.m2;
  state.m3 = freshState.m3;
  state.m4 = freshState.m4;
  state.allValues = freshState.allValues;
  state.pointsInShape = freshState.pointsInShape;
  state.sumLPS = freshState.sumLPS; // Reset sumLPS
 
  return named;
}
 
/**
 * A static basic stats calculator that uses shared helper functions.
 */
export class BasicStatsCalculator extends Calculator {
  private static state: BasicStatsState = createBasicStatsState(true);
 
  public static statsInit(options: { storePointData: boolean }): void {
    Eif (!options.storePointData) {
      this.state.pointsInShape = null;
    }
    this.state = createBasicStatsState(options.storePointData);
  }
 
  public static statsCallback = ({
    value: newValue,
    pointLPS = null,
    pointIJK = null,
  }: {
    value: number | Types.RGB;
    pointLPS?: Types.Point3 | null;
    pointIJK?: Types.Point3 | null;
  }): void => {
    basicStatsCallback(this.state, newValue, pointLPS, pointIJK);
  };
 
  public static getStatistics = (options?: {
    unit: string;
  }): NamedStatistics => {
    return basicGetStatistics(this.state, options?.unit);
  };
}
 
/**
 * An instantiable version of BasicStatsCalculator that shares common logic with the static version.
 */
export class InstanceBasicStatsCalculator extends InstanceCalculator {
  private state: BasicStatsState;
 
  constructor(options: { storePointData: boolean }) {
    super(options);
    this.state = createBasicStatsState(options.storePointData);
  }
 
  /**
   * Resets the internal state.
   * @param options Object with storePointData flag.
   */
  statsInit(options: { storePointData: boolean }): void {
    this.state = createBasicStatsState(options.storePointData);
  }
 
  /**
   * Processes a new data point for statistics calculation.
   * @param data The data object containing value and optional points
   */
  statsCallback(data: {
    value: number | Types.RGB;
    pointLPS?: Types.Point3 | null;
    pointIJK?: Types.Point3 | null;
  }): void {
    basicStatsCallback(this.state, data.value, data.pointLPS, data.pointIJK);
  }
 
  /**
   * Computes and returns the statistics based on the accumulated data.
   * @param options Optional parameters including unit
   * @returns The computed statistics
   */
  getStatistics(options?: {
    unit: string;
    spacing?: number[] | number;
  }): NamedStatistics {
    return basicGetStatistics(this.state, options?.unit);
  }
}