-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
2,376 additions
and
1,817 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
libs/insight-viewer/src/Viewer/CXR4HeatmapViewer/HeatmapViewer.styles.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { CSSProperties } from 'react' | ||
|
||
export const style: CSSProperties = { | ||
position: 'absolute', | ||
top: 0, | ||
left: 0, | ||
width: '100%', | ||
height: '100%', | ||
filter: 'blur(3.5px)', | ||
} as const |
14 changes: 14 additions & 0 deletions
14
libs/insight-viewer/src/Viewer/CXR4HeatmapViewer/HeatmapViewer.types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { OverlayContext } from '../../contexts' | ||
|
||
export interface HeatmapViewerProps { | ||
/** Heatmap data format resulting from AI */ | ||
posMap: number[][] | ||
|
||
/** Threshold value (CXR = 0.15, MMG = 0.1) */ | ||
threshold: number | ||
} | ||
|
||
export interface HeatmapDrawProps extends Pick<OverlayContext, 'setToPixelCoordinateSystem' | 'enabledElement'> { | ||
baseCanvas: HTMLCanvasElement | null | ||
heatmapData: ImageData | undefined | ||
} |
12 changes: 12 additions & 0 deletions
12
libs/insight-viewer/src/Viewer/CXR4HeatmapViewer/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import React, { ReactElement } from 'react' | ||
|
||
import useHeatmapDrawing from './useHeatmapDrawing' | ||
import { style } from './HeatmapViewer.styles' | ||
|
||
import type { HeatmapViewerProps } from './HeatmapViewer.types' | ||
|
||
export function CXR4HeatmapViewer(props: HeatmapViewerProps): ReactElement<HTMLCanvasElement> { | ||
const [canvasRef] = useHeatmapDrawing(props) | ||
|
||
return <canvas data-cy-name="heatmap-canvas" ref={canvasRef} style={style} /> | ||
} |
37 changes: 37 additions & 0 deletions
37
libs/insight-viewer/src/Viewer/CXR4HeatmapViewer/useHeatmapDrawing.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* eslint-disable no-param-reassign */ | ||
import { useEffect, useMemo, useRef, RefObject } from 'react' | ||
|
||
import { HeatmapViewerProps } from './HeatmapViewer.types' | ||
import { useOverlayContext } from '../../contexts' | ||
|
||
import drawHeatmap from '../../utils/CXR4HeatmapViewer/drawHeatmap' | ||
import getHeatmapImageData from '../../utils/CXR4HeatmapViewer/getHeatmapImageData' | ||
|
||
function useHeatmapDrawing({ posMap, threshold }: HeatmapViewerProps): [RefObject<HTMLCanvasElement>] { | ||
const canvasRef = useRef<HTMLCanvasElement>(null) | ||
const baseCanvas = canvasRef?.current | ||
const { enabledElement, setToPixelCoordinateSystem } = useOverlayContext() | ||
const { heatmapData, heatmapCanvas } = useMemo( | ||
() => | ||
getHeatmapImageData({ | ||
posMap, | ||
threshold, | ||
canvas: baseCanvas, | ||
}), | ||
[posMap, threshold, baseCanvas] | ||
) | ||
|
||
useEffect(() => { | ||
drawHeatmap({ | ||
baseCanvas, | ||
heatmapData, | ||
heatmapCanvas, | ||
enabledElement, | ||
setToPixelCoordinateSystem, | ||
}) | ||
}, [baseCanvas, heatmapCanvas, heatmapData, setToPixelCoordinateSystem, enabledElement]) | ||
|
||
return [canvasRef] | ||
} | ||
|
||
export default useHeatmapDrawing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
libs/insight-viewer/src/utils/CXR4HeatmapViewer/clearHeatmap.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
interface HeatmapClearProps { | ||
canvas: HTMLCanvasElement | ||
context: CanvasRenderingContext2D | ||
} | ||
|
||
export default function clearHeatmap({ canvas, context }: HeatmapClearProps): void { | ||
context.clearRect(0, 0, canvas.offsetWidth, canvas.offsetHeight) | ||
} |
55 changes: 55 additions & 0 deletions
55
libs/insight-viewer/src/utils/CXR4HeatmapViewer/drawHeatmap.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* eslint-disable no-param-reassign */ | ||
|
||
import { OverlayContext } from '../../contexts' | ||
import clearHeatmap from './clearHeatmap' | ||
|
||
interface DrawHeatmapProps extends Pick<OverlayContext, 'setToPixelCoordinateSystem' | 'enabledElement'> { | ||
baseCanvas: HTMLCanvasElement | null | ||
heatmapData: ImageData | null | ||
heatmapCanvas: HTMLCanvasElement | null | ||
} | ||
|
||
function drawHeatmap({ | ||
baseCanvas, | ||
heatmapData, | ||
heatmapCanvas, | ||
enabledElement, | ||
setToPixelCoordinateSystem, | ||
}: DrawHeatmapProps): void { | ||
if (!heatmapData || !baseCanvas || !heatmapCanvas || !enabledElement) return | ||
|
||
const baseCanvasContext = baseCanvas.getContext('2d') | ||
const heatmapCanvasContext = heatmapCanvas.getContext('2d') | ||
|
||
if (!baseCanvasContext || !heatmapCanvasContext) return | ||
|
||
clearHeatmap({ canvas: baseCanvas, context: baseCanvasContext }) | ||
|
||
const { offsetWidth, offsetHeight } = baseCanvas | ||
|
||
baseCanvas.width = offsetWidth | ||
baseCanvas.height = offsetHeight | ||
heatmapCanvas.width = heatmapData.width | ||
heatmapCanvas.height = heatmapData.height | ||
|
||
baseCanvasContext.save() | ||
heatmapCanvasContext.putImageData(heatmapData, 0, 0) | ||
|
||
setToPixelCoordinateSystem(baseCanvasContext) | ||
|
||
baseCanvasContext.drawImage( | ||
heatmapCanvas, | ||
0, | ||
0, | ||
heatmapCanvas.width, | ||
heatmapCanvas.height, | ||
0, | ||
0, | ||
enabledElement.image?.width ?? 0, | ||
enabledElement.image?.height ?? 0 | ||
) | ||
|
||
baseCanvasContext.restore() | ||
} | ||
|
||
export default drawHeatmap |
61 changes: 61 additions & 0 deletions
61
libs/insight-viewer/src/utils/CXR4HeatmapViewer/getHeatmapImageData.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { getRGBArray } from './getRGBArray' | ||
|
||
interface GetHeatmapImageDataProps { | ||
canvas: HTMLCanvasElement | null | ||
posMap: number[][] | ||
threshold: number | ||
} | ||
|
||
interface GetHeatmapImageDataReturn { | ||
heatmapData: ImageData | null | ||
heatmapCanvas: HTMLCanvasElement | null | ||
} | ||
|
||
const MAX_ALPHA = 255 | ||
const HEATMAP_OPACITY = 0.35 | ||
|
||
export default function getHeatmapImageData({ | ||
canvas, | ||
posMap, | ||
threshold, | ||
}: GetHeatmapImageDataProps): GetHeatmapImageDataReturn { | ||
if (!canvas) { | ||
return { heatmapData: null, heatmapCanvas: null } | ||
} | ||
|
||
const heatmapWidth = posMap[0].length ?? 0 | ||
const heatmapHeight = posMap.length ?? 0 | ||
|
||
canvas.width = heatmapWidth | ||
canvas.height = heatmapHeight | ||
|
||
const heatmapCanvas = document.createElement('canvas') | ||
const heatmapImageData = canvas.getContext('2d')?.createImageData(heatmapWidth, heatmapHeight) | ||
const pixels = heatmapImageData?.data | ||
|
||
if (!heatmapImageData || !pixels) { | ||
return { heatmapData: null, heatmapCanvas: null } | ||
} | ||
|
||
// convert prob_map into a heatmap on the canvas | ||
for (let i = 0; i < heatmapHeight; i += 1) { | ||
for (let j = 0; j < heatmapWidth; j += 1) { | ||
const offset = (i * heatmapWidth + j) * 4 | ||
const posProb = posMap[i][j] | ||
|
||
const thPosProb = (posProb - threshold) / (1 - threshold) | ||
if (posProb < threshold) { | ||
pixels[offset + 3] = 0 | ||
} else { | ||
const pixVal = getRGBArray(thPosProb) | ||
|
||
pixels[offset] = pixVal[0] | ||
pixels[offset + 1] = pixVal[1] | ||
pixels[offset + 2] = pixVal[2] | ||
pixels[offset + 3] = Math.round(MAX_ALPHA * HEATMAP_OPACITY) | ||
} | ||
} | ||
} | ||
|
||
return { heatmapData: heatmapImageData, heatmapCanvas } | ||
} |
Oops, something went wrong.