[IDC-1994] Sort series list by SeriesNumber, and sort by same SeriesNumber by date/time. (#2010)
* Sort based on SeriesNumber and SeriesDate/SeriesTime. * Harden, and perform final sort in algorithm if last N entries have the same SeriesNumber. * Switch to insertion rather than sorting as sorting is too slow. Reimplement low priority sorting into new insertion method. * Fix local file viewing.
This commit is contained in:
1 parent
ef28fa2ac1
commit
dbf0501031
12 files changed
+140
-133
No files matched your search
@@ -54,9 +54,6 @@ export class OHIFStudyMetadataSource extends StudyMetadataSource {
|
||||
// Get Study display sets
|
||||
const displaySets = studyMetadata.createDisplaySets();
|
||||
|
||||
// Set studyMetadata display sets
|
||||
studyMetadata.setDisplaySets(displaySets);
|
||||
|
||||
OHIFStudyMetadataSource._updateStudyCollections(studyMetadata);
|
||||
resolve(studyMetadata);
|
||||
})
|
||||
|
||||
@@ -10,8 +10,8 @@ import { api } from 'dicomweb-client';
|
||||
// - createStacks
|
||||
import { isImage } from '../../utils/isImage';
|
||||
import isDisplaySetReconstructable from '../../utils/isDisplaySetReconstructable';
|
||||
import isLowPriorityModality from '../../utils/isLowPriorityModality';
|
||||
import errorHandler from '../../errorHandler';
|
||||
import isLowPriorityModality from '../../utils/isLowPriorityModality';
|
||||
|
||||
export class StudyMetadata extends Metadata {
|
||||
constructor(data, uid) {
|
||||
@@ -308,14 +308,10 @@ export class StudyMetadata extends Metadata {
|
||||
series
|
||||
);
|
||||
|
||||
displaySets.push(...displaySetsForSeries);
|
||||
displaySetsForSeries.forEach(ds => this._insertDisplaySet(ds));
|
||||
});
|
||||
|
||||
return sortDisplaySetList(displaySets);
|
||||
}
|
||||
|
||||
sortDisplaySets() {
|
||||
sortDisplaySetList(this._displaySets);
|
||||
return this._displaySets;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -346,25 +342,9 @@ export class StudyMetadata extends Metadata {
|
||||
this.addDisplaySet(displaySet);
|
||||
});
|
||||
|
||||
this.sortDisplaySets();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set display sets
|
||||
* @param {Array} displaySets Array of display sets (ImageSet[])
|
||||
*/
|
||||
setDisplaySets(displaySets) {
|
||||
if (Array.isArray(displaySets) && displaySets.length > 0) {
|
||||
// TODO: This is weird, can we just switch it to writable: true?
|
||||
this._displaySets.splice(0);
|
||||
|
||||
displaySets.forEach(displaySet => this.addDisplaySet(displaySet));
|
||||
this.sortDisplaySets();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a single display set to the list
|
||||
* @param {Object} displaySet Display set object
|
||||
@@ -372,7 +352,7 @@ export class StudyMetadata extends Metadata {
|
||||
*/
|
||||
addDisplaySet(displaySet) {
|
||||
if (displaySet instanceof ImageSet || displaySet.sopClassModule) {
|
||||
this._displaySets.push(displaySet);
|
||||
this._insertDisplaySet(displaySet);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -393,6 +373,96 @@ export class StudyMetadata extends Metadata {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert the displaySet so that the list has an increasing SeriesNumber,
|
||||
* with the most recent series first for displaySets with the same SeriesNumber.
|
||||
*
|
||||
* If the displaySet is low priority, the same logic is applied, but is sorted within a sub list
|
||||
* At the end of the list, where all low priority data is found.
|
||||
*/
|
||||
_insertDisplaySet(displaySet) {
|
||||
const { SeriesNumber } = displaySet;
|
||||
const displaySets = this._displaySets;
|
||||
let insertIndex = displaySets.length;
|
||||
let firstIndexWithSameSeriesNumber;
|
||||
|
||||
// If low priority, start search from next low priority.
|
||||
if (isLowPriorityModality(displaySet.Modality)) {
|
||||
let startingIndex;
|
||||
|
||||
// Find where the first low priority displaySet is.
|
||||
for (let i = 0; i < displaySets.length; i++) {
|
||||
if (isLowPriorityModality(displaySets[i].Modality)) {
|
||||
startingIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!startingIndex) {
|
||||
startingIndex = displaySets.length;
|
||||
}
|
||||
|
||||
// Find the correct SeriesNumber location to insert within the low priority
|
||||
// Modality displaySets
|
||||
for (let i = startingIndex; i < displaySets.length; i++) {
|
||||
if (
|
||||
displaySets[i].SeriesNumber === SeriesNumber &&
|
||||
!firstIndexWithSameSeriesNumber
|
||||
) {
|
||||
firstIndexWithSameSeriesNumber = i;
|
||||
}
|
||||
|
||||
if (displaySets[i].SeriesNumber > SeriesNumber) {
|
||||
insertIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Find correct SeriesNumber to insert or where the low priority modalities start.
|
||||
for (let i = 0; i < displaySets.length; i++) {
|
||||
if (
|
||||
displaySets[i].SeriesNumber === SeriesNumber &&
|
||||
!firstIndexWithSameSeriesNumber
|
||||
) {
|
||||
firstIndexWithSameSeriesNumber = i;
|
||||
}
|
||||
|
||||
if (
|
||||
displaySets[i].SeriesNumber > SeriesNumber ||
|
||||
isLowPriorityModality(displaySets[i].Modality)
|
||||
) {
|
||||
insertIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If we have multiple displaySets with the same series number, find the insert position based on
|
||||
// SeriesDate and SeriesTime.
|
||||
if (firstIndexWithSameSeriesNumber !== undefined) {
|
||||
// If no SeriesDate, is just a placeholder displaySet, just insert anywhere, it will be re-added later.
|
||||
if (displaySet.SeriesDate) {
|
||||
const seriesDateTime = `${displaySet.SeriesDate}${displaySet.SeriesTime}`;
|
||||
|
||||
for (let i = firstIndexWithSameSeriesNumber; i < insertIndex; i++) {
|
||||
const displaySetI = displaySets[i];
|
||||
|
||||
if (
|
||||
displaySetI.SeriesDate &&
|
||||
`${displaySetI.SeriesDate}${displaySetI.SeriesTime}` <
|
||||
seriesDateTime
|
||||
) {
|
||||
insertIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this._displaySets.splice(insertIndex, 0, displaySet);
|
||||
this.displaySets = this._displaySets;
|
||||
}
|
||||
|
||||
/**
|
||||
* Search the associated display sets using the supplied callback as criteria. The callback is passed
|
||||
* two arguments: display set (an ImageSet instance) and index (the integer
|
||||
@@ -548,49 +618,6 @@ export class StudyMetadata extends Metadata {
|
||||
return this._series.indexOf(series);
|
||||
}
|
||||
|
||||
/**
|
||||
* It sorts the series based on display sets order. Each series must be an instance
|
||||
* of SeriesMetadata and each display sets must be an instance of ImageSet.
|
||||
* Useful example of usage:
|
||||
* Study data provided by backend does not sort series at all and client-side
|
||||
* needs series sorted by the same criteria used for sorting display sets.
|
||||
*/
|
||||
sortSeriesByDisplaySets() {
|
||||
// Object for mapping display sets' index by SeriesInstanceUID
|
||||
const displaySetsMapping = {};
|
||||
|
||||
// Loop through each display set to create the mapping
|
||||
this.forEachDisplaySet((displaySet, index) => {
|
||||
if (!(displaySet instanceof ImageSet)) {
|
||||
throw new OHIFError(
|
||||
`StudyMetadata::sortSeriesByDisplaySets display set at index ${index} is not an instance of ImageSet`
|
||||
);
|
||||
}
|
||||
|
||||
// In case of multiframe studies, just get the first index occurence
|
||||
if (displaySetsMapping[displaySet.SeriesInstanceUID] === void 0) {
|
||||
displaySetsMapping[displaySet.SeriesInstanceUID] = index;
|
||||
}
|
||||
});
|
||||
|
||||
// Clone of actual series
|
||||
const actualSeries = this.getSeries();
|
||||
|
||||
actualSeries.forEach((series, index) => {
|
||||
if (!(series instanceof SeriesMetadata)) {
|
||||
throw new OHIFError(
|
||||
`StudyMetadata::sortSeriesByDisplaySets series at index ${index} is not an instance of SeriesMetadata`
|
||||
);
|
||||
}
|
||||
|
||||
// Get the new series index
|
||||
const seriesIndex = displaySetsMapping[series.getSeriesInstanceUID()];
|
||||
|
||||
// Update the series object with the new series position
|
||||
this._series[seriesIndex] = series;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares the current study instance with another one.
|
||||
* @param {StudyMetadata} study An instance of the StudyMetadata class.
|
||||
@@ -865,53 +892,3 @@ function _getDisplaySetFromSopClassModule(
|
||||
}
|
||||
return displaySet;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sort series primarily by Modality (i.e., series with references to other
|
||||
* series like SEG, KO or PR are grouped in the end of the list) and then by
|
||||
* series number:
|
||||
*
|
||||
* --------
|
||||
* | CT #3 |
|
||||
* | CT #4 |
|
||||
* | CT #5 |
|
||||
* --------
|
||||
* | SEG #1 |
|
||||
* | SEG #2 |
|
||||
* --------
|
||||
*
|
||||
* @param {*} a - DisplaySet
|
||||
* @param {*} b - DisplaySet
|
||||
*/
|
||||
|
||||
function seriesSortingCriteria(a, b) {
|
||||
const isLowPriorityA = isLowPriorityModality(a.Modality);
|
||||
const isLowPriorityB = isLowPriorityModality(b.Modality);
|
||||
if (!isLowPriorityA && isLowPriorityB) {
|
||||
return -1;
|
||||
}
|
||||
if (isLowPriorityA && !isLowPriorityB) {
|
||||
return 1;
|
||||
}
|
||||
return sortBySeriesNumber(a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sort series by series number. Series with low
|
||||
* @param {*} a - DisplaySet
|
||||
* @param {*} b - DisplaySet
|
||||
*/
|
||||
function sortBySeriesNumber(a, b) {
|
||||
const seriesNumberAIsGreaterOrUndefined =
|
||||
a.SeriesNumber > b.SeriesNumber || (!a.SeriesNumber && b.SeriesNumber);
|
||||
|
||||
return seriesNumberAIsGreaterOrUndefined ? 1 : -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts a list of display set objects
|
||||
* @param {Array} list A list of display sets to be sorted
|
||||
*/
|
||||
function sortDisplaySetList(list) {
|
||||
return list.sort(seriesSortingCriteria);
|
||||
}
|
||||
Reference in new issue
Block a user