Hi,
The following question steamed from interacting with Brainstorm from an IPython notebook, but it probably (not sure, need to check) applies to any use of Brainstorm through a Matlab server (rather than the usual Matlab interface). Basically, in my notebook, I’m opening scout mat files, changing the colors of the scouts according to statistical calculation of scouts average activity (power, connectivity, etc.), saving the modified scout mat file, loading it in Brainstorm, displaying it and saving the resulting figure. However, I’m meeting some difficulties for the plotting part of this pipeline… From Python’s side, I use python-matlab-bridge (https://arokem.github.io/python-matlab-bridge/) to open a socket to a Matlab server:
from pymatbridge import Matlab
mlab = Matlab() #Matlab(matlab='C:/Program Files/MATLAB/R2015a/bin/matlab.exe')
mlab.start()
matlabCode = \
"""
rootPath = 'C:/Users/Orechr/Dropbox/code/analyses/John/notebooks/';
addpath(rootPath)
"""
result = mlab.run_code(matlabCode)
Then, I would run the following Matlab code each time I want to make a figure (with different arguments for the Matlab function of course) :
matlabCode = \
"""
show_scouts('AaCi/tess_cortex_pial_low.mat', 'left', 'conditionEffect_inter', 'test.png')
"""
result = mlab.run_code(matlabCode)
First annoyance is that I have to add “brainstorm nogui” each time I want to run this last piece of code, making it in fact
matlabCode = \
"""
brainstorm nogui
show_scouts('AaCi/tess_cortex_pial_low.mat', 'left', 'conditionEffect_inter', 'test.png')
"""
result = mlab.run_code(matlabCode)
Of course, directly in Matlab, I would only have to run “brainstorm nogui” once but for some reason I don’t understand, here it has to be ran at every call. It should not be the case since Matlab’s session is always the same (e.g., I don’t need to re-set the path at every call… its valid for the whole session, until I hit “mlab.stop()”). If I don’t run this “brainstorm nogui” command at every call, it throws an exception on first Brainstorm call and the reason seems to be associated with the a blank Brainstorm’s global variable “GlobalData” (i.e. GlobalData == []).
The second issue is that I get the following error:
which I don’t get when I execute this code in Matlab environment.
The third issue is that when I run this code in the Matlab environment, I have opaque scouts (alpha=0; as should be according to the code of the “show_scouts” function) but when I run it through the Matlab server, I get translucent scouts as if there was something overriding my specification that alpha=0. Here is the code of the “show_scouts” function (in which I’m probably messing with Brainstorm in ways a gentleman should not):
function show_scouts(SurfaceFile, orient, atlasName, figName)
% orient : 'left', 'right_intern', 'right', 'left_intern', 'back', 'front', 'bottom', 'top'
sSurf = bst_memory('LoadSurface', SurfaceFile);
[sSubject, iSubject, iSurf] = bst_get('SurfaceFile', SurfaceFile);
iAtlas = 0;
for i=1:size(sSurf.Atlas, 2)
if strcmp(sSurf.Atlas(i).Name, atlasName)
iAtlas = i;
end
end
global GlobalData;
GlobalData.Surface(iSurf).iAtlas = iAtlas;
GlobalData.Surface(iSurf).isAtlasModified = 1;
SurfAlpha=0.0;
% Get GlobalData DataSet associated with subjectfile (create if does not exist)
SubjectFile = sSubject.FileName;
iDS = bst_memory('GetDataSetSubject', SubjectFile, 1);
iDS = iDS(1);
% Prepare FigureId structure
FigureId = db_template('FigureId');
FigureId.Type = '3DViz';
FigureId.SubType = '';
FigureId.Modality = '';
% Create figure
[hFig, iFig, isNewFig] = bst_figures('CreateFigure', iDS, FigureId, 'AlwaysCreate');
if isempty(hFig)
bst_error('Could not create figure.', 'View surface', 0);
return;
end
% Set application data
setappdata(hFig, 'SubjectFile', SubjectFile);
%% ===== DISPLAY SURFACE =====
% Add surface to figure
iSurf = panel_surface('AddSurface', hFig, SurfaceFile);
if isempty(iSurf)
return
end
% Set transparency
if ~isempty(SurfAlpha)
panel_surface('SetSurfaceTransparency', hFig, iSurf, SurfAlpha);
end
% Set figure as current figure
%bst_figures('SetCurrentFigure', hFig, '3D');
% Show all scouts for this surface (for cortex only)
panel_scout('ReloadScouts', hFig);
% Make sure to update the Headlight
camlight(findobj(hFig, 'Tag', 'FrontLight'), 'headlight');
% Camera basic orientation
figure_3d('SetStandardView', hFig, orient);
set(hFig, 'Name', 'Atlas');
set(hFig, 'Visible', 'off');
out_figure_image(hFig, figName);
bst_figures('DeleteFigure', hFig, 'NoUnload');
%bst_memory('UnloadAll', 'Forced');
Any idea on what could cause these three issues? I have the feeling that they are probably all linked with Brainstorm’s global variable. My guess is that global variables are not necessarily handled in the same way in these two contexts (global variables are the root of all computational evil after all). Although I know this question is very specific, does anyone had any experience with similar issues?