All files / core/src/utilities calculateNeighborhoodStats.ts

0% Statements 0/33
0% Branches 0/18
0% Functions 0/1
0% Lines 0/30

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                                                                                                                                         
import type { PixelDataTypedArray, Point3 } from '../types';
 
type NeighborhoodStats = {
  mean: number;
  stdDev: number;
  count: number;
};
/**
 * Calculates statistical properties (mean, standard deviation) of a neighborhood around a center point in a 3D volume.
 * @param scalarData - The array containing voxel intensity values
 * @param dimensions - The dimensions of the volume [width, height, depth]
 * @param centerIjk - The center point coordinates in IJK space
 * @param radius - The radius of the neighborhood to analyze in pixels
 * @returns An object containing the mean, standard deviation, and count of voxels in the neighborhood
 */
export function calculateNeighborhoodStats(
  scalarData: PixelDataTypedArray,
  dimensions: Point3,
  centerIjk: Point3,
  radius: number
): NeighborhoodStats {
  const [width, height, numSlices] = dimensions;
  const numPixelsPerSlice = width * height;
 
  let sum = 0;
  let sumSq = 0;
  let count = 0;
 
  const [cx, cy, cz] = centerIjk.map(Math.round);
 
  for (let z = cz - radius; z <= cz + radius; z++) {
    if (z < 0 || z >= numSlices) {
      continue;
    }
    for (let y = cy - radius; y <= cy + radius; y++) {
      if (y < 0 || y >= height) {
        continue;
      }
      for (let x = cx - radius; x <= cx + radius; x++) {
        if (x < 0 || x >= width) {
          continue;
        }
 
        const index = z * numPixelsPerSlice + y * width + x;
        const value = scalarData[index];
        sum += value;
        sumSq += value * value;
        count++;
      }
    }
  }
 
  if (count === 0) {
    const centerIndex = cz * numPixelsPerSlice + cy * width + cx;
    if (centerIndex >= 0 && centerIndex < scalarData.length) {
      const centerValue = scalarData[centerIndex];
      return { mean: centerValue, stdDev: 0, count: 1 };
    } else {
      return { mean: 0, stdDev: 0, count: 0 }; // Or throw error
    }
  }
 
  const mean = sum / count;
  const variance = sumSq / count - mean * mean;
  const stdDev = Math.sqrt(Math.max(0, variance)); // Ensure non-negative variance
 
  return { mean, stdDev, count };
}