
    DUf                         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
 n# e$ r  ed          dw xY w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 dS )    )product)ceilN)COOz,The 'sparse' package is required to use dask)tokenize   )	CSRReader
query_rect)parse_cooler_uri	partitionc                 
  	 t          j        | d          5 }||         	|!t          	                                          }t	          	|d                            }i }|D ]=}t          j        	|         j                  }|t          ||j                  ||<   >t          j
        	fd|D             |          }|D ]"}t          j        g ||         d          ||<   #	 d d d            n# 1 swxY w Y   ||||fS )	Nrr   )enum)keyc                 T    i | ]$}|t          j        g |         j                   %S ))dtype)nparrayr   ).0r   grps     P/var/www/html/software/conda/lib/python3.11/site-packages/cooler/sandbox/dask.py
<dictcomp>z#_get_group_info.<locals>.<dictcomp>&   s0    EEES"(2SX^444EEE    )columnsT)
categoriesordered)h5pyFilelistkeyslencheck_dtyper   sorted__getitem__pd	DataFrameCategorical)
path	grouppathr   fnrowscategoricalsr   dtmetar   s
            @r   _get_group_infor.      sz   	4		 W	l<

##DCQL!! 	C 	CC!s3x~666B~$*22>$B$B$BS! |EEEEEEEt
 
 
   	W 	WCrl36GQUVVVDII	W'W W W W W W W W W W W W W W W, $l**s   CC44C8;C8c                 N   	 ||                                  t          j        | d          5 }||         |         |         cd d d            ||                                 S S # 1 swxY w Y   	 ||                                 d S d S # ||                                 w w xY w)Nr   acquirer   r   release)filepathr(   r   slclockr)   s         r   _slice_datasetr6   /   s    LLNNNYx%% 	*Y<$S)	* 	* 	* 	* 	* 	* 	* LLNNNN 	* 	* 	* 	* 	* 	* 	* 	* 	* LLNNNNN 4LLNNNN s.   +B A%B %A))B ,A)-B B$c                 L   	 ||                                  t          j        | d          5 fd|D             cd d d            ||                                 S S # 1 swxY w Y   	 ||                                 d S d S # ||                                 w w xY w)Nr   c                 :    i | ]}|         |                  S  r9   )r   r   r)   r(   r4   s     r   r   z _slice_group.<locals>.<dictcomp>?   s)    @@@CC9c*3/@@@r   r0   )r3   r(   r   r4   r5   r)   s    ` ` @r   _slice_groupr:   :   s   LLNNNYx%% 	A@@@@@@4@@@	A 	A 	A 	A 	A 	A 	A LLNNNN 	A 	A 	A 	A 	A 	A 	A 	A 	A LLNNNNN 4LLNNNN s.   +B
 A$ B
 $A((B
 +A(,B
 
B#c                     |                                 D ]0\  }}t          j                            | |         |d          | |<   1| S )NT)r   )itemsr$   r&   
from_codes)datacategorical_columnsr   category_dicts       r   _restore_categoriesrA   E   sP    17799 V V]N--d3iPT-UUS		Kr   逖 c           	      f   t          |           \  }}t          |||          \  }}}}	t          ||||          }
d|
z   }dt          t	          d||                    dd         z   }|d         |dz
  k    r
g ||dz
  R }i }t	          dt          t          ||z                                D ]N}t          ||z  |dz   |z            }t          |||||f}|	r
t          ||	f}t          j        |d|j        f|||f<   Ot          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   slicer:   rA   r$   r%   r   dd	set_index)	group_urir   	chunksizeindexr5   r3   r(   r*   r-   r+   token	task_name	divisionsdskir4   	data_dictdfs                     r   
read_tablerW   K   s|   8 +955Hi&5h	4&P&P#E4| Xy)T::E%-I uU2ui8899!""==I}	!!

AI
 
	 C1c$uy0112233 J JA	MAEY#677!8Yc4H	 	G,iFI\9dDLIIqL 
c9dI	6	6B\\%5\99Ir   c           	         | j         }|                     d          5 }|d         |         j        }	t          ||d          }
|rt	          |
j        ||||d          \  }}}n|
                    ||||          \  }}}t          |          s|                    |	          }d d d            n# 1 swxY w Y   t          ||z
  ||z
  f|||z
  ||z
  f          }|s|	                                }|S )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   readerrT   jvarrs                  r   _array_selectro      sA   !H	#  "8U#)2u	::: 	3 r2r2dKKKGAq!!ll2r2r22GAq!1vv 	 A                              q2vq2v"r'27);
<
<
<C kkmmJs   BB00B47B4countF   c           	          t           j        ||||          }d|z   }	||z
  z
  f}
t           dddd          }fdt          d|
d         |          D             }t	          d |D                       |
d         ff}t          t          |	ggd |D             R            } fd|D             }t          t          ||                    }t          j
        ||	|||
          S )	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                 "    g | ]\  }}||fS r9   r9   )r   lohire   rf   s      r   
<listcomp>z#load_dask_array.<locals>.<listcomp>   s&    OOO62rr2r2OOOr   c              3   8   K   | ]}|d          |d         z
  V  dS )rE   r   Nr9   )r   ss     r   	<genexpr>z"load_dask_array.<locals>.<genexpr>   s.      00AAaD1Q4K000000r   rE   c                 F    g | ]}t          t          |                    S r9   )rH   r    )r   dims     r   rv   z#load_dask_array.<locals>.<listcomp>   s$    &I&I&I3uSXX&I&I&Ir   c                 0    g | ]}t           g|R S r9   )ro   )r   r4   rb   rg   rh   s     r   rv   z#load_dask_array.<locals>.<listcomp>   s0    PPP#}c=C====PPPr   )r-   r\   )r   uriro   r   rG   r   r   dictzipdaArray)rb   rc   rd   re   rf   rg   rh   rN   rP   rQ   r\   r-   sliceschunksr   valuesrS   s   `  ````          r   load_dask_arrayr      s+   6 SWb"b"eY??E%-I"Wb2gEaAq%>>DOOOOOYq%(I-N-NOOOF000000058+>FJ&I&I&&I&I&IJJJKKDPPPPPPPPPF
s4  
!
!C8CFUCCCCr   )N)NrB   NN)rp   Frq   )!	itertoolsr   mathr   r   numpyr   pandasr$   sparser   ImportError
dask.arrayr   r   dask.dataframe	dataframerK   	dask.baser   corer   r	   utilr
   r   r.   r6   r:   rA   rW   ro   r   r9   r   r   <module>r      s                       P P P P
+D
E
E4OP                   ( ( ( ( ( ( ( ( . . . . . . . .+ + +4        8 8 8 8v  $ GJ&D &D &D &D &D &Ds   ! 3