B s    r>c Csddl}|dkr:t|dd}|d|d}t||d}n>|dkrlt|d d}|d |d}t||d}n td |t|}y t|}Wn6tk r}zd }t|||fWdd}~XYnXt|tsd }t||t | } | dddgkr d}t|| ft|dt r6t dd|dDsLd}t||dft|dtsrd}t||dfyt |d} Wn:tk r}zd}t||dfWdd}~XYnX|d|d| fS)z# see read_array_header_1_0 rN)r rr zarray header lengthzsz&_read_array_header..zshape is not valid: %rz%fortran_order is not a valid bool: %rz)descr is not a valid dtype descriptor: %r)r7runpackrr>r SyntaxErrorrXdictr8keystuplenumpyallboolr' TypeError) r"rr7Z hlength_strZ header_lengthrAr/err`r'rrrrGsH    "   $rGTcCs:t|t|t||}|dkr8|dkr8tjdtdd|jdkrHd}ntd|jd}|jj r|slt d|d krxi}t j ||fd di|n|j jr|j jst|r|j|n2x|tj|d d d g|ddD]}||dqWnJt|r||n4x2tj|d d d g|ddD]}||dqWd S)a' Write an array to an NPY file, including a header. If the array is neither C-contiguous nor Fortran-contiguous AND the file_like object is not a real file object, this function will have to copy data in memory. Parameters ---------- fp : file_like object An open, writable file object, or similar object with a ``.write()`` method. array : ndarray The array to write to disk. version : (int, int) or None, optional The version number of the format. None means use the oldest supported version that is able to store the data. Default: None allow_pickle : bool, optional Whether to allow writing pickled data. Default: True pickle_kwargs : dict, optional Additional keyword arguments to pass to pickle.dump, excluding 'protocol'. These are only useful when pickling objects in object arrays on Python 3 to Python 2 compatible format. Raises ------ ValueError If the array cannot be persisted. This includes the case of allow_pickle=False and array being an object array. Various other errors If the array contains Python objects as part of its dtype, the process of pickling them may raise various errors if the objects are not picklable. )r rz>Stored array in format 2.0. It can only beread by NumPy >= 1.9r ) stacklevelrir z5Object arrays cannot be saved when allow_pickle=FalseNZprotocolZ external_loopZbufferedZ zerosize_okF)r+ buffersizeorderC)rrDr0warningswarn UserWarningitemsizemaxr' hasobjectrpickledumpr+r-r,rTZtofilerbZnditerr@tobytes)r"r.r allow_pickle pickle_kwargsused_verrichunkrrr write_arrays8$       rzc Cst|}t|t||\}}}t|dkr2d}ntjj|tjd}|jr|sVt d|dkrbi}yt j |f|}Wn@t k r} z"t jddkrt d| fWdd} ~ XYnXnt|rtj|||d}ntj||d}|jdkrXttt|j} xZtd|| D]J} t| || } t| |j} t|| d }tj||| d|| | | <q W|rx|ddd |_|}n||_|S) a Read an array from an NPY file. Parameters ---------- fp : file_like object If this is not a real file object, then this may take extra memory and time. allow_pickle : bool, optional Whether to allow reading pickled data. Default: True pickle_kwargs : dict Additional keyword arguments to pass to pickle.load. These are only useful when loading object arrays saved on Python 2 when using Python 3. Returns ------- array : ndarray The array from the data on disk. Raises ------ ValueError If the data is invalid, or allow_pickle=False and the file contains an object array. rr )r'z6Object arrays cannot be loaded when allow_pickle=FalseNr z]Unpickling a python object failed: %r You may need to pass the encoding= option to numpy.load)r'countz array data)r#rrGr=rbZmultiplyreduceZint64rqrrrload UnicodeErrorrrrZfromfileZndarrayro BUFFER_SIZEminrangerYrZ frombufferr)Z transpose)r"rvrwrr)r*r'r{r.errZmax_read_countiZ read_countZ read_sizedatarrr read_arraySsD      rr+Fc Cs0t|tstdd|krt|t|}|jr>d}t|tt|||d}t ||d}z8t |||} |dkr| dkrt j dt dd |} Wd |XnTt |d }z>t|}t|t||\}}}|jrd}t||} Wd |X|rd } nd } |dkrd}tj|||| || d} | S)a Open a .npy file as a memory-mapped array. This may be used to read an existing file or create a new one. Parameters ---------- filename : str The name of the file on disk. This may *not* be a file-like object. mode : str, optional The mode in which to open the file; the default is 'r+'. In addition to the standard file modes, 'c' is also accepted to mean "copy on write." See `memmap` for the available mode strings. dtype : data-type, optional The data type of the array if we are creating a new file in "write" mode, if not, `dtype` is ignored. The default value is None, which results in a data-type of `float64`. shape : tuple of int The shape of the array if we are creating a new file in "write" mode, in which case this parameter is required. Otherwise, this parameter is ignored and is thus optional. fortran_order : bool, optional Whether the array should be Fortran-contiguous (True) or C-contiguous (False, the default) if we are creating a new file in "write" mode. version : tuple of int (major, minor) or None If the mode is a "write" mode, then this is the version of the file format used to create the file. None means use the oldest supported version that is able to store the data. Default: None Returns ------- marray : memmap The memory-mapped array. Raises ------ ValueError If the data or the mode is invalid. IOError If the file is not found or cannot be opened correctly. See Also -------- memmap zDFilename must be a string. Memmap cannot use existing file handles.wz6Array can't be memory-mapped: Python objects in dtype.)r%r*r)b)r rz>Stored array in format 2.0. It can only beread by NumPy >= 1.9r )rgNrbrhrkzw+zr+)r'r)rjmodeoffset)rXr rrrbr'rqr_r(openrDrlrmrntellcloser#rGZmemmap) filenamerr'r)r*rrr/r"rxrrjZmarrayrrr open_memmapsL2            rran out of datacCst}xTy8||t|}||7}t|dksPWqtjk rVYqXqWt||krd}t|||t|fn|SdS)a+ Read from file-like object until size bytes are read. Raises ValueError if not EOF is encountered before size bytes are read. Non-blocking objects only supported if they derive from io objects. Required as e.g. ZipExtFile in python 2.6 can return less data than requested. rz)EOF: reading %s, expected %d bytes got %dN)rrOr=rMBlockingIOErrorr)r"sizeZerror_templaterrrrrrrs   r)N)NTN)TN)rNNFN)r)(__doc__Z __future__rrrrbrrMrlZnumpy.lib.utilsrZ numpy.compatrrrr r rrrZcPicklerr=rrrrr#r(r0rDrErFrHrIr>rGrzrrrrrrrs<     2 "&6 N [ n