54.3 Using External Python and Importing Python Libraries
Python is a widely used, high-level programming language which is very rich. TreeAge Pro has basic Python functionality built into the software. There are many additional Python Libraries available which allow your calculations to support high-level mathematical functions by using pre-built expressions. Using these libraries will extend the expressions and functionality of Python in TreeAge Pro.
To take advantage of these additional complex functions, it is necessary to install additional Python libraries. For example NumPy and Sci Py support high-level mathematical functions for operating multi-dimensional arrays.
For more details on NumPy and SciPy see the websites: http://www.numpy.org/ and https://www.scipy.org/ Installation of Python and Python libaries can be tricky. TreeAge Software cannot support the installation of Python, Numpy, SciPy, py4j or any other external Python modules. Please use Python resources at www.python.org for support. |
When any additional Python libraries are installed, it is necessary to install the py4j library. This library allows communication between treeage and the externally installed libraries to work.
To install the py4j library, enter the following command into a command prompt:
python -m pip install --user py4j
We will use the Special Features tutorial example model, "Python Example – ExternalNumPy.trex" to demonstrate how to enable external python and then use the external libraries. This model uses a NumPy function for multiplication. Clearly, NumPy is not required for multiplication, but if this model runs, you will know you have a successful connection to external Python, including the NumPy library.
When you open a model with Python script, you will be prompted "Do you want to enable python scripts?". You should select "yes".
If you select "No" and later need to enable Python scripts, you will need to save and close the model, re-open and then select enable Python scripts as above.
Once you have selected to enable python scripts you will directed to Application Preferences. If you are using External Libraries - such as NumPy - then select "Use external Python installation".
To run the model Python Example – ExternalNumPy.trex we need to make sure the steps above are followed which are:
-
Set Application Preferences to use external Python.
-
External Python is installed including the communication package: py4j.
-
The numpy package is installed.
The figure below shows a simple variable definition using the external python library numpy.
The function PythonImportNumpy reads in 4 arguments from the model and then does two things:
-
It imports the external Python library numpy and gives it the name np.
-
It uses one of the functions in numpy, product, to calculate the product of the 4 arguments and send them back to TreeAge Pro via the return command.
Rolling back the model will give you the product of the four arguments in the function. In the model, that is: 1 x 2 x 3 x 4 = 24.
If this model runs, you will be able to use other special functions within NumPy and probably also SciPy within your own models.