AnyWave:MATLAB Batch

From WikiMEG
Revision as of 15:29, 9 April 2020 by Bruno (Talk | contribs)

Jump to: navigation, search

Introduction

One of the features of AnyWave is to run some processing from the command line.
This is called batch processing, allowing to process many files in different locations using a script file run by the OS.

Make my plugin runnable in command line

If you want AnyWave to handle your plugin using the command line, you must do as follow:

1 - edit the desc.txt

name = MyPlugin
description = do something in MATLAB
category = Process:Test:MyPlugin
flags = CanRunFromCommandLine

Note: we've added a line with the flags keyword. This keyword will inform AnyWave of the capabilities of your plugin.
You may combine flags using the : (the colon character). Example:
flags = CanRunFromCommandLine:NoDataRequired
This indicates that the plugin is able to be called in batch mode, but also indicates that no data file is required. (The plugin will not run on a data file.)

2 - edit your MATLAB code

Modify the main.m file:

function main(varargin)
global args;  %% IMPORTANT: this variable will hold the arguments set by the user on the command line. 
if isdeployed  % this is the code to use if you plan to Compile your plugin to distribute it.
% STANDALONE AnyWave Plugin code
   global host;   
   global port;
   global pid;
 
   if (nargin < 3) % basic argument checking
       error('missing arguments.');
  end
  host = varargin{1};
  port = str2num(varargin{2});
  pid = str2num(varargin{3});
  if (nargin > 3)
    args = varargin{4};
  end
 
  assignin('base', 'host',  host);
  assignin('base', 'port', port);
  assignin('base', 'pid', pid);
  assignin('base', 'args', args);
% end of STANDALONE AnyWave Plugin code
end
 
% we assume here that the code will always run in batch mode.
% To check if the plugin was called in batch mode, just check if args variable is empty or not.
if isempty(args)
   error('this plugin will only run in batch mode');
end
 
% code your stuff here
% use args to get the arguments:
% args is a structure containing fields named upon the arguments set by the command line.
%
% one of the common argument set by AnyWave is input_file which contains the path to the data file to process.
% so to get the file use :  file = args.input_file;


Make my plugin compatible

This is quite simple. First, you need to edit the desc.txt and add the CanRunFromCommandLine flag:

edit desc.txt

Note: you can also use the NoDataRequired if your plugin does not a file to be open in AnyWave to run.
In this case the flags line will be: flags = CanRunFromCommandLine:NoDataRequired

name = MyPlugin
description = do something in MATLAB
category = Process:Test:MyPlugin
flags = CanRunFromCommandLine

JSON Files

After you've modified the desc.txt you will need to create two json files and place them in your plugin folder.

ui.json

There are some important things to setup in ui.json file:
There is a key describing the input parameters of the plugin. Suppose that you want to compute something on several files, you must then set several input_keys in the json file.
This is done by creating the input_keys key.
This key will hold the names of all the other keys that must be considered as files that AnyWave will provide to our plugin when running it.
In our example below, there is only ONE file key, that we called input_file which is the default key name for the file argument.
Hence, AnyWave will bring a GUI to pick up all the files we want, and run our plugin on all those files.
Once you setup the FILES entries keys, you must also describe them in the file by setting two values to the corresponding key.
The first value is always the decorated readable name of the key that will be used to create the GUI.
The second value is a string. If you set "check" as the value, that will tell AnyWave to check that the file setup is a data file and not a side car file.
If you don't want a data file to be set, just set an empty string as the value.

Let's setup an example using one input file and two parameters:

{
"input_keys" : [ "input_file"],
"input_file" : ["Input File", "check"],
"hp" : ["High Pass Filter", "double"],
"lp" : ["Low Pass Filter", "double"],
"fields_ordering" : ["hp", "lp"]
}

input_keys : mandatory key. Here, we setup only one input file, and the key is called "input_file".
Then we must describe that key:

  • "Input File" is the name used in the GUI to describe the parameter.
  • "check" indicates that the file MUST be a data file that AnyWave can read.

fields_ordering: mandatory key. Describe the GUI fields order. Here we set up HP before LP in the GUI.

Between the mandatory keys, you have the other parameters:

  • hp described as High Pass Filter, and "double" indicates this parameter is a double value.
  • lp described as Low Pass Filter, and "double" indicates this parameter is a double value.

Others possible parameter values:

value description
"double" double precision value
"int" integer value
"list" indicates we want a combo list selection among predefined item.
"string" string value
"stringList" an array of strings.

default.json

This file comes to se the default values for parameters described in ui.json.
So you need to define the same keys and set a default value for them.
In our skeleton that will be:

{
"input_file" : "", 
"hp" : 0.,
"lp" : 0.
}

GUI representation of ui.json

See below the GUI representation of keys inserted in the ui.json:

key GUI
"hp" : ["High Pass", "double"] Json ui key double.png
"n_iter" : ["#Iterations", "int"] Json ui key int.png
"modality" : ["Modality", "list"] Json ui key list.png
"output_prefix" : ["Output Prefix", "string"] Json ui key string.png

Note: The list type will make a combobox list selection of items. Define the possible items in default.json file:
"modality" : ["MEG", "EEG", "SEEG"]

Example

A small test plugin that takes TWO files as input:

  • a MEG file
  • an EEG file

desc.txt

name = MAT_BATCH
description = TEST_BATCH
category = Process:TEST:MAT_BATCHING
flags = CanRunFromCommandLine:NoDataRequired

ui.json

{
"input_keys" : [ "meg_file", "eeg_file"],
"meg_file" : ["MEG File", "check"],
"eeg_file" : ["EEG File", "check"],
"output_file_prefix" : ["Output File Prefix", "string"],
 "fields_ordering": [ "output_file_prefix" ]
}

We set up two files as input (eeg_file, meg_file).
output_file_prefix is a string variable we will use to prefix our output filename.
There is only one field because meg_file and eeg_file are input files keys, so they won't appear in the left part of the GUI but in the input file part.

default.json

{
"meg_file" : "",
"eeg_file" : "",
"output_file_prefix" : "resampled"
}

main.m

function main(varargin)
global args;
if isdeployed
% STANDALONE AnyWave Plugin code
   global host;
   global port;
   global pid;
 
   if (nargin < 3)
       error('missing arguments.');
  end
  host = varargin{1};
  port = str2num(varargin{2});
  pid = str2num(varargin{3});
  if (nargin > 3)
    args = varargin{4};
  end
 
  assignin('base', 'host',  host);
  assignin('base', 'port', port);
  assignin('base', 'pid', pid);
  assignin('base', 'args', args);
% end of STANDALONE AnyWave Plugin code
end
 
% we assume here that the code will always run in batch mode.
% To check if the plugin was called in batch mode, just check if args variable is empty or not.
if isempty(args)
   error('this plugin will only run in batch mode');
end
 
 
pathMEG = args.meg_file;
pathEEG = args.eeg_file;
infosMEG = aw_getfileinfo(pathMEG);
if isempty(infosMEG)
    error('Could not open MEG file');
end
infosEEG = aw_getfileinfo(pathEEG);
if isempty(infosEEG)
   error('Could not open EEG file');
end
 
cfg=[];
cfg.file = pathEEG;
markersEEG = aw_getmarkers(cfg);
cfg.file = pathMEG;
cfg.channels = { 'Trigger' };
markersMEG = aw_gettriggers(cfg);
 
cfg = [];
cfg.file = pathEEG;
cfg.filtering = 'yes';
cfg.start = 0;
cfg.duration = -1;
cfg.eeg_lp = infosEEG.max_sr / 3;
 
eegdata = aw_getdata(cfg);
assert(~isempty(eegdata));
disp(eegdata);
end