Skip to main content

Python to GNU Octave bridge --> run m-files from python.

Project description

oct2py - Python to GNU Octave bridge.

Overview

Uses Octave to run commands and m-files. Run:

>>> import oct2py
>>> oct2py.demo()

for a live demo of features. Supports any Octave function or m-file, passing the data seamlessly between Python and Octave using MAT files. If you want to run legacy m-files, do not have MATLAB®, and do not fully trust a code translator, this is your library.

IPython Notebook Integration

Oct2py is used by IPython to enable %octavemagic, to include inline plotting. See example here.

New in Version 1.0.0

  • Support for Python3.3

  • Support for logging (e.g. oc = Oct2Py(logger=my_logger))

  • Oct2Py can be used as a context manger (with Oct2Py() as oc:)

  • Support for unicode characters

  • Improved support for cell array and sparse matrices

  • Bug fix: Changes to user m. files were not updated during a session

  • Bug fix: Removed popup console window on Windows

  • Thanks to @stefanv, @jenshnielsen, @idella, @klonuo, @Juanlu001, @bj0, @graingert, @jordigh, @wannesm, and Konstantin Markov for helping to get this out of beta.

Installation

You must have GNU Octave installed and in your PATH. On Windows, the easiest way to get Octave is to use an installer from sourceforge. On Linux, it should be available from your package manager. Additionally, you must have the numpy and scipy libraries installed, then run:

python setup.py install

or:

pip install oct2py

or:

easy_install oct2py

Note for Windows users: You may have to follow these instructions in order to use inline figures in IPython (or specify -f svg).

Datatypes

All Octave variable types are mapped to comparable Python types. See Oct2Py Data Conversions in the documentation for a full list. Wherever possible, value and type are preserved on a roundtrip to Octave. For example, if you send an ndarray of type np.int8, Octave receives an int8 matrix, and the value returned would be the original array, of type np.int8. Almost all Python types can be sent to Octave (including ndarrays of arbitrary rank) and read back in the same form. Note that dictionaries are mapped to Octave structures, which are returned as Struct objects. These objects behave just like an Octave struct, but can be accessed as a dictionary as well:

>>> from oct2py import Struct
>>> a = Struct()
>>> a.b = 'spam'  # a["b"] == 'spam'
>>> a.c.d = 'eggs'  # a.c["d"] == 'eggs'
>>> print a
{'c': {'d': 'eggs'}, 'b': 'spam'}

Performance

There is a penalty for passing data via MAT files. Running speed_test.py shows the effect. After a startup time for the Octave engine (<1s typically), raw function calls take almost no penalty. The penalty for reading and writing from the MAT file is around 1-2ms on my laptop. This penalty is felt for both incoming and outgoing values. As the data becomes larger, the delay begins to increase (somewhere around a 100x100 array). If you have any loops, you would be better served using a raw “run” command for the loop rather than implementing the loop in python:

>>> import oct2py
>>> oct2py.speed_test()

Plotting

Plotting commands do not automatically result in the window being displayed by python. In order to force the plot to be drawn, the command “figure(gcf() + 1);’” is tacked onto anything that looks like a plot command, when called using this package. If you have plot statements in your function that you would like to display, you must add that line after each plot statement.

Thread Safety

Each instance of the Octave object has an independent session of Octave and uses independent random MAT files. The library therefore should be thread safe. See thread_test.py for an example of several objects writing a different value for the same variable name simultaneously and successfully retrieving their own result:

>>> import oct2py
>>> oct2py.thread_test()

Note for MATLAB® users

Octave supports most but not all of the core syntax and commands. The main noticable differences are nested functions are not allowed, and GUIs (including uigetfile, etc.) are not supported. There are several Octave packages (think toolboxes), including image and statistics.

Similar work

  • pytave - Python to Octave bridge, but does not run on win32 (which is the reason for this library).

  • mlabwrap - Python to MATLAB® bridge, requires a MATLAB® license. The oct2py library API is modeled after mlabwrap.

  • ompc, smop - Matlab to Python conversion tools. Both rely on effective parsing of code and a runtime helper library. Ideally one or both of these projects render this one unnecessary. The idea of using introspection to find “nargout” was borrowed from the ompc project.

CI Status

oct2py has automatic testing enabled through the convenient Travis CI project. Here is the latest build status:

https://travis-ci.org/blink1073/oct2py.png?branch=master

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

oct2py-1.0.0.zip (123.2 kB view details)

Uploaded Source

oct2py-1.0.0.tar.gz (111.8 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

oct2py-1.0.0.win32-py3.3.exe (226.5 kB view details)

Uploaded Source

oct2py-1.0.0.win32-py3.2.exe (226.5 kB view details)

Uploaded Source

oct2py-1.0.0.win32-py2.7.exe (226.5 kB view details)

Uploaded Source

File details

Details for the file oct2py-1.0.0.zip.

File metadata

  • Download URL: oct2py-1.0.0.zip
  • Upload date:
  • Size: 123.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for oct2py-1.0.0.zip
Algorithm Hash digest
SHA256 3841172614e7990fee4b2c93d64a4003faad7a5caee5ac74f2cfb440cf91505b
MD5 7b99e8cd726fb60a6d028d3b567b9b1c
BLAKE2b-256 511a2844c3639e19d8bd20e27f3065ea35a11c5e6eb11718d485cc21bbeee4b9

See more details on using hashes here.

File details

Details for the file oct2py-1.0.0.tar.gz.

File metadata

  • Download URL: oct2py-1.0.0.tar.gz
  • Upload date:
  • Size: 111.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for oct2py-1.0.0.tar.gz
Algorithm Hash digest
SHA256 753cdda9b20551c09a53bfa5d2bcb0eab7d7c55a7f2f76d008fd0a7e7cca4c04
MD5 19c3ecdd014b15c0843d19e0f5b8e091
BLAKE2b-256 3fb27f385c8dfd5b825e2d0386dccd43558140500856849b18f857686ab16c92

See more details on using hashes here.

File details

Details for the file oct2py-1.0.0.win32-py3.3.exe.

File metadata

File hashes

Hashes for oct2py-1.0.0.win32-py3.3.exe
Algorithm Hash digest
SHA256 df1d4c65f2d6062875edaefef212d4ee70abf0850534e85866a0a0fc891fb63e
MD5 d88352e5746e9e78e979df6d9986db2b
BLAKE2b-256 710eaf9a948fef3a3f45a0e572f995fc279b3d618a9b7a0e1d2f17e976c042e7

See more details on using hashes here.

File details

Details for the file oct2py-1.0.0.win32-py3.2.exe.

File metadata

File hashes

Hashes for oct2py-1.0.0.win32-py3.2.exe
Algorithm Hash digest
SHA256 a5412518fc029294567533345e0fa34a1f25d07d7e24780dd25f65c072e41309
MD5 2fde818f74f692668e4a780872d691f9
BLAKE2b-256 762fc70503983da43a7b8674858ffe314e18c443dcf5ebe2d477afec498f9f26

See more details on using hashes here.

File details

Details for the file oct2py-1.0.0.win32-py2.7.exe.

File metadata

File hashes

Hashes for oct2py-1.0.0.win32-py2.7.exe
Algorithm Hash digest
SHA256 eb647979da7d9aa74d6229e22e69b27f7edd9f3f0ebe0a78f2baa476f9df7d86
MD5 4a8f4dccc137fddd648ab991884146bc
BLAKE2b-256 7ef1909cdb58d3f8159f5f1688848ab10e2e06e74a7bfd74493e7eea01094bb8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page