Browse code

heavy CrossVal Toolbox development. unusable snapshot!

git-svn-id: https://svn.discofish.de/MATLAB/spmtoolbox/SVMCrossVal@123 83ab2cfd-5345-466c-8aeb-2b2739fb922d

Christoph Budziszewski authored on 03/02/2009 14:36:11
Showing 11 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,51 @@
1
+function coordinateStruct = computeCoordinates(imageStruct)
2
+%imageStruct.(someName).images
3
+%imageStruct.(someName).roiImages
4
+coordinateStruct = struct;
5
+
6
+subjectCellList = fieldnames(imageStruct);
7
+
8
+for subject = 1:length(subjectCellList)
9
+    subjectFieldName = cell2mat(subjectCellList(subject));
10
+    % ROI Image Coordinates
11
+    V  = imageStruct.(subjectFieldName).images;
12
+    Vm = imageStruct.(subjectFieldName).roiImages;
13
+    coordinateStruct.(subjectFieldName) = ...
14
+        computeRoiImageCoordinates(V,Vm);
15
+    % Parsed Voxel Definitions
16
+    coordinateStruct.(subjectFieldName) = ...
17
+        appendManualVoxelCoordinates(coordinateStruct.(subjectFieldName),V);
18
+    
19
+end
20
+end
21
+
22
+function coordinates = appendManualVoxelCoordinates(coordinateStruct,V)
23
+    coordinates =coordinateStruct;
24
+end
25
+
26
+function coordinates = computeRoiImageCoordinates(V,Vm)
27
+    nVolImage = length(V);
28
+    nRoiImage = length(Vm);
29
+    
30
+    coordinates = cell(nVolImage,1);
31
+ 
32
+    for iVolImage = 1:nVolImage
33
+        coordinates{iVolImage} = [];
34
+        for jRoiImage = 1:nRoiImage
35
+            x = []; y = []; z = [];
36
+            [x1 y1] = ndgrid(1:V(iVolImage).dim(1),1:V(iVolImage).dim(2));
37
+            for p = 1:V(iVolImage).dim(3) % resample mask Vm(jRoiImage) in space of V(iVolImage)
38
+                B = spm_matrix([0 0 -p 0 0 0 1 1 1]);
39
+                M = inv(B*inv(V(iVolImage).mat)*Vm(jRoiImage).mat);
40
+                msk = find(spm_slice_vol(Vm(jRoiImage),M,V(iVolImage).dim(1:2),0));
41
+                if ~isempty(msk)
42
+                    z1 = p*ones(size(msk(:)));
43
+                    x = [x; x1(msk(:))];
44
+                    y = [y; y1(msk(:))];
45
+                    z = [z; z1];
46
+                end;
47
+            end;
48
+            coordinates{iVolImage} = [coordinates{iVolImage}; x, y, z];
49
+        end
50
+    end
51
+end
0 52
\ No newline at end of file
1 53
new file mode 100644
2 54
new file mode 100644
... ...
@@ -0,0 +1,9 @@
1
+function path = getStudyBasePath(studyID)
2
+switch studyID
3
+    case 'CHOICE24'
4
+        path = fullfile('D:','Analyze','Choice','24pilot');
5
+    otherwise
6
+        error('SVMCrossVal:getBasePath:noid',...
7
+            'Please specify a base path in ''getStudyBasePath'' or check the studyID');
8
+end
9
+end
0 10
new file mode 100644
... ...
@@ -0,0 +1,23 @@
1
+function datastruct = loadImageFileNamesData(loadParams)
2
+
3
+global SVMCROSSVAL_SUBJECT_PREFIX;
4
+
5
+RESULT_PATH = fullfile('results','SPM.mat');
6
+
7
+for subjectCell = loadParams.subjectCellArray
8
+    SubjectID   = cell2mat(subjectCell);
9
+    subjectString = strcat(SVMCROSSVAL_SUBJECT_PREFIX,SubjectID); 
10
+    
11
+    display('loading SPM.mat ...');
12
+    des = load(fullfile(getStudyBasePath(loadParams.StudyID),SubjectID,RESULT_PATH));
13
+    des = des.SPM;
14
+    display('... done.');
15
+    
16
+    datastruct.(subjectString).images      = getImageFileList(des,loadParams.use_smoothed_image_hack);
17
+    
18
+    ROIIMAGE = 1;
19
+    if ROIIMAGE
20
+        %load ROI Image Header
21
+        datastruct.(subjectString).roiImages = readRoiImage(sprintf('Select ROI Images for Subject %s',SubjectID));
22
+    end
23
+end
0 24
\ No newline at end of file
1 25
new file mode 100644
... ...
@@ -0,0 +1,54 @@
1
+function retVal = main(action,parameterModel)
2
+
3
+% if nargs ~=2 
4
+%     error('SVMCrossVal:main:argument','wrong number of arguments');
5
+% end
6
+
7
+StudyID = 'CHOICE24';
8
+
9
+
10
+        loadParams.StudyID = StudyID;
11
+        loadParams.use_smoothed_image_hack = 1;
12
+        loadParams.subjectCellArray = getSubjectIDString(parameterModel);
13
+
14
+        imageStruct = loadImageFileNamesData(loadParams);
15
+        assignin('base','imageStruct',imageStruct);
16
+        
17
+        coordinateStruct = computeCoordinates(imageStruct);
18
+        assignin('base','coordinateStruct',coordinateStruct);
19
+
20
+end
21
+
22
+% generate parameter structs for subroutines
23
+function timelineParams = getTimeLineParams(paramModel)
24
+timelineParams = struct;
25
+timelineParams.frameShiftStart = getDouble(paramModel.txtFrameShiftStart);  % -20;
26
+timelineParams.frameShiftEnd   = getDouble(paramModel.txtFrameShiftEnd); %15;
27
+timelineParams.decodeDuration  = getDouble(paramModel.txtFrameShiftDur);
28
+timelineParams.psthStart       = getDouble(paramModel.txtPSTHStart); % -25;
29
+timelineParams.psthEnd         = getDouble(paramModel.txtPSTHEnd); % 20;
30
+timelineParams.baselineStart   = getDouble(paramModel.txtBaselineStart); % -22;
31
+timelineParams.baselineEnd     = getDouble(paramModel.txtBaselineEnd); % -20;
32
+
33
+end
34
+
35
+function calculateParams = parseCalculateParams(paramModel)
36
+calculateParams  = struct;
37
+calculateParams.smoothed        = getChkValue(paramModel.chkSmoothed);
38
+calculateParams.svmargs         = get(paramModel.txtSVMopts,'String');
39
+calculateParams.sessionList     = 1:3;
40
+
41
+calculateParams.CROSSVAL_METHOD      = CROSSVAL_METHOD_DEF.svmcrossval;
42
+calculateParams.VOXEL_SELECTION_MODE = VOXEL_SELECTION_MODE_DEF.roiImage;
43
+calculateParams.PROJECT_BASE_PATH    = PROJECT_BASE_PATH;
44
+calculateParams.PROJECT_RESULT_PATH  = PROJECT_RESULT_PATH;
45
+calculateParams.RANDOMIZE       = 0;
46
+
47
+classStruct = parseClassDef(paramModel);
48
+calculateParams.labelMap        = LabelMap(classStruct.labelCells , classStruct.conditionCells, 'auto'); % LabelMap({'<','>','<+<','>+>','<+>','>+<'},{-2,-1,1,2,3,4}); 0 is autolabel
49
+calculateParams.classList       = getClasses(calculateParams.labelMap);
50
+calculateParams.eventList       = classStruct.eventMatrix; %[9,11,13; 10,12,14];
51
+calculateParams.subjectSelection = subjectSelection;
52
+ 
53
+% assignin('base','calculateParams',calculateParams);
54
+end
... ...
@@ -1,50 +1,24 @@
1
-% filenameList as in SPM.xY.VY
2
-% voxelList in [x y z], 1 coordinate per row
1
+% filenameList (e.g. as in SPM.xY.VY)
2
+% voxelList in [x y z], 1 coordinate per row, untransformed
3 3
 
4
-function extr = calculateImageData(filenameList, voxelList)
5
-%function extr = calculateImageData(voxelList,des,smoothed)
6
-% global USE_DRIVE_CHECK_HACK;
7
-
8
-% dtype='PSTH';
9
-
10
-% switch dtype 
11
-%     case 'PSTH'
12
-%         V=des.xY.VY;
13
-%     case 'betas'
14
-%         V=des.Vbeta;
15
-% end;
16
-
17
-% if USE_DRIVE_CHECK_HACK
18
-% if V(1).fname(1)~='D'
19
-%      for z=1:length(V) % Change Drive Letter - HACK!
20
-%       	V(z).fname(1) = 'D';
21
-%      end; 
22
-% end
23
-% end
24
-
25
-% if (~smoothed)
26
-%   for z=1:length(V) % Change smoothed Filename - HACK!
27
-%       % D:....SUBJECTID\session\swfanders...
28
-%       % D:....SUBJECTID\session\wfanders...
29
-%       tmp = findstr(filesep,V(z).fname);
30
-%       V(z).fname = strcat(V(z).fname(1:tmp(end)),V(z).fname(tmp(end)+2:end));
31
-%   end;
32
-% end
4
+function extr = calculateImageData(filenameList, voxelList, imageOpts)
5
+TRANSFORM_METHOD = 'image';
33 6
 
34 7
 V = filenameList;
35 8
 
36 9
 vox = voxelList;
37 10
 nVoxel = size(vox,1);
38 11
 nImage = numel(V);
12
+nRoiFiles = ;
39 13
 
40 14
 for kImage=1:nImage
41
-	extr(kImage) = struct(...
42
-        'val',   repmat(NaN, [1 nVoxel]),...
43
-		'mean',  repmat(NaN, [1 nVoxel]),...
44
-		'sum',   repmat(NaN, [1 nVoxel]),...
45
-		'nvx',   repmat(NaN, [1 nVoxel]),...
46
-		'posmm', repmat(NaN, [3 nVoxel]),...
47
-		'posvx', repmat(NaN, [3 nVoxel]));
15
+    extr(kImage) = struct(...
16
+        'dat',   repmat(NaN, [1 nVoxel]),...
17
+        'mean',  repmat(NaN, [1 nVoxel]),...
18
+        'sum',   repmat(NaN, [1 nVoxel]),...
19
+        'nvx',   repmat(NaN, [1 nVoxel]),...
20
+        'posmm', repmat(NaN, [3 nVoxel]),...
21
+        'posvx', repmat(NaN, [3 nVoxel]));
48 22
 
49 23
     switch TRANSFORM_METHOD
50 24
         case 'single'
... ...
@@ -53,8 +27,8 @@ for kImage=1:nImage
53 27
             y = roicenter(2,:);
54 28
             z = roicenter(3,:);
55 29
         case 'image'
56
-             x = []; y = []; z = [];
57
-            for iRoiFile = 1:nRoiFiles 
30
+            x = []; y = []; z = [];
31
+            for iRoiFile = 1:nRoiFiles
58 32
                 [x1 y1] = ndgrid(1:V(k).dim(1),1:V(k).dim(2));
59 33
                 for p = 1:V(k).dim(3) % resample mask Vm(iRoiFile) in space of V(k)
60 34
                     B = spm_matrix([0 0 -p 0 0 0 1 1 1]);
... ...
@@ -67,37 +41,13 @@ for kImage=1:nImage
67 41
                         z = [z; z1];
68 42
                     end;
69 43
                 end;
70
-
71 44
             end
72 45
     end
73
-    
74
-
75
-	for iVoxel = 1:nVoxel
76
-
77
-%         if rad==0
78
-            x = roicenter(1,iVoxel);
79
-            y = roicenter(2,iVoxel);
80
-            z = roicenter(3,iVoxel);
81
-%         else
82
-%             tmp = spm_imatrix(V(kImage).mat);
83
-%             vdim = tmp(7:9);
84
-%             vxrad = ceil((rad*ones(1,3))./(ones(nVoxel,1)*vdim))';
85
-%             [x y z] = ndgrid(-vxrad(1,iVoxel):sign(vdim(1)):vxrad(1,iVoxel), ...
86
-%                       -vxrad(2,iVoxel):sign(vdim(2)):vxrad(2,iVoxel), ...
87
-%                       -vxrad(3,iVoxel):sign(vdim(3)):vxrad(3,iVoxel));
88
-%             sel = (x./vxrad(1,iVoxel)).^2 + (y./vxrad(2,iVoxel)).^2 + ...
89
-%                   (z./vxrad(3,iVoxel)).^2 <= 1;
90
-%             x = roicenter(1,iVoxel)+x(sel(:));
91
-%             y = roicenter(2,iVoxel)+y(sel(:));
92
-%             z = roicenter(3,iVoxel)+z(sel(:));
93
-%         end;
94
-
95
-
96
-		dat = spm_sample_vol(V(kImage), x, y, z,0);
97
-        extr(kImage).dat(iVoxel)      = dat;
98
-		extr(kImage).mean(iVoxel)     = nanmean(dat);
99
-        extr(kImage).nvx(iVoxel)      = numel(dat);
100
-	end;
101 46
 
47
+    dat = spm_sample_vol(V(kImage), x, y, z,0);
48
+    extr(kImage).dat(iVoxel)      = dat;
49
+    extr(kImage).mean(iVoxel)     = nanmean(dat);
50
+    extr(kImage).nvx(iVoxel)      = numel(dat);
102 51
 end;
52
+
103 53
 end
104 54
\ No newline at end of file
... ...
@@ -96,13 +96,20 @@ for subjectCell = calculateParams.subjectSelection
96 96
     subjectParams = struct;
97 97
     
98 98
     subjectParams.des             = spm.SPM;
99
-
99
+    smoothed = calculateParams.smoothed;
100
+    
101
+    DataImageFilenames      = getImageFileList(des,~smoothed);
102
+    subjectParams.voxelList = getTransformedCoordinates(...
103
+        calculateParams.VOXEL_SELECTION_MODE,
104
+        
100 105
     switch calculateParams.VOXEL_SELECTION_MODE 
101 106
         case VOXEL_SELECTION_MODE_DEF.manualGui
102
-            subjectParams.voxelList       = parseVoxelList(paramModel,SubjectID);
107
+            parsedVoxelList          = parseVoxelList(paramModel,SubjectID);
108
+              = transformVoxelList(parsedVoxelList);
109
+            
103 110
             
104 111
         case VOXEL_SELECTION_MODE_DEF.roiImage
105
-            subjectParams.voxelList       = readRoiImage();
112
+            subjectParams.voxelList       = readRoiImage(); % image for subject!
106 113
     end
107 114
     
108 115
     
... ...
@@ -1,4 +1,4 @@
1
-function fileList = getImageFileList(des,use_unsmoothed_hack)
1
+function fileList = getImageFileList(des,use_smoothed_image_hack)
2 2
 
3 3
 global USE_DRIVE_CHECK_HACK;
4 4
 
... ...
@@ -12,7 +12,7 @@ if USE_DRIVE_CHECK_HACK
12 12
         end;
13 13
     end
14 14
 end
15
-if use_unsmoothed_hack
15
+if ~use_smoothed_image_hack
16 16
     for z=1:nFiles % Change smoothed Filename - HACK!
17 17
         % D:....SUBJECTID\session\swfanders...
18 18
         % D:....SUBJECTID\session\wfanders...
... ...
@@ -166,7 +166,8 @@ end
166 166
 function cbRunSVM(src,evnt,model)
167 167
     display('RUN');
168 168
     if isSane(model)
169
-        classify(model)
169
+%         classify(model)
170
+        main('all',model);
170 171
     else
171 172
         error('spmtoolbox:SVMCrossVal:paramcheck','please verify all parameters');
172 173
     end
... ...
@@ -1,24 +1,4 @@
1
-function voxelList = readRoiImage()
2
-
3
-Vm = spm_vol(spm_select([1 Inf],'image','Select ROI image'));
4
-nRoiFiles = size(Vm,2);
5
-
6
-% for iRoiFile = 1:nRoiFiles
7
-%     x = []; y = []; z = [];
8
-%     [x1 y1] = ndgrid(1:V(k).dim(1),1:V(k).dim(2));
9
-%     for p = 1:V(k).dim(3) % resample mask Vm(iRoiFile) in space of V(k)
10
-%         B = spm_matrix([0 0 -p 0 0 0 1 1 1]);
11
-%         M = inv(B*inv(V(k).mat)*Vm(iRoiFile).mat);
12
-%         msk = find(spm_slice_vol(Vm(iRoiFile),M,V(k).dim(1:2),0));
13
-%         if ~isempty(msk)
14
-%             z1 = p*ones(size(msk(:)));
15
-%             x = [x; x1(msk(:))];
16
-%             y = [y; y1(msk(:))];
17
-%             z = [z; z1];
18
-%         end;
19
-%     end;
20
-% 
21
-% end
22
-voxelList = Vm;
23
-
1
+function imageList = readRoiImage(formatstring)
2
+wd = 'C:\Dokumente und Einstellungen\Christoph\Eigene Dateien\Diplomarbeit\data';
3
+imageList = spm_vol(spm_select([1 Inf],'image',formatstring,[],wd));
24 4
 end
25 5
\ No newline at end of file
... ...
@@ -1,18 +1,25 @@
1 1
 function spm_SVMCrossVal(varargin)
2 2
 
3 3
 %define global constants
4
-global USE_DRIVE_CHECK_HACK;
5
-USE_DRIVE_CHECK_HACK = 1;  %enables subroutine to check if image path starts with 'D'
6
-
7
-global CROSSVAL_METHOD_DEF;
8
-CROSSVAL_METHOD_DEF.svmcrossval       = 'svm crossval';
9
-CROSSVAL_METHOD_DEF.classPerformance  = 'svm class performance';
10
-CROSSVAL_METHOD_DEF.crossSubject      = 'svm across subject testing';
11
-CROSSVAL_METHOD_DEF.somTraining       = 'som Training';
12
-
13
-global VOXEL_SELECTION_MODE_DEF;
14
-VOXEL_SELECTION_MODE_DEF.manualGui    = 'manually defined in GUI';
15
-VOXEL_SELECTION_MODE_DEF.roiImage     = 'use ROI image and popup image selector';
4
+global SVMCROSSVAL_USE_DRIVE_CHECK_HACK;
5
+%enables subroutine to check if image path starts with 'D'
6
+SVMCROSSVAL_USE_DRIVE_CHECK_HACK                  = 1;  
7
+
8
+global SVMCROSSVAL_CROSSVAL_METHOD_DEF;
9
+% supported classification methods
10
+SVMCROSSVAL_CROSSVAL_METHOD_DEF.svmcrossval       = 'svm crossval';
11
+SVMCROSSVAL_CROSSVAL_METHOD_DEF.classPerformance  = 'svm class performance';
12
+SVMCROSSVAL_CROSSVAL_METHOD_DEF.crossSubject      = 'svm across subject testing';
13
+SVMCROSSVAL_CROSSVAL_METHOD_DEF.somTraining       = 'som Training';
14
+
15
+global SVMCROSSVAL_VOXEL_SELECTION_MODE_DEF;
16
+% supported voxel selection methods
17
+SVMCROSSVAL_VOXEL_SELECTION_MODE_DEF.manualGui    = 'manually defined in GUI';
18
+SVMCROSSVAL_VOXEL_SELECTION_MODE_DEF.roiImage     = 'use ROI image by pop-up image selector';
19
+
20
+global SVMCROSSVAL_SUBJECT_PREFIX;
21
+% internally used to prefix subject-ids starting with numbers.
22
+SVMCROSSVAL_SUBJECT_PREFIX                        = 'subject';
16 23
 
17 24
 switch nargin
18 25
 case 0