ó Kc@sÙdZddddgZddlZddlZddlZddlZd„ZejdƒZd „Z d „Z ejd ƒZ ejd ƒZ ejd ƒZ ejdƒZejdƒZejdƒZejdƒZejdƒZejdƒZejdƒZejdƒZejdƒZd„ZddlZd„Zd„Zdd„Zd„Zejjdd„ZejdƒZ ejjddd„Z"d „Z#ejjd!„Z$e%d"krÕe$ej&ƒndS(#s6 This module converts code written for numarray to run with numpy Makes the following changes: * Changes import statements import numarray.package --> import numpy.numarray.package as numarray_package with all numarray.package in code changed to numarray_package import numarray --> import numpy.numarray as numarray import numarray.package as --> import numpy.numarray.package as from numarray import --> from numpy.numarray import from numarray.package import --> from numpy.numarray.package import package can be convolve, image, nd_image, mlab, linear_algebra, ma, matrix, fft, random_array * Makes search and replace changes to: - .imaginary --> .imag - .flat --> .ravel() (most of the time) - .byteswapped() --> .byteswap(False) - .byteswap() --> .byteswap(True) - .info() --> numarray.info(self) - .isaligned() --> .flags.aligned - .isbyteswapped() --> (not .dtype.isnative) - .typecode() --> .dtype.char - .iscontiguous() --> .flags.contiguous - .is_c_array() --> .flags.carray and .dtype.isnative - .is_fortran_contiguous() --> .flags.fortran - .is_f_array() --> .dtype.isnative and .flags.farray - .itemsize() --> .itemsize - .nelements() --> .size - self.new(type) --> numarray.newobj(self, type) - .repeat(r) --> .repeat(r, axis=0) - .size() --> .size - self.type() -- numarray.typefrom(self) - .typecode() --> .dtype.char - .stddev() --> .std() - .togglebyteorder() --> numarray.togglebyteorder(self) - .getshape() --> .shape - .setshape(obj) --> .shape=obj - .getflat() --> .ravel() - .getreal() --> .real - .setreal() --> .real = - .getimag() --> .imag - .setimag() --> .imag = - .getimaginary() --> .imag - .setimaginary() --> .imag t convertfilet convertallt converttreet convertsrciÿÿÿÿNc Cs^d|}d|}d|}d}|}d|krK|jddƒ}ntjd|d||f|ƒ}|j|d|ƒ}|j|d ||fƒ}||krÁ|j||ƒ}nd}t|ƒ} td|ƒ} xn|j||ƒ} | dkrPn| | }||d kr(qænd || |||f}|| | 7}qæ||fS( Ns import %ss import %s as sfrom %s import it.t_s(import\s+[^,\n\r]+,\s*)(%s)s \1%s as %ssimport %s as %st*s%sfrom %s import %s(treplacetretsubtlentfind( tfstrtnametnewnamet importstrt importasstrtfromstrtfromalltname_tindtNlentNlen2tfound((s>/usr/lib/python2.7/site-packages/numpy/numarray/alter_code1.pyt changeimports>s4         s([.]flat(\s*?[[=]))cCsL|jdƒ}|jtjd|ƒ}d|| tjd||f}|S(Ntimportis%s%s%s%ss!import numpy.numarray as numarray(R trfindtostlinesep(tastrRtstart((s>/usr/lib/python2.7/site-packages/numpy/numarray/alter_code1.pyt addimport`s cCsZ|jddƒ}|jddƒ}|jddƒ}|jddƒ}|jd d ƒ}|jd d ƒ}|jd dƒ}|jddƒ}|jddƒ}|jddƒ}|jddƒ}|jddƒ}|jddƒ}|jddƒ}|jddƒ}|jddƒ}tjd|ƒ}|jddƒ}|jd dƒ}|S(!Ns .imaginarys.imags.byteswapped()s.byteswap(False)s .byteswap()s.byteswap(True)s .isaligned()s.flags.aligneds.iscontiguous()s.flags.contiguouss.is_fortran_contiguous()s.flags.fortrans .itemsize()s .itemsizes.size()s.sizes .nelements()s .typecode()s .dtype.chars .stddev()s.std()s .getshape()s.shapes .getflat()s.ravel()s.getreals.reals.getimags .getimaginarys@@@@\2s.flats@@@@(Rt flatindex_reR (Rttmpstr((s>/usr/lib/python2.7/site-packages/numpy/numarray/alter_code1.pyt replaceattris(s(\S+)\s*[.]\s*info\s*[(]\s*[)]s%(\S+)\s*[.]\s*new\s*[(]\s*(\S+)\s*[)]s)(\S+)\s*[.]\s*togglebyteorder\s*[(]\s*[)]s(\S+)\s*[.]\s*type\s*[(]\s*[)]s'(\S+)\s*[.]\s*isbyteswapped\s*[(]\s*[)]s$(\S+)\s*[.]\s*is_c_array\s*[(]\s*[)]s$(\S+)\s*[.]\s*is_f_array\s*[(]\s*[)]s((\S+)\s*[.]\s*repeat\s*[(]\s*(\S+)\s*[)]s*(\S+)\s*[.]\s*setshape\s*[(]\s*(\S+)\s*[)]s)(\S+)\s*[.]\s*setreal\s*[(]\s*(\S+)\s*[)]s)(\S+)\s*[.]\s*setimag\s*[(]\s*(\S+)\s*[)]s.(\S+)\s*[.]\s*setimaginary\s*[(]\s*(\S+)\s*[)]cCstjd|ƒ\}}tjd|ƒ\}}tjd|ƒ\}}tjd|ƒ\}}||||dkr‡t|ƒ}ntjd|ƒ}tjd|ƒ}t jd|ƒ}t jd |ƒ}t jd |ƒ}t jd |ƒ}t jd |ƒ}tjd |ƒ}|S( Nsnumarray.info(\1)snumarray.newobj(\1, \2)snumarray.togglebyteorder(\1)snumarray.typefrom(\1)isnot \1.dtype.isnatives%\1.dtype.isnative and \1.flags.carrays%\1.dtype.isnative and \1.flags.farrays\1.repeat(\2, axis=0)s \1.shape = \2s \1.real = \2s \1.imag = \2(tinfo_retsubntnew_ret toggle_rettype_reRt isbyte_reR t iscarr_ret isfarr_ret repeat_ret setshape_ret setreal_ret setimag_retsetimaginary_re(Rtn1tn2tn3tn4((s>/usr/lib/python2.7/site-packages/numpy/numarray/alter_code1.pyt replaceother‘sc Cs|}t|ddƒ\}}d}d}xeddddddd d d d g D]?}|dkrod |}nt|||||ƒ\}}qPWt|ƒ}t|ƒ}||kr tjjtjdƒd}tj j ƒj dƒ}d|||f}|dfS|dfS(Ntnumarraysnumpy.numarrayttconvolvetimagetnd_imagetmlabtlinear_algebratmatmatrixtfftt random_arrayRiiÿÿÿÿs %b %d, %Ys8## Automatically adapted for numpy.numarray %s by %s %si( RR"R4Rtpathtsplittsystargvtdatetimetdatettodaytstrftime(tfilestrtsavestrRtbasetnewbaseR R RF((s>/usr/lib/python2.7/site-packages/numpy/numarray/alter_code1.pyR¨s$  $    cCs*t|dƒ}|j|ƒ|jƒdS(Ntw(tfiletwritetclose(R RHtfid((s>/usr/lib/python2.7/site-packages/numpy/numarray/alter_code1.pyt makenewfile½s icCst|ƒ}|jƒ}|jƒt|ƒ\}}|rŒ|rotjj|ƒ\}}tj||dƒn tj|ƒt ||ƒndS(s Convert the filename given from using Numarray to using NumPy Copies the file to filename.orig and then over-writes the file with the updated code s.origN( topentreadRORRR@tsplitexttrenametremoveRQ(tfilenametorigRPRHtchangedRJtext((s>/usr/lib/python2.7/site-packages/numpy/numarray/alter_code1.pyRÂs    cCs|d}t|ƒdS(Ni(R(targsRW((s>/usr/lib/python2.7/site-packages/numpy/numarray/alter_code1.pytfromargsÔs cCsVtjtjj|dƒƒ}x1|D])}|ddkrAq%nt||ƒq%WdS(sýConvert all .py files to use numpy.oldnumeric (from Numeric) in the directory given For each file, a backup of .py is made as .py.orig. A new file named .py is then written with the updated code. s*.pyiøÿÿÿssetup.pyN(tglobRR@tjoinR(tdirecRXtfilestafile((s>/usr/lib/python2.7/site-packages/numpy/numarray/alter_code1.pyRØs  s(numarray/libnumarray.h)c Cs|dkr-tjtjj|dƒƒ}n@g}x7|D]/}|jtjtjj|d|ƒƒƒq:Wx¦|D]ž}t|ƒ}|jƒ}|jƒt j d|ƒ\}}|dkrt|rõtjj |ƒ\} }tj || dƒn tj |ƒt||ƒqtqtWdS(sªReplace Numeric/arrayobject.h with numpy/oldnumeric.h in all files in the directory with extension give by list ext (if ext is None, then all files are replaced).Rs*.%ssnumpy/libnumarray.his.origN(tNoneR]RR@R^textendRRRSROt header_reR$RTRURVRQ( R_RZRXR`taextRaRPR tnRJ((s>/usr/lib/python2.7/site-packages/numpy/numarray/alter_code1.pyRæs  ! -      cCs-t|ddƒt|ddgddƒdS(NRXithtc(RR(targtdirnametfnames((s>/usr/lib/python2.7/site-packages/numpy/numarray/alter_code1.pyt_funcýscCstjj|tdƒdS(s-Convert all .py files in the tree given N(RR@twalkRlRb(R_((s>/usr/lib/python2.7/site-packages/numpy/numarray/alter_code1.pyRst__main__('t__doc__t__all__RBRRR]RtcompileR RR"R#R%R&R'R(R)R*R+R,R-R.R/R4RDRRQRR\R@tcurdirRRdRbRRlRt__name__RC(((s>/usr/lib/python2.7/site-packages/numpy/numarray/alter_code1.pyt6sD