
    tf*                        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y)    )annotationsN)Iterator)wraps)Number)merge)Array)NUMPY_GE_122)
percentile)tokenize)HighLevelGraphc                N   t        |       }t        |       sd |fS t        |t              rt        |      }| j                  j
                  dk(  rmt        | 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,ddlm} |r|j1                  d      }n#|j3                  d      }n|j3                  d      }t        |||      j1                  |j                        }|d   dk(  r t5        |d   |j5                               |d<   ||fS t'        j(                  | j                  t&        j6                        sd}t        | ||      |fS )	Ncategory)methodr   DatetimeTZDtype)SeriesIndex)PANDAS_GE_200i8nearest)len
isinstancer   listdtypenamenp_percentilecatcodespandasCategorical
from_codes
categoriesorderedtype__name__r   r   valuesnp
issubdtype
datetime64dask.dataframe._compatr   astypeviewminnumber)	aqr   nresultpdr%   r   a2s	            _/var/www/html/software/conda/envs/higlass/lib/python3.12/site-packages/dask/array/percentile.py_percentiler5      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<]]4([[&T"Br1V4;;FLLIQ419F1Ivzz|4F1Iqy==")),Af-q00    c                B    ddl m}  |       }|j                  |        |S )Nr   TDigest)crickr9   update)r.   r9   ts      r4   _tdigest_chunkr=   9   s    	AHHQKHr6   c                    ddl m}  |       } |j                  |  t        j                  |j                  | dz              S )Nr   r8   g      Y@)r:   r9   r   r&   arrayquantile)qsdigestsr9   r<   s       r4   _percentiles_from_tdigestrC   B   s6    	AAGGW88AJJrEz*++r6   c           	        ddl m} ddlm}m} g d}||v rt        j                  dt               |}d|v r1t        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$        j(                        r |g |
 ||             dz  j"                  }
 || |
      }||vrt+        d|       |dk(  r|dk(  rt%        j&                  |
t$        j,                        s$t%        j&                  |
t$        j(                        rdddlm}  |dd       d|	z   }t3        | j5                               D ci c]  \  }}||ft6        |f }}}d|	z   }|dft8        |t;        |      fi}nt%        j<                  |d	d      }d|d<   d|	z   }t3        | j5                               D ci c]  \  }}||f||||f }}}d|	z   }|dft>        ||gtA        | jB                  d         z  t;        |      |fi}tE        ||      }tG        jH                  ||| g      }tK        ||tA        |      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)
array_safemeta_from_array)default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   rN         ?)r   z"`internal_method=` must be one of rJ   linear)import_requiredr:   z=crick is a required dependency for using the t-digest method.zpercentile_tdigest_chunk-zpercentile_tdigest-constant)moded   zpercentile_chunk-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.utilsrQ   	enumerate__dask_keys__r=   rC   sortedpadmerge_percentilesr   rW   r   r   from_collectionsr   )r.   r/   r   internal_methodkwargsr5   rF   rG   allowed_internal_methodstokenr   rX   rQ   r   ikeydskname2dsk2calc_qgraphs                        r4   r
   r
   K   s   R E<=))X	
 !& MM
 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   <J:.K 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   )rF   rM   NzNo non-trivial arrays found   r   r   z3qs, vals, and Ns parameters must be the same length)shaperL   rP   left)siderightlowerhighermidpointrO   zVinterpolation method can only be 'linear', 'lower', 'higher', 'midpoint', or 'nearest')$rZ   rF   r   r   r   mapziprd   r&   fullr   nanr   r   rk   r   r   r   r    r!   r"   r'   r-   
empty_likediffappendconcatenateargsorttakecumsumsuminterpsearchsortedminimummaximumabs)finalqrA   valsr   Nsraise_on_nanrF   r/   valNLvr1   r2   countscountcombined_valscombined_counts
sort_order
combined_q	desired_qrvr{   r}   r~   upperlower_residualupper_residualmaskindexs                                 r4   rk   rk      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/6

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)rP   )rP   rH   )r~   NT)
__future__r   r[   collections.abcr   	functoolsr   numbersr   numpyr&   tlzr   dask.array.corer   dask.array.numpy_compatr	   r
   r   	dask.baser   dask.highlevelgraphr   r5   r=   rC   rk    r6   r4   <module>r      s^    "  $     ! 0 ?  . r}}#1 #1L,A?Hqr6   