
    |hP                     x   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<                  j?                  dej@                  ejB                  ejD                  ejF                  ejH                  ejJ                  ejL                  ejN                  g      d        Z(d Z)d  Z*d! Z+d" Z,d# Z-y)$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y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                  |j                  z
  dz  j                         z   }n~t        j                  |       } | j                  t        j                        } t        j                  |      }|j                  t        j                        }| |z
  dz  j                         }t        j                  |      S )Nr   g        r   )
isinstancenpndarrayasarrayshapedtype	complex64
complex128realsumimagastypefloat64mathsqrt)abts      h/var/www/html/test/engine/venv/lib/python3.12/site-packages/skimage/segmentation/tests/test_watershed.pydiffr)   +   s^   a$JJqMa$JJqM	QWW1<Aa1fIww2<<//177

? 4 JJq"--(JJq"--(ffqvvo!#((*qvv1.D-I-I-KKJJqMHHRZZ JJqMHHRZZ !e\ 99Q<    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y)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 y)	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    xx%%%%%%%%%% HH
 ((&%%%%%%%%% GG
 gtzz288,,,''''',,
 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|      }| j                  |t        k         y	)
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    xx%%%%%%%%%%% HH
  ((&%%%%%%%%%% GG
  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
|      }| j                  |t        k         y	)
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    xx%%%%%%%%%% HH
 ((%%%%%%%%%& GG
 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
|      }| j                  |t        k         y)	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       xx%%%%%%%%%% HH
 ((%%%%%%%%%& GG
 gtzz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
|      }| j                  |t        k         y)	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	|      }| j                  |t        k         y)	zwatershed 6r2   r1   r0   r5   r3   r7   r6   NrP   rG   s        r(   test_watershed06zTestWatershed.test_watershed06C  s    xx%%%%%%%%%
 HH
 ((%%%%%%%%&
 GG
 gtzz2'''',,,,,
 
 	$r*   c                 H   t         }|dk7  }t        j                  |j                  t              }d|d<   d|d<   t        ||| j                  |      }t        j                  |dk(        }t        j                  |dk(        }| j                  t        ||z
        dk         y)	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+gtzz=
 sax sax EEM*a/0r*   c                 v   t         j                         }d|dddf<   |dk7  }t        j                  |j                  t
              }d|d<   d|d	<   t        ||| j                  |
      }t        j                  |dk(        }t        j                  |dk(        }| j                  t        ||z
        dk         y)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+gtzz=
 sax sax EEM*a/0r*   c                    t        j                  d      }t         j                  j                  ddd      j	                  t
              }t        j                  dt
              }d}|D ]  \  }}d|||f<   ||||f<   |dz  } t        |dd      }t        ||| j                         t        j                  |j	                  t         j                        || j                         y	)
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<<SA((<- 	DAqE!Q$KGAqDM1HC	
 ai8%$**-%,,ryy17DJJ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|      }| j                  |t        k         y)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}    xx<|Dbhh
 ((<|Dbgg
 gtzz2lL,Ms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                  dD cg c]M  \  }}t        j                  |j                  t              |z
  dz  |j                  t              |z
  dz        O c}}      }	t        j                  |	d      }
| j                  t        j                  |	|||||f   dz
  f   |
k(               yc c}}w )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   )r   r   r   r   N)r   r`   ra   r8   r   mgriddstackr$   r!   floatminrF   all)r=   rx   r?   	structurer@   iji0j0ddmins              r(   test_watershed11zTestWatershed.test_watershed11  s0    "((8S)HH!#57KL
	 w	2xx"ad
#1II CB %2-!3ahhuo6Jq5PQ
 vva|qAs1a4y1}!45=>?s   AE
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 ]/  \  }}| j                  t        j                  ||k(        |k(         1 y)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    xx$$$$$$$$$$$$$$$_ac
H	 AI	y!tV!DIU1X'78 	7ICOOBFF29-56	7r*   c                 z   t         j                  j                         j                  d      }t        j                  dt         j                        }g d|g dg df<   t        j
                  |      }t        ||      }t         j                  j                  ||       t        j                  ||k(        rJ y)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.=((828842>./777+5'*


 0':66&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_    BGGFD!E0d3%j0%d0%d0%d-%^1 1"H&
%@8j7X	-r*   r,   c                  b   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
                  j                  ||       t        | |      }t        j                  g dg dg dg dg dgt              }t         j
                  j                  ||       t        | |d	d      }||k7  }d||dk(  <   t        j                  |      rJ y )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$    HHVEE!Q$KE!QR%LHHV3'EE$KE$Ku$7Gxx	
 	H JJGX.ue$Fxx	
 	H JJFH- E5d4PK'J#(J{a vvj!!!!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                  j!                  ||       }|d   j"                  dk(  sJ t        j$                  ddt&              j)                  dd      }t         j*                  j-                  |d   j.                  |       |d   j0                  dk(  sJ |d   j0                  dk(  sJ y)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MM,5E
 ))%0HHNF
1a4LAL88HNN$/D Dvxx4JGQy'6FOO''	'JE 8??aiic?GGANOJJE!H44o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                  j	                  ||       t        j                  g d       } t        j                  g d      }t        j                  g d	      }t        | || d
k7        }t         j                  j                  ||       y)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     xx446 7Exx446 7Gxx446 7H ug.FJJFH- XX+,,Ehh,-Gxx-.HugEQJ?FJJ!!&(3r*   c                     t        j                  g d      } t        j                  g d      }t        j                  g d      }t        | |      }t         j                  j	                  ||       t        j                  g d      } t        j                  g d      }t        | |      }t         j                  j	                  ||       y)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     xx67Exx67Gxx67Hug.FJJFH- xx67Exx67Hug.FJJ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                  j	                  ||       y)	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     so     xx..0 1Exx..0 1Gxx..0 1Hug.FJJ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
                  j                  ||       y)
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     sn    HHVEE!QR%Lqd3Gxx	
 hh	H JJGX.r*   r   c                     t        j                  d      }t        j                  d|       }t        ||      }|j                  |j                  k(  sJ y )N)rn   rn   )r   r`   r   r   )r   rx   r?   r@   s       r(   test_watershed_output_dtyper     sD    
 HHZ Ehhz5)G
E7
#C99%%%r*   c                      t        j                  d      } t        j                  d      }t        j                  t              5  t        | |       d d d        y # 1 sw Y   y xY w)Nr   r   rZ   r   r   pytestraises
ValueErrorr   )rx   r?   s     r(   test_incorrect_markers_shaper     sG    GGFOEggfoG	z	" "%!" " "s   AA#c                      t        j                  d      } t        j                  d      }t        j                  t              5  t        | d|       d d d        y # 1 sw Y   y xY w)Nr   r   ro   r   r   )rx   r^   s     r(   test_incorrect_mask_shaper     sI    GGFOE776?D	z	" /%./ / /s   AA%c                  t    t         } | dk7  }t        | dd|      }t        j                  ||    dk(        sJ y )Nr	      r   )r   r^   r   )r_   r   r   r   r>   r^   r@   s      r(   test_markers_in_maskr    s<    D3;D
D"14
8C66#te*/"""r*   c                  h    t         } | dk7  }t        | |      }t        j                  |      dk(  sJ y )Nr	   r]   r   )r_   r   r   maxr  s      r(   test_no_markersr    s2    D3;D
Dt
$C66#;!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(  r"J  t        t        d      g d      D ]"  \  }}t        j                  ||k(        |k(  r"J  y)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"    s+    ::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MM,5EMM%.EMM%.E ((/G))GbL15:Kxxq(!Q/H kqdI kqdI
 99Y%%a(A---99Y%%a(A--- q#KL 0	Tvvi3&'4///0 q#EF 0	Tvvi3&'4///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>r2     s/   6        , * ! "rxxaaaaaaaaaaaaaaaaaaaaa)c d00
-H%% 
-D+"\*'Z4@.,."/$ XXrww		288RYY"))RXXV&	&"/#*0r*   