Error when compiling brainstorm

Hi!

We are developping a home tool (using the App Designer) based on Brainstorm. The idea would be to create a standalone app. To do so, we tried to compile Brainstorm (bst_compiled.m) but we got an error (see error message below). It seems the file 'plugin.mat' is missing, and I can't find it in the brain2mesh-master folder.

Error using bst_compile (line 208)
COMPILE> MCC returned an error:
Error while determining required deployable files. Compilation terminated. Details:
Error using matlab.depfun.internal.MatFileInspector/determineType (line 32)
File, function or class "brain2mesh-master\plugin.mat"
may not exist. Neither WHICH nor EXIST could find an exact, case-sensitive match. Please check the spelling of
the name, and that any required directories are on the MATLAB path.

Is there a way to solve this problem ?

Thank you in advance!
Corentin

There is something wrong in the configuration of the path of the files provided to mcc.
It looks like your call results in trying to add "brain2mesh-master\plugin.mat" which indeed doesn't exist.

Put a breakpoint at line 194 to check that the plugin brain2mesh is correctly loaded.
It should add:

-a "C:\Users\franc\.brainstorm\plugins\brain2mesh\brain2mesh-master" 
-a "C:\Users\franc\.brainstorm\plugins\brain2mesh\plugin.mat"

If you have issues compiling Brainstorm with the plugins, then compile it without.
Make sure you clean your Matlab path before running brainstorm compile: it should contain only the Matlab installation folders.

Hi Francois,

Thank you for your answer, there is no more problem with the loading of the plugin brain2mesh.

However, we now have an error (see error message below) when trying to copy a file. The problem is that the folder R2021b contains no file. Which file is it trying to copy and why does our R2021b folder is empty?

Error using copyfile
No matching files named 'D:\Brainstorm\brainstorm3\bin\R2021b\*.*' were found.

Error in bst_compile (line 264)
copyfile(fullfile(binDir, '*.*'), destDir);

Thank you in advance,
Corentin

This copy operation is completely optional, it is part of the creation of a .zip file with the new binary distribution. You could ignore this error, but the fact that the folder brainstorm3\bin\R2021b\ is empty is a sign that more silent errors occurred before in the script.

The folder brainstorm3\bin\R2021b\ is supposed to contain the compiled brainstorm distribution packaged as a .jar file. See the brainstorm3\bin\R2020a\ from the current distribution for execution.

If you don't have any file at the moment of the copy, it is because the creation of this .jar file failed:

In order to understand why this is not working: you need to debug the bst_compile.m script, and try to run the jar file creation from a CMD terminal manually or check the existence of each file.
The problem is due to another problem before, maybe the compilation with mcc failed?...

Once you manage to get the .jar file generated correct: Here are additional files that could be added to the brainstorm3\bin\R2021b\ folder, to make it easier to start the compiled Brainstorm version from the .jar file:
R2021b.zip (2.5 KB)

Hi François,

We have been working on an app that uses Brainstorm. Up until recently, we were using the app in a Matlab environment (using Brainstorm nogui). Now, we want to have a compiled version of this app.

We compiled the app but had problems. First, we had to move the two .jar files (brainstorm.jar and riverLayout.jar) to a specific folder. We then had a problem when calling Brainstorm functions. For example, when calling the function bst_get('ProtocolInfo'), we got an error because the function 'panel_process1' was unrecognized.

We then tried to compile the app without starting Brainstorm (we removed the lines that were starting Brainstorm nogui). It did not work. When calling the function bst_get('ProtocolInfo'), we got an error because the variable 'GlobalData' was empty.

We are not sure how we can obtain a compiled app that uses Brainstorm.

Any advice would be greatly appreciated!
Thanks,
Corentin

Can you please confirm can you can compile a non-modified version of today's version of Brainstorm using brainstorm compile noplugs?
Tutorials/Scripting - Brainstorm

If this work, but you can't compile your modifications, please:

  • describe what you've changing,
  • post all the error messages you get
  • copy-paste the output of the ver command

Make sure that you can't find a way to run your code as a script passed to the compiled version of Brainstorm we distribute:
https://neuroimage.usc.edu/brainstorm/Tutorials/Scripting#Without_Matlab

Hi Francois,

Thanks for your help! We can now compile a modified version of Brainstorm correctly (although we will be using the version you distribute!).

We tried running our code as a script using the compiled version of Brainstorm (and MATLAB Runtime). Our script takes a json file as an input. The json file contains a list of processes with the parameters. This list of processes is then stored in the property of a custom MATLAB class that we have developped.

Although we can run simple script, it seems like MATLAB Runtime does not support script that calls custom classes.

Is there a way to work around this problem ?

Thanks,
Corentin

Although we can run simple script, it seems like MATLAB Runtime does not support script that calls custom classes.

I added a new paragraph to the documentation:

"In this configuration, the Matlab Runtime is not allowed to compile and execute .m scripts as a regular Matlab installation would. To work around this limitation, Brainstorm reads the script.m file and executes its contents with the Matlab eval function. Therefore this approach does not support the definition of functions or classes within the script.m. The compiled version of Brainstorm can only run code that can be executed from the Matlab Command Window. If you need more flexibility, you need to recompile a modified version of Brainstorm including your custom code (see below)."

https://neuroimage.usc.edu/brainstorm/Tutorials/Scripting#Without_Matlab

Is there a way to work around this problem ?

Unfortunately not.
If you need additional classes or functions, you need to include them in the compilation of Brainstorm...

Hi Francois,

Sorry for the late response!
We included our classes and functions in the compilation of Brainstorm.

It all works fine when we run a script with no parameters (or argument). We tried to run a MATLAB function file with an argument, but it does not work (see error message below). It seems like the parameter passed in the command window is not recognized. We use the syntax indicated in the tutorial : brainstorm3.bat 'script.m' 'parameters'.

Is it possible to run a function file that requires arguments ?

Thank you for your help,
Corentin

Here is the error messgae:

"WARNING: This file is a function, trying to convert to a script...
Error using panel_command>ExecuteScript
The number of argmuents in the function does not match the number of parameters passed in the command line.

Error in panel_command (line 26)

Error in brainstorm (line 277)
"

Indeed, I introduced this bug when allowing the execution of scripts on local databases. It was not possible anymore to execute scripts with command-line parameters in a "standard" context (database in $HOME/.brainstorm).
I tried to fix this with this commit: Bugfix: Allow compiled execution with script parameters · brainstorm-tools/brainstorm3@1afea7d · GitHub

Please update Brainstorm and try re-running the compilation of your modified version of Brainstorm.
Let me know how it goes.

I added a section to explain how to pass parameters to the scripts in the documentation:
https://neuroimage.usc.edu/brainstorm/Tutorials/Scripting#Parameters

Hi Francois,

Just a quick question about compilation in Linux.

It seems it's not available when compiling Brainstorm including all plugins. I get the following error:
Preparing the folder spmtrip with bst_spmtrip.m is available only for Windows (for the moment).

I then tried compiling Brainstorm without plugins, but I get the following error:
COMPILE> MCC returned an error:
bin/bash: mcc: command not found

I don't understand this error since we have the MATLAB Compiler installed (MATLAB Compiler Version 8.2 (R2021a) and MATLAB Compiler SDK Version 6.10 (R2021a)).

Is it possible to compile Brainstorm in Linux or is it a problem with the MATLAB Compiler we have?

Thanks for your time,
Corentin

Sorry for the response delay...

Preparing the folder spmtrip with bst_spmtrip.m is available only for Windows

I modified the functions bst_spmtrip and bst_compile to work also on Linux and MacOS:

bin/bash: mcc: command not found

This is because the Matlab bin folder is not included in the system path.
Before starting Matlab, make sure that the folder that contains the executable mcc is on from the system path.
I edited the tutorial to add this information:
https://neuroimage.usc.edu/brainstorm/Tutorials/Scripting#How_to_compile_Brainstorm

Hi François,

Thanks for the response, the compilation on Linux seems to work fine.

I still have one question: I tried using Brainstorm compiled on Linux but it did not work (see error message below). I tested with a script that used to work with Brainstorm compiled on Windows. I made sure the syntax was correct (brainstorm3.command MATLABROOT script.m parameters)

Thanks for your help,
Corentin

Error message:

Please wait...
 
If it hangs for more than a few minutes: try pressing ENTER.
Alternatively, download Brainstorm for a different version of the Matlab Runtime.
(See the installation instructions on the Brainstorm website)
 
 
Usage : brainstorm start           : Start Brainstorm
        brainstorm nogui           : Start Brainstorm with hidden interface (for scripts)
        brainstorm server          : Start Brainstorm on a distant server (completely headless)
        brainstorm <script> <args> : Start Brainstorm in server mode, execute the input script and quit
        brainstorm ... local       : Start Brainstorm with a local database (in .brainstorm folder)
        brainstorm stop            : Quit Brainstorm
        brainstorm update          : Download and install latest Brainstorm update (see bst_update)
        brainstorm reset           : Re-initialize Brainstorm database and preferences
        brainstorm digitize        : Digitize electrodes positions and head shape using a Polhemus system
        brainstorm setpath         : Add Brainstorm subdirectories to current path
        brainstorm startjava       : Add Brainstorm Java classes to dynamic classpath
        brainstorm info            : Open Brainstorm website
        brainstorm forum           : Open Brainstorm forum
        brainstorm license         : Display license
        brainstorm tutorial name   : Run the validation script attached to a tutorial (ctf, neuromag, raw, resting, yokogawa)
        brainstorm tutorial all    : Run all the validation scripts
        brainstorm packagebin      : Create separate zip files for all the currently available binary distributions
  res = brainstorm('status')     : Return brainstorm status (1=running, 0=stopped)
 
BST> Compiled code returned without errors.

Passing arguments works in different ways in Windows and Linux, maybe there is something to fix...

Can you try compiling only Brainstorm first, not your script? (so that executing from the command line starts the Brainstorm interface)
Just to make sure the compilation does work in your setup.

I tried compiling only Brainstorm and I get the same behavior. When I execute brainstorm3.command in the command line, the Brainstorm interface opens. So the compilation seems to work. Also, I get the same error message when trying to run a script with or without parameters.

Also, I get the same error message when trying to run a script with or without parameters.

This seems to indicate there is an error somewhere on your side.
The current version of Brainstorm works on Linux for executing a script.

Example script:

% Input files
sFiles = {'sepi01/tutorial_eeg_band_copy/data_block001.mat'};

% Start a new report
bst_report('Start', sFiles);

% Process: Add EEG positions
sFiles = bst_process('CallProcess', 'process_channel_addloc', sFiles, [], ...
    'channelfile', {'', ''}, ...
    'usedefault',  'ICBM152: 10-10 65', ...  % ICBM152: 10-10 65
    'fixunits',    1, ...
    'vox2ras',     1);

disp([10 'EXECUTION OF THE SCRIPT WORKS' 10]);

% Save and display report
ReportFile = bst_report('Save', sFiles);
bst_report('Open', ReportFile);

Execution from a terminal (Ubuntu 20 on WSL / bash):

ftadel@DESKTOP-SQA66ET:/mnt/c/Work/Dev$ ./brainstorm3/bin/R2022b/brainstorm3.command /home/ftadel/MATLAB_Runtime/R2022b /mnt/c/Users/franc/script_new.m

Please wait...

If it hangs for more than a few minutes: try pressing ENTER.
Alternatively, download Brainstorm for a different version of the Matlab Runtime.
(See the installation instructions on the Brainstorm website)


BST> Starting Brainstorm:
BST> =================================
BST> BrainstormHomeDir = /home/ftadel/.mcrCache9.13/bst_ja0/bst_javabuil
BST> Version: 09-Jan-2023
BST> Deleting old process reports...
BST> Loading configuration file...
BST> Plugin brain2mesh: /home/ftadel/.mcrCache9.13/bst_ja0/Users/franc/.brainstorm/plugins/brain2mesh
BST> Plugin iso2mesh: /home/ftadel/.mcrCache9.13/bst_ja0/Users/franc/.brainstorm/plugins/iso2mesh
BST> Plugin brainentropy: /home/ftadel/.mcrCache9.13/bst_ja0/Users/franc/.brainstorm/plugins/brainentropy
BST> Plugin blackrock: /home/ftadel/.mcrCache9.13/bst_ja0/Users/franc/.brainstorm/plugins/blackrock
BST> Plugin neuroelectrics: /home/ftadel/.mcrCache9.13/bst_ja0/Users/franc/.brainstorm/plugins/neuroelectrics
BST> Plugin xdf: /home/ftadel/.mcrCache9.13/bst_ja0/Users/franc/.brainstorm/plugins/xdf
BST> Plugin libsvm: /home/ftadel/.mcrCache9.13/bst_ja0/Users/franc/.brainstorm/plugins/libsvm
BST> Plugin fastica: /home/ftadel/.mcrCache9.13/bst_ja0/Users/franc/.brainstorm/plugins/fastica
BST> Plugin picard: /home/ftadel/.mcrCache9.13/bst_ja0/Users/franc/.brainstorm/plugins/picard
BST> Plugin derivelfp: /home/ftadel/.mcrCache9.13/bst_ja0/Users/franc/.brainstorm/plugins/derivelfp
BST> Plugin nirstorm: /home/ftadel/.mcrCache9.13/bst_ja0/Users/franc/.brainstorm/plugins/nirstorm
BST> Plugin mcxlab-cl: /home/ftadel/.mcrCache9.13/bst_ja0/Users/franc/.brainstorm/plugins/mcxlab-cl
BST> Plugin mia: /home/ftadel/.mcrCache9.13/bst_ja0/Users/franc/.brainstorm/plugins/mia
BST> Plugin spm12: /home/ftadel/.mcrCache9.13/bst_ja0/Users/franc/.brainstorm/spmtrip
BST> Reading process folder...
BST> Loading current protocol...
BST> =================================


EXECUTION OF THE SCRIPT WORKS

BST> Saving protocol "TutorialEpilepsy"...
BST> Emptying temporary directory...
BST> Brainstorm stopped.
BST> Compiled code returned without errors.

Hi Francois,

Sorry for the delay.

The problem seemed to be the path to the script. The script I was trying to execute contained a capital letter and it apparently caused of the error.

For example, If I run a script named 'script_new.m', I have no error. If I simply modify the name to 'Script_new.m', I get the error I mentioned earlier. I have the same problem if the folder containing the script has a capital letter.

It is a weird problem and I don't understand why, but I'll be aware. If you know the reason, I'd be curious to understand!

Thanks for your time!
Corentin

The function lower was applied to the command line before trying to execute the script, causing the file not to be found on Linux and MacOS (while windows file names are not case sensitive).
I fixed this bug here: https://github.com/brainstorm-tools/brainstorm3/commit/f3c58de81e18c092d2d884e2d7b86d3ffedab256

I will post updated compiled versions of Brainstorm later today (2022b and 2020a).
Thanks for reporting this issue.