o
    e+                     @   sz  d Z ddlmZmZmZ ddlZddlZddlm	Z
 ddlmZ ddlmZ d	d
 Zdd Zdd Zdd Zdd Zdd Zdd ZdOddZdOddZdOddZdOddZdOdd ZdOd!d"Z	dOd#d$Zd%d& Zd'd( Zd)d* Zd+d, Z d-d. Z!d/d0 Z"d1d2 Z#d3d4 Z$d5d6 Z%d7d8 Z&d9d: Z'd;d< Z(d=d> Z)d?d@ Z*dAdB Z+dCdD Z,dEdF Z-dGdH Z.dIdJ Z/dOdKdLZ0dMdN Z1dS )PzJFunctions dealing with normalization of user input data to imgaug classes.    )print_functiondivisionabsolute_importN   )imgaug)dtypes   )IAugmentablec                 C   s   | d u rd S t | r!| jdv sJ d| j| jf dd | D S t| ts/J dt| f g }| D ]!}t|tr@|| q3t |sNJ dt|f ||j q3|S )N)      zZExpected array 'shapes' to be 3- or 4-dimensional, got %d dimensions and shape %s instead.c                 S      g | ]}|j qS  shape).0imager   r   QD:\Projects\ConvertPro\env\Lib\site-packages\imgaug/augmentables/normalization.py
<listcomp>       z&_preprocess_shapes.<locals>.<listcomp>zEExpected 'shapes' to be None or ndarray or list, got type %s instead.zUExpected each entry in list 'shapes' to be either a tuple or an ndarray, got type %s.)	iais_np_arrayndimr   
isinstancelisttypetupleappend)shapesresultshape_ir   r   r   _preprocess_shapes   s2   


r    c                 C   sB   | d u rt d|||f t| |krt d|||t| f d S )Na   Tried to convert data of form '%s' to '%s'. This required %d corresponding image shapes, but argument 'shapes' was set to None. This can happen e.g. if no images were provided in a Batch, as these would usually be used to automatically derive image shapes.a  Tried to convert data of form '%s' to '%s'. This required exactly %d corresponding image shapes, but instead %d were provided. This can happen e.g. if more images were provided than corresponding augmentables, e.g. 10 images but only 5 segmentation maps. It can also happen if there was a misunderstanding about how an augmentable input would be parsed. E.g. if a list of N (x,y)-tuples was provided as keypoints and the expectation was that this would be parsed as one keypoint per image for N images, but instead it was parsed as N keypoints on 1 image (i.e. 'shapes' would have to contain 1 shape, but N would be provided). To avoid this, it is recommended to provide imgaug standard classes, e.g. KeypointsOnImage for keypoints instead of lists of tuples.)
ValueErrorlen)r   n
from_ntypeto_ntyper   r   r   _assert_exactly_n_shapes$   s   r&   c                 C   s$   | j |krtd|||| j f d S )NzTried to convert an array to list of %s. Expected that array to be of shape %s, i.e. %d-dimensional, but got %d dimensions instead.)r   r!   )arrr   	shape_strr%   r   r   r   _assert_single_array_ndim?   s   

r)   c                    s   d}t | dkrg }nt| d r| }n	d}dd | D }t fdd|D rEtd||| dd	d |D dd
d |D f d S )Niterabler   iterable of iterablec                 S      g | ]	}|D ]}|qqS r   r   r   Zarrs_sublistr'   r   r   r   r   T       z,_assert_many_arrays_ndim.<locals>.<listcomp>c                    s   g | ]}|j  kqS r   r   r   r'   r/   r   r   r   V       zTried to convert an %s of arrays to a list of %s. Expected each array to be of shape %s, i.e. to be %d-dimensional, but got dimensions %s instead (array shapes: %s)., c                 S      g | ]}t |jqS r   )strr   r0   r   r   r   r   ]   r1   c                 S   r3   r   r4   r   r0   r   r   r   r   ^   r1   r"   r   r   anyr!   join)arrsr   r(   r%   iterable_type_str	arrs_flatr   r/   r   _assert_many_arrays_ndimH   s"   r<   c                 C   s2   | j d |krtd||| j d t| j f d S )NzTried to convert an array to a list of %s. Expected the array's last dimension to have size %d, but got %d instead (array shape: %s).)r   r!   r4   )r'   sizer%   r   r   r   %_assert_single_array_last_dim_exactlya   s   r?   c              
      s   d}t | dkrg }nt| d r| }n	d}dd | D }t fdd|D rDtd|| dd	d |D dd
d |D f d S )Nr*   r   r+   c                 S   r,   r   r   r-   r   r   r   r   v   r.   z8_assert_many_arrays_last_dim_exactly.<locals>.<listcomp>c                    s   g | ]	}|j d   kqS r=   r   r0   r>   r   r   r   x   r.   zTried to convert an %s of array to a list of %s. Expected the arrays' last dimensions to have size %d, but got %s instead (array shapes: %s).r2   c                 S   s   g | ]	}t |jd  qS r@   r5   r0   r   r   r   r   ~   r.   c                 S   r3   r   r5   r0   r   r   r   r      r1   r6   )r9   r>   r%   r:   r;   r   rA   r   $_assert_many_arrays_last_dim_exactlyj   s"   rB   c                 C   s   | d u rd S t | r'| jdkr| tjdtjf S | jdkr%| dtjf S | S t | rWg }| D ]$}|jdv s?J d|jf |jdkrO||dtjf  q0|| q0|S tdt| f )Nr   .r
   )r   r
   zGot a list of arrays as argument 'images'. Expected each array in that list to have 2 or 3 dimensions, i.e. shape (H,W) or (H,W,C). Got %d dimensions instead.zhExpected argument 'images' to be any of the following: None or array or iterable of array. Got type: %s.)	r   r   r   npZnewaxisis_iterabler   r!   r   )imagesr   r   r   r   r   normalize_images   s4   




rF   c                    s   ddl m  t|}t| }tjt|d|d}|dkrd S |dkr;t| ddd	 |t| d
  fddt	| |D S |d	krB| gS |dkrHd S |dkrft
| ddd	 |t| d
  fddt	| |D S |dksqJ d|f | S )Nr   HeatmapsOnImagezList[HeatmapsOnImage]r$   r%   r   Nonearray[float]r   z	(N,H,W,C)rH   r#   c                       g | ]
\}} ||d qS r   r   r   attr_ir   rG   r   r   r          z&normalize_heatmaps.<locals>.<listcomp>iterable[empty]iterable-array[float]r
   z(H,W,C)c                    rM   rN   r   rO   rG   r   r   r      rQ   iterable-HeatmapsOnImage$Got unknown normalization type '%s'.)Zimgaug.augmentables.heatmapsrH   r    estimate_heatmaps_norm_type	functoolspartialr&   r)   r"   zipr<   inputsr   ntype _assert_exactly_n_shapes_partialr   rG   r   normalize_heatmaps   s:   


r^   c                    s&  ddl m  t|}t| }tjt|d|d}|dkrd S |dv rKt| ddd	 |t| d
 |dkr? fddt	| |D S  fddt	| |D S |d	krR| gS |dkrXd S |dv rt
| ddd	 |t| d
 |dkrz fddt	| |D S  fddt	| |D S |dksJ d|f | S )Nr   SegmentationMapsOnImagezList[SegmentationMapsOnImage]rI   rJ   
array[int]array[uint]array[bool]r   z(N,H,W,#SegmapsPerImage)r`   rL   rd   c                    rM   rN   r   r   rP   r   r_   r   r   r      rQ   z/normalize_segmentation_maps.<locals>.<listcomp>c                    rM   rN   r   re   r_   r   r   r      rQ   rR   iterable-array[int]iterable-array[uint]iterable-array[bool]r
   z(H,W,#SegmapsPerImage)ri   c                    rM   rN   r   re   r_   r   r   r      rQ   c                    rM   rN   r   re   r_   r   r   r      rQ    iterable-SegmentationMapsOnImagerU   )Zimgaug.augmentables.segmapsr`   r    estimate_segmaps_norm_typerW   rX   r&   r)   r"   rY   r<   rZ   r   r_   r   normalize_segmentation_maps   sT   




rl   c                    s  ddl m m t|}t| }tjt|d|d}|dkr| S |dv rCt| ddd	 t	| d
d	 |t
| d fddt| |D S |dkr^|dd  | d | d dg|d dgS |dkrq|dd | g|d dgS |d	krx| gS |dkr~d S |dv rt| d
dd	 t| d
d	 |t
| d fddt| |D S |dkr|dd  fdd| D |d dgS |dkr|dd | |d dgS |dkr| S |dkrd S |dkr|t
| d fddt| |D S |dksJ d|f |t
| d fd dt| |D S )!Nr   KeypointKeypointsOnImagezList[KeypointsOnImage]rI   rJ   rK   rb   rc   r
   z(N,K,2)ro   r   rL   c                       g | ]\}} j ||d qS rN   from_xy_arrayre   ro   r   r   r          z'normalize_keypoints.<locals>.<listcomp>tuple[number,size=2]r   xyr   rn   rR   rS   rg   rh   z(K,2)c                    rq   rN   rr   re   rt   r   r   r     ru   iterable-tuple[number,size=2]c                    rM   )rw   r   )r   rx   ry   rn   r   r   r         iterable-Keypointiterable-KeypointsOnImageiterable-iterable[empty]&iterable-iterable-tuple[number,size=2]c                    *   g | ]\}} j tj|tjd |dqS )dtyper   )rs   rC   arrayfloat32re   rt   r   r   r   "      iterable-iterable-KeypointrU   c                    rM   rN   r   re   rt   r   r   r   -  rQ   )imgaug.augmentables.kpsrn   ro   r    estimate_keypoints_norm_typerW   rX   r&   r)   r?   r"   rY   r<   rB   rZ   r   rm   r   normalize_keypoints   sx   








r   c                    s*  ddl m m t|}t| }tjt|d|d}|dkrd S |dv rCt| ddd	 t	| d
d	 |t
| d fddt| |D S |dkrd|dd  | d | d | d | d dg|d dgS |dkrw|dd | g|d dgS |d	kr~| gS |dkrd S |dv rt| ddd	 t| d
d	 |t
| d fddt| |D S |dkr|dd  fdd| D |d dgS |dkr|dd | |d dgS |dkr| S |dkrd S |dkr|t
| d fddt| |D S |dksJ d |f |t
| d fd!dt| |D S )"Nr   BoundingBoxBoundingBoxesOnImagezList[BoundingBoxesOnImage]rI   rJ   rp   r
   z(N,B,4)r   r   rL   c                    rq   rN   from_xyxy_arrayre   r   r   r   r   D  ru   z,normalize_bounding_boxes.<locals>.<listcomp>tuple[number,size=4]r   r   x1y1x2y2r   r   rR   rz   z(B,4)c                    rq   rN   r   re   r   r   r   r   _  ru   iterable-tuple[number,size=4]c                    s$   g | ]\}}}} ||||d qS )r   r   )r   r   r   r   r   )r   r   r   r   h  s    
iterable-BoundingBoxiterable-BoundingBoxesOnImager   &iterable-iterable-tuple[number,size=4]c                    r   r   )r   rC   r   r   re   r   r   r   r   u  r   iterable-iterable-BoundingBoxrU   c                    rM   rN   r   re   r   r   r   r     rQ   )Zimgaug.augmentables.bbsr   r   r    !estimate_bounding_boxes_norm_typerW   rX   r&   r)   r?   r"   rY   r<   rB   rZ   r   r   r   normalize_bounding_boxes2  s   








r   c                 C   (   ddl m}m} t||ddgt| |dS )Nr   )PolygonPolygonsOnImagez#polys#points
cls_singlecls_oi
axis_namesestimate_ntype_funcr[   r   )Zimgaug.augmentables.polysr   r   $_normalize_polygons_and_line_stringsestimate_polygons_norm_type)r[   r   r   r   r   r   r   normalize_polygons     r   c                 C   r   )Nr   )
LineStringLineStringsOnImagez#linesr   r   )Zimgaug.augmentables.linesr   r   r   estimate_line_strings_norm_type)r[   r   r   r   r   r   r   normalize_line_strings  r   r   c                    s  j } j }d|d |d f }d|d |d f }	d|d f }
t|}||}tjt|d|f |d}|dkr;d S |d	v r`t|d
|| t|d| |t|d  fddt||D S ||krs|dd  |g|d dgS ||krz|gS |dkrd S |dv rt	|d|	| t
|d| |t|d  fddt||D S |dkr|dd  |g|d dgS |dkr|dd  |g|d dgS |d|f kr|dd  ||d dgS |d|f kr|S |dkrd S |dv rt	|d|
| t
|d| |t|d  fddt||D S |dkr3|dd  fdd|D |d dgS |dkrM|dd  fdd|D |d dgS |d|f krh|t|d  fddt||D S |d krod S |d!v s{J d"|f |t|d  fd#dt||D S )$Nz(N,%s,%s,2)r   r   z	(%s,%s,2)z(%s,2)zList[%s]rI   rJ   rp   r   r   rL   c                    *   g | ]\}} fd d|D |dqS )c                       g | ]} |qS r   r   r   Zpointsr   r   r   r         C_normalize_polygons_and_line_strings.<locals>.<listcomp>.<listcomp>r   r   re   r   r   r   r   r         z8_normalize_polygons_and_line_strings.<locals>.<listcomp>r   rR   rz   r
   c                    r   )c                    r   r   r   r   r   r   r   r     r   r   r   r   re   r   r   r   r     s    r{   r~   iterable-%sr   iterable-iterable-array[float]iterable-iterable-array[int]iterable-iterable-array[uint]c                    r   )c                    r   r   r   r   r   r   r   r     r   r   r   r   re   r   r   r   r     r   r   c                    r   r   r   r   rP   r   r   r   r     r   r   c                    r   r   r   r   r   r   r   r     r   iterable-iterable-%sc                    rM   rN   r   re   )r   r   r   r     s    
!iterable-iterable-iterable[empty])/iterable-iterable-iterable-tuple[number,size=2]#iterable-iterable-iterable-KeypointrU   c                    r   )c                    r   r   r   r   r   r   r   r     r   r   r   r   re   r   r   r   r     r   )__name__r    rW   rX   r&   r)   r?   r"   rY   r<   rB   )r   r   r   r   r[   r   cls_single_namecls_oi_nameZaxis_names_4_strZaxis_names_3_strZaxis_names_2_strr\   r]   r   r   r   r     s   











r   c                 C   s  |d u r| d u sJ dt | f d S t|r|t| s/|jdkr(t| |gS t| t|S |jdkr^| jd dksGJ d| jd | jf | jd dksZJ d| jd | jf | d S |jdkrz| jd dksvJ d	| jd | jf | d
 S | S t|rg }t| |D ]?\}}|jdkr|jd dksJ d|jd |jf |	|d d d d df  q|jdksJ d|j|jf |	| q|S t
dt |f )NzmExpected (normalized) 'images' to be None due to (unnormalized) 'images_old' being None. Got type %s instead.r   r   r   zExpected normalized images of shape (N,H,W,C) to have N=1 due to the unnormalized images being a single 2D image. Got instead N=%d and shape %s.r
   zExpected normalized images of shape (N,H,W,C) to have C=1 due to the unnormalized images being a single 2D image. Got instead C=%d and shape %s.)r   .r   zExpected normalized images of shape (N,H,W,C) to have C=1 due to unnormalized images being a single 3D image. Got instead C=%d and shape %s).r   zExpected each image of shape (H,W,C) to have C=1 due to the corresponding unnormalized image being a 2D image. Got instead C=%d and shape %s.zMExpected 'image_old' to be three-dimensional, got %d dimensions and shape %s.zlExpected argument 'images_old' to be any of the following: None or array or iterable of array. Got type: %s.)r   r   r   r   invert_normalize_imagesr   r   rD   rY   r   r!   )rE   Z
images_oldr   r   Z	image_oldr   r   r   r     sx   







r   c                    s  t |}|dkr| d u sJ dt| f | S |dkr>t| |jd ks1J dt| |jd f |j tdd | D  S |dkrVt| d	ksRJ dt| d	f | d S |d
kri| d u sgJ dt| f g S |dkrt|\}}}|j  fdd| D S |dksJ d|f | S )NrJ   znExpected (normalized) 'heatmaps' to be None due (unnormalized) 'heatmaps_old' being None. Got type %s instead.rK   r   zfExpected as many heatmaps after normalization as before normalization. Got %d (after) and %d (before).c                 S   r   r   )arr_0to1r   Zhm_ir   r   r   r   \  r   z-invert_normalize_heatmaps.<locals>.<listcomp>rH   r   rR   zExpected heatmaps after normalization to be None, due to the heatmaps before normalization being an empty iterable. Got type %s instead.rS   c                    s   g | ]}t |j qS r   )restore_dtype_and_merger   r   input_dtyper   r   r   m  s    rT   rU   )rV   r   r"   r   r   r   find_first_nonempty)heatmapsZheatmaps_oldr\   nonempty_r   r   r   invert_normalize_heatmapsM  s\   



r   c                    s  t |}|dkr| d u sJ dt| f | S |dv r>t| |jd ks1J dt| |jd f |j tdd | D  S |dkrVt| d	ksRJ dt| d	f | d S |d
kri| d u sgJ dt| f g S |dv rt|\}}}|j  fdd| D S |dksJ d|f | S )NrJ   zExpected (normalized) 'segmentation_maps' to be None due (unnormalized) 'segmentation_maps_old' being None. Got type %s instead.ra   r   zoExpected as many segmentation maps after normalization as before normalization. Got %d (after) and %d (before).c                 S      g | ]}|  qS r   )get_arrr   Zsegmap_ir   r   r   r     r   z6invert_normalize_segmentation_maps.<locals>.<listcomp>r`   r   rR   zExpected segmentation maps after normalization to be None, due to the segmentation maps before normalization being an empty iterable. Got type %s instead.rf   c                       g | ]	}t |  qS r   )r   r   r   r   r   r   r         rj   rU   )rk   r   r"   r   r   r   r   )segmentation_mapsZsegmentation_maps_oldr\   r   r   r   r   r   "invert_normalize_segmentation_mapsu  sZ   



r   c                    s  t |}|dkr| d u sJ dt| f | S |dv r7t| dks*J dt| f |j tdd | D  S |dkrnt| dksJJ d	t| f t| d
 jdks^J dt| d
 j | d
 jd
 j| d
 jd
 jfS |dkrt| dksJ dt| f t| d
 jdksJ dt| d
 j | d
 jd
 S |dkrt| dksJ dt| f | d
 S |dkr| d u sJ dt| f g S |dv rt|\}}}|j  fdd| D S |dkrt| dksJ dt| f dd | d
 jD S |dkrt| dksJ dt| f | d
 jS |dkr| S |dkr4| d u s.J dt| f |d d  S |dkr@dd | D S |dksLJ d |f d!d | D S )"NrJ   zpExpected (normalized) 'keypoints' to be None due (unnormalized) 'keypoints_old' being None. Got type %s instead.rp   r   zExpected a single KeypointsOnImage instance after normalization due to getting a single ndarray before normalization. Got %d instances instead.c                 S   r   r   )to_xy_arrayr   Zkpsoir   r   r   r     r   z.invert_normalize_keypoints.<locals>.<listcomp>rv   zExpected a single KeypointsOnImage instance after normalization due to getting a single (x,y) tuple before normalization. Got %d instances instead.r   zExpected a KeypointsOnImage instance containing a single Keypoint after normalization due to getting a single (x,y) tuple before normalization. Got %d keypoints instead.rn   zExpected a single KeypointsOnImage instance after normalization due to getting a single Keypoint before normalization. Got %d instances instead.zExpected a KeypointsOnImage instance containing a single Keypoint after normalization due to getting a single Keypoint before normalization. Got %d keypoints instead.ro   zExpected a single KeypointsOnImage instance after normalization due to getting a single KeypointsOnImage before normalization. Got %d instances instead.rR   zExpected keypoints after normalization to be None, due to the keypoints before normalization being an empty iterable. Got type %s instead.rz   c                    r   r   )r   r   )r   Zkps_ir   r   r   r         r{   zExpected a single KeypointsOnImage instance after normalization due to getting an iterable of (x,y) tuples before normalization. Got %d instances instead.c                 S      g | ]}|j |jfqS r   rw   r   kpr   r   r   r     s    r~   zExpected a single KeypointsOnImage instance after normalization due to getting an iterable of Keypoint before normalization. Got %d instances instead.r   r   zExpected keypoints after normalization to be None, due to the keypoints before normalization being an empty iterable of iterables. Got type %s instead.r   c                 S      g | ]
}d d |j D qS )c                 S   r   r   rw   r   r   r   r   r     s    z9invert_normalize_keypoints.<locals>.<listcomp>.<listcomp>	keypointsr   r   r   r   r         r   rU   c                 S      g | ]	}|j d d  qS Nr   r   r   r   r   r     r.   )	r   r   r"   r   r   r   rx   ry   r   )r   Zkeypoints_oldr\   r   r   r   r   r   invert_normalize_keypoints  s   







r   c                    s  t |}|dkr| d u sJ dt| f | S |dv r7t| dks*J dt| f |j tdd | D  S |dkrot| dksJJ d	t| f t| d
 jdks^J dt| d
 j | d
 jd
 }|j|j|j|j	fS |dkrt| dksJ dt| f t| d
 jdksJ dt| d
 j | d
 jd
 S |dkrt| dksJ dt| f | d
 S |dkr| d u sJ dt| f g S |dv rt
|\}}}|j  fdd| D S |dkrt| dksJ dt| f dd | d
 jD S |dkrt| dksJ dt| f | d
 jS |dkr| S |dkr5| d u s/J dt| f |d d  S |dkrAdd | D S |dksMJ d |f d!d | D S )"NrJ   zzExpected (normalized) 'bounding_boxes' to be None due (unnormalized) 'bounding_boxes_old' being None. Got type %s instead.rp   r   zExpected a single BoundingBoxesOnImage instance after normalization due to getting a single ndarray before normalization. Got %d instances instead.c                 S   r   r   )to_xyxy_arrayr   Zbbsoir   r   r   r     s    z3invert_normalize_bounding_boxes.<locals>.<listcomp>r   zExpected a single BoundingBoxesOnImage instance after normalization due to getting a single (x1,y1,x2,y2) tuple before normalization. Got %d instances instead.r   zExpected a BoundingBoxesOnImage instance containing a single BoundingBox after normalization due to getting a single (x1,y1,x2,y2) tuple before normalization. Got %d bounding boxes instead.r   zExpected a single BoundingBoxesOnImage instance after normalization due to getting a single BoundingBox before normalization. Got %d instances instead.zExpected a BoundingBoxesOnImage instance containing a single BoundingBox after normalization due to getting a single BoundingBox before normalization. Got %d bounding boxes instead.r   zExpected a single BoundingBoxesOnImage instance after normalization due to getting a single BoundingBoxesOnImage before normalization. Got %d instances instead.rR   zExpected bounding boxes after normalization to be None, due to the bounding boxes before normalization being an empty iterable. Got type %s instead.rz   c                    r   r   )r   r   r   r   r   r   r   :  r   r   zExpected a single BoundingBoxesOnImage instance after normalization due to getting a an iterable of (x1,y1,x2,y2) tuples before normalization. Got %d instances instead.c                 S       g | ]}|j |j|j|jfqS r   r   r   bbr   r   r   r   C      r   zExpected a single BoundingBoxesOnImage instance after normalization due to getting an iterable of BoundingBox before normalization. Got %d instances instead.r   r   zExpected bounding boxes after normalization to be None, due to the bounding boxes before normalization being an empty iterable of iterables. Got type %s instead.r   c                 S   r   )c                 S   r   r   r   r   r   r   r   r   X  s     z>invert_normalize_bounding_boxes.<locals>.<listcomp>.<listcomp>bounding_boxesr   r   r   r   r   W  r   r   rU   c                 S   r   r   r   r   r   r   r   r   ]  r.   )estimate_normalization_typer   r"   r   r   r   r   r   r   r   r   )r   Zbounding_boxes_oldr\   r   r   r   r   r   r   invert_normalize_bounding_boxes  s   







r   c              	   C      t | |tdddd dd S )Nr   r   c                 S      | j S r   polygons)Zpsoir   r   r   <lambda>e      z+invert_normalize_polygons.<locals>.<lambda>c                 S   r   r   )Zexterior)Zpolyr   r   r   r   f  r   )+_invert_normalize_polygons_and_line_stringsr   )r   Zpolygons_oldr   r   r   invert_normalize_polygons`     r   c              	   C   r   )Nr   r   c                 S   r   r   line_strings)Zlsoir   r   r   r   n  r   z/invert_normalize_line_strings.<locals>.<lambda>c                 S   r   r   )coords)Zlsr   r   r   r   o  r   )r   r   )r   Zline_strings_oldr   r   r   invert_normalize_line_stringsi  r   r   c           
   	      sL  ddl m  ||}|dkr| d u sJ dt| f | S |dv r1|jtfdd| D S ||krht| dksFJ d	||t| f t| d dks`J d
|||t| d f | d d S ||krt| dks}J d	||t| f | d S |dkr| d u sJ dt| f g S |dv rt|\}}	}	|jfdd| D S |dkrt| dksJ d|t| f t| d dksJ d||||t| f dd | d d D S |dkr%t| dksJ d|t| f t| d dksJ d||||t| f  fdd| d d D S |d|f kr`t| dks?J d||t| f t| d t|ksZJ d|||||t| f | d S |d|f krj| S |dkr| d u s}J dt| f |d d  S |dv rt|\}}	}	|jfdd| D S |dkrt| dksJ d|t| f fd d| d D S |d!krt| dksJ d"|t| f  fd#d| d D S |d$|f krfd%d| D S |d&kr |d d  S |d'krfd(d| D S |d)ksJ d*|f  fd+d| D S ),Nr   r|   rJ   zExpected (normalized) polygons/line strings to be None due (unnormalized) polygons/line strings being None. Got type %s instead.rp   c                    "   g | ]}fd d |D qS )c                    r   r   r   r   entityget_points_funcr   r   r     r   J_invert_normalize_polygons_and_line_strings.<locals>.<listcomp>.<listcomp>r   r   Zoiget_entities_funcr   r   r   r     s    z?_invert_normalize_polygons_and_line_strings.<locals>.<listcomp>r   z|Expected a single %s instance after normalization due to getting a single %s before normalization. Got %d instances instead.zExpected a %s instance containing a single %s after normalization due to getting a single %s before normalization. Got %d instances instead.rR   zExpected polygons/line strings after normalization to be None, due to the polygons/line strings before normalization being an empty iterable. Got type %s instead.rz   c                    s(   g | ]}t fd d |D qS )c                    r   r   r   r   r   r   r   r     r   r   r   r  r  r   r   r   r   r     s    r{   zExpected a single %s instance after normalization due to getting an iterable of (x,y) tuples before normalization. Got %d instances instead.zExpected a %s instance after normalization containing a single %s instance due to getting an iterable of (x,y) tuples before normalization. Got a %s with %d %s instances instead.c                 S      g | ]
}|d  |d fqS r   r   r   r   pointr   r   r   r     s    r~   zExpected a single %s instance after normalization due to getting an iterable of Keypoint before normalization. Got %d instances instead.zExpected a %s instance after normalization containing a single %s instance due to getting an iterable of Keypoint before normalization. Got a %s with %d %s instances instead.c                        g | ]} |d  |d dqS r   r   rw   r   r  r|   r   r   r         r   zExpected a single %s instance after normalization due to getting an iterable of %s before normalization. Got %d instances instead.zExpected a %s instance after normalization containing a single %s instance due to getting an iterable of %s before normalization. Got a %s with %d %s instances instead.r   zExpected polygons/line strings after normalization to be None, due to the polygons/line strings before normalization being an empty iterable of iterables. Got type %s instead.r   c                    s$   g | ]}fd d |D qS )c                    s   g | ]	}t  |qS r   r  r   )r   r   r   r   r     r   r   r   r  r  r   r   r     s    r   zExpected a single %s instance after normalization due to getting an iterable of iterables of (x,y) tuples before normalization. Got %d instances instead.c                       g | ]}d d  |D qS )c                 S   r  r  r   r  r   r   r   r     r}   r   r   r   r   r   r   r     s    r   zExpected a single %s instance after normalization due to getting an iterable of iterables of Keypoint before normalization. Got %d instances instead.c                    "   g | ]} fd d|D qS )c                    r
  r  r   r  r|   r   r   r     r  r   r   r   rn   r   r   r   r     s    
r   c                    r   r   r   r  )r  r   r   r      r   r   r   c                    r   )c                    r  )c                 S   r  r  r   r  r   r   r   r     r   U_invert_normalize_polygons_and_line_strings.<locals>.<listcomp>.<listcomp>.<listcomp>r   r   r   r   r   r     s    r   r   r  r  r   r   r     s    
r   rU   c                    s$   g | ]} fd d|D qS )c                    r  )c                    r
  r  r   r  r|   r   r   r     r   r  r   r   r  r   r   r     s    
r   r   r  )rn   r  r   r   r   r     s    )r   rn   r   r   r   r"   r   )
r[   Z
inputs_oldr   r   r   r  r   r\   r   r   r   )rn   r  r   r   r   r   r  s.  
















r   c                 C   s$   | |v sJ d|d || f d S )NzPGot an unknown datatype for argument '%s'. Expected datatypes were: %s. Got: %s.r2   )r8   )type_strvalid_type_strsZarg_namer   r   r   _assert_is_of_norm_type  s   
r  c                 C       t | }g d}t||d |S )N)rJ   rK   rH   rR   rS   rT   r   r   r  )r   r  r  r   r   r   rV   "  s   rV   c                 C   r  )N)
rJ   rb   rc   rd   r`   rR   rg   rh   ri   rj   r   r  )r   r  r  r   r   r   rk   0  s   rk   c                 C   r  )N)rJ   rK   rb   rc   rv   rn   ro   rR   rS   rg   rh   r{   r~   r   r   r   r   r   r  )r   r  r  r   r   r   r   C  s   r   c                 C   r  )N)rJ   rK   rb   rc   r   r   r   rR   rS   rg   rh   r   r   r   r   r   r   r   r  )r   r  r  r   r   r   r   \  s   r   c                 C      t | dddS )Nr   r   r   ._estimate_polygons_and_line_segments_norm_typer   r   r   r   r   v     r   c                 C   r  )Nr   r   r   r  r   r   r   r   r   {  r  r   c                 C   s^   t | }dddd||ddddd	d
d|f d|f ddddddd|f dddg}t||| |S )NrJ   rK   rb   rc   rR   rS   rg   rh   r{   r~   r   r   r   r   r   r   r   r   r   r   r   r  )r[   r   r   Zaugmentable_namer  r  r   r   r   r    s8   r  c                 C   s   t | \}}}t|||}|S r   )r   _nonempty_info_to_type_str)r[   r   successparentsr  r   r   r   r     s   r   c                    s^   t | tr" fdd| D } dd | D }tt|dkr"t| } t| r-t	|  } | S )Nc                    s   g | ]}t | qS r   r  r   Zarr_ir   r   r   r     s    z+restore_dtype_and_merge.<locals>.<listcomp>c                 S   r   r   r   r  r   r   r   r     r   r   )
r   r   r"   setrC   r   r   r   iadtZrestore_dtypes_)r'   r   r   r   r   r   r     s   



r   c                 C   s.   t | ot| t ot| d ot |  S )Nr   )r   rD   r   r	   hasattrZ	is_string)objr   r   r   _is_iterable  s   



r"  c           	      C   s   |d u rg }| d u st | r| d|fS t| rat| dkr#d d|fS | d | u r.| d|fS d }d}|}| D ]%}t||| g d\}}}|rM|||f  S t|t|kr[|}|}|}q6|||fS | d|fS )NTr   F)r  )r   r   r"  r"   r   )	attrr  Znonempty_deepestZsuccess_deepestZparents_deepestrP   r   r  Zparents_foundr   r   r   r     s2   





r   c           	      C   sT  t |dksJ dt |f d}t |dkr$dddgt | f }|s+d|f S t |d	ko7t|d
 t}|rnt |d
 dkoLtdd |d
 D }|rnddgt |d	  }dt |d
 f }d||gdS | d u rtdS t| r| jj	}ddddd}d|||v r|| f S |f S t| t
sJ dt| f d|| jjf S )Nr   z%Expected 'parents' to be <=4, got %d. r   z%s--r*   z%siterable[empty]r   r=   c                 S   s   g | ]}t |qS r   )r   Zis_single_number)r   valr   r   r   r     r1   z._nonempty_info_to_type_str.<locals>.<listcomp>ztuple[number,size=%d]rJ   floatZuintintbool)fuibz%sarray[%s]z1Expected 'nonempty' to be an object, got type %s.z%s%s)r"   r8   r   r   alllstripr   r   r   kindobjectr   	__class__r   )	r   r  r  Zparent_itersZis_parent_tupleZis_only_numbers_in_tupleZtpl_namer0  Zkind_mapr   r   r   r    sF   

r  r   )2__doc__
__future__r   r   r   rW   numpyrC   r$  r   r   r   r  baser	   r    r&   r)   r<   r?   rB   rF   r^   rl   r   r   r   r   r   r   r   r   r   r   r   r   r   r  rV   rk   r   r   r   r   r  r   r   r"  r   r  r   r   r   r   <module>   sX    		


+
I
S

n@(+]c		 *"
	+