Libraries

Algorithms are fundamental elements in the platform that formally describe how to process data. In particular, they are always attached to a specific processing block with a given set of inputs and outputs. When an algorithm needs to be applied in a slightly different processing block, this may, hence, lead to a lot of code duplication. Duplicate code is undesirable for a number of reasons such as high maintenance cost.

To address this problem, the platform defines the concept of libraries. Libraries allow users to put code required by several different algorithms into a common location. Once done, code from a library may be used by any algorithm as long as the algorithm mentions its dependency to it in its JSON declaration. In addition, a library may depend on another library.

Definition

Similarly to algorithms, a library consists of two folds:

  • A JSON declaration indicating:
    • The language in which the library is written
    • Library dependencies of this library
{
    "uses": {
        "otherlib": "user/otherlibrary/1"
    },
    "language": "python"
}
  • Source code. For the Python back-end, this may consist of any Python function and classes, as long as dependencies are fulfilled.
def simple_function(array):
    return len([v for v in array if v != 0])

class MyLibraryClass:

    def __init__(self, multiplier=37):
        self.multiplier = multiplier

    def function_from_my_library(value):
        return value * self.multiplier

The web client of the BEAT platform provides a graphical editor for algorithm, which simplifies its JSON declaration definition. It also includes a simple Python code editor.

Usage

To use a defined library in an algorithm or in another library, it is sufficient to:

  • Add the library dependency into the JSON declaration of the algorithm (or of the library). The name given as a key is the one used to import the library, while the corresponding value is the fullname, that is author/name/version of the library.
{
    ...
    "uses": {
        "mylib": "user/mylibrary/1"
    },
    ...
}
  • Import the library and use its desired functionalities.
import mylib
...
array = [0, 1, 2, 3]
array_processed = mylib.simple_function(array)