
    tf                         d dl mZ d dlmZ d dl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mZmZ dd	lmZmZ d
 ZddZddZd ZddZd Z	 ddZ y# e$ r
  ed      dw xY w)    )product)ceilN)COOz,The 'sparse' package is required to use dask)tokenize   )	CSRReader
query_rect)parse_cooler_uri	partitionc                 >   t        j                  | d      5 }||   }|t        |j                               }t	        ||d            }i }|D ]B  }t        j
                  ||   j                        }|)t        ||j                        ||<   D t        j                  |D ci c]'  }|t        j                  g ||   j                        ) c}|      }	|D ]   }t        j                  g ||   d      |	|<   " 	 d d d        |	fS c c}w # 1 sw Y   xY w)	Nrr   )enum)key)dtype)columnsT)
categoriesordered)h5pyFilelistkeyslencheck_dtyper   sorted__getitem__pd	DataFramenparrayCategorical)
path	grouppathr   fgrpnrowscategoricalsr   dtmetas
             ]/var/www/html/software/conda/envs/higlass/lib/python3.12/site-packages/cooler/sandbox/dask.py_get_group_infor*      s!   	4	 W	l<
#DCQL! 	CC!!s3x~~6B~$*22>>$BS!	C ||@DES"((2SX^^44Et
   	WCrl36GQUVDI	W'W, $l** F!W Ws$   AD6/D%,D
.DDDc                    	 ||j                          t        j                  | d      5 }||   |   |   cd d d        ||j                          S S # 1 sw Y   nxY w	 ||j                          y y # ||j                          w w xY wNr   acquirer   r   release)filepathr"   r   slclockr#   s         r)   _slice_datasetr3   /   s    LLNYYx% 	*Y<$S)	* 	* LLN 	* 	* 	* LLN 4LLN s!   (A3 A	A3 AA3 3Bc                 >   	 ||j                          t        j                  | d      5 }|D ci c]  }|||   |   |    c}cd d d        ||j                          S S c c}w # 1 sw Y   nxY w	 ||j                          y y # ||j                          w w xY wr,   r-   )r0   r"   r   r1   r2   r#   r   s          r)   _slice_groupr5   :   s    LLNYYx% 	A;?@CC9c*3//@	A 	A LLN  A	A 	A 	A LLN 4LLN s3   (B A&A!A&	B !A&&A/+B Bc                     |j                         D ]-  \  }}t        j                  j                  | |   |d      | |<   / | S )NT)r   )itemsr   r    
from_codes)datacategorical_columnsr   category_dicts       r)   _restore_categoriesr<   E   sJ    1779 V]NN--d3iPT-US	VK    c           	         t        |       \  }}t        |||      \  }}}}	t        ||||      }
d|
z   }dt        t	        d||            dd z   }|d   |dz
  k7  r
g ||dz
  }i }t	        dt        t        ||z                    D ]Q  }t        ||z  |dz   |z        }t        |||||f}|	r	t        ||	f}t        j                  |d|j                  f|||f<   S t        j                  ||||      }||j                  |dd	      }|S )
a  
    Create a dask dataframe around a column-oriented table in HDF5.

    A table is a group containing equal-length 1D datasets.

    Parameters
    ----------
    group_uri : str
        URI to the HDF5 group storing the table.
    keys : list, optional
        list of HDF5 Dataset keys, default is to use all keys in the group
    chunksize : int, optional
        Chunk size
    index : str, optional
        Sorted column to use as index
    lock : multiprocessing.Lock, optional
        Lock to serialize HDF5 read/write access. Default is no lock.

    Returns
    -------
    :class:`dask.dataframe.DataFrame`

    Notes
    -----
    Learn more about the `dask <https://docs.dask.org/en/latest/>`_ project.

    zdaskify-h5py-table-)r      Nr   TF)r   drop)r
   r*   r   tuplerangeintr   slicer5   r<   r   r   r   dd	set_index)	group_urir   	chunksizeindexr2   r0   r"   r%   r(   r&   token	task_name	divisionsdskir1   	data_dictdfs                     r)   
read_tablerR   K   sN   8 +95Hi&5h	4&P#E4| Xy)T:E%-I uU2ui89!"==I}	!

AI
	 C1c$uy0123 JA	MAEY#67!8Yc4H	,iFI\\9dDLLIIqLJ 
c9dI	6B\\%5\9Ir=   c           	         | j                   }| j                  d      5 }|d   |   j                  }	t        ||d      }
|r t	        |
j
                  ||||d      \  }}}n|
j                  ||||      \  }}}t        |      s|j                  |	      }d d d        t        |z
  |z
  f||z
  ||z
  f      }|s|j                         }|S # 1 sw Y   ;xY w)Nr   pixelsi e)	max_chunkT)duplex)shape)
_is_symm_upperopenr   r   r	   queryr   astyper   todense)clri0i1j0j1fieldsparse_arrayis_upperh5r   readerrO   jvarrs                  r)   _array_selectrj      s    !!H	#  "8U#))2u	: r2r2dKGAq!ll2r2r2GAq!1vA  q2vq2v"r'27);
<CkkmJ   s   A7CCc                    t        | j                  ||||||      }d|z   }	||z
  ||z
  f}
t        | dddd||      }t        d|
d   |      D cg c]  \  }}||||f }}}t	        d |D              |
d   ff}t        t        |	gg|D cg c]  }t        t        |             c}       }|D cg c]  }t        | g||| }}t        t        ||            }t        j                  ||	|||
      S c c}}w c c}w c c}w )aj  
    Create a parallel Dask array around the matrix representation of a cooler.

    Parameters
    ----------
    clr : :class:`cooler.Cooler`
        Cooler object
    i0, i1 : int
        Row query range
    j0, j1 : int
        Column query range
    field : str
        Value column to query
    sparse_array : bool, optional
        Create a dask array backed by :class:`sparse.COO` sparse arrays
        instead of dense numpy arrays (default).
    chunksize : int, optional
        Length of the rowwise chunks to partition the underlying data into.

    Returns
    -------
    :class:`dask.array.Array`

    zcooler-array-slice-r   c              3   2   K   | ]  }|d    |d   z
    yw)r@   r   N ).0ss     r)   	<genexpr>z"load_dask_array.<locals>.<genexpr>   s     0AAaD1Q4K0s   r@   )r(   rW   )r   urirj   r   rB   r   r   rC   r   dictzipdaArray)r]   r^   r_   r`   ra   rb   rc   rI   rK   rL   rW   r(   lohisliceschunksdimr   r1   valuesrN   s                        r)   load_dask_arrayr|      s   6 SWWb"b"eY?E%-I"Wb2gEaAq%>D-6q%(I-NO62rr2r2OFO00058+>FJ&&I3uSX&IJKDIOP#}c=C===PFP
s4 
!C88CFUCC P&IPs   C;D4D)N)Ni NN)countF   )!	itertoolsr   mathr   r   numpyr   pandasr   sparser   ImportError
dask.arrayr   rt   dask.dataframe	dataframerF   	dask.baser   corer   r	   utilr
   r   r*   r3   r5   r<   rR   rj   r|   rm   r=   r)   <module>r      s|        P    ( .+48v$ GJ&D[  P
D
E4OPs   A A,