-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathdicom-web-store.ts
More file actions
318 lines (271 loc) · 9.44 KB
/
dicom-web-store.ts
File metadata and controls
318 lines (271 loc) · 9.44 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
import { computed, ref, watch } from 'vue';
import { useLocalStorage, UrlParams } from '@vueuse/core';
import { defineStore } from 'pinia';
import vtkURLExtract from '@kitware/vtk.js/Common/Core/URLExtract';
import { omit, remapKeys } from '@/src/utils';
import { fileToDataSource } from '@/src/io/import/dataSource';
import {
convertSuccessResultToDataSelection,
importDataSources,
} from '@/src/io/import/importDataSources';
import { useDICOMStore } from '@/src/store/datasets-dicom';
import { useMessageStore } from '@/src/store/messages';
import { useDicomMetaStore } from '@/src/store/dicom-web/dicom-meta-store';
import {
searchForStudies,
fetchSeries,
fetchInstanceThumbnail,
retrieveStudyMetadata,
retrieveSeriesMetadata,
parseUrl,
} from '@/src/core/dicom-web-api';
import { useViewStore } from '@/src/store/views';
const DICOM_WEB_URL_PARAM = 'dicomweb';
export type ProgressState = 'Remote' | 'Pending' | 'Error' | 'Done';
export interface VolumeProgress {
state: ProgressState;
loaded: number;
total: number;
}
interface Progress {
[name: string]: VolumeProgress;
}
export const isDownloadable = (progress?: VolumeProgress) =>
!progress || ['Pending', 'Done'].every((state) => state !== progress.state);
const fetchFunctions = {
host: searchForStudies,
studies: retrieveStudyMetadata,
series: retrieveSeriesMetadata,
};
const levelToFetchKey = {
studies: 'studyInstanceUID',
series: 'seriesInstanceUID',
};
const levelToMetaKey = {
studies: 'StudyInstanceUID',
series: 'SeriesInstanceUID',
};
type InitialDicomListFetchProgress = 'Idle' | 'Pending' | 'Done';
/**
* Collect DICOM data from DICOMWeb
*/
export const useDicomWebStore = defineStore('dicom-web', () => {
const { VITE_DICOM_WEB_NAME, VITE_DICOM_WEB_URL } = import.meta.env;
// GUI display name
const hostName = VITE_DICOM_WEB_NAME
? ref(VITE_DICOM_WEB_NAME)
: useLocalStorage<string>('dicomWebHostName', '');
// URL param overrides env var, which overrides local storage
const urlParams = vtkURLExtract.extractURLParameters() as UrlParams;
const dicomWebFromURLParam = urlParams[DICOM_WEB_URL_PARAM] as
| string
| undefined;
const hostConfig = dicomWebFromURLParam ?? VITE_DICOM_WEB_URL;
// Don't save URL param or env var to local storage. Only save user input.
const savedHost = useLocalStorage<string | null>('dicomWebHost', ''); // null if cleared by vuetify text input
const host = ref<string | null>(hostConfig ?? savedHost.value);
watch(savedHost, () => {
host.value = savedHost.value;
});
// Remove trailing slash and pull study/series IDs from URL
const parsedURL = computed(() => parseUrl(host.value ?? ''));
const cleanHost = computed(() => parsedURL.value.host);
const isConfigured = computed(() => !!cleanHost.value);
const fetchVolumeThumbnail = async (volumeKey: string) => {
const dicoms = useDicomMetaStore();
const volumeInfo = dicoms.volumeInfo[volumeKey];
const middleSlice = Math.floor(volumeInfo.NumberOfSlices / 2);
const middleInstance = dicoms.volumeInstances[volumeKey]
.map((instanceKey) => dicoms.instanceInfo[instanceKey])
.sort(
({ InstanceNumber: a }, { InstanceNumber: b }) => Number(a) - Number(b)
)[middleSlice];
const studyKey = dicoms.volumeStudy[volumeKey];
const studyInfo = dicoms.studyInfo[studyKey];
const instance = {
studyInstanceUID: studyInfo.StudyInstanceUID,
seriesInstanceUID: volumeInfo.SeriesInstanceUID,
sopInstanceUID: middleInstance.SopInstanceUID,
};
return fetchInstanceThumbnail(cleanHost.value, instance);
};
const fetchPatientMeta = async (patientKey: string) => {
const dicoms = useDicomMetaStore();
const series = await Promise.all(
dicoms.patientStudies[patientKey]
.map((studyKey) => dicoms.studyInfo[studyKey])
.map(async (studyMeta) => {
const seriesMetas = await retrieveStudyMetadata(cleanHost.value, {
studyInstanceUID: studyMeta.StudyInstanceUID,
});
return seriesMetas.map((seriesMeta) => ({
...studyMeta,
...seriesMeta,
}));
})
);
series.flat().forEach((instance) => dicoms.importMeta(instance));
};
const fetchVolumesMeta = async (volumeKeys: string[]) => {
const dicoms = useDicomMetaStore();
const series = await Promise.all(
volumeKeys.map(async (volumeKey) => {
const volumeMeta = dicoms.volumeInfo[volumeKey];
const studyMeta = dicoms.studyInfo[dicoms.volumeStudy[volumeKey]];
const instanceMetas = await retrieveSeriesMetadata(cleanHost.value, {
studyInstanceUID: studyMeta.StudyInstanceUID,
seriesInstanceUID: volumeMeta.SeriesInstanceUID,
});
return instanceMetas.map((instanceMeta) => ({
...studyMeta,
...volumeMeta,
...instanceMeta,
}));
})
);
series.flat().forEach((instance) => dicoms.importMeta(instance));
};
const volumes = ref({} as Progress);
const downloadVolume = async (volumeKey: string) => {
const dicoms = useDicomMetaStore();
if (!isDownloadable(volumes.value[volumeKey])) return;
volumes.value[volumeKey] = {
...volumes.value[volumeKey],
state: 'Pending',
loaded: 0,
total: 0,
};
const { SeriesInstanceUID: seriesInstanceUID } =
dicoms.volumeInfo[volumeKey];
const studyKey = dicoms.volumeStudy[volumeKey];
const { StudyInstanceUID: studyInstanceUID } = dicoms.studyInfo[studyKey];
const seriesInfo = { studyInstanceUID, seriesInstanceUID };
const progressCallback = ({ loaded, total }: ProgressEvent) => {
volumes.value[volumeKey] = {
...volumes.value[volumeKey],
loaded,
total,
};
};
try {
const files = await fetchSeries(
cleanHost.value,
seriesInfo,
progressCallback
);
if (!files) {
throw new Error('Could not fetch series');
}
const [loadResult] = await importDataSources(files.map(fileToDataSource));
if (!loadResult) {
throw new Error('Did not receive a load result');
}
if (loadResult.type === 'error') {
throw loadResult.error;
}
const selection = convertSuccessResultToDataSelection(loadResult);
useViewStore().setDataForAllViews(selection);
volumes.value[volumeKey] = {
...volumes.value[volumeKey],
state: 'Done',
};
} catch (error) {
const messageStore = useMessageStore();
messageStore.addError('Failed to load DICOM', { error: error as Error });
volumes.value[volumeKey] = {
...volumes.value[volumeKey],
state: 'Error',
loaded: 0,
};
}
};
const fetchDicomsProgress = ref<InitialDicomListFetchProgress>('Idle');
const fetchError = ref<undefined | unknown>(undefined);
// DICOMWeb DataBrowser components automatically expands panels if true
const linkedToStudyOrSeries = ref(false);
const fetchDicoms = async () => {
fetchDicomsProgress.value = 'Pending';
fetchError.value = undefined;
linkedToStudyOrSeries.value = false;
const dicoms = useDicomMetaStore();
dicoms.$reset();
if (!cleanHost.value) return;
const deepestLevel = Object.keys(
parsedURL.value
).pop() as keyof typeof fetchFunctions; // at least host key guaranteed to exist
linkedToStudyOrSeries.value =
deepestLevel === 'studies' || deepestLevel === 'series';
const fetchFunc = fetchFunctions[deepestLevel];
const urlIDs = omit(parsedURL.value, 'host');
const fetchOptions = remapKeys(urlIDs, levelToFetchKey);
try {
const fetchedMetas = await fetchFunc(
cleanHost.value,
fetchOptions as any
);
const urlMetaIDs = remapKeys(urlIDs, levelToMetaKey);
const fullMeta = fetchedMetas.map((fetchedMeta) => ({
...urlMetaIDs,
...fetchedMeta,
}));
fullMeta.forEach((instance) => dicoms.importMeta(instance));
} catch (error) {
fetchError.value = error;
console.error(error);
}
if (deepestLevel === 'series') {
const seriesID = Object.values(parsedURL.value).pop() as string;
downloadVolume(seriesID);
}
fetchDicomsProgress.value = 'Done';
};
const message = computed(() => {
if (fetchError.value)
return `Error fetching DICOMWeb data: ${fetchError.value}`;
const dicoms = useDicomMetaStore();
if (
fetchDicomsProgress.value === 'Done' &&
Object.values(dicoms.patientInfo).length === 0
)
return 'Found zero dicoms.';
return '';
});
// Safe to call in components' setup()
let lastFetchedHostUrl: string | null = '';
const fetchDicomsOnce = () => {
if (lastFetchedHostUrl !== host.value || message.value) {
lastFetchedHostUrl = host.value;
fetchDicoms();
}
};
const loadedDicoms = useDICOMStore();
loadedDicoms.$onAction(({ name, args, after }) => {
if (name !== 'deleteVolume') return;
after(() => {
const [loadedVolumeKey] = args;
const volumeKey = Object.keys(volumes.value).find((key) =>
loadedVolumeKey.startsWith(key)
);
if (volumeKey)
volumes.value[volumeKey] = {
...volumes.value[volumeKey],
state: 'Remote',
loaded: 0,
};
});
});
return {
host,
savedHost,
isConfigured,
hostName,
message,
volumes,
linkedToStudyOrSeries,
fetchDicomsOnce,
fetchPatientMeta,
fetchVolumesMeta,
fetchVolumeThumbnail,
downloadVolume,
};
});