Skip to content

Commit 20970da

Browse files
committed
Merge pull request #26 from dankessler/svmbatch/main
Pull svmbatch development history into main MethodsCore repo
2 parents 4668842 + 591fc3a commit 20970da

7 files changed

Lines changed: 480 additions & 0 deletions

File tree

svmbatch/README

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
SVM Batch
2+
3+
Written 2011 by Daniel Kessler, kesslerd@umich.edu
4+
5+
**Prerequisites**
6+
This was code was written and tested in AFNI_2011_05_26_1457
7+
8+
9+
This batch script should not require any modification by you. Instead, you customize it by editing the support text files that come with it, namely:
10+
11+
*svmdir
12+
This file should contain a path to an extant directory where the script should store all of its intermediates and results
13+
14+
*filelist1
15+
This should contain a whitespace-delimted list of (ideally absolute) paths to files that 3dsvm will consider to be examples of class 1. If you are giving it data from img/hdr pairs, be sure to point to the hdr (it will figure out where the img is based on the hdr).
16+
17+
*filelist2
18+
Just like filelist1, except these are files that represent examples from class 2.
19+
20+
That's it. Other support files may be added as we extend functionality.
21+
22+
23+
**Likely future additions
24+
25+
1) Automatic testing of model on training set
26+
2) Automated partitioning of examples into training set and test set for model cross-validation
27+
3) Permutation testing. This will be computationally intensive, but not too hard to implement.

svmbatch/filelist1

Whitespace-only changes.

svmbatch/filelist2

Whitespace-only changes.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
%function [Y] = permutation_test(Pt,Pdir,name)
2+
% Calculate nonparametric distributions based on svm permutation tests
3+
% Pt - filename of weight vector from real model
4+
% Pdir - file directory in which to search for results of permutation test
5+
% img/hdr pairs
6+
% name - string of name to write out (without .img extension)
7+
8+
%% Make list of files
9+
Pp=spm_select('List',Pdir,'[0-9]+\.hdr');
10+
11+
%% Read in files
12+
th=spm_vol(Pt);
13+
tvol=spm_read_vols(th);
14+
15+
16+
cd(Pdir);
17+
ph=spm_vol(Pp);
18+
pvol=spm_read_vols(ph);
19+
cd ..
20+
21+
%% Create dumping space for comparisons
22+
23+
rvol=zeros(size(tvol));
24+
rh=th;
25+
rh.fname=[name '.img'];
26+
rh.descrip='SPM{T_[10000000]}';
27+
28+
%% Loop over dimensions
29+
30+
% For each voxel, calculate proportion of permutations that resulted in
31+
% smaller or equal value. This returns the nonparametric CDF. We then
32+
% convert this, using norminv, to a z-score. Extremely positive values will
33+
% have high CDF scores, high p scores, and high z scores. Very negative
34+
% values will have very small CDF scores, small p scores, and negative z
35+
% scores. We convert to z for easier visualization in xjview.
36+
37+
for i=1:size(rvol,1), for j=1:size(rvol,2), for k=1:size(rvol,3)
38+
rvol(i,j,k)=norminv(sum(pvol(i,j,k,:)<=tvol(i,j,k))/size(pvol,4));
39+
end,end,end
40+
41+
%% Mask based on tvol
42+
% Some voxels that were zero in the real weight vector are out-of-brain
43+
% voxels. Implicit masking during first level testing should have set them
44+
% to 0. All permutation tests will likely also return 0, yielding a bizarre
45+
% value for the CDF. To protect against this, we set the z-score of these
46+
% points to be 0, for a corresponding p of .5
47+
48+
for i=1:size(rvol,1), for j=1:size(rvol,2), for k=1:size(rvol,3)
49+
if tvol(i,j,k)==0, rvol(i,j,k)=0; end
50+
end,end,end
51+
52+
%% Replace Inf and -Inf values with prior min and max, respectively
53+
% rvol(find(rvol==Inf))=max(rvol(~isinf(rvol)));
54+
% rvol(find(rvol==-Inf))=min(rvol(~isinf(rvol)));
55+
56+
rvol(find(rvol==Inf))=norminv((1-1/size(pvol,4)));
57+
rvol(find(rvol==-Inf))=norminv((1/size(pvol,4)));
58+
59+
%Fix the origin for
60+
61+
rh.mat(:,4)=[-81 -115 -53 1];
62+
63+
64+
%% Write out the resulting p values in one giant file
65+
spm_write_vol(rh,rvol);
66+
67+
%%Slice up the file into its sub-totems, and write them out separately from
68+
%%bottom up
69+
70+
for i=1:(th.dim(3)/46)
71+
cth_range=[1:46] + (i-1)*46;
72+
cth=rh;
73+
cth.fname=sprintf('%s%s%s%.3d%s',pwd,'/', name ,i,'.img');
74+
cth.dim(3)=[46];
75+
ctvol=rvol(:,:,cth_range);
76+
spm_write_vol(cth,ctvol);
77+
end

0 commit comments

Comments
 (0)