
    bi+                        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	Z
d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ  ee
j*                        dd       Zd Zd ZddZddZ ee
      d        Zy)    )annotationsN)Iterator)wraps)Number)merge)Array)tokenize)HighLevelGraph)derived_fromc                V   t        |       }t        |       sd |fS t        |t              rt        |      }| j                  j
                  dk(  rwt        j                  | j                  j                  ||      }dd l
}|j                  j                  || j                  j                  | j                  j                        |fS t        | j                        j                   dk(  r2dd l
}t        | |j"                  |j$                  f      r| j&                  } t        j(                  | j                  t        j*                        r| }t        |       j                   dv r|j-                  d      }n|j/                  d      }t        j                  |||      j-                  |j                        }|d   dk(  r t1        |d   |j1                               |d<   ||fS t        j(                  | j                  t        j2                        sd}t        j                  | ||      |fS )Ncategory)methodr   DatetimeTZDtype)SeriesIndexi8nearest)len
isinstancer   listdtypenamenp
percentilecatcodespandasCategorical
from_codes
categoriesorderedtype__name__r   r   values
issubdtype
datetime64astypeviewminnumber)aqr   nresultpdr$   a2s           P/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/dask/array/percentile.py_percentiler2      s   AAq6Qw!XGww||z!quu{{Af=~~((1C1CQWW__UWXXXAGG}!22a"))RXX./A	}}QWWbmm,722t$BT"Br1V4;;FLLIQ419F1Ivzz|4F1Iqy==")),==Af-q00    c                B    ddl m}  |       }|j                  |        |S )Nr   TDigest)crickr6   update)r+   r6   ts      r1   _tdigest_chunkr:   3   s    	AHHQKHr3   c                    ddl m}  |       } |j                  |  t        j                  |j                  | dz              S )Nr   r5   g      Y@)r7   r6   r   r   arrayquantile)qsdigestsr6   r9   s       r1   _percentiles_from_tdigestr@   <   s6    	AAGGW88AJJrEz*++r3   c           	     V   ddl m} ddlm} ddlm}m} | j                  dkD  rVt        j                  || j                  j                  dk(  r| j                  j                  d      nd      } || |fd|i|S g d	}	||	v rt        j                  d
t               |}d|v r+t        j                  dt               |j!                  d      }|rt#        d|j%                                | j                  dk(  st'        d      t)        |t*              r|g} || ||             }t-        | ||      }
| j                  }t        j.                  |t        j0                        r |g | ||             dz  j                  } || |      }||	vrt3        d|	       |dk(  r|dk(  rt        j.                  |t        j4                        s$t        j.                  |t        j0                        rdddlm}  |dd       d|
z   }t;        | j=                               D ci c]  \  }}||ft>        |f }}}d|
z   }|dft@        |tC        |      fi}nt        jD                  |dd      }d|d<   d|
z   }t;        | j=                               D ci c]  \  }}||f||||f }}}d|
z   }|dftF        ||gtI        | jJ                  d         z  tC        |      |fi}tM        ||      }tO        jP                  ||| g       }tS        ||tI        |      ff|!      S c c}}w c c}}w )"u  Approximate percentile of 1-D array

    Parameters
    ----------
    a : Array
    q : array_like of float
        Percentile or sequence of percentiles to compute, which must be between
        0 and 100 inclusive.
    method : {'linear', 'lower', 'higher', 'midpoint', 'nearest'}, optional
        The interpolation method to use when the desired percentile lies
        between two data points ``i < j``. Only valid for ``internal_method='dask'``.

        - 'linear': ``i + (j - i) * fraction``, where ``fraction``
          is the fractional part of the index surrounded by ``i``
          and ``j``.
        - 'lower': ``i``.
        - 'higher': ``j``.
        - 'nearest': ``i`` or ``j``, whichever is nearest.
        - 'midpoint': ``(i + j) / 2``.

        .. versionchanged:: 2022.1.0
            This argument was previously called "interpolation"

    internal_method : {'default', 'dask', 'tdigest'}, optional
        What internal method to use. By default will use dask's internal custom
        algorithm (``'dask'``).  If set to ``'tdigest'`` will use tdigest for
        floats and ints and fallback to the ``'dask'`` otherwise.

        .. versionchanged:: 2022.1.0
            This argument was previously called “method”.

    interpolation : str, optional
        Deprecated name for the method keyword argument.

        .. deprecated:: 2022.1.0

    See Also
    --------
    numpy.percentile : Numpy's equivalent Percentile function
    r   )percentile_lookup)r=   )
array_safemeta_from_array   fd   r   )defaultdasktdigestz8The `method=` argument was renamed to `internal_method=`interpolationzFThe `interpolation=` argument to percentile was renamed to `method= ` z0percentile() got an unexpected keyword argument z+Percentiles only implemented for 1-d arrayslike)r   rM         ?)r   z"`internal_method=` must be one of rJ   linear)import_requiredr7   z=crick is a required dependency for using the t-digest method.zpercentile_tdigest_chunk-zpercentile_tdigest-constant)modezpercentile_chunk-zpercentile-)dependencies)chunksmeta)*dask.array.dispatchrB   dask.array.reductionsr=   dask.array.utilsrC   rD   ndimr   true_divider   kindr"   warningswarnFutureWarningpop	TypeErrorkeysNotImplementedErrorr   r   r	   r%   integer
ValueErrorfloating
dask.utilsrP   	enumerate__dask_keys__r:   r@   sortedpadmerge_percentilesr   rU   r   r
   from_collectionsr   )r+   r,   r   internal_methodkwargsr2   r=   rC   rD   allowed_internal_methodstokenr   rV   rP   r   ikeydskname2dsk2calc_qgraphs                         r1   r   r   E   s'   R E.<vvzNN1177<<33Faggll3/CP16V6v66=))F	
 !& T	
 O,>v{{}oN
 	
 66Q;!"OPP!VC1?1-.AQ6"EGGE	}}UBJJ'Be/!2DEKRR1E*D6601I0JK
 	
 	9$h]]5"++."--rzz2R.T	
 +U2;DQ__EV;W
17CT1I,,
 
 &-
66#;GH
 1:.r
"U* $AOO$56
3 1IS&&99
 

 %AJ!3qxx{++s
 T
C++E3aSIEAyl>>A

s   !LL%c                @   ddl m} t        | t              rt	        |       }  || |       } t	        t        t        |            }t	        |      }|t        | \  }}t	        |      }t	        t        t        |||      D 	cg c]  \  }}}	|	s
|||	f c}	}}       }
|
s@|rt        d      t        j                  t        |d         dz
  t        j                        S |
\  }}}|d   j                  j                  dk(  rct        | ||D cg c]  }|j                   c}|||      }ddl}|j"                  j%                  ||d   j&                  |d   j(                        S t        j*                  |d   j                  t        j,                        sd}t        |      t        |      k7  st        |      t        |      k7  rt        d	      g }t        ||      D ]f  \  }}	t        j.                  | t        |      
      }t        j0                   |||d               |dd |d   |d<   ||	z  }|j3                  |       h t        j4                  |      } |t        j4                  |      |      }t        j6                  |      }t        j8                  ||      }t        j8                  ||      }t        j:                  |      } || |      } | t=        |      z  }|dk(  rt        j>                  |||      }|S t        j@                  ||d      }t        j@                  ||d      dz
  }t        jB                  |t        |      dz
  |       t        jB                  ||      }t        jD                  ||      }|dk(  r||   }|S |dk(  r||   }|S |dk(  rd||   ||   z   z  }|S |dk(  rLt        jF                  ||   |z
        }t        jF                  ||   |z
        }||kD  }|}||   ||<   ||   }|S t        d      c c}	}}w c c}w )a  Combine several percentile calculations of different data.

    Parameters
    ----------

    finalq : numpy.array
        Percentiles to compute (must use same scale as ``qs``).
    qs : sequence of :class:`numpy.array`s
        Percentiles calculated on different sets of data.
    vals : sequence of :class:`numpy.array`s
        Resulting values associated with percentiles ``qs``.
    Ns : sequence of integers
        The number of data elements associated with each data set.
    method : {'linear', 'lower', 'higher', 'midpoint', 'nearest'}
        Specify the interpolation method to use to calculate final
        percentiles.  For more information, see :func:`numpy.percentile`.

    Examples
    --------

    >>> finalq = [10, 20, 30, 40, 50, 60, 70, 80]
    >>> qs = [[20, 40, 60, 80], [20, 40, 60, 80]]
    >>> vals = [np.array([1, 2, 3, 4]), np.array([10, 11, 12, 13])]
    >>> Ns = [100, 100]  # Both original arrays had 100 elements

    >>> merge_percentiles(finalq, qs, vals, Ns=Ns)
    array([ 1,  2,  3,  4, 10, 11, 12, 13])
    r   )rC   rL   NzNo non-trivial arrays found   r   r   z3qs, vals, and Ns parameters must be the same length)shaperE   rO   left)sideright)outlowerhighermidpointrN   zVinterpolation method can only be 'linear', 'lower', 'higher', 'midpoint', or 'nearest')$rY   rC   r   r   r   mapzipre   r   fullr   nanr   r   rl   r   r   r   r   r    r!   r%   r*   
empty_likediffappendconcatenateargsorttakecumsumsuminterpsearchsortedminimummaximumabs)finalqr>   valsr   Nsraise_on_nanrC   r,   valNLvr.   r/   countscountcombined_valscombined_counts
sort_order
combined_q	desired_qrvr|   r~   r   upperlower_residualupper_residualmaskindexs                                 r1   rl   rl      s   : ,&(#fV,F	c$m	B:D	z:b	bBSBb0AGG91c1QAsA;GHIA:;;wws2a5zA~rvv..LBb Aw}}Z'"B$/Q/\
 	~~((a1C1CT!W__UU==a		2
4yCGs2w#b'1NOO FB 1fCF3GGJqqt45ab	Q4a
e NN4(M !7mLOM*JGGM:6Mggoz:O ?+J ]3FR I YYy*m<2 I/ z96B
IGDqH


4]+a/T:

4'

4'Wu%B" I! xu%B I z!e,}U/CCDB I y VVJu$5	$ABNVVJu$5	$ABN!N2DE+E$Ku%B I	 5 K H 0s   PPPc                    ddl m} t        j                  || j                  j
                  dk(  r| j                  j                  d      nd      } || |fi |S )Nr   )nanquantilerF   rG   )rX   r   r   r[   r   r\   r"   )r+   r,   ro   r   s       r1   nanpercentiler   A  sE    1
qqww||s/B!'',,s+LAq!&v&&r3   )rO   )rO   rH   )r   NT)
__future__r   r]   collections.abcr   	functoolsr   numbersr   numpyr   tlzr   dask.array.corer   	dask.baser	   dask.highlevelgraphr
   rg   r   r   r2   r:   r@   rl   r    r3   r1   <module>r      su    "  $     !  . # r}}1 1B,E?Pqh b' 'r3   