PYTHON

Purpose:

Executes a command in Python using ActiveX.

Syntax:

PYTHON("command")

"command"

-

A string. The expression in Python syntax to evaluate.

Returns:

A scalar, series, or string, the result of the command.

Example:

python("2.5**3 // 2")

 

returns 7, the value of raising 2.5 to the third power and performing integer division by 2. The syntax is exactly as if the command was entered into a native Python command prompt.

Example:

python("import numpy as np");

python("np.random.rand(4, 3)");

 

returns a 4x3 matrix of random values.

Example:

python("rdata = np.random.rand(4, 3)");

W1: python("np.cumsum(rdata, 0)");

W2: cumsum(python("rdata"));

W3: W1 - W2

 

Creates a 4x3 matrix of random values in the Python workspace named rdata. W1 executes the Python CUMSUM function located in the NumPy module and contains the column based cumulative sum of rdata.

 

W2 computes the column based cumulative sum of the Python variable rdata using the native CUMSUM function.

 

W3 computes the difference between W1 and W2. The result is all zeros indicating the series are equal.

Remarks:

The PYTHON function attempts to connect to the running instance of the Python automation server, otherwise PYTHON starts and connects to a new instance of Python. A copy of Python 2.7 or higher must be available on the host machine.

 

Not all Python commands return meaningful results.

 

Numeric data is transferred as double precision values.

 

PyLink

 

The built-in PyLink Module provides a simpler and perhaps more natural "dot" syntax for running Python commands and functions. For example

 

python("import numpy as np");

python("numpy.rand(10, 3)");

 

can be invoked as:

 

py.numpy.rand(10, 3)

 

This syntax has the advantage of automatically and transparently passing data to and from Python. For example:

 

W1: gnorm(1000, 1)

W2: py.numpy.diff(w1)

 

W1 contains 1000 samples of random data. W2 processes the data in W1 with the Python diff function. When W1 changes, W2 automatically updates and the new data is processed by Python.

 

The Python diff function executes just like any other function except:

 

1.

The required data is sent to Python

2.

Python modules contained in the expression are imported.

3.

Python executes the diff function on the data

4.

The result is returned to W2

 

 

Mixing Expressions

 

Python functions can be mixed with standard functions. For example:

 

W1: gnorm(1000, 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 MOVAVG function.

 

 

Data Exchange

 

Although generally not required, PyLink supports explicit data exchange between DADiSP and Python variables. For example:

 

 

// assign a series to a Python variable
py.PyVar = 1..100;
  
// return a Python variable
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:


pydiff = py.numpy.diff

 

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)



Multiple Return Values

 

Multiple return values from Python are supported:

 

W1: rand(10)

W2: (d, v) = py.scipy.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

 

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.

 
Additional Features

 

Error messages or warnings produced by Python are displayed in DADiSP.

Functions that produce lists, dictionaries or other objects that are not assigned to a variable are presented in a pop-up dialog box. For example, to display a dictionary of current Python global objects:

py.globals()

 

The pyglobals function displays the same information

 

The pyhelp function displays Python online documentation. For example:

 

pyhelp py.scipy.linalg.eig

 

The standard help information for the Python eig function is displayed.

 

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.

See Also:

CREATEOBJECT

EVAL

EVALTOSTR

EXECUTE

FEVAL

MATLAB