mrkrd/matlab_wrapper

MatlabSession execption

Closed this issue · 5 comments

Exception TypeError: 'isinstance() arg 2 must be a class, type, or tuple of classes and types' in <bound method MatlabSession.__del__ of <MatlabSession:/usr/local/MATLAB/R2015a>> ignored
mrkrd commented

Sorry! Here is more information:

Python: 2.7.6
Matlab: 2015a
OS: Ubuntu 14.04

I'm evaluating a custom function stored in a .m file and capture its standard output. Unfortunately, I wasn't able to reproduce the error message with a small example. The code snippet below in principle recapitulates the actual code. I included it in the hope that it may still be helpful. Note that I'm running the code on a SLURM cluster with a network file system (NFS) and only get the error upon submission (using srun for example), but not when running it on the frontend node.

import matlab_wrapper as matlab
import tempfile
import os

filename = os.path.join(tempfile.gettempdir(), 'test_function.m')

with open(filename, 'w') as f:
    f.write("result = num2str(5)")

with open(filename, 'r') as f:
    function = f.read()
    print 'Evaluate the following expression: \n'
    print function
    print '\n'

try:
    engine = matlab.MatlabSession()
    engine.eval("out = evalc('{0}')".format(function))
    out = engine.get("out")
    print 'Output:'
    print out

finally:
    os.remove(filename)
mrkrd commented

I'm evaluating a custom function stored in a .m file and capture its
standard output. Unfortunately, I wasn't able to reproduce the error
message with a small example. The code snippet below in principle
recapitulates the actual code. I included it in the hope that it may
still be helpful.

Why do you save a MATLAB function in an m-file from Python? Is it
dynamically generated?

Is it really a function that you save in the m-file? Or a script like
in the simplified example below? (there's a difference between function
and script m-files in MATLAB)

Note that I'm running the code on a SLURM cluster with a network file
system (NFS) and only get the error upon submission (using srun for
example), but not when running it on the frontend node.

This will be hard for me to help directly, because I don't have such a
setup. If I understand correctly, you don't have the problem when
running your script locally without scheduler, right? In this case it
has something to do with the interaction between parallel environment,
MATLAB, and Python. But before you start some deep debugging, perhaps
it's possible to simplify the script and fix the issue. Please keep
reading…

import matlab_wrapper as matlab
import tempfile
import os

filename = os.path.join(tempfile.gettempdir(), 'test_function.m')

with open(filename, 'w') as f:
    f.write("result = num2str(5)")

with open(filename, 'r') as f:
    function = f.read()
    print 'Evaluate the following expression: \n'
    print function
    print '\n'

try:
    engine = matlab.MatlabSession()
    engine.eval("out = evalc('{0}')".format(function))
    out = engine.get("out")
    print 'Output:'
    print out

finally:
    os.remove(filename)

This looks quite complicated. I think you could just eval the string
that you first write to the m-file by doing something like that:

matlab = matlab_wrapper.MatlabSession()
matlab.eval("result = num2str(5)")
out = matlab.get("result")

It's just a suggestion, because I don't understand the bigger context of
the script.

eval(evalc()) looks very suspicious btw ;)

Let me know about your progress, further issues or questions.

And good luck! :)
Marek

PS there are some examples in the repository that you might find useful:

https://github.com/mrkrd/matlab_wrapper/tree/master/examples

Why do you save a MATLAB function in an m-file from Python? Is it
dynamically generated?

This was just done for illustration and testing. The m-file would normally already exist and would not be dynamically created from Python.

Is it really a function that you save in the m-file? Or a script like
in the simplified example below? (there's a difference between function
and script m-files in MATLAB)

I have Matlab function files, no scripts.

eval(evalc()) looks very suspicious btw ;)

I agree that this is quite hacky. It was the only way I was able to capture the standard output/error of the evaluated function, however. Any suggestions?

Time for some props: Your wrapper is in my opinion the best implementation out there. I've tested all. With its support for numpy arrays, it's in my opinion even better than the standard Python engine shipped with never Matlab versions.

In the meanwhile, I've successfully run several hundred thousands of jobs. Being able to provide additional startup options turned out to be essential. I still sometimes get the error message. It doesn't break anything, but it probably wouldn't hurt to investigate it further. Let me know if you need any additional information.

mrkrd commented

Is it really a function that you save in the m-file? Or a script
like in the simplified example below? (there's a difference between
function and script m-files in MATLAB)

I have Matlab function files, no scripts.

In this case, it's best/easiest to use matlab_wrapper as in the README
file:

https://github.com/mrkrd/matlab_wrapper#usage

eval(evalc()) looks very suspicious btw ;)

I agree that this is quite hacky. It was the only way I was able to
capture the standard output/error of the evaluated function,
however. Any suggestions?

Errors are captured by the matlab_wrapper and displayed as part of the
Python exception.

Standard output: if it's a part of the result, I would just return it as
a string from the function.

Time for some props: Your wrapper is in my opinion the best
implementation out there. I've tested all. With its support for numpy
arrays, it's in my opinion even better than the standard Python engine
shipped with never Matlab versions.

Thank you ☺

In the meanwhile, I've successfully run several hundred thousands of
jobs. Being able to provide additional startup options turned out to
be essential. I still sometimes get the error message. It doesn't
break anything, but it probably wouldn't hurt to investigate it
further. Let me know if you need any additional information.

At the moment I have almost no information or setup how to debug it.
To investigate it, I need a script that fails in some way. Otherwise I
have nothing to work on.