Libraries¶
Algorithms are fundamental elements in the BEAT framework 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 concept of libraries has been defined. 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.
Note
Naming Convention
Libraries are named using three values joined by a /
(slash) operator:
username: indicates the author of the library
name: indicates the name of the library
version: indicates the version (integer starting from
1
) of the library
Each tuple of these three components defines a unique name inside the framework.
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
..note
The graphical interface of BEAT provides user-friendly editors to configure
the main components of the system (for example: libraries, data formats,
etc.), which simplifies their `JSON`_ declaration definition. One needs
only to declare a library using the described specifications when not using
this graphical interface.
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 full name, 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)