Skip to content

Duplicate Code: Chunk Alignment Logic and Resolution Selection Structure #121

@github-actions

Description

@github-actions

Analysis of recent commits in fideus-labs/fidnii

Assignee: @copilot

Summary

Two significant code duplication patterns were found in the source files via semantic analysis. Both patterns exceed the 10-line threshold and represent maintainability risk.


Pattern 1: Chunk Alignment Logic Duplicated Across Files

Duplication Details

  • Severity: Medium
  • Occurrences: 2 nearly identical implementations
  • Locations:
    • fidnii/src/ResolutionSelector.ts (lines 166–200) — alignRegionToChunks
    • fidnii/src/ClipPlanes.ts (lines 500–546) — alignToChunks

Both functions perform the same chunk-boundary alignment math: aligning a PixelRegion's start down and end up to chunk boundaries, clamped to the volume shape. The core logic is ~30 lines of identical arithmetic.

alignRegionToChunks (ResolutionSelector.ts):

const alignedStart: [number, number, number] = [
  Math.floor(region.start[0] / chunkShape[0]) * chunkShape[0],
  Math.floor(region.start[1] / chunkShape[1]) * chunkShape[1],
  Math.floor(region.start[2] / chunkShape[2]) * chunkShape[2],
]
const alignedEnd: [number, number, number] = [
  Math.min(Math.ceil(region.end[0] / chunkShape[0]) * chunkShape[0], volumeShape[0]),
  Math.min(Math.ceil(region.end[1] / chunkShape[1]) * chunkShape[1], volumeShape[1]),
  Math.min(Math.ceil(region.end[2] / chunkShape[2]) * chunkShape[2], volumeShape[2]),
]
return { start: alignedStart, end: alignedEnd }

alignToChunks (ClipPlanes.ts) — identical math, different return shape:

const chunkAlignedStart: [number, number, number] = [
  Math.floor(region.start[0] / chunkShape[0]) * chunkShape[0],
  // ...same...
]
const chunkAlignedEnd: [number, number, number] = [
  Math.min(Math.ceil(region.end[0] / chunkShape[0]) * chunkShape[0], volumeShape[0]),
  // ...same...
]
const needsClipping = chunkAlignedStart[0] !== region.start[0] || /* ... */
return { start: region.start, end: region.end, chunkAlignedStart, chunkAlignedEnd, needsClipping }

ClipPlanes.ts already imports getChunkShape and getVolumeShape from ResolutionSelector.ts, so it could also call alignRegionToChunks directly.

Refactoring Recommendation

Have alignToChunks delegate to alignRegionToChunks:

export function alignToChunks(region: PixelRegion, ngffImage: NgffImage): ChunkAlignedRegion {
  const aligned = alignRegionToChunks(region, ngffImage)
  const needsClipping =
    aligned.start[0] !== region.start[0] || aligned.start[1] !== region.start[1] ||
    aligned.start[2] !== region.start[2] || aligned.end[0] !== region.end[0] ||
    aligned.end[1] !== region.end[1] || aligned.end[2] !== region.end[2]
  return {
    start: region.start, end: region.end,
    chunkAlignedStart: aligned.start, chunkAlignedEnd: aligned.end,
    needsClipping,
  }
}

Pattern 2: Resolution Selection Loop Duplicated Within Same File

Duplication Details

  • Severity: High
  • Occurrences: 2 structurally near-identical functions in the same file
  • Locations:
    • fidnii/src/ResolutionSelector.ts (lines 34–92) — selectResolution
    • fidnii/src/ResolutionSelector.ts (lines 268–339) — select2DResolution

Both functions share the same structure (~50 lines):

  1. Iterate resolution levels from finest to coarsest
  2. For each level: call clipPlanesToPixelRegionalignRegionToChunks → compute dimensions
  3. Check if pixel count ≤ maxPixels, return if so
  4. Fall back to lowest resolution with the same region/dimensions calculation

The only difference is how pixelCount is computed: dimensions[0]*dimensions[1]*dimensions[2] for 3D vs dimensions[inPlaneAxes[0]] * dimensions[inPlaneAxes[1]] * slabDepth for 2D slabs.

Duplicated fallback block (appears twice per function, 4× total):

const lowestImage = images[images.length - 1]
const region = clipPlanesToPixelRegion(clipPlanes, volumeBounds, lowestImage, viewportBounds)
const alignedRegion = alignRegionToChunks(region, lowestImage)
const dimensions: [number, number, number] = [
  alignedRegion.end[0] - alignedRegion.start[0],
  alignedRegion.end[1] - alignedRegion.start[1],
  alignedRegion.end[2] - alignedRegion.start[2],
]

Refactoring Recommendation

Extract a shared helper and accept a pixelCountFn callback:

type PixelCountFn = (dimensions: [number, number, number], image: NgffImage) => number

function selectResolutionWithPixelCount(
  multiscales: Multiscales,
  maxPixels: number,
  clipPlanes: ClipPlanes,
  volumeBounds: VolumeBounds,
  viewportBounds: VolumeBounds | undefined,
  pixelCountFn: PixelCountFn,
): ResolutionSelection {
  const images = multiscales.images
  for (let i = 0; i < images.length; i++) {
    const image = images[i]
    const region = clipPlanesToPixelRegion(clipPlanes, volumeBounds, image, viewportBounds)
    const alignedRegion = alignRegionToChunks(region, image)
    const dimensions: [number, number, number] = [
      alignedRegion.end[0] - alignedRegion.start[0],
      alignedRegion.end[1] - alignedRegion.start[1],
      alignedRegion.end[2] - alignedRegion.start[2],
    ]
    const pixelCount = pixelCountFn(dimensions, image)
    if (pixelCount <= maxPixels) return { levelIndex: i, dimensions, pixelCount }
  }
  // fallback
  const lowestImage = images[images.length - 1]
  const region = clipPlanesToPixelRegion(clipPlanes, volumeBounds, lowestImage, viewportBounds)
  const alignedRegion = alignRegionToChunks(region, lowestImage)
  const dimensions: [number, number, number] = [/* ... */]
  return { levelIndex: images.length - 1, dimensions, pixelCount: pixelCountFn(dimensions, lowestImage) }
}

Impact Analysis

  • Maintainability: Bug fixes to chunk alignment math must be applied in two files; changes to resolution selection loop must be applied twice.
  • Bug Risk: Divergence has already occurred — alignToChunks and alignRegionToChunks have subtly different variable naming and return structures, making it easy to overlook one when fixing a bug.
  • Code Bloat: ~80 duplicated lines across these two patterns.

Implementation Checklist

  • Review duplication findings
  • Refactor alignToChunks in ClipPlanes.ts to delegate to alignRegionToChunks
  • Extract shared resolution selection helper in ResolutionSelector.ts
  • Ensure existing Playwright tests still pass after refactoring
  • Verify no functionality broken

Analysis Metadata

  • Analyzed Files: 11 source files in fidnii/src/
  • Detection Method: Serena semantic code analysis (LSP + symbol search + pattern matching)
  • Analysis Date: 2026-04-01

Generated by Duplicate Code Detector

To install this agentic workflow, run

gh aw add github/gh-aw/.github/workflows/duplicate-code-detector.md@33cd6c7f1fee588654ef19def2e6a4174be66197

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions