My first Jython task

What is a HIPE task?

A HIPE task is everything you see in the Task view of HIPE. Choose any entry in this view and double click on it.

HIPE tasks can be written in Jython and Java. Jython is the language you use to write scripts in the Editor view of HIPE. Java is the language most of HIPE itself is written in.

A simple HIPE task

The following is a simple task that takes a Table Dataset as input and outputs an array with the averages of each column of the Table Dataset.

class TableAverageTask(JTask):  # 1
   'Averages the columns of a Table Dataset.'
   def __init__(self, name="tableAverage"):
       JTask.__init__(self, name)
       self.setDescription("Computes the average of each column of a Table Dataset.")  # 2
       p = TaskParameter("table", valueType = TableDataset, mandatory = 1)  # 3
       p.description = "The table of whose columns to compute the average"
       self.addTaskParameter(p)
       p = TaskParameter("result", valueType = Double1d, type = OUT)  # 4
       p.description = "The array of averages of the table's columns"
       self.addTaskParameter(p)
   def execute(self):  # 5
       columns = self.table.columnCount
       divider = 1.0 / columns
       self.result = Double1d(table.rowCount)
       for column in range(columns):
          self.result.add(table.getColumn(column).data)
       self.result = self.result.multiply(divider)

This is the task itself, but to use it in HIPE there are two more steps you need to take:

  1. You must instantiate the task. That is, you must create an object from the TableAverageTask class. If this sounds obscure to you, have a look at the [...] section of the Scripting and Data Mining guide.
  2. You must then register the task instance, so that HIPE is aware of the task and can put it in the right places in the Tasks view.

These additional steps are accomplished by the following lines. Note that these lines are not indented and not part of the TableAverageTask class.

TODO: Add code

  1. Note that from the task name TableAverageTask we obtained tableAverage as instance name. This is a general rule. The task name is made of words with capitalised initials and ends with Task. The instance name starts with a lower case letter and drops the Task part.
  2. Here we register the task in the GENERAL and SPECTRUM categories. This means that the task will appear in the General and Spectrum folders in the Tasks view, in addition to the All folder.

Turning the task into a plugin

Now that your task is complete, you can turn it into a HIPE plug-in, so that you and your colleagues can easily install and uninstall it from HIPE.

To turn the tableAverage task into a plugin, follow these steps:

  1. Copy the source code into a file called plugin.py.
  2. Add the plugin.py file to a zip file. The zip file name must be made by three parts:
    • The plug-in name, for example TableAveragePlugin.
    • An underscore character.
    • The plug-in version number, which can be any combination of digits separated by dots. For example, 0.1, 1.0.0, 2.3.3 and so on.

The plug-in is now ready. Read on to find out how to install it

Installing the plug-in

To install the plug-in, follow these steps:

  1. In HIPE, choose Tools --> Plug-ins. The _Plug-ins_dialogue box opens.
  2. Click the Install new button. The Install new plug-in dialogue box opens.
  3. Since this is a local file, click the folder button and navigate to the zip file you just created. Click the Open button.
  4. Restart HIPE to complete the installation.

Now you should see the tableAverage task in three places within the Tasks view: in the All, General and Spectrum folders.

Executing the plug-in

Double click on any of the tableAverage entries in the Tasks view. A dialogue box opens in the Editor view. Although we did not write a single line related to graphical interfaces in our TableAverageTask class, the task framework made one available for free.

If you hover the mouse pointer on the table entry in the Inputs panel, you will see a tooltip informing you that the parameter is mandatory and must be a TableDataset. You will also see the short description we added in the source code. The same happens with the result parameter in the Outputs panel.

Create a variable by issuing the following command in the Console view:

notValid = Double1d([3.0, 5.0, 8.2])

This is not a good input for the task. Try dragging the notValid entry in the Variables view to the small grey circle next to the table entry in the task dialogue box. This is how you can set parameters via point and click, but in this case the small circle becomes red. In the source code we specified that the input must be a TableDataset, so HIPE is rejecting everything else. We could have specified even more stringent conditions (for example, that the TableDataset must not be empty).

Issue these commands to create a valid TableDataset:

TODO: put commands.

Sharing the plug-in

To share the plug-in with your colleagues, you can either send them directly the zip file, or put it on a Web server, so that they can install it directly by pointing HIPE to the file URL. For example, you can install the little task described in this tutorial by choosing Tools --> Plug-ins_in HIPE, clicking the _Install new button and entering the following in the Enter URL for plug-in field: TODO: put URL.

Edit | Attach | Watch | Print version | History: r11 | r4 < r3 < r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions...
Topic revision: r2 - 2011-09-12 - DavideRizzo
 
This site is powered by the TWiki collaboration platform Powered by Perl