PyDADiSP 1.3
Calling DADiSP from Python
PyDADiSP is a Python module that enables Python to call DADiSP functions
and exchange data. Numeric NumPy arrays, scalars and strings are
supported in fast binary format using the COM automation interfaces
of Python and DADiSP.
How Does It Work?
The key to the simplicity and power of PyDADiSP resides in the DADiSP
class created by Python. The DADiSP class supports the execution of
any DADiSP expression from the DADiSP environment and returns the
results to Python via COM. Because the code is still run in
DADiSP, the results are exactly the same as would normally be produced
by DADiSP.
Getting Started
To begin the connection to DADiSP, first set the path to the
pydadisp module:
>>> import sys
>>> dsphome = "C:\\Program Files\\dsp69"
>>> sys.path.append(dsphome+"\\Modules\\pylink\\pyfiles\\pydadisp") |
where the variable dsphome is the path to your DADiSP installation.
Next, import the pydadisp module and create an instance of the PyDADiSP class.
>>> import pydadisp
>>> dp = pydadisp.PyDADiSP() |
Python starts DADiSP silently in the background. Now invoke any DADiSP function using the
familiar "dot" syntax. For example:
returns the version of the connected DADiSP as a string.
Input parameters are supplied in the normal fashion:
>>> a = dp.randn(1000, 1) |
creates a 1000 sample series of normally distributed random values
and returns the series as a NumPy array to the Python variable
a.
Data Exchange
Use the dot notation to push data to DADiSP and pull data from DADiSP.
>>> import numpy as np
>>> a = np.array([1, 2, 3, 4, 5])
>>> dp.a = a * a
>>> b = dp.a
>>> b
array([1., 4., 9., 16., 25.]) |
Mixing Expressions
DADiSP and Python functions can be combined and used
interchangeably. For example:
>>> import numpy as np
>>> b = np.linspace(1, 5, 5)
>>> c = dp.cumsum(b)
>>> b
array([1., 2., 3., 4., 5.])
>>> c
array([1., 3., 6., 10., 15.]) |
The variable b contains the values [1, 2, 3, 4, 5]. The cumulative sum
of the data is computed by the DADiSP cumsum function and the result is
assigned to the Python variable c as a NumPy array containing the values
[1, 3, 6, 10, 15].
The DADiSP cumsum function executes just like any other Python
function except:
- Python sends the required data to DADiSP.
- DADiSP executes the function.
- The results are returned to Python.
The entire process is performed automatically, transparently and
efficiently.
By default, single column DADiSP arrays are treated as 1D Python
NumPy arrays.
>>> dp.linspace(1, 5, 5)
array([1., 2., 3., 4., 5.]) |
To use single column Numpy arrays:
>>> dp = pydadisp.PyDADiSP(array_1d=False)
>>> dp.linspace(1, 5, 5)
array([[1.],
[2.],
[3.],
[4.],
[5.]]) |
Evaluate or Execute a DADiSP Expression
The
dadisp function executes a DADiSP expression and returns the result if any.
>>> dp.dadisp("reverse(1..5)")
array([5., 4., 3., 2., 1.]) |
The dadisp function can be used to setup a Worksheet for evaluation.
>>> dp.newworksheet(4, 0)
>>> dp.dadisp("W1 := 1..5")
>>> dp.dadisp("W2 := w1 * w1")
>>> dp.dadisp("W3 := cumsum(w2)")
>>> dp.w3
array([1., 5., 14., 30., 55.])
>>> # change W1
>>> dp.dadisp("W1 := {2, 3, 4}")
>>> # result of propagation
>>> dp.w3
array([4., 13., 29.])
>>> # change W1 again with numpy array
>>> import numpy as np
>>> dp.w1 = np.array([10, 11, 12])
>>> # result of propagation
>>> dp.w3
array([100., 221., 365.]) |
Handling Complex Arrays
Complex scalars and arrays are fully supported. For example:
>>> a = np.linspace(3, 7, 5)
>>> a
array([3., 4., 5., 6., 7.])
>>> b = dp.fft(a)
>>> b
array([25. +0.j , -2.5+3.4409548j , -2.5+0.81229924j,
-2.5-0.81229924j, -2.5-3.4409548j ])
>>> c = np.real(dp.ifft(b))
>>> c
array([3., 4., 5., 6., 7.]) |
Multiple Return Values
Multiple return values from a DADiSP function are handled naturally.
>>> x = np.array([5, 1, 3, 2])
>>> # return sorted values and indices
>>> y, idx = dp.sort(x, "ascend")
>>> y
array([1., 2., 3., 5.])
>>> idx
array([2., 4., 3., 1.])
>>> # convert idx to integer array
>>> idx = np.asarray(idx, dtype='int')
>>> # index original values, Python first index starts at 0
>>> x[idx-1]
array([1, 2, 3, 5]) |
Set DADiSP Visibility
PyDADiSP normally runs DADiSP silently, however DADiSP can be made visible to allow full access
to the DADiSP user interface.
>>> # make DADiSP visible
>>> dp.setvisible(True)
>>> # hide DADiSP
>>> dp.setvisbile(False)
>>> # get DADiSP visibility
>>> v = dp.setvisible() |
DADiSP Progress Messages
DADiSP progress messages for long computations are echoed back to Python if the
progresstext function is enabled.
>>> # display DADiSP progress messages
>>> dp.progresstext(True)
>>> # suppress progress messages
>>> dp.progresstext(False)
>>> # get progress message state
>>> p = dp.progresstext() |
Error Handling
Errors that occur in DADiSP raise the
DADiSPError exception.
>>> # results in an error in the DADiSP workspace
>>> dp.fft(100)
...
DADiSPError: fft: Need Series |
Interrupt a Long Running DADiSP Computation
Press and hold the
Ctrl+Break key to interrupt a long running DADiSP computation.
Restart the DADiSP Server
Use
restart to restart the DADiSP server and reset to the initial state.
Requirements
PyDADiSP requires DADiSP 6.9 Build 01 or higher.
PyDADiSP requires Python Version 3.8 or higher. 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
See Also
PyLink - Calling Python from DADiSP