Re IDC #2765: fix loading of segmentations at first startup (#2768)

Re IDC #2761: fix loading of segmentations

update dcmjs to 0.19.9
This commit is contained in:
Davide Punzo authored and GitHub committed 2022-04-05 09:39:22 +02:00
1 parent b94d4782d1
commit 32828d9a6e
15 files changed
+173 -177

No files matched your search

+1 -1
View File
@@ -39,7 +39,7 @@
"dependencies": {
"@babel/runtime": "^7.5.5",
"ajv": "^6.10.0",
"dcmjs": "0.19.8",
"dcmjs": "0.19.9",
"dicomweb-client": "^0.8.3",
"immer": "9.0.12",
"isomorphic-base64": "^1.0.2",
@@ -49,7 +49,12 @@ import studyMetadataManager from './studyMetadataManager';
* @param {object} snackbar
* @returns void
*/
async function loadAndCacheDerivedDisplaySets(referencedDisplaySet, studies, logger, snackbar) {
async function loadAndCacheDerivedDisplaySets(
referencedDisplaySet,
studies,
logger,
snackbar
) {
const { StudyInstanceUID, SeriesInstanceUID } = referencedDisplaySet;
const promises = [];
const studyMetadata = studyMetadataManager.get(StudyInstanceUID);
@@ -80,60 +85,111 @@ async function loadAndCacheDerivedDisplaySets(referencedDisplaySet, studies, log
});
// For each type, see if any are loaded, if not load the most recent.
await Promise.all(Object.keys(displaySetsPerModality).map(async (key) => {
const displaySets = displaySetsPerModality[key];
await Promise.all(
Object.keys(displaySetsPerModality).map(async key => {
const displaySets = displaySetsPerModality[key];
const isLoaded = displaySets.some(displaySet => displaySet.isLoaded);
if (isLoaded) {
return;
}
if (displaySets.some(displaySet => displaySet.loadError)) {
return;
}
// find most recent and load it.
let recentDateTime = 0;
let recentDisplaySet = displaySets[0];
displaySets.forEach(displaySet => {
const dateTime = Number(
`${displaySet.SeriesDate}${displaySet.SeriesTime}`
);
if (dateTime > recentDateTime) {
recentDateTime = dateTime;
recentDisplaySet = displaySet;
const isLoaded = displaySets.some(displaySet => displaySet.isLoaded);
if (isLoaded) {
return;
}
});
try {
if (recentDisplaySet.hasOwnProperty('getSourceDisplaySet') &&
typeof recentDisplaySet.getSourceDisplaySet === 'function') {
await recentDisplaySet.getSourceDisplaySet(studies);
} else {
await recentDisplaySet.load(referencedDisplaySet, studies);
if (displaySets.some(displaySet => displaySet.loadError)) {
return;
}
} catch (error) {
recentDisplaySet.isLoaded = false;
recentDisplaySet.loadError = true;
logger.error({ error, message: error.message });
snackbar.show({
title: 'Error loading derived display set:',
message: error.message,
type: 'error',
error,
autoClose: false,
// find most recent and load it.
let recentDateTime = 0;
let recentDisplaySet = displaySets[0];
displaySets.forEach(displaySet => {
const dateTime = Number(
`${displaySet.SeriesDate}${displaySet.SeriesTime}`
);
if (dateTime > recentDateTime) {
recentDateTime = dateTime;
recentDisplaySet = displaySet;
}
});
}
}));
try {
if (
recentDisplaySet.hasOwnProperty('getSourceDisplaySet') &&
typeof recentDisplaySet.getSourceDisplaySet === 'function'
) {
if (recentDisplaySet.Modality === 'SEG' && logger) {
const onDisplaySetLoadFailureHandler = error => {
logger.error({ error, message: error.message });
snackbar.show({
title: 'DICOM Segmentation Loader',
message: error.message,
type: 'error',
autoClose: true,
});
};
let activatedLabelmapIndex = -1;
while (activatedLabelmapIndex == -1) {
const {
referencedDisplaySet,
activatedLabelmapPromise,
} = await recentDisplaySet.getSourceDisplaySet(
studies,
true,
onDisplaySetLoadFailureHandler
);
activatedLabelmapIndex = await activatedLabelmapPromise;
const selectionFired = new CustomEvent(
'extensiondicomsegmentationsegselected',
{
detail: { activatedLabelmapIndex: activatedLabelmapIndex },
}
);
document.dispatchEvent(selectionFired);
const lastDateTime = Number(
`${recentDisplaySet.SeriesDate}${recentDisplaySet.SeriesTime}`
);
recentDateTime = 0;
displaySets.forEach(displaySet => {
const dateTime = Number(
`${displaySet.SeriesDate}${displaySet.SeriesTime}`
);
if (dateTime > recentDateTime && dateTime < lastDateTime) {
recentDateTime = dateTime;
recentDisplaySet = displaySet;
}
});
}
} else {
await recentDisplaySet.getSourceDisplaySet(studies);
}
} else {
await recentDisplaySet.load(referencedDisplaySet, studies);
}
} catch (error) {
recentDisplaySet.isLoaded = false;
recentDisplaySet.loadError = true;
logger.error({ error, message: error.message });
snackbar.show({
title: 'Error loading derived display set:',
message: error.message,
type: 'error',
error,
autoClose: false,
});
}
})
);
/*
* TODO: Improve the way we notify parts of the app
* that depends on derived display sets to be loaded.
* (Implement pubsub for better tracking of derived display sets)
*/
* TODO: Improve the way we notify parts of the app
* that depends on derived display sets to be loaded.
* (Implement pubsub for better tracking of derived display sets)
*/
const event = new CustomEvent('deriveddisplaysetsloadedandcached');
document.dispatchEvent(event);
};
}
export default loadAndCacheDerivedDisplaySets;
+1 -1
View File
@@ -66,7 +66,7 @@
"cornerstone-math": "^0.1.9",
"cornerstone-tools": "^6.0.6",
"cornerstone-wado-image-loader": "^4.1.0",
"dcmjs": "0.19.8",
"dcmjs": "0.19.9",
"dicom-parser": "^1.8.11",
"dicomweb-client": "^0.8.3",
"hammerjs": "^2.0.8",
@@ -105,6 +105,11 @@ class Viewer extends Component {
if (this.props.dialog) {
this.props.dialog.dismissAll();
}
document.removeEventListener(
'segmentationLoadingError',
this._updateThumbnails
);
}
retrieveTimepoints = filter => {
@@ -212,6 +217,12 @@ class Viewer extends Component {
),
});
}
document.addEventListener(
'segmentationLoadingError',
this._updateThumbnails.bind(this),
false
);
}
componentDidUpdate(prevProps) {
@@ -255,6 +266,20 @@ class Viewer extends Component {
}
}
_updateThumbnails() {
const { studies, activeViewportIndex, viewports } = this.props;
const activeViewport = viewports[activeViewportIndex];
const activeDisplaySetInstanceUID = activeViewport
? activeViewport.displaySetInstanceUID
: undefined;
this.setState({
thumbnails: _mapStudiesToThumbnails(studies, activeDisplaySetInstanceUID),
activeDisplaySetInstanceUID,
});
}
_getActiveViewport() {
return this.props.viewports[this.props.activeViewportIndex];
}
@@ -461,9 +486,7 @@ const _checkForDerivedDisplaySets = async function(displaySet, study) {
/**
* Async function to check if there are any inconsistences in the series.
*
* For segmentation checks that the geometry is consistent with the source images:
* 1) no frames out of plane;
* 2) have the same width and height.
* For segmentation returns any error during loading.
*
* For reconstructable 3D volume:
* 1) Is series multiframe?
@@ -477,10 +500,7 @@ const _checkForDerivedDisplaySets = async function(displaySet, study) {
* @param {*object} displaySet
* @returns {[string]} an array of strings containing the warnings
*/
const _checkForSeriesInconsistencesWarnings = async function(
displaySet,
studies
) {
const _checkForSeriesInconsistencesWarnings = async function(displaySet) {
if (displaySet.inconsistencyWarnings) {
// warnings already checked and cached in displaySet
return displaySet.inconsistencyWarnings;
@@ -546,98 +566,14 @@ const _checkForSeriesInconsistencesWarnings = async function(
if (displaySet.isModalitySupported === false) {
inconsistencyWarnings.push('The datasets modality is not supported.');
}
displaySet.inconsistencyWarnings = inconsistencyWarnings;
} else {
const segMetadata = displaySet.metadata;
if (!segMetadata) {
return inconsistencyWarnings;
}
const { referencedDisplaySet } = displaySet.getSourceDisplaySet(
studies,
false
);
if (!referencedDisplaySet) {
return inconsistencyWarnings;
}
const imageIds = referencedDisplaySet.images.map(image =>
image.getImageId()
);
if (!imageIds || imageIds.length === 0) {
return inconsistencyWarnings;
}
for (
let i = 0,
groupsLen = segMetadata.PerFrameFunctionalGroupsSequence.length;
i < groupsLen;
++i
) {
const PerFrameFunctionalGroups =
segMetadata.PerFrameFunctionalGroupsSequence[i];
if (!PerFrameFunctionalGroups) {
continue;
}
let SourceImageSequence = undefined;
if (segMetadata.SourceImageSequence) {
SourceImageSequence = segMetadata.SourceImageSequence[i];
} else if (PerFrameFunctionalGroups.DerivationImageSequence) {
SourceImageSequence =
PerFrameFunctionalGroups.DerivationImageSequence.SourceImageSequence;
}
if (!SourceImageSequence) {
if (inconsistencyWarnings.length === 0) {
const warningMessage =
'The segmentation ' +
'has frames out of plane respect to the source images.';
inconsistencyWarnings.push(warningMessage);
}
continue;
}
const { ReferencedSOPInstanceUID } = SourceImageSequence;
const imageId = imageIds.find(imageId => {
const sopCommonModule = cornerstone.metaData.get(
'sopCommonModule',
imageId
);
if (!sopCommonModule) {
return;
}
return sopCommonModule.sopInstanceUID === ReferencedSOPInstanceUID;
});
if (!imageId) {
continue;
}
const sourceImageMetadata = cornerstone.metaData.get('instance', imageId);
if (
segMetadata.Rows !== sourceImageMetadata.Rows ||
segMetadata.Columns !== sourceImageMetadata.Columns
) {
const warningMessage =
'The segmentation ' +
'has frames with different geometry ' +
'dimensions (Rows and Columns) respect to the source images.';
inconsistencyWarnings.push(warningMessage);
break;
}
}
if (inconsistencyWarnings.length !== 0) {
const warningMessage =
'The segmentation format is not supported yet. ' +
'The segmentation data (segments) could not be loaded.';
inconsistencyWarnings.push(warningMessage);
if (displaySet.loadError) {
inconsistencyWarnings.push(displaySet.segLoadErrorMessagge);
displaySet.inconsistencyWarnings = inconsistencyWarnings;
}
}
// cache the warnings
displaySet.inconsistencyWarnings = inconsistencyWarnings;
return inconsistencyWarnings;
};
@@ -736,10 +672,7 @@ const _mapStudiesToThumbnails = function(studies, activeDisplaySetInstanceUID) {
altImageText = displaySet.Modality ? displaySet.Modality : 'UN';
}
const hasWarnings = _checkForSeriesInconsistencesWarnings(
displaySet,
studies
);
const hasWarnings = _checkForSeriesInconsistencesWarnings(displaySet);
const hasDerivedDisplaySets = _checkForDerivedDisplaySets(
displaySet,
@@ -154,12 +154,25 @@ class ViewerMain extends Component {
});
};
const { referencedDisplaySet } = displaySet.getSourceDisplaySet(
const {
referencedDisplaySet,
activatedLabelmapPromise,
} = displaySet.getSourceDisplaySet(
this.props.studies,
true,
onDisplaySetLoadFailureHandler
);
displaySet = referencedDisplaySet;
activatedLabelmapPromise.then(activatedLabelmapIndex => {
const selectionFired = new CustomEvent(
'extensiondicomsegmentationsegselected',
{
detail: { activatedLabelmapIndex: activatedLabelmapIndex },
}
);
document.dispatchEvent(selectionFired);
});
} else {
displaySet = displaySet.getSourceDisplaySet(this.props.studies);
}
@@ -36,7 +36,7 @@ function DicomStorePickerModal({
user,
url,
},
onClose
onClose,
});
}
};