Skip to content

Commit

Permalink
fix(datasets-dicom): rebuild volume if image deleted
Browse files Browse the repository at this point in the history
And only build volume if not already requested to build.
  • Loading branch information
PaulHax committed Apr 11, 2024
1 parent b089616 commit c2d791d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
37 changes: 28 additions & 9 deletions src/store/datasets-dicom.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import vtkITKHelper from '@kitware/vtk.js/Common/DataModel/ITKHelper';
import vtkImageData from '@kitware/vtk.js/Common/DataModel/ImageData';
import { defineStore } from 'pinia';
import { Image } from 'itk-wasm';
import { DataSourceWithFile } from '@/src/io/import/dataSource';
Expand Down Expand Up @@ -61,6 +62,9 @@ interface State {
// volume invalidation information
needsRebuild: Record<string, boolean>;

// Promise resolving to vtkImageData
volumeImageData: Record<string, Promise<vtkImageData>>;

// patientKey -> patient info
patientInfo: Record<string, PatientInfo>;
// patientKey -> array of studyKeys
Expand Down Expand Up @@ -122,6 +126,7 @@ export const useDICOMStore = defineStore('dicom', {
sliceData: {},
volumeToImageID: {},
imageIDToVolumeKey: {},
volumeImageData: {},
patientInfo: {},
patientStudies: {},
studyInfo: {},
Expand Down Expand Up @@ -254,6 +259,10 @@ export const useDICOMStore = defineStore('dicom', {
delete this.imageIDToVolumeKey[imageID];
}

if (volumeKey in this.volumeImageData) {
delete this.volumeImageData[volumeKey];
}

removeFromArray(this.studyVolumes[studyKey], volumeKey);
if (this.studyVolumes[studyKey].length === 0) {
this.deleteStudy(studyKey);
Expand Down Expand Up @@ -354,15 +363,17 @@ export const useDICOMStore = defineStore('dicom', {
return this.getVolumeSlice(volumeKey, middleSlice, true);
},

async buildVolume(volumeKey: string, forceRebuild: boolean = false) {
async _buildVolume(volumeKey: string, forceRebuild: boolean = false) {
const imageStore = useImageStore();
const dicomIO = this.$dicomIO;

const rebuild = forceRebuild || this.needsRebuild[volumeKey];

if (!rebuild && this.volumeToImageID[volumeKey]) {
const imageID = this.volumeToImageID[volumeKey]!;
return imageStore.dataIndex[imageID];
const existingImageID = this.volumeToImageID[volumeKey];
const imageExists =
existingImageID && imageStore.dataIndex[existingImageID];
if (!rebuild && imageExists) {
return imageStore.dataIndex[existingImageID];
}

const fileStore = useFileStore();
Expand All @@ -372,21 +383,29 @@ export const useDICOMStore = defineStore('dicom', {
await dicomIO.buildImage(files)
);

const existingImageID = this.volumeToImageID[volumeKey];
if (existingImageID) {
if (imageExists) {
// was a rebuild
imageStore.updateData(existingImageID, image);
} else {
const info = this.volumeInfo[volumeKey];
const name =
cleanupName(info.SeriesDescription) || info.SeriesInstanceUID;
const imageID = imageStore.addVTKImageData(name, image);
this.imageIDToVolumeKey[imageID] = volumeKey;
this.volumeToImageID[volumeKey] = imageID;
const newImageID = imageStore.addVTKImageData(name, image);
this.imageIDToVolumeKey[newImageID] = volumeKey;
this.volumeToImageID[volumeKey] = newImageID;
}

delete this.needsRebuild[volumeKey];

return image;
},

buildVolume(volumeKey: string) {
if (volumeKey in this.volumeImageData) {
return this.volumeImageData[volumeKey];
}
this.volumeImageData[volumeKey] = this._buildVolume(volumeKey);
return this.volumeImageData[volumeKey];
},
},
});
2 changes: 1 addition & 1 deletion src/store/load-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ function loadLayers(
layersStore.addLayer(primarySelection, layerSelection);
}

// Converts DICOM SEG modalities to segmentations if found
// Loads DICOM SEG modalities to segmentations if found
function loadSegmentations(
primaryDataSource: VolumeResult,
succeeded: Array<PipelineResultSuccess<ImportResult>>
Expand Down
2 changes: 1 addition & 1 deletion src/store/segmentGroups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ export const useSegmentGroupStore = defineStore('segmentGroup', () => {
function getNextColor() {
const color = DEFAULT_SEGMENT_MASKS[lastColorIndex].color;
lastColorIndex = (lastColorIndex + 1) % DEFAULT_SEGMENT_MASKS.length;
return color;
return [...color];
}

function decodeSegments(image: DataSelection) {
Expand Down

0 comments on commit c2d791d

Please sign in to comment.