σ
ίΖχXc           @@ s   d  Z  d d l m Z d d l m Z d d l Z d d l Z d d l m Z y d d l	 Z	 Wn e
 k
 rw d Z	 n Xd e f d     YZ d   Z d S(	   s   Utilities related to disk I/O.i    (   t   absolute_import(   t   print_functionN(   t   defaultdictt
   HDF5Matrixc           B@ sM   e  Z d  Z e e  Z d d d d  Z d   Z d   Z	 e
 d    Z RS(   s  Representation of HDF5 dataset to be used instead of a Numpy array.

    # Example

    ```python
        x_data = HDF5Matrix('input/file.hdf5', 'data')
        model.predict(x_data)
    ```

    Providing `start` and `end` allows use of a slice of the dataset.

    Optionally, a normalizer function (or lambda) can be given. This will
    be called on every slice of data retrieved.

    # Arguments
        datapath: string, path to a HDF5 file
        dataset: string, name of the HDF5 dataset in the file specified
            in datapath
        start: int, start of desired slice of the specified dataset
        end: int, end of desired slice of the specified dataset
        normalizer: function to be called on data when retrieved

    # Returns
        An array-like HDF5 dataset.
    i    c         C@ s°   t  d  k r t d   n  | t |  j j    k rU t  j |  } | |  j | <n |  j | } | | |  _ | |  _ | d  k r |  j j	 d |  _
 n	 | |  _
 | |  _ d  S(   Ns7   The use of HDF5Matrix requires HDF5 and h5py installed.i    (   t   h5pyt   Nonet   ImportErrort   listt   refst   keyst   Filet   datat   startt   shapet   endt
   normalizer(   t   selft   datapatht   datasetR   R   R   t   f(    (    s3   /tmp/pip-build-X4mzal/keras/keras/utils/io_utils.pyt   __init__+   s    		c         C@ s   |  j  |  j S(   N(   R   R   (   R   (    (    s3   /tmp/pip-build-X4mzal/keras/keras/utils/io_utils.pyt   __len__=   s    c         C@ st  t  | t  rW | j |  j |  j k rN t | j |  j | j |  j  } qBt  nλ t  | t  r | |  j |  j k  r | |  j } qBt  n­ t  | t j  rε t j	 |  |  j |  j k  rά |  j | j
   } qBt  n] t  | t  r<t	 |  |  j |  j k  r3g  | D] } | |  j ^ q} qBt  n t  |  j d  k	 re|  j |  j |  S|  j | Sd  S(   N(   t
   isinstancet   slicet   stopR   R   t
   IndexErrort   intt   npt   ndarrayt   maxt   tolistR   R   R   R   (   R   t   keyt   idxt   x(    (    s3   /tmp/pip-build-X4mzal/keras/keras/utils/io_utils.pyt   __getitem__@   s(    &			#	c         C@ s   |  j  |  j f |  j j d S(   Ni   (   R   R   R   R   (   R   (    (    s3   /tmp/pip-build-X4mzal/keras/keras/utils/io_utils.pyR   \   s    N(   t   __name__t
   __module__t   __doc__R   R   R   R   R   R   R"   t   propertyR   (    (    (    s3   /tmp/pip-build-X4mzal/keras/keras/utils/io_utils.pyR      s   		c         C@ so   t  } t j d  d k r" t } n  | d |   } x | d	 k rP | d  } q5 W| d k ra t St d  t S(
   sΡ   Produces a prompt asking about overwriting a file.

    # Arguments
        filepath: the path to the file to be overwritten.

    # Returns
        True if we can proceed with overwrite, False otherwise.
    i   i   s.   [WARNING] %s already exists - overwrite? [y/n]t   yt   ns&   Enter "y" (overwrite) or "n" (cancel).s'   [TIP] Next time specify overwrite=True!(   i   i   (   R'   R(   (   t   inputt   syst   version_infot	   raw_inputt   Falset   printt   True(   t   filepatht	   get_inputt	   overwrite(    (    s3   /tmp/pip-build-X4mzal/keras/keras/utils/io_utils.pyt   ask_to_proceed_with_overwritea   s    		

(   R%   t
   __future__R    R   t   numpyR   R*   t   collectionsR   R   R   R   t   objectR   R3   (    (    (    s3   /tmp/pip-build-X4mzal/keras/keras/utils/io_utils.pyt   <module>   s   
R