Top Diff List Farm Source Search Help RSS Login

HARK-Python

Softwares > HARK-Python

HARK-Python


  What is HARK-Python?

HARK-Python is a glue that combines HARK and Python using Boost.Python.

HARK-python provides two functions:

  1. data visualization nodes using matplotlib, a powerful visualization module for python
  2. a wrapper to develop a HARK node using python.


  Benefits of HARK-Python

Rich visualization

The visualization of HARK basic packages is not enough. Only DisplayLocalization for visualizing localization results is provided.
HARK-Python package provides visualization modules for

  • waveform(plotWaveform),
  • spectrogram (plotSpec), and
  • localization(plotSource).

Quick development of a new node.


When you use PyCodeExecutor node, you can just write your code.

1. write your code like this:

import harkpython.harkbasenode as harkbasenode
class HarkNode (harkbasenode.HarkBaseNode):
    def __init__(self):
        # define the output names and types of your node as tuples here.
    def calculate(self):
        # write your code here.

2. then, build a network that uses your code.


  Installation

  1. add HARK repository and install Basic HARK Packages. see HARK installation instructions for details.
  2. Install HARK-Python
sudo apt-get install hark-python


  Tutorial

Tutorial 1: Run examples

1. run

/usr/share/doc/hark-python/run_examples.sh

2. Then, you will see the instruction like this:

Localization
/usr/share/doc/hark-python/examples/plotSourceNetwork.n /usr/share/doc/hark-python/examples/plotSourceExample.xml

Spectrogram
/usr/share/doc/hark-python/examples/plotSpecNetwork.n /usr/share/doc/hark-python/examples/longsample.wav

Waveform
/usr/share/doc/hark-python/examples/plotWaveformNetwork.n /usr/share/doc/hark-python/examples/shortsample.wav

3. To run these instructions, cut and paste the command line.


Tutorial 2: Implement your node with python

Goal: calculate input1^3 + input2^5

1. Put two Constants and one PyCodeExecutor

2. Set parameters

    1. node_Constant_1:VALUE: int 3
    2. node_Constant_2:VALUE: int 5
    3. node_PyCodeExecutor_1:DIRECTORY_NAME: /usr/share/hark-designer/userdata/networks

(It's set in DIRECTORY_NAME that a path of the network file you save.)

3. Set terminals to node_PyCodeExecutor_1

    1. Add Input: input1
    2. Add Input: input2
    3. Add Output: output (and Set as Output)

4. Connect terminals

    1. node_Constant_1:VALUE to node_PyCodeExecutor:input1
    2. node_Constant_2:VALUE to node_PyCodeExecutor:input2

5. Save as samplecode.n

6. write a code. save it as samplecode.py at the same path as the network file.

import harkpython.harkbasenode as harkbasenode

class HarkNode(harkbasenode.HarkBaseNode):
    def __init__(self):
        self.outputNames=("output",)  # one output terminal named "output"
        self.outputTypes=("prim_float",)  # the type is primitive float.
 
    def calculate(self):
        self.outputValues["output"] = self.input1 ** 3 + self.input2 ** 5
       # set output value
       # from two inputs: input1 and input2.

7. When click Execute button, you'll see "Status: Normally finished. see more"
Click see more link and see the complete log will be shown the following.

<Float 3152 >

  Advanced tips

Detailed description of harkbasenode.

see /usr/share/doc/hark-python/harkbasenode.html or for details.
or
in python interpreter.

import harkpython.harkbasenode
help harkpython.harkbasenode

Other examples

see /usr/lib/python2.7/dist-packages/harkpython

Support types

  • Primitives:
    • int, float, complex<float>, and ObjectRef (= Source)
  • Containers:
    • Vector<T> [T = int, float, complex<float>, and Source]
    • Matrix<T> [T = int, float, complex<float>]
    • Map<int, T> [T = Vector<int>, Vector<float>, Vector<complex<float> >, Float, Matrix<int>, Matrix<float>, Matrix<complex<float> >]

Instance variables

  1. self.nodeName: the name of the node. e.g., node_PyCodeExecutor_1.
  2. self.nodeID: the ID of the node. if the node name is node_PyCodeExecutor_1, the ID is 1.
  3. self.count: the number of iteration.

If you want to plot the data for every 100 frames,

if self.count % 100 == 0
    pylab.plot(data)
    pylab.draw()

Parameters of PyCodeExecutor and how they are used.

  1. DIRECTORY_NAME --> This path is added to python path.
  2. FILENAME (no extensions)
  3. CLASSNAME

Equivalent code

import sys
sys.path.append(DIRECTORY_NAME)
import FILENAME
object = FILENAME.CLASSNAME()

# For each iteration, calculate() is called
object.calculate()