3
I4W^®  ã               @   sˆ   d dl mZ d dl mZ d dl mZ d dl mZ d dlmZmZmZm	Z
 d dlmZ d dlmZ d dlmZ d d	lZG d
d„ deƒZd	S )é    )Úabsolute_import)Údivision)Úprint_function)Úunicode_literals)ÚcoreÚscopeÚ	workspaceÚ_import_c_extension)ÚReader)ÚDataset)Úfrom_column_listNc                   s\   e Zd ZdZd‡ fdd„	Zdd„ Zdd	d
„Zdd„ Zdd„ Zdd„ Z	dd„ Z
dd„ Z‡  ZS )ÚDBFileReaderZdb_file_readerNéd   Fc                s    |d k	st dƒ‚|tjƒ ks2t dj|tjƒ d�ƒ‚tjj|ƒ| _|| _|p\dj| j	ƒ | j
d�| _|| _|| _tt| ƒj| j|ƒƒ t| j| jd ƒ| _d | _d S )Nzdb_path can't be None.zMdb_type [{db_type}] is not available. 
Choose one of these: {registered_dbs}.)Údb_typeÚregistered_dbsz{db_name}_{default_name_suffix})Zdb_nameÚdefault_name_suffixZ_dataset)ÚAssertionErrorÚCr   ÚformatÚosÚpathÚ
expanduserÚdb_pathr   Ú_extract_db_name_from_db_pathr   ÚnameÚ
batch_sizeÚ	loop_overÚsuperr   Ú__init__Ú_init_reader_schemar   Z_schemaÚdsÚ	ds_reader)Úselfr   r   r   r   r   Úfield_names)Ú	__class__© ú=/tmp/pip-build-e66j97b2/torch/caffe2/python/db_file_reader.pyr   )   s    	zDBFileReader.__init__c             C   s   |p| j ƒ d S )NZ_db_file_reader)r   )r"   r   r%   r%   r&   Ú
_init_nameI   s    
zDBFileReader._init_namec                s’   |rt |ƒS tjj| jƒs,tdj| jd�ƒ‚tj| j	ƒ� t
jƒ ‰ W dQ R X tjtjdg g d| j| jdˆ d�ƒ ‡ fdd„tjƒ D ƒ}t |ƒ}|S )	a_  Restore a reader schema from the DB file.

        If `field_names` given, restore scheme according to it.

        Overwise, loade blobs from the DB file into the workspace,
        and restore schema from these blob names.
        It is also assumed that:
        1). Each field of the schema have corresponding blobs
            stored in the DB file.
        2). Each blob loaded from the DB file corresponds to
            a field of the schema.
        3). field_names in the original schema are in alphabetic order,
            since blob names loaded to the workspace from the DB file
            will be in alphabetic order.

        Load a set of blobs from a DB file. From names of these blobs,
        restore the DB file schema using `from_column_list(...)`.

        Returns:
            schema: schema.Struct. Used in Reader.__init__(...).
        z"db_path [{db_path}] does not exist)r   NÚLoadT)Úabsolute_pathÚdbr   Zload_allZ
add_prefixc                s&   g | ]}|j ˆ ƒr|tˆ ƒd … ‘qS )N)Ú
startswithÚlen)Ú.0Z	blob_name)Úblob_prefixr%   r&   ú
<listcomp>x   s    z4DBFileReader._init_reader_schema.<locals>.<listcomp>)r   r   r   Úexistsr   r   r   r   Ú	NameScoper   r   ZCurrentNameScoper   ZRunOperatorOnceZCreateOperatorr   ZBlobs)r"   r#   Z	col_namesZschemar%   )r.   r&   r   M   s&    
z DBFileReader._init_reader_schemac             C   s^   | j r| j j||ƒ nD| j|ƒ | j|ƒ | jj|| j| jd�| _ | j j|ƒ | j j	|ƒ dS )ab  From the Dataset, create a _DatasetReader and setup a init_net.

        Make sure the _init_field_blobs_as_empty(...) is only called once.

        Because the underlying NewRecord(...) creats blobs by calling
        NextScopedBlob(...), so that references to previously-initiated
        empty blobs will be lost, causing accessibility issue.
        )r   r   N)
r!   Úsetup_exÚ_init_field_blobs_as_emptyÚ_feed_field_blobs_from_db_filer    Zrandom_readerr   r   Zsort_and_shuffleZcomputeoffset)r"   Úinit_netZ
finish_netr%   r%   r&   r2   ~   s    	

zDBFileReader.setup_exc             C   s   | j stdƒ‚| j j|ƒS )Nzsetup_ex must be called first)r!   r   Úread)r"   Zread_netr%   r%   r&   r6   ”   s    zDBFileReader.readc          	   C   s(   t j| jƒ� | jj|ƒ W dQ R X dS )z:Initialize dataset field blobs by creating an empty recordN)r   r1   r   r    Z
init_empty)r"   r5   r%   r%   r&   r3   ˜   s    z'DBFileReader._init_field_blobs_as_emptyc             C   sJ   t jj| jƒs tdj| jd�ƒ‚|jg | jjƒ | j| j	d| jj
ƒ d� dS )z=Load from the DB file at db_path and feed dataset field blobsz"db_path [{db_path}] does not exist)r   T)r*   r   r)   Zsource_blob_namesN)r   r   r0   r   r   r   r(   r    Z	get_blobsr   r#   )r"   Únetr%   r%   r&   r4   �   s    z+DBFileReader._feed_field_blobs_from_db_filec             C   s   t jj| jƒjddƒd S )z±Extract DB name from DB path

            E.g. given self.db_path=`/tmp/sample.db`,
            it returns `sample`.

            Returns:
                db_name: str.
        Ú.é   r   )r   r   Úbasenamer   Úrsplit)r"   r%   r%   r&   r   ª   s    	z*DBFileReader._extract_db_name_from_db_path)Nr   FN)N)Ú__name__Ú
__module__Ú__qualname__r   r   r'   r   r2   r6   r3   r4   r   Ú__classcell__r%   r%   )r$   r&   r      s      
1r   )Ú
__future__r   r   r   r   Zcaffe2.pythonr   r   r   r	   r   Zcaffe2.python.dataior
   Zcaffe2.python.datasetr   Zcaffe2.python.schemar   r   r   r%   r%   r%   r&   Ú<module>   s   