Saturday, June 5, 2010

How to launch and terminate Matlab from Python

The recommended way to automate Matlab is to use COM API. However, there are situations where COM is not desired. In this case, the following Python script will launch Matlab, then find the process ID to provide the ability to terminate the process if needed. The reason for this is because occasionally when Matlab is launched from the command line, it shells a new process which is independent of the launching shell. Therefore, to monitor and terminate the Matlab process (say if a script goes awry), the Matlab process and its ID need to be found in the windows process list. Once the process ID is known, it can be treated as any other process by Python.

import wmi
c = wmi.WMI()
import os

os.popen('matlab')

print ‘Matlab launched’
raw_input('Press Enter to continue...')
print ‘Looking for Matlab in process list’
for process in c.Win32_Process(name='matlab.exe'):
    print process.ProcessId, process.Name
    found = True
    if found:
        raw_input('Press Enter to terminate Matlab...')
        process.Terminate()
    else:
        print '!!! Matlab not found...'

 

This code was tested with Matlab 2007b and Python 2.5.1 with PyWin32 and wmi on windows XP.

Thursday, May 20, 2010

Linear Time Invariant System – Representations and conversions between.


image


Using this chart, you can see how to convert among linear system representations. It shows how to convert …

Once you see how these representations are all interchangeable, it is easy to see how to reuse the different tools, which are usually specific to a given representation.

The continuous-time models and equations for conversion are shown below.


Linear Time Invariant Systems Poster V05 - Continuous Time Only

 


This entry answers the following questions (and more…):

  • How to convert an s-domain model into a differential equation.
  • How to convert a transfer function into a differential equation.
  • How to convert an impulse response into a differential equation.
  • How to convert an impulse response into a frequency response.
  • How to convert an impulse response into a transfer function.
  • …..


All text and images are release under the Creative Commons Attribution 3.0 License as long as this blog and the author (Ed Tate) are attributed.

Sunday, April 25, 2010

PythonXY 2.6.2 + Panda3d 1.7.0 + JModelica 1.1b1 – How to get to work together

If Panda3D 1.7.0 is added to Python using a ‘pth’ file, then JModelica 1.1b1 will load the wrong ‘mscvrt.dll’ and fail to initialize. This problem occurs:

In [1]: import jmodelica.examples.cstr as cstr
C:\Python26\lib\site-packages\jpype\_pykeywords.py:18: DeprecationWarning: the s
ets module is deprecated
  import sets
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)

C:\JModelica.org-1.1b1\work\<ipython console> in <module>()

C:\JModelica.org-1.1b1\Python\jmodelica\examples\cstr.py in <module>()
      9 from jmodelica.initialization.ipopt import NLPInitialization
     10 from jmodelica.initialization.ipopt import InitializationOptimizer
---> 11 from jmodelica.simulation.sundials import TrajectoryLinearInterpolation
     12 from jmodelica.simulation.sundials import SundialsDAESimulator
     13 from jmodelica.optimization import ipopt

C:\JModelica.org-1.1b1\Python\jmodelica\simulation\sundials.py in <module>()
     14
     15 try:
---> 16     from pysundials import cvodes
     17     from pysundials import ida
     18     from pysundials import nvecserial

C:\Python26\lib\site-packages\pysundials\cvodes.py in <module>()
     38
     39 import ctypes
---> 40 import sundials_core
     41 import nvecserial
     42

C:\Python26\lib\site-packages\pysundials\sundials_core.py in <module>()
     87 f.close()
     88
---> 89 libc = loadlib("c")
     90 try:
     91         libc.fdopen.argtypes = [ctypes.c_int, ctypes.c_int]

C:\Python26\lib\site-packages\pysundials\sundials_core.py in loadlib(libname)
     63                 lib = ctypes.CDLL(libpaths[libname])
     64         except OSError, e:
---> 65                 raise OSError("%s\nCannot load shared library %s. Please
check you config file and ensure the paths to the shared libraries are correct.
"%(e, libpaths[libname]))
     66         return lib
     67

OSError: [Error 1114] A dynamic link library (DLL) initialization routine failed

Cannot load shared library C:\Panda3D-1.7.0\bin\msvcrt.dll. Please check you con
fig file and ensure the paths to the shared libraries are correct.

The occurs in pysundials because the util.find_library('msvcrt') in the ctypes module is attempting to load the ‘msvcrt.dll’ which is installed with Panda3D. This problem can be fixed by forcing pysundials to load a compatible msvcrt.dll file, rather than the most recent. This is done by modifying pysundials_core.py to point to the correct dll. The following change starting at line 42 will force a specific dll to be loaded. The path and dll to use will be specific to each system.

if os.name == "nt":
    #clib = util.find_library('msvcrt')
    clib = 'C:\\Program Files (x86)\\Java\\jdk1.6.0_17\\jre\\bin\\msvcrt.dll'
else:
    clib = util.find_library('c')

An interesting tool - Pyspread

This is an interesting project: Pyspread. It is still a little rough, but shows a lot of promise as a way to use Python. Basically it is a spreadsheet built in Python and using Python to define cell behavior. I’ve installed it under PythonXY and it appears to work. A little slow, but not bad for a beta.

Alternatives to a prototyping tools that make python easier to use are:

In general some alternative environments to prototype behavior include:

Wednesday, April 21, 2010

How to get Panda3D to work with PythonXY

This note applies to Panda3d ver 1.7.0 and PythonXY ver 2.6.2 on a windows system.

  1. Install PythonXY.
  2. Install Panda3D, however do NOT set it as the default python interpreter.
  3. Add a ‘pth’ file to the python site-packages directory. I created a file named panda.pth and saved it at “C:\Python26\Lib\site-packages”. The file had the following contents:

C:/Panda3D-1.7.0
C:/Panda3D-1.7.0/bin

To check that the installation works, use an example from the Panda3D manual.

from direct.showbase.ShowBase import ShowBase
class MyApp(ShowBase):
	def __init__(self):
		ShowBase.__init__(self)
app = MyApp()
app.run()