DADiSP / PyLink 2.6
Python Code Execution Module
PyLink runs Python programs directly from DADiSP and exchanges
data between DADiSP and Python. Both built-in and
custom Python functions or scripts are executed just as if they were
native DADiSP functions.
Arrays, scalar and string data are exchanged transparently in fast binary
format using the
ActiveX Automation interfaces of DADiSP and Python.
How Does It Work?
The key to the simplicity and power of PyLink resides in the
Python Object created by DADiSP. The Python Object supports the
execution of any Python expression from the Python
environment and returns the results to DADiSP via ActiveX.
Because the code is still run in Python, the results are
exactly the same as would normally be produced by Python.
PY Dot Syntax
The default name of the Python Object is
py. Simply
preface the original Python function name with
py. to
run any Python code from DADiSP. For example, the following DADiSP
command displays the current version of the available Python
release:
The returned version text is displayed by DADiSP. Because the connection
to Python is automatically established when the Python Object is
first invoked, explicit initialization is not required. In this case, the
Python
sys module is automatically imported and the
version
property is retrieved as a string.
Mix and Match
DADiSP and Python functions can be combined and used
interchangeably within a Worksheet or SPL functions. For example:
W1: gnorm(10000, 1)
W2: py.numpy.diff(W1) |
W1 contains 1000 samples of Gaussian distributed random noise. The
data is then processed by the Python
diff function located in the numpy module and
the result is displayed in W2. When W1 changes, W2 automatically updates and
the new data is processed by Python.
The Python
diff function executes just like any other DADiSP
function except
- DADiSP sends the required data to Python.
- Python modules contained in the expression are imported.
- Python executes the function.
- The results are returned to DADiSP.
The entire process is perfomed automatically, transparently and
efficiently.
DADiSP and Python functions can be freely mixed. For example:
W1: gnorm(10000, 1)
W2: movavg(py.numpy.diff(w1), 10) |
The Python
diff function is applied to the data in W1 and the
result is smoothed by means of a 10 point moving average computed by the
DADiSP
movavg function. Any combination of DADiSP and Python functions
can be combined in this manner.
Data Exchange
Although generally not required, PyLink supports explicit
data exchange between DADiSP and Python variables. For example:
// assign a DADiSP series to a Python variable
py.PyVar = 1..100;
// return a Python variable to DADiSP and process
x = 10 * py.PyVar; |
The Python variable
PyVar is created and assigned a series
of values from 1 to 100. The DADiSP variable
x contains the
value of the Python variable multiplied by 10. Though required
data transfers are automatically handled by the Python Object, the
"dot syntax" provides a natural method for exchanging program variables
when desired.
Function Aliases
A Python function can be assigned a more convenient
name. For example:
The
pydiff object can now be used in place of the more verbose
py.numpy.diff syntax.
W1: grand(1000, 1)
W2: py.numpy.diff(w1)
W3: pydiff(w1) |
The result from
pydiff in W2 and
py.numpy.diff
in W3 are identical.
Importing Python Modules
As shown above, modules directly referenced by the
py
object are automatically imported. Additionally, the
pyimport function explictly
imports Python modules. For example:
pyimport("numpy", "np")
W1: gnorm(100, 1)
W2: py.numpy.diff(w1)
W3: py.np.diff(w1) |
The NumPy library is explicitly imported as np. The expressions
py.numpy.diff(w1) and py.np.diff(w1)
are now equivalent.
The pyimport function can also be used in "statement" form
when manually entered on the command line.
pyimport numpy as np
W1: gnorm(100, 1)
W3: py.np.diff(w1) |
The NumPy library is again explicitly imported as np. The functional
form of pyimport should be used in SPL routines, but
the statement form might be more familiar for manual command line interaction.
The pyimportfrom function implements the Python from import statement:
from module import name as alias |
For example:
pyimportfrom("scipy", "signal")
W1: gnorm(100, 1)
W2: gnorm(100, 1)
W3: py.signal.convolve(w1, w2) |
Use pyfrom for a more natural statement form of pyimportfrom
pyfrom scipy import signal
W1: gnorm(100, 1)
W2: gnorm(100, 1)
W3: py.signal.convolve(w1, w2) |
The Python Function
The
Python function
accepts a Python expression as a string. For example:
W1: python("range(10, 100, 2)")
W2: integ(W1) |
creates a 45 point series in W1 using the Python
range function
and integrates the series in W2.
Additional Features
The
pyhelp function displays the original help text for
Python functions. For example:
pyhelp py.scipy.linalg.eig |
The standard help information for the Python eig function is
displayed in DADiSP.
The pysource function displays the source file for
Python functions, modules or classes. For example:
pysource py.scipy.linalg.eig |
The source for the eig function is
displayed.
Python functions that return multiple values are supported.
W1: rand(10)
W2: (d, v) = py.linalg.eig(W1);v |
The Python eig function returns two arrays. The second array, a
matrix of eigenvectors, is placed and displayed in W2.
Named arguments for Python functions are supported. The argument name and
values are specified as strings.
W1: gnorm(1000, 1, 0, 0.5)
W2: {py.numpy.std(w1, "ddof=1")}
W3: {std(w1)} |
W2 and W3 contain the sample standard deviation of W1. The Python
std function accepts an optional argument named ddof
that determines the normalization of the result. In this case, ddof
is set to 1, producing the same result as the DADiSP std function.
Error messages or warnings produced by Python are displayed in
DADiSP.
Functions that produce lists, dictionaries or other objects
not assigned to a variable are presented in
a pop-up dialog box. For example, to display a dictionary of current
Python global objects:
The
pyglobals function displays the same information.
Requirements
Pylink requires DADiSP 6.9 Build 01 or higher. Contact us for information about updating
your current version of DADiSP.
PyLink requires Python Version 2.7 or higher to be available from
the machine running DADiSP. The following
Python modules are also required:
- comtypes 1.1.7 or higher
- ctypes 1.1.0 or higher
- numpy 1.19.2 or higher
- pywin32 300 or higher
The SciPy module is recommended.
See
PyDADiSP to run DADiSP code directly in Python.