Skip to content

Commit

Permalink
docs(migration): Remove UMD builds and clarify it and refactor code f…
Browse files Browse the repository at this point in the history
…or improved readability and performance (#1636)

* fix: Update image UIDs and refactor contour generation logic

- Changed StudyInstanceUID and SeriesInstanceUID in stackPosition example for improved data handling.
- Refactored generateContourSetsFromLabelmap function to directly manipulate segmentation data, enhancing performance and readability.
- Updated isSliceEmptyForSegment function to utilize segData instead of voxelManager, streamlining the contour generation process.

* api
  • Loading branch information
sedghi authored Nov 28, 2024
1 parent 2f69e66 commit 4ea08d8
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 37 deletions.
1 change: 0 additions & 1 deletion .webpack/webpack.base.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const exclude = excludeNodeModulesExcept([]);
module.exports = (env, argv, { DIST_DIR }) => {
const mode = NODE_ENV === 'production' ? 'production' : 'development';
const isProdBuild = argv.mode !== 'development';
const outputFilename = isProdBuild ? '[name].umd.min.js' : '[name].umd.js';

const config = {
devtool: 'eval-source-map',
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"format-check": "npx lerna run format-check",
"build": "npx lerna run build --stream && npx lerna run build:loader",
"build:esm": "npx lerna run build:esm --stream",
"build:umd": "npx lerna run build:umd --stream",
"watch": "npx lerna watch -- lerna run build --scope=$LERNA_PACKAGE_NAME --include-dependents",
"build:update-api:core": "cd packages/core && npm run build:update-api",
"build:update-api:tools": "cd packages/tools && npm run build:update-api",
Expand Down
4 changes: 2 additions & 2 deletions packages/core/examples/stackPosition/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,9 @@ async function run() {
// Get Cornerstone imageIds and fetch metadata into RAM
const imageIds = await createImageIdsAndCacheMetaData({
StudyInstanceUID:
'1.3.6.1.4.1.9590.100.1.2.19841440611855834937505752510708699165',
'1.3.6.1.4.1.14519.5.2.1.99.1071.55651399101931177647030363790032',
SeriesInstanceUID:
'1.3.6.1.4.1.9590.100.1.2.160160590111755920740089886004263812825',
'1.3.6.1.4.1.14519.5.2.1.99.1071.11955901484749168523821342348553',
wadoRsRoot:
getLocalUrl() || 'https://d14fa38qiwhyfd.cloudfront.net/dicomweb',
});
Expand Down
3 changes: 1 addition & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@
"clean": "rm -rf node_modules/.cache/storybook && shx rm -rf dist",
"clean:deep": "yarn run clean && shx rm -rf node_modules",
"build": "yarn run build:esm",
"build:umd": "cross-env NODE_ENV=production webpack --config .webpack/webpack.prod.js",
"build:all": "yarn run build:esm && yarn run build:umd",
"build:all": "yarn run build:esm",
"dev": "tsc --project ./tsconfig.json --watch",
"format-check": "npx eslint ./src --quiet",
"api-check": "api-extractor --debug run ",
Expand Down
4 changes: 2 additions & 2 deletions packages/docs/docs/migration-guides/2x/1-general.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ https://compat-table.github.io/compat-table/es2016plus/

## Remove of CJS, only ESM builds

Starting with Cornerstone3D 2.x, we will no longer ship the CommonJS (CJS) build of the library. You most likely won't need to make any changes to your codebase. If you are aliasing the cjs library in your bundler, you can remove it completely.
Starting with Cornerstone3D 2.x, we will no longer ship the CommonJS (CJS) and UMD builds of the library. You most likely won't need to make any changes to your codebase. If you are aliasing the cjs library in your bundler, you can remove it completely.

<details>
<summary>Why?</summary>
Both Node.js and modern browsers now support ECMAScript Modules (ESM) by default. However, in the rare case where you need a non-ESM version, you can use the Universal Module Definition (UMD) build of the library.
Both Node.js and modern browsers now support ECMAScript Modules (ESM) by default.
</details>


Expand Down
3 changes: 1 addition & 2 deletions packages/nifti-volume-loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@
"scripts": {
"build:esm": "tsc --project ./tsconfig.json",
"build:esm:watch": "tsc --project ./tsconfig.json --watch",
"build:umd": "cross-env NODE_ENV=production webpack --config .webpack/webpack.prod.js",
"build:all": "yarn run build:umd && yarn run build:esm",
"build:all": "yarn run build:esm",
"dev": "tsc --project ./tsconfig.json --watch",
"build": "yarn run build:all && yarn run copy-dts",
"clean": "rm -rf node_modules/.cache/storybook && shx rm -rf dist",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,18 @@ function generateContourSetsFromLabelmap({ segmentations }) {
console.warn(`No volume found for ${segVolumeId}`);
return;
}
const voxelManager = vol.voxelManager as Types.IVoxelManager<number>;
const segData = voxelManager.getCompleteScalarDataArray() as Array<number>;

const numSlices = vol.dimensions[2];

// NOTE: Workaround for marching squares not finding closed contours at
// boundary of image volume, clear pixels along x-y border of volume
const voxelManager = vol.voxelManager as Types.IVoxelManager<number>;
const pixelsPerSlice = vol.dimensions[0] * vol.dimensions[1];

for (let z = 0; z < numSlices; z++) {
for (let y = 0; y < vol.dimensions[1]; y++) {
const index = y * vol.dimensions[0] + z * pixelsPerSlice;
voxelManager.setAtIndex(index, 0);
voxelManager.setAtIndex(index + vol.dimensions[0] - 1, 0);
segData[index] = 0;
segData[index + vol.dimensions[0] - 1] = 0;
}
}

Expand All @@ -57,18 +56,12 @@ function generateContourSetsFromLabelmap({ segmentations }) {
numberOfComponents: 1,
size: pixelsPerSlice * numSlices,
dataType: 'Uint8Array',
}) as vtkDataArray;

});
const { containedSegmentIndices } = segment;
for (let sliceIndex = 0; sliceIndex < numSlices; sliceIndex++) {
// Check if the slice is empty before running marching cube
if (
isSliceEmptyForSegment(
sliceIndex,
voxelManager,
pixelsPerSlice,
segIndex
)
isSliceEmptyForSegment(sliceIndex, segData, pixelsPerSlice, segIndex)
) {
continue;
}
Expand All @@ -77,12 +70,12 @@ function generateContourSetsFromLabelmap({ segmentations }) {
try {
// Modify segData for this specific segment directly
for (let i = 0; i < pixelsPerSlice; i++) {
const value = voxelManager.getAtIndex(i + frameStart);
const value = segData[i + frameStart];
if (value === segIndex || containedSegmentIndices?.has(value)) {
// @ts-expect-error vtk has wrong types
// @ts-ignore
scalars.setValue(i + frameStart, 1);
} else {
// @ts-expect-error vtk has wrong types
// @ts-ignore
scalars.setValue(i, 0);
}
}
Expand All @@ -99,16 +92,12 @@ function generateContourSetsFromLabelmap({ segmentations }) {
imageDataCopy.getPointData().setScalars(scalars);

// Connect pipeline
// @ts-ignore
mSquares.setInputData(imageDataCopy);
const cValues = [1];
// @ts-ignore
mSquares.setContourValues(cValues);
// @ts-ignore
mSquares.setMergePoints(false);

// Perform marching squares
// @ts-ignore
const msOutput = mSquares.getOutputData();

// Clean up output from marching squares
Expand Down Expand Up @@ -147,17 +136,12 @@ function generateContourSetsFromLabelmap({ segmentations }) {
return ContourSets;
}

function isSliceEmptyForSegment(
sliceIndex,
voxelManager,
pixelsPerSlice,
segIndex
) {
function isSliceEmptyForSegment(sliceIndex, segData, pixelsPerSlice, segIndex) {
const startIdx = sliceIndex * pixelsPerSlice;
const endIdx = startIdx + pixelsPerSlice;

for (let i = startIdx; i < endIdx; i++) {
if (voxelManager.getAtIndex(i) === segIndex) {
if (segData[i] === segIndex) {
return false;
}
}
Expand Down

0 comments on commit 4ea08d8

Please sign in to comment.