Write a Python plugin¶
A Python plugin is a folder with a small text file that describes it (desc.txt) and a Python script
(__main__.py). AnyWave lists the plugin in its Processes menu and, when you launch it, runs the script with a
Python interpreter. The script talks to AnyWave through the anywave package: it reads the data and
the markers of the recording that is open, and sends back markers, messages and changes of display.
The folder of your plugins is:
Documents\AnyWave\Plugins\Pythonon Windows;/Users/<username>/AnyWave/Plugins/Pythonon macOS;/home/<username>/AnyWave/Plugins/Pythonon Linux.
Requirements¶
You need a Python 3 interpreter, and the anywave-plugin-api package, version 1.0.3 or later (with numpy and
PyQt5, which pip installs for it), in the Python environment that runs the plugins. Open Edit > Preferences > Python:

- Python interpreter is the Python that AnyWave uses. It is detected when AnyWave starts, and Set path picks
another one. On Windows, Install Python installs one for you (with
winget) when none is found. - Set default Python environment chooses where the plugins run: one of the virtual environments (venv) that AnyWave found, or Do not use venv, use default Python installation, which runs the plugins with the interpreter above.
- When there is no environment called
anywave, a button Add anywave venv creates it and installsanywave-plugin-apiin it. This is the quickest way to start.
The virtual environments are the subfolders of Plugins/Python/venv that contain a Python environment made with
python -m venv. The name of the folder is the name of the environment. To use your own libraries, install them in
the environment with its own pip:
Plugins\Python\venv\anywave\Scripts\pip install scipy # Windows
Plugins/Python/venv/anywave/bin/pip install scipy # macOS and Linux
To use an environment other than the default one for one plugin, add venv = <name> to its
desc.txt.
To use the package in an environment that you made yourself:
pip install anywave-plugin-api
Your first plugin¶
The example of this page is a detector: it reads the EEG channels, computes the RMS of every channel on windows of one second, and marks the windows where the RMS is above a threshold.
1. Create the plugin¶
Choose Processes > Create new MATLAB or Python plugin. This is the Plugin Creation Assistant.

Give a name and a description (both are required), choose Python as the Type and click Create. The
choices of the other groups end in desc.txt, see below:
| In the assistant | Written in desc.txt |
|---|---|
| Channel Selection: Accepts, Requires or Ignores channel selection | modifiers_flags |
| Default channel source: Get current montage or Get raw file channels | input_flags |
| Can run with no file open | flags = nodatarequired |
| Can be run from command line | flags = canrunfromcommandline |
AnyWave creates the folder RmsDetector in your Python plugins folder, with desc.txt and a __main__.py that
already calls anywave.init(sys.argv), and opens it. A name that is already used by another Python plugin is refused.
2. Write __main__.py¶
import sys
import numpy as np
import anywave as aw
aw.init(sys.argv)
props = aw.get_props()
aw.send_message('RMS detector on ' + props['data_file'])
threshold = 50.0 # in the unit of the channels (microvolts for EEG)
# 1. the EEG channels of the current montage, without filter
out = aw.get_data({'types': ['EEG'], 'raw_data': True})
if not out:
raise SystemExit('No EEG channel in the current montage.')
data = out[0]['data'] # one row per channel, one column per sample
names = [c.name for c in out[0]['channels']]
sr = out[0]['channels'][0].sr # sampling rate in Hz
# 2. RMS of every channel on windows of one second
size = int(round(sr))
markers = []
for w in range(data.shape[1] // size):
segment = data[:, w * size:(w + 1) * size]
rms = np.sqrt(np.mean(segment ** 2, axis=1))
for c in np.nonzero(rms > threshold)[0]:
marker = aw.Marker(label='high_rms', position=float(w), duration=1.0,
value=float(rms[c]), colour='#FF8800')
marker.targets = [names[c]]
markers.append(marker)
# 3. send the markers back to AnyWave
if markers:
aw.send_markers(markers)
aw.send_message('%d marker(s) sent' % len(markers))
Every function is described in The anywave package.
3. Add a menu entry¶
Open desc.txt and add a category line, to put the plugin in a submenu of the Processes menu:
name = RmsDetector
description = Marks the seconds where the RMS is too high
category = Process:Detectors:RMS detector
input_flags = getcurrentmontage
4. Run it¶
AnyWave looks for the plugins when it starts: restart it after you added a plugin. (Changing the code of
__main__.py needs no restart: the script is read each time the plugin runs.)
Open a recording and choose Processes > Detectors > RMS detector. AnyWave starts the script with the Python environment of the plugin.
- The Processes panel shows the plugin, its state (here Finished) and its running time. Remove takes it out of the list, Show Log gives access to its log.

- Everything the script writes with
print(), and the messages ofanywave.send_message(), are in the debug log (View > Debug logs). Choose the tagProcess::RmsDetectorto see the plugin only.

- The markers sent by the plugin are in the marker list. Here the 19
high_rmsmarkers are drawn in orange, on the channel that triggered each one (Display markers, in the menu of the view, shows them in the signal view).

The plugin folder¶
Plugins/Python/RmsDetector/
├── desc.txt required: what AnyWave knows about the plugin
├── __main__.py required: the script that AnyWave runs
├── args.json optional: the options of the command line
└── batch_ui.json optional: the parameters window of the Batch
AnyWave runs the script with two numbers on its command line, the identifier of the plugin and the port of AnyWave,
which anywave.init(sys.argv) reads: always pass sys.argv as it is. Put your other modules in the folder of the
plugin: it is where Python looks first for the modules that __main__.py imports.
desc.txt¶
A text file with one key = value line per setting. Lines that start with # are comments. A value cannot contain
the character =.
| key | meaning |
|---|---|
name |
required. The name of the plugin, as it is listed in AnyWave and as the command line calls it. |
description |
required. A sentence that says what the plugin does. |
version |
the version of your plugin, for instance 1.2.0. Default: 1.0.0. |
category |
Process:<Menu>:<Entry>: the plugin is in the submenu <Menu> of the Processes menu, under the label <Entry>. Without it, the plugin is a direct entry of the Processes menu, with its name. |
venv |
the name of the Python environment that runs this plugin (see Requirements). Without it, the default environment of the preferences is used. |
input_flags |
which channels get_data returns when you do not ask for any in particular: getcurrentmontage (the channels of the montage, the default) or getasrecordedchannels (the channels of the file, without the montage). |
flags |
what the plugin can do, see below. |
modifiers_flags |
written by the assistant from its Channel Selection choice: acceptchannelselection, processrequireschannelselection or processignoreschannelselection. It does not change what get_data returns, see the tip below. |
The flags are separated by a colon, and written in any case:
| flag | effect |
|---|---|
nodatarequired |
the plugin can run even when no recording is open (its other name is processdoesntrequiredata) |
canrunfromcommandline |
the plugin can run from the command line and in the Batch |
flags = nodatarequired:canrunfromcommandline
Tip
To get the channels that are selected in the views, ask for them in your code:
aw.get_data({'channels_source': 'selection'}), see get_data.
Command line¶
A plugin can run when AnyWave is launched from the command line, with no window:
anywave --run MyPlugin --input_file <filepath> --plugin_option1 option --plugin_option2 option ...
Add the flag canrunfromcommandline to desc.txt:
flags = canrunfromcommandline
Handle the command line options¶
The options of the command line are in the dictionary returned by anywave.get_props():
import sys
import anywave as aw
aw.init(sys.argv)
args = aw.get_props()
print(args['plugin_option1']) # value of plugin_option1 on the command line
print(args['plugin_option2']) # value of plugin_option2 on the command line
This only works when AnyWave knows the options. Otherwise the command line stops with an error that says that
plugin_option1 and plugin_option2 are unknown options. List them in a file called args.json in the folder
of the plugin:
{
"parameters" : ["plugin_option1", "plugin_option2"]
}
The names of the keys are in lowercase. Besides parameters, args.json accepts two more keys: switches for
options written alone on the command line (--name) and flags for options that take true or false
(--name true). See Command line for the three kinds.
Parameters window for the Batch¶
The Batch tool of AnyWave runs a plugin on many files. It asks for the parameters of the plugin once, before the
first file, and applies them to every file. To make your plugin available in the Batch with a parameters window,
without writing any user interface code, add a second file called batch_ui.json next to args.json. The plugin
must also have the canrunfromcommandline flag in desc.txt. AnyWave builds the window from this description.
For example, this batch_ui.json:
{
"gain": { "label": "Gain", "type": "double", "method": "spinbox", "default": 2.5, "min": 0, "max": 100 },
"iterations": { "label": "Iterations", "type": "int", "method": "spinbox", "default": 10, "min": 1, "max": 500 },
"window": { "label": "Window", "type": "string", "method": "combobox", "choices": ["hann", "hamming", "none"], "default": "hann" },
"output_tag": { "label": "Output label", "type": "string", "method": "lineedit", "default": "run1" },
"normalize": { "label": "Normalize", "type": "bool", "method": "checkbox", "default": true },
"fields_ordering": ["gain", "iterations", "window", "output_tag", "normalize"]
}
with this args.json:
{
"parameters": ["gain", "iterations", "window", "output_tag"],
"flags": ["normalize"]
}
gives a window with five fields: two number boxes (decimal and integer), a list, a text field and a check box.
| key | meaning |
|---|---|
label |
the text shown in front of the field (default: the name of the option) |
type |
int, double, string or bool |
method |
spinbox (number, int or double), combobox (a list, needs choices), lineedit (text) or checkbox (bool) |
default |
the initial value |
min, max |
the limits of a spinbox |
choices |
the entries of a combobox |
fields_ordering |
required: the options to show, in the order of the window |
Things to know:
- Every field of batch_ui.json is also an option of the plugin: declare it in args.json (
parametersfor numbers and text,flagsfor check boxes). - The Batch writes the values, with their type (number, text,
trueorfalse), in the job file of each run (see Running a plugin). The plugin receives them like the other options of the command line (in the dictionary returned byaw.get_props()). - The filters, the montage and the choice of markers (
hp,lp,notch,montage_file,create_montage,use_markers,skip_markers) are asked by the Batch itself: do not declare them, they are ignored. - This window exists only in the Batch. When your plugin is launched from the Processes menu, AnyWave shows no parameters window of its own: the plugin asks the user for what it needs itself.
More¶
- The anywave package: all the functions.
- get_data: the channels and the time ranges to read.
- Debug a Python plugin in your IDE.