Skip to content

Instantly share code, notes, and snippets.

@martcous
Last active October 1, 2018 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martcous/accae9bf2be4b2a3e2b6f8be864c76af to your computer and use it in GitHub Desktop.
Save martcous/accae9bf2be4b2a3e2b6f8be864c76af to your computer and use it in GitHub Desktop.
Compute area of selected scouts
function varargout = process_scout_area( varargin )
% PROCESS_SCOUTAREA: Computes the area of selected scouts
% @=============================================================================
% This function is part of the Brainstorm software:
% https://neuroimage.usc.edu/brainstorm
%
% Copyright (c)2000-2018 University of Southern California & McGill University
% This software is distributed under the terms of the GNU General Public License
% as published by the Free Software Foundation. Further details on the GPLv3
% license can be found at http://www.gnu.org/copyleft/gpl.html.
%
% FOR RESEARCH PURPOSES ONLY. THE SOFTWARE IS PROVIDED "AS IS," AND THE
% UNIVERSITY OF SOUTHERN CALIFORNIA AND ITS COLLABORATORS DO NOT MAKE ANY
% WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF
% MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, NOR DO THEY ASSUME ANY
% LIABILITY OR RESPONSIBILITY FOR THE USE OF THIS SOFTWARE.
%
% For more information type "brainstorm license" at command prompt.
% =============================================================================@
%
% Authors: Martin Cousineau, 2018
eval(macro_method);
end
%% ===== GET DESCRIPTION =====
function sProcess = GetDescription() %#ok<DEFNU>
% Description the process
sProcess.Comment = 'Compute scout area';
sProcess.Category = 'Custom';
sProcess.SubGroup = 'External';
sProcess.Index = 400;
sProcess.Description = '';
% Definition of the input accepted by this process
sProcess.InputTypes = {'data', 'results', 'timefreq', 'matrix'};
sProcess.OutputTypes = {'data', 'results', 'timefreq', 'matrix'};
sProcess.nInputs = 1;
sProcess.nMinFiles = 1;
% === CSV OUTPUT FILE
SelectOptions = {...
'', ... % Filename
'', ... % FileFormat
'save', ... % Dialog type: {open,save}
'Save output...', ... % Window title
'ExportData', ... % LastUsedDir: {ImportData,ImportChannel,ImportAnat,ExportChannel,ExportData,ExportAnat,ExportProtocol,ExportImage,ExportScript}
'single', ... % Selection mode: {single,multiple}
'files', ... % Selection mode: {files,dirs,files_and_dirs}
{{'.csv'}, 'ASCII: Comma-separated (*.csv)', 'ASCII-CSV'}, ...
''};
sProcess.options.csvfile.Comment = 'Output CSV file: ';
sProcess.options.csvfile.Type = 'filename';
sProcess.options.csvfile.Value = SelectOptions;
% === SCOUTS
sProcess.options.scouts.Comment = '';
sProcess.options.scouts.Type = 'scout';
sProcess.options.scouts.Value = {};
end
%% ===== FORMAT COMMENT =====
function Comment = FormatComment(sProcess) %#ok<DEFNU>
Comment = sProcess.Comment;
end
%% ===== RUN =====
function sInputs = Run(sProcess, sInputs) %#ok<DEFNU>
subjects = {};
scouts = {};
areas = [];
nOutputs = 0;
% Parse inputs
if ~isempty(sProcess.options.csvfile.Value)
csvFile = sProcess.options.csvfile.Value{1};
else
bst_report('Error', sProcess, sInputs, 'No output file specified.');
return;
end
if ~isempty(sProcess.options.scouts.Value) && ~isempty(sProcess.options.scouts.Value{2})
selectedAtlas = sProcess.options.scouts.Value{1};
selectedScouts = sProcess.options.scouts.Value{2};
else
bst_report('Error', sProcess, sInputs, 'No selected scout.');
return;
end
for iInput = 1:length(sInputs)
% Load basic subject information
sInput = sInputs(iInput);
subject = sInput.SubjectName;
sSubject = bst_get('Subject', subject);
if ismember(subject, subjects)
disp(['Warning: subject ' subject ' already computed, skipping...']);
continue;
end
% Find selected atlas
foundAtlas = 0;
if ~isempty(sSubject.iCortex)
SurfaceFile = sSubject.Surface(sSubject.iCortex).FileName;
sSurf = bst_memory('LoadSurface', SurfaceFile);
for iAtlas = 1:length(sSurf.Atlas)
if strcmpi(sSurf.Atlas(iAtlas).Name, selectedAtlas)
foundAtlas = iAtlas;
break;
end
end
end
if foundAtlas == 0
bst_report('Error', sProcess, sInput, ['Could not find atlas for subject ' subject]);
continue;
end
% Find selected scouts
sScouts = sSurf.Atlas(foundAtlas).Scouts;
for iSelScout = 1:length(selectedScouts)
foundScout = 0;
for iScout = 1:length(sScouts)
if strcmpi(sScouts(iScout).Label, selectedScouts{iSelScout})
foundScout = iScout;
break;
end
end
if foundScout == 0
bst_report('Error', sProcess, sInput, ['Could not find scout ' selectedScouts{iSelScout} ' for subject ' subject]);
continue;
end
% Compute area
allVertices = unique([sScouts(foundScout).Vertices]);
totalArea = sum(sSurf.VertArea(allVertices)) * 100 * 100;
nOutputs = nOutputs + 1;
subjects{nOutputs} = subject;
scouts{nOutputs} = selectedScouts{iSelScout};
areas(nOutputs) = totalArea;
end
end
% Save output
fid = fopen(csvFile, 'w') ;
fprintf(fid, 'atlas,subject,scout,area\n');
for iOutput = 1:nOutputs
fprintf(fid, '%s,%s,%s,%f\n', selectedAtlas, subjects{iOutput}, scouts{iOutput}, areas(iOutput));
end
fclose(fid);
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment