o
    "j6                     @   s@   d dl Z d dlmZmZ d dlZd dlZdZdd Zdd Z	dS )    N)MappingSequenceZ_paddle_field_c                    sX    fdd t | tsg } | g|g d\}}||d fS g } | |g d\}}||fS )a%  
    For lod_blocking_queue only receive tensor array, flatten batch
    data, extract numpy.array data out as a list of numpy.array to
    send to lod_blocking_queue, and save the batch data structure
    such as fields in other types (str, int, etc) or key-value map
    of dictionaries
    c                    s  t | trk| D ]_}t |tjtjtjjjjfr*|	t
 |  |	| |d7 }qt |tttjfr9|	| qt |trM ||g |\}}|	| qt |tra ||i |\}}|	| q|	| q||fS t | tr|  D ]\\}}t |tjtjtjjjjfrt
 | ||< |	| |d7 }qtt |tttjfr|||< qtt |tr ||g |\}}|||< qtt |tr̈ ||i |\}}|||< qt|||< qt||fS tdt|  )N   wrong flat data type: )
isinstancer   npZndarraypaddleZTensorbasecoreeagerappendFIELD_PREFIXstrbytesnumbersNumberr   items	TypeErrortype)batch
flat_batch	structure	field_idxfieldZfield_structk_flatten Z/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/io/dataloader/flat.pyr   "   s^   













z _flatten_batch.<locals>._flattenr   )r   r   )r   r   r   _r   r   r   _flatten_batch   s   	
5r    c                    s    fdd t tsJ dtdkr|S t |ttfr2|t d ks.J d| d S  |d}|d tksCJ d|S )z
    After reading list of Tensor data from lod_blocking_queue outputs,
    use this function to restore the batch data structrue, replace
    :attr:`_paddle_field_x` with data from flat_batch
    c                    sb  t | trTt| D ]H\}}t |tr9|tr9t|td}t||}| d us.J d| | |< d |< q	t |tt	t
jfrCq	t |ttfrQ | | |}q	|S t | tr|  D ]H\}}t |tr|trt|td}t||}| d usJ d| | |< d |< q]t |tt	t
jfrq]t |ttfr | | |}q]|S tdt|  )N zflat_batch[{}] parsed repeatlyr   )r   r   	enumerater   
startswithr   intreplacemaxr   r   r   r   r   r   r   )r   r   ir   Zcur_field_idxr   _restorer   r   r   r)   g   sB   





z _restore_batch.<locals>._restorez!flat_batch is not a list or tupler   zinvalid structure: r   zTensor parse incomplete)r   r   lenr   r   r   )r   r   r   r   r(   r   _restore_batch`   s   "
r+   )
r   collections.abcr   r   numpyr   r   r   r    r+   r   r   r   r   <module>   s   G