-
Notifications
You must be signed in to change notification settings - Fork 327
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add TMTV calculation for segmentations
- Loading branch information
Showing
4 changed files
with
98 additions
and
7 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
54 changes: 54 additions & 0 deletions
54
packages/cornerstone-tools/src/util/segmentation/calculateTMTV.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,54 @@ | ||
import { IImageVolume } from '@precisionmetrics/cornerstone-render/src/types' | ||
import isEqual from '../math/vec3/isEqual' | ||
|
||
/** | ||
* Given a list of labelmaps (with the possitibility of overlapping regions), | ||
* and a referenceVolume, it calculates the total metabolic turnover volume (TMTV) | ||
* by flattening and rasterizing each segment into a single labelmap and summing | ||
* the total number of volume voxels. It should be noted that for this calculation | ||
* we do not double count voxels that are part of multiple labelmaps. | ||
* @param {} labelmaps | ||
* @param {number} segmentIndex | ||
* @returns {number} TMTV | ||
*/ | ||
function calculateTMTV( | ||
labelmaps: Array<IImageVolume>, | ||
segmentIndex = 1 | ||
): number { | ||
labelmaps.forEach(({ direction, dimensions, origin }) => { | ||
if ( | ||
!isEqual(dimensions, labelmaps[0].dimensions) || | ||
!isEqual(direction, labelmaps[0].direction) || | ||
!isEqual(origin, labelmaps[0].origin) | ||
) { | ||
throw new Error('labelmaps must have the same size and shape') | ||
} | ||
}) | ||
|
||
const labelmap = labelmaps[0] | ||
|
||
const arrayType = labelmap.scalarData.constructor | ||
const outputData = new arrayType(labelmap.scalarData.length) | ||
|
||
labelmaps.forEach((labelmap) => { | ||
const { scalarData } = labelmap | ||
for (let i = 0; i < scalarData.length; i++) { | ||
if (scalarData[i] === segmentIndex) { | ||
outputData[i] = segmentIndex | ||
} | ||
} | ||
}) | ||
|
||
// count non-zero values inside the outputData, this would | ||
// consider the overlapping regions to be only counted once | ||
const tmtv = outputData.reduce((acc, curr) => { | ||
if (curr > 0) { | ||
return acc + 1 | ||
} | ||
return acc | ||
}, 0) | ||
|
||
return tmtv | ||
} | ||
|
||
export default calculateTMTV |
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