Skip to content

Instantly share code, notes, and snippets.

@rcassani
Created July 13, 2023 16:03
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 rcassani/be32ae4aac5f322b4adcb4605d0cae8b to your computer and use it in GitHub Desktop.
Save rcassani/be32ae4aac5f322b4adcb4605d0cae8b to your computer and use it in GitHub Desktop.
Brainstorm process: Process a raw file, create new raw file and add it to database
function varargout = process_something_raw_file( varargin )
% PROCESS_SOMETHING_RAW_FILE: Do something on a raw file, create new raw file, add it to BST database.
%
% @=============================================================================
% This software is part of the Brainstorm software:
% http://neuroimage.usc.edu/brainstorm
%
% Copyright (c)2000-2023 Brainstorm by the University of Southern California
% This software is distributed under the terms of the GNU General Public License
% as published by the Free Software Foundation. Further details on the GPL
% 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:
eval(macro_method);
end
%% ===== GET DESCRIPTION =====
function sProcess = GetDescription() %#ok<DEFNU>
% Description the process
sProcess.Comment = 'Something on raw file';
sProcess.Description = '';
sProcess.Category = 'Custom';
sProcess.SubGroup = 'Example';
sProcess.Index = 9999;
sProcess.Description = '';
% Definition of the input accepted by this process
sProcess.InputTypes = {'raw'};
sProcess.OutputTypes = {'raw'};
sProcess.nInputs = 1;
sProcess.nMinFiles = 1;
% Multiplier
sProcess.options.multiplier.Comment = 'Multiply raw data by: ';
sProcess.options.multiplier.Type = 'value';
sProcess.options.multiplier.Value = {1000, 'No units', 0};
end
%% ===== FORMAT COMMENT =====
function Comment = FormatComment(sProcess) %#ok<DEFNU>
Comment = sProcess.Comment;
end
%% ===== RUN =====
function sOutputFiles = Run(sProcess, sInput) %#ok<DEFNU>
sOutputFiles = {};
% Input structure
[sMat, matName] = in_bst(sInput.FileName, [], 1, 1, 'no');
signalsIn = sMat.(matName);
% Output structure
sOutMat = sMat;
signalsOut = signalsIn * sProcess.options.multiplier.Value{1};
% In sFile structure
DataMat = in_bst_data(sInput.FileName, 'F');
sFileIn = DataMat.F;
% Get channel file
ChannelMat = in_bst_channel(sInput.ChannelFile);
% Get input raw path and name
if ismember(sFileIn.format, {'CTF', 'CTF-CONTINUOUS'})
[~, rawBaseIn] = bst_fileparts(bst_fileparts(sFileIn.filename));
else
[~, rawBaseIn] = bst_fileparts(sFileIn.filename);
end
% Make sure that there are not weird characters in the folder names
rawBaseIn = file_standardize(rawBaseIn);
% New folder name
newCondition = ['@raw', rawBaseIn, '_something'];
% Get new condition name
ProtocolInfo = bst_get('ProtocolInfo');
newStudyPath = file_unique(bst_fullfile(ProtocolInfo.STUDIES, sInput.SubjectName, newCondition));
% Output file name derives from the condition name
[~, rawBaseOut, rawBaseExt] = bst_fileparts(newStudyPath);
rawBaseOut = strrep([rawBaseOut rawBaseExt], '@raw', '');
% Full output filename
RawFileOut = bst_fullfile(newStudyPath, [rawBaseOut '.bst']);
% Get input study (to copy the creation date)
sInputStudy = bst_get('AnyFile', sInput.FileName);
% Get new condition name
[~, ConditionName] = bst_fileparts(newStudyPath, 1);
% Create output condition
iOutputStudy = db_add_condition(sInput.SubjectName, ConditionName, [], sInputStudy.DateOfStudy);
% Get output study
sOutputStudy = bst_get('Study', iOutputStudy);
% Full file name
MatFile = bst_fullfile(ProtocolInfo.STUDIES, bst_fileparts(sOutputStudy.FileName), ['data_0raw_' rawBaseOut '.mat']);
% Create an empty Brainstorm-binary file
sFileOut = out_fopen(RawFileOut, 'BST-BIN', sFileIn, ChannelMat);
% Set Output sFile structure
sOutMat.F = sFileOut;
% Save new link to raw .mat file
bst_save(MatFile, sOutMat, 'v6');
% Create new channel file
db_set_channel(iOutputStudy, ChannelMat, 2, 0);
% Write block
out_fwrite(sFileOut, ChannelMat, 1, [], [], signalsOut);
% Register in BST database
db_add_data(iOutputStudy, MatFile, sOutMat);
sOutputFiles{end+1} = MatFile;
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment