
    tf*                        d dl mZ d dl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Zd dlmZ d dlmZ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mZ dZd Zd Z d Z!ddZ"y)    )annotationsN)Counter)reduce)product)mul)config)Arraynormalize_chunks)meta_from_array)tokenize)flatten)HighLevelGraph)Mparse_bytesa  
Dask's reshape only supports operations that merge or split existing dimensions
evenly. For example:

>>> x = da.ones((6, 5, 4), chunks=(3, 2, 2))
>>> x.reshape((3, 2, 5, 4))  # supported, splits 6 into 3 & 2
>>> x.reshape((30, 4))       # supported, merges 6 & 5 into 30
>>> x.reshape((4, 5, 6))     # unsupported, existing dimensions split unevenly

To work around this you may call reshape in multiple passes, or (if your data
is small enough) call ``compute`` first and handle reshaping in ``numpy``
directly.
c           
         t        d D              sJ t               dz
  }t        |      dz
  }t        t                     D cg c]  }d  }}t        t        |            D cg c]  }d  }}|dk\  s|dk\  r |   ||   k(  r|   ||<   |   ||<   |dz  }|dz  }1 |   }||   }	|dk(  rd||<   |dz  }ni|	dk(  rd||<   |dz  }nX||	k  ro|dz
  }
|
dk\  r=t        t         |
|dz          |	k  r$|
dz  }
|
dk\  rt        t         |
|dz          |	k  r$t        t         |
|dz          |	k7  rt        t              t         fdt        |      D              rKt        |dz         D ]
  }|   ||<    |   t        j                  t        t        |
|             z  ||<   nt        |
dz   |dz         D ]  } |   f||<    t        t        t        t        |
dz   |dz                }t        |
   |      ||
<   t        t         |
dz   |dz          t        fd||
   D              ||<   |dz  }|
dz
  }n||	kD  r|dz
  }|dk\  r=t        t        |||dz          |k  r$|dz  }|dk\  rt        t        |||dz          |k  r$t        t        |||dz          |k7  rt        t              t        t        ||dz   |dz          t        |         ||<   t        |dz   |dz         D ]  }||   f||<    t        fd||   D              ||<   |dz
  }|dz  }|dk\  r|dk\  rt        |      t        |      fS c c}w c c}w )Nc              3  <   K   | ]  }t        |t                y wN)
isinstancetuple).0cs     \/var/www/html/software/conda/envs/higlass/lib/python3.12/site-packages/dask/array/reshape.py	<genexpr>z"reshape_rechunk.<locals>.<genexpr>$   s     6z!U#6s      r   )r   c              3  F   K   | ]  }t        |         |   k(    y wr   )len)r   iinchunksinshapes     r   r   z"reshape_rechunk.<locals>.<genexpr>D   s#     Ea3x{#wqz1Es   !c              3  (   K   | ]	  }|z    y wr    )r   r   prods     r   r   z"reshape_rechunk.<locals>.<genexpr>R   s      -!"D1H-   c              3  (   K   | ]	  }|z    y wr   r!   )r   r   css     r   r   z"reshape_rechunk.<locals>.<genexpr>f   s     +QAG+Qr#   )allr   ranger   r   NotImplementedError_not_implemented_messagemathr"   mapexpand_tupler   contract_tuple)r   outshaper   iioir   result_inchunksresult_outchunksdindoutileftchunk_reductionoleftr%   r"   s   ` `          @@r   reshape_rechunkr8   #   s   6X6666	W	B	X	B%*3w<%89t9O9&+CM&:;;;
'R1W2;(2,&"*2,OB#+B<R !GB!GBbk|!8"&OB!GBQY#'R !GB4ZFE
vc7526+BCdJ
 
vc7526+BCdJ c752623t;)*BCC E59EErAv 5A)1!OA&5'/|diiXeB/07 ( $ uqy"q&1 7A*1!*OA&7 #)c#x	BQRF7S.T"U)5huo)W&c7519rAv#>?', -&5e&<- ( $ !GBB4ZFE1*Xeb1f-E!F!L
 1*Xeb1f-E!F!Lc8EBF34;)*BCCXeai"q&9:B"0"r"BOB519b1f- 5'/{n #5 ',+Q_R=P+Q&QU#B!GB 'R1WB !5)9#:::I :;s   	M,	Mc                &   |dk(  r| S g }| D ]`  }|}t        ||z  d      }|d|z  k\  r1|j                  t        |             |t        |      z  }|d|z  k\  r1|sP|j                  |       b t        |       t        |      k(  sJ t	        |      S )z

    >>> expand_tuple((2, 4), 2)
    (1, 1, 2, 2)

    >>> expand_tuple((2, 4), 3)
    (1, 1, 1, 1, 2)

    >>> expand_tuple((3, 4), 2)
    (1, 2, 2, 2)

    >>> expand_tuple((7, 4), 3)
    (2, 2, 3, 1, 1, 2)
    r      )maxappendintsumr   )chunksfactoroutr   xparts         r   r,   r,   n   s     {
C 1v:q!1t8mJJs4y!TNA 1t8m JJqM v;#c(""":    c                    t        |       |z  dk(  sJ g }d}| D ]*  }||z  }||z  }||z  }||z  }|s|j                  |       , t        |      S )zReturn simple chunks tuple such that factor divides all elements

    Examples
    --------

    >>> contract_tuple((2, 2, 8, 4), 4)
    (4, 8, 4)
    r   )r>   r<   r   )r?   r@   rA   residualchunkdivgoods          r   r-   r-      st     v;1$$$
CH vo6>|JJt :rD   c                J	    ddl m} ddlm} t	        t        ||            }|D cg c]
  }|dk7  s	| }}t        |      t        |      k  r|t        |      t        |      z
  dkD  rt        d      t        |      dk(  r| j                  dk(  r| S  || j                  t        t        |d      z         t	         fd|D              }t        j                  t        | j                              r!t        dt!        | j                        z        t        t        |d      | j                  k7  rt        d	      | j                  |k(  r| S t#        | t        |            }d
t%        | |      z   }	| j&                  dk(  rt)        t+        | j-                                     }
|	fdt        |      z  z   t.        j0                  |
|fi}t	        d |D              }t3        j4                  |	|| g      }t7        ||	||      S t        | j                        }t        |      }|s1||kD  r,| j9                  t;        ||z
        D ci c]  }|d c}      } t=        | j                  || j>                        \  }}t        t        |D cg c]  }tA        |       c}      | jB                  jD                  z  }|5tG        tI        jJ                  d            }tI        jJ                  dd      }ntG        |      }d}||kD  r|d}tM        jN                  ||d       nt|rrtQ        |      tQ        |      z  }g }|D ]:  }||   dkD  r|jS                  |       ||xx   dz  cc<   *|jS                  d       < tU        |||| jB                  |      }| j9                  |      }tW        tY        |jZ                  gg|D cg c]  }t;        t        |             c}       }tW        tY        |	gg|D cg c]  }t;        t        |             c}       }tW        tY        |       }t]        |||      D ci c]  \  }}}|t.        j0                  ||f }}}}t3        j4                  |	||g      }t7        ||	||      S c c}w c c}w c c}w c c}w c c}w c c}}}w )ab  Reshape array to new shape

    Parameters
    ----------
    shape : int or tuple of ints
        The new shape should be compatible with the original shape. If
        an integer, then the result will be a 1-D array of that length.
        One shape dimension can be -1. In this case, the value is
        inferred from the length of the array and remaining dimensions.
    merge_chunks : bool, default True
        Whether to merge chunks using the logic in :meth:`dask.array.rechunk`
        when communication is necessary given the input array chunking and
        the output shape. With ``merge_chunks==False``, the input array will
        be rechunked to a chunksize of 1, which can create very many tasks.
    limit: int (optional)
        The maximum block size to target in bytes. If no limit is provided,
        it defaults to using the ``array.chunk-size`` Dask config value.

    Notes
    -----
    This is a parallelized version of the ``np.reshape`` function with the
    following limitations:

    1.  It assumes that the array is stored in `row-major order`_
    2.  It only allows for reshapings that collapse or merge dimensions like
        ``(1, 2, 3, 4) -> (1, 6, 4)`` or ``(64,) -> (4, 4, 4)``

    .. _`row-major order`: https://en.wikipedia.org/wiki/Row-_and_column-major_order

    When communication is necessary this algorithm depends on the logic within
    rechunk.  It endeavors to keep chunk sizes roughly the same when possible.

    See :ref:`array-chunks.reshaping` for a discussion the tradeoffs of
    ``merge_chunks``.

    See Also
    --------
    dask.array.rechunk
    numpy.reshape
    r   )PerformanceWarning)sanitize_indexr   z&can only specify one unknown dimensionc              3  0   K   | ]  }|d k(  rn|  yw)rM   Nr!   )r   smissing_sizes     r   r   zreshape.<locals>.<genexpr>   s     Da2gl14Ds   z_Array chunk size or shape is unknown. shape: %s

Possible solution with x.compute_chunk_sizes()z)total size of new array must be unchangedzreshape-)r   c              3  "   K   | ]  }|f 	 y wr   r!   )r   ds     r   r   zreshape.<locals>.<genexpr>   s     +t+s   )dependencies)metaNzarray.chunk-sizez array.slicing.split-large-chunksTa  Reshaping is producing a large chunk. To accept the large
chunk and silence this warning, set the option
    >>> with dask.config.set(**{'array.slicing.split_large_chunks': False}):
    ...     array.reshape(shape)

To avoid creating the large chunks, set the option
    >>> with dask.config.set(**{'array.slicing.split_large_chunks': True}):
    ...     array.reshape(shape)Explicitly passing ``limit`` to ``reshape`` will also silence this warning
    >>> array.reshape(shape, limit='128 MiB')   )
stacklevelauto)shapelimitdtypeprevious_chunks)/dask.array.corerK   dask.array.slicingrL   r   r+   r   
ValueErrorndimsizer   r   npisnanr>   rX   strr   r   npartitionsnextr   __dask_keys__r   reshaper   from_collectionsr	   rechunkr'   r8   r?   r;   rZ   itemsizer   r   getwarningswarnr   r<   r
   listr   namezip)!rB   rX   merge_chunksrY   rK   rL   rO   known_sizesrT   ro   keydskr?   graphr3   r4   r   r   	outchunksmax_chunksize_in_bytessplitmsgmatching_chunks
chunk_planrA   x2r   in_keysout_keysshapesabrP   s!                                   @r   rg   rg      s!   T 31#ne,-E#/qBw1/K/
;#e*$u:K((1,EFF u:?qvv{H%affvc;/J&JKDeDD	xxAGG=?B177|L
 	

 c5!&DEEww%1c%j)DE**D}}71??,-.wE
**QYYU,CD+U++//cLUD&t44 agg,Cu:DC$JIIU3:%67q!t78)!''5!((CHi#C))DQ#a&)DEHXHXX}FJJ'9:;

=tDE"%=@  MM#1a@%h/')2DDOJ  ."3'!+%%c*#C(A-(%%f-. )gg (I 
8	B 7BGG9Ix'H!c!f'HIJGGTFHY%GeCFm%GHIH'9%&F7:8Wf7U
V
V1e1qyy!U##
VC
V++D#RDIEid33y 0L 8 *EX (I%G
Vs(   
RR
R
R
=R3R:R)TN)#
__future__r   r*   rl   collectionsr   	functoolsr   	itertoolsr   operatorr   numpyra   daskr   r\   r	   r
   dask.array.utilsr   	dask.baser   	dask.corer   dask.highlevelgraphr   
dask.utilsr   r   r)   r8   r,   r-   rg   r!   rD   r   <module>r      sQ    "         3 ,   . % H;V>.J4rD   