-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQModeling.m
1955 lines (1580 loc) · 85.3 KB
/
QModeling.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
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function varargout = QModeling(varargin)
% QMODELING MATLAB code for QModeling.fig
%
% QModeling v 1.8 is an open source program, developed as a toolbox for SPM
% (Stadistical Parametric Imaging), to make kinetic analysis for PET studies
% based on compartmental models.
%---------------------------------------------------------------------------------
% QModeling 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.
%
% QModeling 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 QModeling. If not, see <http://www.gnu.org/licenses/>.
%---------------------------------------------------------------------------------
% Copyright (C) 2014, 2018 Francisco Javier López González, Jose Paredes Pacheco,
% Karl-Khader Thurnhofer Hemsi, Núria Roé Vellvé, Antonio L. Gutierrez
% Cardo, Manuel Enciso Garcia Oliveros, Carlos Rossi Jimenez
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @QModeling_OpeningFcn, ...
'gui_OutputFcn', @QModeling_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before QModeling is made visible.
function QModeling_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to QModeling (see VARARGIN)
startQM()
global QMmaindata
global QMlastPreprocess
global QMpath
global QMf_logfile
%We save the temporary folder path
actualpath = mfilename('fullpath');
QMpath = actualpath(1:end-10);
if exist(strcat(QMpath,filesep,'temp'),'dir') ~= 7
mkdir(QMpath,'temp');
end
%Create/open and initialize the log file
QM_initiateLogFile();
fprintf(QMf_logfile,strcat('\n','----------------------------------------------','\n'));
fprintf(QMf_logfile,date);
fprintf(QMf_logfile,strcat('\n','----------------------------------------------','\n'));
aux_date=datestr(now);
fprintf(QMf_logfile,strcat(aux_date(end-7:end),' Starting QModeling \n'));
%Add folders to path
addpath(QMpath,strcat(QMpath,filesep,'lib'),...
strcat(QMpath,filesep,'lib',filesep,'nifti'),...
strcat(QMpath,filesep,'lib',filesep,'wintools'),...
strcat(QMpath,filesep,'lib',filesep,'CopyPaste'),...
strcat(QMpath,filesep,'models'),...
strcat(QMpath,filesep,'models',filesep,'SRTM'),...
strcat(QMpath,filesep,'models',filesep,'SRTM2'),...
strcat(QMpath,filesep,'models',filesep,'PatlakRef'),...
strcat(QMpath,filesep,'models',filesep,'LoganPlot'),...
strcat(QMpath,filesep,'models',filesep,'TwoTCM'),...
strcat(QMpath,filesep,'icons'),...
strcat(QMpath,filesep,'help'),...
strcat(QMpath,filesep,'help',filesep,'html'),...
strcat(QMpath,filesep,'command_line'));
%Publish focus function
handles.setFocus = @setPanelFocus;
handles.deleteTempImages=@deleteTempImages;
% Choose default command line output for QModeling
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
format long
%Define global variable which saves main data for the toolbox working
QMmaindata = struct('num_files',0,'times',0,'Cpet',0,'Crois',0,'idCt',1,'idCr',2,'flags',struct('consistency',0,'minWindow',1,'studies',0));
QMlastPreprocess = struct('SRTM',struct('idCt',1,'idCr',2,'k2amin',0,'k2amax',0,'threshold',0,'M',[],'B',[],'numBF',0,'plots',[],'results',[]),...
'SRTM2',struct('idCt',1,'idCr',2,'k2_pChecked',false,'B',0,'th3',0,'threshold',0,'plots',[],'results',[]),...
'PatlakRef',struct('idCt',1,'idCr',2,'t',[],'plots',[],'results',[]),...
'LoganPlot',struct('idCt',1,'idCr',2,'t',[],'plots',[],'results',[]),...
'TwoTCM',struct('idCt',1,'idCplasma',2,'Cblood',[],'vB',[],'k4_checked',false,'M',[],'B',[],'alpha',[],'threshold',0,'plots',[],'results',[]));
% UIWAIT makes QModeling wait for user response (see UIRESUME)
% uiwait(handles.QModeling_figure);
% --- Outputs from this function are returned to the command line.
function varargout = QModeling_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
deleteTempImages(handles);
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on selection change in selectModel_Popupmenu.
function selectModel_Popupmenu_Callback(hObject, eventdata, handles)
% hObject handle to selectModel_Popupmenu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns selectModel_Popupmenu contents as cell array
% contents{get(hObject,'Value')} returns selected item from selectModel_Popupmenu
global QMlastPreprocess
global QMmaindata
%global QMmaindata
selected = get(handles.selectModel_Popupmenu,'Value');
if selected == 1, model = 'SRTM'; end
if selected == 2, model = 'SRTM2'; end
if selected == 3, model = 'PatlakRef'; end
if selected == 4, model = 'LoganPlot'; end
if selected == 5, model = 'TwoTCM'; end
data = getfield(QMlastPreprocess,model);
if isempty(data.plots)
Refresh_Tacs(handles,data);
set(handles.setMapParam_Pushbutton,'Enable','off')
set(handles.selectImage_Popupmenu,'Enable','off');
set(handles.viewImage_Pushbutton,'Enable','off');
set(handles.showTacs_Radiobutton,'Visible','off')
set(handles.showPreprocess_Radiobutton,'Visible','off')
set(handles.results_Text,'String','');
set(handles.results_Text2,'String','');
set(handles.saveResults_Pushbutton,'Enable','off')
set(handles.corrMatrix_Pushbutton,'Enable','off')
%Set panel focus
setPanelFocus(handles,'ModelingPanel');
else
Refresh_Plots_Results(handles, selected);
set(handles.setMapParam_Pushbutton,'Enable','on')
if QMmaindata.flags.studies==selected
set(handles.selectImage_Popupmenu,'Enable','on');
set(handles.viewImage_Pushbutton,'Enable','on');
%Set panel focus
setPanelFocus(handles,'ViewParamImgPanel');
else
set(handles.selectImage_Popupmenu,'Enable','off');
set(handles.viewImage_Pushbutton,'Enable','off');
%Set panel focus
setPanelFocus(handles,'PixelCalcPanel');
end
set(handles.showTacs_Radiobutton,'Visible','on')
set(handles.showPreprocess_Radiobutton,'Visible','on')
set(handles.showTacs_Radiobutton,'Value',0)
set(handles.showPreprocess_Radiobutton,'Value',1)
end
% --- Executes during object creation, after setting all properties.
function selectModel_Popupmenu_CreateFcn(hObject, eventdata, handles)
% hObject handle to selectModel_Popupmenu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in preprocessModel_Pushbutton.
function preprocessModel_Pushbutton_Callback(hObject, eventdata, handles)
% hObject handle to preprocessModel_Pushbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global QMmaindata
global QMf_logfile
error = 0;
%Set panel focus
setPanelFocus(handles,'ModelingPanel');
if size(QMmaindata.Crois,2) == 1
% errordlg(char({'To use reference models you must have al least 2 diferent TACs';'View help.'}),'Preprocessing error','modal');
% fprintf(QMf_logfile,strcat(' ERROR: Insufficient number of TACs to use reference models','\n'));
errordlg(char({'To use reference models you must have at least 2 diferent TACs';...
'In other case, the TAC for the region of interest and the blood/plasma radioactivity concentration are required';...
'View help.'}),'Preprocessing error','modal');
fprintf(QMf_logfile,strcat(' ERROR: Insufficient number of TACs','\n'));
return;
end
set(handles.setMapParam_Pushbutton,'Enable','off');
set(hObject,'Enable','off');
selected = get(handles.selectModel_Popupmenu,'Value');
switch selected
case 1,
try
QM_SRTMPreprocessView('mainhandles',handles.QModeling_figure);
set(handles.corrMatrix_Pushbutton,'Enable','on');
catch ME
errordlg(char({'An error ocurred during the preprocess:';'Please, check logfile.txt'}),'Preprocessing error','modal');
error = 1;
fprintf(QMf_logfile,strcat(' ERROR: During the preprocess','\n',' Error message: ',ME.message,'\n',...
' File: ',ME.stack(1).file,'\n',' Line: ',num2str(ME.stack(1).line),'\n'));
end
case 2,
try
QM_CarsonPreprocessView('mainhandles',handles.QModeling_figure);
set(handles.corrMatrix_Pushbutton,'Enable','on');
catch ME
errordlg(char({'An error ocurred during the preprocess:';'Please, check logfile.txt'}),'Preprocessing error','modal');
error = 1;
fprintf(QMf_logfile,strcat(' ERROR: During the preprocess','\n',' Error message: ',ME.message,'\n',...
' File: ',ME.stack(1).file,'\n',' Line: ',num2str(ME.stack(1).line),'\n'));
end
case 3,
try
QM_patlakPreprocessView('mainhandles',handles.QModeling_figure);
set(handles.corrMatrix_Pushbutton,'Enable','off');
catch ME
errordlg(char({'An error ocurred during the preprocess:';'Please, check logfile.txt'}),'Preprocessing error','modal');
error = 1;
fprintf(QMf_logfile,strcat(' ERROR: During the preprocess','\n',' Error message: ',ME.message,'\n',...
' File: ',ME.stack(1).file,'\n',' Line: ',num2str(ME.stack(1).line),'\n'));
end
case 4,
try
QM_LoganPlotView('mainhandles',handles.QModeling_figure);
set(handles.corrMatrix_Pushbutton,'Enable','off');
catch ME
errordlg(char({'An error ocurred during the preprocess:';'Please, check logfile.txt'}),'Preprocessing error','modal');
error = 1;
fprintf(QMf_logfile,strcat(' ERROR: During the preprocess','\n',' Error message: ',ME.message,'\n',...
' File: ',ME.stack(1).file,'\n',' Line: ',num2str(ME.stack(1).line),'\n'));
end
case 5,
try
QM_TwoTCMPreprocessView('mainhandles',handles.QModeling_figure);
set(handles.corrMatrix_Pushbutton,'Enable','on');
catch ME
errordlg(char({'An error ocurred during the preprocess:';'Please, check logfile.txt'}),'Preprocessing error','modal');
error = 1;
fprintf(QMf_logfile,strcat(' ERROR: During the preprocess','\n',' Error message: ',ME.message,'\n',...
' File: ',ME.stack(1).file,'\n',' Line: ',num2str(ME.stack(1).line),'\n'));
end
end
if error ~= 1
%Set visible plots options
set(handles.plottedCt_Popupmenu,'Enable','off');
set(handles.plottedCr_Popupmenu,'Enable','off');
if ~isempty(get(handles.results_Text,'String'))
% If showTacs_Radiobutton selected, enable selection tacs
if get(handles.showTacs_Radiobutton,'Value') == 1
set(handles.plottedCt_Popupmenu,'Enable','on');
set(handles.plottedCr_Popupmenu,'Enable','on');
end
%Set panel focus
setPanelFocus(handles,'PixelCalcPanel');
%Set enable pixel-wise calculation button
set(handles.setMapParam_Pushbutton,'Enable','on')
set(handles.saveResults_Pushbutton,'Enable','on')
else % If nothing preprocessed, enable selection tacs
set(handles.plottedCt_Popupmenu,'Enable','on');
set(handles.plottedCr_Popupmenu,'Enable','on');
end
end
set(hObject,'Enable','on');
% --- Executes during object creation, after setting all properties.
function LoadData_uipanel_CreateFcn(hObject, eventdata, handles)
% hObject handle to LoadData_uipanel (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
set(hObject,'BorderType','line','HighlightColor',[0.043;0.518;0.78],'BorderWidth',3);
% --- Executes on button press in loadStudy_Pushbutton.
function loadStudy_Pushbutton_Callback(hObject, eventdata, handles)
% hObject handle to loadStudy_Pushbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global QMmaindata
% global reorient
global QMf_logfile
error = 0;
% reorient = false;
set(handles.setMapParam_Pushbutton,'UserData',0);
% set(handles.plottedCt_Popupmenu,'Enable','off');
% set(handles.plottedCr_Popupmenu,'Enable','off');
set(handles.editTime_Pushbutton,'Enable','off');
set(handles.loadStudy_Pushbutton,'Enable','off');
set(handles.selectMask_Pushbutton,'Enable','off');
%Set panel focus
setPanelFocus(handles,'LoadPanel');
%Load study
paths = spm_select([1,600], 'image', 'Select .img or .nii images from PET studio ','','','.*',[1 600]);
if ~isempty(paths)
%Reset main GUI
set(handles.prepareTacs_Pushbutton,'Enable','off');
cla(handles.axes,'reset');
set(handles.showTacs_Radiobutton,'Visible','off')
set(handles.showPreprocess_Radiobutton,'Visible','off')
set(handles.preprocessModel_Pushbutton,'Enable','off');
set(handles.results_Text,'String','');
set(handles.results_Text2,'String','');
set(handles.setMapParam_Pushbutton,'Enable','off');
set(handles.selectImage_Popupmenu,'Enable','off');
set(handles.viewImage_Pushbutton,'Enable','off');
set (handles.selectModel_Popupmenu,'Enable','off');
num_files = size(paths,1);
%Save in UserData from study_Textbox the PET paths
set(handles.study_Textbox,'UserData',paths);
if num_files ~= 0
%Show PET name
auxstr = '';
auxpath = paths(1,:);
auxpath = auxpath(find(auxpath == filesep,1,'last')+1:end-2);
if length(auxpath)+11 > 34
auxpath = strcat(auxpath(1:20),'...');
end
auxstr = strcat(auxstr,auxpath,' (',num2str(num_files),' files)');
end
set(handles.study_Textbox,'String',auxstr);
%Update the logfile
aux_date=datestr(now);
fprintf(QMf_logfile,strcat(aux_date(end-8:end),' Loading PET study -',auxstr,'-\n'));
waitbarhandle = waitbar(0,'Loading PET study, please wait...');
ver=version('-release');
if (str2num(ver(1:4))<2014) || isequal(ver,'2014a')
set(findobj(waitbarhandle,'type','patch'),'edgecolor','b','facecolor','b');
else
wbc = allchild(waitbarhandle); %you need to get at a hidden child
wbc(1).JavaPeer.setForeground( wbc(1).JavaPeer.getBackground.BLUE )
wbc(1).JavaPeer.setStringPainted(true)
end
if num_files ~= 0
if num_files == 1 %Case multiframe
%Open the multiframe PET study
multifile = paths(1,1:max(strfind(paths,','))-1); % To raise ',1'
multifile_hdr = spm_vol(multifile);
num_files = length(multifile_hdr);
QMmaindata.num_files = num_files;
%Initialize the PET times variable
times = zeros(num_files,2);
try
for frame = 1:num_files
current_hdr = multifile_hdr(frame);
times(frame,1) = current_hdr.private.timing.toffset;
times(frame,2) = times(frame,1) + current_hdr.private.timing.tspace;
waitbar(frame/num_files,waitbarhandle)
end
QMmaindata.times = times;
catch er
%Checking if is an internal error
if (isfield(current_hdr,'private') && isfield(current_hdr.private,'timing')...
&& isfield(current_hdr.private.timing,'toffset') && isfield(current_hdr.private.timing,'tspace'))
errordlg(char({'An error ocurred reading the PET times.';'Please, edit times.'}),'Reading error','modal');
uicontrol(handles.editTime_Pushbutton);
error = 1;
fprintf(QMf_logfile,strcat(' ERROR Reading the PET times','\n'));
else %No times found in study, we insert default timetable
actualpath = mfilename('fullpath');
actualpath = actualpath(1:end-10);
aux = load(strcat(actualpath,filesep,'TimeTable.txt'));
if length(aux)<num_files %length of Timetable is not sufficient. There are more frames than start and end times at the Timetable.txt
errordlg(char({'There are more frames at the study than start and end times at the default TimeTable file. Please, edit the default TimeTable file.'}),'Reading error','modal');
uicontrol(handles.editTime_Pushbutton);
error = 1;
fprintf(QMf_logfile,strcat(' ERROR There are more frames at the study than start and end times at the default TimeTable file.','\n'));
else
times = aux(1:num_files,:);
QMmaindata.times = times;
warndlg(char({'No times found in your study. Default times has been loaded';...
'If you like to change it, select Change times'}),'Reading warning','modal');
fprintf(QMf_logfile,strcat(' WARNING: No times found in the study. Default times has been loaded','\n'));
end
end
end
delete(waitbarhandle)
else
QMmaindata.num_files = num_files;
%Initialize the PET times variable
times = zeros(num_files,2);
try
for frame = 1:num_files
%Open the current frame for the PET study
file = paths(frame,:);
file_hdr = spm_vol(file);
% %Check if PET study is reoriented
% if(reorient == false && (~isequal(file_hdr.mat(1,2:3),[0,0]) ||...
% file_hdr.mat(2,1)~=0 || file_hdr.mat(2,3)~=0 || ~isequal(file_hdr.mat(3,1:2),[0,0]) ||...
% ~isequal(file_hdr.mat(4,1:3),[0,0,0])))
% warndlg(char({'The study is not oriented';'The parametric images will be show reoriented'}),'Reading warning','modal');
% reorient = true;
% fprintf(QMf_logfile,strcat(' WARNING: The study is not oriented. The parametric images will be show reoriented','\n'));
% end
times(frame,1) = file_hdr.private.timing.toffset;
times(frame,2) = times(frame,1) + file_hdr.private.timing.tspace;
waitbar(frame/num_files,waitbarhandle)
end
QMmaindata.times = times;
catch er
%Checking if is an internal error
if (isfield(file_hdr,'private') && isfield(file_hdr.private,'timing')...
&& isfield(file_hdr.private.timing,'toffset') && isfield(file_hdr.private.timing,'tspace'))
errordlg(char({'An error ocurred reading the PET times.';'Please, edit times.'}),'Reading error','modal');
uicontrol(handles.editTime_Pushbutton);
error = 1;
fprintf(QMf_logfile,strcat(' ERROR Reading the PET times','\n'));
else %No times in study, we insert default timetable
actualpath = mfilename('fullpath');
actualpath = actualpath(1:end-10);
aux = load(strcat(actualpath,filesep,'TimeTable.txt'));
times = aux(1:num_files,:);
QMmaindata.times = times;
warndlg(char({'No times found in your study. Default times has been loaded';...
'If you like to change it, select Change times'}),'Reading warning','modal');
fprintf(QMf_logfile,strcat(' WARNING: No times found in the study. Default times has been loaded','\n'));
end
end
delete(waitbarhandle)
end
end
if error == 0 && ~isempty(get(handles.mask_Textbox,'String'))
set(handles.prepareTacs_Pushbutton,'Enable','on');
end
set(handles.editTime_Pushbutton,'Enable','on');
%Checking if the PET times are consistent
checkTimesConsistency();
if error == 0
%Detele the last parametric images created in temp file
deleteTempImages(handles)
aux_date=datestr(now);
fprintf(QMf_logfile,strcat(aux_date(end-8:end),' Study loaded \n'));
else
set(handles.selectImage_Popupmenu,'Enable','on');
end
end
set(handles.loadStudy_Pushbutton,'Enable','on');
set(handles.selectMask_Pushbutton,'Enable','on');
function study_Textbox_Callback(hObject, eventdata, handles)
% hObject handle to study_Textbox (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of study_Textbox as text
% str2double(get(hObject,'String')) returns contents of study_Textbox as a double
if isempty(get(handles.study_Textbox,'String'))
set(handles.prepareTacs_Pushbutton,'Enable','off');
end
% --- Executes during object creation, after setting all properties.
function study_Textbox_CreateFcn(hObject, eventdata, handles)
% hObject handle to study_Textbox (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in editTime_Pushbutton.
function editTime_Pushbutton_Callback(hObject, eventdata, handles)
% hObject handle to editTime_Pushbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
QM_editTimes('mainhandles',handles.QModeling_figure);
checkTimesConsistency();
% --- Executes on button press in Load Mask/TACs_Pushbutton.
function selectMask_Pushbutton_Callback(hObject, eventdata, handles)
% hObject handle to selectMask_Pushbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%Set panel focus
setPanelFocus(handles,'LoadPanel');
file = spm_select([1,1],'any','Select group mask (.img or .nii) or TACs (.txt, .xls or .mat)');
if ~isempty(file)
[path, name, ext]=fileparts(file);
% if size(file,1) ~= 0
% auxstr = file(find(file == filesep,1,'last')+1:end-2);
% end
%
% set(handles.mask_Textbox,'String',auxstr);
set(handles.mask_Textbox,'String',strcat(name,ext));
if ~isempty(get(handles.study_Textbox,'String'))
set(handles.prepareTacs_Pushbutton,'Enable','on');
end
% [path name ext] = fileparts(file);
if (strcmp(ext,'.nii') || strcmp(ext,'.img')) %look for the file with ROI names
codes = strcat(path,filesep,name,'.txt');
if exist(codes,'file') == 0 %If file doesn't exist
uiwait(msgbox({'The .txt with the names of the ROIs has not been found.';...
'After you close this window, a new window dialog will be open for select it.';...
'The file format is:';...
'';...
'region_A id_A';...
'region_B id_B';...
'region_C id_C';...
'region_D id_D';...
'...';...
'';...
'If the .txt file is not selected or an identifier doesnt exist,';...
'default names will be loaded for the ROIs.'},'Information','help','modal'));
codes = spm_select([1,1],'txt','Select a .txt file with the ROIs names. Example per line: occipital 1');
end
elseif (strcmp(ext,'.mat')||strcmp(ext,'.txt')||strcmp(ext,'.xls')||strcmp(ext,'.xlsx'))
codes=ext;
else
codes='ERROR';
end
set(handles.mask_Textbox,'UserData',{file;codes});
% file = spm_select([1,1],'image','Select group mask (.img or .nii)');
%
% if ~isempty(file)
%
% if size(file,1) ~= 0
% auxstr = file(find(file == filesep,1,'last')+1:end-2);
% end
%
% set(handles.mask_Textbox,'String',auxstr);
%
% if ~isempty(get(handles.study_Textbox,'String'))
% set(handles.prepareTacs_Pushbutton,'Enable','on');
% end
%
% [path name ext] = fileparts(file);
% codes = strcat(path,filesep,name,'.txt');
% if exist(codes,'file') == 0 %If file doesn't exist
%
% uiwait(msgbox({'The .txt with the names of the ROIs has not been found.';...
% 'After you close this window, a new window dialog will be open for select it.';...
% 'The file format is:';...
% '';...
% 'region_A id_A';...
% 'region_B id_B';...
% 'region_C id_C';...
% 'region_D id_D';...
% '...';...
% '';...
% 'If the .txt file is not selected or an identifier doesnt exist,';...
% 'default names will be loaded for the ROIs.'},'Information','help','modal'));
%
%
% codes = spm_select([1,1],'txt','Select a .txt file with the ROIs names. Example per line: occipital 1');
% end
%
% set(handles.mask_Textbox,'UserData',{file;codes});
end
function mask_Textbox_Callback(hObject, eventdata, handles)
% hObject handle to mask_Textbox (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of mask_Textbox as text
% str2double(get(hObject,'String')) returns contents of mask_Textbox as a double
if isempty(get(handles.mask_Textbox,'String'))
set(handles.prepareTacs_Pushbutton,'Enable','off');
end
% --- Executes during object creation, after setting all properties.
function mask_Textbox_CreateFcn(hObject, eventdata, handles)
% hObject handle to mask_Textbox (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in prepareTACs_Pushbutton.
function prepareTacs_Pushbutton_Callback(hObject, eventdata, handles)
% hObject handle to prepareTacs_Pushbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global QMmaindata
global QMlastPreprocess
global QMf_logfile
error = 0;
QMlastPreprocess = struct('SRTM',struct('idCt',1,'idCr',2,'k2amin',0,'k2amax',0,'threshold',0,'M',[],'B',[],'numBF',0,'plots',[],'results',[]),...
'SRTM2',struct('idCt',1,'idCr',2,'B',0,'th3',0,'threshold',0,'plots',[],'results',[]),...
'PatlakRef',struct('idCt',1,'idCr',2,'t',[],'plots',[],'results',[]),...
'LoganPlot',struct('idCt',1,'idCr',2,'t',[],'plots',[],'results',[]),...
'TwoTCM',struct('idCt',1,'idCplasma',2,'Cblood',[],'vB',[],'k4_checked',false,'M',[],'B',[],'alpha',[],'threshold',0,'plots',[],'results',[]));
set(handles.setMapParam_Pushbutton,'UserData',0);
%Set panel focus
setPanelFocus(handles,'LoadPanel');
%Disable the follow steps
set(handles.preprocessModel_Pushbutton,'Enable','off');
set(handles.setMapParam_Pushbutton,'Enable','off');
set(handles.selectImage_Popupmenu,'Enable','off');
set(handles.viewImage_Pushbutton,'Enable','off');
set(handles.saveResults_Pushbutton,'Enable','off');
set(handles.corrMatrix_Pushbutton,'Enable','off');
%Update the logfile
aux_date=datestr(now);
fprintf(QMf_logfile,strcat(aux_date(end-8:end),' Preparing TACs \n'));
if QMmaindata.times == 0
errordlg(char({'Cannot prepare TACs because have not times.';'Please, edit times.'}),'Reading error','modal');
uicontrol(handles.editTime_Pushbutton);
fprintf(QMf_logfile,strcat(' ERROR: No times loaded','\n'));
elseif size(QMmaindata.times,1) ~= QMmaindata.num_files
errordlg(char({'Cannot prepare TACs because the number of frames is diferent than the number of times.';'Please, edit times.'}),'Reading error','modal');
uicontrol(handles.editTime_Pushbutton);
fprintf(QMf_logfile,strcat(' ERROR: Diferent number of frames and times','\n'));
else
waitbarhandle = waitbar(0,'Preparing TACs, please wait...');
ver=version('-release');
if (str2num(ver(1:4))<2014) || isequal(ver,'2014a')
set(findobj(waitbarhandle,'type','patch'),'edgecolor','b','facecolor','b');
else
wbc = allchild(waitbarhandle); %you need to get at a hidden child
wbc(1).JavaPeer.setForeground( wbc(1).JavaPeer.getBackground.BLUE )
wbc(1).JavaPeer.setStringPainted(true)
end
try
[QMmaindata.Cpet QMmaindata.Crois num_rois] = QM_prepareTACs(handles,waitbarhandle,QMmaindata.num_files);
catch ME
if strcmp(ME.identifier,'PrepareTACs:dimensionsDismatch')
errordlg(char({'Cannot prepare TACs because the dimensions of the PET and the mask dismatch';'Please, select new mask.'}),'Reading error','modal');
uicontrol(handles.selectMask_Pushbutton);
fprintf(QMf_logfile,strcat(' ERROR: The dimensions of the PET and the mask dismatch','\n'));
elseif strcmp(ME.identifier,'PrepareTACs:fileFormatIncorrect')
errordlg(char({'Cannot prepare TACs because the file format is not accepted';'Please, select a new correct format TACs file.'}),'Reading error','modal');
uicontrol(handles.selectMask_Pushbutton);
fprintf(QMf_logfile,strcat(' ERROR: TACs file format not accepted','\n'));
elseif strcmp(ME.identifier, 'PrepareTACs:badFormattedData')
errordlg(char({'Cannot prepare TACs because data format is not correct';'Please, read the manual to ensure your data is well formatted.'}),'Reading error','modal');
uicontrol(handles.selectMask_Pushbutton);
fprintf(QMf_logfile,strcat(' ERROR: Data format for TACs data is not correct','\n'));
else
errordlg(char({'An unexpected error ocurred reading the PET';'Please, open help.'}),'Reading error','modal');
fprintf(QMf_logfile,strcat(' ERROR: Reading the PET','\n'));
end
error = 1;
end
%Plot the TACs
delete(waitbarhandle); %Need to delete before ploting
if error == 0
%Update the logfile
aux_date=datestr(now);
fprintf(QMf_logfile,strcat(aux_date(end-8:end),' TACs prepared \n'));
%Clean results panel
set(handles.results_Text,'String','');
set(handles.results_Text2,'String','');
%Average time between frames and translated to minutes
tavg = ((QMmaindata.times(:,1)+QMmaindata.times(:,2))/2);
tmin = tavg/60;
%Reset selected TACs
QMmaindata.idCt = 1;
if num_rois == 1
QMmaindata.idCr = 1;
else
QMmaindata.idCr = 2;
end
%Plot TACs
% plot(handles.axes,tmin,QMmaindata.Crois{2,QMmaindata.idCt},'b.-');
% hold on
% plot(handles.axes,tmin,QMmaindata.Crois{2,QMmaindata.idCr},'r.-');
% hold off
% plot(handles.axes,tmin,QMmaindata.Crois{2,QMmaindata.idCr},'r.-');
% hold on
% plot(handles.axes,tmin,QMmaindata.Crois{2,QMmaindata.idCt},'b.-');
% hold off
plot(handles.axes,tmin,QMmaindata.Crois{2,QMmaindata.idCt},'b.-',tmin,QMmaindata.Crois{2,QMmaindata.idCr},'r.-');
% hold on
% plot(handles.axes,tmin,QMmaindata.Crois{2,QMmaindata.idCt},'b.-');
% hold off
xlabel('Time (minutes)')
ylabel('Original units')
% aux = get(handles.mask_Textbox,'UserData');
% [~, mask_name, mask_ext]=fileparts(aux{1});
% mask_name = aux{1}(find(aux{1} == filesep,1,'last')+1:end-2);
% title(['Time-activity curves for mask: ' strcat(mask_name,mask_ext)])
title('Time-activity curves')
% set(handles.axes,'UserData',{'TACs',[tmin QMmaindata.Crois{2,QMmaindata.idCt:QMmaindata.idCr}]});
t_aux=table([1:1:length(tmin)]',tmin,QMmaindata.Crois{2,QMmaindata.idCt:QMmaindata.idCr});
set(handles.axes,'UserData',{'TACs',table2cell(t_aux)});
%Enable selection TACs popupmenus
strmenu = cell(1,num_rois);
for i = 1:num_rois
strmenu{1,i} = QMmaindata.Crois{1,i}; %strcat('TAC ',num2str(i));
end
set(handles.plottedCt_Popupmenu,'String',strmenu);
set(handles.plottedCt_Popupmenu,'Enable','on');
set(handles.plottedCt_Popupmenu,'Value',1);
set(handles.plottedCr_Popupmenu,'String',strmenu);
if num_rois == 1
set(handles.plottedCr_Popupmenu,'Value',1);
else
set(handles.plottedCr_Popupmenu,'Value',2);
end
set(handles.plottedCr_Popupmenu,'Enable','on');
set(handles.showTacs_Radiobutton,'Visible','off')
set(handles.showPreprocess_Radiobutton,'Visible','off')
%Set panel focus
setPanelFocus(handles,'ModelingPanel');
%Enable preprocess buttons
set(handles.selectModel_Popupmenu,'Enable','on');
set(handles.preprocessModel_Pushbutton,'Enable','on');
%Detele the last parametric images created in temp file
deleteTempImages(handles)
end
end
% --------------------------------------------------------------------
function QModeling_MenuBar_Callback(hObject, eventdata, handles)
% hObject handle to QModeling_MenuBar (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function Help_MenuBar_Callback(hObject, eventdata, handles)
% hObject handle to Help_MenuBar (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global QMpath
open(strcat(QMpath,filesep,'help',filesep,'html',filesep,'manual.html'))
% --- Executes on button press in setMapParam_Pushbutton.
function setMapParam_Pushbutton_Callback(hObject, eventdata, handles)
% hObject handle to setMapParam_Pushbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global QMmaindata
global QMlastPreprocess
global QMf_logfile
error = 0;
%Set panel focus
setPanelFocus(handles,'PixelCalcPanel');
selected = get(handles.selectModel_Popupmenu,'Value');
if selected == 1, model = 'SRTM'; end
if selected == 2, model = 'SRTM2'; end
if selected == 3, model = 'PatlakRef'; end
if selected == 4, model = 'LoganPlot'; end
if selected == 5, model = 'TwoTCM'; end
data = getfield(QMlastPreprocess,model);
if isempty(data.plots)
errordlg(char({'An error ocurred during the pixel-wise calculation:';'You do not have a preprocess with the model selected.';'View help.'}),'Pixel calculation','modal');
fprintf(QMf_logfile,strcat(' ERROR: A preprocess with the model selected does not exist','\n'));
else
switch selected
case 1,
%Call SRTM
try
QM_mapParamSRTM('mainhandles',handles.QModeling_figure);
catch ME
errordlg(char({'An error ocurred during the pixel-wise calculation:';'Please, check logfile.txt'}),'Pixel calculation','modal');
error = 1;
fprintf(QMf_logfile,strcat(' ERROR: During the pixel-wise calculation','\n',' Error message: ',ME.message,'\n',...
' File: ',ME.stack(1).file,'\n',' Line: ',num2str(ME.stack(1).line),'\n'));
end
case 2,
%Call SRTM2
try
QM_mapParamCarson('mainhandles',handles.QModeling_figure);
catch ME
errordlg(char({'An error ocurred during the pixel-wise calculation:';'Please, check logfile.txt'}),'Pixel calculation','modal');
error=1;
fprintf(QMf_logfile,strcat(' ERROR: During the pixel-wise calculation','\n',' Error message: ',ME.message,'\n',...
' File: ',ME.stack(1).file,'\n',' Line: ',num2str(ME.stack(1).line),'\n'));
end
case 3,
%Call PatlakRef
try
QM_mapParamPatlak('mainhandles',handles.QModeling_figure);
catch ME
errordlg(char({'An error ocurred during the pixel-wise calculation:';'Please, check logfile.txt'}),'Pixel calculation','modal');
error = 1;
fprintf(QMf_logfile,strcat(' ERROR: During the pixel-wise calculation','\n',' Error message: ',ME.message,'\n',...
' File: ',ME.stack(1).file,'\n',' Line: ',num2str(ME.stack(1).line),'\n'));
end
case 4,
%Call LoganPlot
try
QM_mapParamLogan('mainhandles',handles.QModeling_figure);
catch ME
errordlg(char({'An error ocurred during the pixel-wise calculation:';'Please, check logfile.txt'}),'Pixel calculation','modal');
error = 1;
fprintf(QMf_logfile,strcat(' ERROR: During the pixel-wise calculation','\n',' Error message: ',ME.message,'\n',...
' File: ',ME.stack(1).file,'\n',' Line: ',num2str(ME.stack(1).line),'\n'));
end
case 5,
%Call TwoTCM
try
QM_mapParamTwoTCM('mainhandles',handles.QModeling_figure);
catch ME
errordlg(char({'An error ocurred during the pixel-wise calculation:';'Please, check logfile.txt'}),'Pixel calculation','modal');
error = 1;
fprintf(QMf_logfile,strcat(' ERROR: During the pixel-wise calculation','\n',' Error message: ',ME.message,'\n',...
' File: ',ME.stack(1).file,'\n',' Line: ',num2str(ME.stack(1).line),'\n'));
end
end
executed=get(hObject, 'UserData');
if error == 0 && executed == 1
set(handles.selectImage_Popupmenu,'Enable','on');
set(handles.viewImage_Pushbutton,'Enable','on');
QMmaindata.flags.studies=selected;
%Set panel focus
setPanelFocus(handles,'ViewParamImgPanel');
else
set(handles.selectImage_Popupmenu,'String','Parametric Images');
set(handles.selectImage_Popupmenu,'Enable','off');
set(handles.viewImage_Pushbutton,'Enable','off');
end
end
% --- Executes on selection change in plottedCt_Popupmenu.
function plottedCt_Popupmenu_Callback(hObject, eventdata, handles)
% hObject handle to plottedCt_Popupmenu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns plottedCt_Popupmenu contents as cell array
% contents{get(hObject,'Value')} returns selected item from plottedCt_Popupmenu
global QMmaindata
QMmaindata.idCt = get(hObject,'Value');
QMmaindata.idCr = get(handles.plottedCr_Popupmenu,'Value');
tavg = ((QMmaindata.times(:,1)+QMmaindata.times(:,2))/2);
tmin = tavg/60;
plot(handles.axes,tmin,QMmaindata.Crois{2,QMmaindata.idCt},'b.-');
hold on
plot(handles.axes,tmin,QMmaindata.Crois{2,QMmaindata.idCr},'r.-');
hold off
xlabel('Time (minutes)')
ylabel('Original units')
% aux = get(handles.mask_Textbox,'UserData');
% mask_name = aux{1}(find(aux{1} == filesep,1,'last')+1:end-2);
% title(['Time-activity curves for mask: ' mask_name])
title('Time-activity curves');
% set(handles.axes,'UserData',{'TACs',[tmin QMmaindata.Crois{2,QMmaindata.idCt} QMmaindata.Crois{2,QMmaindata.idCr}]});
if QMmaindata.idCt==QMmaindata.idCr
t_aux=table([1:1:length(tmin)]',tmin,QMmaindata.Crois{2,QMmaindata.idCt});
else
t_aux=table([1:1:length(tmin)]',tmin,QMmaindata.Crois{2,QMmaindata.idCt}, QMmaindata.Crois{2,QMmaindata.idCr});
end
set(handles.axes,'UserData',{'TACs',table2cell(t_aux)});
% --- Executes during object creation, after setting all properties.
function plottedCt_Popupmenu_CreateFcn(hObject, eventdata, handles)
% hObject handle to plottedCt_Popupmenu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.