snapshot, working on fbs
Christoph Budziszewski

Christoph Budziszewski commited on 2009-03-27 14:57:26
Zeige 5 geänderte Dateien mit 195 Einfügungen und 12 Löschungen.


git-svn-id: https://svn.discofish.de/MATLAB/spmtoolbox/SVMCrossVal@166 83ab2cfd-5345-466c-8aeb-2b2739fb922d
... ...
@@ -0,0 +1,40 @@
1
+function decode = fbs_calculateDecodePerformance(header,psth,svmopts)
2
+
3
+RANDOMIZE_DATAPOINTS = header.svmrnd;
4
+
5
+timeline = header.timeline;
6
+timeline.frameShiftStart = header.frameShift.frameShiftStart;
7
+timeline.frameShiftEnd   = header.frameShift.frameShiftEnd;
8
+timeline.decodeDuration  = header.frameShift.decodeDuration;
9
+
10
+timeLineStart  = timeline.frameShiftStart;
11
+timeLineEnd    = timeline.frameShiftEnd;
12
+
13
+timePointArgs.pst           = psth;
14
+timePointArgs.labelMap      = LabelMap(header.classDef.labelCells,header.classDef.conditionCells);
15
+timePointArgs.eventList     = header.classDef.eventMatrix;
16
+
17
+timePointMatrix = buildTimePointMatrix(timeline,timePointArgs);
18
+
19
+decodePerformance = [];
20
+for index = 1:timeLineEnd-timeLineStart+1
21
+
22
+    svmdata      = timePointMatrix{index}(:,2:size(timePointMatrix{index},2));
23
+    svmlabel     = timePointMatrix{index}(:,1);
24
+
25
+    if RANDOMIZE_DATAPOINTS
26
+        rndindex  = randperm(length(svmlabel));
27
+        svmdata   = svmdata(rndindex,:);
28
+        svmlabel  = svmlabel(rndindex);
29
+    end
30
+
31
+    decodePerformance = [decodePerformance; svm_single_crossval(svmlabel,svmdata,svmopts)];
32
+    
33
+    
34
+end
35
+
36
+decode  = decodePerformance;
37
+end
38
+
39
+
40
+
... ...
@@ -0,0 +1,18 @@
1
+function subjectStruct = fbs_load_mask(basedir,subjects)
2
+nSubjects = numel(subjects);
3
+
4
+for s = 1:nSubjects
5
+
6
+    subjectName = cell2mat(subjects(s));
7
+
8
+    subjectStruct{s}.dir = fullfile(basedir,cell2mat(subjects(s)));
9
+    d = load(fullfile(subjectStruct{s}.dir,'results','SPM.mat'));
10
+    subjectStruct{s}.des = d.SPM;
11
+    subjectStruct{s}.name = subjectName;
12
+    subjectStruct{s}.roiFile = ui_selectRoiImage(...
13
+        sprintf('Select MASK File for %s',subjectName),...
14
+        fullfile(subjectStruct{s}.dir,'results'));
15
+
16
+end
17
+
18
+end
0 19
\ No newline at end of file
... ...
@@ -49,8 +49,6 @@ switch task
49 49
         
50 50
         out.subjectdata = runCoordTable(coordargs);
51 51
 
52
-        assignin('base','preprocessedData',out);
53
-        
54 52
     case 'ROI'
55 53
         disp('ROI');
56 54
         out = struct;
... ...
@@ -71,8 +69,6 @@ switch task
71 69
         
72 70
         out.subjectdata = runROIImageMaskMode(roiargs);
73 71
         
74
-        assignin('base','preprocessedData',out);
75
-        
76 72
     case 'FBS'
77 73
         disp('FSB');
78 74
         out = struct;
... ...
@@ -81,14 +77,15 @@ switch task
81 77
         out.header.timeline = timeLine;
82 78
         out.header.classDef = classDef;
83 79
         
84
-        fbsargs = struct;
85
-        fbsargs.subject         = subjects;
80
+
81
+        out.subjectdata  = fbs_load_mask(model.baseDir,subjects);
86 82
         
87 83
         if(size(subjects,2)>1)
88 84
             display(sprintf('No BATCH Support for Searchlight!'));
89 85
             return
90 86
         end
91 87
         
88
+        fbsargs = struct;
92 89
         fbsargs.timeline        = timeLine;
93 90
         fbsargs.classes         = classDef;
94 91
         fbsargs.mask            = mask;
... ...
@@ -97,9 +94,15 @@ switch task
97 94
         fbsargs.eventList       = classDef.eventMatrix;
98 95
         fbsargs.psthOpts        = psthOpts;
99 96
 
100
-        out.subjectdata = runFBSImageMaskMode(fbsargs);
97
+        out.header.args         = fbsargs;
98
+
99
+        % no more preprocessing in this step!
101 100
                 
102 101
 end
102
+
103
+assignin('base','preprocessedData',out);
104
+
105
+
103 106
 end
104 107
 
105 108
 function decode(model,task)
... ...
@@ -137,8 +140,9 @@ switch task
137 140
     case 'X-SOM'
138 141
         disp('not implemented')
139 142
     case 'FBS'
140
-        disp('not implemented')
141
-        fbs.radius        = getSearchlightRadius(model);
143
+        disp('this method is under high development.')
144
+        fbsargs.radius  = getSearchlightRadius(model);
145
+            runFBSImageMaskMode(header,data,fbsargs);
142 146
         
143 147
 end
144 148
 
... ...
@@ -0,0 +1,121 @@
1
+function output =  runFBSImageMaskMode(header,subjectdata,fbsargs)
2
+args = header.args;
3
+
4
+subjects = subjectdata;
5
+nSubjects = numel(subjects);
6
+sessionlist = args.sessionList;
7
+pstopts = args.psthOpts;
8
+pstopts.eventList = args.eventList;
9
+pstopts.sessionList = sessionlist;
10
+
11
+radius = fbsargs.radius;
12
+
13
+timeline = header.timeline;
14
+timeline.frameShiftStart = header.frameShift.frameShiftStart;
15
+timeline.frameShiftEnd   = header.frameShift.frameShiftEnd;
16
+timeline.decodeDuration  = header.frameShift.decodeDuration;
17
+
18
+timePointArgs.labelMap      = LabelMap(header.classDef.labelCells,header.classDef.conditionCells);
19
+timePointArgs.eventList     = header.classDef.eventMatrix;
20
+
21
+timeLineStart  = timeline.frameShiftStart;
22
+timeLineEnd    = timeline.frameShiftEnd;
23
+
24
+disp(sprintf('batch processing  %g subjects.',nSubjects));
25
+
26
+for s = 1:nSubjects
27
+    
28
+    disp(sprintf('processing subject %s.',subjects{s}.name));
29
+    % load image data
30
+   
31
+    disp('fetching volume definitions, please wait');
32
+    volumes = spm_vol(getImageFileList(subjects{s}.dir,sessionlist,args.mask));
33
+
34
+    disp('computing volume values, please wait');
35
+    
36
+    [extr x y z] = calculateRoiImageData(volumes,subjects{s}.roiFile);
37
+    
38
+clear volumes; %save memory ??
39
+disp('cleared volumes');
40
+
41
+    
42
+    nVoxel = size(extr(1).dat,1);
43
+
44
+    indexToCoordMap = java.util.HashMap;
45
+    coordToIndexMap = java.util.HashMap;
46
+    for iVoxel = 1:nVoxel
47
+        coord = [x(iVoxel),y(iVoxel),z(iVoxel)];
48
+        indexToCoordMap.put(iVoxel,coord);
49
+        coordToIndexMap.put(coord,iVoxel);
50
+    end
51
+    
52
+    subjects{s}.indexToCoordMap = indexToCoordMap;
53
+    subjects{s}.coordToIndexMap = coordToIndexMap;
54
+        
55
+    % calculate psth
56
+    
57
+    warn = warning('off','all');
58
+    pstopts.des = subjects{s}.des;
59
+
60
+    disp(sprintf('computing psth for %g voxel.',nVoxel));
61
+
62
+    for iVoxel = 1:nVoxel
63
+        rawdata = [];
64
+        for iImage = 1:length(extr);
65
+            tmp = extr(iImage);
66
+            rawdata = [rawdata tmp.dat(iVoxel)];
67
+        end
68
+%         coordNameHelper = strcat('v',num2str(iVoxel));
69
+        pst{iVoxel} = calculatePST(args.timeline,pstopts,rawdata); % do not store in subjectStruct, so we can clear it later.
70
+
71
+         p = iVoxel/nVoxel*100;
72
+         if(mod(iVoxel,nVoxel/100)==0)
73
+             sprintf(' %g%%\t complete',p);
74
+         end
75
+    end
76
+    disp('psth done');
77
+    warning(warn);
78
+    %run searchlight
79
+    
80
+
81
+    
82
+    display(sprintf('rastering %g coordinates with approx. %g mm radius',nVoxel,radius));
83
+    % for each timeslice
84
+
85
+    for index = 1:timeLineEnd-timeLineStart+1
86
+        timeShift = index;
87
+            % center timepoint && relative shift
88
+            frameStart  = floor(-globalStart+1+timeShift - 0.5*decodeDuration);
89
+            frameEnd    = min(ceil(frameStart+decodeDuration + 0.5*decodeDuration),-globalStart+globalEnd);
90
+
91
+        
92
+
93
+        for iVoxel = 1:nVoxel % linear structure avoids 3D-Loop.
94
+            % get coordinate for iVoxel
95
+%             coord = 
96
+            
97
+            % get surrounding coordinates within radius
98
+%             sphere = 
99
+
100
+            % calculate decode performance on these coordinates
101
+            svmdata      = timePointMatrix{index}(:,2:size(timePointMatrix{index},2));
102
+            svmlabel     = timePointMatrix{index}(:,1);
103
+
104
+            if RANDOMIZE_DATAPOINTS
105
+                rndindex  = randperm(length(svmlabel));
106
+                svmdata   = svmdata(rndindex,:);
107
+                svmlabel  = svmlabel(rndindex);
108
+            end
109
+            decode = svm_single_crossval(svmlabel,svmdata,svmopts);
110
+            % save the decode value to the corresponding coordinate
111
+        end
112
+        
113
+    display('rastering done');
114
+    % save data for timeslice
115
+    
116
+    end %for each timeslice
117
+
118
+    clear pst; %save memory!
119
+end
120
+
121
+end
... ...
@@ -266,10 +266,10 @@ function model = createFirstStepPanel(model,parent,DEFAULT)
266 266
         set(btnRunButton1,'Callback',{@cbRunPreprocessing,model,'COORD'}); % set here, because of model.    
267 267
         set(btnRunButton1,'Enable','on');
268 268
         
269
-        btnRunButton2 = uicontrol(pButtons,'String','run full-brain Searchlight',...
269
+        btnRunButton2 = uicontrol(pButtons,'String',sprintf('use this settings for FBS'),...
270 270
             'Units','normalized','Position',[0.33 0 0.33 1]);
271 271
         set(btnRunButton2,'Callback',{@cbRunPreprocessing,model,'FBS'}); % set here, because of model.  
272
-        set(btnRunButton2,'Enable','off');
272
+        set(btnRunButton2,'Enable','on');
273 273
         
274 274
         btnRunButton3 = uicontrol(pButtons,'String','run ROI-Image processing',...
275 275
             'Units','normalized','Position',[0.66 0 0.33 1]);
... ...
@@ -363,7 +363,7 @@ pSearchlight = uipanel(parent,'Units','normalized','Position',[0.0 0.1 1 0.3]);
363 363
     btnRunFBS = uicontrol(pSearchlight,'String','run Spatiotemporal FB classification',...
364 364
         'Units','normalized',...
365 365
         'Position',[0.0 0.0 1 0.3]);
366
-    set(btnRunFBS,'Enable','off');
366
+    set(btnRunFBS,'Enable','on');
367 367
     
368 368
 
369 369
 % button callbacks set here, because of model.
370 370