
    Cd)                        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 d dlmZ  ee
j                  dd            Zd Zd ZddZddZdS )    )annotationsN)Iterator)wraps)Number)merge)Array)
_numpy_122)
percentile)tokenize)HighLevelGraphlinearc                   t          |           }t          |           sd |fS t          |t                    rt          |          }| j        j        dk    rRt          | j        j        ||          }dd l	}|j
                            || j        j        | j        j                  |fS t          | j                  j        dk    r'dd l	}t          | |j        |j        f          r| j        } t'          j        | j        t&          j                  r|| }|                    d          }t          |||                              |j                  }|d         dk    r+t1          |d         |                                          |d<   ||fS t'          j        | j        t&          j                  sd}t          | ||          |fS )Ncategory)methodr   DatetimeTZDtypei8nearest)len
isinstancer   listdtypenamenp_percentilecatcodespandasCategorical
from_codes
categoriesorderedtype__name__SeriesIndexvaluesnp
issubdtype
datetime64viewastypeminnumber)aqr   nresultpdr%   a2s           5lib/python3.11/site-packages/dask/array/percentile.py_percentiler4      s   AAq66 Qw!X GGw|z!!qu{Af===~((1CQW_UUWXXXAG}}!222a")RX.// 	A	}QWbm,, [[r1V444;;FLIIQ4199F1Ivzz||44F1Iqy="),, Af---q00    c                P    ddl m}  |            }|                    |            |S )Nr   TDigest)crickr8   update)r-   r8   ts      r3   _tdigest_chunkr<   1   s3    		AHHQKKKHr5   c                    ddl m}  |            } |j        |  t          j        |                    | dz                      S )Nr   r7   g      Y@)r9   r8   r   r&   arrayquantile)qsdigestsr8   r;   s       r3   _percentiles_from_tdigestrB   :   sN    		AAGW8AJJrEz**+++r5   defaultc                   ddl m ddlm}m} g d}|v rt          j        dt                     }d|v r6t          rt          j        dt                     |	                    d          |r$t          d|                                           | j        d	k    st          d
          t          |t                    r|g} || ||                     }t!          | |          }| j        }	t%          j        |	t$          j                  r |g |	 ||                     dz  j        }	 || |	          }
||vrt+          d|           |dk    rdk    rt%          j        |	t$          j                  st%          j        |	t$          j                  reddlm}  |dd           d|z   fdt3          |                                           D             }d|z   }|dft6          |t9          |          fi}nt%          j        |d	d          dd<   d|z   fdt3          |                                           D             }d|z   }|dft<          |gt?          | j         d                   z  t9          |          fi}tC          ||          }tE          j#        ||| g          }tI          ||t?          |          ff|
           S )!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 ``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)
array_safemeta_from_array)rC   dasktdigestzJIn Dask 2022.1.0, the `method=` argument was renamed to `internal_method=`interpolationzXIn Dask 2022.1.0, the `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 rI   r   )import_requiredr9   z=crick is a required dependency for using the t-digest method.zpercentile_tdigest_chunk-c                .    i | ]\  }}|ft           |fS  )r<   ).0ikeyr   s      r3   
<dictcomp>zpercentile.<locals>.<dictcomp>   s5     
 
 
17CT1I,
 
 
r5   zpercentile_tdigest-constant)moded   zpercentile_chunk-c                (    i | ]\  }}|f|fS rQ   rQ   )rR   rS   rT   r4   calc_qr   r   s      r3   rU   zpercentile.<locals>.<dictcomp>   s<     
 
 
3 1IS&&9
 
 
r5   zpercentile-)dependencies)chunksmeta)%dask.array.dispatchrE   dask.array.utilsrF   rG   warningswarnFutureWarningr	   pop	TypeErrorkeysndimNotImplementedErrorr   r   r   r   r&   r'   integer
ValueErrorfloating
dask.utilsrO   	enumerate__dask_keys__rB   sortedpadmerge_percentilesr   r]   r   r   from_collectionsr   )r-   r.   r   internal_methodkwargsrF   rG   allowed_internal_methodstokenr   r^   rO   dskname2dsk2graphr4   r[   r   s     `             @@@r3   r
   r
   C   s   R EDDDDD<<<<<<<<===)))X	
 	
 	
 !&   	M  
 O,, 
Nv{{}}NN
 
 	
 6Q;;!"OPPP!V C
1??1--...AQ6""EGE	}UBJ'' SBe//!2D2DEEEKR?1E***D666K1IKK
 
 	
 	9$$h]5"+.. 24-rz2R2R  	/.....T	
 	
 	
 +U2
 
 
 
;DQ__EVEV;W;W
 
 
 &-
66#;;GH
 1:...r
"U*
 
 
 
 
 
 
#AOO$5$566
 
 

 %AJ!3qx{+++s
 T

C+E3aSIIIEAyl>>>>r5   lowerTc           
        ddl m} t          | t                    rt	          |           }  || |           } t	          t          t          |                    }t	          |          }|t          | \  }}t	          |          }t	          t          d t          |||          D                        }|sF|rt          d          t          j	        t          |d                   dz
  t          j                  S |\  }}}|d         j        j        dk    rTt          | |d	 |D             |||          }ddl}	|	j                            ||d         j        |d         j                  S t          j        |d         j        t          j                  sd
}t          |          t          |          k    s t          |          t          |          k    rt          d          g }
t          ||          D ]w\  }}t          j        | t          |                    }t          j         |||d                             |dd<   |d         |d<   ||z  }|
                    |           xt          j        |          } |t          j        |
          |          }t          j        |          }t          j        ||          }t          j        ||          }t          j        |          } || |          } | t;          |          z  }|dk    rt          j        |||          }n%t          j        ||d          }t          j        ||d          dz
  }t          j         |t          |          dz
  |           t          j         ||          }t          j!        ||          }|dk    r	||         }n|dk    r	||         }n|dk    rd||         ||         z   z  }nk|d
k    rVt          j"        ||         |z
            }t          j"        ||         |z
            }||k    }|}||         ||<   ||         }nt          d          |S )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   )rF   rL   Nc                $    g | ]\  }}}||||fS rQ   rQ   )rR   r.   valNs       r3   
<listcomp>z%merge_percentiles.<locals>.<listcomp>   s)    GGG91c1QGAsA;GGGr5   zNo non-trivial arrays found   r   c                    g | ]	}|j         
S rQ   )r   )rR   vs     r3   r   z%merge_percentiles.<locals>.<listcomp>   s    ///Q///r5   r   z3qs, vals, and Ns parameters must be the same length)shaperK   r   left)siderightr{   highermidpointrN   zVinterpolation method can only be 'linear', 'lower', 'higher', 'midpoint', or 'nearest')#r`   rF   r   r   r   mapziprj   r&   fullr   nanr   r   rq   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_nanrF   Lr0   r1   countsr.   r   countcombined_valscombined_counts
sort_order
combined_q	desired_qrvr   r   r{   upperlower_residualupper_residualmaskindexs                               r3   rq   rq      se   : ,+++++&(## fZV,,,F	c$mm		B::D	z:b	bBSGGBb0A0AGGGHIIA / 	<:;;;ws2a5zzA~rv...LBb Aw}Z''"B//$///\
 
 	~((a1CT!W_UUU=a	22 
4yyCGGs2ww#b''11NOOO FB  1fCFF333GJJqqt44455abb	Q4a
e N4((M j!7!7mLLLOM**JGM:66Mgoz::O ?++J Z]333FR I Yy*m<<z96BBB
IGDDDqH

4]++a/666
4''
4''Wu%BBxu%BBz!!e,}U/CCDBBy  VJu$5	$ABBNVJu$5	$ABBN!N2DE+E$Ku%BB5   Ir5   )r   )r   rC   )r{   NT)
__future__r   ra   collections.abcr   	functoolsr   numbersr   numpyr&   tlzr   dask.array.corer   dask.array.numpy_compatr	   r
   r   	dask.baser   dask.highlevelgraphr   r4   r<   rB   rq   rQ   r5   r3   <module>r      sV   " " " " " "  $ $ $ $ $ $                       ! ! ! ! ! ! . . . . . . ? ? ? ? ? ?       . . . . . . r}1 1 1 1<  , , ,A? A? A? A?Hq q q q q qr5   