-
Notifications
You must be signed in to change notification settings - Fork 2
/
XIPline_Code_Script.m
374 lines (312 loc) · 12.7 KB
/
XIPline_Code_Script.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
%This script is intended for debugging, developing, and testing new tools before implementing them in the main interface.
% Package: https://github.com/aboodbdaiwi/HP129Xe_Analysis_App
%
% Author: Abdullah S. Bdaiwi
% Work email: abdullah.bdaiwi@cchmc.org
% Personal email: abdaiwi89@gmail.com
% Website: https://www.cincinnatichildrens.org/research/divisions/c/cpir
%
% Please add updates at the end. Ex: 3/10/24 - ASB: update ....
%% calibration
[filename, path] = uigetfile('*.*','Select xenon data file');
XeFullPath = [path,filename];
XeDataLocation = path(1:end-1);
[~,~,xe_ext] = fileparts(XeFullPath);
MainInput.XeFullPath = XeFullPath;
MainInput.XeDataLocation = XeDataLocation;
MainInput.XeFileName = filename;
MainInput.XeDataext = xe_ext;
cd(MainInput.XeDataLocation)
% [GasExResults, CalResults] = Calibration.XeCTC_Calibration(MainInput);
[GasExResults, CalResults] = Calibration.XeCTC_Calibration_MRD(MainInput);
% the rest of the calculations is in the main interface
%% Image analysis
clc; clear; close all; % start with fresh variables, etc.
MainInput.Institute = '';
MainInput.Scanner = '';
MainInput.ScannerSoftware = '';
MainInput.SequenceType = '';
MainInput.XeDataLocation = '';
MainInput.XeDataType = '';
MainInput.NoProtonImage = '';
MainInput.HDataLocation = '';
MainInput.HDataType = '';
MainInput.AnalysisType = '';
MainInput.SubjectAge = [];
MainInput.RegistrationType = '';
% add patinet info
MainInput.PatientInfo = '';
% 1) choose the type of analysis
MainInput.AnalysisType = 'GasExchange'; % 'Ventilation', 'Diffusion', 'GasExchange'
% 2) Do you have protom images?
MainInput.NoProtonImage = 0; % 1: There is no proton images % 0: There is proton images
MainInput.Institute = 'CCHMC'; % 'CCHMC', 'XeCTC', 'Duke'
MainInput.Scanner = 'Philips'; % Siemens, Philips
MainInput.ScannerSoftware = '5.9.0'; % '5.3.1', '5.6.1','5.9.0'
MainInput.SequenceType = '2D GRE'; % '2D GRE', '3D Radial'
MainInput.denoiseXe= 'no';
% diary Log.txt
[filename, path] = uigetfile('*.*','Select xenon data file');
XeFullPath = [path,filename];
XeDataLocation = path(1:end-1);
[~,Xe_name,xe_ext] = fileparts(XeFullPath);
MainInput.XeFullPath = XeFullPath;
MainInput.XeDataLocation = XeDataLocation;
MainInput.XeFileName = filename;
MainInput.Xe_name = Xe_name;
MainInput.XeDataext = xe_ext;
cd(MainInput.XeDataLocation)
% select calibration
if strcmp(MainInput.Institute,'XeCTC') && (strcmp(MainInput.XeDataext,'.dat') || strcmp(MainInput.XeDataext,'.p'))
[filename, path] = uigetfile('*.*','Select calibration data file');
CalFullPath = [path,filename];
CalDataLocation = path(1:end-1);
[~,Cal_name,Cal_ext] = fileparts(CalFullPath);
MainInput.CalFullPath = CalFullPath;
MainInput.CalDataLocation = CalDataLocation;
MainInput.CalFileName = filename;
MainInput.Cal_name = Cal_name;
MainInput.CalDataext = Cal_ext;
end
% select proton
if MainInput.NoProtonImage == 0
[filename, path] = uigetfile('*.*','Select proton data file');
HFullPath = [path,filename];
HDataLocation = path(1:end-1);
[~,H_name,H_ext] = fileparts(HFullPath);
MainInput.HFullPath = HFullPath;
MainInput.HDataLocation = HDataLocation;
MainInput.HFileName = filename;
MainInput.H_name = H_name;
MainInput.HDataext = H_ext;
end
[Ventilation, Diffusion, GasExchange, Proton] = LoadData.LoadReadData(MainInput);
if strcmp(MainInput.AnalysisType,'Ventilation') == 1
figure; Global.imslice(Ventilation.Image)
elseif strcmp(MainInput.AnalysisType,'Diffusion') == 1
figure; Global.imslice(Diffusion.Image)
elseif strcmp(MainInput.AnalysisType,'GasExchange') == 1
figure; Global.imslice(GasExchange.VentImage)
end
if MainInput.NoProtonImage == 0
figure; Global.imslice(Proton.Image)
end
% Ventilation.Image = flipdim(Ventilation.Image,3);
% Ventilation.Image = permute(Ventilation.Image,[3 2 1]); %
% Rotated = imrotate(Diffusion.Image,-90);
%% Registration
% Move from 1st to 3rd dim and flip Z (optional)
% Prot_Test = permute(Ventilation.Image,[3 2 1]); %
% Test3 = flip(Prot_Test,1);
% Ventilation.Image = flipdim(Test3,3);
% figure; imslice(Ventilation.Image)
% Flip Z of Vent and Proton (optional)
% Ventilation.Image = flipdim(Ventilation.Image,3);
% Proton.Image = flipdim(Proton.Image,3);
% Ventilation.LungMask = flipdim(Ventilation.LungMask,3);
% preform registration
clc
cd(MainInput.XeDataLocation)
%Proton.Image = P_image; % Use only for interpulation
%
% diary LogFile_LoadingData
MainInput.RegistrationType = 'affine'; % 'translation' | 'rigid' | 'similarity' | 'affine'
% enable this code in case number of slices is different between xe and H
MainInput.SliceSelection = 0;
if MainInput.SliceSelection == 1
MainInput.Xestart = 1;
MainInput.Xeend = 13;
MainInput.Hstart = 1;
MainInput.Hend = 10;
end
if strcmp(MainInput.AnalysisType, 'Ventilation')
Xesize = size(Ventilation.Image);
Hsize = size(Proton.Image);
sizeRatio = Hsize./Xesize;
elseif strcmp(MainInput.AnalysisType, 'GasExchange')
Xesize = size(GasExchange.VentImage);
Hsize = size(Proton.Image);
sizeRatio = Hsize./Xesize;
end
useRatio = 1;
if useRatio == 1
MainInput.XeVoxelInfo.PixelSize1 = sizeRatio(1);
MainInput.XeVoxelInfo.PixelSize2 = sizeRatio(2);
MainInput.XeVoxelInfo.SliceThickness = sizeRatio(3);
else % manual
MainInput.XeVoxelInfo.PixelSize1 = 1.13; %Y
MainInput.XeVoxelInfo.PixelSize2 = 1.13; %X
MainInput.XeVoxelInfo.SliceThickness = 1;
end
MainInput.ProtonVoxelInfo.PixelSize1 = 1;
MainInput.ProtonVoxelInfo.PixelSize2 = 1;
MainInput.ProtonVoxelInfo.SliceThickness = 1;
[Proton] = Registration.PerformRegistration(Proton,Ventilation,GasExchange,MainInput);
viewing_img = Proton.ProtonRegisteredColored;
S = orthosliceViewer(viewing_img,...
'DisplayRange', [min(viewing_img(:)) max(viewing_img(:))]);
%% Load Mask (.nii or .gz)
% clc
% [filename, path] = uigetfile('*.*','Select proton data file');
% cd(path)
% Mask = LoadData.load_nii(filename);
% A = Mask.img;
% A = permute(A,[1 3 2]);
% A = double(squeeze(A));
% A = imrotate(A,-90);
% A = flip(A,2);
% Mask = A;
% figure; Global.imslice(Mask)
%% segmenting
clc
cd(MainInput.XeDataLocation)
% diary Log.txt
MainInput.SegmentationMethod = 'Auto'; % 'Threshold' || 'Manual' || 'Auto'
MainInput.SegmentAnatomy = 'Parenchyma'; % 'Airway'; || 'Parenchyma'
MainInput.Imagestosegment = 'Xenon'; % 'Xe & Proton Registered' | 'Xenon' | 'Registered Proton'
MainInput.thresholdlevel = 0.6; % 'threshold'
MainInput.SE = 1;
MainInput.SegmentManual = 'Freehand'; % 'AppSegmenter' || 'Freehand'
MainInput.SliceOrientation = 'coronal'; % 'coronal' ||'transversal' || 'sagittal' ||'isotropic'
[Proton,Ventilation,Diffusion,GasExchange] = Segmentation.PerformSegmentation(Proton,Ventilation,Diffusion,GasExchange,MainInput);
% create vessle mask
if strcmp(MainInput.AnalysisType,'Ventilation')
MainInput.SegmentVessels = 1; % 0 || 1
elseif strcmp(MainInput.AnalysisType,'Diffusion')
MainInput.SegmentVessels = 0; % 0 || 1
elseif strcmp(MainInput.AnalysisType,'GasExchange')
MainInput.SegmentVessels = 0; % 0 || 1
end
MainInput.vesselImageMode = 'xenon'; % xenon || proton
MainInput.SliceOrientation = 'coronal';
switch MainInput.vesselImageMode
case 'xenon'
MainInput.frangi_thresh = 0.25;
case 'proton'
MainInput.frangi_thresh = 0.2; % you can change this threshold to increase or decrease the mask
end
if MainInput.SegmentVessels == 1
[Ventilation] = Segmentation.Vasculature_filter(Proton, Ventilation, MainInput);
else
Ventilation.VesselMask = zeros(size(Ventilation.Image));
Ventilation.vessel_stack = zeros(size(Ventilation.Image));
end
maskarray = double(Ventilation.LungMask + Ventilation.VesselMask);
maskarray(maskarray > 1) = 0;
Ventilation.LungMask = double(maskarray);
figure; Global.imslice(Ventilation.VesselMask)
figure; Global.imslice(Ventilation.LungMask)
if ~isfield(Diffusion, 'AirwayMask')
disp('Diffusion.AirwayMask does not exist');
else
disp('Diffusion.AirwayMask exists');
end
if ~exist('Diffusion.AirwayMask','var')
disp('airway mask not found')
else
disp('airway mask found')
end
%% Ventilation analysis
clc
close all;
cd(MainInput.XeDataLocation)
% diary LogFile_LoadingData
Ventilation.N4Analysis = 1;
Ventilation.IncompleteThresh = 60;
Ventilation.RFCorrect = 0;
Ventilation.CompleteThresh = 15;
Ventilation.HyperventilatedThresh = 200;
Ventilation.HeterogeneityIndex = 'yes';
Ventilation.ThreshAnalysis = 'yes'; % 'yes'; || 'no'
Ventilation.LB_Analysis = 'yes'; % 'yes'; || 'no'
Ventilation.LB_Normalization = 'percentile'; % 'mean'; || 'median' || 'percentile'
Ventilation.Kmeans = 'yes'; % 'yes'; || 'no'
Ventilation.AKmeans = 'yes'; % 'yes'; || 'no'
Ventilation.DDI2D = 'no'; % 'yes'; || 'no'
Ventilation.DDI3D = 'no'; % 'yes'; || 'no'
Ventilation.ImageResolution = [3, 3, 15];
MainInput.PixelSpacing = Ventilation.ImageResolution(1:2);
MainInput.SliceThickness = Ventilation.ImageResolution(3);
Ventilation.DDIDefectMap = 'Threshold'; % 'Threshold'; || 'Linear Binning' || 'Kmeans'
Ventilation.GLRLM_Analysis = 'no'; % 'yes'; || 'no'
switch Ventilation.LB_Normalization
case 'mean'
Ventilation.Thresholds = [0.609909, 0.827439, 0.998199, 1.143472, 1.27208]; % median
Ventilation.Hdist = [-2.100652, 1.154155, 0.238074];
case 'median'
Ventilation.Thresholds = [0.609909, 0.827439, 0.998199, 1.143472, 1.27208]; % median
Ventilation.Hdist = [-2.100652, 1.154155, 0.238074];
case 'percentile'
Ventilation.Thresholds = [0.448181, 0.621903, 0.752298, 0.860778, 0.955443]; % percentile
Ventilation.Hdist = [-2.515712, 0.87849, 0.188364];
end
[Ventilation] = VentilationFunctions.Ventilation_Analysis(Ventilation, Proton, MainInput);
%% Diffusion analysis
clc
cd(MainInput.XeDataLocation)
% diary Log.txt
MainInput.PatientAge = '17';
Diffusion.ADCFittingType = 'Log Linear'; % 'Log Weighted Linear' | 'Log Linear' | 'Non-Linear' | 'Bayesian'
Diffusion.ADCAnalysisType = 'human'; % human | animals; % human | animals
Diffusion.bvalues = '[0, 6.25, 12.5, 18.75, 25]';
% Diffusion.bvalues = '[0, 7.5, 15]';
% Diffusion.bvalues = '[0, 10, 20, 30]'; % for T-Scanner
Diffusion.ADCLB_Analysis = 'yes'; % 'yes'; || 'no'
Diffusion.ADCLB_RefMean = '0.0002*age+0.029';
Diffusion.ADCLB_RefSD = '5e-5*age+0.0121';
Diffusion.MorphometryAnalysis = 'yes'; % yes || no
Diffusion.MorphometryAnalysisType = 'human'; % human | animals
Diffusion.MA_WinBUGSPath = 'C:\Users\BAS8FL\Desktop\WinBUGS14';
Diffusion.CMMorphometry = 'yes'; % yes || no
Diffusion.SEMMorphometry = 'yes'; % yes || no
Diffusion.Do = 0.14; % cm2/s
Diffusion.Delta = 3.5; % ms
[Diffusion] = DiffusionFunctions.Diffusion_Analysis(Diffusion,MainInput);
% diary off
%% Gas Exchange analysis
clc;
cd(MainInput.XeDataLocation)
% diary Log.txt
MainInput.ImportHealthyCohort = 0; % 0: import CCHMC healthy Ref., 1: import .mat H. Ref., 2: enter values manually
if MainInput.ImportHealthyCohort == 0
MainInput.HealthyReferenceType = 'Default';
elseif MainInput.ImportHealthyCohort == 1
GasExchange.ImportHealthyCohort = 'yes';
[filename, path] = uigetfile('*.mat*','Import Healthy Cohort');
GasExchange.HealthyCohortFullPath = [path,filename];
GasExchange.HealthyCohortDataLocation = path(1:end-1);
[~,~,HealthyCohort_ext] = fileparts(GasExchange.HealthyCohortFullPath);
GasExchange.HealthyCohortFileName = filename;
GasExchange.HealthyCohort_ext = HealthyCohort_ext;
MainInput.HealthyReferenceType = 'Import';
elseif MainInput.ImportHealthyCohort == 2
GasExchange.VentHealthyMean = 0.51; GasExchange.VentHealthyStd = 0.19;%6
GasExchange.DissolvedHealthyMean = 0.0075; GasExchange.DissolvedHealthyStd = 0.00125;%6
GasExchange.BarrierHealthyMean = 0.0049; GasExchange.BarrierHealthyStd = 0.0015;%6
GasExchange.RBCHealthyMean = 0.0026; GasExchange.RBCtHealthyStd = 0.0010;%6
GasExchange.RBCBarrHealthyMean = 0.53; GasExchange.RBCBarrHealthyStd = 0.18;%6
GasExchange.RBCOscHealthyMean = 8.9596; GasExchange.RBCOscHealthyStd = 10.5608;%6
MainInput.HealthyReferenceType = 'Manual';
end
[GasExchange] = GasExchangeFunctions.GasExchange_Analysis(GasExchange,Proton,MainInput);
disp('Analysis done')
% diary off
%% patient report
clc
MainInput.studyID = '740H';
MainInput.studyType = 'CF';
MainInput.patientID = '001';
MainInput.scanDate = '7/6/2023';
MainInput.xeDoseVolume = '1000';
MainInput.PatientName = 'test test';
MainInput.MRMnumber = '1201201';
MainInput.sex = 'M';
MainInput.age = '25';
MainInput.height = '180';
MainInput.weight = '200';
MainInput.summaryofFindings = 'all good';
MainInput.notes = 'all good';
MainInput.dataAnalyst = 'ASB';
MainInput.processingDate = '7/6/2023';
Global.PatientReport(MainInput)
%%