Skip to content

Commit

Permalink
Improved handle opening Data, Results and Matrices with different tim…
Browse files Browse the repository at this point in the history
…e definition (#735)

* Improved handle opening Data, Results and Matrices with different time definition.
  
* Bugfix: Correctly load montages when opening a new Data file and unloading other Data files

* Bugfix: Error handling in for opening Result files with diff time definition

---------

Co-authored-by: rcassani <[email protected]>
  • Loading branch information
Edouard2laire and rcassani authored Sep 9, 2024
1 parent e8b808e commit 23b805b
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 42 deletions.
123 changes: 81 additions & 42 deletions toolbox/core/bst_memory.m
Original file line number Diff line number Diff line change
Expand Up @@ -795,9 +795,6 @@ function LoadChannelFile(iDS, ChannelFile)
GlobalData.DataSet(iDS).DataFile = file_short(DataFile);
GlobalData.DataSet(iDS).Measures = Measures;

% ===== LOAD CHANNEL FILE =====
LoadChannelFile(iDS, ChannelFile);

% ===== Check time window consistency with previously loaded data =====
if isTimeCheck
% Update time window
Expand All @@ -814,25 +811,18 @@ function LoadChannelFile(iDS, ChannelFile)
return;
% Otherwise: unload all the other datasets
else
% Save newly created dataset
bakDS = GlobalData.DataSet(iDS);
% Unload everything
UnloadAll('Forced');
% If not everything was unloaded correctly (eg. the user cancelled half way when asked to save the modifications)
if ~isempty(GlobalData.DataSet)
% Unload the new dataset
UnloadDataSets(iDS);
iDS = [];
return;
iDS = UnloadOtherDs(iDS);
if isempty(iDS)
return
end
% Restore new dataset
GlobalData.DataSet = bakDS;
iDS = 1;
% Update time window
isTimeCoherent = CheckTimeWindows();
end
end
end

% ===== LOAD CHANNEL FILE =====
LoadChannelFile(iDS, ChannelFile);

% ===== UPDATE TOOL TABS =====
if ~isempty(iDS) && strcmpi(GlobalData.DataSet(iDS).Measures.DataType, 'raw')
Expand Down Expand Up @@ -1251,14 +1241,24 @@ function ReloadStatDataSets() %#ok<DEFNU>
isTimeCoherent = CheckTimeWindows();
% If loaded results are not coherent with previous data
if ~isTimeCoherent
% Remove it
GlobalData.DataSet(iDS).Results(iResult) = [];
iDS = [];
iResult = [];
bst_error(['Time definition for this file is not compatible with the other files' 10 ...
'already loaded in Brainstorm.' 10 10 ...
'Close existing windows before opening this file, or use the Navigator.'], 'Load results', 0);
return
res = java_dialog('question', [...
'The time definition is not compatible with previously loaded files.' 10 ...
'Unload all the other files first?' 10 10], 'Load results', [], {'Unload other files', 'Cancel'});
% Cancel: Unload the new dataset
if isempty(res) || strcmpi(res, 'Cancel')
UnloadDataSets(iDS);
iDS = [];
return;
% Otherwise: unload all the other datasets
else
iDS = UnloadOtherDs(iDS);
if isempty(iDS)
iResult = [];
return
end
% Update time window
isTimeCoherent = CheckTimeWindows();
end
end
end
% Update TimeWindow panel, if it exists
Expand Down Expand Up @@ -1982,40 +1982,60 @@ function LoadResultsMatrix(iDS, iResult)
if isempty(iDS) && isempty(Mat.Events)
iDS = GetDataSetStudy(sStudy.FileName);
end
% Create dataset
if isempty(iDS)
% Check time against existing DS
isTimeOkDs = 1;
if ~isempty(iDS) && (length(Mat.Time) >= 2)
% Save measures information if no DataFile is available
if isempty(GlobalData.DataSet(iDS).Measures) || isempty(GlobalData.DataSet(iDS).Measures.Time)
GlobalData.DataSet(iDS).Measures.Time = double(Mat.Time([1, end]));
GlobalData.DataSet(iDS).Measures.SamplingRate = double(Mat.Time(2) - Mat.Time(1));
GlobalData.DataSet(iDS).Measures.NumberOfSamples = length(Mat.Time);
elseif (abs(Mat.Time(1) - GlobalData.DataSet(iDS).Measures.Time(1)) > 1e-5) || ...
(abs(Mat.Time(end) - GlobalData.DataSet(iDS).Measures.Time(2)) > 1e-5) || ...
~isequal(length(Mat.Time), GlobalData.DataSet(iDS).Measures.NumberOfSamples)
isTimeOkDs = 0;
end
end
% Create dataset if not existent or different time definition
if isempty(iDS) || ~isTimeOkDs
% Create a new DataSet only for results
iDS = length(GlobalData.DataSet) + 1;
GlobalData.DataSet(iDS) = db_template('DataSet');
GlobalData.DataSet(iDS).SubjectFile = file_short(sStudy.BrainStormSubject);
GlobalData.DataSet(iDS).StudyFile = file_short(sStudy.FileName);
% Save measures information
GlobalData.DataSet(iDS).Measures.Time = double(Mat.Time([1, end]));
GlobalData.DataSet(iDS).Measures.SamplingRate = double(Mat.Time(2) - Mat.Time(1));
GlobalData.DataSet(iDS).Measures.NumberOfSamples = length(Mat.Time);
end
% Make sure that there is only one dataset selected
iDS = iDS(1);

% ===== CHECK TIME =====
% If there time in this file
if (length(Mat.Time) >= 2)
isTimeOkDs = 1;
% Save measures information if no DataFile is available
if isempty(GlobalData.DataSet(iDS).Measures) || isempty(GlobalData.DataSet(iDS).Measures.Time)
GlobalData.DataSet(iDS).Measures.Time = double(Mat.Time([1, end]));
GlobalData.DataSet(iDS).Measures.SamplingRate = double(Mat.Time(2) - Mat.Time(1));
GlobalData.DataSet(iDS).Measures.NumberOfSamples = length(Mat.Time);
elseif (abs(Mat.Time(1) - GlobalData.DataSet(iDS).Measures.Time(1)) > 1e-5) || ...
(abs(Mat.Time(end) - GlobalData.DataSet(iDS).Measures.Time(2)) > 1e-5) || ...
~isequal(length(Mat.Time), GlobalData.DataSet(iDS).Measures.NumberOfSamples)
isTimeOkDs = 0;
end
% Update time window
isTimeCoherent = CheckTimeWindows();
% If loaded file are not coherent with previous data
if ~isTimeCoherent || ~isTimeOkDs
iDS = [];
bst_error(['Time definition for this file is not compatible with the other files' 10 ...
'already loaded in Brainstorm.' 10 10 ...
'Close existing windows before opening this file, or use the Navigator.'], 'Load matrix', 0);
return
res = java_dialog('question', [...
'The time definition is not compatible with previously loaded files.' 10 ...
'Unload all the other files first?' 10 10], 'Load matrix', [], {'Unload other files', 'Cancel'});
% Cancel: Unload the new dataset
if isempty(res) || strcmpi(res, 'Cancel')
UnloadDataSets(iDS);
iDS = [];
return;
% Otherwise: unload all the other datasets
else
iDS = UnloadOtherDs(iDS);
if isempty(iDS)
iMatrix = [];
return
end
% Update time window
isTimeCoherent = CheckTimeWindows();
end
end
% Update TimeWindow panel
panel_time('UpdatePanel');
Expand Down Expand Up @@ -3556,5 +3576,24 @@ function SaveChannelFile(iDS)
end
end

%% ===== UNLOAD OTHER DS =====
function iDS = UnloadOtherDs(iDS)
% Unload Brainstorm datasets except for iDS. It returns the new iDS (iDS=1) for the kept DS
global GlobalData;
% Save dataset to keep
bakDS = GlobalData.DataSet(iDS);
% Unload everything
UnloadAll('Forced');
% If not everything was unloaded correctly (eg. the user cancelled half way when asked to save the modifications)
if ~isempty(GlobalData.DataSet)
% Unload also dataset to keep
UnloadDataSets(iDS);
iDS = [];
return;
end
% Restore dataset
GlobalData.DataSet = bakDS;
iDS = 1;
end


3 changes: 3 additions & 0 deletions toolbox/gui/view_surface_data.m
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@
iDS = bst_memory('GetDataSetSubject', sSubject.FileName, 1);
end

if isempty(iDS)
return;
end

%% ===== MODALITY =====
if isempty(Modality)
Expand Down

0 comments on commit 23b805b

Please sign in to comment.