-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[VIEWER-167] Heatmap Viewer 스펙 변경 #422
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9eed60e
feat: update Heatmap spec
LTakhyunKim e4901be
feat: add Legacy Heatmap viewer component
LTakhyunKim 1a523ee
docs: add legacy heatmap viewer example
LTakhyunKim 1160997
docs: apply () to rgb comment
LTakhyunKim 817ea51
refactor: apply constant to max alpha and opacity
LTakhyunKim b831f8f
feat: delete unused gaussian blur logic and apply blur style
LTakhyunKim 8c8496b
refactor: rename HeatmapViewer to CXR4HeatmapViewer
LTakhyunKim edf7dbf
refactor: rename LegacyHeatmapViewer to HeatmapViewer
LTakhyunKim 8b9f009
fix: delete unused loader optons
LTakhyunKim c5634c7
docs: add title of heatmap viewer docs
LTakhyunKim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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 } | ||
} |
29 changes: 29 additions & 0 deletions
29
libs/insight-viewer/src/utils/CXR4HeatmapViewer/getRGBArray.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,29 @@ | ||
/** | ||
* A function that converts probability values | ||
* (0~1) of a specific area into [r, g, b] arrays corresponding to JET colormap colors | ||
*/ | ||
export function getRGBArray(value: number): number[] { | ||
const v = Math.max(Math.min(value, 1), 0) | ||
let r, g, b | ||
|
||
if (v < 0.25) { | ||
// Between 0 and 0.25: (74,230,255) fixed | ||
r = 74 | ||
g = 230 | ||
b = 255 | ||
} else if (v < 0.5) { | ||
// Between 0.25 and 0.5: linearly convert from (74,230,255) to (221,255,0) | ||
const t = (v - 0.25) / 0.25 | ||
r = Math.round(74 + t * (221 - 74)) | ||
g = Math.round(230 + t * (255 - 230)) | ||
b = Math.round(255 + t * (0 - 255)) | ||
} else { | ||
// 0.5 to 1 interval: linearly convert from (221,255,0) to (255,0,92) | ||
const t = (v - 0.5) / 0.5 | ||
r = Math.round(221 + t * (255 - 221)) | ||
g = Math.round(255 + t * (0 - 255)) | ||
b = Math.round(0 + t * (92 - 0)) | ||
} | ||
|
||
return [r, g, b] | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
각 뷰어 위에다 작게 이름(
HeatmapViewer
) 써주면 좋겠네요There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
말씀해주신 것처럼 각 Heatmap 의 title 을 추가했습니다.
c5634c7
![스크린샷 2024-10-31 오전 11 05 14](https://private-user-images.githubusercontent.com/88369343/381797471-f25797ba-a30a-4c36-8499-bf08dffe24ca.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk1OTgwOTksIm5iZiI6MTczOTU5Nzc5OSwicGF0aCI6Ii84ODM2OTM0My8zODE3OTc0NzEtZjI1Nzk3YmEtYTMwYS00YzM2LTg0OTktYmYwOGRmZmUyNGNhLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjE1VDA1MzYzOVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWNiMWM4YmYzNDg4MzVmNTlhZThiMGQ3ODYxZGJlZDFlNTllMjFjNjNlZThmMGQ1MDI0YzlhMmM1NDRmMjgwMGYmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.I5tODfh1SRlvY30O3thpDcDdXg55hlmGVFd0hEb6YhQ)