Re: Python from Matlab (Stefan Huber )


Subject: Re: Python from Matlab
From:    Stefan Huber  <stefan.huber@xxxxxxxx>
Date:    Fri, 22 Aug 2014 14:31:31 +0200
List-Archive:<http://lists.mcgill.ca/scripts/wa.exe?LIST=AUDITORY>

--001a1134ab00ba63d00501370617 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Hi, as you can simply set up calls to the shell from Matlab, like with this example on a Linux machine: unix(['pwd']); ... you can run the same way your python scripts: unix(['python mynewfancyscript.py ' mybestoptions '>>& mystuff.log']); Or add the shebang line '#! /usr/bin/env python' on top of your python script to avoid the explicit call of the python interpeter: unix(['mynewfancyscript.py ' mybestoptions '>>& mystuff.log']); Moreover, scipy provides with the interface scipy.io a straightforward manner to translate Matlab files 1-to-1 into Python. http://docs.scipy.org/doc/scipy/reference/tutorial/io.html In Matlab you save your options, e.g. a parameter struct: save('params.mat','params'); The loadmat function in python then returns a dictionary with your variable names as keys and your matrices as its values: #! /usr/bin/env python import sys import scipy.io as sio if __name__ =3D=3D "__main__": if (len(sys.argv[:]) =3D=3D 2): matparams =3D sio.loadmat(sys.argv[1], struct_as_record=3DFalse, squeeze_me=3DTrue) params =3D matparams['params'] Best, --=20 Stefan Huber Ph.D. candidate at the Sound Analysis/Synthesis Team, IRCAM, Stefan.Huber@xxxxxxxx Staff member, Acapela Group, Stefan.Huber@xxxxxxxx On Fri, Aug 22, 2014 at 10:55 AM, Sebastian Ewert <s.ewert@xxxxxxxx> wrote: > Hi, > > the upcoming version of matlab has native python support (at least > according to the beta changelog). I have no idea how useful it is going t= o > be... > > From the pre-release documentation: > > "py package for using Python functions and objects in MATLAB, and engine > interface for calling MATLAB from Python" > > Sebastian > > > On 22/08/2014 08:44, Alain de Cheveigne wrote: > >> Hi Marek, >> >> I am looking for the opposite: an easy, efficient and reliable way to >> call Python from Matlab. My programming environment nowdays is matlab a= nd >> I can't easily change. The best course that I see is to implement new c= ode >> in Python and call it from matlab, and make the switch to Python when >> Python code reaches critical mass. I suspect many people are in a simil= ar >> position. >> >> Alain >> >> >> >> On 21 Aug 2014, at 11:20, Marek Rudnicki <marek.rudnicki@xxxxxxxx> wrote: >> >> Etienne Gaudrain <e.p.c.gaudrain@xxxxxxxx> writes: >>> >>> Dear Marek, >>>> >>>> This looks very promising, I'm glad to finally see the first signs of = a >>>> shift from Matlab to Python. Thanks a lot for sharing this!! >>>> >>> Dear Etienne >>> >>> I'm glad you like it. >>> >>> BTW, if you still have some MATLAB legacy code that you would like to >>> use in Python, then matlab_wrapper [1] could be helpful. It allows you >>> to call MATLAB functions directly from Python environment (the MATLAB >>> process is started in the background), e.g.: >>> >>> matlab.workspace.sin([0.1, 0.2, 0.3]) >>> >>> >>> Cheers >>> Marek >>> >>> >>> [1] https://github.com/mrkrd/matlab_wrapper >>> >>> >>> >>> On 19/08/2014 14:50, Marek Rudnicki wrote: >>>> >>>>> Hi all, >>>>> >>>>> we would like to announce *cochlea* -- a collection of inner ear >>>>> models in Python. It was developed in the group of Werner Hemmert [1= ] >>>>> at the Technische Universit=C3=A4t M=C3=BCnchen. After a few years o= f >>>>> development, we decided that it is stable and would like to contribut= e >>>>> it to the auditory community. >>>>> >>>>> >>>>> >>>>> The main features of the package are: >>>>> >>>>> - simple to use (each model is implemented as a single Python >>>>> function: sound in, spikes out) >>>>> - fast (you can generate responses of hundreds or even thousands o= f >>>>> nerve fibers) >>>>> - all models have the same interface (easy to make comparisons and >>>>> pick the one that best suits your needs) >>>>> - up-to-date (recent models included) >>>>> >>>>> >>>>> >>>>> Currently implemented models are: >>>>> >>>>> - Zilany, M. S., Bruce, I. C., & Carney, L. H. (2014). Updated >>>>> parameters and expanded simulation options for a model of the >>>>> auditory periphery. The Journal of the Acoustical Society of >>>>> America, 135(1), 283-286. >>>>> - Zilany, M. S., Bruce, I. C., Nelson, P. C., & Carney, >>>>> L. H. (2009). A phenomenological model of the synapse between th= e >>>>> inner hair cell and auditory nerve: long-term adaptation with >>>>> power-law dynamics. The Journal of the Acoustical Society of >>>>> America, 126(5), 2390-2412. >>>>> - Holmberg, M. (2007). Speech Encoding in the Human Auditory >>>>> Periphery: Modeling and Quantitative Assessment by Means of >>>>> Automatic Speech Recognition. PhD thesis, Technical University >>>>> Darmstadt. >>>>> - MATLAB Auditory Periphery by Meddis et al. (external model, not >>>>> implemented in the package, but easily accessible through >>>>> matlab_wrapper). >>>>> >>>>> >>>>> >>>>> We are really grateful to the authors of those models for allowing us >>>>> to use their code it in *cochlea*. We release the package under the >>>>> GNU General Public License, so that you are free to copy, use and >>>>> modify the code. We also encourage you to contribute back your >>>>> changes. >>>>> >>>>> >>>>> The code is distributed on GitHub [2] and the package/documentation >>>>> are hosted on the Python Package Index [3]. Check also our demo [4]! >>>>> >>>>> >>>>> If you would like to give a feedback, have questions or found some >>>>> problem, do not hesitate to email me or open an issue on GitHub [2]. >>>>> >>>>> >>>>> Thank you and best regards >>>>> Marek Rudnicki >>>>> >>>>> >>>>> [1] http://www.imetum.tum.de/research/bai/home/?L=3D1 >>>>> [2] https://github.com/mrkrd/cochlea >>>>> [3] https://pythonhosted.org/cochlea/ >>>>> [4] http://nbviewer.ipython.org/github/mrkrd/cochlea/blob/ >>>>> master/examples/cochlea_demo.ipynb >>>>> >>>> > -- > Dr. Sebastian Ewert > Centre for Digital Music, Queen Mary University of London > s.ewert@xxxxxxxx > Phone: +44 207 882 8287 > --001a1134ab00ba63d00501370617 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr">Hi,<div> as you can simply set up calls to the shell from Matlab, like with this example on a Linux machine:<br> unix([&#39;pwd&#39;]);<br> <br> ... you can run the same way your python scripts:<br> unix([&#39;python mynewfancyscript.py &#39; mybestoptions &#39;&gt;&gt;= &amp; mystuff.log&#39;]);<br> <br> Or add the shebang line &#39;#! /usr/bin/env python&#39; on top of your python script to avoid the explicit call of the python interpeter:<br> unix([&#39;mynewfancyscript.py &#39; mybestoptions &#39;&gt;&gt;&amp; mystuff.log&#39;]);<br> <br> <br> Moreover, scipy provides with the interface <a href=3D"http://scipy.io"= target=3D"_blank">scipy.io</a> a straightforward manner to translate Matlab files 1-to-1 into Python.<br= > <a href=3D"http://docs.scipy.org/doc/scipy/reference/tutorial/io.html" = target=3D"_blank">http://docs.scipy.org/doc/scipy/reference/tutorial/io.htm= l</a><br> <br> In Matlab you save your options, e.g. a parameter struct:<br> save(&#39;params.mat&#39;,&#39;params&#39;);<br> <br> The loadmat function in python then returns a dictionary with your vari= able names as keys and your matrices as its values:</div><div>#! /usr/bin/e= nv python<br></div><div><br></div><div>import sys<br></div><div>import <a h= ref=3D"http://scipy.io" target=3D"_blank">scipy.io</a> as sio</div> <div><br></div><div>if __name__ =3D=3D &quot;__main__&quot;:</div><div>=C2= =A0 =C2=A0=C2=A0if (len(sys.argv[:]) =3D=3D 2):</div><div>=C2=A0 =C2=A0 =C2= =A0 =C2=A0=C2=A0matparams =3D sio.loadmat(sys.argv[1], struct_as_record=3DF= alse, squeeze_me=3DTrue)<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 params =3D matparams[= &#39;params&#39;]<br> <br> Best,<br><span class=3D""><font color=3D"#888888"><pre cols=3D"72">--= =20 Stefan Huber Ph.D. candidate at the Sound Analysis/Synthesis Team, IRCAM, <a href=3D"mai= lto:Stefan.Huber@xxxxxxxx" target=3D"_blank">Stefan.Huber@xxxxxxxx</a> Staff member, Acapela Group, <a href=3D"mailto:Stefan.Huber@xxxxxxxx= e" target=3D"_blank">Stefan.Huber@xxxxxxxx</a> </pre></font></span>= <br><br></div></div><div class=3D"gmail_extra"><br><br><div class=3D"gmail_= quote"> On Fri, Aug 22, 2014 at 10:55 AM, Sebastian Ewert <span dir=3D"ltr">&lt;<a = href=3D"mailto:s.ewert@xxxxxxxx" target=3D"_blank">s.ewert@xxxxxxxx</a>= &gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin:0 0= 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> Hi,<br> <br> the upcoming version of matlab has native python support (at least accordin= g to the beta changelog). I have no idea how useful it is going to be...<br= > <br> >From the pre-release documentation:<br> <br> &quot;py package for using Python functions and objects in MATLAB, and engi= ne interface for calling MATLAB from Python&quot;<br> <br> Sebastian<br> <br> <br> On 22/08/2014 08:44, Alain de Cheveigne wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> Hi Marek,<br> <br> I am looking for the opposite: an easy, efficient=C2=A0 and reliable way to= call Python from Matlab.=C2=A0 My programming environment nowdays is matla= b and I can&#39;t easily change.=C2=A0 The best course that I see is to imp= lement new code in Python and call it from matlab, and make the switch to P= ython when Python code reaches critical mass.=C2=A0 I suspect many people a= re in a similar position.<br> <br> Alain<br> <br> <br> <br> On 21 Aug 2014, at 11:20, Marek Rudnicki &lt;<a href=3D"mailto:marek.rudnic= ki@xxxxxxxx" target=3D"_blank">marek.rudnicki@xxxxxxxx</a>&gt; wrote:<br> <br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> Etienne Gaudrain &lt;<a href=3D"mailto:e.p.c.gaudrain@xxxxxxxx" target=3D"_b= lank">e.p.c.gaudrain@xxxxxxxx</a>&gt; writes:<br> <br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> Dear Marek,<br> <br> This looks very promising, I&#39;m glad to finally see the first signs of a= <br> shift from Matlab to Python. Thanks a lot for sharing this!!<br> </blockquote> Dear Etienne<br> <br> I&#39;m glad you like it.<br> <br> BTW, if you still have some MATLAB legacy code that you would like to<br> use in Python, then matlab_wrapper [1] could be helpful.=C2=A0 It allows yo= u<br> to call MATLAB functions directly from Python environment (the MATLAB<br> process is started in the background), e.g.:<br> <br> matlab.workspace.sin([0.1, 0.2, 0.3])<br> <br> <br> Cheers<br> Marek<br> <br> <br> [1] <a href=3D"https://github.com/mrkrd/matlab_wrapper" target=3D"_blank">h= ttps://github.com/mrkrd/<u></u>matlab_wrapper</a><br> <br> <br> <br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> On 19/08/2014 14:50, Marek Rudnicki wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> Hi all,<br> <br> we would like to announce *cochlea* -- a collection of inner ear<br> models in Python.=C2=A0 It was developed in the group of Werner Hemmert [1]= <br> at the Technische Universit=C3=A4t M=C3=BCnchen.=C2=A0 After a few years of= <br> development, we decided that it is stable and would like to contribute<br> it to the auditory community.<br> <br> <br> <br> The main features of the package are:<br> <br> =C2=A0 =C2=A0- simple to use (each model is implemented as a single Python<= br> =C2=A0 =C2=A0 =C2=A0function: sound in, spikes out)<br> =C2=A0 =C2=A0- fast (you can generate responses of hundreds or even thousan= ds of<br> =C2=A0 =C2=A0 =C2=A0nerve fibers)<br> =C2=A0 =C2=A0- all models have the same interface (easy to make comparisons= and<br> =C2=A0 =C2=A0 =C2=A0pick the one that best suits your needs)<br> =C2=A0 =C2=A0- up-to-date (recent models included)<br> <br> <br> <br> Currently implemented models are:<br> <br> =C2=A0 =C2=A0- Zilany, M. S., Bruce, I. C., &amp; Carney, L. H. (2014). Upd= ated<br> =C2=A0 =C2=A0 =C2=A0parameters and expanded simulation options for a model = of the<br> =C2=A0 =C2=A0 =C2=A0auditory periphery. The Journal of the Acoustical Socie= ty of<br> =C2=A0 =C2=A0 =C2=A0America, 135(1), 283-286.<br> =C2=A0 =C2=A0- Zilany, M. S., Bruce, I. C., Nelson, P. C., &amp; Carney,<br= > =C2=A0 =C2=A0 =C2=A0L. H. (2009). A phenomenological model of the synapse b= etween the<br> =C2=A0 =C2=A0 =C2=A0inner hair cell and auditory nerve: long-term adaptatio= n with<br> =C2=A0 =C2=A0 =C2=A0power-law dynamics. The Journal of the Acoustical Socie= ty of<br> =C2=A0 =C2=A0 =C2=A0America, 126(5), 2390-2412.<br> =C2=A0 =C2=A0- Holmberg, M. (2007). Speech Encoding in the Human Auditory<b= r> =C2=A0 =C2=A0 =C2=A0Periphery: Modeling and Quantitative Assessment by Mean= s of<br> =C2=A0 =C2=A0 =C2=A0Automatic Speech Recognition. PhD thesis, Technical Uni= versity<br> =C2=A0 =C2=A0 =C2=A0Darmstadt.<br> =C2=A0 =C2=A0- MATLAB Auditory Periphery by Meddis et al. (external model, = not<br> =C2=A0 =C2=A0 =C2=A0implemented in the package, but easily accessible throu= gh<br> =C2=A0 =C2=A0 =C2=A0matlab_wrapper).<br> <br> <br> <br> We are really grateful to the authors of those models for allowing us<br> to use their code it in *cochlea*.=C2=A0 We release the package under the<b= r> GNU General Public License, so that you are free to copy, use and<br> modify the code.=C2=A0 We also encourage you to contribute back your<br> changes.<br> <br> <br> The code is distributed on GitHub [2] and the package/documentation<br> are hosted on the Python Package Index [3].=C2=A0 Check also our demo [4]!<= br> <br> <br> If you would like to give a feedback, have questions or found some<br> problem, do not hesitate to email me or open an issue on GitHub [2].<br> <br> <br> Thank you and best regards<br> Marek Rudnicki<br> <br> <br> [1] <a href=3D"http://www.imetum.tum.de/research/bai/home/?L=3D1" target=3D= "_blank">http://www.imetum.tum.de/<u></u>research/bai/home/?L=3D1</a><br> [2] <a href=3D"https://github.com/mrkrd/cochlea" target=3D"_blank">https://= github.com/mrkrd/<u></u>cochlea</a><br> [3] <a href=3D"https://pythonhosted.org/cochlea/" target=3D"_blank">https:/= /pythonhosted.org/<u></u>cochlea/</a><br> [4] <a href=3D"http://nbviewer.ipython.org/github/mrkrd/cochlea/blob/master= /examples/cochlea_demo.ipynb" target=3D"_blank">http://nbviewer.ipython.org= /<u></u>github/mrkrd/cochlea/blob/<u></u>master/examples/cochlea_demo.<u></= u>ipynb</a><span class=3D"HOEnZb"><font color=3D"#888888"><br> </font></span></blockquote></blockquote></blockquote></blockquote><span cla= ss=3D"HOEnZb"><font color=3D"#888888"> <br> -- <br> Dr. Sebastian Ewert<br> Centre for Digital Music, Queen Mary University of London<br> <a href=3D"mailto:s.ewert@xxxxxxxx" target=3D"_blank">s.ewert@xxxxxxxx<= /a><br> Phone: <a href=3D"tel:%2B44%20207%20882%208287" value=3D"+442078828287" tar= get=3D"_blank">+44 207 882 8287</a><br> </font></span></blockquote></div><br></div> --001a1134ab00ba63d00501370617--


This message came from the mail archive
http://www.auditory.org/postings/2014/
maintained by:
DAn Ellis <dpwe@ee.columbia.edu>
Electrical Engineering Dept., Columbia University