Skip to content

Commit

Permalink
feat: Add max value calculations to rectangle and ellipse
Browse files Browse the repository at this point in the history
  • Loading branch information
sedghi authored and swederik committed Mar 22, 2022
1 parent 556bdcd commit 2ebabc1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ export default class EllipticalRoiTool extends BaseAnnotationTool {

_getTextLines = (data, targetUID) => {
const cachedVolumeStats = data.cachedStats[targetUID]
const { area, mean, stdDev, isEmptyArea, Modality } = cachedVolumeStats
const { area, mean, stdDev, max, isEmptyArea, Modality } = cachedVolumeStats

if (mean === undefined) {
return
Expand All @@ -885,20 +885,25 @@ export default class EllipticalRoiTool extends BaseAnnotationTool {
? `Area: Oblique not supported`
: `Area: ${area.toFixed(2)} mm${String.fromCharCode(178)}`
let meanLine = `Mean: ${mean.toFixed(2)}`
let maxLine = `Max: ${max.toFixed(2)}`
let stdDevLine = `Std Dev: ${stdDev.toFixed(2)}`

if (Modality === 'PT') {
meanLine += ' SUV'
maxLine += ' SUV'
stdDevLine += ' SUV'
} else if (Modality === 'CT') {
meanLine += ' HU'
maxLine += ' HU'
stdDevLine += ' HU'
} else {
meanLine += ' MO'
maxLine += ' MO'
stdDevLine += ' MO'
}

textLines.push(areaLine)
textLines.push(maxLine)
textLines.push(meanLine)
textLines.push(stdDevLine)

Expand Down Expand Up @@ -1000,6 +1005,17 @@ export default class EllipticalRoiTool extends BaseAnnotationTool {
let count = 0
let mean = 0
let stdDev = 0
let max = -Infinity

const meanMaxCalculator = (
canvasCoords,
ijkCoords,
index,
newValue
) => {
if (newValue > max) {
max = newValue
}

const yMultiple = dimensions[0]
const zMultiple = dimensions[0] * dimensions[1]
Expand Down Expand Up @@ -1073,7 +1089,7 @@ export default class EllipticalRoiTool extends BaseAnnotationTool {
imageData,
dimensions,
(canvasCoords) => pointInEllipse(ellipse, canvasCoords),
meanCalculator
meanMaxCalculator
)

mean /= count
Expand Down Expand Up @@ -1105,6 +1121,7 @@ export default class EllipticalRoiTool extends BaseAnnotationTool {
Modality: metadata.Modality,
area,
mean,
max,
stdDev,
isEmptyArea,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,7 @@ export default class RectangleRoiTool extends BaseAnnotationTool {
*/
_getTextLines = (data, targetUID: string) => {
const cachedVolumeStats = data.cachedStats[targetUID]
const { area, mean, stdDev, Modality } = cachedVolumeStats
const { area, mean, max, stdDev, Modality } = cachedVolumeStats

if (mean === undefined) {
return
Expand All @@ -849,21 +849,26 @@ export default class RectangleRoiTool extends BaseAnnotationTool {

const areaLine = `Area: ${area.toFixed(2)} mm${String.fromCharCode(178)}`
let meanLine = `Mean: ${mean.toFixed(2)}`
let maxLine = `Max: ${max.toFixed(2)}`
let stdDevLine = `Std Dev: ${stdDev.toFixed(2)}`

// Give appropriate units for the modality.
if (Modality === 'PT') {
meanLine += ' SUV'
maxLine += ' SUV'
stdDevLine += ' SUV'
} else if (Modality === 'CT') {
meanLine += ' HU'
maxLine += ' HU'
stdDevLine += ' HU'
} else {
meanLine += ' MO'
maxLine += ' MO'
stdDevLine += ' MO'
}

textLines.push(areaLine)
textLines.push(maxLine)
textLines.push(meanLine)
textLines.push(stdDevLine)

Expand Down Expand Up @@ -962,6 +967,7 @@ export default class RectangleRoiTool extends BaseAnnotationTool {
let count = 0
let mean = 0
let stdDev = 0
let max = -Infinity

const yMultiple = dimensions[0]
const zMultiple = dimensions[0] * dimensions[1]
Expand All @@ -974,6 +980,10 @@ export default class RectangleRoiTool extends BaseAnnotationTool {
for (let i = iMin; i <= iMax; i++) {
const value = scalarData[k * zMultiple + j * yMultiple + i]

if (value > max) {
max = value
}

count++
mean += value
}
Expand Down Expand Up @@ -1002,6 +1012,7 @@ export default class RectangleRoiTool extends BaseAnnotationTool {
area,
mean,
stdDev,
max,
}
} else {
this.isHandleOutsideImage = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,25 @@ import isEqual from '../math/vec3/isEqual'
* we do not double count voxels that are part of multiple labelmaps.
* @param {} labelmaps
* @param {number} segmentIndex
* @returns {number} TMTV
* @returns {number} TMTV in ml
*/
function calculateTMTV(
labelmaps: Array<IImageVolume>,
segmentIndex = 1
): number {
labelmaps.forEach(({ direction, dimensions, origin }) => {
labelmaps.forEach(({ direction, dimensions, origin, spacing }) => {
if (
!isEqual(dimensions, labelmaps[0].dimensions) ||
!isEqual(direction, labelmaps[0].direction) ||
!isEqual(spacing, labelmaps[0].spacing) ||
!isEqual(origin, labelmaps[0].origin)
) {
throw new Error('labelmaps must have the same size and shape')
}
})

const labelmap = labelmaps[0]
const [xSpacing, ySpacing, zSpacing] = labelmap.spacing

const arrayType = labelmap.scalarData.constructor
const outputData = new arrayType(labelmap.scalarData.length)
Expand All @@ -41,14 +43,14 @@ function calculateTMTV(

// count non-zero values inside the outputData, this would
// consider the overlapping regions to be only counted once
const tmtv = outputData.reduce((acc, curr) => {
const numVoxels = outputData.reduce((acc, curr) => {
if (curr > 0) {
return acc + 1
}
return acc
}, 0)

return tmtv
return numVoxels * xSpacing * ySpacing * zSpacing
}

export default calculateTMTV

0 comments on commit 2ebabc1

Please sign in to comment.