Attachment 'process_headmodel_test.m'
Download 1 function varargout = process_headmodel_test( varargin )
2 % PROCESS_HEADMODEL_TEST:
3
4 % @=============================================================================
5 % This software is part of the Brainstorm software:
6 % http://neuroimage.usc.edu/brainstorm
7 %
8 % Copyright (c)2000-2013 Brainstorm by the University of Southern California
9 % This software is distributed under the terms of the GNU General Public License
10 % as published by the Free Software Foundation. Further details on the GPL
11 % license can be found at http://www.gnu.org/copyleft/gpl.html.
12 %
13 % FOR RESEARCH PURPOSES ONLY. THE SOFTWARE IS PROVIDED "AS IS," AND THE
14 % UNIVERSITY OF SOUTHERN CALIFORNIA AND ITS COLLABORATORS DO NOT MAKE ANY
15 % WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF
16 % MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, NOR DO THEY ASSUME ANY
17 % LIABILITY OR RESPONSIBILITY FOR THE USE OF THIS SOFTWARE.
18 %
19 % For more information type "brainstorm license" at command prompt.
20 % =============================================================================@
21 %
22 % Authors:
23
24 eval(macro_method);
25 end
26
27
28 %% ===== GET DESCRIPTION =====
29 function sProcess = GetDescription() %#ok<DEFNU>
30 % Description the process
31 sProcess.Comment = 'Headmodel test #1';
32 sProcess.FileTag = '';
33 sProcess.Category = 'Custom';
34 sProcess.SubGroup = 'Sources';
35 sProcess.Index = 1000;
36 % Definition of the input accepted by this process
37 sProcess.InputTypes = {'data', 'raw'};
38 sProcess.OutputTypes = {'data', 'raw'};
39 sProcess.nInputs = 1;
40 sProcess.nMinFiles = 1;
41 % Definition of the options
42 % Options: Radio button
43 sProcess.options.optionname1.Comment = {'Radio button 1', 'Radio button 2', 'Radio button 3'};
44 sProcess.options.optionname1.Type = 'radio';
45 sProcess.options.optionname1.Value = 1;
46 % Option: Combo box
47 sProcess.options.optionname2.Comment = 'Combobox example:';
48 sProcess.options.optionname2.Type = 'combobox';
49 sProcess.options.optionname2.Value = {3, {'method 1', 'method 2', 'method 3'}}; % {Default index, {list of entries}}
50 % Option: Real value
51 sProcess.options.optionname3.Comment = 'Real value: ';
52 sProcess.options.optionname3.Type = 'value';
53 sProcess.options.optionname3.Value = {3, 'units', 3}; % {Default value, units, precision}
54 % Option: Integer value
55 sProcess.options.optionname4.Comment = 'Integer value: ';
56 sProcess.options.optionname4.Type = 'value';
57 sProcess.options.optionname4.Value = {3, 'units', 0};
58 % Option: Text field
59 sProcess.options.optionname5.Comment = 'Text field: ';
60 sProcess.options.optionname5.Type = 'text';
61 sProcess.options.optionname5.Value = 'default text';
62 % Option: Checkbox
63 sProcess.options.optionname6.Comment = 'Check box text';
64 sProcess.options.optionname6.Type = 'checkbox';
65 sProcess.options.optionname6.Value = 1; % Selected or not by default
66 % Option: Atlas
67 sProcess.options.optionname7.Comment = 'Select atlas:';
68 sProcess.options.optionname7.Type = 'atlas';
69 sProcess.options.optionname7.Value = [];
70 end
71
72
73 %% ===== FORMAT COMMENT =====
74 function Comment = FormatComment(sProcess) %#ok<DEFNU>
75 Comment = sProcess.Comment;
76 end
77
78
79 %% ===== RUN =====
80 function OutputFiles = Run(sProcess, sInputs) %#ok<DEFNU>
81 % Initialize returned list of files
82 OutputFiles = {};
83 % Get option values
84 Option1 = sProcess.options.optionname1.Comment{sProcess.options.optionname1.Value};
85 Option2 = sProcess.options.optionname2.Value{2}(sProcess.options.optionname2.Value{1});
86 Option3 = sProcess.options.optionname3.Value{1};
87 Option4 = sProcess.options.optionname4.Value{1};
88 Option5 = sProcess.options.optionname5.Value;
89 Option6 = sProcess.options.optionname6.Value;
90 AtlasName = sProcess.options.optionname7.Value;
91
92 % ===== GET THE STUDIES TO PROCESS =====
93 % Get channel studies
94 [sChannels, iChanStudies] = bst_get('ChannelForStudy', unique([sInputs.iStudy]));
95 % Check if there are channel files everywhere
96 if (length(sChannels) ~= length(iChanStudies))
97 bst_report('Error', sProcess, sInputs, ['Some of the input files are not associated with a channel file.' 10 'Please import the channel files first.']);
98 return;
99 end
100 % Keep only once each channel file
101 iChanStudies = unique(iChanStudies);
102 if isempty(iChanStudies)
103 bst_report('Error', sProcess, sInputs, 'Nothing to process');
104 return;
105 end
106
107 % ===== LOOP ON EACH HEAD MODEL TO CALCULATE =====
108 for i = 1:length(iChanStudies)
109
110 % === LOAD CHANNEL FILE ===
111 % Get study
112 sChanStudy = bst_get('Study', iChanStudies(i));
113 % Load channel file
114 ChannelMat = in_bst_channel(sInputs(1).ChannelFile);
115 % Find the MEG channels
116 iMEG = good_channel(ChannelMat.Channel, [], 'MEG');
117 iEEG = good_channel(ChannelMat.Channel, [], 'EEG');
118 iSEEG = good_channel(ChannelMat.Channel, [], 'SEEG');
119 iECOG = good_channel(ChannelMat.Channel, [], 'ECOG');
120 % Number of channels
121 nChannels = length(ChannelMat.Channel);
122
123 % === LOAD SURFACES ===
124 % Get the subject definition
125 sSubject = bst_get('Subject', sChanStudy.BrainStormSubject);
126 % MRI
127 if ~isempty(sSubject.iAnatomy)
128 MriFile = sSubject.Anatomy(sSubject.iAnatomy(1)).FileName;
129 MriMat = in_mri_bst(MriFile);
130 else
131 % bst_report('Error', sProcess, sInput, 'Subject's MRI is missing.');
132 % return;
133 end
134 % Cortex
135 if ~isempty(sSubject.iCortex)
136 CortexFile = sSubject.Surface(sSubject.iCortex(1)).FileName;
137 CortexMat = in_tess_bst(CortexFile);
138 else
139 % bst_report('Error', sProcess, sInput, 'Cortex surface is missing.');
140 % return;
141 end
142 % Scalp
143 if ~isempty(sSubject.iScalp)
144 ScalpFile = sSubject.Surface(sSubject.iScalp(1)).FileName;
145 ScalpMat = in_tess_bst(ScalpFile);
146 else
147 % bst_report('Error', sProcess, sInput, 'Head surface is missing.');
148 % return;
149 end
150 % Inner skull
151 if ~isempty(sSubject.iInnerSkull)
152 InnerSkullFile = sSubject.Surface(sSubject.iInnerSkull(1)).FileName;
153 InnerSkullMat = in_tess_bst(InnerSkullFile);
154 else
155 % bst_report('Error', sProcess, sInput, 'Inner skull surface is missing.');
156 % return;
157 end
158 % Outer skull
159 if ~isempty(sSubject.iOuterSkull)
160 OuterSkullFile = sSubject.Surface(sSubject.iOuterSkull(1)).FileName;
161 OuterSkullMat = in_tess_bst(OuterSkullFile);
162 else
163 % bst_report('Error', sProcess, sInput, 'Outer skull surface is missing.');
164 % return;
165 end
166 % ALL the other surfaces can be loaded in the same way
167
168 % % Get atlas
169 % iAtlas = find(strcmpi({CortexMat.Atlas.Name}, AtlasName));
170 % if isempty(iAtlas)
171 % bst_report('Warning', sProcess, sInput, ['Atlas not found: "' sProcess.options.atlas.Value '"']);
172 % end
173 % sAtlas = CortexMat.Atlas(iAtlas);
174
175
176
177 % ===== PROCESS =====
178 %%%% EDIT THIS CODE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
179 % Create source space
180 HeadModelType = 'surface'; % Or 'volume'
181 nSources = size(CortexMat.Vertices, 1);
182 GridLoc = rand(nSources, 3); % Source locations
183 GridOrient = rand(nSources, 3); % Source orientations ([] in case of volume source model)
184 % Creating gain matrix
185 Gain = rand(nChannels, 3 * nSources);
186
187 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
188
189
190 % ===== SAVE HEAD MODEL =====
191 % Create a new data file structure
192 HeadmodelMat = db_template('headmodelmat');
193 HeadmodelMat.MEGMethod = 'test'; % You need to fill at least one of those 4 fields
194 HeadmodelMat.EEGMethod = '';
195 HeadmodelMat.ECOGMethod = '';
196 HeadmodelMat.SEEGMethod = '';
197 HeadmodelMat.Gain = Gain;
198 HeadmodelMat.Comment = 'Test file comment';
199 HeadmodelMat.HeadModelType = HeadModelType;
200 HeadmodelMat.GridLoc = GridLoc;
201 HeadmodelMat.GridOrient = GridOrient;
202 HeadmodelMat.SurfaceFile = CortexFile; % Leave empty in the case of a volume head model
203 HeadmodelMat.InputSurfaces = []; % Cell array of the surface file names that were used to calculate this file
204 % Generate new filename
205 HeadmodelFile = bst_process('GetNewFilename', fileparts(sChanStudy.FileName), 'headmodel_');
206 % Save on disk
207 save(HeadmodelFile, '-struct', 'HeadmodelMat');
208
209 % ===== REGISTER IN DATABASE =====
210 % Database structure
211 newHeadModel = db_template('HeadModel');
212 newHeadModel.FileName = file_win2unix(file_short(HeadmodelFile));
213 newHeadModel.Comment = HeadmodelMat.Comment;
214 newHeadModel.HeadModelType = HeadmodelMat.HeadModelType;
215 newHeadModel.MEGMethod = HeadmodelMat.MEGMethod;
216 newHeadModel.EEGMethod = HeadmodelMat.EEGMethod;
217 newHeadModel.ECOGMethod = HeadmodelMat.ECOGMethod;
218 newHeadModel.SEEGMethod = HeadmodelMat.SEEGMethod;
219 % Register in database
220 if isempty(sChanStudy.HeadModel)
221 sChanStudy.HeadModel = newHeadModel;
222 else
223 sChanStudy.HeadModel(end+1) = newHeadModel;
224 end
225 % Make it the default head model
226 sChanStudy.iHeadModel = length(sChanStudy.HeadModel);
227 % Update database
228 bst_set('Study', iChanStudies(i), sChanStudy);
229 % Refresh tree
230 panel_protocols('UpdateNode', 'Study', iChanStudies(i));
231 end
232 % Return the input files
233 OutputFiles = {sInputs.FileName};
234 end
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.