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 | ||||||
|
Classes | ||||||||||
|
Data | ||
__author__ = 'Takeshi Mizumoto' __date__ = '25 Oct. 2011' |
Author | ||
Takeshi Mizumoto |