Attachment 'process_example_customavg.m'
Download 1 function varargout = process_example_customavg( varargin )
2 % PROCESS_EXAMPLE_CUSTOMAVG: Example file that reads all the data files in input, and saves the average.
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: Francois Tadel, 2013
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 = 'Process example: Custom average';
32 sProcess.FileTag = '';
33 sProcess.Category = 'Custom';
34 sProcess.SubGroup = 'Examples';
35 sProcess.Index = 1000;
36 % Definition of the input accepted by this process
37 sProcess.InputTypes = {'data'};
38 sProcess.OutputTypes = {'data'};
39 sProcess.nInputs = 1;
40 sProcess.nMinFiles = 2;
41 % Definition of the options
42 % === OPTION EXAMPLE
43 sProcess.options.example1.Comment = {'Choice 1', 'Choice 2', 'Choice 3'};
44 sProcess.options.example1.Type = 'radio';
45 sProcess.options.example1.Value = 1;
46 % === OPTION EXAMPLE
47 sProcess.options.example2.Comment = 'Example option 2';
48 sProcess.options.example2.Type = 'checkbox';
49 sProcess.options.example2.Value = 1;
50 % === OPTION EXAMPLE
51 sProcess.options.example3.Comment = 'Example option 3: ';
52 sProcess.options.example3.Type = 'value';
53 sProcess.options.example3.Value = {5, 'units', 2};
54 end
55
56
57 %% ===== FORMAT COMMENT =====
58 function Comment = FormatComment(sProcess) %#ok<DEFNU>
59 Comment = sProcess.Comment;
60 end
61
62
63 %% ===== RUN =====
64 function OutputFiles = Run(sProcess, sInputs) %#ok<DEFNU>
65 % Initialize returned list of files
66 OutputFiles = {};
67 % Get option values
68 example1 = sProcess.options.example1.Value;
69 example2 = sProcess.options.example2.Value;
70 example3 = sProcess.options.example3.Value{1};
71
72 % ===== LOAD THE DATA =====
73 % Read the first file in the list, to initialize the loop
74 DataMat = in_bst(sInputs(1).FileName, [], 0);
75 epochSize = size(DataMat.F);
76 Time = DataMat.Time;
77 % Initialize the load matrix: [Nchannels x Ntime x Nepochs]
78 AllMat = zeros(epochSize(1), epochSize(2), length(sInputs));
79 % Reading all the input files in a big matrix
80 for i = 1:length(sInputs)
81 % Read the file #i
82 DataMat = in_bst(sInputs(i).FileName, [], 0);
83 % Check the dimensions of the recordings matrix in this file
84 if ~isequal(size(DataMat.F), epochSize)
85 % Add an error message to the report
86 bst_report('Error', sProcess, sInputs, 'One file has a different number of channels or a different number of time samples.');
87 % Stop the process
88 return;
89 end
90 % Add the current file in the big load matrix
91 AllMat(:,:,i) = DataMat.F;
92 end
93
94 % ===== PROCESS =====
95 % Just doing a simple average of the trials, can be replaced with anything
96 AllMat = mean(AllMat, 3);
97
98 % ===== SAVE THE RESULTS =====
99 % Get the output study (pick the one from the first file)
100 iStudy = sInputs(1).iStudy;
101 % Create a new data file structure
102 DataMat = db_template('datamat');
103 DataMat.F = AllMat;
104 DataMat.Comment = 'Custom comment';
105 DataMat.ChannelFlag = ones(epochSize(1), 1); % List of good/bad channels (1=good, -1=bad)
106 DataMat.Time = Time;
107 DataMat.DataType = 'recordings';
108 DataMat.nAvg = length(sInputs); % Number of epochs that were averaged to get this file
109 % Create a default output filename
110 OutputFiles{1} = bst_process('GetNewFilename', fileparts(sInputs(1).FileName), 'data_custom_');
111 % Save on disk
112 save(OutputFiles{1}, '-struct', 'DataMat');
113 % Register in database
114 db_add_data(iStudy, OutputFiles{1}, DataMat);
115 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.