
    1-PhP                     &   d Z ddlZddlZddlZddlZddlmZ ddl	Z
ddlmZ ddlmZ ddl	mZ ddlmZ d	Z ej        g d
g d
g dg dg dg dg dg dg dg dg dg dg dg dg dg dg dg dg dg d
g d
g          Zd Z G d dej                  Zd Zd Zd Zd Zd Zd Zej                            dej         ej!        ej"        ej#        ej$        ej%        ej&        ej'        g          d             Z(d Z)d  Z*d! Z+d" Z,d# Z-dS )$z0test_watershed.py - tests the watershed function    N)ndimage)gaussian)peak_local_max)label   )	watershedg-q=)   r	   r	   r	   r	   r	   r	   r	   r	   r	   r	   r	   r	   r	   r	   r	   )r	   r	   r	   r	   r	      r
   r
   r
   r
   r
   r	   r	   r	   r	   r	   )r	   r	   r	   r
   r
         r   r   r   r   r
   r
   r	   r	   r	   )r	   r	   r
   r   r      o   g   r   r   r   r   r   r
   r	   r	   )r	   r	   r
   r   r   ^   H   4   r   r   r   r   r   r
   r	   r	   )r	   r	   r
   r   r   r   '      r   r   r   r   r   r
   r	   r	   )r	   r	   r
   r   r   r   r   r   r   r   r   r   r   r
   r	   r	   )r	   r	   r	   r
   r   r   r   r   r   r   r   r   r
   r	   r	   r	   )r	   r	   r	   r	   r
   r   r   r   r   r   r   r
   r	   r	   r	   r	   )r	   r	   r	   r	   r	   r
   r   r   r   r   r
   r	   r	   r	   r	   r	   c                    t          | t          j                  st          j        |           } t          |t          j                  st          j        |          }d| j        v rd|j        v rdS d|| dk    <   | j        t          j        t          j        fv s|j        t          j        t          j        fv rt          j        | t          j                  } t          j        |t          j                  }| j        |j        z
  dz  	                                | j
        |j
        z
  dz  	                                z   }nt          j        |           } |                     t          j                  } t          j        |          }|                    t          j                  }| |z
  dz  	                                }t          j        |          S )Nr   g        r   )
isinstancenpndarrayasarrayshapedtype	complex64
complex128realsumimagastypefloat64mathsqrt)abts      i/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/skimage/segmentation/tests/test_watershed.pydiffr)   +   sk   a$$ JqMMa$$ JqMM	QW1<<sAa1fIw2<///17

? 4 4 Jq"-((Jq"-((fqvo!#((**qv1.D-I-I-K-KKJqMMHHRZ  JqMMHHRZ  !e\  9Q<<    c                   ~    e Zd Z ej        de          Zd Zd Zd Z	d Z
d Zd Zd Zd	 Zd
 Zd Zd Zd Zd ZdS )TestWatershed   r.   c                    t          j        g dg dg dg dg dg dg dg dg dg dg
t           j                  }t          j        g dg dg dg dg dg dg dg dg dg dg
t           j                  }t	          ||| j                  }t          j        g dg dg dg dg dg dg dg dg dg dg
          }t          ||          }|t          k     sJ dS )	zwatershed 1r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r4   r4   r4   r4   r4   r4   r4   r4   r   r   r   r   r   r4   N)r   arrayuint8int8r   eightr)   eps)selfdatamarkersoutexpectederrors         r(   test_watershed01zTestWatershed.test_watershed01F   s   x%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% H
 
 (&&&%%%%%%%%%%%%%%%%%%%%%%%%%%% G
 
 gtz228,,,,,,,,,''''''''''''''',,,,,,
 
 Xs##s{{{{{{r*   c                    t          j        g dg dg dg dg dg dg dg dg dg dg dgt           j                  }t          j        g dg dg dg dg dg dg dg dg dg dg dgt           j                  }t	          ||          }t          g dg dg dg dg dg dg dg dg dg dg dg|          }|                     |t          k                d	S )
zwatershed 2r0   r1   r2   r3   r5   r6   )r4   r4   r   r   r   r4   r4   r7   Nr   r8   r9   r:   r   r)   
assertTruer<   r=   r>   r?   r@   rB   s        r(   test_watershed02zTestWatershed.test_watershed02x   s   x%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% H
 
  (&&&%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% G
 
  g&&,,,,,,,,,,,,)))'''''''''))),,,,,, 
 
  	$$$$$r*   c                    t          j        g dg dg dg dg dg dg dg dg dg dg
t           j                  }t          j        g dg dg dg dg dg dg dg dg dg dg
t           j                  }t	          ||          }t          g dg dg dg dg dg dg dg dg dg dg
|          }|                     |t          k                d	S )
zwatershed 3r0   r1   r   r   r   r   r   r   r   r   r   r   r   r.   r   r   r   r   r   r   r   r   r4   r6   )r4   r   r   r   r.   r   r4   r4   r   r   r   r.   r.   r4   NrE   rG   s        r(   test_watershed03zTestWatershed.test_watershed03   s   x%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% H
 
 (%%%%%%%%%%%%%%%%%%%%%%%%%%%&&& G
 
 g&&,,,''''''''''''''',,,,,,,,,,,, 
 
 	$$$$$r*   c                    t          j        g dg dg dg dg dg dg dg dg dg dg
t           j                  }t          j        g dg dg dg dg dg dg dg dg dg dg
t           j                  }t	          ||| j                  }t          g dg dg dg dg dg dg dg dg dg dg
|          }|                     |t          k                dS )	zwatershed 4r0   r1   rJ   rK   rL   r6   rM   N	r   r8   r9   r:   r   r;   r)   rF   r<   rG   s        r(   test_watershed04zTestWatershed.test_watershed04      x%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% H
 
 (%%%%%%%%%%%%%%%%%%%%%%%%%%%&&& G
 
 gtz22,,,''''''''''''''',,,,,,,,,,,, 
 
 	$$$$$r*   c                    t          j        g dg dg dg dg dg dg dg dg dg dg
t           j                  }t          j        g dg dg dg dg dg dg dg dg dg dg
t           j                  }t	          ||| j                  }t          g dg dg dg dg dg dg dg dg dg dg
|          }|                     |t          k                dS )	zwatershed 5r0   r1   rJ   )r   r   r.   r   r   r   r   rL   r6   )r4   r.   r.   r   r   r   r4   NrP   rG   s        r(   test_watershed05zTestWatershed.test_watershed05  rR   r*   c                    t          j        g dg dg dg dg dg dg dg dg dg	t           j                  }t          j        g dg dg dg dg dg dg dg dg dg	t           j                  }t	          ||| j                  }t          g dg dg dg dg dg dg dg dg dg	|          }|                     |t          k                dS )	zwatershed 6r2   r1   r0   r5   r3   r7   r6   NrP   rG   s        r(   test_watershed06zTestWatershed.test_watershed06C  s}   x%%%%%%%%%%%%%%%%%%%%%%%%%%%
 H
 
 (%%%%%%%%%%%%%%%%%%%%%%%%&&&
 G
 
 gtz22'''''''''''',,,,,,,,,,,,,,,
 
 
 	$$$$$r*   c                 T   t           }|dk    }t          j        |j        t                    }d|d<   d|d<   t          ||| j        |          }t          j        |dk              }t          j        |dk              }|                     t          ||z
            dk               dS )	z3A regression test of a competitive case that failedr	   r         r      rZ   maskrY   N)
blobr   zerosr   intr   r;   r   rF   absr=   r>   r^   r?   r@   size1size2s          r(   test_watershed07zTestWatershed.test_watershed07r  s    s{(4:s++gtz===
 sax  sax  EEM**a/00000r*   c                    t                                           }d|dddf<   |dk    }t          j        |j        t
                    }d|d<   d|d	<   t          ||| j        |
          }t          j        |dk              }t          j        |dk              }| 	                    t          ||z
            dk               dS )z2The border pixels + an edge are all the same valuer   
   rZ   	   r	   r   rX   r   r[   r]   rY   N)r_   copyr   r`   r   ra   r   r;   r   rF   rb   rc   s          r(   test_watershed08zTestWatershed.test_watershed08  s    yy{{R1Ws{(4:s++gtz===
 sax  sax  EEM**a/00000r*   c                    t          j        d          }t           j                            ddd                              t
                    }t          j        dt
                    }d}|D ]\  }}d|||f<   ||||f<   |dz  }t          |dd          }t          ||| j                   t          j
        |                    t           j                  || j                   d	S )
zTest on an image of reasonable size

        This is here both for timing (does it take forever?) and to
        ensure that the memory constraints are reasonable
        )  rm   r   rm   )d   r   r      reflect)sigmamodeN)r   r`   randomuniformr!   ra   r   r   r;   ndiwatershed_iftuint16)r=   imagecoordsr?   idxxys          r(   test_watershed09zTestWatershed.test_watershed09  s     &&""1dH55<<SAA(<-- 	 	DAqE!Q$KGAqDM1HCCai888%$*---%,,ry117DJGGGGGr*   c                 R   t          j        g dg dg dg dgt           j                  }t          j        g dg dg dg dgt           j                  }t	          ||| j                  }t          g dg dg dg dg|          }|                     |t          k                dS )zwatershed 10)r   r   r   r   )r   r   r   r   )r   r   r   r   )r.   r   r   ro   )r   r   r   r   )r.   r.   ro   ro   NrP   rG   s        r(   test_watershed10zTestWatershed.test_watershed10  s    x\\<<<|||Dbh
 
 (\\<<<|||Dbg
 
 gtz22lllLLL,,,MsSS$$$$$r*   c           
      
   t          j        d          }t          j        dt                    }d|d<   d|d<   d|d<   d|d	<   t          j        g d
g dg d
g          }t	          |||          }t           j        ddddf         \  t          j        fddD                       }t          j        |d          }|                     t          j	        ||f         dz
  f         |k                         dS )zFMake sure that all points on this plateau are assigned to closest seed   r   r      r   r   r   rh   r.   rh   r   ro   rh   rh   )FTF)TTTr   r   c                     g | ]V\  }}t          j                            t                    |z
  d z                      t                    |z
  d z            WS )r   )r   r$   r!   float).0i0j0ijs      r(   
<listcomp>z2TestWatershed.test_watershed11.<locals>.<listcomp>  sa       B %2-!3ahhuoo6Jq5PQQ  r*   )r   r   r   r   N)
r   r`   ra   r8   r   mgriddstackminrF   all)	r=   rx   r?   	structurer@   ddminr   r   s	          @@r(   test_watershed11zTestWatershed.test_watershed11  s4    ""(8S))H!!!#5#5#57K7K7KL
 
	 w	22x"ad
#1I    B  
 
 va||qAs1a4y1}!45=>>?????r*   c                    t          j        g dg dg dg dg dg dg dg dg d	g d
g dg dg dg dg dg dg          }|dk    }t          |          }t          ||dd          }t	          t          d          g d          D ]4\  }}|                     t          j        ||k              |k               5dS )zThe watershed line)   r	   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r	   r   r   r   r   f   r   r   r   r   r   r   r   r   r   )r   r	   r   r   r   r   r   r   M   r   r   r   r   r   r   r   )r   r	   r	   r   r   r   r   r   r   r   r   r   r   r   r   r	   )r   r   r	   r   r   r   r   r   r   r   r   r   r   r   r	   r	   )r   r   r	   r	   r	   r   r   r   r   r   r   r   r   r	   r	   r   )r   r   r   r   r	   r	   r	   r	   r	   r	   r	   r	   r	   r	   r   r   )r   r   r   r   r   r   r   r   r	   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r	   r   r   r	   r   r   r   r   )r   r   r   r   r   r   r   r	   r	   r   r   r   r   r   r   r   )r   3   r   r   r   r   r   r	   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r	   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r	   r   r	   r   r   r   r   r   r   )r   r   r   r   r   r   r	   r	   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r	   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r	   r   r   r   r   r   r   r   r   r   r   r   T)connectivitywatershed_linero   )"   J   r   r   N)r   r8   r   r   ziprangerF   r   )r=   r>   	markerbinmarkerwslabareas          r(   test_watershed12zTestWatershed.test_watershed12  s   x  $  $  $  $  $  $  $  $  $  $  $  $  $  $  $  _ac
 c
H	 AI	y!!tV!DIIIU1XX'7'7'788 	7 	7ICOOBF29--56666	7 	7r*   c                    t           j                                                            d          }t          j        dt           j                  }g d|g dg df<   t          j        |          }t          ||          }t           j                            ||           t          j	        ||k              rJ dS )z.Test to ensure input markers are not modified.r   )sizer   )r   r   r.   ro   )r   r      r   )r   r   r   r   N)
r   rs   default_rngr`   r9   rj   r   testingassert_equalr   )r=   rx   r?   original_markersresults        r(   !test_watershed_input_not_modifiedz/TestWatershed.test_watershed_input_not_modified  s    	%%''..H.==(8284442>,,./77++5'**

 0':::6&G+,,,,,,,r*   N)__name__
__module____qualname__r   onesboolr;   rC   rH   rN   rQ   rT   rV   rf   rk   r}   r   r   r   r    r*   r(   r,   r,   C   s        BGFD!!E0 0 0d3% 3% 3%j0% 0% 0%d0% 0% 0%d0% 0% 0%d-% -% -%^1 1 1 1 1 1"H H H&
% 
% 
%@ @ @8j7 j7 j7X	- - - - -r*   r,   c                  |   t          j        d          } d| d d df<   d| d d dd f<   t          j        dt                    }d|d<   d|d<   t          | |d	
          }t          j        g dg dg dg dg dgt                    }t           j                            ||           t          | |          }t          j        g dg dg dg dg dgt                    }t           j                            ||           t          | |d	d          }||k    }d||dk    <   t          j        |          rJ d S )Nr   rY   r   r.   r   ro   r   )r   r   )r   r   {Gz?compactness)r   r   r   r   r   r   r   r   r   r   r   r   T)r   r   Fr   )r   r`   ra   r   r8   r   r   any)rx   seedscompactrA   normalcompact_wsl
differences          r(   test_compact_watershedr     s    HVEE!!!Q$KE!!!QRR%LHV3'''EE$KE$Ku$777Gx	
 	 	 	H JGX...ue$$Fx	
 	 	 	H JFH--- E5d4PPPK'J#(J{a vj!!!!!!!r*   c                     t          j        d          \  } }d\  }}}}d\  }}| |z
  dz  ||z
  dz  z   |dz  k     }| |z
  dz  ||z
  dz  z   |dz  k     }	t          j        ||	          }
t          j        |
          }t          |t          j        d          |
          }|dddfxx         d	z  cc<   t          j        |j        t          
          }d|t          |j                  <   t          j        |          \  }}t          | ||
          }t          j                            ||           }|d         j        dk    sJ t          j        ddt&                                        dd          }t           j                            |d         j        |           |d         j        dk    sJ |d         j        dk    sJ dS )a  
    Check edge case behavior reported in gh-6632

    While we initially viewed the behavior described in gh-6632 [1]_ as a bug,
    we have reverted that decision in gh-7661. See [2]_ for an explanation.
    So this test now actually asserts the behavior reported in gh-6632 as
    correct.

    .. [1] https://github.com/scikit-image/scikit-image/issues/6632.
    .. [2] https://github.com/scikit-image/scikit-image/issues/7661#issuecomment-2645810807
    )P   r   )   r   ,   r   )      r   r-   )	footprintlabelsNr   rY   r   Tr]   )intensity_imager   i)startstopr   r4   rh   i  )r   indices
logical_orru   distance_transform_edtr   r   r`   r   r   tupleTr   r   skimagemeasureregionpropsextentaranger   reshaper   r   image_intensity
num_pixels)r{   r|   x1y1x2y2r1r2mask_circle1mask_circle2rx   distancery   r^   r?   _r   propsexpected_regions                      r(   "test_watershed_with_markers_offsetr   3  s    :hDAq#NBBFBFq=AFq=02q58LFq=AFq=02q58LM,55E
 )%00HHNNNF
111a4LLLALLL8HN$///D Dvx4JGQy'666FO''	'JJE 8?aic???GGANNOJE!H4oFFF 8"$$$$8$&&&&&&r*   c                     t          j        g dg dg          } t          j        g dg dg          }t          j        g dg dg          }t          | |          }t           j                            ||           t          j        g d           } t          j        g d          }t          j        g d	          }t          | || d
k              }t           j                            ||           dS )a  
    Test edge case behavior when markers spill over into another basin / compete.

    While we initially viewed the behavior described in gh-6632 [1]_ as a bug,
    we have reverted that decision in gh-7661. See [2]_ for an explanation.
    So this test now actually asserts the behavior reported in gh-6632 as
    correct.

    .. [1] https://github.com/scikit-image/scikit-image/issues/6632.
    .. [2] https://github.com/scikit-image/scikit-image/issues/7661#issuecomment-2645810807
    )	rY   r   ro   r.   r   r.   r   r   r   )	r   r   r   r   r   r   r   r   r   )	r   r   r   r   r   r   r   r   r   )	r   r   r   r   r   r   r   r   r   )	r   r   r   r   r   r   r   r   r   r?   )r   r   r   r   r   r   r.   )r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r?   r^   N)r   r8   r   r   r   assert_array_equalrx   r?   rA   r   s       r(   %test_watershed_simple_basin_overspillr   `  s%    x4444446 7 7Ex4444446 7 7Gx4444446 7 7H ug...FJFH--- X+++,,,Eh,,,--Gx---..HugEQJ???FJ!!&(33333r*   c                     t          j        g d          } t          j        g d          }t          j        g d          }t          | |          }t           j                            ||           t          j        g d          } t          j        g d          }t          | |          }t           j                            ||           dS )a  
    Edge case: Basins should be distributed evenly between contesting markers.

    Markers should be prevented from spilling over into another basin and
    conquering it against other markers with the same claim, just because they
    get to the basin one step earlier.
    )
r   r   r   r   r   r   r   r   r   r   )
r   r   r   r   r   r   r   r   r   r   )
r   r   r   r   r   r   r   r   r   r   r   )
r   r   r   r   r   r   r   r   r   r   Nr   r8   r   r   r   r   s       r(   +test_watershed_evenly_distributed_overspillr     s     x66677Ex66677Gx66677Hug...FJFH--- x66677Ex66677Hug...FJFH-----r*   c                     t          j        g dg dg          } t          j        g dg dg          }t          j        g dg dg          }t          | |          }t           j                            ||           dS )	zCheck that markers placed at maxima don't conquer other pixels.

    Regression test for gh-7661 [1]_.

    .. [1] https://github.com/scikit-image/scikit-image/issues/7661
    )r   r   r   r.   ro   r   ro   )r   r   r   r.   ro   ro   ro   )r   r   r   r   r   r   r   r0   )r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   Nr   r   s       r(   test_markers_on_maximar     s     x......0 1 1Ex......0 1 1Gx......0 1 1Hug...FJFH-----r*   c                     t          j        d          } d| ddddf<   t          | dd          }t          j        g dg dg dg dg dgt           j        	          }t           j                            ||           dS )
z>Test that passing just the number of seeds to watershed works.r   r   Nr.   r   r   r   r   r   )r   r`   r   r8   int32r   r   )rx   r   rA   s      r(   test_numeric_seed_watershedr     s    HVEE!!!QRR%Lqd333Gx	
 h	 	 	H JGX.....r*   r   c                     t          j        d          }t          j        d|           }t          ||          }|j        |j        k    sJ d S )N)rn   rn   )r   r`   r   r   )r   rx   r?   r@   s       r(   test_watershed_output_dtyper     sN    
 HZ  Ehz5))G
E7
#
#C9%%%%%%r*   c                      t          j        d          } t          j        d          }t          j        t                    5  t          | |           d d d            d S # 1 swxY w Y   d S )Nr   r   rZ   r   r   pytestraises
ValueErrorr   )rx   r?   s     r(   test_incorrect_markers_shaper    s    GFOOEgfooG	z	"	" " "%!!!" " " " " " " " " " " " " " " " " "s   A  A$'A$c                      t          j        d          } t          j        d          }t          j        t                    5  t          | d|           d d d            d S # 1 swxY w Y   d S )Nr   r   ro   r   r   )rx   r^   s     r(   test_incorrect_mask_shaper    s    GFOOE76??D	z	"	" / /%..../ / / / / / / / / / / / / / / / / /s   A""A&)A&c                      t           } | dk    }t          | dd|          }t          j        ||          dk              sJ d S )Nr	      r   )r   r^   r   )r_   r   r   r   r>   r^   r@   s      r(   test_markers_in_maskr    sL    D3;D
D"14
8
8
8C6#te*/"""""""r*   c                  v    t           } | dk    }t          | |          }t          j        |          dk    sJ d S )Nr	   r]   r   )r_   r   r   maxr  s      r(   test_no_markersr
    sA    D3;D
Dt
$
$
$C6#;;!r*   c                  "   t          j        d          \  } }d\  }}}}}}}}	d\  }
}}}| |z
  dz  ||z
  dz  z   |
dz  k     }| |z
  dz  ||z
  dz  z   |dz  k     }| |z
  dz  ||z
  dz  z   |dz  k     }| |z
  dz  ||	z
  dz  z   |dz  k     }t          j        ||          }t          j        ||          }t          j        ||          }t          j        |          }t          j        |dz  d          dz  }t          j        |dk    dd          }t          d	|z
  |dd
          }t          d	|z
  |dd
          }t          j        |          j	        d         dk    sJ t          j        |          j	        d         dk    sJ t          t          d          g d          D ]#\  }}t          j        ||k              |k    sJ $t          t          d          g d          D ]#\  }}t          j        ||k              |k    sJ $dS )z
    Watershed segmentation should output different result for
    different connectivity
    when markers are calculated where None is supplied.
    Issue = 5084
    )i  i  )      i,  x   rn   rn   iT  r  )rn   2   (   r   r      r   )decimalsr   r  r   )r^   r   r   rY   r   )  E  iO  iY+  i  i,  )r  r  iO  ib0  i,  N)r   r   r   ru   r   aroundwherer   uniquer   r   r   r   )r{   r|   r   r   r   r   x3y3x4y4r   r   r3r4r   r   mask_circle3mask_circle4rx   DummyBTDummyBT_disImg_mask	labels_c1	labels_c2r   r   s                             r(   test_connectivityr%    sv    :j!!DAq%K"BBBB$NBBFq=AFq=02q58LFq=AFq=02q58LFq=AFq=02q58LFq=AFq=02q58LM,55EM%..EM%..E (//G)GbL1555:Kxq(!Q//H kqd  I kqd  I
 9Y%a(A----9Y%a(A---- q#K#K#KLL 0 0	Tvi3&''4/////q#E#E#EFF 0 0	Tvi3&''4/////0 0r*   ).__doc__r#   unittestnumpyr   r   scipyr   ru   skimage.measurer   skimage._shared.filtersr   skimage.featurer   r   
_watershedr   r<   r8   r_   r)   TestCaser,   r   r   r   r   r   r   markparametrizer9   r:   rw   int16uint32r   uint64int64r   r  r  r  r
  r%  r   r*   r(   <module>r5     s   6 6                        , , , , , , * * * * * * ! ! ! ! ! ! " " " " " "rxaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)c d d0  0
- 
- 
- 
- 
-H% 
- 
- 
-D+" +" +"\*' *' *'Z4 4 4@. . .,. . ."/ / /$ Xrw	28RY")RXV & &	 &" " "/ / /# # #  *0 *0 *0 *0 *0r*   