-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathdatasets-dicom.ts
More file actions
388 lines (328 loc) · 11.6 KB
/
datasets-dicom.ts
File metadata and controls
388 lines (328 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import vtkITKHelper from '@kitware/vtk.js/Common/DataModel/ITKHelper';
import { defineStore } from 'pinia';
import { Image } from 'itk-wasm';
import { DataSourceWithFile } from '@/src/io/import/dataSource';
import { pick, removeFromArray } from '../utils';
import { useImageStore } from './datasets-images';
import { useFileStore } from './datasets-files';
import { StateFile, DatasetType } from '../io/state-file/schema';
import { serializeData } from '../io/state-file/utils';
import { DICOMIO } from '../io/dicom';
export const ANONYMOUS_PATIENT = 'Anonymous';
export const ANONYMOUS_PATIENT_ID = 'ANONYMOUS';
export function imageCacheMultiKey(offset: number, asThumbnail: boolean) {
return `${offset}!!${asThumbnail}`;
}
export interface VolumeKeys {
patientKey: string;
studyKey: string;
volumeKey: string;
}
export interface PatientInfo {
PatientID: string;
PatientName: string;
PatientBirthDate: string;
PatientSex: string;
}
export interface StudyInfo {
StudyID: string;
StudyInstanceUID: string;
StudyDate: string;
StudyTime: string;
AccessionNumber: string;
StudyDescription: string;
}
export interface VolumeInfo {
NumberOfSlices: number;
VolumeID: string;
Modality: string;
SeriesInstanceUID: string;
SeriesNumber: string;
SeriesDescription: string;
WindowLevel: string;
WindowWidth: string;
}
interface State {
// volumeKey -> imageCacheMultiKey -> ITKImage
sliceData: Record<string, Record<string, Image>>;
// volumeKey -> imageID
volumeToImageID: Record<string, string | undefined>;
// imageID -> volumeKey
imageIDToVolumeKey: Record<string, string>;
// volume invalidation information
needsRebuild: Record<string, boolean>;
// patientKey -> patient info
patientInfo: Record<string, PatientInfo>;
// patientKey -> array of studyKeys
patientStudies: Record<string, string[]>;
// studyKey -> study info
studyInfo: Record<string, StudyInfo>;
// studyKey -> array of volumeKeys
studyVolumes: Record<string, string[]>;
// volumeKey -> volume info
volumeInfo: Record<string, VolumeInfo>;
// parent pointers
// volumeKey -> studyKey
volumeStudy: Record<string, string>;
// studyKey -> patientKey
studyPatient: Record<string, string>;
}
const readDicomTags = (dicomIO: DICOMIO, file: File) =>
dicomIO.readTags(file, [
{ name: 'PatientName', tag: '0010|0010', strconv: true },
{ name: 'PatientID', tag: '0010|0020', strconv: true },
{ name: 'PatientBirthDate', tag: '0010|0030' },
{ name: 'PatientSex', tag: '0010|0040' },
{ name: 'StudyInstanceUID', tag: '0020|000d' },
{ name: 'StudyDate', tag: '0008|0020' },
{ name: 'StudyTime', tag: '0008|0030' },
{ name: 'StudyID', tag: '0020|0010', strconv: true },
{ name: 'AccessionNumber', tag: '0008|0050' },
{ name: 'StudyDescription', tag: '0008|1030', strconv: true },
{ name: 'Modality', tag: '0008|0060' },
{ name: 'SeriesInstanceUID', tag: '0020|000e' },
{ name: 'SeriesNumber', tag: '0020|0011' },
{ name: 'SeriesDescription', tag: '0008|103e', strconv: true },
{ name: 'WindowLevel', tag: '0028|1050' },
{ name: 'WindowWidth', tag: '0028|1051' },
]);
/**
* Trims and collapses multiple spaces into one.
* @param name
* @returns string
*/
const cleanupName = (name: string) => {
return name.trim().replace(/\s+/g, ' ');
};
export const getDisplayName = (info: VolumeInfo) => {
return (
cleanupName(info.SeriesDescription || info.SeriesNumber) ||
info.SeriesInstanceUID
);
};
export const useDICOMStore = defineStore('dicom', {
state: (): State => ({
sliceData: {},
volumeToImageID: {},
imageIDToVolumeKey: {},
patientInfo: {},
patientStudies: {},
studyInfo: {},
studyVolumes: {},
volumeInfo: {},
volumeStudy: {},
studyPatient: {},
needsRebuild: {},
}),
actions: {
async importFiles(datasets: DataSourceWithFile[]) {
if (!datasets.length) return [];
const dicomIO = this.$dicomIO;
const fileToDataSource = new Map(
datasets.map((ds) => [ds.fileSrc.file, ds])
);
const allFiles = [...fileToDataSource.keys()];
const volumeToFiles = await dicomIO.categorizeFiles(allFiles);
const fileStore = useFileStore();
// Link VolumeKey and DatasetFiles in fileStore
Object.entries(volumeToFiles).forEach(([volumeKey, files]) => {
const volumeDatasetFiles = files.map((file) => {
const source = fileToDataSource.get(file);
if (!source)
throw new Error('Did not match File with source DataSource');
return source;
});
fileStore.add(volumeKey, volumeDatasetFiles);
});
await Promise.all(
Object.entries(volumeToFiles).map(async ([volumeKey, files]) => {
// Read tags of first file
if (!(volumeKey in this.volumeInfo)) {
const tags = await readDicomTags(dicomIO, files[0]);
// TODO parse the raw string values
const patient = {
PatientID: tags.PatientID || ANONYMOUS_PATIENT_ID,
PatientName: tags.PatientName || ANONYMOUS_PATIENT,
PatientBirthDate: tags.PatientBirthDate || '',
PatientSex: tags.PatientSex || '',
};
const study = pick(
tags,
'StudyID',
'StudyInstanceUID',
'StudyDate',
'StudyTime',
'AccessionNumber',
'StudyDescription'
);
const volumeInfo = {
...pick(
tags,
'Modality',
'SeriesInstanceUID',
'SeriesNumber',
'SeriesDescription',
'WindowLevel',
'WindowWidth'
),
NumberOfSlices: files.length,
VolumeID: volumeKey,
};
this._updateDatabase(patient, study, volumeInfo);
}
// invalidate any existing volume
if (volumeKey in this.volumeToImageID) {
// buildVolume requestor uses this as a rebuild hint
this.needsRebuild[volumeKey] = true;
}
})
);
return Object.keys(volumeToFiles);
},
_updateDatabase(
patient: PatientInfo,
study: StudyInfo,
volume: VolumeInfo
) {
const patientKey = patient.PatientID;
const studyKey = study.StudyInstanceUID;
const volumeKey = volume.VolumeID;
if (!(patientKey in this.patientInfo)) {
this.patientInfo[patientKey] = patient;
this.patientStudies[patientKey] = [];
}
if (!(studyKey in this.studyInfo)) {
this.studyInfo[studyKey] = study;
this.studyVolumes[studyKey] = [];
this.studyPatient[studyKey] = patientKey;
this.patientStudies[patientKey].push(studyKey);
}
if (!(volumeKey in this.volumeInfo)) {
this.volumeInfo[volumeKey] = volume;
this.volumeStudy[volumeKey] = studyKey;
this.sliceData[volumeKey] = {};
this.studyVolumes[studyKey].push(volumeKey);
}
},
deleteVolume(volumeKey: string) {
const imageStore = useImageStore();
if (volumeKey in this.volumeInfo) {
const studyKey = this.volumeStudy[volumeKey];
delete this.volumeInfo[volumeKey];
delete this.sliceData[volumeKey];
delete this.volumeStudy[volumeKey];
if (volumeKey in this.volumeToImageID) {
const imageID = this.volumeToImageID[volumeKey]!;
imageStore.deleteData(imageID!);
delete this.volumeToImageID[volumeKey];
delete this.imageIDToVolumeKey[imageID];
}
removeFromArray(this.studyVolumes[studyKey], volumeKey);
if (this.studyVolumes[studyKey].length === 0) {
this.deleteStudy(studyKey);
}
}
},
deleteStudy(studyKey: string) {
if (studyKey in this.studyInfo) {
const patientKey = this.studyPatient[studyKey];
delete this.studyInfo[studyKey];
delete this.studyPatient[studyKey];
[...this.studyVolumes[studyKey]].forEach((volumeKey) =>
this.deleteVolume(volumeKey)
);
delete this.studyVolumes[studyKey];
removeFromArray(this.patientStudies[patientKey], studyKey);
if (this.patientStudies[patientKey].length === 0) {
this.deletePatient(patientKey);
}
}
},
deletePatient(patientKey: string) {
if (patientKey in this.patientInfo) {
delete this.patientInfo[patientKey];
[...this.patientStudies[patientKey]].forEach((studyKey) =>
this.deleteStudy(studyKey)
);
delete this.patientStudies[patientKey];
}
},
async serialize(stateFile: StateFile) {
const dataIDs = Object.keys(this.volumeInfo);
await serializeData(stateFile, dataIDs, DatasetType.DICOM);
},
async deserialize(files: DataSourceWithFile[]) {
return this.importFiles(files).then((volumeKeys) => {
if (volumeKeys.length !== 1) {
// Volumes are store individually so we should get one back.
throw new Error('Invalid state file.');
}
return volumeKeys[0];
});
},
// returns an ITK image object
async getVolumeSlice(
volumeKey: string,
sliceIndex: number,
asThumbnail = false
) {
const dicomIO = this.$dicomIO;
const fileStore = useFileStore();
const cacheKey = imageCacheMultiKey(sliceIndex, asThumbnail);
if (
volumeKey in this.sliceData &&
cacheKey in this.sliceData[volumeKey]
) {
return this.sliceData[volumeKey][cacheKey];
}
if (!(volumeKey in this.volumeInfo)) {
throw new Error(`Cannot find given volume key: ${volumeKey}`);
}
const volumeInfo = this.volumeInfo[volumeKey];
const numSlices = volumeInfo.NumberOfSlices;
if (sliceIndex < 1 || sliceIndex > numSlices) {
throw new Error(`Slice ${sliceIndex} is out of bounds`);
}
const volumeFiles = fileStore.getFiles(volumeKey);
if (!volumeFiles) {
throw new Error(`No files found for volume key: ${volumeKey}`);
}
const sliceFile = volumeFiles[sliceIndex - 1];
const itkImage = await dicomIO.getVolumeSlice(sliceFile, asThumbnail);
this.sliceData[volumeKey][cacheKey] = itkImage;
return itkImage;
},
// returns an ITK image object
async getVolumeThumbnail(volumeKey: string) {
const { NumberOfSlices } = this.volumeInfo[volumeKey];
const middleSlice = Math.ceil(NumberOfSlices / 2);
return this.getVolumeSlice(volumeKey, middleSlice, true);
},
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 fileStore = useFileStore();
const files = fileStore.getFiles(volumeKey);
if (!files) throw new Error('No files for volume key');
const image = vtkITKHelper.convertItkToVtkImage(
await dicomIO.buildImage(files)
);
const existingImageID = this.volumeToImageID[volumeKey];
if (existingImageID) {
imageStore.updateData(existingImageID, image);
} else {
const name = this.volumeInfo[volumeKey].SeriesInstanceUID;
const imageID = imageStore.addVTKImageData(name, image);
this.imageIDToVolumeKey[imageID] = volumeKey;
this.volumeToImageID[volumeKey] = imageID;
}
delete this.needsRebuild[volumeKey];
return image;
},
},
});