This repository has been archived by the owner on May 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
LoadImage.m
723 lines (543 loc) · 24 KB
/
LoadImage.m
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
function image = LoadImage(path, name, planUID)
% LoadImage loads the reference CT and associated IVDT information from a
% specified TomoTherapy patient archive and plan UID. This function has
% currently been validated for version 3.X, 4.X and 5.X patient archives.
% This function calls FindIVDT to load the IVDT data if it is not found
% within the archive (as is the case for 3.X archives).
%
% The following variables are required for proper execution:
% path: path to the patient archive XML file
% name: name of patient XML file in path
% planUID: UID of plan to extract reference image from
%
% The following variables are returned upon succesful completion:
% image: structure containing the image data, dimensions, width,
% start coordinates, structure set UID, couch checksum and IVDT
%
% Below is an example of how this function is used:
%
% path = '/path/to/archive/';
% name = 'Anon_0001_patient.xml';
% planUID = '1.2.826.0.1.3680043.2.200.1688035198.217.40463.657';
% image = LoadImage(path, name, planUID);
%
% Author: Mark Geurts, mark.w.geurts@gmail.com
% Copyright (C) 2015 University of Wisconsin Board of Regents
%
% This program is free software: you can redistribute it and/or modify it
% under the terms of the GNU General Public License as published by the
% Free Software Foundation, either version 3 of the License, or (at your
% option) any later version.
%
% This program is distributed in the hope that it will be useful, but
% WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
% Public License for more details.
%
% You should have received a copy of the GNU General Public License along
% with this program. If not, see http://www.gnu.org/licenses/.
% Execute in try/catch statement
try
% Log start of image load and start timer
if exist('Event', 'file') == 2
Event(sprintf('Extracting reference image from %s for plan UID %s', ...
name, planUID));
tic;
end
% The patient XML is parsed using xpath class
import javax.xml.xpath.*
% Read in the patient XML and store the Document Object Model node to doc
doc = xmlread(fullfile(path, name));
% Initialize a new xpath instance to the variable factory
factory = XPathFactory.newInstance;
% Initialize a new xpath to the variable xpath
xpath = factory.newXPath;
% Store class UID
image.classUID = '1.2.840.10008.5.1.4.1.1.2';
%% Load patient demographics
% Search for patient XML object patientName
expression = ...
xpath.compile('//FullPatient/patient/briefPatient/patientName');
% Evaluate xpath expression and retrieve the results
nodeList = expression.evaluate(doc, XPathConstants.NODESET);
% If a patient name was found
if nodeList.getLength > 0
% Store the first returned value
subnode = nodeList.item(0);
% Set patient name
image.patientName = char(subnode.getFirstChild.getNodeValue);
% Otherwise, warn the user that patient info wasn't found
else
if exist('Event', 'file') == 2
Event(['Patient demographics could not be found. It is possible ', ...
'this is not a valid patient archive.'], 'ERROR');
else
error(['Patient demographics could not be found. It is possible ', ...
'this is not a valid patient archive.']);
end
end
% Search for patient XML object patientID
expression = ...
xpath.compile('//FullPatient/patient/briefPatient/patientID');
% Evaluate xpath expression and retrieve the results
nodeList = expression.evaluate(doc, XPathConstants.NODESET);
% If a patient ID was found
if nodeList.getLength > 0
% Store the first returned value
subnode = nodeList.item(0);
% Set patient ID
image.patientID = char(subnode.getFirstChild.getNodeValue);
end
% Search for patient XML object patientBirthDate
expression = ...
xpath.compile('//FullPatient/patient/briefPatient/patientBirthDate');
% Evaluate xpath expression and retrieve the results
nodeList = expression.evaluate(doc, XPathConstants.NODESET);
% If a birthdate was found
if nodeList.getLength > 0
% Store the first returned value
subnode = nodeList.item(0);
% If birthdate is not empty
if ~isempty(subnode.getFirstChild)
% Set patient birth date
image.patientBirthDate = ...
char(subnode.getFirstChild.getNodeValue);
end
end
% Search for patient XML object patientGender
expression = ...
xpath.compile('//FullPatient/patient/briefPatient/patientGender');
% Evaluate xpath expression and retrieve the results
nodeList = expression.evaluate(doc, XPathConstants.NODESET);
% If a gender was found
if nodeList.getLength > 0
% Store the first returned value
subnode = nodeList.item(0);
% Set patient sex
image.patientSex = char(subnode.getFirstChild.getNodeValue);
end
%% Load plan info
% Declare a new xpath search expression. Search for all plans
expression = ...
xpath.compile('//fullPlanDataArray/fullPlanDataArray');
% Evaluate xpath expression and retrieve the results
nodeList = expression.evaluate(doc, XPathConstants.NODESET);
% Loop through the results
for i = 1:nodeList.getLength
% Set a handle to the current result
node = nodeList.item(i-1);
%% Verify plan UID
% Search for procedure XML object databaseUID
subexpression = xpath.compile('plan/briefPlan/dbInfo/databaseUID');
% Evaluate xpath expression and retrieve the results
subnodeList = subexpression.evaluate(node, XPathConstants.NODESET);
% If a UID was found
if subnodeList.getLength > 0
% Store the first returned value
subnode = subnodeList.item(0);
else
% Otherwise, continue to next result
continue
end
% If the plan data array does not match the provided UID, continue to
% next result
if ~strcmp(char(subnode.getFirstChild.getNodeValue), planUID)
continue
end
%% Load IVDT
% Search for procedure XML object fullDoseIVDT
subexpression = xpath.compile('plan/fullDoseIVDT');
% Evaluate xpath expression and retrieve the results
subnodeList = subexpression.evaluate(node, XPathConstants.NODESET);
% Store the first returned value
subnode = subnodeList.item(0);
% Save the IVDT UID to the return structure as char array
image.fullDoseIVDT = ...
char(subnode.getFirstChild.getNodeValue);
% Search for full plan IVDT data array (works with 3.X archives)
subexpression = ...
xpath.compile('fullPlanIVDTDataArray/fullPlanIVDTDataArray');
% Evaluate xpath expression and retrieve the results
subnodeList = subexpression.evaluate(node, XPathConstants.NODESET);
% If a IVDT data was found within the patient archive
if subnodeList.getLength > 0
% Log action
if exist('Event', 'file') == 2
Event('Loading legacy IVDT data array from patient XML');
end
% Initialize IVDT array
image.ivdt = zeros(subnodeList.getLength, 1);
% Loop through IVDT entries
for j = 1:subnodeList.getLength
% Retrieve handle to this IVDT entry
subnode = subnodeList.item(j-1);
% Store numerical value
image.ivdt(j) = ...
str2double(subnode.getFirstChild.getNodeValue);
end
% Reshape IVDT array
image.ivdt = reshape(image.ivdt, [], 2);
% Otherwise, look within imaging archives using FindIVDT
else
% Load the IVDT
image.ivdt = FindIVDT(path, ...
image.fullDoseIVDT, 'TomoPlan');
end
%% Load patient position
% Search for procedure XML object patientPosition
subexpression = xpath.compile('plan/patientPosition');
% Evaluate xpath expression and retrieve the results
subnodeList = subexpression.evaluate(node, XPathConstants.NODESET);
% If a patient position was found
if subnodeList.getLength > 0
% Store the first returned value
subnode = subnodeList.item(0);
% Save patient position to return structure as char array
image.position = ...
char(subnode.getFirstChild.getNodeValue);
% Log position
if exist('Event', 'file') == 2
Event(['The patient position was identified as ', ...
image.position]);
end
% Otherwise, warn the user
elseif exist('Event', 'file') == 2
Event(sprintf('The patient position was not found in %s', name), ...
'WARN');
end
%% Load structure set UID
% Search for procedure XML object planStructureSetUID
subexpression = xpath.compile('plan/planStructureSetUID');
% Evaluate xpath expression and retrieve the results
subnodeList = subexpression.evaluate(node, XPathConstants.NODESET);
% If a structure set UID was found
if subnodeList.getLength > 0
% Store the first returned value
subnode = subnodeList.item(0);
% Save structure set UID to return structure as char array
image.structureSetUID = ...
char(subnode.getFirstChild.getNodeValue);
end
%% Load couch checksum and insertion position
% Search for procedure XML object couchChecksum
subexpression = xpath.compile('plan/couchChecksum');
% Evaluate xpath expression and retrieve the results
subnodeList = subexpression.evaluate(node, XPathConstants.NODESET);
% If a couch checksum was found
if subnodeList.getLength > 0
% Store the first returned value
subnode = subnodeList.item(0);
% Save the couch checksum to return structure as char array
image.couchChecksum = ...
char(subnode.getFirstChild.getNodeValue);
end
% Search for procedure XML object couchInsertionPosition
subexpression = xpath.compile('plan/couchInsertionPosition');
% Evaluate xpath expression and retrieve the results
subnodeList = subexpression.evaluate(node, XPathConstants.NODESET);
% If a couch insertion position was found
if subnodeList.getLength > 0
% Store the first returned value
subnode = subnodeList.item(0);
% Save the couch checksum to return structure as char array
image.couchInsertionPosition = ...
char(subnode.getFirstChild.getNodeValue);
end
%% Load plan date/time
% Search for plan modification date
subexpression = ...
xpath.compile('plan/briefPlan/modificationTimestamp/date');
% Evaluate xpath expression and retrieve the results
subnodeList = subexpression.evaluate(node, XPathConstants.NODESET);
% Retrieve a handle to the results
subnode = subnodeList.item(0);
% Store the plan date
d = char(subnode.getFirstChild.getNodeValue);
% Search for plan modification time
subexpression = ...
xpath.compile('plan/briefPlan/modificationTimestamp/time');
% Evaluate xpath expression and retrieve the results
subnodeList = subexpression.evaluate(node, XPathConstants.NODESET);
% Retrieve a handle to the results
subnode = subnodeList.item(0);
% Store the plan time
t = char(subnode.getFirstChild.getNodeValue);
% Store the date and time as a timestamp
image.timestamp = datenum([d,'-',t], 'yyyymmdd-HHMMSS');
%% Load associated plan trial
% Search for procedure XML object approvedPlanTrialUID
subexpression = xpath.compile('plan/briefPlan/approvedPlanTrialUID');
% Evaluate xpath expression and retrieve the results
subnodeList = subexpression.evaluate(node, XPathConstants.NODESET);
% If a couch checksum was found
if subnodeList.getLength > 0
% Store the first returned value
subnode = subnodeList.item(0);
% Save the plan trial to return structure as char array
image.planTrialUID = ...
char(subnode.getFirstChild.getNodeValue);
% Otherwise, log event warning
elseif exist('Event', 'file') == 2
Event(sprintf(['An approved plan trial UID this image set was not', ...
' found in %s'], name), 'WARN');
end
% Declare a new xpath search expression. Search for all plan trials
subexpression = xpath.compile(['fullPlanTrialArray/', ...
'fullPlanTrialArray/patientPlanTrial']);
% Evaluate xpath expression and retrieve the results
subnodeList = subexpression.evaluate(node, XPathConstants.NODESET);
% Loop through the plan trials
for j = 1:subnodeList.getLength
% Retrieve handle to this image
subnode = subnodeList.item(j-1);
%% Verify plan trial UID
% Search for procedure XML object databaseUID
subsubexpression = xpath.compile('dbInfo/databaseUID');
% Evaluate xpath expression and retrieve the results
subsubnodeList = ...
subsubexpression.evaluate(subnode, XPathConstants.NODESET);
% If a UID was found
if subsubnodeList.getLength > 0
% Store the first returned value
subsubnode = subsubnodeList.item(0);
else
% Otherwise, continue to next result
continue
end
% If the plan data array does not match the provided UID, continue to
% next result
if ~strcmp(char(subsubnode.getFirstChild.getNodeValue), ...
image.planTrialUID)
continue
end
%% Load reference image isocenter
% Search for procedure XML object referenceImageIsocenter
subsubexpression = xpath.compile('referenceImageIsocenter/x');
% Evaluate xpath expression and retrieve the results
subsubnodeList = ...
subsubexpression.evaluate(subnode, XPathConstants.NODESET);
% If an isocenter X was found
if subsubnodeList.getLength > 0
% Store the first returned value
subsubnode = subsubnodeList.item(0);
% Save the isocenter X to return structure as char array
image.isocenter(1) = ...
str2double(subsubnode.getFirstChild.getNodeValue);
end
% Search for procedure XML object referenceImageIsocenter
subsubexpression = xpath.compile('referenceImageIsocenter/y');
% Evaluate xpath expression and retrieve the results
subsubnodeList = ...
subsubexpression.evaluate(subnode, XPathConstants.NODESET);
% If an isocenter Y was found
if subsubnodeList.getLength > 0
% Store the first returned value
subsubnode = subsubnodeList.item(0);
% Save the isocenter Y to return structure as char array
image.isocenter(2) = ...
str2double(subsubnode.getFirstChild.getNodeValue);
end
% Search for procedure XML object referenceImageIsocenter
subsubexpression = xpath.compile('referenceImageIsocenter/z');
% Evaluate xpath expression and retrieve the results
subsubnodeList = ...
subsubexpression.evaluate(subnode, XPathConstants.NODESET);
% If an isocenter Z was found
if subsubnodeList.getLength > 0
% Store the first returned value
subsubnode = subsubnodeList.item(0);
% Save the isocenter Z to return structure as char array
image.isocenter(3) = ...
str2double(subsubnode.getFirstChild.getNodeValue);
end
% Reference plan trial was found, so exit loop
break;
end
%% Load associated image
% Search for associated images
subexpression = ...
xpath.compile('fullImageDataArray/fullImageDataArray/image');
% Evaluate xpath expression and retrieve the results
subnodeList = subexpression.evaluate(node, XPathConstants.NODESET);
% Loop through the images
for j = 1:subnodeList.getLength
% Retrieve handle to this image
subnode = subnodeList.item(j-1);
% Check if image type is KVCT, otherwise continue
subsubexpression = xpath.compile('imageType');
% Evaluate xpath expression and retrieve the results
subsubnodeList = ...
subsubexpression.evaluate(subnode, XPathConstants.NODESET);
% Store the first returned value
subsubnode = subsubnodeList.item(0);
% If this image is not a KVCT/Adapt image, continue to next subnode
if strcmp(char(subsubnode.getFirstChild.getNodeValue), ...
'KVCT') == 0 && ...
strcmp(char(subsubnode.getFirstChild.getNodeValue), ...
'Registered_MVCT') == 0
continue
end
% Inform user that image data was found
if exist('Event', 'file') == 2
Event(sprintf('Image data identified for plan UID %s', ...
planUID));
end
%% Load CT filename
% Search for path to ct image
subsubexpression = xpath.compile('arrayHeader/binaryFileName');
% Evaluate xpath expression and retrieve the results
subsubnodeList = ...
subsubexpression.evaluate(subnode, XPathConstants.NODESET);
% Store the first returned value
subsubnode = subsubnodeList.item(0);
% Store filename with a path to the binary KVCT data
image.filename = fullfile(path, ...
char(subsubnode.getFirstChild.getNodeValue));
%% Load frame of reference UID
% Search for path to ct image
subsubexpression = xpath.compile('frameOfReference');
% Evaluate xpath expression and retrieve the results
subsubnodeList = ...
subsubexpression.evaluate(subnode, XPathConstants.NODESET);
% Store the first returned value
subsubnode = subsubnodeList.item(0);
% Store frameRefUID
image.frameRefUID = ...
char(subsubnode.getFirstChild.getNodeValue);
%% Load image dimensions
% Search for x dimensions of image
subsubexpression = xpath.compile('arrayHeader/dimensions/x');
% Evaluate xpath expression and retrieve the results
subsubnodeList = ...
subsubexpression.evaluate(subnode, XPathConstants.NODESET);
% Store the first returned value
subsubnode = subsubnodeList.item(0);
% Store x dimensions to return structure
image.dimensions(1) = ...
str2double(subsubnode.getFirstChild.getNodeValue);
% Search for y dimensions of image
subsubexpression = xpath.compile('arrayHeader/dimensions/y');
% Evaluate xpath expression and retrieve the results
subsubnodeList = ...
subsubexpression.evaluate(subnode, XPathConstants.NODESET);
% Store the first returned value
subsubnode = subsubnodeList.item(0);
% Store y dimensions to return structure
image.dimensions(2) = ...
str2double(subsubnode.getFirstChild.getNodeValue);
% Search for z dimensions of image
subsubexpression = xpath.compile('arrayHeader/dimensions/z');
% Evaluate xpath expression and retrieve the results
subsubnodeList = ...
subsubexpression.evaluate(subnode, XPathConstants.NODESET);
% Store the first returned value
subsubnode = subsubnodeList.item(0);
% Store z dimensions to return structure
image.dimensions(3) = ...
str2double(subsubnode.getFirstChild.getNodeValue);
%% Load start coordinates
% Search for the x coordinate of the first voxel
subsubexpression = xpath.compile('arrayHeader/start/x');
% Evaluate xpath expression and retrieve the results
subsubnodeList = ...
subsubexpression.evaluate(subnode, XPathConstants.NODESET);
% Store the first returned value
subsubnode = subsubnodeList.item(0);
% Store x start coordinate (in cm) to return structure
image.start(1) = ...
str2double(subsubnode.getFirstChild.getNodeValue);
% Search for the y coordinate of the first voxel
subsubexpression = xpath.compile('arrayHeader/start/y');
% Evaluate xpath expression and retrieve the results
subsubnodeList = ...
subsubexpression.evaluate(subnode, XPathConstants.NODESET);
% Store the first returned value
subsubnode = subsubnodeList.item(0);
% Store y start coordinate (in cm) to return structure
image.start(2) = ...
str2double(subsubnode.getFirstChild.getNodeValue);
% Search for the z coordinate of the first voxel
subsubexpression = xpath.compile('arrayHeader/start/z');
% Evaluate xpath expression and retrieve the results
subsubnodeList = ...
subsubexpression.evaluate(subnode, XPathConstants.NODESET);
% Store the first returned value
subsubnode = subsubnodeList.item(0);
% Store z start coordinate (in cm) to return structure
image.start(3) = ...
str2double(subsubnode.getFirstChild.getNodeValue);
%% Load voxel widths
% Search for the voxel size in the x direction
subsubexpression = xpath.compile('arrayHeader/elementSize/x');
% Evaluate xpath expression and retrieve the results
subsubnodeList = ...
subsubexpression.evaluate(subnode, XPathConstants.NODESET);
% Store the first returned value
subsubnode = subsubnodeList.item(0);
% Store x voxel width (in cm) to return structure
image.width(1) = ...
str2double(subsubnode.getFirstChild.getNodeValue);
% Search for the voxel size in the y direction
subsubexpression = xpath.compile('arrayHeader/elementSize/y');
% Evaluate xpath expression and retrieve the results
subsubnodeList = ...
subsubexpression.evaluate(subnode, XPathConstants.NODESET);
% Store the first returned value
subsubnode = subsubnodeList.item(0);
% Store y voxel width (in cm) to return structure
image.width(2) = ...
str2double(subsubnode.getFirstChild.getNodeValue);
% Search for the voxel size in the z dimension
subsubexpression = xpath.compile('arrayHeader/elementSize/z');
% Evaluate xpath expression and retrieve the results
subsubnodeList = ...
subsubexpression.evaluate(subnode, XPathConstants.NODESET);
% Store the first returned value
subsubnode = subsubnodeList.item(0);
% Store z voxel width (in cm) to return structure
image.width(3) = ...
str2double(subsubnode.getFirstChild.getNodeValue);
% Reference image was found, so exit loop
break;
end
% Plan has been found, so exit loop
break;
end
% If a filename does not exist
if ~isfield(image, 'filename')
% Throw an error
if exist('Event', 'file') == 2
Event(sprintf(['An associated image filename was not found ', ...
'for UID %s'], planUID), 'ERROR');
else
error('An associated image filename was not found for UID %s', ...
planUID);
end
end
%% Load the planned image array
% Open read file handle to binary image
fid = fopen(image.filename, 'r', 'b');
% Read in and store unsigned int binary data, reshaping by image dimensions
image.data = single(reshape(fread(fid, image.dimensions(1) * ...
image.dimensions(2) * image.dimensions(3), 'uint16'), ...
image.dimensions(1), image.dimensions(2), ...
image.dimensions(3)));
% Close file handle
fclose(fid);
% Clear temporary variables
clear fid i j node subnode subsubnode nodeList subnodeList subsubnodeList ...
expression subexpression subsubexpression doc factory xpath d t;
% Log conclusion of image loading
if exist('Event', 'file') == 2
Event(sprintf(['Reference binary image loaded successfully in %0.3f ', ...
'seconds with dimensions (%i, %i, %i) '], toc, ...
image.dimensions(1), image.dimensions(2), image.dimensions(3)));
end
% Catch errors, log, and rethrow
catch err
if exist('Event', 'file') == 2
Event(getReport(err, 'extended', 'hyperlinks', 'off'), 'ERROR');
else
rethrow(err);
end
end