
    _Mh                     P   d dl Z d dlZd dlZd dlmZmZ d dlmZmZ d dl	m
Z
mZmZmZmZmZmZmZ d dlmZ d dlmZmZ d Zd Zd	 Z G d
 d          Z G d d          Z G d d          Z G d d          Z G d d          Zd Z G d d          Z  G d d          Z!dS )    N)raiseswarns)xp_assert_closexp_assert_equal)	argrelmax	argrelminpeak_prominencespeak_widths_unpack_condition_args
find_peaksfind_peaks_cwt_identify_ridge_lines)gaussian)_local_maxima_1dPeakPropertyWarningc                    t          j        d|                              t                    }t          j        |t                    }t          |          D ].\  }}|| |         z
  |z  }|t          j        |dz             z  }/|S )Nr   dtype   )nparangeastypefloatzeros	enumerateexp)center_locssigmastotal_lengthxdataout_dataindsigmatmps           d/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/scipy/signal/tests/test_peak_finding.py_gen_gaussiansr&      s    Ia&&--e44ExE222H'' & &
U{3''50BFS!V9%%%O    c                     t          |           }||dz   z  }t          j        |||z
  |                              t                    }t          || |          }||fS )N   )num)lenr   linspacer   intr&   )r   r   	num_peaksdeltar   r!   s         r%   _gen_gaussians_evenr0      sa    FIIM*E+e\E%9yIIIPPQTUUKk6<@@H[  r'   c                 *   d }t          j        |          }t          j        |          }t          j        |dgt                    }| |dddf<   |d         | d         z
  t          |          z
  }||k     rt          d          |t          |          z  dz
  }|t          |          z  dz
  }	t          d|          D ]}
||
dz
  df         }||
dz
  df         dz   }|
|z  dk    r0t          |          dk    r|d|
z  |	                                z  z  }|
|	z  dk    r*t          |          dk    r||	                                z  } |||d                   } |||d                   }||g||
ddf<   |dddf         |dddf         gS )	a  
    Generate coordinates for a ridge line.

    Will be a series of coordinates, starting a start_loc (length 2).
    The maximum distance between any adjacent columns will be
    `max_distance`, the max distance between adjacent rows
    will be `map_gap'.

    `max_locs` should be the size of the intended matrix. The
    ending coordinates are guaranteed to be less than `max_locs`,
    although they may not approach `max_locs` at all.
    c                 F    t          | d          }t          ||          }|S )Nr   )maxmin)r*   max_valouts      r%   keep_boundsz$_gen_ridge_line.<locals>.keep_bounds5   s#    #qkk#w
r'   r   r   r   Nz3Cannot generate ridge line according to constraintsr)   )
copydeepcopyr   r   r-   sum
ValueErrorr+   rangepop)
start_locsmax_locslength	distancesgapsr7   locsr   dist_intgap_intr"   nextcolnextrows                r%   _gen_ridge_linerI   '   s     
 =Di((I8VQKs+++DDAAAJA;A.T:LfNOOOI&*Hs4yy 1$GQ 	* 	*sQwz"sQwz"Q&(Nac)nnq&8&8c	9==??22G'MQSYY]]txxzz!G+gx{33+gx{33)S!!!VAJQQQT
##r'   c            	           e Zd Zd Zd Zd Zd Zej        	                    d e
j        g d           e
j        g d           e
j        g d          g          d	             Zd
 ZdS )TestLocalMaxima1dc                     t          j        g t           j                  }t          |          D ]/}t	          |t          j        g           d           |j        J 0dS )zTest with empty signal.r   Fcheck_dtypeN)r   arrayfloat64r   r   baseselfxrO   s      r%   
test_emptyzTestLocalMaxima1d.test_emptyT   sg    HRrz***%a(( 	& 	&EE28B<<UCCCC:%%%%	& 	&r'   c                     t          j        dd          }t          |          D ]9}t          |t          j        g t           j                             |j        J :dS )zTest with linear signal.r   d   r   N)r   r,   r   r   rO   intprQ   rR   s      r%   test_linearzTestLocalMaxima1d.test_linear[   sf    K3%a(( 	& 	&EE28Bbg#>#>#>???:%%%%	& 	&r'   c                     t          j        ddd          }|dddxx         dz  cc<   t          j        dddt           j                  }t	          |          D ]}t          ||d	
           |j        J dS )zTest with simple signal.
   2   r   N   r)   r   FrM   )r   r,   r   rX   r   r   rQ   )rS   rT   expectedrO   s       r%   test_simplezTestLocalMaxima1d.test_simpleb   s    KR$$	!$Q$19QARW555%a(( 	& 	&E E8????:%%%%		& 	&r'   c                 <   t          j        g d          }t          |          \  }}}t          |t          j        g d          d           t          |t          j        g d          d           t          |t          j        g d          d           dS )z+Test if flat maxima are detected correctly.)gr   r)   r   r   r   r   r^   r^   r^   gQ@   rb   rb   rb   r[   rc   rc   rc   rc   r[   )r   rb            FrM   )r   rb            )r      	         N)r   rO   r   r   )rS   rT   	midpoints
left_edgesright_edgess        r%   test_flat_maximaz"TestLocalMaxima1d.test_flat_maximam   s    H / / / 0 0-=a-@-@*	:{	28,=,=,=#>#>ERRRR
BH->->->$?$?USSSSRX.?.?.?%@%@eTTTTTTr'   rT   )      ?r   r   )      @r^   r   rb   rb   )      @rj   rj   r      ru   ru   c                     t          |          D ]9}t          |t          j        g t          j                             |j        J :dS )z,Test if behavior on signal edges is correct.r   N)r   r   r   rO   rX   rQ   rR   s      r%   test_signal_edgesz#TestLocalMaxima1d.test_signal_edgesv   sW     &a(( 	& 	&EE28Bbg#>#>#>???:%%%%	& 	&r'   c                 J   t          t          d          5  t          t          j        d                     ddd           n# 1 swxY w Y   t          t          d          5  t          t          j        dt
                               ddd           n# 1 swxY w Y   t          t          d          5  t          d	d
g           ddd           n# 1 swxY w Y   t          t          d          5  t          d           ddd           dS # 1 swxY w Y   dS )z,Test input validation and raised exceptions.zwrong number of dimensionsmatch)r)   r)   Nzexpected 'const float64_t'r)   r   listrr          @z'x' must not be None)r   r<   r   r   onesr-   	TypeErrorrS   s    r%   test_exceptionsz!TestLocalMaxima1d.test_exceptions   s   J&BCCC 	. 	.RWV__---	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	.J&BCCC 	4 	4RWQc222333	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4IV,,, 	' 	'b"X&&&	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	'I%;<<< 	# 	#T"""	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	#sG   "AA	A	&)BB"B<CC!C;DDDN)__name__
__module____qualname__rU   rY   r`   rq   pytestmarkparametrizer   rO   rw   r    r'   r%   rK   rK   R   s        & & && & &	& 	& 	&U U U [S!!!""'''((#  
& & 
&	# 	# 	# 	# 	#r'   rK   c                   2    e Zd Zd Zd Zd Zd Zd Zd ZdS )TestRidgeLinesc                     t          j        ddg          }t          |t          j        dd          d          }t	          |          dk    sJ d S )Nrm   rW   r   r)   r   r   r   r   fullr+   rS   	test_matrliness      r%   rU   zTestRidgeLines.test_empty   sG    Hb#Y''	%iQCC5zzQr'   c                 ^   t          j        ddg          }d|d<   t          |t          j        dd          d          }t	          |          dk    sJ t          j        ddg          }d|dddf<   t          |t          j        dd          d          }t	          |          dk    sJ d S )Nrm   rW   r)   )r   r\   r   r   r\   r   r   s      r%   test_minimalzTestRidgeLines.test_minimal   s    Hb#Y''		%%iQCC5zzQHb#Y''		!A#r'%iQCC5zzQr'   c                    g d}g d}t          j        ddg          dz   }d}t          ddg|j        |||          }d	||d         |d	         f<   t          j        dt          |                    }t          ||t          |          d	z             }t          |          d	k    sJ t          |d         |          D ]\  }}	t          ||	d
           d S )Nr   r)   r   rj   )r   r)   r   r   r)   rm   r]   g-q=re   r      r)   FrM   )
r   r   rI   shaper   r3   r   r+   zipr   )
rS   rB   rC   r   rA   linemax_distancesidentified_linesiline_line_s
             r%   test_single_passzTestRidgeLines.test_single_pass   s     LL	Hb"X&&.	2w	DQQ&'	$q'47"#C	NN3301>14TQ@ @ #$$)))) !1!!4d;; 	> 	>MFEFEu=====	> 	>r'   c                    g d}g d}t          j        ddg          }d}t          ddg|j        |||          }d||d         |d         f<   d	}t          j        d|          }t          ||t          |          dz             }t          |          d
k    sJ |D ]}	t          j        |	d                   }
t           j	        
                    t          j        |
          |           t          j        |	d                   }t           j	        
                    t          j        |          t          |          dz              d S )Nr   )r   r)   r   rb   rm   r]   re   r   r   r)   r^   r   皙?)r   r   rI   r   r   r   r3   r+   difftestingassert_array_lessabs)rS   rB   rC   r   rA   r   max_distr   r   ilineadistsagapss               r%   test_single_bigdistz"TestRidgeLines.test_single_bigdist   sD    LL	||Hb"X&&	2w	DQQ&'	$q'47"#H--01>14TQ@ @ #$$))))% 	I 	IEWU1X&&FJ((BBBGE!H%%EJ((D		CHHHH	I 	Ir'   c                 x   g d}d}g d}t          j        ddg          }d}t          ddg|j        |||          }d	||d         |d	         f<   d
}t          j        d|          }t          |||          }	t          |	          dk    sJ |	D ]}
t          j        |
d	                   }t           j        	                    t          j
        |          |           t          j        |
d                   }t           j        	                    t          j
        |          t          |          dz              d S )Nr   r^   )r   rb   r   r)   rm   r]   re   r   r   r)   ru   r   r   r   r   rI   r   r   r   r+   r   r   r   r   r3   rS   rB   max_gaprC   r   rA   r   r   r   r   r   r   r   s                r%   test_single_biggapz!TestRidgeLines.test_single_biggap   s5    LL	||Hb"X&&	2w	DQQ&'	$q'47"#H--0M7SS#$$))))% 	I 	IEWU1X&&FJ((BBBGE!H%%EJ((D		CHHHH	I 	Ir'   c                 v   dg}d}ddg}t          j        ddg          }d}t          ddg|j        |||          }d||d         |d         f<   d}t          j        d|          }t          |||          }	t          |	          dk    sJ |	D ]}
t          j        |
d                   }t           j        	                    t          j
        |          |           t          j        |
d                   }t           j        	                    t          j
        |          t          |          dz              d S )	Nr   r)   r^   ru   r]      r   r   r   r   s                r%   test_single_biggapsz"TestRidgeLines.test_single_biggaps   s3   C	1vHb"X&&	2w	DQQ&'	$q'47"#H--0M7SS#$$))))% 	I 	IEWU1X&&FJ((BBBGE!H%%EJ((D		CHHHH	I 	Ir'   N)	r   r   r   rU   r   r   r   r   r   r   r'   r%   r   r      sy          
	 	 	> > >I I I,I I I*I I I I Ir'   r   c                   &    e Zd Zd Zd Zd Zd ZdS )
TestArgrelc                    t          j        g t                    }t          j        d          }t	          |          }t          t          |          d           t          |d         |d           t          j        d          }t	          |d          \  }}t          ||d           t          ||d           t	          |d          \  }}t          ||d           t          ||d           d S )	Nr   rj   r)   r   FrM   )r^   rj   axis)r   rO   r-   r   r   r   r+   )rS   empty_arrayz1iz2rowcols          r%   rU   zTestArgrel.test_empty   s     hr---Xa[[bMMA"""!ku====XfRa(((S[e<<<<[e<<<<Ra(((S[e<<<<[e<<<<<<r'   c                 ,   t          j        g dg dg dg dg dg          }t          |d          \  }}t          j        |          }t	          ||         g dd	
           t	          ||         g dd	
           t          |d          \  }}t          j        |          }t	          ||         g dd	
           t	          ||         g dd	
           t          |d          \  }}t          j        |          }t	          ||         g dd	
           t	          ||         g dd	
           t          |d          \  }}t          j        |          }t	          ||         g dd	
           t	          ||         g dd	
           d S )N)r)   r   r   r^   r   )r   r)   r   r   r^   )r^   r   r)   r   r   )r   r^   r   r)   r   )r)   r   r^   r   r)   r   r   r)   r   r^   FrM   )rb   r   r)   r)   )r   r^   rb   )r^   r)   r   )r   rO   r   argsortr   r   )rS   rT   r   r   orders        r%   
test_basiczTestArgrel.test_basic	  s   
 Hooo%oo%oo%oo%oo	' ( ( QQ'''S
3E
III5AAAAE
III5AAAAQQ'''S
3E
III5AAAAE
III5AAAAQQ'''S
3E
III5AAAAE
III5AAAAQQ'''S
3E
III5AAAAE
III5AAAAAAr'   c                 $   d}g d}t          |d          \  }}||         dz  |||z   <   ||         dz  |||z
  <   t          ||d          d         }t          |          t          |          k    sJ ||k                                    sJ d S )Nr   )rr   r|         $@rt   g      .@  gwJ?clip)r   moder   )r0   r   r+   all)rS   r   r   	test_dataact_locsrel_max_locss         r%   test_highorderzTestArgrel.test_highorder(  s    ,,,1&#>>	8&/&9'&A	(U"#&/&9'&A	(U"# %fEEEaH<  CMM1111(--///////r'   c                    g d}t          |d          \  }}d}t          j        dt          |                    |z
  }t          j        |||         g          }t          |dd          \  }}t          d|j        d                   D ]V}	||	k    }
t          ||
                   t          |          k    sJ |||
         ||	z  z
  k                                    sJ Wd S )N)rr   r|   r   rW   rm   r   r)   )r   r   )	r0   r   r   r+   vstackr   r=   r   r   )rS   r   r   r   
rot_factor	rot_rangetest_data_2rel_max_rowsrel_max_colsrwindss              r%   test_2d_gaussianszTestArgrel.test_2d_gaussians3  s    !!!1&#>>	8
IaY00:=	iIi,@ ABB%.{!%L%L%L"l;,Q/00 	L 	LB B&D|D)**c(mm;;;;d!3jm!CDIIKKKKKK		L 	Lr'   N)r   r   r   rU   r   r   r   r   r'   r%   r   r      sX        = = =0B B B>	0 	0 	0L L L L Lr'   r   c                   \    e Zd Zd Zd Zd Zd Zd Zd Ze	j
        j        d             ZdS )	TestPeakProminencesc                    t          g dg           }t          |t          j        t          j        t          j        g          D ]\  }}|j        dk    sJ |j        |k    sJ  t          g g           }t          |t          j        t          j        t          j        g          D ]\  }}|j        dk    sJ |j        |k    sJ  dS )N
        Test if an empty array is returned if no peaks are provided.
        r   r   N)r	   r   r   rP   rX   sizer   )rS   r6   arrr   s       r%   rU   zTestPeakProminences.test_emptyD  s     yyy"--cBJ#ABB 	& 	&JC8q====9%%%%%r2&&cBJ#ABB 	& 	&JC8q====9%%%%%	& 	&r'   c                    t          j        g d          }t          j        g d          }t          j        g d          }t          j        g d          }||         t          j        ||         ||         gd          z
  }t          ||          }t	          |d         |d           t	          |d	         |d           t	          |d
         |d           dS )z
        Test if height of prominences is correctly calculated in signal with
        rising baseline (peak widths are 1 sample).
        )r8   333333?r   r)   皙	@g?g
ףp=
@g @)r)   r   rb   ru   )r   r   r   rj   )r^   r^   rj   rg   r   r   FrM   r)   r   N)r   rO   r3   r	   r   )rS   rT   peakslbasesrbasespromsr6   s          r%   r   zTestPeakProminences.test_basicR  s     H;;;<<&&,,,'',,,''%261V9ai"8qAAAAq%((A59999AE::::AE::::::r'   c                    g d}g d}t          ||          \  }}}t          |t          j        g d          d           t          |g dd           t          |g dd           g d}t          j        g d          }t          ||          \  }}}t          |t          j        g d	                     t          ||d
z
  d           t          ||d
z   d           dS )z"
        Test edge cases.
        )r   r   r)   r   r)   r   r   r)   r^   rj   )r|   r   r   FrM   )r   r   r   )ru   ru   ru   )r   r)   r   r)   r   r)   r   )rr   r)   r)   r)   N)r	   r   r   asarrayrO   rS   rT   r   r   r   r   s         r%   test_edge_casesz#TestPeakProminences.test_edge_casesc  s   
 "!!		 0E : :vvrz+++66EJJJJ			u====			u==== "!!## 0E : :vvrz+++66777	u====	u======r'   c                 F   t          j        g dd          }t          j        g dd          }t          |ddd         |ddd                   \  }}}t          |t          j        g d                     t          |g dd           t          |g d	d           dS )
:
        Test with non-C-contiguous input arrays.
        )irk   rk   r   r^   r)   r   )r)   r   rb   N)g      "@rk   r   )r   r   r^   FrM   )r^   r^   rj   )r   repeatr	   r   r   r   s         r%   test_non_contiguousz'TestPeakProminences.test_non_contiguousw  s     I)))1--	)))Q'' 033Q3sss D Dvvrz+++66777			u====			u======r'   c                     g d}dg}t          ||          }t          |g d          D ]\  }}||k    sJ dD ]@\  }}t          |||          }t          |d|z
  d|z   d|z
  g          D ]\  }}||k    sJ AdS )	zO
        Test if wlen actually shrinks the evaluation range correctly.
        )r   r)   r   r^   r)   r   r8   r^   )rs   r   ru   ))rd   r   )rg   r   )ru   r   )rj   r)   )r   r)   r^   r   )皙?r   rs   r   ru   N)r	   r   )rS   rT   peakr   promvalwlenr   s           r%   	test_wlenzTestPeakProminences.test_wlen  s     #""s D))UKKK00 	 	ID#3;;;;;S 	# 	#GD!$Qd33E aQA(>?? # #	cs{{{{{#	# 	#r'   c                    t          t          d          5  t          g dgddg           ddd           n# 1 swxY w Y   t          t          d          5  t          g dddgg           ddd           n# 1 swxY w Y   t          t          d          5  t          ddg           ddd           n# 1 swxY w Y   t          t          d	          5  t          g dg           ddd           n# 1 swxY w Y   d
D ]C}t          t          d	          5  t          g d|g           ddd           n# 1 swxY w Y   Dt          t          d          5  t          g dddg           ddd           n# 1 swxY w Y   t          t          d          5  t          t	          j        d          ddgd           ddd           dS # 1 swxY w Y   dS )zA
        Verify that exceptions and warnings are raised.
        	1-D arrayry   )r   r)   r)   r   r)   r   Nr^   r   not a valid index)ir8   r^     r)   r   r   cannot safely castr   ffffff@r   r\   rj   r   )r   r<   r	   r~   r   r   )rS   ps     r%   r   z#TestPeakProminences.test_exceptions  s   
 Jk222 	5 	5lll^aV444	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 Jk222 	5 	5\\\QF8444	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 Jk222 	& 	&Q%%%	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& J&9::: 	& 	&R!%%%	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& % 	1 	1A
*=>>> 1 1 QC0001 1 1 1 1 1 1 1 1 1 1 1 1 1 1 I%9::: 	7 	7\\\C:666	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 Jf--- 	< 	<RYr]]QF;;;;	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	<sy   9= =A<<B B B;;B?B?C::C>C>D??E	E	!FF	F#'GGGc                    d}dD ]C}t          t          |          5  t          g d|g           ddd           n# 1 swxY w Y   Dt          t          |          5  t          g ddgd           ddd           dS # 1 swxY w Y   dS )	>
        Verify that appropriate warnings are raised.
        z!some peaks have a prominence of 0)r   r)   r   ry   r   Nr   r)   r)   r)   r   r   r   )r   r   r	   )rS   msgr   s      r%   test_warningsz!TestPeakProminences.test_warnings  s+   
 2 	2 	2A*#666 2 2 QD1112 2 2 2 2 2 2 2 2 2 2 2 2 2 2&c222 	; 	;___qc::::	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	;s!   =A	A	BB	BN)r   r   r   rU   r   r   r   r   r   r   r   thread_unsafer   r   r'   r%   r   r   B  s        & & &; ; ;"> > >(	> 	> 	># # # < < << [	; 	; 	; 	; 	;r'   r   c                       e Zd Zd Zej                            d          d             Zd Zd Z	ej        j
        d             Zd Zej                            d          d             Zd	S )
TestPeakWidthsc                    t          g g           d         }t          |t          j                  sJ |j        dk    sJ t          g dg           d         }t          |t          j                  sJ |j        dk    sJ t          g g           }|D ]+}t          |t          j                  sJ |j        dk    sJ ,dS )r   r   r   N)r
   
isinstancer   ndarrayr   )rS   widthsr6   r   s       r%   rU   zTestPeakWidths.test_empty  s     R$$Q'&"*-----{aYYY++A.&"*-----{a"b!! 	! 	!Cc2:.....8q=====	! 	!r'   #ignore:some peaks have a width of 0c           	         t          j        g d          }d}dD ]\  }}}}t          |dg|          \  }}}	}
t          |t          j        |g                     t          |t          j        d||z  z
  g                     t          |	t          j        |g                     t          |
t          j        |g                     dS )zk
        Test a simple use case with easy to verify results at different relative
        heights.
        )r)   r   r)   r   r)   r   r8   r   ))        r	  rs   rs   )g      ?rr         @      @)      ?r|   r|         @)      ?rs         ?      @)rr   r  rr   rt   )r|   rt   rr         @)rs   rt   rr   r  r^   N)r   rO   r
   r   r   )rS   rT   
prominence
rel_height
width_truelip_truerip_true
width_calcheightlip_calcrip_calcs              r%   r   zTestPeakWidths.test_basic  s     H+++,,
;
 	> 	>6J
Hh 6AA3
6$ 6$2J(J
J<(@(@AAAFBJJ4K0K/L$M$MNNNHbj(&<&<===Hbj(&<&<====	> 	>r'   c           	          t          j        g dd          }t          j        dgd          }t          |ddd         |ddd                   }t          |t          j        dgdgdgdgg                     dS )	r   )r   rW   r]   rb   r)   r^   Nr  K   r  )r   r   r
   r   r   )rS   rT   r   results       r%   r   z"TestPeakWidths.test_non_contiguous  s     IlllA&&	1#q!!QsssVU33Q3Z00
TFRD4&3%#@AA	
 	
 	
 	
 	
r'   c                    t          t          d          5  t          t          j        d          t          j        d                     ddd           n# 1 swxY w Y   t          t          d          5  t          ddg           ddd           n# 1 swxY w Y   t          t          d          5  t          t          j        d          t          j        dt          j        	                     ddd           n# 1 swxY w Y   t          t          d          5  t          t          j        d          d           ddd           n# 1 swxY w Y   t          t          d
          5  t          t          j        d          ddg           ddd           n# 1 swxY w Y   t          t          d
          5  t          g ddg           ddd           n# 1 swxY w Y   t          t          d          5  t          t          j        d          ddg           ddd           n# 1 swxY w Y   t          t          d          5  t          g dddgd           ddd           n# 1 swxY w Y   t          t          d          5  t          g ddgd           ddd           dS # 1 swxY w Y   dS )zD
        Verify that argument validation works as intended.
        r   ry   )r^   rb   r^   Nr   r\   r   r   r   rd   rh   r)   r   r   r   r   r  )r   r)   r   r)   r   r8   r  None)r)   r   r)   )NNNprominence_data)	r   r<   r
   r   r   r}   r   rX   r~   r   s    r%   r   zTestPeakWidths.test_exceptions  s    Jk222 	6 	6(("'!**555	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 Jk222 	  	 A3	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  Jk222 	G 	G	"rwvRW'E'E'EFFF	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G Jk222 	* 	*	"q)))	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* J&9::: 	0 	0	"2w///	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 J&9::: 	$ 	$QF###	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ I%9::: 	3 	3	"Sz222	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 Jl333 	@ 	@!QB????	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ IV,,, 	L 	L			A38JKKKK	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	Ls   5AAA9BBB8ADD	D	&#EEE6%F''F+.F+G''G+.G+%H99H= H=I==JJKKKc                    d}t          t          |          5  t          g ddgd           ddd           n# 1 swxY w Y   t          t          |          5  t          g dd	gt          j        d
gt          j                  t          j        d	gt          j                  t          j        d	gt          j                  f           ddd           dS # 1 swxY w Y   dS )r   zsome peaks have a width of 0ry   r   r)   r   r)   r   r  Nr   r   r	  r!  )r   r   r
   r   rO   rP   rX   )rS   r   s     r%   r   zTestPeakWidths.test_warnings  sg   
 -&c222 	6 	6			A315555	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 &c222 	 	!!#2$
!;!;!#1#rw!7!7!#1#rw!7!7!9   	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s    ;??A3CC #C c                    g d}dg}t          g d          D ]\  }\  }}}t          j        |t          j                  t          j        |t          j                  t          j        |t          j                  f}|dk     rd}nd}t          t          |          5  t          |||	           d
d
d
           n# 1 swxY w Y   d
S )z4Test with mismatching peak and / or prominence data.r$  r)   ))rr   )r8   r   )r&  r   )r^   )r&  r'  r(  )rr   rr   r   r   r   r   )r)  r(  r'  )r&  r*  r'  )r&  r(  r+  r   r^   z#prominence data is invalid for peakz4arrays in `prominence_data` must have the same shapery   r!  N)r   r   rO   rP   rX   r   r<   r
   )	rS   rT   r   r   prominences
left_basesright_basesr"  rz   s	            r%    test_mismatching_prominence_dataz/TestPeakWidths.test_mismatching_prominence_data#  sC   IIs9B D
 D
 D
 : : 	F 	F5A5Z  "x2:FFF!x
"'BBB!x27CCCEO 1uu=N
%000 F FAt_EEEEF F F F F F F F F F F F F F F%	F 	Fs   "CC	C	c                     g d}t          t          |dgd          g d           t          t          |dgd          g d           dS )	z3Test if x == eval_height counts as an intersection.)r   r)   r   r)   r^   r^   r^   r)   r   r)   r   rj   r   )r   r  ))r	  rs   rt   r2  gUUUUUU?))r  r&  r1  )g      @N)r   r
   )rS   rT   s     r%   test_intersection_rulesz&TestPeakWidths.test_intersection_rules<  sw     .--AaSQ???444	6 	6 	6 	AaSSAAA444	6 	6 	6 	6 	6r'   N)r   r   r   rU   r   r   filterwarningsr   r   r   r   r   r/  r3  r   r'   r%   r  r    s        ! ! ! [ EFF> > GF>.	
 	
 	
L L L@ [  "F F F2 [ EFF	6 	6 GF	6 	6 	6r'   r  c                     t          j        d          } | }|dz   }|ddd         }dt          d| |          k    sJ dt          d| |          k    sJ dt          d| |          k    sJ dt          d| |          k    sJ dt          d| |          k    sJ t          ||f| |          \  }}t          |||                    t          |||                    t	          t
          d	
          5  t          |t          j        d          |           ddd           n# 1 swxY w Y   t	          t
          d
          5  t          d|ft          j        d          |           ddd           dS # 1 swxY w Y   dS )zW
    Verify parsing of condition arguments for `scipy.signal.find_peaks` function.
    r\   r)   Nr   NN)r)   NNr   )rs   r  zarray size of lowerry   rh   zarray size of upper)r   r   r   r   r   r<   )rT   	amin_true	amax_truer   	amin_calc	amax_calcs         r%   test_unpack_condition_argsr<  I  sN    		"AIBIaddOE 1,5IIIIII.q!U;;;;;;.y!UCCCCCC.y!UCCCCCC.y!UCCCCCC 29i2H!USSIyIy/000Iy/000 

"7	8	8	8 @ @y")B--???@ @ @ @ @ @ @ @ @ @ @ @ @ @ @	
"7	8	8	8 H Hi0")B--GGGH H H H H H H H H H H H H H H H H Hs$   2$D""D&)D&&E66E:=E:c                       e Zd Zh dZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zej                            dd          d             Zej                            di ddiddiddig          d             ZdS )TestFindPeaks>
   r  left_ips	right_ipsr-  r,  r.  peak_heightswidth_heightsleft_thresholdsright_thresholdsc                     d}t          t          j        d          ||||          \  }}|j        dk    sJ | j        D ]}||         j        dk    sJ dS )z@
        Test behavior for signal without local maxima.
        r6  r\   r  	thresholdr  widthr   N)r   r   r}   r   property_keys)rS   open_intervalr   propskeys        r%   test_constantzTestFindPeaks.test_constantl  s     %!"'"++)6--:-Q Q Qu zQ% 	( 	(C:?a'''''	( 	(r'   c                     t          j        g d          }t          j        |j        dz  dz             }||ddd<   t          j        |j        t
                    }|ddd         |ddd<   t          j        ||          }t          |d          \  }}t          |g dd	
           t          |d         |d	
           t          |d         ||dz
  dz  z
  d	
           t          |d         ||dz  z   d	
           t          t          |d          d         g dd	
           t          t          |d          d         g dd	
           t          t          |d          d         ddgd	
           dS )z8
        Test plateau size condition for peaks.
        )r)   r   r^   rb   rd   rm   o   r   r)   Nr   r6  )plateau_size)r)   r^   rg   rh   rf   !   rW   FrM   plateau_sizesro   rp   rb   r   )rh   rf   rQ  rW   )Nr  )r)   r^   rg   )rj   r]   rf   rQ  )	r   rO   r   r   r}   r-   r   r   r   )rS   rR  rT   repeatsr   rK  s         r%   test_plateau_sizezTestFindPeaks.test_plateau_sizex  s   
 !9!9!9::H]'!+a/00!$Q$'!&,,,!$Q$1Ia!! "!,???u999uMMMMo.5QQQQl+Uma6GA5M-M$)	+ 	+ 	+ 	+m,emq6H.H$)	+ 	+ 	+ 	+ 	
11555a8:K:K:K$)	+ 	+ 	+ 	+
1;???BIII$)	+ 	+ 	+ 	+
17;;;A>R$)	+ 	+ 	+ 	+ 	+ 	+r'   c                 6   d}t          |d          \  }}t          |t          j        g d          d           t          |d         t          j        g d          d           t          t          |d	          d
         t          j        ddg          d           t          t          |d          d
         t          j        ddg          d           t          t          |d          d
         t          j        dg          d           dS )z2
        Test height condition for peaks.
        )r	  UUUUUU?r	  r
  r   r  r   r6  )r  r   FrM   rA  )rV  r
  r  r  r   r^   rj   )Nr^   r)   )r   r^   N)r   r   r   rO   rS   rT   r   rK  s       r%   test_height_conditionz#TestFindPeaks.test_height_condition  s3    )!!L999urx			22FFFFn-rx/G/G$)	+ 	+ 	+ 	+
1S111!4bh1v6F6F$)	+ 	+ 	+ 	+
1Y777:BHaV<L<L$)	+ 	+ 	+ 	+
1V444Q71#$)	+ 	+ 	+ 	+ 	+ 	+r'   c                    d}t          |d          \  }}t          |t          j        ddg          d           t          |d         t          j        d	d
g                     t          |d         t          j        ddg                     t          t          |d          d         t          j        dg          d           t          t          |d          d         t          j        g t                    d           t          t          |d          d         t          j        ddg          d           t          t          |d          d         t          j        dg          d           t          t          |d          d         t          j        g t                    d           dS )z5
        Test threshold condition for peaks.
        )r   r   r)   rb   r8   r6  )rG  r)   r^   FrM   rC  r|   rs   rD  rr   rt   r   r   r  r   )Nrj   )Nrb   )r   rb   N)r   r   r   rO   r-   rW  s       r%   test_threshold_conditionz&TestFindPeaks.test_threshold_condition  s    !!|<<<urxA//UCCCC/0"(C:2F2FGGG0128S#J3G3GHHH
122215rx}}$)	+ 	+ 	+ 	+
1444Q7"C9P9P9P$)	+ 	+ 	+ 	+
1	:::1=rxA?O?O$)	+ 	+ 	+ 	+
1	:::1=rx}}$)	+ 	+ 	+ 	+
1777:BHRs<S<S<S$)	+ 	+ 	+ 	+ 	+ 	+r'   c                 F   t          j        ddd          }t          j        d          }||xx         t          j        dd|j                  z  cc<   t          t          |d          d         |d           t          |d	          d         }t          j        ||d
          j        dk    sJ t          j        |          }t          |dt          j	        |          z             g d}t          |d          d         }|j        dk    r|d         dk    sJ dS )z4
        Test distance condition for peaks.
        r)      r^   r   distancer   FrM   g9m4 @T)assume_uniqueru   )r)   r8   r   r\   N)
r   r   r   r,   r   r   r   	setdiff1dr   	ones_like)rS   	peaks_allrT   peaks_subsetdfss        r%   test_distance_conditionz%TestFindPeaks.test_distance_condition  s.   
 IaQ''	HRLL	)Aq).999 	
1q111!4iUSSSS "!f555a8|L)4HHHMQRRRRR gl##Qr|C000111 !!b111!4 A%%,q/Q*>*>*>*>*>*>r'   c                 h   t          j        ddd          }t          j        ddd          }t          j        dd|j                  }||xx         |z  cc<   ||         ||dz            z
  }d}t          j        |d         |k    ||d         k    z            }t          ||          \  }}t          |||         d	
           t          |d         ||         d	
           t          |d         t          j        |d                              t          |d         ||         dz   d	
           dS )z6
        Test prominence condition for peaks.
        r   r\   rW   r)   c   r   )r^   rk   )r  FrM   r,  r-  r.  N)r   r,   r   r   nonzeror   r   
zeros_like)	rS   rT   
peaks_trueoffsetr,  intervalkeep
peaks_calc
propertiess	            r%   test_prominence_conditionz'TestFindPeaks.test_prominence_condition  sV    K2s##Yq"a((
QJO44	*
ma
Q&77za[K'K8A;,FGI I ",A(!C!C!C
J
Jt$4%HHHH
=1;t3DRWXXXX
<0j&>??	A 	A 	A
=1:d3Ca3G$)	+ 	+ 	+ 	+ 	+ 	+r'   c                    t          j        g d          }t          |dd          \  }}|j        dk    sJ t	          |dt          j        |          z             t          |d         t          j        dg                     t          |d	         t          j        d
g                     t          |d         t          j        dg                     t          |d         t          j        dg                     dS )z1
        Test width condition for peaks.
        )	r)   r   r)   r   r)   r   r8   rb   r   r7  r  )rH  r  r)   rg   r  g?rB  rr   r?  g@r@  g      @N)r   rO   r   r   r   rc  r   r   rW  s       r%   test_width_conditionz"TestFindPeaks.test_width_condition  s     H11122!!9FFFuzQqe!4!44555hTF););<<<o.
B40@0@AAAj)2:se+<+<===k*BJv,>,>?????r'   c                     d}g d}t          |||||          \  }}t          |          t          | j                  k    sJ | j        D ]}|j        ||         j        k    sJ dS )z+
        Test returned properties.
        r6  )
r   r)   r   r   r  r   r^   r   rj   rk   rF  N)r   r+   rI  r   )rS   rJ  rT   r   rK  rL  s         r%   test_propertieszTestFindPeaks.test_properties  s     %,,,!!)6--:-Q Q Qu 5zzS!3444444% 	1 	1C:s00000	1 	1r'   c                    t          t          d          5  t          t          j        d                     ddd           n# 1 swxY w Y   t          t          d          5  t          t          j        d                     ddd           n# 1 swxY w Y   t          t          d          5  t          t          j        d          d	           ddd           dS # 1 swxY w Y   dS )
z5
        Test exceptions raised by function.
        r   ry   r)   Nr+  r^  r\   r8   r]  )r   r<   r   r   rO   r}   r   r   s    r%   test_raiseszTestFindPeaks.test_raises  s    Jk222 	$ 	$rx{{###	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$Jk222 	( 	(rwv'''	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	(Jj111 	3 	3ry}}r2222	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3s5   "AA	A	&"BBB5$C&&C*-C*z(ignore:some peaks have a prominence of 0r  c                    t          g dddd          \  }}t          |dt          j        |          z             t          |d         t          j        |d                              t          |d         t          j        |d                              t          |d         t          j        |d                              dD ]}t          ||         |d	
           dS )z
        Test behavior of prominence and width calculation if the given window
        length is smaller than a peak's plateau size.

        Regression test for gh-9110.
        r   r6  r   )r  rH  r   r,  r  rB  )r-  r.  r?  r@  FrM   N)r   r   r   rc  rk  )rS   r   rK  rL  s       r%   test_wlen_smaller_plateauz'TestFindPeaks.test_wlen_smaller_plateau  s     "///l(41> > >uq2<#6#66777m,bmE-<P.Q.QRRRhuX)G)GHHHo.U?=S0T0TUUUI 	B 	BCE#J5AAAAA	B 	Br'   kwargsr^  rs   r  r6  rH  r7  c                     t          j        ddd          }|                                }d|j        _        t          |          \  }}t          |fi |\  }}t          ||           dS )z4
        Test readonly arrays are accepted.
        r   r\      FN)r   r,   r9   flags	writeabler   r   )rS   r{  rT   
x_readonlyr   _peaks_readonlys          r%   test_readonly_arrayz!TestFindPeaks.test_readonly_array  sn     K2r""VVXX
%*
"a==q&z<<V<<~.....r'   N)r   r   r   rI  rM  rT  rX  rZ  rg  rr  rt  rv  rx  r   r   r4  rz  r   r  r   r'   r%   r>  r>  e  s>       ? ? ?M
( 
( 
(+ + +:+ + + + + +(? ? ?4+ + +*@ @ @1 1 1	3 	3 	3 [ J EG GB BG GB  [X
	S	|$	)	(  / / / / /r'   r>  c                   2    e Zd Zd Zd Zd Zd Zd Zd ZdS )TestFindPeaksCwtc                     g d}d}t          ||          \  }}t          j        dt          |                    }t	          ||ddd          }t          ||dd	
           dS )zX
        Generate a series of gaussians and attempt to find the peak locations.
        rt   rs   r   g      4@r   g      I@r   r   r   r   N)
gap_threshmin_snr
min_lengthFz4Found maximum locations did not equal those expected)rN   err_msg)r0   r   r   r3   r   r   )rS   r   
num_pointsr   r   r  
found_locss          r%   test_find_peaks_exactz&TestFindPeaksCwt.test_find_peaks_exact6  s     433
1&*EE	83F,,#Iv!Q48: : :

H$) V	
 	
 	
 	
 	
 	
r'   c                 D   g d}d}t          ||          \  }}t          j        dt          |                    }d}t          j                            d           |t          j                            |          dz
  d|z  z  z  }t          ||dd	|d
z            }d}t          |          t          |          k    s
J |            t          j	        ||z
            }	t          j
        |          d
z  }
t          j                            |	|
dd|
 z              dS )zz
        Verify that peak locations are (approximately) found
        for a series of gaussians with added noise.
        r  r   r   gQ?or  r   r}  r)   rj   )r  r  r  z-Different number of peaks found than expectedzMaximum location differedzby more than N)r0   r   r   r3   randomseedrandr   r+   r   rO   r   r   )rS   r   r  r   r   r  	noise_ampr  r  diffs	max_diffss              r%   test_find_peaks_withnoisez*TestFindPeaksCwt.test_find_peaks_withnoiseE  s:   
 433
1&*EE	83F,,	
	x   binnZ00369EE	#Iv"45y1}N N N
 A:#h--//////zH,--HV$$q(	

$$UI7R%@Y%@%@8A 	B 	B 	B 	B 	Br'   c                    d}d}t           j                            d          }|                    |          dz
  d|z  z  }t          j        dd          }t          ||dd	
          }t          |          dk    sJ dS )zQ
        Verify that no peak is found in
        data that's just noise.
        rr   rW   iW
r  r   r\   r]   rj   r   )r  
noise_percr   N)r   r  RandomStater  r   r   r+   )rS   r  r  rngr   r  r  s          r%   test_find_peaks_nopeakz'TestFindPeaksCwt.test_find_peaks_nopeak[  s    
 	
i##I..XXj))C/!I+>	2r""#IvqRPPP
:!######r'   c                     t          dd          }t          j        g d          }t          ||t                     }t	          |t          j        dg          d           d S )N   r   )r)   r   r^   rb   )waveletrW   FrM   )r   r   rO   r   r   r   )rS   rT   r  as       r%   )test_find_peaks_with_non_default_waveletsz:TestFindPeaksCwt.test_find_peaks_with_non_default_waveletsh  s_    S!,,,''1fh7772:se,,%@@@@@@r'   c                 &   ddg}d}t          ||          \  }}t          j        dt          |          d          }d}t          j                            d          }||                    |          dz
  d|z  z  z  }|d	d
xx         dz  cc<   t          ||dddd          }t          j	        t                    5  |j        |j        k    sJ 	 ddd           n# 1 swxY w Y   t          ||dddd          }|j        |j        k    sJ dS )zq
        Verify that window_size is passed correctly to private function and
        affects the result.
        r|   r   r   g?皙?r  r  r      i@  r)   r^   N)r  r  r  window_sizerm   )r0   r   r   r3   r  r  r  r   r   r   AssertionErrorr   )	rS   r   r  r   r   r  r  r  r  s	            r%   test_find_peaks_window_sizez,TestFindPeaksCwt.test_find_peaks_window_sizeo  s   
 s
1&*EE	83FS11	i##H--chhz**S01Y;??	 	#c'a#Iv!Q/3G G G
]>** 	4 	4?hm33333	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 $Iv!Q/3E E E
(-//////s   ?CC#&C#c                     t          j        dt           j        d          }t          j        |          }d}t	          ||          }t           j                            |d           dS )z]
        Verify that the `width` argument
        in `find_peaks_cwt` can be a float
        r   r  r)       N)r   r   pisinr   r   assert_equal)rS   xsr   r  r  s        r%   test_find_peaks_with_one_widthz/TestFindPeaksCwt.test_find_peaks_with_one_width  sX    
 Yq"%&&F2JJ	#Iv66



B/////r'   N)	r   r   r   r  r  r  r  r  r  r   r'   r%   r  r  4  st        
 
 
B B B,$ $ $A A A0 0 04
0 
0 
0 
0 
0r'   r  )"r9   numpyr   r   r   r   scipy._lib._array_apir   r   scipy.signal._peak_findingr   r   r	   r
   r   r   r   r   scipy.signal.windowsr    scipy.signal._peak_finding_utilsr   r   r&   r0   rI   rK   r   r   r   r  r<  r>  r  r   r'   r%   <module>r     s                         B B B B B B B B	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 * ) ) ) ) ) R R R R R R R R  ! ! !($ ($ ($V8# 8# 8# 8# 8# 8# 8# 8#v_I _I _I _I _I _I _I _IDPL PL PL PL PL PL PL PLfx; x; x; x; x; x; x; x;vI6 I6 I6 I6 I6 I6 I6 I6XH H H8L/ L/ L/ L/ L/ L/ L/ L/^_0 _0 _0 _0 _0 _0 _0 _0 _0 _0r'   