Difference between revisions of "AnyWave:WritePythonScripted"

From WikiMEG
Jump to: navigation, search
(The Python module)
(getData(start, duration, , ))
Line 106: Line 106:
  
  
Examples:
+
Getting data as they are filtered by AnyWave:
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
 
#
 
#
Line 118: Line 118:
 
# Accessing first channel's data
 
# Accessing first channel's data
 
data = anywave.input_channels[0].data
 
data = anywave.input_channels[0].data
 +
</syntaxhighlight>
  
 +
Getting data with no filtering applied:
 +
<syntaxhighlight lang="python">
 +
#
 +
# The AnyWave python module is automatically imported, so we don't need to import it again
 +
#
 +
 +
# Get data and filter them as they are filtered in AnyWave.
 +
anywave.getData(0, -1, 'No Filtering')
 +
 +
# data are part of input_channels objects.
 +
# Accessing first channel's data
 +
data = anywave.input_channels[0].data
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 14:17, 30 July 2014

Introduction

This section targets people who have a good knowledge and practice of the Python programming language.
The purpose is to explain how to write a Python script that will be the heart of a plug-in executed by AnyWave.
We will also explain how to create a text file to describe our plug-in to AnyWave

The AnyWave-Python API (Application Programming Interface) consists in a Python module that is automatically imported in the Python environment by AnyWave.

Some packages are also embedded in AnyWave:

  • numpy
  • scipy
  • pyqtgraph
  • sklearn
  • nibabel
  • openGL
  • matplotlib

Where to start?

The first thing to do is to create the basic structure for a plug-in.
A Python Scripted plug-in is very simple, it is a folder containing at least two files:

  • desc.txt (a text file describing the plug-in)
  • __main__.py (the Python code)

Writing the desc.txt file

The file must be named desc.txt and may looks like:

name = Python plug-in
description = I am a Python test plugin
category = Process:Python:My Python Plugin

The category line is optional. It tells AnyWave where the plug-in will appear in the menus. Here we decided to caterogize it as a Process, with a sub category called Python. Finally, the plug-in name will be My Python Plugin.

The category feature is usefull to separate plug-in that won't really do some calculation but convert data to another format on launch external tools.

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 Numpy array format'
  • 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.

Copy the folder to the right location

The prefered location is the user's AnyWave directory.

Python location.png

See that the folder is placed in the Python subfolder. This will allow AnyWave to recognize it as a Python plug-in.

Check that the plug-in is available

Loaded plugin.png

The Python module

AnyWave will execute the Python plug-in in a separated Python environment which is embedded in the application.

A Python interpreter will execute the script '__main.py__' of our plug-in.

Before the script is being executed, AnyWave will do some initializing like importing a module named anywave in the Python's environment.

The module provides methods and attributes that will allow interactions between AnyWave and the Python script's environment.

Here is a brief descriptions of available attributes and methods: (parameters between <> are optional)

Attributes Short description
input_channels The list of input channels set for the plug-in by AnyWave.
data_path The full path to the data file. (Empty before calling getDataInfo or getData)
total_duration The total duration of data in seconds, value is 0 until a call to getData or getDataInfo is done.

input_channels

This attribute is a list of channel objects. A channel object contains the following attributes:

  • label - a string which is the electrode name
  • ref - a string with the name of the reference electrode. Can be empty if no reference is set.
  • data - a numpy array containing the signal.
Methods Short description
getData(start, duration, <keyword>, <filtering options> get data for input channels.
getDataInfo() get data information (fill the data_path and total_duration attributes)
getInputChannels() get information about input channels. Set the input_channels attibutes.
getMarkers(keywords) get markers.
addMarker(keywords) add a new marker.

getData(start, duration, <keyword>, <filtering options>)

This method will request data for input channels.

start must be the starting position in data, in seconds.

duration must be the duration of data in seconds or -1 for all the data after the starting position.

Optional parameters can be provided to ask for specific filtering options on data. If no optional parameter is provided, data will be filtered depending on the filtering options set in AnyWave.

keyword can be:

  • 'No Filtering' - means that we don't want AnyWave to apply any filter on data.
  • 'User Filtering Options' - means that we want to set the filtering options on data.


Getting data as they are filtered by AnyWave:

#
# The AnyWave python module is automatically imported, so we don't need to import it again
#
 
# Get data and filter them as they are filtered in AnyWave. 
anywave.getData(0, -1)
 
# data are part of input_channels objects.
# Accessing first channel's data
data = anywave.input_channels[0].data

Getting data with no filtering applied:

#
# The AnyWave python module is automatically imported, so we don't need to import it again
#
 
# Get data and filter them as they are filtered in AnyWave. 
anywave.getData(0, -1, 'No Filtering')
 
# data are part of input_channels objects.
# Accessing first channel's data
data = anywave.input_channels[0].data