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):
- Iterate resolution levels from finest to coarsest
- For each level: call
clipPlanesToPixelRegion → alignRegionToChunks → compute dimensions
- Check if pixel count ≤
maxPixels, return if so
- 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
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
Analysis of recent commits in fideus-labs/fidnii
Assignee:
@copilotSummary
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
fidnii/src/ResolutionSelector.ts(lines 166–200) —alignRegionToChunksfidnii/src/ClipPlanes.ts(lines 500–546) —alignToChunksBoth 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):alignToChunks(ClipPlanes.ts) — identical math, different return shape:ClipPlanes.tsalready importsgetChunkShapeandgetVolumeShapefromResolutionSelector.ts, so it could also callalignRegionToChunksdirectly.Refactoring Recommendation
Have
alignToChunksdelegate toalignRegionToChunks:Pattern 2: Resolution Selection Loop Duplicated Within Same File
Duplication Details
fidnii/src/ResolutionSelector.ts(lines 34–92) —selectResolutionfidnii/src/ResolutionSelector.ts(lines 268–339) —select2DResolutionBoth functions share the same structure (~50 lines):
clipPlanesToPixelRegion→alignRegionToChunks→ computedimensionsmaxPixels, return if soThe only difference is how
pixelCountis computed:dimensions[0]*dimensions[1]*dimensions[2]for 3D vsdimensions[inPlaneAxes[0]] * dimensions[inPlaneAxes[1]] * slabDepthfor 2D slabs.Duplicated fallback block (appears twice per function, 4× total):
Refactoring Recommendation
Extract a shared helper and accept a
pixelCountFncallback:Impact Analysis
alignToChunksandalignRegionToChunkshave subtly different variable naming and return structures, making it easy to overlook one when fixing a bug.Implementation Checklist
alignToChunksinClipPlanes.tsto delegate toalignRegionToChunksResolutionSelector.tsAnalysis Metadata
fidnii/src/