ó
£áSc           @` sõ  d  Z  d d l m Z m Z m Z d d d d g Z d d l Z d d l Z d d l Z d d l	 Z	 d „  Z
 e j d	 ƒ Z d
 „  Z d „  Z e j d ƒ Z e j d ƒ Z e j d ƒ Z e j d ƒ Z e j d ƒ Z e j d ƒ Z e j d ƒ Z e j d ƒ Z e j d ƒ Z e j d ƒ Z e j d ƒ Z e j d ƒ Z d „  Z d d l Z d „  Z d „  Z d d „ Z d „  Z  e j! j" d d „ Z# e j d ƒ Z$ e j! j" d d d  „ Z& d! „  Z' e j! j" d" „ Z( e) d# k rñe( e j* ƒ n  d S($   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 <yyy> --> import numpy.numarray.package as <yyy>

   from numarray import <xxx> --> from numpy.numarray import <xxx>
   from numarray.package import <xxx>
       --> from numpy.numarray.package import <xxx>

   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

i    (   t   divisiont   absolute_importt   print_functiont   convertfilet
   convertallt   converttreet
   convertsrcNc         C` se  d | } d | } d | } d } | } d | k rK | j  d d ƒ } n  t j d | d | | f |  ƒ }  |  j  | d | ƒ }  |  j  | d	 | | f ƒ }  | | k rÁ |  j  | | ƒ }  n  d } t | ƒ }	 t d | ƒ }
 xu t rZ|  j | | ƒ } | d k  rPn  | |	 } |  | d
 k r.qæ n  d |  |  | |  | f }  | |
 |	 7} qæ W|  | f S(   Ns	   import %ss   import %s as s   from %s import i    t   .t   _s   (import\s+[^,\n\r]+,\s*)(%s)s
   \1%s as %ss   import %s as %st   *s   %sfrom %s import %s(   t   replacet   ret   subt   lent   Truet   find(   t   fstrt   namet   newnamet	   importstrt   importasstrt   fromstrt   fromallt   name_t   indt   Nlent   Nlen2t   found(    (    sT   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/numarray/alter_code1.pyt   changeimports@   s4    


	
s   ([.]flat(\s*?[[=]))c         C` sL   |  j  d ƒ } |  j t j d | ƒ } d |  |  t j d |  | f }  |  S(   Nt   importi    s   %s%s%s%ss!   import numpy.numarray as numarray(   R   t   rfindt   ost   linesep(   t   astrR   t   start(    (    sT   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/numarray/alter_code1.pyt	   addimportb   s    c         C` sZ  |  j  d d ƒ }  |  j  d d ƒ }  |  j  d d ƒ }  |  j  d d ƒ }  |  j  d	 d
 ƒ }  |  j  d d ƒ }  |  j  d d ƒ }  |  j  d d ƒ }  |  j  d d ƒ }  |  j  d d ƒ }  |  j  d d ƒ }  |  j  d d ƒ }  |  j  d d ƒ }  |  j  d d ƒ }  |  j  d d ƒ }  |  j  d d ƒ }  t j d |  ƒ } | j  d d ƒ } | j  d  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   @@@@(   R
   t   flatindex_reR   (   R!   t   tmpstr(    (    sT   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/numarray/alter_code1.pyt   replaceattrk   s(    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*[)]c         C` s  t  j d |  ƒ \ }  } t j d |  ƒ \ }  } t j d |  ƒ \ }  } t j d |  ƒ \ }  } | | | | d k r‡ t |  ƒ }  n  t j d |  ƒ }  t j d |  ƒ }  t	 j d |  ƒ }  t
 j d	 |  ƒ }  t j d
 |  ƒ }  t j d |  ƒ }  t j d |  ƒ }  t j d |  ƒ }  |  S(   Ns   numarray.info(\1)s   numarray.newobj(\1, \2)s   numarray.togglebyteorder(\1)s   numarray.typefrom(\1)i    s   not \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(   t   info_ret   subnt   new_ret	   toggle_ret   type_reR#   t	   isbyte_reR   t	   iscarr_ret	   isfarr_ret	   repeat_ret   setshape_ret
   setreal_ret
   setimag_ret   setimaginary_re(   R!   t   n1t   n2t   n3t   n4(    (    sT   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/numarray/alter_code1.pyt   replaceother“   s    c      
   C` s  |  } t  |  d d ƒ \ }  } d } d } xe d d d d d d d	 d
 d d g
 D]? } | d k ro d | } n  t  |  | | | | ƒ \ }  } qP Wt |  ƒ }  t |  ƒ }  | |  k r	t j j t j d ƒ d } t j	 j
 ƒ  j d ƒ } d | | |  f }  |  d f S|  d f S(   Nt   numarrays   numpy.numarrayt    t   convolvet   imaget   nd_imaget   mlabt   linear_algebrat   mat   matrixt   fftt   random_arrayR   i    iÿÿÿÿs	   %b %d, %Ys8   ## Automatically adapted for numpy.numarray %s by %s

%si   (   R   R&   R8   R   t   patht   splitt   syst   argvt   datetimet   datet   todayt   strftime(   t   filestrt   savestrR   t   baset   newbaseR   R   RJ   (    (    sT   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/numarray/alter_code1.pyR   ª   s$    $
c         C` s*   t  |  d ƒ } | j | ƒ | j ƒ  d  S(   Nt   w(   t   filet   writet   close(   R   RL   t   fid(    (    sT   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/numarray/alter_code1.pyt   makenewfile¿   s    i   c         C` s�   t  |  ƒ } | j ƒ  } | j ƒ  t | ƒ \ } } | rŒ | ro t j j |  ƒ \ } } t j |  | d ƒ n t j |  ƒ t	 |  | ƒ n  d S(   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(
   t   opent   readRS   R   R   RD   t   splitextt   renamet   removeRU   (   t   filenamet   origRT   RL   t   changedRN   t   ext(    (    sT   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/numarray/alter_code1.pyR   Ä   s    
c         C` s   |  d } t  | ƒ d  S(   Ni   (   R   (   t   argsR[   (    (    sT   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/numarray/alter_code1.pyt   fromargsÖ   s    
c         C` sV   t  j  t j j |  d ƒ ƒ } x1 | D]) } | d d k rA q% n  t | | ƒ q% Wd S(   sý   Convert all .py files to use numpy.oldnumeric (from Numeric) in the directory given

    For each file, a backup of <usesnumeric>.py is made as
    <usesnumeric>.py.orig.  A new file named <usesnumeric>.py
    is then written with the updated code.
    s   *.pyiøÿÿÿs   setup.pyN(   t   globR   RD   t   joinR   (   t   direcR\   t   filest   afile(    (    sT   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/numarray/alter_code1.pyR   Ú   s
     s   (numarray/libnumarray.h)c   
      C` s  | d k r- t j t j j |  d ƒ ƒ } n@ g  } x7 | D]/ } | j t j t j j |  d | ƒ ƒ ƒ q: Wx¦ | D]ž } t | ƒ } | j ƒ  } | j ƒ  t	 j
 d | ƒ \ } } | d k rt | rõ t j j | ƒ \ }	 } t j | |	 d ƒ n t j | ƒ t | | ƒ qt qt Wd S(   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).R	   s   *.%ss   numpy/libnumarray.hi    s   .origN(   t   NoneRa   R   RD   Rb   t   extendRV   RW   RS   t	   header_reR(   RX   RY   RZ   RU   (
   Rc   R^   R\   Rd   t   aextRe   RT   R   t   nRN   (    (    sT   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/numarray/alter_code1.pyR   è   s     !-
c         C` s-   t  | d d ƒt | d d g d d ƒd  S(   NR\   i    t   ht   c(   R   R   (   t   argt   dirnamet   fnames(    (    sT   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/numarray/alter_code1.pyt   _funcÿ   s    c         C` s   t  j j |  t d ƒ d S(   s-   Convert all .py files in the tree given

    N(   R   RD   t   walkRp   Rf   (   Rc   (    (    sT   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/numarray/alter_code1.pyR     s    t   __main__(+   t   __doc__t
   __future__R    R   R   t   __all__RF   R   R   Ra   R   t   compileR$   R#   R&   R'   R)   R*   R+   R,   R-   R.   R/   R0   R1   R2   R3   R8   RH   R   RU   R   R`   RD   t   curdirR   Rh   Rf   R   Rp   R   t   __name__RG   (    (    (    sT   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/numarray/alter_code1.pyt   <module>6   sF   									