ó
£áSc           @` sÛ   d  d l  m Z m Z m Z d g Z d  d l Z d  d l Z d  d l Z d d l	 m
 Z
 m Z m Z d  d l m Z m Z e Z d d d	 d
 g Z d	 d
 g Z i d d 6d d 6d	 d 6d
 d 6Z d e f d „  ƒ  YZ d S(   i    (   t   divisiont   absolute_importt   print_functiont   memmapNi   (   t   uint8t   ndarrayt   dtype(   t   longt
   basestringt   rt   cs   r+s   w+t   readonlyt   copyonwritet	   readwritet   writec           B` s>   e  Z d  Z d Z e d d d d d „ Z d „  Z d „  Z RS(	   s	  
    Create a memory-map to an array stored in a *binary* file on disk.

    Memory-mapped files are used for accessing small segments of large files
    on disk, without reading the entire file into memory.  Numpy's
    memmap's are array-like objects.  This differs from Python's ``mmap``
    module, which uses file-like objects.

    This subclass of ndarray has some unpleasant interactions with
    some operations, because it doesn't quite fit properly as a subclass.
    An alternative to using this subclass is to create the ``mmap``
    object yourself, then create an ndarray with ndarray.__new__ directly,
    passing the object created in its 'buffer=' parameter.

    This class may at some point be turned into a factory function
    which returns a view into an mmap buffer.

    Parameters
    ----------
    filename : str or file-like object
        The file name or file object to be used as the array data buffer.
    dtype : data-type, optional
        The data-type used to interpret the file contents.
        Default is `uint8`.
    mode : {'r+', 'r', 'w+', 'c'}, optional
        The file is opened in this mode:

        +------+-------------------------------------------------------------+
        | 'r'  | Open existing file for reading only.                        |
        +------+-------------------------------------------------------------+
        | 'r+' | Open existing file for reading and writing.                 |
        +------+-------------------------------------------------------------+
        | 'w+' | Create or overwrite existing file for reading and writing.  |
        +------+-------------------------------------------------------------+
        | 'c'  | Copy-on-write: assignments affect data in memory, but       |
        |      | changes are not saved to disk.  The file on disk is         |
        |      | read-only.                                                  |
        +------+-------------------------------------------------------------+

        Default is 'r+'.
    offset : int, optional
        In the file, array data starts at this offset. Since `offset` is
        measured in bytes, it should normally be a multiple of the byte-size
        of `dtype`. When ``mode != 'r'``, even positive offsets beyond end of
        file are valid; The file will be extended to accommodate the
        additional data. The default offset is 0.
    shape : tuple, optional
        The desired shape of the array. If ``mode == 'r'`` and the number
        of remaining bytes after `offset` is not a multiple of the byte-size
        of `dtype`, you must specify `shape`. By default, the returned array
        will be 1-D with the number of elements determined by file size
        and data-type.
    order : {'C', 'F'}, optional
        Specify the order of the ndarray memory layout: C (row-major) or
        Fortran (column-major).  This only has an effect if the shape is
        greater than 1-D.  The default order is 'C'.

    Attributes
    ----------
    filename : str
        Path to the mapped file.
    offset : int
        Offset position in the file.
    mode : str
        File mode.

    Methods
    -------
    close
        Close the memmap file.
    flush
        Flush any changes in memory to file on disk.
        When you delete a memmap object, flush is called first to write
        changes to disk before removing the object.


    Notes
    -----
    The memmap object can be used anywhere an ndarray is accepted.
    Given a memmap ``fp``, ``isinstance(fp, numpy.ndarray)`` returns
    ``True``.

    Memory-mapped arrays use the Python memory-map object which
    (prior to Python 2.5) does not allow files to be larger than a
    certain size depending on the platform. This size is always < 2GB
    even on 64-bit systems.

    Examples
    --------
    >>> data = np.arange(12, dtype='float32')
    >>> data.resize((3,4))

    This example uses a temporary file so that doctest doesn't write
    files to your directory. You would use a 'normal' filename.

    >>> from tempfile import mkdtemp
    >>> import os.path as path
    >>> filename = path.join(mkdtemp(), 'newfile.dat')

    Create a memmap with dtype and shape that matches our data:

    >>> fp = np.memmap(filename, dtype='float32', mode='w+', shape=(3,4))
    >>> fp
    memmap([[ 0.,  0.,  0.,  0.],
            [ 0.,  0.,  0.,  0.],
            [ 0.,  0.,  0.,  0.]], dtype=float32)

    Write data to memmap array:

    >>> fp[:] = data[:]
    >>> fp
    memmap([[  0.,   1.,   2.,   3.],
            [  4.,   5.,   6.,   7.],
            [  8.,   9.,  10.,  11.]], dtype=float32)

    >>> fp.filename == path.abspath(filename)
    True

    Deletion flushes memory changes to disk before removing the object:

    >>> del fp

    Load the memmap and verify data was stored:

    >>> newfp = np.memmap(filename, dtype='float32', mode='r', shape=(3,4))
    >>> newfp
    memmap([[  0.,   1.,   2.,   3.],
            [  4.,   5.,   6.,   7.],
            [  8.,   9.,  10.,  11.]], dtype=float32)

    Read-only memmap:

    >>> fpr = np.memmap(filename, dtype='float32', mode='r', shape=(3,4))
    >>> fpr.flags.writeable
    False

    Copy-on-write memmap:

    >>> fpc = np.memmap(filename, dtype='float32', mode='c', shape=(3,4))
    >>> fpc.flags.writeable
    True

    It's possible to assign to copy-on-write array, but values are only
    written into the memory copy of the array, and not written to disk:

    >>> fpc
    memmap([[  0.,   1.,   2.,   3.],
            [  4.,   5.,   6.,   7.],
            [  8.,   9.,  10.,  11.]], dtype=float32)
    >>> fpc[0,:] = 0
    >>> fpc
    memmap([[  0.,   0.,   0.,   0.],
            [  4.,   5.,   6.,   7.],
            [  8.,   9.,  10.,  11.]], dtype=float32)

    File on disk is unchanged:

    >>> fpr
    memmap([[  0.,   1.,   2.,   3.],
            [  4.,   5.,   6.,   7.],
            [  8.,   9.,  10.,  11.]], dtype=float32)

    Offset into a memmap:

    >>> fpo = np.memmap(filename, dtype='float32', mode='r', offset=16)
    >>> fpo
    memmap([  4.,   5.,   6.,   7.,   8.,   9.,  10.,  11.], dtype=float32)

    g      YÀs   r+i    t   Cc         C` sQ  d d  l  } d d  l } y t | } Wn@ t k
 rh | t k ri t d t t t j ƒ  ƒ ƒ ‚ qi n Xt | d ƒ r‡ | }	 t	 }
 n+ t
 | | d k rŸ d p¢ | d ƒ }	 t }
 | d k rÙ | d  k rÙ t d ƒ ‚ n  |	 j d d	 ƒ |	 j ƒ  } t | ƒ } | j } | d  k rY| | } | | rC|	 j ƒ  t d
 ƒ ‚ n  | | } | f } n< t | t ƒ st| f } n  d } x | D] } | | 9} q�Wt | | | ƒ } | d k sÍ| d k r| | k  r|	 j | d d ƒ |	 j t j j d ƒ ƒ |	 j ƒ  n  | d k r| j } n! | d k r7| j } n	 | j } | | | j } | | 8} | | 8} | j  |	 j ƒ  | d | d | ƒ} t j  |  | d | d | d | d | ƒ} | | _! | | _" | | _# t | t$ ƒ rõ| j% j& | ƒ | _' nE t | d ƒ r1t | j( t$ ƒ r1| j% j& | j( ƒ | _' n	 d  | _' |
 rM|	 j ƒ  n  | S(   Ni    s   mode must be one of %st   readR
   R	   t   bs   w+s   shape must be giveni   s?   Size of available data is not a multiple of the data-type size.i   s   r+t    t   accesst   offsetR   t   buffert   ordert   name()   t   mmapt   os.patht   mode_equivalentst   KeyErrort   valid_filemodest
   ValueErrort   listt   keyst   hasattrt   Falset   opent   Truet   Nonet   seekt   tellt
   dtypedescrt   itemsizet   closet
   isinstancet   tupleR   R   t   npt   compatt   asbytest   flusht   ACCESS_COPYt   ACCESS_READt   ACCESS_WRITEt   ALLOCATIONGRANULARITYt   filenoR   t   __new__t   _mmapR   t   modeR   t   patht   abspatht   filenameR   (   t   subtypeR:   R   R7   R   t   shapeR   R   t   ost   fidt   own_filet   flent   descrt   _dbytest   bytest   sizet   kt   acct   startt   mmt   self(    (    sK   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/core/memmap.pyR5   Ã   sv    !	%	



$	

$				c         C` s|   t  | d ƒ rT t j |  | ƒ rT | j |  _ | j |  _ | j |  _ | j |  _ n$ d  |  _ d  |  _ d  |  _ d  |  _ d  S(   NR6   (   R    R,   t   may_share_memoryR6   R:   R   R7   R$   (   RI   t   obj(    (    sK   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/core/memmap.pyt   __array_finalize__  s    !			c         C` s5   |  j  d k	 r1 t |  j  d ƒ r1 |  j  j ƒ  n  d S(   sÜ   
        Write any changes in the array to the file on disk.

        For further information, see `memmap`.

        Parameters
        ----------
        None

        See Also
        --------
        memmap

        R/   N(   t   baseR$   R    R/   (   RI   (    (    sK   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/core/memmap.pyR/   !  s    !N(	   t   __name__t
   __module__t   __doc__t   __array_priority__R   R$   R5   RL   R/   (    (    (    sK   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/core/memmap.pyR      s   ©	Q	(   t
   __future__R    R   R   t   __all__t   warningst   syst   numpyR,   t   numericR   R   R   t   numpy.compatR   R   R'   R   t   writeable_filemodesR   R   (    (    (    sK   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/core/memmap.pyt   <module>   s   	
