Code Monkey home page Code Monkey logo

oct2py's Introduction

Hi ๐Ÿ‘‹, I'm Steve

Hi, I'm a Project Jupyter maintainer and Executive Council member. I am an Air Force Veteran and have been an open source maintainer since 2011. I was previously a core developer on the scikit-image and Spyder IDE teams.

blink1073

steve_silvester

  • ๐Ÿ”ญ Iโ€™m currently working on JupyterLab, the next-generation web-based user interface for Project Jupyter.

jupyterlab

  • ๐Ÿ‘ฏ Iโ€™m looking to collaborate on JupyterLab

  • ๐Ÿ˜„ Pronouns: he/him

  • ๐Ÿ“„ Know about my experiences on LinkedIn

  • โšก Fun fact I like reading everything by Stephen King, and lifting weights with my wife

blink1073

ย blink1073

blink1073

oct2py's People

Contributors

adityaapte avatar andrewbolster avatar ankostis avatar blink1073 avatar cel4 avatar dependabot[bot] avatar github-actions[bot] avatar jenshnielsen avatar jordigh avatar maostu avatar minrk avatar moorepants avatar nikolaz111 avatar noahfx avatar pooyaest avatar pre-commit-ci[bot] avatar shoyer avatar suever avatar takluyver avatar yasirroni avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

oct2py's Issues

Returning a data structure using oct2py in Python 2.7

Hello,

I am attempting to return a data structure using a custom function in Octave-4.0.0 and calling it in python.

In octave I wrote a simple function to return a structure:

function y = f(a,b)
y.a = a;
y.b = b;
endfunction

when I use the function in the octave command window the following happens as expected:

y = f(1,2)
y =

scalar structure containing the fields:

a =  1
b =  2

Next, I try to use the function in python. I have already imported octave in the current python session using ('from oct2py import Oct2Py; octave = Oct2Py(executable='/usr/bin/octave-cli-4.0.0')
When I try to attempt '>>> octave.f(1,2)' or '>>> y = octave.f(1,2)' the following error occurs:

Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python2.7/site-packages/oct2py/core.py", line 409, in octave_command
return self._call(name, _args, *_kwargs)
File "/usr/local/lib/python2.7/site-packages/oct2py/core.py", line 516, in _call
data = self.eval(cmd, temp_dir=temp_dir, **eval_kwargs)
File "/usr/local/lib/python2.7/site-packages/oct2py/core.py", line 297, in eval
data = self._reader.extract_file()
File "/usr/local/lib/python2.7/site-packages/oct2py/matread.py", line 88, in extract_file
data[key] = get_data(data[key])
File "/usr/local/lib/python2.7/site-packages/oct2py/matread.py", line 139, in get_data
for i in range(val.size)])
File "/usr/local/lib/python2.7/site-packages/oct2py/matread.py", line 99, in get_data
if "'|O" in str(val.dtype) or "O'" in str(val.dtype):
AttributeError: 'mat_struct' object has no attribute 'dtype'

Are data structures able to be returned in python using oct2py? If so how can I go about fixing this?

Thanks.

datatype of empty string returned is <u1

function x = return_empty_str()
x=''

this function when called by Oct2py returns:

array([], dtype='<U1')

while

function x = return_empty_str()
x='foo'

when called gives

u'foo'

Why don't we get u'' in the first case?

Oct2py version 3.1.0, python version 2.7

Oct2py hangs in Windows

Oct2py hangs in Windows after importing "octave" in either Python or IPython in Windows command line. Currently using Windows 7, Python 2.7.8, Anaconda 2.1.0, oct2py 2.4.0. It also hangs during the nosetests after cloning the repo.

oct2py c2d fails with "columns must match" error

Hi,
I'm trying to use octave's c2d function to transfer a continuous time control system to a discrete one.
This seems to work fine as long as A, B are the same size. The code below however (where B is 2x1) fails however informing me that the numbers of columns must match (in the temporary .mat file, I assume)

I'm using scipy 0.9.0, python 2.7 (both installed from the ubuntu repos) and oct2py-0.3.5-py2.7.egg that I installed via easy-install (and that was working fine for solving the discrete algebraic Riccati equation - helped me out big time!)

Code:
from oct2py import Oct2Py
oc = Oct2Py()
import scipy as sp

A = sp.array([[1.0, 1.0],[0,1.0]])
B = sp.array([ [1.0], [0.0] ])
C =sp.eye(2)

sys = oc.ss(A,B,C,[])
print sys
dsys = oc.call('c2d', sys,0.5)
print dsys

OUTPUT:
{'a': array([[ 1., 1.],
[ 0., 1.]]), 'c': array([[ 1., 0.],
[ 0., 1.]]), 'b': array([[ 1.],
[ 0.]]), 'd': array([[ 0.],
[ 0.]]), 'stname': [u'x_1', u'x_2'], 'tsam': 0.0, 'n': 2.0, 'sys': array([[ 2., 0., 0., 1.]]), 'nz': 0.0, 'outname': [u'y_1', u'y_2'], 'yd': array([[ 0., 0.]]), 'inname': u'u_1'}
Traceback (most recent call last):
File "/media/sf_Dropbox/Work_2012_07+/ConVerIterate/Python/oct2py_experiment/oct2py_c2d.py", line 21, in
dsys = oc.call('c2d', sys,0.5)
File "/usr/local/lib/python2.7/dist-packages/oct2py-0.3.5-py2.7.egg/oct2py/oct2py.py", line 211, in call
resp = self.eval(cmd, verbose=verbose)
File "/usr/local/lib/python2.7/dist-packages/oct2py-0.3.5-py2.7.egg/oct2py/oct2py.py", line 360, in eval
raise Oct2PyError(msg)
oct2py.utils.Oct2PyError: """
load /tmp/tmpuBWU8L.mat "A
" "B
"
[a
_] = c2d(A__, B__)
save "-v6" /tmp/tmpHwjPoP.mat "a__"
"""
number of columns must match (5 != 3)

M files fail if you don't have a docstring

It looks like if you don't provide a doc string in an m-file (comments right after the call signature), that the function call fails. If I remove the comment in roundtrip.m (from your example) I get this error when trying to use it:

In [1]: from oct2py import octave

In [2]: octave.roundtrip(1.0)
---------------------------------------------------------------------------
Oct2PyError                               Traceback (most recent call last)
<ipython-input-2-4350e71a22ee> in <module>()
----> 1 octave.roundtrip(1.0)

/usr/local/lib/python2.7/dist-packages/oct2py/session.pyc in __getattr__(self, attr)
    416         else:
    417             name = attr
--> 418         doc = self._get_doc(name)
    419         octave_command = self._make_octave_command(name, doc)
    420         #!!! attr, *not* name, because we might have python keyword name!

/usr/local/lib/python2.7/dist-packages/oct2py/session.pyc in _get_doc(self, name)
    393         except Oct2PyError:
    394             msg = '"{0}" is not a recognized octave command'.format(name)
--> 395             raise Oct2PyError(msg)
    396         return doc
    397 

Oct2PyError: "roundtrip" is not a recognized octave command

You raise an error if the _get_doc call returns this:

ipdb> doc = self._eval('help {0}'.format(name), log=False, verbose=False)
*** Oct2PyError: Oct2Py tried to run:
"""
help roundtrip
"""
Octave returned:
help: 'roundtrip' is not documented

Which seems to be raise on line 520 of session.py:

elif line == '\x15':

This shouldn't be raised on the failed octave help command when it doesn't return a docstring.

Also the current error message is misleading. This was really tricky to hunt down.

Octavemagic fails to deliver inline images in IPython (on Windows)

I have necessary programs that output correctly for example print -depsc or print -dsvg ... from within Octave.

IPython extension works fine except for drawing inline images as shown in their example notebook file

Problem seems to be backslashes in created temp file. For example if my temp folder is c:\temp octavemagic instructs octave/ghostscript to output file with this kind of path:

... -OutputFile=c: emp mplaf_rm/__ipy_oct_fig_001.png c:\temp\oct-35.eps

where obviously backslash follow by t is passed as tab.

DeprecationWarning, ResourceWarnings

Test suite of Oct2Py 1.3.0 triggers 1 DeprecationWarning and 2 ResourceWarnings (and 1 DeprecationWarning in IPython).
I use NumPy 1.8.0, SciPy 0.13.2, IPython 1.1.0.

/usr/lib64/python3.3/site-packages/IPython/core/completerlib.py:53: DeprecationWarning: imp.get_suffixes() is deprecated; use the constants defined on importlib.machinery instead
  r'|'.join(re.escape(s[0]) for s in imp.get_suffixes()))
.../tmp/oct2py-1.3.0/oct2py/ipython/octavemagic.py:348: ResourceWarning: unclosed file <_io.BufferedReader name='/tmp/tmp3kq68r/__ipy_oct_fig_001.png'>
  images = [open(imgfile, 'rb').read() for imgfile in \
/tmp/oct2py-1.3.0/oct2py/ipython/octavemagic.py:348: ResourceWarning: unclosed file <_io.BufferedReader name='/tmp/temp/tmp3kq68r/__ipy_oct_fig_002.png'>
  images = [open(imgfile, 'rb').read() for imgfile in \
.................................../tmp/oct2py-1.3.0/oct2py/tests/test_oct2py.py:455: DeprecationWarning: Implicitly casting between incompatible kinds. In a future numpy release, this will raise an error. Use casting="unsafe" if this is intentional.
  outgoing += np.random.rand(*size)
....................E........E

Capturing Multiple Returns

I have a function that looks like:

function [f, h1f, h2f, A, theta, psi] = ...
    monofilt(im, nscale, minWaveLength, mult, sigmaOnf, orientWrap)

Doing this:

ret = %octave monofilt(b, 3, 4, 2, 0.65) -i b

yields only the f return value. How do I capture the remaining values?

Too aggressively deleting tmpfiles

I am currently trying to run several oct2py-utilising processes in parallel and they start interfering because they delete each other's MAT files. I noticed that you're running a very aggressive cleanup method:

def _remove_temp_files(dirname):
    """
    Remove the created mat files in the user's temp folder
    """
    import os
    import glob

    for fname in glob.glob(os.path.join(dirname, 'tmp*.mat')):
        try:
            os.remove(fname)
        except OSError:  # pragma: no cover
            pass

Now without having dug deeper into the code to see what exactly is needed, may I suggest using a context manager like the following to create tempfiles?

class tempfile:
    """ Context for temporary file.
    Will find a free temporary filename upon entering
    and will try to delete the file on leaving

    """
    def __init__(self, suffix=''):
        self.suffix = suffix

    def __enter__(self):
        import tempfile as tmp
        self.handle, self.name = tmp.mkstemp(suffix=self.suffix)
        return self.name

    def __exit__(self, type, value, traceback):
        try:
            os.close(self.handle)
            os.remove(self.name)
        except OSError as e:
            if e.errno == 2:
                pass
            else:
                raise e

you would use it like

with tempfile(suffix=".mat") as matfile:
    oct2py.whatever(matfile)

and the context manager would clean up the file afterwards itself (even in the case of exceptions)

eval chokes on non-ascii character in help string

The minimal example m-file,
ftp://currents.soest.hawaii.edu/pub/outgoing/bad_character.m
causes oct2py.octave.eval (and therefor all attempts at access via oct2py) to fail. The culprit is a non-ascii character (used as an apostrophe) in the help string.
The traceback is not very helpful, but ends with a TypeError:

/home/efiring/envs/python3/lib/python3.4/site-packages/oct2py/core.py in evaluate(self, cmds, verbose, logger, log, timeout, pre_call, post_call)
    743             line = self.readline()
    744 
--> 745             if chr(3) in line:
    746                 break
    747 

TypeError: argument of type 'NoneType' is not iterable

because line is None. I suspect line is None because of some earlier encode/decode failure that was masked.

Not able to addpath.

I'm trying to use oct2py with some custom m-files but I haven't been able to get your m-file examples to work.

First I create `/tmp/roundtrip.m' and then open IPython and run your example:

In [1]: %doctest_magic
ERROR: Line magic function `%doctest_magic` not found.

In [2]: %doctest_mode
Exception reporting mode: Plain
Doctest mode is: ON
>>> %paste
>>> from oct2py import octave
>>> import numpy as np
>>> x = np.array([[1, 2], [3, 4]], dtype=float)
>>> out, oclass = octave.roundtrip(x)
>>> # or octave.call('roundtrip', x)
>>> # or octave.call('roundtrip.m', x)
>>> # or octave.call('/path/to/roundtrip.m', x)
>>> import pprint
>>> pprint.pprint([x, x.dtype, out, oclass, out.dtype])

## -- End pasted text --
Traceback (most recent call last):
  File "<ipython-input-3-310b52e8e762>", line 4, in <module>
    out, oclass = octave.roundtrip(x)
  File "/usr/local/lib/python2.7/dist-packages/oct2py/session.py", line 418, in __getattr__
    doc = self._get_doc(name)
  File "/usr/local/lib/python2.7/dist-packages/oct2py/session.py", line 395, in _get_doc
    raise Oct2PyError(msg)
Oct2PyError: "roundtrip" is not a recognized octave command

>>> oc
oct     octave  
>>> octave.addpath('/tmp/roundtrip.m')
u'.:/usr/lib/x86_64-linux-gnu/octave/3.6.4/site/oct/x86_64-pc-linux-gnu:/usr/lib/x86_64-linux-gnu/octave/site/oct/api-v48+/x86_64-pc-linux-gnu:/usr/lib/x86_64-linux-gnu/octave/site/oct/x86_64-pc-linux-gnu:/usr/share/octave/3.6.4/site/m:/usr/share/octave/site/api-v48+/m:/usr/share/octave/site/m:/usr/share/octave/site/m/startup:/usr/lib/x86_64-linux-gnu/octave/3.6.4/oct/x86_64-pc-linux-gnu:/usr/share/octave/3.6.4/m:/usr/share/octave/3.6.4/m/help:/usr/share/octave/3.6.4/m/image:/usr/share/octave/3.6.4/m/path:/usr/share/octave/3.6.4/m/polynomial:/usr/share/octave/3.6.4/m/statistics:/usr/share/octave/3.6.4/m/statistics/distributions:/usr/share/octave/3.6.4/m/statistics/models:/usr/share/octave/3.6.4/m/statistics/tests:/usr/share/octave/3.6.4/m/statistics/base:/usr/share/octave/3.6.4/m/plot:/usr/share/octave/3.6.4/m/geometry:/usr/share/octave/3.6.4/m/miscellaneous:/usr/share/octave/3.6.4/m/set:/usr/share/octave/3.6.4/m/strings:/usr/share/octave/3.6.4/m/sparse:/usr/share/octave/3.6.4/m/prefs:/usr/share/octave/3.6.4/m/general:/usr/share/octave/3.6.4/m/specfun:/usr/share/octave/3.6.4/m/special-matrix:/usr/share/octave/3.6.4/m/deprecated:/usr/share/octave/3.6.4/m/io:/usr/share/octave/3.6.4/m/pkg:/usr/share/octave/3.6.4/m/linear-algebra:/usr/share/octave/3.6.4/m/elfun:/usr/share/octave/3.6.4/m/optimization:/usr/share/octave/3.6.4/m/startup:/usr/share/octave/3.6.4/m/audio:/usr/share/octave/3.6.4/m/signal:/usr/share/octave/3.6.4/m/time:/usr/share/octave/3.6.4/m/testfun'
>>> %paste
>>> from oct2py import octave
>>> import numpy as np
>>> x = np.array([[1, 2], [3, 4]], dtype=float)
>>> out, oclass = octave.roundtrip(x)
>>> # or octave.call('roundtrip', x)
>>> # or octave.call('roundtrip.m', x)
>>> # or octave.call('/path/to/roundtrip.m', x)
>>> import pprint
>>> pprint.pprint([x, x.dtype, out, oclass, out.dtype])

## -- End pasted text --
Traceback (most recent call last):
  File "<ipython-input-5-310b52e8e762>", line 4, in <module>
    out, oclass = octave.roundtrip(x)
  File "/usr/local/lib/python2.7/dist-packages/oct2py/session.py", line 418, in __getattr__
    doc = self._get_doc(name)
  File "/usr/local/lib/python2.7/dist-packages/oct2py/session.py", line 395, in _get_doc
    raise Oct2PyError(msg)
Oct2PyError: "roundtrip" is not a recognized octave command

octave.addpath seems to return the path listing but it does not append (or prepend) the path I tried adding.

return cell from octave

>>> from oct2py import Oct2Py
>>> op = Oct2Py()
>>> op.run('x = {[0.4194 0.3629 -0.0000; 0.0376 0.3306 0.0000; 0 0 1.0000],\
...        [0.5645 -0.2903 0; 0.0699 0.1855 0.0000;0.8500 0.8250 1.0000]};')
>>> x = op.get('x')
>>> x
[array([ 0.4194,  0.3629, -0.    ]), array([ 0.5645, -0.2903,  0.    ])]
>>> print op.run('x')
x =
{
  [1,1] =

    0.41940  0.36290  -0.00000
    0.03760  0.33060  0.00000
    0.00000  0.00000  1.00000

  [1,2] =

    0.56450  -0.29030  0.00000
    0.06990  0.18550  0.00000
    0.85000  0.82500  1.00000

}

Is this datatype not supported or I did something wrong?

Octave 4 starts in GUI mode

>>> from oct2py import Oct2Py
>>> oct = Oct2Py()

This will start 2 Octave gui sessions...
Is there any option to call octave-cli instead?

Clarification to documentation: 1-D vectors in Python default passed as row vectors via oct2py

This is not a bug, but if I have a 1-D vector in Python, say
x.shape -> (256,)
this gets passed to octave with
size(x) -> (1,256)
This breaks functions where vector orientation is important.

Passing vectors from Python to Matlab requires explicitly defining the orientation by passing x as:
octave.myfunction(x[:,np.newaxis])
for a column vector

Looking further, this may be simply something that one "should know" about numpy vectors, and that to communicate with Matlab that has notions of array but not vectors, one needs to be explicit instead of implicit about the vector orientation. 1-D arrays in Matlab are by default row vectors, so the behavior of oct2py in passing vectors from Python to Octave isn't truly totally a surprise.

Errors and failures with Octave 3.8

I recently upgraded Octave from 3.6.4 to 3.8.1 and now test suite of oct2py 1.3.0 has about 28 more errors and 2 more failures. New errors and failures occur with all versions of Python.

Example error (all new errors have the same Octave error):

======================================================================
ERROR: Test the call command
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/oct2py-1.3.0/oct2py/tests/test_oct2py.py", line 567, in test_call
    out = octave.call('roundtrip.m', 1)
  File "/tmp/oct2py-1.3.0/oct2py/session.py", line 240, in call
    resp = self._eval(cmd, verbose=verbose, timeout=timeout)
  File "/tmp/oct2py-1.3.0/oct2py/session.py", line 395, in _eval
    timeout=timeout)
  File "/tmp/oct2py-1.3.0/oct2py/session.py", line 647, in evaluate
    raise Oct2PyError(msg)
Oct2PyError: Oct2Py tried to run:
"""
[a__] = roundtrip(A__)
"""
Octave returned:
'y' undefined near line 2 column 31

2 new failures:

======================================================================
FAIL: Testing help command
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/oct2py-1.3.0/oct2py/tests/test_oct2py.py", line 591, in test_help
    assert 'Compute the cosine for each element of X in radians.' in doc
AssertionError

======================================================================
FAIL: Test all of the types, originating in octave, and returning
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/oct2py-1.3.0/oct2py/tests/test_oct2py.py", line 286, in test_octave_origin
    assert ret == 'ans =  1'
AssertionError

----------------------------------------------------------------------

oct2py.tests.test_oct2py.test_plot(), oct2py.tests.test_oct2py.test_call_path() errors

oct2py.tests.test_oct2py.test_plot(), oct2py.tests.test_oct2py.test_call_path() fail with errors with each Python version (2.7, 3.1, 3.2, 3.3).
I use Octave 3.6.4, NumPy 1.8.0, SciPy 0.13.2, IPython 1.1.0, Pexpect 3.0.

======================================================================
ERROR: oct2py.tests.test_oct2py.test_plot
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib64/python3.3/site-packages/nose/case.py", line 198, in runTest
    self.test(*self.arg)
  File "/tmp/oct2py-1.3.0/oct2py/tests/test_oct2py.py", line 751, in test_plot
    octave.plot([1, 2, 3])
  File "/tmp/oct2py-1.3.0/oct2py/session.py", line 410, in octave_command
    return self.call(name, *args, **kwargs)
  File "/tmp/oct2py-1.3.0/oct2py/session.py", line 240, in call
    resp = self._eval(cmd, verbose=verbose, timeout=timeout)
  File "/tmp/oct2py-1.3.0/oct2py/session.py", line 395, in _eval
    timeout=timeout)
  File "/tmp/oct2py-1.3.0/oct2py/session.py", line 650, in evaluate
    self.interact(line)
  File "/tmp/oct2py-1.3.0/oct2py/session.py", line 755, in interact
    inp = inp_func() + '\n'
EOFError: EOF when reading a line

======================================================================
ERROR: oct2py.tests.test_oct2py.test_call_path
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib64/python3.3/site-packages/nose/case.py", line 198, in runTest
    self.test(*self.arg)
  File "/tmp/oct2py-1.3.0/oct2py/tests/test_oct2py.py", line 826, in test_call_path
    octave.addpath(os.path.dirname(__file__))
  File "/tmp/oct2py-1.3.0/oct2py/session.py", line 410, in octave_command
    return self.call(name, *args, **kwargs)
  File "/tmp/oct2py-1.3.0/oct2py/session.py", line 240, in call
    resp = self._eval(cmd, verbose=verbose, timeout=timeout)
  File "/tmp/oct2py-1.3.0/oct2py/session.py", line 395, in _eval
    timeout=timeout)
  File "/tmp/oct2py-1.3.0/oct2py/session.py", line 650, in evaluate
    self.interact(line)
  File "/tmp/oct2py-1.3.0/oct2py/session.py", line 755, in interact
    inp = inp_func() + '\n'
EOFError: EOF when reading a line

----------------------------------------------------------------------
Ran 68 tests in 1015.829s

FAILED (errors=2)

Octave process do not exit after Oct2Py().exit()

Hello

First of all, thank you very much for your effort in connecting Octave with Python! This has already helped me a lot.

But I think, there might be a bug in Oct2Py. On my computer the Octave processes are not ended, when the exit-method of the respective Oct2Py instance is called. Instead the Octave processes are marked as "" when calling "ps -e" and live until the Python interpreter is closed.

I'm using oct2py 2.4.0 (from pip) and Octave 3.8.2 (from apt) on a Debian Testing/Unstable installation.

I have written a small script that shows the described behaviour (at least on my computer). The output of the os.system calls is in the comments:

import os

print "Octave instances before importing oct2py:"
os.system("ps -e | grep octave")    # [No output]
print

import oct2py
print "Octave instances after importing oct2py:"
os.system("ps -e | grep octave")    # 10756 pts/1    00:00:00 octave-cli
print

oct2py.octave.exit()
print "Octave instances after exiting the convenience Octave instance:"
os.system("ps -e | grep octave")    # 10756 pts/1    00:00:00 octave-cli <defunct>
print

octave = oct2py.Oct2Py()
print "Octave instances after starting a new instance:"
os.system("ps -e | grep octave")    # 10764 pts/1    00:00:00 octave-cli
print

octave.exit()
print "Octave instances after exiting the new instance:"
os.system("ps -e | grep octave")    # 10764 pts/1    00:00:00 octave-cli <defunct>
print

for i in range(5):
    with oct2py.Oct2Py() as octave:
        pass
print "Octave instances after starting and exiting a few instances:"
os.system("ps -e | grep octave")    # 10780 pts/1    00:00:00 octave <defunct>

There are three things that I find remarkable in the script's output (The first one is the actual issue):

  • Instead of exiting, the octave-cli and octave processes are becoming instead of being terminated.
  • When a new Oct2Py instance is created, these -processes are garbage collected.
  • Using Oct2Py with the "with" statement creates an "octave" process instead of an "octave-cli" process.

I have found no way of terminating the octave processes, that works for me.There is the oct2py.kill_octave() function, but I cannot use that, because I use oct2py in a library and I don't know if there are other Oct2Py instances that shall not be killed yet. Furthermore, this function recreates the oct2py.octave instance, which is one of the instances that I'm trying to terminate.

Thank you very much!
Jonas

Redirecting output to logger

I was wondering if I could redirect the output of Octave functions when using verbose=True to a logger and I saw the get_log function, but I am not sure if this fits my needs.

It seems that verbosity

https://github.com/blink1073/oct2py/blob/master/oct2py/_oct2py.py#L364

always just prints the line to the default standard out. Maybe the Oct2Py objects should log Octave's output and other messages into a log that could be changed when instantiating the class:

>>> octave = Oct2Py()  # Uses default logger, stdout
>>> custom_logger = [...]  # Custom logger, i.e. file logger, custom formatting, etc
>>> octave = Oct2Py(logger=custom_logger)

and instead of using the verboseargument on calls, control the level of the logger. What do you think? Is it feasible? I would provide a patch but I have no much time available myself, I could give it a try in no less than three weeks.

oct2py hangs when called via nosetests or py.tests on python 2.7

Hi,

I have a weird testing framework to compare the results of a python library with a MatlabTM library. The tests runs just fine on Python 3, but hangs on Python 2. Here is a gist of the code:

rootpath = os.path.dirname(__file__)
octave = Oct2Py(timeout=3)  # Hangs here
path = os.path.join(rootpath, 'matlab_library')
_ = octave.addpath(octave.genpath(path))

Note that everything works fine for Python2 when called outside nose or pytest.

Here is the full travis log:

https://travis-ci.org/pyoceans/python-seawater/builds/69282280

Oct2py Libmmd.dll and libifcoremd.dll issues with PythonXY

Hello,

The oct2py initializer init.py looks for dlls no longer existing in the numpy package directory in Python(x,y)-2.7.6.1. These dlls are libmmd.dll and libifcoremd.dll.

47     basepath = imp.find_module('numpy')[1]
---> 48     lib1 = ctypes.CDLL(os.path.join(basepath, 'core', 'libmmd.dll'))
49     lib2 = ctypes.CDLL(os.path.join(basepath, 'core', 'libifcoremd.dll'))

I resolved by copying dlls from [Python27 root]/DLLs to [numpy package root]/core but oct2py should no longer look for these dlls in the above location.

Tests failing in Python 3.4 on Ubuntu 12.04

When I run

python3.4 `which iptest3` --all -j

from a Snake Charmer VM, I get:

======================================================================
ERROR: IPython.extensions.tests.test_octavemagic.test_octave_roundtrip
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/nose/case.py", line 198, in runTest
    self.test(*self.arg)
  File "/usr/local/lib/python3.4/dist-packages/IPython/extensions/tests/test_octavemagic.py", line 33, in test_octave_roundtrip
    ip.run_line_magic('octave_push', 'x y')
  File "/usr/local/lib/python3.4/dist-packages/IPython/core/interactiveshell.py", line 2126, in run_line_magic
    result = fn(*args,**kwargs)
  File "<string>", line 2, in octave_push
  File "/usr/local/lib/python3.4/dist-packages/IPython/core/magic.py", line 193, in <lambda>
    call = lambda f, *a, **k: f(*a, **k)
  File "/usr/local/lib/python3.4/dist-packages/IPython/extensions/octavemagic.py", line 145, in octave_push
    self._oct.put(input, self.shell.user_ns[input])
  File "/usr/local/lib/python3.4/dist-packages/oct2py/session.py", line 289, in put
    self._eval(load_line, verbose=verbose, timeout=timeout)
  File "/usr/local/lib/python3.4/dist-packages/oct2py/session.py", line 395, in _eval
    timeout=timeout)
  File "/usr/local/lib/python3.4/dist-packages/oct2py/session.py", line 647, in evaluate
    raise Oct2PyError(msg)
oct2py.utils.Oct2PyError: Oct2Py tried to run:
"""
load /tmp/tmprl5sx8op.mat "y"
"""
Octave returned:
load: unable to find file /tmp/tmprl5sx8op.mat

======================================================================
ERROR: IPython.extensions.tests.test_octavemagic.test_octave_cell_magic
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/nose/case.py", line 198, in runTest
    self.test(*self.arg)
  File "/usr/local/lib/python3.4/dist-packages/IPython/extensions/tests/test_octavemagic.py", line 44, in test_octave_cell_magic
    'z = x + y;')
  File "/usr/local/lib/python3.4/dist-packages/IPython/core/interactiveshell.py", line 2162, in run_cell_magic
    result = fn(magic_arg_s, cell)
  File "<string>", line 2, in octave
  File "/usr/local/lib/python3.4/dist-packages/IPython/core/magic.py", line 193, in <lambda>
    call = lambda f, *a, **k: f(*a, **k)
  File "/usr/local/lib/python3.4/dist-packages/IPython/extensions/octavemagic.py", line 270, in octave
    self._oct.put(input, val)
  File "/usr/local/lib/python3.4/dist-packages/oct2py/session.py", line 289, in put
    self._eval(load_line, verbose=verbose, timeout=timeout)
  File "/usr/local/lib/python3.4/dist-packages/oct2py/session.py", line 395, in _eval
    timeout=timeout)
  File "/usr/local/lib/python3.4/dist-packages/oct2py/session.py", line 647, in evaluate
    raise Oct2PyError(msg)
oct2py.utils.Oct2PyError: Oct2Py tried to run:
"""
load /tmp/tmp03y794ri.mat "y"
"""
Octave returned:
load: unable to find file /tmp/tmp03y794ri.mat

----------------------------------------------------------------------

Steps:

Clone Snake Charmer: https://github.com/andrewclegg/snake-charmer.git

Checkout this revision (so as to avoid any confounding factors): 7d5e90e8b8816b0a28b037cf3094cdf4f24bf19f

Start up a VM: vagrant up

... wait about an hour ...

Log in to new VM:

vagrant ssh

Run tests:

python3.4 `which iptest3` --all -j

When I kick off the tests from within an IPython Notebook, this doesn't happen. Should it just be skipping these tests when you're not in a notebook?

keyboard command leaves user hanging

I was wrapping an m-file with oct2py that would raise an error under a rare situation. The author of this file left a keyboard call in the code for debugging purposes. When you type keyboard in Octave you get a debug> prompt, but calling the function with oct2py just seems like it hangs. Not sure if you can propogate the debug> prompt or at least give the user a warning. Took me while to figure this out as the keyboard call was deep in the m file.

Consider marking tests that depend on a gui.

I think it would make sense to mark tests that spawn a gui with a gui label to make it easy to disable those with nose.

This would only require the attr decorator from nose for each of those tests: nose.plugins.attrib.attr:

from nose.plugins.attrib import attr

@attr('gui')
def test_plot(self):
     pass

Excluding all those tests labelled with gui would be as easy as running nosetests -a '!gui'

I can prepare a PR if you think that's a good idea.

Using keyword arguments causes parameter names used in calling

If I write:

octave.monofilt(a, nscale=3, minWaveLength=4, mult=2, sigmaOnf=0.65)

The generated M code looks like:

load /tmp/tmpw3BX7L.mat "A__"
monofilt(A__, "sigmaOnf", 0.65, "minWaveLength", 4, "mult", 2, "nscale", 3)

which is not desired. Ideally if keyword arguments are not supported, an exception should be raised to the user.

The correct Python is to drop the keyword arguments and use positional arguments:

octave.monofilt(a, 3, 4, 2, 0.65)

leading to:

monofilt(A__,  3, 4, 2, 0.65 )

octavemagic and addpath

How do I use the octavemagic in IPython Notebook along with addpath?

This works:

from oct2py import octave
octave.addpath('/home/ubuntu/Downloads/')
octave.monofilt(a, 3, 4, 2, 0.65)

but then this:

%octave monofilt(a) -i a

leads to:

---------------------------------------------------------------------------
OctaveMagicError                          Traceback (most recent call last)
<ipython-input-12-2814f62667cf> in <module>()
----> 1 get_ipython().magic(u'octave monofilt(a) -i a')

/usr/local/lib/python2.7/dist-packages/IPython/core/interactiveshell.pyc in magic(self, arg_s)
   2203         magic_name, _, magic_arg_s = arg_s.partition(' ')
   2204         magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
-> 2205         return self.run_line_magic(magic_name, magic_arg_s)
   2206 
   2207     #-------------------------------------------------------------------------

/usr/local/lib/python2.7/dist-packages/IPython/core/interactiveshell.pyc in run_line_magic(self, magic_name, line)
   2124                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2125             with self.builtin_trap:
-> 2126                 result = fn(*args,**kwargs)
   2127             return result
   2128 

/usr/local/lib/python2.7/dist-packages/oct2py/ipython/octavemagic.pyc in octave(self, line, cell, local_ns)

/usr/local/lib/python2.7/dist-packages/IPython/core/magic.pyc in <lambda>(f, *a, **k)
    191     # but it's overkill for just that one bit of state.
    192     def magic_deco(arg):
--> 193         call = lambda f, *a, **k: f(*a, **k)
    194 
    195         if callable(arg):

/usr/local/lib/python2.7/dist-packages/oct2py/ipython/octavemagic.pyc in octave(self, line, cell, local_ns)
    312             msg = re.sub('"""\s+', '"""\n', msg)
    313             msg = re.sub('\s+"""', '\n"""', msg)
--> 314             raise OctaveMagicError(msg)
    315 
    316         key = 'OctaveMagic.Octave'

OctaveMagicError: Oct2Py tried to run:
"""
monofilt(a)
"""
Octave returned:
'monofilt' undefined near line 3 column 13

FAIL: Testing help command

======================================================================
FAIL: Testing help command
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/var/tmp/portage/dev-python/oct2py-3.3.3/work/oct2py-3.3.3/oct2py/tests/test_usage.py", line 66, in test_help
    assert 'Compute the cosine for each element of X in radians.' in doc
AssertionError

----------------------------------------------------------------------

octpy-3.3.3

octavemagic license

Hi,
As I told you before I am packaging oct2py to get it into Debian distribution.
Reviewing the licences one of my sponsors noticed that octavemagic is licensed under the New BSD license, this license requires that the licence is actually present verbatim and that the licence holders are credited.

So I've made a pull request which includes the license #57

Thanks for writing such incredible software :)

Cheers!

ipython notebook using oct2py.ipython extension hangs

this bug was already reported in some earlier version, see http://stackoverflow.com/questions/24999313/octave-hangs-in-ipython
it appeared now again to me with the new oct2py version 2.1

When I use ipython 2.2, octave 3.8, oct2py 2.1 on my Ubuntu 14.04 (everything well linked) simple tests like

%load_ext oct2py.ipython
x = %octave [1 2; 3 4];
x

hang in ipython notebook. The kernel is busy and nothing happens

I uninstall oct2py and installed version oct2py version 1.6 like it was said to work in the above stackoverflow-link, and indeed everything works fine now (of course still not in oct2py 2.1).

oct2py imports octave but hangs when calling an octave function.

Hi,

I am using Linux CentOS6.6 Operating System and use Python 2.6 when trying to communicate with octave. I use Python2.6 because when I installed oct2py ('pip install oct2py') the package installed in the same path as Python2.6. As for octave, I am running octave 4.0.0 which is also in the same path as python2.6 (both in '/usr/bin') (I believe that means they are in the same path, I am not sure).

I can open python2.6 in terminal and type in 'from oct2py import octave'... When doing this python2.6 continues and moves to the next line displaying '>>>' though the octave gui will open in a separate window (I am unsure if this should be the case)... The next step I take is simply try to do an octave command such as ('>>> octave.ones(3)') and what happens next is python2.6 goes to the next line not displaying '>>>' and seams to hang (nothing happens next).. I have to exit terminal or use 'ctrl+z' to stop and exit python2.6

Do you have any suggestions as to why this is happening? I cannot seem to figure out why I can't carry out octave commands.

Thank you for the help.

_get_doc fails when doc contains non-ascii characters

This is because in here

https://github.com/blink1073/oct2py/blob/master/oct2py/_oct2py.py#L407

oct2py tries to eval type {name}, but as the doc might have non-ascii characters , here

https://github.com/blink1073/oct2py/blob/master/oct2py/_oct2py.py#L350

the error comes. I haven't gone deeper in the code logic so I have to ask: is there any particular reason to use 'ascii' in the decoding? Probably changing this to utf-8 or detecting the charset of the file would be the shortest solution but I am not sure if it breaks anything else.

OSX maverick issue

Hi,
I'm using the oct2py in OSX Maverick, and I don't know how to fix this issue:

import oct2py

ImportError: dlopen(/Users/usuario/src/scipy/scipy/special/_ufuncs.so, 2): Library not loaded: /usr/local/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1/libgfortran.3.dylib
Referenced from: /Users/usuario/src/scipy/scipy/special/_ufuncs.so
Reason: image not found

I downloaded octave: brew install octave
GNU Octave, version 3.8.2

and pip installed oct2py

Thanks in advance
Mabel

IPython notebook hangs

Running the provided sample causes my entire machine (not just the example) to hang when using the subplot examples.

Machine info:


$ ipython --version
2.3.0
$ pip show oct2py

---
Name: oct2py
Version: 2.4.0
Location: /usr/local/lib/python2.7/dist-packages
Requires: 
$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION="Ubuntu 14.04.1 LTS"
$ octave --version
GNU Octave, version 3.8.1
...

Feature request: /run/shm instead of /tmp for .mat file passing

Feature request:
for those of us using oct2py as a core piece of code, could we have the option to use /run/shm (RAM) instead of /tmp (HDD). This may substantially speed up a wide class of uses of oct2py with files big and small where seek or read/write speed is a bottleneck.

I'm sure it's in the source code somewhere, but making the directory used to pass .mat files an end-user parameter would be nice for potential speedups of oct2py.

Are .m functions cached?

In an IPython notebook, I was testing a local function (local .m file) with the octavemagic extension. (the octavemagic extension uses oct2py)

I noticed that after editing the .m file, the results from IPython did not change until I restarted the kernel (restarts the python process).

Note that if you are in the octave terminal and call a function, the changes are present immediately.

This can become annoying if you are editing/debugging an octave .m file, having to restart the process each time there is a change.

I posted a bug at ipython, but they said they don't do anything on their side to cache functions, so I'm posting this here.

Patch for hiding popup consoles on Windows

Hi,

I didn't noticed Windows mentioned as tested platform, but it seems to work just fine. Only issue is that every Octave object spawns console window. This patch fixes it:

--- _utils.bak
+++ _utils.py
@@ -35,10 +35,19 @@

     """
     try:
-        session = subprocess.Popen(['octave', '-q', '--braindead'],
-                                 stderr=subprocess.STDOUT,
-                                 stdin=subprocess.PIPE,
-                                 stdout=subprocess.PIPE)
+        if sys.platform == "win32":
+            startupinfo = subprocess.STARTUPINFO()
+            startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
+            session = subprocess.Popen(['octave', '-q', '--braindead'],
+                                      stderr=subprocess.STDOUT,
+                                      stdin=subprocess.PIPE,
+                                      stdout=subprocess.PIPE,
+                                      startupinfo=startupinfo)
+        else:
+            session = subprocess.Popen(['octave', '-q', '--braindead'],
+                                       stderr=subprocess.STDOUT,
+                                       stdin=subprocess.PIPE,
+                                       stdout=subprocess.PIPE)
     except OSError:
         msg = ('\n\nPlease install GNU Octave and put it in your path\n')
         raise Oct2PyError(msg)

calling octave asynchronously

from oct2py import Oct2Py
oct1 = Oct2Py()
oct2 = Oct2Py()
ret = oct1.eval('pause(10)')

This will block the process for 10 seconds. Is there any way to call it asynchronously and check later ret.done() if oct1 is done? This is how the official matlab python engine works.

Octave getting killed in an improper way?

Somewhere between 2046b6a and ecc2880 a new bug emerged:

The tests run fine, but it looks like as if octave gets killed in an improper way after that.

I get a huge non-python stacktrace (which I'm not able to catch at the moment :( )

This is among the many lines:

*** Error in `octave': double free or corruption (fasttop): 0x0000000002770cb0 ***

Sometimes I also get a python error:

......................................................................
----------------------------------------------------------------------
Ran 70 tests in 55.682s

OK
Exception in thread Thread-1 (most likely raised during interpreter shutdown):Exception in thread Thread-2 (most likely raised during interpreter shutdown):

 Traceback (most recent call last):
 Traceback (most recent call last):  File "/usr/lib64/python2.7/threading.py", line 810, in  __bootstrap_inner

  File "/usr/lib64/python2.7/threading.py", line 763, in run
  File "/home/ch/repositories/oct2py/oct2py/session.py", line 516, in read_incoming  File "/usr/lib64/python2.7/threading.py", line 810, in __bootstrap_inner
  File "/usr/lib64/python2.7/encodings/utf_8.py", line 16, in decode

<type 'exceptions.AttributeError'>: 'NoneType' object has no attribute 'utf_8_decode'  File "/usr/lib64/python2.7/threading.py", line 763, in run

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.