Giter Club home page Giter Club logo

me4pyutils's Introduction

ME4PyUtils : MATLAB Engine API for Python utils

Aim

This set of python module and matlab functions aim to ease some data type conversion between matlab and numpy python module. The MATLAB(R) Engine API for Python (ME4P) provides a package for Python to call MATLAB as a computational engine. MPE4P provides nativelly constructors to create arrays in Python (matlab.mlarray.X). This type is built on the array python module. Hower it not possible to create directlt and pass numpy.ndarray. This module proposes some tricks that can be used to

  • convert such mlarray into numpy.ndarray using frombuffer
  • convert numpy.ndarray into matlab.mlarray.X using prealloc and strides
  • pass easily sparse matrix as python dict
  • avoid to copy data (not always possible with complex)

The conversion to numpy is fast, the bottleneck comes from the data transfert between python and matlab through the MATLAB Engine API for Python when the data become significant.

some tricks and ideas comes from here

Basic Usage

import ME4PyUtils as ME
import numpy as np

# start matlab
eng = matlab.engine.start_matlab()
# conversion to matlab type before using it in matlab
np_a = np.random.uniform(size=(1000))
m_a = ME.np2mlarray(np_a)
eng.std(m_a)
# conversion to numpy type before using it in python
m_b = eng.rand(matlab.int64([1,1000]))
np_b = ME.mlarray2np(m_b)
np.std(np_b)

Note that if copies of the array are needed, it can be done using copy module or np.copy.

Installation

  1. install ME4P (see matlab documentation)
  2. Just download or clone the project. Eventually unzip the file and add the folder to matlab and python path.

Test and compare speedup with other possibility

python ME4Putils # set speedtest=True

Frequent issues with MATLAB Engine API

MATLAB Engine API for Python dislikes :

  • Calling a matlab function with integer parameters instead of float. These parameters are casted into long integer and will mislead matlab with int64
eng.sqrt(1)  # will crash
eng.sqrt(1.) # works
eng.mod(5,2) # works because integer are expected from matlab
  • Returning more than one output, without explicitly set nargout
s,id = eng.sort(np2mlarray(np.array([1.1,3.300,2.01])),nargout=2)  # works, id (index) is a double array...
s,id = eng.sort(np2mlarray(np.array([1.1,3.300,2.01])))  # crash

Alternatives

The propose approach use MATLAB Engine API for Python, but other projects have been proposed, and use numpy nativelly.

Transplant (socket communication)

Oct2py (mat-file communication, possible to speed up with RAM drive (tmpfs) by seting temp_dir properly)

mlab

me4pyutils's People

Contributors

nennigb avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

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.