Skip to content

Commit 1347fca

Browse files
committed
Simulations: Bugfixes on simulation in different studies
1 parent a99505d commit 1347fca

3 files changed

Lines changed: 28 additions & 16 deletions

File tree

doc/license.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<body alink="#fff000" link="#fff000" vlink="#fff000">
66
<h4><span style="font-family: Arial Black; color: #ffffff;"><strong>THERE IS NO UNDO BUTTON!<BR>SET UP A <FONT color=red>BACKUP</FONT> OF YOUR DATABASE</strong></span></h4>
77
<HR>
8-
<!-- LICENCE_START -->Version: 3.260522 (22-May-2026)<br>
8+
<!-- LICENCE_START -->Version: 3.260525 (25-May-2026)<br>
99
<span style="font-style: italic;">COPYRIGHT &copy; 2000-2025
1010
USC &amp; McGill University.<br>
1111
</span>

doc/version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
% Brainstorm
2-
% v. 3.260522 (22-May-2026)
2+
% v. 3.260525 (25-May-2026)

toolbox/math/bst_simulation.m

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@
3636
%% ===== PARSE INPUTS =====
3737
global GlobalData;
3838

39-
% If target study is empty, we use the study of the source map
40-
if (nargin < 5) || isempty(iStudy)
41-
[~, iStudy] = bst_get('ResultsFile', ResultsFile);
39+
% No target study
40+
if (nargin < 5)
41+
iStudy = [];
4242
end
4343
% Is iVertices obtained on a volume atlas
4444
if (nargin < 4) || isempty(isVolumeAtlas)
@@ -75,7 +75,7 @@
7575
end
7676
% Get associated data file
7777
if ~isempty(ResultsMat.DataFile)
78-
DataFile = file_short(ResultsMat.DataFile);
78+
[~, DataFile] = bst_fileparts(file_short(ResultsMat.DataFile));
7979
else
8080
DataFile = [];
8181
end
@@ -124,6 +124,18 @@
124124

125125
% ===== LOAD GAIN MATRIX =====
126126
bst_progress('text', 'Loading head model...');
127+
% Get target study
128+
[sStudyResults, iStudyResults] = bst_get('ResultsFile', ResultsFile);
129+
if isempty(iStudy) || (iStudy == iStudyResults)
130+
sStudy = sStudyResults;
131+
iStudy = iStudyResults;
132+
else
133+
sStudy = bst_get('Study', iStudy);
134+
if isempty(sStudy)
135+
bst_error(sprintf('Target study (%d) does not exist in the Protocol.', iStudy), 'bst_simulation', 0);
136+
return
137+
end
138+
end
127139
% Get default headmodel for this study
128140
sHeadModel = bst_get('HeadModelForStudy', iStudy);
129141
if isempty(sHeadModel)
@@ -137,18 +149,18 @@
137149
nLocHeadmodel = size(HeadModelMat.GridLoc, 1);
138150

139151
% If the head model doesn't match the number of vertices: try loading the head model pointed by the results file
140-
if (nLocHeadmodel ~= nLocResults)
152+
if (nLocHeadmodel ~= nLocResults) && (iStudy == iStudyResults)
141153
% Get headmodel file from ResultsFile
142154
HeadModelFile = ResultsMat.HeadModelFile;
143155
% Load HeadModel file
144156
HeadModelMat = in_bst_headmodel(HeadModelFile, 0, 'Gain', 'GridLoc', 'GridOrient', 'GridAtlas');
145157
% Number of dipoles in headmodel
146158
nLocHeadmodel = size(HeadModelMat.GridLoc, 1);
147-
% Check again the number of vertices
148-
if (nLocHeadmodel ~= nLocResults)
149-
bst_error(sprintf('Number of dipoles in the head model (%d) and the inverse model (%d) do not match.', nLocHeadmodel, nLocResults), 'bst_simulation', 0);
150-
return;
151-
end
159+
end
160+
% Check the number of vertices
161+
if (nLocHeadmodel ~= nLocResults)
162+
bst_error(sprintf('Number of dipoles in the head model (%d) and the inverse model (%d) do not match.', nLocHeadmodel, nLocResults), 'bst_simulation', 0);
163+
return;
152164
end
153165
% If no orientations: error
154166
if (nComponents ~= 3) && isempty(HeadModelMat.GridOrient)
@@ -211,7 +223,7 @@
211223
DataMat.Comment = DataComment;
212224
DataMat.Time = TimeVector;
213225
DataMat.F = F;
214-
DataMat.ChannelFlag = GlobalData.DataSet(iDS).Results(iResult).ChannelFlag;
226+
DataMat.ChannelFlag = ones(size(HeadModelMat.Gain, 1), 1);
215227
DataMat.DataType = 'recordings';
216228
% History
217229
DataMat = bst_history('add', DataMat, 'simulation', 'File simulated: Headmodel * Results');
@@ -222,11 +234,11 @@
222234
%% ===== SAVE FILE =====
223235
% Output file
224236
if isempty(DataFile)
225-
outputFolder = bst_fileparts(GlobalData.DataSet(iDS).StudyFile);
226-
newDataFile = bst_fullfile(ProtocolInfo.STUDIES, outputFolder, ['data_simulation_', strTime, '.mat']);
237+
OutputFileName = ['data_simulation_', strTime, '.mat'];
227238
else
228-
newDataFile = bst_fullfile(ProtocolInfo.STUDIES, strrep(DataFile, '.mat', '_simulation.mat'));
239+
OutputFileName = [DataFile, '_simulation.mat'];
229240
end
241+
newDataFile = bst_fullfile(ProtocolInfo.STUDIES, bst_fileparts(sStudy.FileName), OutputFileName);
230242
newDataFile = file_unique(newDataFile);
231243
% Save file
232244
bst_save(newDataFile, DataMat, 'v6');

0 commit comments

Comments
 (0)