harkbasenode (25 Oct. 2011)
index
/home/mizumoto/src/hark/hark-python/harkbasenode.py

Hark Base Node is a base class for PyCodeExecutor in hark-python.
 
* Introduction
You can take advantage of Python modules for HARK. 
For example, 
you can plot using pylab or matplotlib as you want, 
you can write a small test code without having any source codes.
 
 
 
* How to use
To implement your python code for PyCodeExecutor, 
you need to do following things.
  (1) define a class that inherits HarkBaseNode
      The default class name is HarkNode. 
      (you can specify any name from the parameer of PyCodeExecutor.)
  (2) in __init__ method, 
        set the tuple self.outputNames to define the output terminal names, and 
        set the tuple self.outputTypes to define the output terminal types.
  (3) in calculate method,
        get input values from instance variables,
        implement your code in calculate method, and
        set your calculation results to the dictionary self.outputValues.
Detailed descriptions are written in each method.
 
 
 
* Sample code and network
This is the simplest sample.
other sample scripts and network files are in example/ directory.
run the shell script run_examples.sh to run them.
 
** Network
   Add the PyCodeExecutor node,
   add input1, input2, sum, and sqrt by hand.
                               |--------------|
       [Constant: int: 5] -----|input1     sum|---[Print]=OUTPUT
       [Cnostant: float: 2.5]--|input2    sqrt|---[Print]=OUTPUT_1
                               |--------------|
 
**Code
import harkbasenode  # This is the class defined here.
import numpy         # for calculate square root.
 
class HarkNode(harkbasenode.HarkBaseNode):   # inherit
    def __init__(self):
        # This node has two outputs: 
        # sum of primitive float, and sqrt of primitive complex.
        self.outputNames = ("sum", "sqrt")
        self.outputTypes = ("prim_float", "prim_complex)
 
    def calculate(self):
        # for each frame, these two values are calculated.
        self.outputValues["sum"] = self.input1 + self.input2
        self.outputValues["sqrt"] = numpy.sqrt(self.output1 * self.output2)
 
 
 
* Advanced tips
(1) Additional instance variables
You have some system variables such as 
  self.count
     The current number of frame. 
     You can "plot for each 50 frames" using this value.
  self.nodeName
     A node's name which is unique in the network, e.g., node_PyCodeExecutor_1.
  self.nodeID
     A node's number which is unique in the network, 
     e.g., nodeID = 1 if the node name is node_PyCodeExecutor_1.
     You can plot in different figure using:
         pylab.figure(self.nodeID)

 
Modules
       
exceptions
numpy

 
Classes
       
HarkBaseNode

 
class HarkBaseNode
    A base class for PyCodeExecutor in hark-python.
 
  Methods defined here:
__init__(self)
In this constructor, you need to specify output names and types as tuples.
e.g., 
    self.outputNames = ("output1", "output2")
    self.outputTypes = ("prim_float", "matrix_complex")
 
You can use [a-zA-Z0-9_] for names, and 
you can use [prim|vector|matrix]_[int|float|complex] for types.
If you specified wrong type, TypeError exception will be raised.
calculate(self)
For each iteration in flowdesigner, this function is called.
 
[Getting input]
Before the call, PyCodeExecutor gives the input values as instance variables.
e.g., 
Suppose tht your PyCodeExecutor node is like this:
         |---------------|
     ----|fft      output|---
     ----|integer        |
         |---------------|
Then, you can get the fft and input as:
   self.fft
   self.integer
to know all the inputs, use the following code:
   print dir()
 
 
[Setting output]
Set the output values to the dictionary self.outputValues
Its keys are self.outputNames defined in __init__.
convertValues(self)
Internal method.
You do not need run this method. This will automatically called in PyCodeExecutor.
This method converts a python-friendly types into C++-friendly types, 
such as from numpy.array to vector<int>.
makeup(self)
Internal method. 
You do not need run this method. This will automatically called in PyCodeExecutor.
This builds the dictionary to give the output values to flowdesigner, 
and checks type strings.

 
Data
        __author__ = 'Takeshi Mizumoto'
__date__ = '25 Oct. 2011'

 
Author
        Takeshi Mizumoto