Attachment 'example_djm_partitions.m'
Download 1 function example_djm_partitions()
2 %{
3 %%%%%%%%%%%%%%%%%%%%%
4 djm: This shows a simple group analysis for the vt mask of the AK6 dataset.
5 It illustrates three types of partition: single split-half (e.g. oddeven), nfold
6 (aka leave-one-out), and repeated split-half.
7 %%%%%%%%%%%%%%%%%%%%%
8 %}
9
10 %% Define data
11 config=cosmo_config();
12 subs={'s01','s02','s03','s04','s05','s06','s07','s08'};
13 partitions={'odd_even', 'nfold', 'full_split_half'};
14
15 rng(0); % reset random number generator for reproducability
16
17 figure(9); clf(9);
18
19 result=nan( numel(subs), numel(partitions) ); % djm, preallocate array
20 for s=1:numel(subs)
21 fprintf('.')
22 data_path=fullfile(config.tutorial_data_path,'ak6',subs{s});
23 data_fn=fullfile(data_path,'glm_T_stats_perrun.nii');
24 mask_fn=fullfile(data_path,'vt_mask.nii');
25 ds=cosmo_fmri_dataset(data_fn,'mask',mask_fn,...
26 'targets',repmat(1:6,1,10),... % 6 types of animal
27 'chunks',floor(((1:60)-1)/6)+1); % 10 runs
28
29 % remove constant features (due to liberal masking)
30 ds=cosmo_remove_useless_data(ds);
31
32 % Assign a function handle to the cosmo_crossvalidation_measure
33 measure=@cosmo_crossvalidation_measure;
34
35 % Make a struct containing the arguments for the measure:
36 args=struct();
37 args.classifier=@cosmo_classify_nn;
38
39 for p=1:numel(partitions)
40
41 switch partitions{p}
42 case 'odd_even'
43 args.partitions=cosmo_oddeven_partitioner(ds,'half');
44 case 'nfold'
45 args.partitions=cosmo_nfold_partitioner(ds);
46 case 'full_split_half'
47 args.partitions=cosmo_nchoosek_partitioner(ds,0.5);
48 otherwise
49 error('unknown partition type')
50 endswitch
51 cosmo_check_partitions(args.partitions,ds);
52
53 % Apply the measure to ds, with args as second argument.
54 tic
55 ds_accuracy=measure(ds,args);
56
57 % for subject 1, plot partitions, and how long this scheme took
58 if s==1
59 subplot(2,2,p)
60 plot_partitions(args.partitions);
61 title(sprintf('%s; tooks %.1f s per subject',partitions{p},toc),'interpreter','none')
62 drawnow
63 endif
64
65 % store the result as accuracy minus chance (so positve values indicate information present)
66 result(s,p)=ds_accuracy.samples-1/6;
67
68 end % next partition scheme
69 end % next subject
70
71 % Run ttest versus chance (now zero)
72 [h, p, ci, stat]=ttest(result);
73
74 % plot results with 95% confidence intervals
75 subplot(2,2,4)
76 errorbar(mean(result),range(ci)./2,'o')
77 set(gca,'xtick',1:numel(partitions),'xticklabel',partitions)
78 ylabel('accuracy minus chance')
79 ylim([-0.2 0.5]);
80 hold on
81 plot(xlim,[0 0],'--')
82 drawnow
83
84 return
85
86 function plot_partitions(partitions)
87 % djm: plot partitions like in documentation
88 maxs=max(cellfun(@max,[ partitions.train_indices partitions.test_indices]));
89 nc=numel( partitions.test_indices);
90 out=zeros(maxs,nc);
91 for c=1:nc
92 out(partitions.train_indices{c},c)=1;
93 out(partitions.test_indices{c},c)=2;
94 endfor
95 imagesc(out);
96 if nc<21
97 set(gca,'xtick',1:nc)
98 else
99 set(gca,'xtick',[1 nc])
100 endif
101 xlabel('classifiers')
102 ylabel('samples')
103 colormap([1 1 1; 0 1 0; 1 0 0]);
104 caxis([0 2])
105 return
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.