Difference between revisions of "AnyWave:MATLAB Batch"

From WikiMEG
Jump to: navigation, search
(parameters key/value)
(3. Define the arguments required by your plugin)
 
(32 intermediate revisions by one user not shown)
Line 1: Line 1:
 
=Introduction=
 
=Introduction=
One of the features of AnyWave is to run some processes in batch.<br/>
+
One of the features of AnyWave is to run some processing from the command line.<br/>
This means that you can use a dedicated GUI to program several processing on many files and execute all this processing<br/>
+
This is called batch processing, allowing to process many files in different locations using a script file run by the OS.<br/>
This feature is accessible from here:<br/>
+
=Make my plugin runnable in command line=
[[File:Process_batch_processing.png]]
+
If you want AnyWave to handle your plugin using the command line, you must do as follow:<br/>
=Make my plugin compatible=
+
== 1 - edit the desc.txt==
This is quite simple. First, you need to edit the desc.txt and add the CanRunFromCommandLine flag:<br/>
+
==edit desc.txt==
+
Note: you can also use the NoDataRequired if your plugin does not a file to be open in AnyWave to run.<br/>
+
In this case the flags line will be: flags = CanRunFromCommandLine:NoDataRequired<br/>
+
 
<syntaxhighlight lang="text">
 
<syntaxhighlight lang="text">
 
name = MyPlugin
 
name = MyPlugin
Line 15: Line 11:
 
flags = CanRunFromCommandLine
 
flags = CanRunFromCommandLine
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
Note: we've added a line with the flags keyword. This keyword will inform AnyWave of the capabilities of your plugin.<br/>
 +
You may combine flags using the : (the colon character). Example:<br/>
 +
flags = CanRunFromCommandLine:NoDataRequired<br/>
 +
This indicates that the plugin can be called from the command line but also indicates that no data file is required. (The plugin will not run on a data file.)<br/>
 +
== 2 - edit your MATLAB code ==
 +
Modify the main.m file:<br/>
 +
<syntaxhighlight lang="matlab">
 +
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;
  
After you've modified the desc.txt you will need to create two json files and place them in your plugin folder.<br/>
+
  if (nargin < 3) % basic argument checking
==UI.JSON==
+
      error('missing arguments.');
This file will describe parameters needed by your plugin in order to run.<br/>
+
  end
Use the following file as a skeleton for your needs:<br/>
+
  host = varargin{1};
We also adds two parameters, for data filtering.
+
  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;
 +
 
 +
</syntaxhighlight>
 +
== 3. Define the arguments required by your plugin==
 +
AnyWave offers a set of predefined arguments name, let's call them common arguments.<br/>
 +
See here to view all the common arguments name to use.<br/>
 +
But sometimes, you will need to use specific arguments for your plugin and you will need to let AnyWave know about them.<br/>
 +
To inform AnyWave about the argument key it will recognize from the command line and pass them to your plugin, you need to add a json file to the folder containing your plugin.<br>
 +
<br/>
 +
The file must be named '''args.json''' and must contain at least one key called '''parameters'''.<br/>
 +
An optional key named '''flags''' may also  be added. flags will be used as boolean arguments for the plugin.<br/>
 +
Example:<br/>
 
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
 
{
 
{
"input_keys" : [ "input_file"],
+
"parameters" : [ "eeg_file", "meg_file"],
"input_file" : ["Input File", "check"],
+
"flags" : [ "downsample", "use_threshold"]
"hp" : ["High Pass Filter", "double"],
+
"lp" : ["Low Pass Filter", "double"],
+
"fields_ordering" : ["hp", "lp"]
+
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
===mandatory keys/values===
+
Hence, AnyWave will be able to parse --eeg_file and --meg_file keys from the command line and pass them to your plugin (in the args global variable):<br/>
There are some required keys:<br/>
+
AnyWave will also parse arguments --downsample <true|yes|false|no> and set the corresponding boolean value.<br/>
'''input_keys''' : an array of string containing the key names of input files to be set by AnyWave.<br/>
+
<syntaxhighlight lang="matlab">
in our skeleton, there is only one key, 'input_file'. This means you must also provide that key afterward in the json file.<br/>
+
function main(varargin)
<br/>
+
global args;  %% DO NOT FORGET
let's have a look to our input_file key:<br/>
+
% init code
The value is an array of strings:
+
% ...
* The first one is a Human Readable version of the key. This will be used in the AnyWave GUI.
+
* the second  value is "check" meaning that the file must be a file AnyWave can read (and a check will be done by AnyWave).
+
'''fields_ordering''' : an array of strings containing all the other keys in the order we would like AnyWave will show them in the GUI.<br/>
+
Here, we set up hp to be first, and lp will follow.<br/>
+
This mean the GUI wil show the HP key first and then LP.<br/>
+
  
===parameters key/value===
+
eeg = args.eeg_file;
Beside the two required keys, you can add a key/value pair for each parameter you need to use in your plugin.<br/>
+
meg = args.meg_file;
In our example, we use lp and hp keys, for low pass and high pass filters.<br/>
+
 
The key must match the values in 'fields_ordering'.<br/>
+
% compute something...
The value must be an array of strings:<br/>
+
end
* the first value is the Human Readable name of the key, that will appear in the GUI.
+
</syntaxhighlight>
* the second value is the type of the variable. Here double means we want a numeric double value.
+

Latest revision as of 15:30, 21 April 2020

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 can be called from the command line 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;

3. Define the arguments required by your plugin

AnyWave offers a set of predefined arguments name, let's call them common arguments.
See here to view all the common arguments name to use.
But sometimes, you will need to use specific arguments for your plugin and you will need to let AnyWave know about them.
To inform AnyWave about the argument key it will recognize from the command line and pass them to your plugin, you need to add a json file to the folder containing your plugin.

The file must be named args.json and must contain at least one key called parameters.
An optional key named flags may also be added. flags will be used as boolean arguments for the plugin.
Example:

{
"parameters" : [ "eeg_file", "meg_file"],
"flags" : [ "downsample", "use_threshold"]
}

Hence, AnyWave will be able to parse --eeg_file and --meg_file keys from the command line and pass them to your plugin (in the args global variable):
AnyWave will also parse arguments --downsample <true|yes|false|no> and set the corresponding boolean value.

function main(varargin)
global args;  %% DO NOT FORGET
% init code
% ...
 
eeg = args.eeg_file;
meg = args.meg_file;
 
% compute something...
end