forked from NISOx-BDI/SwE-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swe_conman.m
1823 lines (1608 loc) · 75.3 KB
/
swe_conman.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=swe_conman(varargin)
% Contrast manager: GUI for defining valid contrasts
%==========================================================================
% FORMAT varargout=swe_conman(varargin)
% - An embedded callback, multi-function function
% - For detailed programmers comments,
% see format specifications in main body of program (below user
% help)
%__________________________________________________________________________
%
% The contrast manager is a user interface for the selection and definition
% of contrasts for a given SwE design. At present, the contrast manager
% provides only interactive GUI facilities.
%
% See also: swe_getSPM.m, swe_contrasts.m, swe_contrasts_WB.m
%
%==========================================================================
% U s i n g t h e S W E C o n t r a s t M a n a g e r G U I
%==========================================================================
%
% The contrast manager graphicsl user interface (GUI) is a dialog box for
% the specification and selection of contrasts. The contrast selection
% interface is presented initially, pressing the "Define new contrast..."
% button pops up the contrast definition interface:
%
%__________________________________________________________________________
% ConMan: Contrast selection interface
%
% The contrast selection interface consists of:
%
% * "Show" statistic type radio-buttons: "t-contrasts", "F-contrasts",
% "All":
% Selects the types of contrast that are presented for selection in the
% contrast selection list box. Depending on the use of the contrast
% manager, some options may be unavailable.
%
% * List-box of currently defined contrasts.
% Each contrast is listed by number, type ("T" or "F"), and name. Only
% contrasts of types specified by the settings of the "show"
% radiobuttons are shown.
% Select contrasts by clicking on their entry in the list-box. To
% select multiple contrasts (for conjunctions or masking, if
% appropriate), the standard techniques of drag selection and
% shift-clicking can be used for selecting a set of adjacent contrasts,
% control-click to select individual contrasts separated in the list.
% Selected contrasts are highlit in black.
%
% * Image of the design matrix:
% A grey-scale representation of the design matrix, to aid
% interpretation of the contrasts.
%
% The design matrix is "surfable": Clicking (and holding or dragging)
% around the design matrix image reports the corresponding value of the
% design matrix ('normal' click - "left" mouse button usually), the
% image filename ('extend' mouse click - "middle" mouse), or parameter
% name ('alt' click - "right" mouse). Double clicking the design matrix
% image extracts the design matrix into the base MATLAB workspace.
% (Surfing is handled by swe_DesRep.m)
%
% * Parameter estimability bar
% Under the design matrix the parameter estimability is displayed as
% a 1xp matrix of grey and white squares. Parameters that are not
% uniquely specified by the model are shown with a grey patch.
%
% Recall that only uniquely estimable parameters can be examined
% individually with [0,...,0,1,0,...,0] type contrats.
%
% Surfing the estimability image reports the parameter names and
% their estimability. Double clicking extracts the estimability
% vector into the base MatLab workspace.
%
% * Contrast weights plot/image:
% The weights of the selected contrast(s) are imaged above the design
% matrix, labelled by their contrast number. t-contrasts are displayed
% as bar-charts, F-contrasts have their weights matrix c' depicted as a
% grey-scale image.
%
% Again, the contrast representation is "surfable": Clicking and
% dragging over the contrast image reports the contrast name, type and
% number, and the value of the contrast at the mouse location.
%
% * "Define new contrast..." button:
% Pops up the contrast definition interface (described below)
%
% * "Reset" button:
% Clears the current contrast selection.
%
% * "Done" button:
% Press when the required contrasts have been selected.
%
% * Status line:
% This indicates how many contrasts have been selected, how
% multi-contrast selections will be handled, and whether you can press
% "Done" yet!
%
% * A "?" help button (at the right hand end of the status line):
% This brings up this help text (the help text for swe_conman.m) in
% the spm help viewer.
%
% In addition, the dialog has a figure ContextMenu, accessed by
% right-clicking in the figure background: In addition to providing
% repeats of the "Define new contrast...", "Reset", "Done" & "Help"
% buttons described above, there are two additional options:
%
% - crash out: this bails out of the contrast manager in a nice way!
% - rename: This permits a single contrast to be re-named. You
% must select the contrast to be renamed before pulling
% up the context menu for this option to be available.
%
%__________________________________________________________________________
% ConMan: Contrast definition interface
%
% To define a contrast, you must specify:
% 1) a name
% 2) the statistic type: "t-contrast" for SwE{T} or "F-contrast" for SwE{F}
% 3) a valid set of contrast weights
% (for F-contrasts, this can also be generated given a reduced
% (design as a partition of the existing design
%
% The contrast definition interface consists of:
%
% * A lilac "name" edit widget for naming the new contrast
% Type the name of the contrast in this widget.
% Press return or move the focus to enter the name.
%
% * Radio-buttons for selecting statistic type: "t-contrast" or "F-contrast"
%
% * A large lilac edit widget for entering "contrast weights matrix"
% - Note that contrast weights should be entered transposed, with
% contrast weights in rows.
% - Zero's will be automatically added to the right hand side of
% contrast weights as needed to give contrast weights matching the
% number of parameters. For example, if you are interested in
% contrasting the first two conditions of a design four parameters
% (design matrix with 4 columns), you need only enter "+1 -1". The
% contrast manager will parse this to [+1 -1 0 0].
% - For t-contrasts, this will only accept a single line of input,
% since contrast weights c' for an SwE{T} map must be a row-vector.
% Pressing <return> or moving the focus (by clicking on another GUI
% element, such as the "Submit" button) will enter the contrast
% weights for parsing.
% - For F-contrasts, the widget accepts multi-line input.
% Pressing <return> will move to a new line. Press <ctrl>-<return> or
% move the focus (by clicking on another GUI element, such as the
% "Submit" button) to enter the contrast weights for parsing.
% - Text entered in the "contrast weights matrix" is evaluated in the
% base workspace. Thus, matlab functions can be used in the widget,
% and base workspace variables can be referred to. (See the help for
% spm_input.m for more tips on using evaluated input widgets.)
%
% * For F-contrasts, a "columns for reduced design" edit widget appears:
% - Here you can specify SwE{F} maps by specifying the reduced design
% as a sub-partition of the current design matrix.
% - Enter the indicies of the design matrix columns you wish to retain
% in the reduced design.
% - Pressing <return> or moving the focus (by clicking on another GUI
% element, such as the "Submit" button) will enter the column indicies
% for parsing.
% - An F-contrast weights matrix is constructed from the specified
% partitioning. (The corresponding F-contrast weights are imaged
% above the design matrix picture. Double click (or "surf") the
% contrast image to see the actual values of the F-contrast weights
% matrix.)
% - Again, text entered in the "columns for reduced design" is
% evaluated in the base workspace, permitting the use of functions
% and variables available in the base workspace.
% - (Note that the F-contrast weights matrix produced may not be the
% simplest F-contrast possible for this test, and that the F-contrast
% weights matrix may not be full rank (e.g. may have two rows where
% one would do). Nontheless, the F-test is equivalent for the
% specified partitioning.
%
% * "Submit" button:
% This button can be used to force parsing of the contrast weights (or
% columns for reduced design).
%
% * contrast parsing errors pane & contrast parsing info pane:
% - Once the contrast weights matrix has been entered in the GUI, the
% inout is parsed.
% - First, each line is evaluated.
% - Then, the results for each line are checked to ensure thay define
% valid contrasts, with trailing zeros added as necessary so the
% contrast length matches the number of parameters.
% - Errors encountered during parsing, and invalid contrast weights,
% are reported in the "contrast parsing errors" pane in red text.
% Usually the contrast cannot be defined if there are any errors!
% - Information messages regarding contrast parsing appear in the lower
% "contrast parsing info" pane in green text.
% - When defining an F-contrast via "columns for reduced design", the
% string defining the indicies is similarly parsed, with errors and
% information messages appearing in the two panes.
%
% * Contrast weights plot/image:
% Depicts the contrast once valid contrast weights have been specified.
%
% * Image of the design matrix:
% (As described above for the contrast selection interface)
%
% * Parameter estimability bar
% (As described above for the contrast selection interface)
%
% * "Reset" button:
% Resets the contrast definition interface, clearing any contrast
% currently being defined.
%
% * "Cancel" button:
% Returns to the contrast selection interface without defining a new
% contrast.
%
% * "OK" button:
% Once a valid set of contrast weights has been defined, and the
% contrast named, pressing "OK" defines the contrast and returns to the
% contrast selection interface, with the newly defined contrast
% selected.
%
% * Status line:
% This indicates progress in contrast definition.
% Once a valid set of contrast weights have been specified, and a
% the contrast named, then the status line turns green, indicating
% that the current contrast can be defined by presing "OK".
%
% * A "?" help button (at the right hand end of the status line):
% (As described above for the contrast selection interface)
%
%
%==========================================================================
% S W E C o n t r a s t m a n a g e m e n t
%==========================================================================
%
% Contrasts are stored by SwE in a single structure (See spm_FcUtil.m
% for the definition and handling of the contrast structure.)
%
% Note that the xCon structure for each contrast contains data specific
% to the current experimental design. Therefore, contrast structures
% can only be copied between analyses (to save re-entering contrasts)
% if the designs are *identical*.
%
% Although the contrasts are named by the user, they are referred to
% internally and on the corresponding contrast, ESS and SwE images (see
% swe_getSPM.m) by their contrast number, which indexes them in the
% order in which they were created. Because of this, it can be rather
% involved to delete any but the most recently defined contrast: All
% file references and indices would have to be canonicalised! Thus, no
% "delete" function is provided (as yet).
%
%__________________________________________________________________________
% Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging
% Andrew Holmes
% Based on: swe_conman.m 4617 2012-01-11 15:46:16Z will
%==========================================================================
% - FORMAT specifications
%==========================================================================
%( This is a multi function function: If the first argument is a string,)
%( then this is the action string, specifying the particular action )
%( function to take. )
%
% FORMAT [I,xCon] = swe_conman(SwE,STATmode,n,Prompt,Mcstr,OK2chg)
%
% SwE.xX - Design Matrix structure
% - (see spm_spm.m for structure)
% - fields used directly are:
% .name - cellstr of parameter names
%
% SwE.xCon (in) - Contrast definitions structure array
% (see spm_FcUtil.m for structure, rules & handling)
% - defaults to empty contrast structure
% - fields used directly are:
% .name - contrast name string
% .STAT - character describing statistic required: 'T' or 'F'
% .c - contrast weights (column) vector / matrix
%
% STATmode - string indicating STAT modes to allow contrast
% selection/definition for:
% - 'T' to limit to contrasts defined for SwE{t}
% - 'F' to limit to contrasts defined for SwE{F}
% - 'T|F' to allow either contrasts for SwE{t} or SwE{F}
% (both may be defined, but only one type may be selected)
% - 'T&F' to allow both contrasts for SwE{t} and SwE{F}
% - defaults to 'T|F'
%
% n - Number of contrasts to select, Inf for unlimited
%
% Prompt - Prompt string
%
% Mcstr - string to describe multiple contrast selection
% E.g. ' for conjunction' will result in the status message
% reading 'Selected 2 contrasts for conjunction' when
% two contrasts are selected.
%
% OK2chg - logical, specifying whether the contrast structure can be
% changed. If false, then new contrasts cannot be defined, and
% existing contrasts cannot be renamed.
%
% I - Index (or indices) of contrasts selected
%
% xCon (out) - Contrast definitions structure array (updated)
%
% ----------------
%
% [F,cF] = swe_conman('Initialise',...
% Vis,SwE,STATmode,n,Prompt,Mcstr,OK2chg)
% Initialise ConMan GUI for contrast selection/definition
% Vis - Initialisation action:
% 'close' - closes ConMan window
% 'off' - hides ConMan window
% 'reset' - hides and resets ConMan window
% 'on' - initialises ConMan window using arguments given
% SwE.xX - design matrix structure
% SwE.xCon - contrast definitions structure array
% STATmode - string indicating STAT modes to allow contrast
% n - number of contrasts to select, Inf for unlimited
% Prompt - Prompt string
% Mcstr - string to describe multiple contrast selection
% OK2chg - logical, specifying whether contrast structure can be changed
% F - figure used for contrast manager window
% cF - Figure which was current before function call
%
% FORMAT swe_conman('ImDesMtx',xX,h)
% Utility function to display design matrix image & setup "surfing"
% xX - Design Matrix structure
% - the first of {xX.nX, xX.xKXs.X} is used for display
% .nX - Desgin matrix already scaled for display
% .xKXs.X - temporally filtered design matrix (within space structure)
% .name - px1 CellStr of parameter names
%
% FORMAT swe_conman('ImParEst',xX,h)
% Utility function to display parameter estimability & setup "surfing"
% xX - Design Matrix structure
% xX.xKXs - space structure for K*X
% xX.name - px1 CellStr of parameter names
% h - handle of axes to use
%
% FORMAT swe_conman('ListCon',hConList,xCon,STAT,I)
% Utility function to list contrasts in ListBox
% hConList - handle of GUI ListBox object
% xCon - current contrast definitions structure array
% STAT - STAT character: 'T' or 'F' of empty (show all)
% I - indicies of currently selected contrasts
%
% FORMAT swe_conman('GraphCons',xCon,I,F)
% Utility function to display contrast image/bar-graph & setup "surfing"
% xCon - contrast definitions structure array
% I - indicies of contrasts to display
% F - handle of 'ConMan' figure
%
% FORMAT swe_conman('StatusLine',F,str,col)
% Utility function to update ConMan window statusline
% F - handle of 'ConMan' figure
% str - string to display
% (defaults to status of contrast selection)
% col - colour to use [defaults to 'w' - white
%
% FORMAT swe_conman('Done_CB')
% CallBack for "Done" button on ConMan contrast selection interface
%
% FORMAT swe_conman('ConList_CB')
% CallBack for contrast selection ListBox
%
% FORMAT STAT = swe_conman('TFA')
% CallBack for 'T','F' or 'any' STAT selection RadioButtons
% FORMAT STAT = swe_conman('TFA',F,STAT)
% Initialisation of 'T','F' or 'any' STAT selection RadioButtons
% FORMAT STAT = swe_conman('TFA',F,STAT,STATmode)
% Function to set STAT & STATmode
% Initialisation of 'T','F' or 'any' STAT selection RadioButtons & STATmode
% F - handle of 'ConMan' figure
% STAT - STAT character: 'T' or 'F' of empty (all)
% STATmode - string indicating STAT modes to allow contrast
%
% FORMAT swe_conman('FConMenu_CB')
% CallBack to set state of ConMan contrast selection figure context menu
%
% FORMAT swe_conman('Rename_CB')
% CallBack to handle contrast renaming
%
% FORMAT [c,I,emsg,imsg,msg] = swe_conman('ParseCon',cstr,X,STAT)
% Contrast weights parser: Catch evaluation errors and invalid contrasts
% cstr - string (or cellstr) to evaluate to get contrast(s)
% X - design matrix
% STAT - 'T' or 'F' (for contrast checking)
% c - contrast weights matrix
% I - logical validity indicator: indicates which rows of
% cellstr(cstr) generated valid contrasts which were
% included in c
% emsg - cellstr of error messages produced during parsing
% imsg - cellstr of information messages for valid contrasts
% msg - cellstr of all messages produced during parsing,
% one cell per string in cstr
%
% FORMAT [iX0,I,emsg,imsg] = swe_conman('ParseIStr',str,max)
% DesMtx column index parser: Catch eval errors and invalid indices
% str - string for evaluation to get column indices
% max - number of columns in design matrix
% iX0 - vector of column indices: '!' if evaluation error
% I - 0 if evaluation error, 1 if evaluated OK
% emsg - error message
% (evaluation errors, non-integer indices, out of range indices)
% imsg - information message (valid indices)
%
% FORMAT swe_conman('Reset_CB')
% CallBack handler for "reset" button on contrast selection interface
%
% FORMAT swe_conman('D_Setup_CB')
% CallBack handler for "Define new" button:
% Initialises Contrast Definition interface for use
%
% FORMAT swe_conman('D_ConMtx_CB')
% CallBack handler for contrast weights definition widget
% FORMAT swe_conman('D_X1cols_CB')
% Callback handler for F-contrast "columns for reduced design" widget
%
% FORMAT swe_conman('D_Reset_CB')
% CallBack handler for "reset" button on contrast definition interface
%
% FORMAT swe_conman('D_Cancel_CB')
% CallBack handler for "cancel" button on contrast definition interface
%
% FORMAT swe_conman('D_OK_CB')
% CallBack handler for "OK" button on contrast definiton interface
%
% FORMAT swe_conman('D_Status',F)
% Set status line of contrast definition interface
% F - Handle of ConMan figure [defaults gcbf]
%
% FORMAT [F,H] = swe_conman('CreateFig')
% Creates ConMan dialog figure (definition interface initially hidden)
% F - Handle of ConMan figure created
% H - Stricture of Handles:
% .hConList - handle of contrast selection ListBox
% .hDesMtxAx - handle of axes for design matrix imaging
% .hPrompt - handle of prompt text object
% .hSTATmode - handle of frame containing "T/F/All" radio buttons
% .hStatLin - handle of status line text object (in selection interface)
% .hNew - handle of "Define new contrast" pushbutton
%__________________________________________________________________________
% Version Info: $Format:%ci$ $Format:%h$
%-Parameters
%==========================================================================
COLOUR = [.8,.8,1]; %-Question background colour
PJump = 1; %-Jumping of pointer to ConMan window
%==========================================================================
% [I,xCon] = swe_conman(SwE,STATmode,n,Prompt,Mcstr,OK2chg)
%==========================================================================
if (nargin==0) || ~ischar(varargin{1})
%-Condition arguments
%----------------------------------------------------------------------
if nargin<6, OK2chg = 0; else OK2chg=varargin{6}; end
if nargin<5, Mcstr = ''; else Mcstr=varargin{5}; end
if nargin<4, Prompt='Select contrast(s)...'; else Prompt=varargin{4}; end
if nargin<3, n=1; else n=varargin{3}; end
if nargin<2, STATmode='T|F'; else STATmode=varargin{2}; end
if nargin<1, error('no SwE struct specified'); else SwE = varargin{1};end
%-Change to results directory
%----------------------------------------------------------------------
try, cd(SwE.swd); end
%----------------------------------------------------------------------
try
SwE.xCon;
catch
SwE.xCon = {};
end
%-Setup ConMan window & jump cursor
[F,cF] = swe_conman('Initialise','on',SwE,STATmode,n,Prompt,Mcstr,OK2chg);
if PJump
PLoc = get(0,'PointerLocation');
FRec = get(F,'Position');
set(0,'PointerLocation',[FRec(1)+FRec(3)/2, FRec(2)+FRec(2)/2])
end
%-Copy SwE structure for later change detecton, eg renamed contrasts
tmpSPM = SwE;
%-Wait until filenames have been selected
hDone = findobj(F,'Tag','Done');
waitfor(hDone,'UserData')
%-Exit with error if ConManWin deleted
if ~ishandle(hDone), error('Contrast Manager was quit!'), end
%-Get xCon, I & exit status
status = get(hDone,'UserData');
hConList = findobj(F,'Tag','ConList');
Q = get(hConList,'UserData');
I = Q(get(hConList,'Value'));
SwE = get(F,'UserData');
xCon = SwE.xCon;
%-Save SwE.mat only if SwE structure as changed
if ~isequal(tmpSPM,SwE)
if spm_check_version('matlab','7') >=0
save('SwE.mat', 'SwE', '-V6');
else
save('SwE.mat', 'SwE');
end
end
%-Reset and hide SelFileWin
swe_conman('Initialise','off');
%-Return focus to previous figure (if any)
set(0,'CurrentFigure',cF)
%-Jump cursor back to previous location
if PJump, set(0,'PointerLocation',PLoc), end
%-Exit with error if reset (status=-1)
if status == -1, error(['reset: ',mfilename,' bailing out!']), end
%-Output arguments
varargout={I,xCon};
return
end
%==========================================================================
% - Callbacks & Utility embedded functions
%==========================================================================
switch lower(varargin{1})
%======================================================================
case 'initialise'
%======================================================================
% [F,cF] = swe_conman('Initialise',Vis,SwE,STATmode,n,Prompt,Mcstr,OK2chg)
if nargin<2, Vis='on'; else Vis=varargin{2}; end
%-Recover ConMan figure handle (if it exists)
F = findobj(get(0,'Children'),'Flat','Tag','ConMan');
cF = get(0,'CurrentFigure'); %-Save current figure
switch lower(Vis)
%------------------------------------------------------------------
case 'close'
%------------------------------------------------------------------
close(F)
varargout = {[],cF};
return
%------------------------------------------------------------------
case {'off','reset'}
%------------------------------------------------------------------
varargout = {F,cF}; %-Return figure handles
if isempty(F), return, end %-Return if no ConMan win
set(F,'Visible','off') %-Make window Invisible
if strcmpi(Vis,'reset')
set(findobj(F,'Tag','Done'),'UserData',-1)
end
return
%------------------------------------------------------------------
case 'on'
%------------------------------------------------------------------
%-Sort out arguments
%--------------------------------------------------------------
if nargin<8, OK2chg = 0; else OK2chg=varargin{8}; end
if nargin<7, Mcstr = ''; else Mcstr=varargin{7}; end
Mcstr = cellstr(Mcstr); if length(Mcstr)<2, Mcstr{2}=''; end
if nargin<6, Prompt='Select contrast(s)'; else Prompt=varargin{6}; end
if nargin<5, n=Inf; else n=varargin{5}; end
if nargin<4, STATmode='T&F'; else STATmode=varargin{4}; end
if nargin<3, error('insufficient arguments'), end
SwE = varargin{3};
xCon = SwE.xCon;
xX = SwE.xX;
%-Create/find ConMan window
%--------------------------------------------------------------
if isempty(F) %-Create ConMan win
[F,H] = swe_conman('CreateFig');
else %-Get handles
H.hDesMtxAx = findobj(F,'Tag','DesMtxAx');
H.hParEstAx = findobj(F,'Tag','ParEstAx');
H.hConList = findobj(F,'Tag','ConList');
H.hPrompt = findobj(F,'Tag','Prompt');
H.hTFAf = findobj(F,'Tag','TFAf');
H.hSTATmode = findobj(F,'Tag','STATmode');
H.hStatLin = findobj(F,'Tag','StatusLine');
H.hNew = findobj(F,'Tag','New');
end
varargout = {F,cF}; %-Return figure handles
%-Store required parameters in UserData of various objects
%--------------------------------------------------------------
set(F, 'UserData',SwE)
set(H.hStatLin, 'UserData',Mcstr)
%-Initialise interface
%--------------------------------------------------------------
set(findobj(F,'Tag','Done'),'UserData',0) %-Init. Done UserData
STAT = swe_conman('TFA',F,'',STATmode); %-Init. TFA buttons
set(H.hPrompt,'String',Prompt,'UserData',n) %-Set prompt
swe_conman('ImDesMtx',xX,H.hDesMtxAx) %-Depict DesMtx
swe_conman('ImParEst',xX,H.hParEstAx) %-Parameter estimability
swe_conman('ListCon',H.hConList,xCon,STAT,[]) %-List contrasts
if OK2chg, tmp='on'; else tmp='off'; end %-OK to change xCon?
set(H.hNew,'Enable',tmp) %-En/dis-able change UI
%-**** if isempty(xCon), swe_conman(); end %-Go straight to DNewUI
%-Popup figure, retaining CurrentFigure
%--------------------------------------------------------------
set(get(findobj(F,'Tag','DefineNew'),'UserData'),'Visible','off')
%-Hide define UI
figure(F) %-PopUp figure
set(0,'CurrentFigure',cF) %-Return to prev. figure
return
otherwise
error('Unrecognised ''Vis'' option')
end
%======================================================================
case 'imdesmtx'
%======================================================================
% swe_conman('ImDesMtx',xX,h)
h = varargin{3};
%-Picture design matrix
%------------------------------------------------------------------
axes(h)
if isfield(varargin{2},'nKX') && ~isempty(varargin{2}.nKX)
hDesMtxIm = image((varargin{2}.nKX+1)*32);
else
hDesMtxIm = image(...
(spm_DesMtx('sca',varargin{2}.X,varargin{2}.name)+1)*32);
end
set(h,'YTick',[],'XTick',[]) %-No Tick marks
set(h,'Tag','DesMtxAx','UserData',varargin{2}) %-Reset axis UserData after image
xlabel('Design matrix')
set(hDesMtxIm,'UserData',...
struct('X',varargin{2}.X,'Xnames',{varargin{2}.name}))
set(hDesMtxIm,'ButtonDownFcn','spm_DesRep(''SurfDesMtx_CB'')')
%======================================================================
case 'imparest'
%======================================================================
% swe_conman('ImParEst',xX,h)
xX = varargin{2};
h = varargin{3};
%-Picture design parameter estimability
%------------------------------------------------------------------
axes(h)
est = spm_SpUtil('IsCon',xX.X);
nPar = length(est);
hParEstIm = image((est+1)*32);
set(h, 'XLim',[0,nPar]+.5,'XTick',[1:nPar-1]+.5,'XTickLabel','',...
'YLim',[0,1]+.5,'YDir','reverse','YTick',[],...
'Box','on','TickDir','in','XGrid','on','GridLineStyle','-');
xlabel('parameter estimability')
set(h,'Tag','ParEstAx') %-Reset 'Tag' after image cleared it
set(hParEstIm,'UserData',struct('est',est,'Xnames',{xX.name}))
set(hParEstIm,'ButtonDownFcn','spm_DesRep(''SurfEstIm_CB'')')
%======================================================================
case 'listcon'
%======================================================================
% swe_conman('ListCon',hConList,xCon,STAT,I)
hConList = varargin{2};
xCon = varargin{3};
STAT = varargin{4};
if nargin<5
Q = get(hConList,'UserData');
I = Q(get(hConList,'Value'));
else
I = varargin{5};
end
%-Sort out list, filtering by STAT, and display
%------------------------------------------------------------------
if isempty(xCon)
Q = [];
elseif isempty(STAT)
Q = 1:length(xCon);
else
Q = find(strcmp({xCon(:).STAT},STAT));
end
q = find(ismember(Q,I));
if ~isempty(Q)
str = cell(0);
for i=1:length(Q)
str{i} = sprintf('%03d {%c} : %s',...
Q(i),xCon(Q(i)).STAT,xCon(Q(i)).name);
end
FontAngle = 'Normal';
FontWeight = 'Normal';
Enable = 'on';
else
str = ['no',deblank([' ',STAT]),' contrasts defined'];
FontAngle = 'Italic';
FontWeight = 'Bold';
Enable = 'off';
end
set(hConList,'String',str,...
'UserData',Q,...
'Value',q,...
'FontAngle',FontAngle,'FontWeight',FontWeight,...
'Enable',Enable)
swe_conman('GraphCons',xCon,Q(q),get(hConList,'Parent'))
swe_conman('StatusLine',get(hConList,'Parent'))
%======================================================================
case 'graphcons'
%======================================================================
% swe_conman('GraphCons',xCon,I,F)
if nargin<2, error('insufficient arguments'), end
xCon = varargin{2};
if nargin<3, I=[1:length(xCon)]; else I=varargin{3}; end
if nargin<4, F=[]; else F=varargin{4}; end
if isempty(F), F=spm_figure('FindWin','ConMan'); end
if isempty(F), error('can''t find ConMan win'), end
cF = get(0,'CurrentFigure'); %-Save current figure
set(0,'CurrentFigure',F); %-Make F current
delete(findobj(F,'Tag','ConGrphAx'));
%-Display contrasts
%------------------------------------------------------------------
if isempty(I)
axes('Position',[0.65 (0.7 + .1*(1-0.9)) 0.3 .1*.9],...
'Tag','ConGrphAx','Visible','off')
text(0.5,0.5,'no contrast(s)',...
'FontSize',spm('FontSize',8),...
'FontAngle','Italic',...
'HorizontalAlignment','Center',...
'VerticalAlignment','Middle')
else
nPar = size(xCon(1).c,1);
xx = [repmat([0:nPar-1],2,1);repmat([1:nPar],2,1)];
nCon = length(I);
dy = 0.2/max(nCon,2);
hConAx = axes('Position',[0.65 (0.70 + dy*.1) 0.30 dy*(nCon-.1)],...
'Tag','ConGrphAx','Visible','off');
title('contrast(s)')
htxt = get(hConAx,'title'); set(htxt,'Visible','on')
for ii = nCon:-1:1
i = abs(I(ii));
axes('Position',[0.65 (0.7 + dy*(nCon-ii+.1)) 0.3 dy*.9])
if xCon(i).STAT == 'T' && size(xCon(i).c,2)==1
%-Single vector contrast for SwE{t} - bar
yy = [zeros(1,nPar);repmat(xCon(i).c',2,1);zeros(1,nPar)];
h = patch(xx,yy,[1,1,1]*.5);
set(gca,'Tag','ConGrphAx',...
'Box','off','TickDir','out',...
'XTick',[],...
'XLim', [0,nPar],...
'YTick',[-1,0,+1],'YTickLabel','',...
'YLim', [min(xCon(i).c),max(xCon(i).c)] + ...
[-1 +1] * max(abs(xCon(i).c))/10 )
else
%-F-contrast - image
h = image((xCon(i).c'/max(abs(xCon(i).c(:)))+1)*32);
set(gca,'Tag','ConGrphAx',...
'Box','on','TickDir','out',...
'XTick',[],...
'XLim', [0,nPar]+0.5,...
'YTick',[0:size(xCon(i).c,2)]+0.5,'YTickLabel','',...
'YLim', [0,size(xCon(i).c,2)]+0.5 )
end
if I(ii)>0, ylabel(num2str(i)), end
set(h,'ButtonDownFcn','spm_DesRep(''SurfCon_CB'')',...
'UserData', struct( 'i', I(ii),...
'h', htxt,...
'xCon', xCon(i)))
end
end
set(0,'CurrentFigure',cF) %-Reset CurrentFigure to previous figure
%======================================================================
case 'statusline'
%======================================================================
% swe_conman('StatusLine',F,str,col)
if nargin<2, F = findobj(get(0,'Children'),'Flat','Tag','ConMan');
else F = varargin{2}; end
if nargin<3
n = get(findobj(F,'Tag','Prompt'),'UserData');
m = length(get(findobj(F,'Tag','ConList'),'Value'));
str = sprintf('Selected %d contrast',m);
if m~=1, str=[str,'s']; end
Mcstr = get(findobj(F,'Tag','StatusLine'),'UserData');
if m>1, str=[str,Mcstr{1}]; else str=[str,Mcstr{2}]; end
if m==0
if n<0
str = [str,', press "Done" when finished.'];
else
str = [str,'.'];
end
else
if n==1
str = [str,', press "Done".'];
else
str = [str,', press "Done" when finished.'];
end
end
else
str = varargin{3};
end
if nargin<4, col='w'; else col=varargin{4}; end
set(findobj(F,'Tag','StatusLine'),'String',str,'ForegroundColor',col)
%======================================================================
case 'done_cb'
%======================================================================
% swe_conman('Done_CB')
F = gcbf;
n = get(findobj(F,'Tag','Prompt'),'UserData');
q = get(findobj(F,'Tag','ConList'),'Value');
if n>0 && isempty(q) %-Contrast(s) required, but none selected
if n==1, str = 'Select a contrast!';
elseif isfinite(n), str = sprintf('Select %d contrasts!',n);
else str = 'Select at least one contrast!';
end
elseif length(q)>abs(n) %-Too many contrasts selected
if n<0, tstr='at most'; else tstr='only'; end
if abs(n)==1, str = ['Select',tstr,' one contrast!'];
else str = sprintf('Select %s %d contrasts!',tstr,abs(n));
end
elseif n>0 && isfinite(n) && length(q)<n
if n==1, str = 'Select a contrast!';
else str = sprintf('Select %d contrasts!',n);
end
else
str = '';
end
if ~isempty(str) %-error: display error dialog box
spm('Beep')
msgbox(str,sprintf('%s%s: %s...',spm('ver'),...
spm('GetUser',' (%s)'),mfilename),'error','modal')
else %-OK, set Done UserData tag for handling
set(findobj(F,'Tag','Done'),'UserData',1)
end
%======================================================================
case 'conlist_cb'
%======================================================================
% swe_conman('ConList_CB')
F = gcbf;
hConList = gcbo;
Q = get(hConList,'UserData');
I = Q(get(hConList,'Value'));
SwE = get(F,'UserData');
xCon = SwE.xCon;
swe_conman('GraphCons',xCon,I,F)
swe_conman('StatusLine',get(hConList,'Parent'))
if strcmp(get(F,'SelectionType'),'open'), swe_conman('Done_CB'), end
%======================================================================
case {'tfa','d_tf'}
%======================================================================
% STAT = swe_conman('TFA')
% STAT = swe_conman('TFA',F,STAT)
% STAT = swe_conman('TFA',F,STAT,STATmode)
D = strcmpi(varargin{1},'d_tf'); %-Handling DefineNew UI?
if nargin<2, F = gcbf; else F=varargin{2}; end
if nargin<3 %-Called as CallBack of T or F RadioButton
%------------------------------------------------------------------
h = gcbo;
if get(h,'Value')==0
%-Was selected - don't allow unselection
set(h,'Value',1)
varargout={get(h,'UserData')};
return
else
%-Get new STAT
STAT = get(h,'UserData');
end
else
STAT = varargin{3};
end
if ~(nargin<4) %-Called to set STAT & STATmode
%------------------------------------------------------------------
STATmode = varargin{4};
b_set = 1;
else
STATmode = get(findobj(F,'Tag','STATmode'),'UserData');
b_set = 0;
end
%-Check STATmode & STAT, set temporary STATmode & STAT indicies
%------------------------------------------------------------------
STATinfo = struct(... %-STAT info structure
'mode', { 'T', 'F', 'T|F', 'T&F'},...
'vSTAT', {{'T'},{'F'},{'T','F'},{'T','F',''}},...
'defSTAT', { 'T', 'F', 'T', ''},...
'Enable', { {'on', 'off','off'},...
{'off','on' ,'off'},...
{'on', 'on', 'off'},...
{'on', 'on', 'on' }} );
i = find(strcmp(STATmode,{STATinfo.mode})); %-STATmode index
if isempty(i), error('unknown STATmode'), end %-Check STATmode valid
if D && i==4, i=3; end %-Treat 'T&F' as 'T|F'?
if isempty(STAT), STAT=STATinfo(i).defSTAT; end %-Set STAT as default(?)
j = find(strcmp(STAT,{'T','F',''})); %-STAT index
if isempty(j), error('unknown STAT'); end %-Check known STAT
if ~any(strcmp(STAT,STATinfo(i).vSTAT)) %-Check STAT is
error('Invalid STAT for this STATmode') % valid for
end % this STATmode
%-Set STAT buttons (& Dis/Enable according to STATmode if b_setEnable)
%------------------------------------------------------------------
if ~D, Tag='TFA'; else Tag='D_TF'; end
H = flipud(findobj(F,'Tag',Tag));
set(H(j), 'Value',1)
set(H(setdiff([1:length(H)],j)),'Value',0)
if b_set
%-Set RadioButton 'Enable' & store STATmode
set(findobj(F,'Tag','STATmode'),'UserData',STATmode)
for k=1:length(H), set(H(k),'Enable',STATinfo(i).Enable{k}), end
end
if ~D %-Additional UI setup for main contrast manager
%------------------------------------------------------------------
%-Reset ConList for new STAT if callback for TFA button
%--------------------------------------------------------------
if nargin<3
SwE = get(F,'UserData');
xCon = SwE.xCon;
swe_conman('ListCon',findobj(F,'Tag','ConList'),xCon,STAT)
end
else %-Additional UI setup for DNew contrast definition interface
%------------------------------------------------------------------
%-Get handles of control objects
%--------------------------------------------------------------
hD_ConMtx = findobj(F,'Tag','D_ConMtx');
hD_X1cols = findobj(F,'Tag','D_X1cols');
hD_ConErrs = findobj(F,'Tag','D_ConErrs');
hD_ConInfo = findobj(F,'Tag','D_ConInfo');
HD_Ttxt = findobj(F,'Tag','D_Ttxt');
HD_Ftxt = findobj(F,'Tag','D_Ftxt');
%-Set interface for new STAT
%--------------------------------------------------------------
set(hD_ConMtx,'String',{},'UserData',[]) %-Clear ConMtx box
set(hD_X1cols,'String','') %-Clear X1cols box
set([hD_ConErrs,hD_ConInfo],'String',{},'Value',[]) %-Clear parsing boxes
swe_conman('GraphCons',[],[],F) %-Clear contrast plot
swe_conman('D_Status',F) %-Set StatusLine
switch STAT
case 'T'
set(hD_ConMtx,'Max',1)
set(HD_Ttxt,'Visible','on')
set([hD_X1cols;HD_Ftxt],'Visible','off')
case 'F'