Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[enh] Make all process compatible with raw files #219

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bst_plugin/GLM/nst_glm_add_regressors.m
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@
if strcmp(sFile.FileType, 'data') % Imported data structure
sDataIn = in_bst_data(sFile.FileName);
elseif strcmp(sFile.FileType, 'raw') % Continuous data file
sDataIn = in_bst(sFileFileName, [], 1, 1, 'no');
sDataIn = in_bst(sFile.FileName, [], 1, 1, 'no');
end

ChannelMat = in_bst_channel(sFile.ChannelFile);
Expand Down
Binary file modified bst_plugin/math/dg_voronoi.mexw64
Binary file not shown.
97 changes: 71 additions & 26 deletions bst_plugin/mbll/process_nst_dOD.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
% For more information type "brainstorm license" at command prompt.
% =============================================================================@
%
% Authors: Thomas Vincent (2015-2016)
% Authors: Thomas Vincent (2015-2016), Edouard Delaire (2023)

eval(macro_method);
end
Expand All @@ -36,7 +36,7 @@
% Definition of the input accepted by this process
sProcess.InputTypes = {'data', 'raw'};
% Definition of the outputs of this process
sProcess.OutputTypes = {'data', 'data'};
sProcess.OutputTypes = {'data', 'raw'};
sProcess.nInputs = 1;
sProcess.nMinFiles = 1;

Expand Down Expand Up @@ -115,10 +115,12 @@
if strcmp(sInputs.FileType, 'data') % Imported data structure
sDataIn = in_bst_data(sInputs(1).FileName);
events = sDataIn.Events;
isRaw = 0;
elseif strcmp(sInputs.FileType, 'raw') % Continuous data file
sDataIn = in_bst(sInputs(1).FileName, [], 1, 1, 'no');
sDataRaw = in_bst_data(sInputs(1).FileName, 'F');
events = sDataRaw.F.events;
isRaw = 1;
end

parameters = parse_options(sProcess, sDataIn);
Expand Down Expand Up @@ -149,35 +151,78 @@
if strcmp(cond_name(1:4), '@raw')
cond_name = cond_name(5:end);
end
iStudy = db_add_condition(sInputs.SubjectName, [cond_name, '_dOD']);

if isRaw
newCondition = ['@raw', cond_name, '_dOD'];
else
newCondition = [cond_name, '_dOD'];
end

iStudy = db_add_condition(sInputs.SubjectName, newCondition);
sStudy = bst_get('Study', iStudy);
%

% % Save channel definition
[tmp, iChannelStudy] = bst_get('ChannelForStudy', iStudy);
db_set_channel(iChannelStudy, ChannelMat, 0, 0);

% Save time-series data
% final_nirs = sDataIn.F;
% final_nirs(to_keep, :) = final_nirs';
sDataOut = db_template('data');
sDataOut.F = final_dOD';
sDataOut.Comment = sDataIn.Comment;
%sDataOut.ChannelFlag = sDataIn.ChannelFlag;
sDataOut.ChannelFlag = ones(size(final_dOD, 2), 1);
sDataOut.Time = sDataIn.Time;
sDataOut.DataType = 'recordings';
sDataOut.nAvg = 1;
sDataOut.Events = events;
sDataOut.History = sDataIn.History;
sDataOut = bst_history('add', sDataOut, 'process', sProcess.Comment);
sDataOut.DisplayUnits = 'delta OD';

% Generate a new file name in the same folder
OutputFile = bst_process('GetNewFilename', bst_fileparts(sStudy.FileName), 'data_OD');
sDataOut.FileName = file_short(OutputFile);
bst_save(OutputFile, sDataOut, 'v7');
% Register in database
db_add_data(iStudy, OutputFile, sDataOut);
% Generate a new file name in the same folder
OutputFile = bst_process('GetNewFilename', bst_fileparts(sStudy.FileName), 'data_0raw_OD');

if ~isRaw

% Save time-series data
sDataOut = db_template('data');
sDataOut.F = final_dOD';
sDataOut.Comment = [sDataIn.Comment ' | OD '];
sDataOut.ChannelFlag = ones(size(final_dOD, 2), 1);
sDataOut.Time = sDataIn.Time;
sDataOut.DataType = 'recordings';
sDataOut.nAvg = 1;
sDataOut.Events = events;
sDataOut.History = sDataIn.History;
sDataOut = bst_history('add', sDataOut, 'process', sProcess.Comment);
sDataOut.DisplayUnits = 'delta OD';

sDataOut.FileName = file_short(OutputFile);
Edouard2laire marked this conversation as resolved.
Show resolved Hide resolved
bst_save(OutputFile, sDataOut, 'v7');
% Register in database
db_add_data(iStudy, OutputFile, sDataOut);

else
ProtocolInfo = bst_get('ProtocolInfo');

newStudyPath = bst_fullfile(ProtocolInfo.STUDIES, sInputs.SubjectName, newCondition);

[tmp, rawBaseOut, rawBaseExt] = bst_fileparts(newStudyPath);
rawBaseOut = strrep([rawBaseOut rawBaseExt], '@raw', '');
% Full output filename
RawFileOut = bst_fullfile(newStudyPath, [rawBaseOut '.bst']);

sFileIn = sDataRaw.F;
sFileIn.channelflag = ones(size(final_dOD, 2), 1);
sFileIn.ChannelFlag = ones(size(final_dOD, 2), 1);

[sFileOut, errMsg] = out_fopen(RawFileOut, 'BST-BIN', sFileIn, ChannelMat);

% Set Output sFile structure
sOutMat.format = 'BST-BIN';
sOutMat.F = sFileOut;
sOutMat.DataType = 'raw';
sOutMat.History = sDataIn.History;
sOutMat = bst_history('add', sOutMat, 'process', sProcess.Comment);
sOutMat.ChannelFlag = ones(size(final_dOD, 2), 1);
sOutMat.DisplayUnits = 'delta OD';

sOutMat.Comment = [sDataIn.Comment ' | OD '];
% Save new link to raw .mat file
bst_save(OutputFile, sOutMat, 'v6');
% Create new channel file
db_set_channel(iStudy, ChannelMat, 2, 0);
% Write block
out_fwrite(sFileOut, ChannelMat, 1, [], [], final_dOD');
% Register in BST database
db_add_data(iStudy, OutputFile, sOutMat);
end
end


Expand Down
91 changes: 67 additions & 24 deletions bst_plugin/mbll/process_nst_mbll.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
% For more information type "brainstorm license" at command prompt.
% =============================================================================@
%
% Authors: Thomas Vincent (2015-2020)
% Authors: Thomas Vincent (2015-2020), Edouard Delaire (2023)

eval(macro_method);
end
Expand All @@ -36,7 +36,7 @@
% Definition of the input accepted by this process
sProcess.InputTypes = {'data', 'raw'};
% Definition of the outputs of this process
sProcess.OutputTypes = {'data', 'data'}; %TODO: 'raw' -> 'raw' or 'raw' -> 'data'?
sProcess.OutputTypes = {'data', 'raw'};
sProcess.nInputs = 1;
sProcess.nMinFiles = 1;
% Definition of the options
Expand Down Expand Up @@ -72,10 +72,8 @@
function OutputFile = Run(sProcess, sInputs) %#ok<DEFNU>
OutputFile = {};

% TODO: check for negative values
% Get option values
age = sProcess.options.option_age.Value{1};

do_plp_corr = sProcess.options.option_do_plp_corr.Value;
pvf = sProcess.options.option_pvf.Value{1};
dpf_method = sProcess.options.option_dpf_method.Value{1};
Expand All @@ -87,11 +85,14 @@
if strcmp(sInputs.FileType, 'data') % Imported data structure
sDataIn = in_bst_data(sInputs(1).FileName);
events = sDataIn.Events;
isRaw = 0;
elseif strcmp(sInputs.FileType, 'raw') % Continuous data file
sDataIn = in_bst(sInputs(1).FileName, [], 1, 1, 'no');
sDataRaw = in_bst_data(sInputs(1).FileName, 'F');
events = sDataRaw.F.events;
isRaw = 1;
end


dOD_params = process_nst_dOD('parse_options', sProcess, sDataIn);
if isempty(dOD_params)
Expand All @@ -114,7 +115,6 @@


% Apply MBLL
% TODO: add baseline window and expose it
[nirs_hb, channels_hb] = Compute(fnirs, fchannel_def, age, dOD_params, do_plp_corr, pvf, dpf_method);

% Re-add other channels that were not changed during MBLL
Expand All @@ -125,32 +125,75 @@
if length(cond_name)>=4 && strcmp(cond_name(1:4), '@raw')
cond_name = cond_name(5:end);
end
iStudy = db_add_condition(sInputs.SubjectName, [cond_name, '_Hb']);


if isRaw
newCondition = ['@raw', cond_name, '_Hb'];
else
newCondition = [cond_name, '_Hb'];
end

iStudy = db_add_condition(sInputs.SubjectName, newCondition);
sStudy = bst_get('Study', iStudy);

% Save channel definition
[tmp, iChannelStudy] = bst_get('ChannelForStudy', iStudy);
db_set_channel(iChannelStudy, ChannelMat, 0, 0);

% Save time-series data
sDataOut = db_template('data');
sDataOut.F = final_nirs';
sDataOut.Comment = [sInputs(1).Comment ' | Hb [Topo]'];
sDataOut.ChannelFlag = ones(size(final_nirs, 2), 1);
sDataOut.Time = sDataIn.Time;
sDataOut.DataType = 'recordings';
sDataOut.nAvg = 1;
sDataOut.Events = events;
sDataOut.History = sDataIn.History;
sDataOut = bst_history('add', sDataOut, 'process', sProcess.Comment);
sDataOut.DisplayUnits = 'mol.l-1';

% Generate a new file name in the same folder
OutputFile = bst_process('GetNewFilename', bst_fileparts(sStudy.FileName), 'data_hb');
sDataOut.FileName = file_short(OutputFile);
bst_save(OutputFile, sDataOut, 'v7');
% Register in database
db_add_data(iStudy, OutputFile, sDataOut);
OutputFile = bst_process('GetNewFilename', bst_fileparts(sStudy.FileName), 'data_0raw_hb');
if ~isRaw
% Save time-series data
sDataOut = db_template('data');
sDataOut.F = final_nirs';
sDataOut.Comment = [sInputs(1).Comment ' | Hb [Topo]'];
sDataOut.ChannelFlag = ones(size(final_nirs, 2), 1);
sDataOut.Time = sDataIn.Time;
sDataOut.DataType = 'recordings';
sDataOut.nAvg = 1;
sDataOut.Events = events;
sDataOut.History = sDataIn.History;
sDataOut = bst_history('add', sDataOut, 'process', sProcess.Comment);
sDataOut.DisplayUnits = 'mol.l-1';

% Generate a new file name in the same folder
OutputFile = bst_process('GetNewFilename', bst_fileparts(sStudy.FileName), 'data_hb');
sDataOut.FileName = file_short(OutputFile);
bst_save(OutputFile, sDataOut, 'v7');
% Register in database
db_add_data(iStudy, OutputFile, sDataOut);
else
ProtocolInfo = bst_get('ProtocolInfo');
newStudyPath = bst_fullfile(ProtocolInfo.STUDIES, sInputs.SubjectName, newCondition);

[tmp, rawBaseOut, rawBaseExt] = bst_fileparts(newStudyPath);
rawBaseOut = strrep([rawBaseOut rawBaseExt], '@raw', '');
% Full output filename
RawFileOut = bst_fullfile(newStudyPath, [rawBaseOut '.bst']);

sFileIn = sDataRaw.F;
sFileIn.channelflag = ones(size(final_nirs, 2), 1);
[sFileOut, errMsg] = out_fopen(RawFileOut, 'BST-BIN', sFileIn, ChannelMat);

% Set Output sFile structure
sOutMat.format = 'BST-BIN';
sOutMat.F = sFileOut;
sOutMat.DataType = 'raw';
sOutMat.History = sDataIn.History;
sOutMat = bst_history('add', sOutMat, 'process', sProcess.Comment);
sOutMat.ChannelFlag = ones(size(final_nirs, 2), 1);
sOutMat.DisplayUnits = 'mol.l-1';
sOutMat.Comment = [sInputs(1).Comment ' | Hb [Topo]'];

% Save new link to raw .mat file
bst_save(OutputFile, sOutMat, 'v6');
% Create new channel file
db_set_channel(iStudy, ChannelMat, 2, 0);
% Write block
out_fwrite(sFileOut, ChannelMat, 1, [], [], final_nirs');
% Register in BST database
db_add_data(iStudy, OutputFile, sOutMat);
end
end

function [fdata, fchannel_def] = ...
Expand Down
Loading