forked from qMRLab/qMRLab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qMRILab.m
1429 lines (1242 loc) · 48.7 KB
/
qMRILab.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 = qMRILab(varargin)
% qMRILAB MATLAB code for qMRILab.fig
% GUI to simulate/fit qMRI data
% ----------------------------------------------------------------------------------------------------
% Written by: Jean-François Cabana, 2016
%
% -- MTSAT functionality: P. Beliveau, 2017
% -- File Browser changes: P. Beliveau 2017
% ----------------------------------------------------------------------------------------------------
% If you use qMRILab in your work, please cite :
% Cabana, JF. et al (2016).
% Quantitative magnetization transfer imaging made easy with qMRILab
% Software for data simulation, analysis and visualization.
% Concepts in Magnetic Resonance Part A
% ----------------------------------------------------------------------------------------------------
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @qMRILab_OpeningFcn, ...
'gui_OutputFcn', @qMRILab_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 qMRILab is made visible.
function qMRILab_OpeningFcn(hObject, eventdata, handles, varargin)
if ~isfield(handles,'opened') % qMRI already opened?
handles.opened = 1;
clc;
% startup;
qMRILabDir = fileparts(which(mfilename()));
addpath(genpath(qMRILabDir));
handles.root = qMRILabDir;
handles.methodfiles = '';
handles.CurrentData = [];
handles.FitDataDim = [];
handles.FitDataSize = [];
handles.FitDataSlice = [];
handles.dcm_obj = [];
MethodList = {}; SetAppData(MethodList);
handles.output = hObject;
guidata(hObject, handles);
% cd(fullfile(handles.root, Method));
LoadSimVaryOpt(fullfile(handles.root,'Common','Parameters'), 'DefaultSimVaryOpt.mat', handles);
LoadSimRndOpt(fullfile(handles.root, 'Common','Parameters'), 'DefaultSimRndOpt.mat', handles);
% SET WINDOW AND PANELS
movegui(gcf,'center')
CurrentPos = get(gcf, 'Position');
NewPos = CurrentPos;
NewPos(1) = CurrentPos(1) - 40;
set(gcf, 'Position', NewPos);
% Fill Menu with models
handles.ModelDir = [qMRILabDir filesep 'Models'];
guidata(hObject, handles);
addModelMenu(hObject, eventdata, handles);
% Fill FileBrowser with buttons
MethodList = getappdata(0, 'MethodList');
MethodList = strrep(MethodList, '.m', '');
flist = findall(0,'type','figure');
for iMethod=1:length(MethodList)
% Create browser panel buttons
switch MethodList{iMethod}
case 'bSSFP'
MRIinputs = {'Mask' 'MTdata' 'R1map'};
case 'SIRFSE'
MRIinputs = {'Mask' 'MTdata' 'R1map'};
case 'SPGR'
MRIinputs = {'Mask' 'MTdata' 'R1map' 'B1map' 'B0map'};
otherwise
Modelfun = str2func(MethodList{iMethod});
Model = Modelfun();
close(setdiff(findall(0,'type','figure'),flist)); % close figures that might open when calling models
MRIinputs = Model.MRIinputs;
end
% create file browser uicontrol with specific inputs
FileBrowserList(iMethod) = MethodBrowser(handles.FitDataFileBrowserPanel,handles,{MethodList{iMethod} MRIinputs{:}});
FileBrowserList(iMethod).Visible('off');
end
SetAppData(FileBrowserList);
% LOAD DEFAULTS
if ~isempty(varargin)
Model = varargin{1};
SetAppData(Model);
Method = class(Model);
if length(varargin)>1
data=varargin{2};
for ff=fieldnames(data)';
FileBrowserList(strcmp([FileBrowserList.MethodID],Method)).setFileName(ff{1}, data.(ff{1}))
end
end
else
load(fullfile(handles.root,'Common','Parameters','DefaultMethod.mat'));
end
% Set Default
set(handles.MethodSelection, 'String', Method);
SetActive('FitData', handles);
MethodMenu(hObject, eventdata, handles,Method);
else
OpenOptionsPanel_Callback(hObject, eventdata, handles)
end
% Outputs from this function are returned to the command line.
function varargout = qMRILab_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
wh=findall(0,'tag','TMWWaitbar');
delete(wh);
% Executes when user attempts to close qMRILab.
function qMRILab_CloseRequestFcn(hObject, eventdata, handles)
h = findobj('Tag','OptionsGUI');
delete(h);
delete(hObject);
% cd(handles.root);
AppData = getappdata(0);
Fields = fieldnames(AppData);
for k=1:length(Fields)
rmappdata(0, Fields{k});
end
function MethodSelection_Callback(hObject, eventdata, handles)
Method = GetMethod(handles);
MethodMenu(hObject,eventdata,handles,Method);
function addModelMenu(hObject, eventdata, handles)
methods = sct_tools_ls([handles.ModelDir filesep '*.m'],0,0,2,1);
methodsM = strcat(sct_tools_ls([handles.ModelDir filesep '*.m'],0,0,2,1),'.m');
MethodList = GetAppData('MethodList');
MethodList = {MethodList{:} methodsM{:}};
SetAppData(MethodList)
%###########################################################################################
% COMMON FUNCTIONS
%###########################################################################################
% METHODSELECTION
function MethodMenu(hObject, eventdata, handles, Method)
% Display all the options in the popupmenu
list = sct_tools_ls([handles.ModelDir filesep '*.m'],1,0,2,1);
choices = strrep(list{1},[handles.ModelDir filesep],'');
for i = 2:length(list)
choices = strvcat(choices, strrep(list{i},[handles.ModelDir filesep],''));
end
set(handles.MethodSelection,'String',choices);
SetAppData(Method)
% Display the fitting panel
SetActive('FitData',handles)
% Start by updating the Model object
if ~ismember(Method,{'bSSFP','SIRFSE','SPGR'})
if isappdata(0,'Model') && strcmp(class(getappdata(0,'Model')),Method) % if same method, load the current class with parameters
Model = getappdata(0,'Model');
else % otherwise create a new object of this method
modelfun = str2func(Method);
Model = modelfun();
end
SetAppData(Model)
end
% Now create Simulation panel
handles.methodfiles = fullfile(handles.root,'Models_Functions',[Method 'fun']);
if ismember(Method,{'bSSFP','SIRFSE','SPGR'})
set(handles.SimPanel,'Visible','off') % hide the simulation panel
set(handles.uipanel35,'Visible','on') % show the simulation panel
PathName = fullfile(handles.methodfiles,'Parameters');
LoadDefaultOptions(PathName);
else
% find the Simulation functions of the selected Method
Methodfun = methods(Method);
Simfun = Methodfun(~cellfun(@isempty,strfind(Methodfun,'Sim_')));
% Update Options Panel
set(handles.uipanel35,'Visible','off') % hide the simulation panel for qMT methods
if isempty(Simfun)
set(handles.SimPanel,'Visible','off') % hide the simulation panel
else
set(handles.SimPanel,'Visible','on') % show the simulation panel
delete(setdiff(findobj(handles.SimPanel),handles.SimPanel))
N = length(Simfun); %
Jh = min(0.14,.8/N);
J=1:max(N,6); J=(J-1)/max(N,6)*0.85; J=1-J-Jh-.01;
for i = 1:N
if exist([Simfun{i} '_GUI'],'file')
uicontrol('Style','pushbutton','String',strrep(strrep(Simfun{i},'Sim_',''),'_',' '),...
'Parent',handles.SimPanel,'Units','normalized','Position',[.04 J(i) .92 Jh],...
'HorizontalAlignment','center','FontWeight','bold','Callback',...
@(x,y) SimfunGUI([Simfun{i} '_GUI']));
end
end
end
end
% Update Options Panel
h = findobj('Tag','OptionsGUI');
if ~isempty(h)
delete(h);
end
OpenOptionsPanel_Callback(hObject, eventdata, handles)
% Show FileBrowser
FileBrowserList = GetAppData('FileBrowserList');
MethodNum = find(strcmp([FileBrowserList.MethodID],Method));
for i=1:length(FileBrowserList)
FileBrowserList(i).Visible('off');
end
FileBrowserList(MethodNum).Visible('on');
guidata(hObject, handles);
function SimfunGUI(functionName)
Model = getappdata(0,'Model');
SimfunGUI = str2func(functionName);
SimfunGUI(Model);
function MethodSelection_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% SET DEFAULT METHODSELECTION
function DefaultMethodBtn_Callback(hObject, eventdata, handles)
Method = GetMethod(handles);
setappdata(0, 'Method', Method);
save(fullfile(handles.root,'Common','Parameters','DefaultMethod.mat'),'Method');
% SIMCURVE
function SimCurveBtn_Callback(hObject, eventdata, handles)
SetActive('SimCurve', handles);
% SIMVARY
function SimVaryBtn_Callback(hObject, eventdata, handles)
SetActive('SimVary', handles);
% SIMRND
function SimRndBtn_Callback(hObject, eventdata, handles)
SetActive('SimRnd', handles);
% SET ACTIVE PANEL
function SetActive(panel, handles)
setappdata(0, 'CurrentPanel', panel);
Panels = {'SimCurve', 'SimVary', 'SimRnd', 'FitData'};
for ii = 1:length(Panels)
if (strcmp(panel,Panels{ii}))
PanelOn(Panels{ii}, handles);
else
PanelOff(Panels{ii}, handles);
end
end
function PanelOn(panel, handles)
eval(sprintf('set(handles.%sPanel, ''Visible'', ''on'')', panel));
function PanelOff(panel, handles)
eval(sprintf('set(handles.%sPanel, ''Visible'', ''off'')', panel));
% OPEN OPTIONS
function OpenOptionsPanel_Callback(hObject, eventdata, handles)
Method = GetAppData('Method');
switch Method
case 'bSSFP'
bSSFP_OptionsGUI(gcf);
case 'SPGR'
SPGR_OptionsGUI(gcf);
case 'SIRFSE'
SIRFSE_OptionsGUI(gcf);
otherwise
Model = getappdata(0,'Model');
Custom_OptionsGUI(gcf,Model);
end
% UPDATE OPTIONS
function UpdateOptions(Sim,Prot,FitOpt)
h = findobj('Tag','OptionsGUI');
if ~isempty(h)
OptionsGUIhandles = guidata(h);
set(OptionsGUIhandles.SimFileName, 'String', Sim.FileName);
set(OptionsGUIhandles.ProtFileName, 'String', Prot.FileName);
set(OptionsGUIhandles.FitOptFileName,'String', FitOpt.FileName);
end
% SimSave
function SimSave_Callback(hObject, eventdata, handles)
[FileName,PathName] = uiputfile(fullfile(handles.methodfiles,'SimResults','SimResults.mat'));
if PathName == 0, return; end
CurrentPanel = GetAppData('CurrentPanel');
switch CurrentPanel
case 'SimCurve'
SimCurveSaveResults(PathName, FileName, handles);
case 'SimVary'
SimVarySaveResults(PathName, FileName, handles);
case 'SimRnd'
SimRndSaveResults(PathName, FileName, handles);
end
% SimLoad
function SimLoad_Callback(hObject, eventdata, handles)
[Filename,Pathname] = uigetfile(fullfile('*.mat'));
if Pathname == 0, return; end
load(fullfile(Pathname,Filename));
switch FileType
case 'SimCurveResults'
SetActive('SimCurve', handles)
SimCurveLoadResults(Pathname, Filename, handles);
case 'SimVaryResults'
SetActive('SimVary', handles)
SimVaryLoadResults(Pathname, Filename, handles);
case 'SimRndResults'
SetActive('SimRnd', handles)
SimRndLoadResults(Pathname, Filename, handles);
otherwise
errordlg('Invalid simulation results file');
end
% SimGO
function SimGO_Callback(hObject, eventdata, handles)
CurrentPanel = GetAppData('CurrentPanel');
switch CurrentPanel
case 'SimCurve'
SimCurveGO(handles);
case 'SimVary'
SimVaryGO(handles);
case 'SimRnd'
SimRndGO(handles);
end
% GETAPPDATA
function varargout = GetAppData(varargin)
for k=1:nargin; varargout{k} = getappdata(0, varargin{k}); end
%SETAPPDATA
function SetAppData(varargin)
for k=1:nargin; setappdata(0, inputname(k), varargin{k}); end
% RMAPPDATA
function RmAppData(varargin)
for k=1:nargin; rmappdata(0, varargin{k}); end
% ##############################################################################################
% SINGLE VOXEL SIM
% ##############################################################################################
% SIMULATE DATA
function SimCurveGO(handles)
[Method,Prot,Sim] = GetAppData('Method','Prot','Sim');
switch Method
case 'bSSFP'; MTdata = bSSFP_sim(Sim, Prot, 1);
case 'SIRFSE'; MTdata = SIRFSE_sim(Sim, Prot, 1);
case 'SPGR'; MTdata = SPGR_sim(Sim, Prot, 1);
end
SetAppData(MTdata);
SimCurveUpdate(handles);
% POP FIG
function SimCurvePopFig_Callback(hObject, eventdata, handles)
FileName = get(handles.SimCurveFileName,'String');
Method = GetAppData('Method');
figure('Name',FileName);
switch Method
case 'bSSFP'
axe1 = handles.SimCurveAxe1;
axe2 = handles.SimCurveAxe2;
subplot(2,1,1);
handles.SimCurveAxe1 = gca;
subplot(2,1,2);
handles.SimCurveAxe2 = gca;
guidata(hObject, handles);
SimCurvePlotResults(handles);
handles.SimCurveAxe1 = axe1;
handles.SimCurveAxe2 = axe2;
guidata(hObject, handles);
otherwise
SimCurvePlotResults(handles);
end
% UPDATE FIT
function SimCurveUpdate_Callback(hObject, eventdata, handles)
SimCurveUpdate(handles);
function SimCurveUpdate(handles)
MTdata = GetAppData('MTdata');
SimCurveResults = SimCurveFitData(MTdata);
SimCurveSetFitResults(SimCurveResults, handles);
axes(handles.SimCurveAxe);
SimCurvePlotResults(handles);
SimCurveSaveResults(fullfile(handles.methodfiles,'SimResults'), 'SimCurveTempResults.mat', handles)
% SET FIT RESULTS TABLE
function SimCurveSetFitResults(SimCurveResults, handles)
SetAppData(SimCurveResults);
[Method, Sim, Prot] = GetAppData('Method','Sim','Prot');
Param = Sim.Param;
switch Method
case 'bSSFP'
names = {'F '; 'kr '; 'R1f'; 'R1r'; 'T2f '; 'M0f'};
input = [Param.F; Param.kr; Param.R1f; Param.R1r; Param.T2f; Param.M0f];
case 'SIRFSE'
names = {'F '; 'kr '; 'R1f'; 'R1r'; 'Sf '; 'Sr '; 'M0f'};
[Sr,Sf] = computeSr(Param, Prot);
input = [Param.F; Param.kr; Param.R1f; Param.R1r; Sf; Sr; Param.M0f];
case 'SPGR'
names = {'F '; 'kr '; 'R1f'; 'R1r'; 'T2f '; 'T2r'};
input = [Param.F; Param.kr; Param.R1f; Param.R1r; Param.T2f; Param.T2r];
end
error = 100*(SimCurveResults.table - input)./input;
data = [names, num2cell(input), num2cell(SimCurveResults.table), num2cell(error)];
set(handles.SimCurveResultsTable, 'Data', data);
% SAVE SIM RESULTS
function SimCurveSaveResults(PathName, FileName, handles)
FileType = 'SimCurveResults';
[Sim,Prot,FitOpt,MTdata,MTnoise,SimCurveResults] = GetAppData(...
'Sim','Prot','FitOpt','MTdata','MTnoise','SimCurveResults');
save(fullfile(PathName,FileName), '-regexp', '^(?!(handles)$).');
set(handles.SimCurveFileName,'String',FileName);
% LOAD SIM RESULTS
function SimCurveLoadResults(PathName, FileName, handles)
load(fullfile(PathName,FileName));
if (~exist('SimCurveResults', 'var'))
errordlg('Invalid fit simulation results file');
return;
end
SetAppData(Sim, Prot, FitOpt, MTdata, MTnoise, SimCurveResults);
UpdateOptions(Sim, Prot, FitOpt);
SimCurveSetFitResults(SimCurveResults, handles);
axes(handles.SimCurveAxe);
SimCurvePlotResults(handles);
set(handles.SimCurveFileName, 'String', FileName);
% FIT DATA
function SimCurveResults = SimCurveFitData(MTdata)
[Sim,Prot,FitOpt,Method] = GetAppData('Sim', 'Prot', 'FitOpt', 'Method');
FitOpt.R1 = computeR1obs(Sim.Param);
MTnoise = [];
if (Sim.Opt.AddNoise)
switch Method
case {'SIRFSE', 'SPGR'}
MTnoise = addNoise(MTdata, Sim.Opt.SNR, 'mt');
case 'bSSFP'
MTnoise = addNoise(MTdata, Sim.Opt.SNR, 'magnitude');
end
data = MTnoise;
else
data = MTdata;
end
switch Method
case 'bSSFP'
Fit = bSSFP_fit(data, Prot, FitOpt );
SimCurveResults = bSSFP_SimCurve(Fit, Prot, FitOpt );
case 'SPGR'
Fit = SPGR_fit(data, Prot, FitOpt );
SimCurveResults = SPGR_SimCurve(Fit, Prot, FitOpt );
case 'SIRFSE'
Fit = SIRFSE_fit(data, Prot, FitOpt);
SimCurveResults = SIRFSE_SimCurve(Fit, Prot, FitOpt );
end
SetAppData(MTnoise,SimCurveResults);
% PLOT DATA
function SimCurvePlotResults(handles)
[ Method, Sim, Prot, MTdata, MTnoise, SimCurveResults] = GetAppData(...
'Method','Sim','Prot','MTdata','MTnoise','SimCurveResults');
cla;
switch Method
case 'bSSFP'
axe(1) = handles.SimCurveAxe1;
axe(2) = handles.SimCurveAxe2;
cla(axe(1)); cla(axe(2));
bSSFP_PlotSimCurve(MTdata, MTnoise, Prot, Sim, SimCurveResults, axe);
case 'SIRFSE'
SIRFSE_PlotSimCurve(MTdata, MTnoise, Prot, Sim, SimCurveResults);
case 'SPGR'
SPGR_PlotSimCurve(MTdata, MTnoise, Prot, Sim, SimCurveResults);
end
grid('on');
% ##############################################################################################
% VARY PARAMETER SIM
% ##############################################################################################
% SIMULATE DATA
function SimVaryGO(handles)
[Sim,Prot,FitOpt,Method] = GetAppData('Sim','Prot','FitOpt','Method');
SimVaryOpt = GetSimVaryOpt(handles);
opt = SimVaryOpt.table;
fields = {'F';'kr';'R1f';'R1r';'T2f';'T2r';'M0f';'SNR'};
% Data simulation
for ii = 1:8
if opt(ii,1)
SimVaryOpt.min = opt(ii, 2);
SimVaryOpt.max = opt(ii, 3);
SimVaryOpt.step = opt(ii, 4);
SimVaryResults.(fields{ii}) = VaryParam(fields{ii},Sim,Prot,FitOpt,SimVaryOpt,Method);
end
if (getappdata(0, 'Cancel')); break; end
end
SetAppData(SimVaryResults);
SimVarySaveResults(fullfile(handles.methodfiles,'SimResults'), 'SimVaryTempResults.mat', handles);
SimVaryUpdatePopUp(handles);
axes(handles.SimVaryAxe);
SimVaryPlotResults(handles);
% ######################### SIMVARY OPTIONS ################################
% SAVE SimVaryOpt
function SimVaryOptSave_Callback(hObject, eventdata, handles)
SimVaryOpt = GetSimVaryOpt(handles);
SimVaryOpt.FileType = 'SimVaryOpt';
[FileName,PathName] = uiputfile(fullfile(handles.root,'Common','Parameters','SimVaryOpt.mat'));
if PathName == 0, return; end
save(fullfile(PathName,FileName),'-struct','SimVaryOpt');
setappdata(gcf, 'oldSimVaryOpt', SimVaryOpt);
% LOAD SimVaryOpt
function SimVaryOptLoad_Callback(hObject, eventdata, handles)
[FileName,PathName] = uigetfile(fullfile(handles.root,'Common','Parameters','*.mat'));
if PathName == 0, return; end
LoadSimVaryOpt(PathName, FileName, handles);
% RESET SimVaryOpt
function SimVaryOptReset_Callback(hObject, eventdata, handles)
SimVaryOpt = getappdata(gcf, 'oldSimVaryOpt');
SetSimVaryOpt(SimVaryOpt, handles);
% ########################### PLOT ########################################
% PLOT XAXIS MENU
function SimVaryPlotX_Callback(hObject, eventdata, handles)
axes(handles.SimVaryAxe);
SimVaryPlotResults(handles);
% PLOT YAXIS MENU
function SimVaryPlotY_Callback(hObject, eventdata, handles)
axes(handles.SimVaryAxe);
SimVaryPlotResults(handles);
% POP FIG
function SimVaryPopFig_Callback(hObject, eventdata, handles)
FileName = get(handles.SimVaryFileName,'String');
figure('Name',FileName);
SimVaryPlotResults(handles);
%############################ FUNCTIONS ###################################
% SAVE SIM RESULTS
function SimVarySaveResults(PathName, FileName, handles)
FileType = 'SimVaryResults';
[ Sim, Prot, FitOpt, SimVaryOpt, SimVaryResults] = GetAppData(...
'Sim','Prot','FitOpt','SimVaryOpt','SimVaryResults');
save(fullfile(PathName,FileName), '-regexp', '^(?!(handles)$).');
set(handles.SimVaryFileName, 'String', FileName);
% LOAD SIM RESULTS
function SimVaryLoadResults(PathName, FileName, handles)
load(fullfile(PathName, FileName));
if (~exist('SimVaryResults','var'))
errordlg('Invalid simulation results file');
return;
end
set(handles.SimVaryFileName,'String', FileName);
SetAppData(Sim, Prot, FitOpt, SimVaryOpt, SimVaryResults)
SetSimVaryOpt(SimVaryOpt, handles);
UpdateOptions(Sim, Prot, FitOpt);
SimVaryUpdatePopUp(handles);
axes(handles.SimVaryAxe);
SimVaryPlotResults(handles);
% GET GetSimVaryOpt Get SimVaryOpt from table
function SimVaryOpt = GetSimVaryOpt(handles)
data = get(handles.SimVaryOptTable,'Data');
table(:,2:4) = cell2mat(data(:,2:4));
table(:,1) = cell2mat(data(:,1));
SimVaryOpt.table = table;
SimVaryOpt.runs = str2double(get(handles.SimVaryOptRuns,'String'));
SetAppData(SimVaryOpt);
% SET SetSimVaryOpt Set SimVaryOpt table data
function SetSimVaryOpt(SimVaryOpt, handles)
data = [num2cell(logical(SimVaryOpt.table(:,1))), num2cell(SimVaryOpt.table(:,2:4))];
set(handles.SimVaryOptTable, 'Data', data);
set(handles.SimVaryOptRuns, 'String', SimVaryOpt.runs);
SetAppData(SimVaryOpt);
function SimVaryOptRuns_Callback(hObject, eventdata, handles)
GetSimVaryOpt(handles);
% LOAD LoadSimVaryOpt SimVaryOpt
function LoadSimVaryOpt(PathName, FileName, handles)
SimVaryOpt = load(fullfile(PathName, FileName));
if (~any(strcmp('FileType',fieldnames(SimVaryOpt))) || ~strcmp(SimVaryOpt.FileType,'SimVaryOpt') )
errordlg('Invalid options file');
return;
end
SetSimVaryOpt(SimVaryOpt, handles);
setappdata(gcf, 'oldSimVaryOpt', SimVaryOpt);
% UPDATE POPUP Update the PopUp menus
function SimVaryUpdatePopUp(handles)
[FitOpt, SimVaryResults] = GetAppData('FitOpt','SimVaryResults');
fieldsX = fieldnames(SimVaryResults);
fieldsY = FitOpt.names;
set(handles.SimVaryPlotX, 'Value', 1);
set(handles.SimVaryPlotY, 'Value', 1);
set(handles.SimVaryPlotX, 'String', fieldsX);
set(handles.SimVaryPlotY, 'String', fieldsY);
% PLOT RESULTS
function SimVaryPlotResults(handles)
[Sim, SimVaryResults] = GetAppData('Sim','SimVaryResults');
Param = Sim.Param;
Xcontents = cellstr(get(handles.SimVaryPlotX, 'String'));
Xaxis = Xcontents{get(handles.SimVaryPlotX, 'Value')};
Ycontents = cellstr(get(handles.SimVaryPlotY, 'String'));
Yaxis = Ycontents{get(handles.SimVaryPlotY, 'Value')};
Xmin = SimVaryResults.(Xaxis).x(1) - SimVaryResults.(Xaxis).step;
Xmax = SimVaryResults.(Xaxis).x(end) + SimVaryResults.(Xaxis).step;
X = SimVaryResults.(Xaxis).x;
Y = SimVaryResults.(Xaxis).(Yaxis).mean;
E = SimVaryResults.(Xaxis).(Yaxis).std;
cla;
hold on;
if (strcmp(Xaxis,Yaxis))
plot([Xmin Xmax], [Xmin Xmax], 'k-');
elseif (any(strcmp(Yaxis,fieldnames(Param))))
plot([Xmin Xmax],[Param.(Yaxis) Param.(Yaxis)], 'k-');
end
errorbar(X, Y, E, 'bo');
xlabel(sprintf('Input %s', Xaxis), 'FontWeight', 'Bold');
ylabel(sprintf('Fitted %s', Yaxis), 'FontWeight', 'Bold');
xlim([Xmin Xmax]);
hold off;
grid('on');
% ##############################################################################################
% RANDOM PARAMETERS SIM
% ##############################################################################################
%############################# SIMULATION #################################
% SIMULATE DATA
function SimRndGO(handles)
SimRndOpt = GetSimRndOpt(handles);
[ Sim, Prot, FitOpt, RndParam, Method] = GetAppData(...
'Sim','Prot','FitOpt','RndParam','Method');
if (isempty(RndParam)); RndParam = GetRndParam(handles); end
SimRndResults = VaryRndParam(Sim,Prot,FitOpt,SimRndOpt,RndParam,Method);
SetAppData(SimRndResults);
AnalyzeResults(RndParam, SimRndResults, handles);
SimRndSaveResults(fullfile(handles.methodfiles,'SimResults'), 'SimRndTempResults.mat', handles)
%########################### RANDOM OPTIONS ###############################
% SAVE SimRndOpt
function SimRndOptSave_Callback(hObject, eventdata, handles)
SimRndOpt = GetSimRndOpt(handles);
SimRndOpt.FileType = 'SimRndOpt';
[FileName,PathName] = uiputfile(fullfile(handles.root,'Common','Parameters','SimRndOpt.mat'));
if PathName == 0, return; end
save(fullfile(PathName,FileName), '-struct', 'SimRndOpt');
setappdata(gcf, 'oldSimRndOpt', SimRndOpt);
% LOAD SimRndOpt
function SimRndOptLoad_Callback(hObject, eventdata, handles)
[FileName,PathName] = uigetfile(fullfile(handles.root,'Common','Parameters','*.mat'));
if PathName == 0, return; end
LoadSimRndOpt(PathName, FileName, handles);
% RESET SimRndOpt
function SimRndOptReset_Callback(hObject, eventdata, handles)
SimRndOpt = getappdata(0, 'oldSimRndOpt');
SetSimRndOpt(SimRndOpt, handles);
% SimRndOpt TABLE EDIT
function SimRndOptTable_CellEditCallback(hObject, eventdata, handles)
SimRndOptEdit(handles);
% NUMVOXELS
function SimRndOptVoxels_Callback(hObject, eventdata, handles)
SimRndOptEdit(handles);
% GET RND OPT
function SimRndOpt = GetSimRndOpt(handles)
data = get(handles.SimRndOptTable, 'Data');
table(:,2:3) = cell2mat(data(:,2:3));
table(:,1) = cell2mat(data(:,1));
SimRndOpt.table = table;
SimRndOpt.NumVoxels = str2double(get(handles.SimRndOptVoxels, 'String'));
SetAppData(SimRndOpt);
% SET RND OPT
function SetSimRndOpt(SimRndOpt,handles)
data = [num2cell(logical(SimRndOpt.table(:,1))), num2cell(SimRndOpt.table(:,2:3))];
set(handles.SimRndOptTable, 'Data', data);
set(handles.SimRndOptVoxels, 'String', SimRndOpt.NumVoxels);
SetAppData(SimRndOpt);
% LOAD RND OPT
function LoadSimRndOpt(PathName, FileName, handles)
FullFile = fullfile(PathName,FileName);
if PathName == 0, return; end
SimRndOpt = load(FullFile);
if (~any(strcmp('FileType',fieldnames(SimRndOpt))) || ~strcmp(SimRndOpt.FileType,'SimRndOpt') )
errordlg('Invalid random parameters options file');
return;
end
SetSimRndOpt(SimRndOpt,handles);
setappdata(0, 'oldSimRndOpt', SimRndOpt);
% RND OPT EDIT
function SimRndOptEdit(handles)
RndParam = GetRndParam(handles);
SetAppData(RndParam);
% GETRNDPARAM
function SimRndGetParam_Callback(hObject, eventdata, handles)
SimRndOptEdit(handles)
SimRndUpdatePopUp(handles);
SimRndPlotResults(handles);
% GET RANDOM PARAMETERS
function RndParam = GetRndParam(handles)
Sim = GetAppData('Sim');
Param = Sim.Param;
SimRndOpt = GetSimRndOpt(handles);
n = SimRndOpt.NumVoxels;
Vary = SimRndOpt.table(:,1);
Mean = SimRndOpt.table(:,2);
Std = SimRndOpt.table(:,3);
fields = {'F','kr','R1f','R1r','T2f','T2r','M0f'};
for ii = 1:length(fields)
if(Vary(ii)); RndParam.(fields{ii}) = abs(Mean(ii) + Std(ii)*(randn(n,1)));
else RndParam.(fields{ii}) = Param.(fields{ii})*(ones(n,1));
end
end
SetAppData(RndParam);
% ########################### SIM RESULTS #################################
% SAVE SIM RESULTS
function SimRndSaveResults(PathName, FileName, handles)
FileType = 'SimRndResults';
[ Sim, Prot, FitOpt, SimRndOpt, RndParam, SimRndResults] = GetAppData(...
'Sim','Prot','FitOpt','SimRndOpt','RndParam','SimRndResults');
save(fullfile(PathName,FileName),'Sim','Prot','FitOpt','SimRndOpt','RndParam','SimRndResults','FileType');
set(handles.SimRndFileName, 'String', FileName);
% LOAD SIM RESULTS
function SimRndLoadResults(PathName, FileName, handles)
load(fullfile(PathName,FileName));
if (~exist('SimRndResults','var'))
errordlg('Invalid random simulation results file');
return;
end
set(handles.SimRndFileName,'String', FileName);
SetAppData(Sim,Prot,FitOpt,SimRndOpt,RndParam,SimRndResults);
SetSimRndOpt(SimRndOpt,handles)
UpdateOptions(Sim,Prot,FitOpt);
AnalyzeResults(RndParam, SimRndResults, handles);
% ANALYZE SIM RESULTS
function SimRndStats = AnalyzeResults(Input, Results, handles)
Fields = intersect(fieldnames(Input), fieldnames(Results));
for ii = 1:length(Fields)
n = length(Input.(Fields{ii}));
SimRndStats.Error.(Fields{ii}) = Results.(Fields{ii}) - Input.(Fields{ii}) ;
SimRndStats.PctError.(Fields{ii}) = 100*(Results.(Fields{ii}) - Input.(Fields{ii})) ./ Input.(Fields{ii});
SimRndStats.MPE.(Fields{ii}) = 100/n*sum((Results.(Fields{ii}) - Input.(Fields{ii})) ./ Input.(Fields{ii}));
SimRndStats.RMSE.(Fields{ii}) = sqrt(sum((Results.(Fields{ii}) - Input.(Fields{ii})).^2 )/n);
SimRndStats.NRMSE.(Fields{ii}) = SimRndStats.RMSE.(Fields{ii}) / (max(Input.(Fields{ii})) - min(Input.(Fields{ii})));
end
SetAppData(SimRndStats);
SimRndUpdatePopUp(handles);
SimRndPlotResults(handles);
% ############################## FIGURE ###################################
% UPDATE POPUP MENU
function SimRndUpdatePopUp(handles)
[RndParam, SimRndResults, SimRndStats] = GetAppData('RndParam','SimRndResults','SimRndStats');
axes(handles.SimRndAxe);
colormap('default');
set(handles.SimRndPlotX, 'Value', 1);
set(handles.SimRndPlotY, 'Value', 1);
PlotTypeFields = cellstr(get(handles.SimRndPlotType, 'String'));
PlotType = PlotTypeFields{get(handles.SimRndPlotType, 'Value')};
switch PlotType
case 'Input parameters'
XdataFields = fieldnames(RndParam);
set(handles.SimRndPlotX, 'String', XdataFields);
set(handles.SimRndPlotY, 'String', 'Voxels count');
case 'Fit results'
XdataFields = SimRndResults.fields;
set(handles.SimRndPlotX, 'String', XdataFields);
set(handles.SimRndPlotY, 'String', 'Voxels count');
case 'Input vs. Fit'
XdataFields = fieldnames(RndParam);
set(handles.SimRndPlotX, 'String', XdataFields);
YdataFields = SimRndResults.fields;
set(handles.SimRndPlotY, 'String', YdataFields);
case 'Error'
XdataFields = fieldnames(SimRndStats.Error);
set(handles.SimRndPlotX, 'String', XdataFields);
set(handles.SimRndPlotY, 'String', 'Voxels count');
case 'Pct error'
XdataFields = fieldnames(SimRndStats.PctError);
set(handles.SimRndPlotX, 'String', XdataFields);
set(handles.SimRndPlotY, 'String', 'Voxels count');
case 'RMSE'
set(handles.SimRndPlotX, 'String', 'Parameters');
set(handles.SimRndPlotY, 'String', 'RMSE');
case 'NRMSE'
set(handles.SimRndPlotX, 'String', 'Parameters');
set(handles.SimRndPlotY, 'String', 'NRMSE');
case 'MPE'
set(handles.SimRndPlotX, 'String', 'Parameters');
set(handles.SimRndPlotY, 'String', 'MPE');
end
guidata(gcbf,handles);
% PLOT DATA
function SimRndPlotResults(handles)
[RndParam, SimRndResults, SimRndStats] = GetAppData('RndParam','SimRndResults','SimRndStats');
PlotTypeFields = cellstr(get(handles.SimRndPlotType, 'String'));
PlotType = PlotTypeFields{get(handles.SimRndPlotType, 'Value')};
XdataFields = cellstr(get(handles.SimRndPlotX, 'String'));
Xdata = XdataFields{get(handles.SimRndPlotX, 'Value')};
YdataFields = cellstr(get(handles.SimRndPlotY, 'String'));
Ydata = YdataFields{get(handles.SimRndPlotY, 'Value')};
switch PlotType
case 'Input parameters'
hist(RndParam.(Xdata), 30);
xlabel(['Input ', Xdata], 'FontWeight', 'Bold');
ylabel(Ydata, 'FontWeight',' Bold');
case 'Fit results'
hist(SimRndResults.(Xdata), 30);
xlabel(['Fitted ', Xdata], 'FontWeight','Bold');
ylabel(Ydata, 'FontWeight','Bold');
case 'Input vs. Fit'
plot(RndParam.(Xdata), SimRndResults.(Ydata),'.');
xlabel(['Input ' , Xdata], 'FontWeight','Bold');
ylabel(['Fitted ', Ydata], 'FontWeight','Bold');
case 'Error'
hist(SimRndStats.Error.(Xdata), 30);
xlabel(['Error ', Xdata], 'FontWeight','Bold');
ylabel(Ydata, 'FontWeight','Bold');
case 'Pct error'
hist(SimRndStats.PctError.(Xdata), 30);
xlabel(['Pct Error ', Xdata], 'FontWeight','Bold');
ylabel(Ydata, 'FontWeight','Bold');
case 'RMSE'
Fields = fieldnames(SimRndStats.RMSE);
for ii = 1:length(Fields)
dat(ii) = SimRndStats.RMSE.(Fields{ii});
end
bar(diag(dat),'stacked');
set(gca,'Xtick',1:5,'XTickLabel', Fields);
legend(Fields);
xlabel('Fitted parameters', 'FontWeight','Bold');
ylabel('Root Mean Squared Error', 'FontWeight','Bold');
case 'NRMSE'
Fields = fieldnames(SimRndStats.NRMSE);
for ii = 1:length(Fields)
dat(ii) = SimRndStats.NRMSE.(Fields{ii});
end
bar(diag(dat),'stacked');
set(gca,'Xtick',1:5,'XTickLabel', Fields);
legend(Fields);
xlabel('Fitted parameters', 'FontWeight','Bold');
ylabel('Normalized Root Mean Squared Error', 'FontWeight','Bold');
case 'MPE'
Fields = fieldnames(SimRndStats.MPE);
for ii = 1:length(Fields)
dat(ii) = SimRndStats.MPE.(Fields{ii});
end
bar(diag(dat),'stacked');
set(gca,'Xtick',1:5,'XTickLabel', Fields);
legend(Fields);
xlabel('Fitted parameters', 'FontWeight','Bold');
ylabel('Mean Percentage Error', 'FontWeight','Bold');
end
% ########################### PLOT RESULTS ################################
function SimRndPopFig_Callback(hObject, eventdata, handles)
FileName = get(handles.SimRndFileName,'String');
figure('Name', FileName);
SimRndPlotResults(handles);
function SimRndPlotType_Callback(hObject, eventdata, handles)
SimRndUpdatePopUp(handles);
SimRndPlotResults(handles);
function SimRndPlotX_Callback(hObject, eventdata, handles)
SimRndPlotResults(handles);
function SimRndPlotY_Callback(hObject, eventdata, handles)
SimRndPlotResults(handles);
% ##############################################################################################
% FIT DATA
% ##############################################################################################
% FITDATA
function FitDataBtn_Callback(hObject, eventdata, handles)
contents = cellstr(get(handles.MethodSelection, 'String'));
Method = contents{get(handles.MethodSelection, 'Value')};
SetActive('FitData', handles);
% ############################# FIT DATA ##################################
% FITDATA GO
function FitGO_Callback(hObject, eventdata, handles)
SetActive('FitData', handles);
Method = GetMethod(handles);
setappdata(0, 'Method', Method);
FitGo_FitData(hObject, eventdata, handles);
% Original FitGo function
function FitGo_FitData(hObject, eventdata, handles)
% Get data
data = GetAppData('Data');
[Method, Prot, FitOpt] = GetAppData('Method','Prot','FitOpt');
% If SPGR with SledPike, check for Sf table
if (strcmp(Method,'SPGR') && (strcmp(FitOpt.model, 'SledPikeCW') || strcmp(FitOpt.model, 'SledPikeRP')))
if (~isfield(Prot,'Sf') || isempty(Prot.Sf))
errordlg('An Sf table needs to be computed for this protocol prior to fitting. Please use the protocol panel do do so.','Missing Sf table');
return;
end
end
% Do the fitting
if ismember(Method,{'bSSFP','SIRFSE','SPGR'})
FitResults = FitData(data,Prot,FitOpt,Method,1);
else
Model = getappdata(0,'Model');
FitResults = FitDataCustom(data,Model,1);
FitResults.Model = Model;
end
% Save info with results
FileBrowserList = GetAppData('FileBrowserList');
MethodList = getappdata(0, 'MethodList');
MethodList = strrep(MethodList, '.m', '');
MethodCount = numel(MethodList);
for i=1:MethodCount
if FileBrowserList(i).IsMethodID(Method)