
    bi"                       d dl mZ d dl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  ed      Z ej                  e      Zej#                  e      d	        Zej#                  e      ej#                  e      d
               Zej#                  e      d        Zej#                  e      d        Zej#                  e      ej#                  e      ej#                  e      ej#                  e      d                             Z G d d      Zej#                  e      d        Z ej#                  e!      d        Z"ejG                  d      d        Z$ejG                  d      d        Z%ejG                  d      d        Z&ejG                  d      d        Z'ejG                  d      d        Z(ejG                  d      d        Z)ejG                  d      d        Z*ejG                  d       d!        Z+d" Z, e,        y)#    )annotationsN)array)Version)importlib_metadata)Dispatchsizeof)namec                ,    t        j                  |       S Nsys	getsizeofos    F/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/dask/sizeof.pysizeof_defaultr          ==    c                    t        |       S r   lenr   s    r   sizeof_bytesr      s     q6Mr   c                    | j                   S r   nbytesr   s    r   sizeof_memoryviewr      s    88Or   c                2    | j                   t        |       z  S r   )itemsizer   r   s    r   sizeof_arrayr   #   s    ::Ar   c           
        t        |       }d}||kD  rt        | t        t        f      rt	        j
                  | |      }nt        j                  | |      }t        j                  |       t        ||z  t        t        t        |            z        z   S t        j                  |       t        t        t        |             z   S )N
   )r   
isinstanceset	frozenset	itertoolsislicerandomsampler   r   intsummapr   )seq	num_itemsnum_samplessampless       r   sizeof_python_collectionr0   (   s    
 CIK;cC+,  &&sK8GmmC5G}}S!C#c#fg*>&??%
 
 	
 }}S!CFC(8$999r   c                      e Zd ZdZy)SimpleSizeofa<  Sentinel class to mark a class to be skipped by the dispatcher. This only
    works if this sentinel mixin is first in the mro.

    Examples
    --------
    >>> def _get_gc_overhead():
    ...     class _CustomObject:
    ...         def __sizeof__(self):
    ...             return 0
    ...
    ...     return sys.getsizeof(_CustomObject())

    >>> class TheAnswer(SimpleSizeof):
    ...     def __sizeof__(self):
    ...         # Sizeof always add overhead of an object for GC
    ...         return 42 - _get_gc_overhead()

    >>> sizeof(TheAnswer())
    42

    N)__name__
__module____qualname____doc__ r   r   r2   r2   >   s    r   r2   c                ,    t        j                  |       S r   r   ds    r   sizeof_blockedr;   V   r   r   c                    t        j                  |       t        t        | j	                                     z   t        t        | j                                     z   dt        t                     z  z
  S )N   )r   r   r   listkeysvaluesr9   s    r   sizeof_python_dictrA   [   sY     	a
affh
 	!
ahhj!
"	# fTVn
	r   cupyc                 V    dd l } t        j                  | j                        d        }y )Nr   c                ,    t        | j                        S r   r)   r   xs    r   sizeof_cupy_ndarrayz*register_cupy.<locals>.sizeof_cupy_ndarrayi       188}r   )rB   r   registerndarray)rB   rH   s     r   register_cupyrL   e   s$    __T\\" #r   numbac                     dd l } t        j                  | j                  j                  j
                  j                        d        }y )Nr   c                ,    t        | j                        S r   rE   rF   s    r   sizeof_numba_devicendarrayz2register_numba.<locals>.sizeof_numba_devicendarrayr   rI   r   )
numba.cudar   rJ   cudacudadrvdevicearrayDeviceNDArray)rM   rP   s     r   register_numbarV   n   s7    __UZZ''33AAB Cr   rmmc                 p    dd l } t        | d      r&t        j                  | j                        d        }y y )Nr   DeviceBufferc                ,    t        | j                        S r   rE   rF   s    r   sizeof_rmm_devicebufferz-register_rmm.<locals>.sizeof_rmm_devicebuffer~   s    qxx= r   )rW   hasattrr   rJ   rY   )rW   r[   s     r   register_rmmr]   w   s7     sN#	))	*	! 
+	! $r   numpyc                 V    dd l } t        j                  | j                        d        }y )Nr   c                    d| j                   v r+| t        d | j                   D                 }|j                  S t        | j                        S )Nr   c              3  R   K   | ]  }|d k7  rt        d      n
t        d       ! yw)r   N   )slice).0ss     r   	<genexpr>z?register_numpy.<locals>.sizeof_numpy_ndarray.<locals>.<genexpr>   s#     PQtE!H<Ps   %')stridestupler   r)   )rG   xss     r   sizeof_numpy_ndarrayz,register_numpy.<locals>.sizeof_numpy_ndarray   s>    		>5PaiiPPQB99188}r   )r^   r   rJ   rK   )nprj   s     r   register_numpyrl      s$    __RZZ  !r   pandasc                    dd l dd l} t        | j                  d      ffdt        j                  | j                        fd       }t        j                  | j                        fd       }t        j                  | j                        fd       }t        j                  | j                        d        }y )Nr   pythonc                 l   | syt        d | D              }|syi }| D ]C  }j                  j                  |dd      }|j                         D ]  }||t	        |      <    E dt        |       z  }t        d |j                         D              }t        |      |z  dkD  rt        ||z  |z        S |S )Nr   c              3  2   K   | ]  }t        |        y wr   r   )rd   rG   s     r   rf   z7register_pandas.<locals>.object_size.<locals>.<genexpr>   s     (SV(   d   T)sizereplacec              3  2   K   | ]  }t        |        y wr   r   )rd   is     r   rf   z7register_pandas.<locals>.object_size.<locals>.<genexpr>   s     G!F1IGrr   g      ?)r*   r'   choicetolistidr   r@   r)   )	ri   ncellsunique_samplesrG   r(   rx   nsamplessample_nbytesrk   s	           r   object_sizez$register_pandas.<locals>.object_size   s    (R((  	*AYY%%ac4%@F]]_ *()r!u%*	*
 R=G~/D/D/FGG~)C/ }v-899 ! r   c                   t        | j                        t        | j                        z   }g }d }| j                  j	                         D ]d  }||j
                  |k7  r|j
                  }|dz  }||j                  dd      z  }|j
                  v sJ|j                  |j                         f | | z  }t        d|      S Ni  F)indexdeep)
r   r   columns_seriesr@   dtypememory_usageappend_valuesmax)dfpobject_cols
prev_dtypecolOBJECT_DTYPESr   s        r   sizeof_pandas_dataframez0register_pandas.<locals>.sizeof_pandas_dataframe   s    288vbjj11
 ::$$& 	0C!SYY*%< YY
T	!!E!::AyyM)""3;;/	0 	
[+&&4|r   c                    dt        | j                        z   | j                  dd      z   }| j                  v r| | j                        z  }|S r   )r   r   r   r   r   )re   r   r   r   s     r   sizeof_pandas_seriesz-register_pandas.<locals>.sizeof_pandas_series   sM     6!''?"Q^^%e^%LL77m#QYY''Ar   c                d    d| j                  d      z   }| j                  v r| |       z  }|S )Ni  F)r   )r   r   )rx   r   r   r   s     r   sizeof_pandas_indexz,register_pandas.<locals>.sizeof_pandas_index   s7    !..e.,,77m#QAr   c                t    t        d | j                  D              t        d | j                  D              z   S )Nc              3  2   K   | ]  }t        |        y wr   rw   )rd   ls     r   rf   zDregister_pandas.<locals>.sizeof_pandas_multiindex.<locals>.<genexpr>   s     /6!9/rr   c              3  4   K   | ]  }|j                     y wr   r   )rd   cs     r   rf   zDregister_pandas.<locals>.sizeof_pandas_multiindex.<locals>.<genexpr>   s     5P1ahh5Ps   )r*   levelscodes)rx   s    r   sizeof_pandas_multiindexz1register_pandas.<locals>.sizeof_pandas_multiindex   s+    /ahh//#5P5P2PPPr   )
r^   rm   objectStringDtyper   rJ   	DataFrameSeriesIndex
MultiIndex)pdr   r   r   r   r   rk   r   s        @@@r   register_pandasr      s    R^^H56M!6 __R\\" #* __RYY   __RXX  __R]]#Q $Qr   scipyc                     dd l } ddl m} t        | j                        t        d      k  r%t        j                  |j                        d        }t        j                  |j                        d        }y )Nr   )sparsez1.12.0.dev0c                "    | j                         S r   )
__sizeof__re   s    r   sizeof_spmatrix_dokz.register_spmatrix.<locals>.sizeof_spmatrix_dok   s    <<>!r   c                V    t        d | j                  j                         D              S )Nc              3  2   K   | ]  }t        |        y wr   rw   )rd   vs     r   rf   z=register_spmatrix.<locals>.sizeof_spmatrix.<locals>.<genexpr>   s     :6!9:rr   )r*   __dict__r@   r   s    r   sizeof_spmatrixz*register_spmatrix.<locals>.sizeof_spmatrix   s     :ajj&7&7&9:::r   )r   r   r   __version__r   rJ   
dok_matrixspmatrix)r   r   r   r   s       r   register_spmatrixr      sb    u  !GM$::	**	+	" 
,	" __V__%; &;r   pyarrowc                     dd l fdt        j                  j                        fd       } t        j                  j                        fd       }y )Nr   c                    d}t        | j                        s| j                  } | j                         D ])  }|j	                         D ]  }|s||j
                  z  } + |S )Nr   )r"   ChunkedArraydata
iterchunksbuffersrt   )r   r   chunkbufferpas       r   _get_col_sizez'register_pyarrow.<locals>._get_col_size   sb    $099D__& 	%E--/ %$A%	% r   c                    t        | j                  j                        }| j                         D ]  }| |      z  } t	        |      dz   S Ni  )r   schemametadataitercolumnsr)   )tabler   r   r   s      r   sizeof_pyarrow_tablez.register_pyarrow.<locals>.sizeof_pyarrow_table   sL    5<<(()$$& 	$Cs##A	$1v}r   c                ,    t         |             dz   S r   )r)   )r   r   s    r   sizeof_pyarrow_chunked_arrayz6register_pyarrow.<locals>.sizeof_pyarrow_chunked_array   s    =&'$..r   )r   r   rJ   Tabler   )r   r   r   r   s     @@r   register_pyarrowr      sM     __RXX  __R__%/ &/r   xarrayc                    dd l dd l} t        | j                        }|t        d      k\  }t        j                  | j                  j                  j                        fd       }t        j                  | j                        t        j                  | j                        fd              }t        j                  | j                        fd       }|r$t	        j                  | j                        |      }t        j                  | j                  j                  j                        fd       }t        j                  | j                  j                  j                        fd       }y )Nr   z	2024.02.0c                T     j                   |       t        | j                        z   S r   )r   r   mappingobjr   s    r   xarray_sizeof_frozenz-register_xarray.<locals>.xarray_sizeof_frozen  s"    s}}S!F3;;$777r   c                T     j                   |       t        | j                        z   S r   )r   r   r   r   s    r   xarray_sizeof_daz)register_xarray.<locals>.xarray_sizeof_da  s$     s}}S!F388$444r   c                T     j                   |       t        | j                        z   S r   )r   r   	variablesr   s    r   xarray_sizeof_dsz)register_xarray.<locals>.xarray_sizeof_ds  s"    s}}S!F3==$999r   c                     j                   |       t        | j                        z   t        | j                        z   t        | j                        z   t        | j
                        z   S r   )r   r   _index_type_indexes
_variables_dimsr   s    r   xarray_sizeof_indexesz.register_xarray.<locals>.xarray_sizeof_indexes  s`     CMM#S__%&S\\"# S^^$% SYY	 	
r   c                     j                   |       t        | j                        z   t        | j                        z   t        | j                        z   S r   )r   r   r   dimcoord_dtyper   s    r   xarray_sizeof_pd_indexz/register_xarray.<locals>.xarray_sizeof_pd_index(  sL     CMM#SYY SWWo S__%&	
r   )r   r   r   r   r   rJ   coreutilsFrozen	DataArrayVariableDataset
NamedArrayindexesIndexesPandasIndex)	xrXARRAY_VERSIONXARRAY_GE_2024_02r   r   r   r   r   r   s	           @r   register_xarrayr     s   R^^,N&'+*>>__RWW]]))*8 +8 __R\\"__R[[!5 " #5 __RZZ : !: !??2==9:JK__RWW__,,-
 .
 __RWW__001
 2
r   c                     t        j                  d      D ]  } | j                         }	  |t               ! y# t        $ r% t
        j                  d| j                          Y Pw xY w)zERegister sizeof implementations exposed by the entry_point mechanism.zdask.sizeof)groupz&Failed to register sizeof entry point N)r   entry_pointsloadr   	Exceptionlogger	exceptionr	   )entry_point	registrars     r   _register_entry_point_pluginsr   2  sg    )66]K $$&		f  	89I9I8JK	s   :+A('A()-
__future__r   r%   loggingr'   r   r   packaging.versionr   dask._compatibilityr   
dask.utilsr   r   	getLoggerr3   r   rJ   r   r   bytes	bytearrayr   
memoryviewr   r   r>   rh   r#   r$   r0   r2   r;   dictrA   register_lazyrL   rV   r]   rl   r   r   r   r   r   r7   r   r   <module>r     s`   "    
  % 2 	x	 			8	$          :    :$ 0     f  g  e! ! g  hHQ  HQV g; ; i / !/2 h)
  )
X	  r   