This page presents exemplary ways how to beneficially use the Python bindings of opals modules, partly in combination with other Python extensions. As a prerequisite, a basic understanding of the Python language and its standard library are necessary - otherwise reading Python in a minute is recommended. For instructions on how to debug Python code, see Debugging OPALS Python scripts.
Aiming at fulfilling the needs of different users, every opals module is provided with 3 bindings:
While executables do not depend on any third-party software and may be run instantly, shared libraries offer the possibility for a tight integration into other software, but require compilation. As a drawback, usage of Python modules requires the availability of a Python interpreter. However, OPALS comes with an internal Python installation, and users may enjoy the rich set of language features and the typed, in-memory communication provided by Python bindings in contrast to the limited options available with executables, and the ease of a high-level, interpreted language as opposed to the need for compilation when using shared libraries.
Python (named after the BBC show Monty Python's Flying Circus) features outstanding characteristics, among which the following may be most important:
This page aims at highlighting the benefits of using the Python bindings of OPALS modules. As a prerequisite, a one-minute-introduction to the Python language and its standard library are given in Python in a minute.
The Python bindings of OPALS modules reflect closely their C/C++ - counterparts: for each OPALS module, there exists a corresponding Python module which provides a Python class that derives from opals::IModuleBase's equivalent on the Python side ("opals.Base.Base"). Every such class comes with the respective module's specific
parameters, as well as with all common and global parameters (see Parameter Categories). Every module parameter can be accessed as a Python property of that name. While specific
parameters are accessible as properties of the class itself, common
and global
parameters are accessible as attributes of the attributes commons
or globals
, respectively.
Please note that the get_<parameterName>, set_<parameterName> and isSet_<parameterName> methods have been deprecated and removed in the builds following Sept. 25th, 2016.
All data types exposed by OPALS modules are mapped to a certain Python data type. Mostly, this mapping is clear: e.g. C/C++'s int
is mapped to Python's int
, opals::String
is mapped to Python's str
, and opals::List
is mapped to Python's list
. For each enumeration, a custom data type is exposed on the Python-side, with constants for each enumerator.
Not obvious is the mapping of C/C++ types that have no "natural" correspondence on the Python side. For some, but not all of these types, OPALS defines a custom data type on the Python side (e.g. opals::HistoStats), whose methods are thus accessible to the Python interpreter. C/C++'s std::pair
, opals::Vector
, and opals::Array
are all mapped to Python's list
. opals::Matrix
, respectively, is mapped to a Python list
of Python lists
(which is easily converted to a numpy.matrix). Those types, for whom no special Python equivalent is defined, are represented as str
in Python. Whether such a special type is defined, may be determined by usage of help
.
In the following, two examples are presented that closely follow the ones presented in the example-sections for modules Module Bounds and Module Histo. As extensions to these examples that are conducted with OPALS modules only, simple visualizations are created here, using third-party modules. Kindly note that all needed third-party modules are included in the OPALS Python AddOns.
In this example, Module Import and Module Bounds are used to determine a tight outline of the 3 data sets G111.las, G112.las, and G113.las found in the OPALS demo-directory. Using these outlines, a combined plot is created using the Python module matplotlib
, which provides basic 2D plotting functions. Package os
comes with Python and provides basic operating system functionalities. Its sub-module path
facilitates file path operations. As Module Bounds does not grant in-memory access to the resulting outline, but exports vector data files, these must be read in here. We choose the simple text-format xyz as export format, which stores the 3 coordinates of a point on each line, separated by white space. As Module Bounds exports a single, closed polyline, the topology is unambiguous. Module csv
serves for reading in the xyz-file. The final result looks like this:
In this example, Module Import, Module Grid, Module Diff, Module Algebra, and Module Histo are used to generate a histogram of differences in height within the overlapping region of the 2 data sets strip19.las, and strip20.las found in the OPALS demo-directory. These overlapping regions are further restricted to smooth areas surrounded by point samples in every direction. For further details, see the respective example for Module Histo. As the OPALS-type opals::HistoStats exposes its data members to Python, the results are directly accessible in-memory. In addition to plotting the histogram itself, we overlay the normal probability density distribution derived from the mean and standard deviation returned by Module Histo. The final result looks like this:
OPALS packages are found in $OPALS_ROOT/opals/workflows
.