From 17f8d87d32c9dd5cd8c9c3df2620521bc3a04b3c Mon Sep 17 00:00:00 2001 From: Bruno Alves de Faria Date: Wed, 24 Aug 2016 11:53:31 -0300 Subject: [PATCH] Fixing hanging protocols matching score issues --- .../hangingprotocols/client/protocolEngine.js | 88 +++++++++++-------- Packages/ohif-core/client/lib/index.js | 1 + Packages/ohif-core/client/lib/utils.js | 44 ++++++++++ 3 files changed, 95 insertions(+), 38 deletions(-) create mode 100644 Packages/ohif-core/client/lib/utils.js diff --git a/Packages/hangingprotocols/client/protocolEngine.js b/Packages/hangingprotocols/client/protocolEngine.js index 053613de0..ddd292ca2 100644 --- a/Packages/hangingprotocols/client/protocolEngine.js +++ b/Packages/hangingprotocols/client/protocolEngine.js @@ -462,37 +462,29 @@ HP.ProtocolEngine = class ProtocolEngine { sopInstanceUid: instance.sopInstanceUid, currentImageIdIndex: index, matchingScore: totalMatchScore, - matchDetails: matchDetails + matchDetails: matchDetails, + sortingInfo: { + score: totalMatchScore, + study: study.studyDate + study.studyTime, + series: series.seriesNumber, // TODO: change for seriesDateTime + instance: instance.instanceNumber // TODO: change for acquisitionTime + } }; + // Find the displaySet + const filter = { + sopInstanceUid: instance.sopInstanceUid + }; + const displaySet = _.filter(study.displaySets, ds => _.findWhere(ds.images, filter))[0]; + + // If the instance was found, set the displaySet ID + if (displaySet) { + imageDetails.displaySetInstanceUid = displaySet.displaySetInstanceUid; + imageDetails.imageId = getImageId(instance); + } + if ((totalMatchScore > highestImageMatchingScore) || !bestMatch) { highestImageMatchingScore = totalMatchScore; - - // Set the displaySet ID - study.displaySets.every(displaySet => { - // Skip displaySet if it has no images - if (!displaySet.images.length) { - return true; - } - - // Skip displaySet if series is different - if (displaySet.seriesInstanceUid !== series.seriesInstanceUid) { - return true; - } - - // Try to find the current instance - const instanceFound = _.findWhere(displaySet.images, { - sopInstanceUid: instance.sopInstanceUid - }); - - // If the instance was found, set the displaySet ID - if (instanceFound) { - imageDetails.displaySetInstanceUid = displaySet.displaySetInstanceUid; - imageDetails.imageId = getImageId(instance); - return false; - } - }); - bestMatch = imageDetails; } @@ -501,6 +493,20 @@ HP.ProtocolEngine = class ProtocolEngine { }); }); + // Sort the matchingScores + const sortingFunction = OHIF.utils.sortBy({ + name: 'score', + reverse: true + }, { + name: 'study', + reverse: true + }, { + name: 'series' + }, { + name: 'instance' + }); + matchingScores.sort((a, b) => sortingFunction(a.sortingInfo, b.sortingInfo)); + return { bestMatch: bestMatch, matchingScores: matchingScores @@ -605,21 +611,27 @@ HP.ProtocolEngine = class ProtocolEngine { }); }; - if (details.bestMatch) { - currentViewportData.studyInstanceUid = details.bestMatch.studyInstanceUid; - currentViewportData.seriesInstanceUid = details.bestMatch.seriesInstanceUid; - currentViewportData.sopInstanceUid = details.bestMatch.sopInstanceUid; - currentViewportData.currentImageIdIndex = details.bestMatch.currentImageIdIndex; - currentViewportData.displaySetInstanceUid = details.bestMatch.displaySetInstanceUid; - currentViewportData.imageId = details.bestMatch.imageId; + let currentMatch = details.bestMatch; + let currentPosition = 1; + const scoresLength = details.matchingScores.length; + while (currentPosition < scoresLength && _.findWhere(viewportData, { + imageId: currentMatch.imageId + })) { + currentMatch = details.matchingScores[currentPosition]; + currentPosition++; } - const study = ViewerStudies.findOne({ - studyInstanceUid: details.bestMatch.studyInstanceUid - }); + if (currentMatch.imageId) { + currentViewportData.studyInstanceUid = currentMatch.studyInstanceUid; + currentViewportData.seriesInstanceUid = currentMatch.seriesInstanceUid; + currentViewportData.sopInstanceUid = currentMatch.sopInstanceUid; + currentViewportData.currentImageIdIndex = currentMatch.currentImageIdIndex; + currentViewportData.displaySetInstanceUid = currentMatch.displaySetInstanceUid; + currentViewportData.imageId = currentMatch.imageId; + } if (!currentViewportData.displaySetInstanceUid) { - throw "No matching display set found?"; + throw 'No matching display set found?'; } viewportData.push(currentViewportData); diff --git a/Packages/ohif-core/client/lib/index.js b/Packages/ohif-core/client/lib/index.js index 3f9be996f..cdbb096c7 100644 --- a/Packages/ohif-core/client/lib/index.js +++ b/Packages/ohif-core/client/lib/index.js @@ -2,4 +2,5 @@ import './blaze.js'; import './string.js'; import './ui.js'; import './user.js'; +import './utils.js'; import './viewer.js'; diff --git a/Packages/ohif-core/client/lib/utils.js b/Packages/ohif-core/client/lib/utils.js new file mode 100644 index 000000000..a164f02eb --- /dev/null +++ b/Packages/ohif-core/client/lib/utils.js @@ -0,0 +1,44 @@ +import { OHIF } from 'meteor/ohif:core'; + +OHIF.utils = {}; + +// Return the array sorting function for its object's properties +OHIF.utils.sortBy = function() { + var fields = [].slice.call(arguments), + n_fields = fields.length; + + return function(A, B) { + var a, b, field, key, primer, reverse, result, i; + + for (i = 0; i < n_fields; i++) { + result = 0; + field = fields[i]; + + key = typeof field === 'string' ? field : field.name; + + a = A[key]; + b = B[key]; + + if (typeof field.primer !== 'undefined') { + a = field.primer(a); + b = field.primer(b); + } + + reverse = (field.reverse) ? -1 : 1; + + if (a < b) { + result = reverse * -1; + } + + if (a > b) { + result = reverse * 1; + } + + if (result !== 0) { + break; + } + } + + return result; + }; +};