Difference between revisions of "AnyWave:WritePythonScripted"

From WikiMEG
Jump to: navigation, search
(Channels)
Line 76: Line 76:
 
channel.notch = 50;    # Sets a notch filter for 50Hz
 
channel.notch = 50;    # Sets a notch filter for 50Hz
 
channel.data = numpy.zeros(1000); # Sets a vector of 1000 samples as the data for the channels.
 
channel.data = numpy.zeros(1000); # Sets a vector of 1000 samples as the data for the channels.
 +
channel.sr = 1000;    # Sets the sampling rate of data at 1000Hz.
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 16:55, 31 May 2017

Introduction

Here we assume that you have Python 2.7 installed on your system with at least the numpy package.

Configuring AnyWave to use Pyhton

It's very simple, just open the Preferences UI:

PythonPrefs.png

This is a Windows version of AnyWave on which we are using Anaconda Python package. The path is the location of the Python interpreter.
That's it, your are ready to use Python plugins.

What is a Python plugin?

It's a folder containing at least two files:

  • __main__.py Python code file.
  • desc.txt Text file giving information about the plugin.

Writing the desc.txt file

We will write a Python example plugin which will be named PyExample, so the desc.txt file should look like:

name = PyExample Plugin
description = I'm a Python plugin
category = Process:Python:PyExample

The syntax is to set keywords and values.

Here we have three keywords (name, description, category).
Two keywords are mandatory : name and description. Other keywords are optional.

keywords

name: The plugin name used by Anywave (here PyExample Plugin).
description: a brief description of what the plugin does.
category (optional): It tells AnyWave where the plug-in will appear in the menus. Here, we decided to make it appear under the Python sub-menu in the Processes main menu.

The category feature is usefull to separate plug-ins that won't really do some calculation but convert data to another format or launch external tools. It could also be useful to classify signal processing algorithms.

Three category keywords are recognized:

  • Process : The plug-in will be set in the Processes menu with a subcategory and a name, for example 'Process:Correlation:Compute correlation'
  • File: The plug-in will be set in the File Menu under the Export sub-menu. Example : 'File:Export to file.'
  • View: The plug-in will be set in the View Menu. Example : 'View:Launch 3D viewer'

If no category is specified, AnyWave will set the plug-in in the Processes menu using the name defined in the file.

Copying the plugin to the correct location

To make AnyWave find our plugin we must copy it to a specific location:

PyLocation.png

This is the default Documents folder on Windows in which AnyWave created its own folders.

The Python subfolder is where AnyWave will look for Python plugins.

The AnyWave module

The python support in AnyWave consists in a Python module called anywave which is automatically imported when launching the Python interpreter.
So it is not necessary to import it again when programming a Python plugin.

A short example

Let's see a very simple plugin example: (don't forget to place it in a __main__.py file)

# get informations about the current data associated with our plugin by AnyWave:
infos = anywave.get_plugininfo(); 
print infos;

As you can see it's very short and simple. The get_plugininfo method is documented in the Python objects section.
Basically, it returns the labels of the electrodes, their references, the maximum sampling rate of data, etc.

Python Objects

There are two objects of AnyWave currently available within the Python interpreter: markers and channels.
That means you can get markers from AnyWave in your plugin, or create your own marker in Python and send them to AnyWave.
The same possibility is available for channels.
Markers and Channels are the two main objects to handle data in AnyWave:

  • The signals you see in AnyWave are channels.
  • The time selections, or events are markers.

Channels

# create a channel object
channel = anywave.channel(label="Cz", type="EEG");

This example shows how to create a channel object in Python.
The channel object has the following methods and attributes:

channel.label = "A1";  # the label of the electrode
channel.ref = "A2";    # the electrode used as reference (a bipolar channel)
# ref is optional
channel.type = "EEG";  # Type of channel. Could be (EEG, SEEG, MEG, Trigger, Other, ICA, Source, ECG, EMG)
channel.lpf = 40;      # Sets a low pass filter for the channel (40Hz).
channel.hpf = 1;       # Sets a high pass filter for the channel (1Hz).
channel.notch = 50;    # Sets a notch filter for 50Hz
channel.data = numpy.zeros(1000); # Sets a vector of 1000 samples as the data for the channels.
channel.sr = 1000;     # Sets the sampling rate of data at 1000Hz.