
    ^j"Z                        U d Z ddlZddlZddlZddlZddlmZmZ ddlm	Z
 ddlmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ ddlZddlmZm Z m!Z!m"Z"m#Z#m$Z$ ddl%m&Z&m'Z'm(Z( erddl)Z*ddl+Z, G d	 d
e      Z- G d de      Z. edee/e0f   e1ed   eee/df      ee/df   edde-f   d      Z2 edee/e0f   e1ed   eee/df      ee/df   edde-f   e/d      Z3 ede2e2d      Z4 ede3e3d      Z5de!de0fdZ6de.de3fdZ7de.de8fdZ9dKde2de0de$fdZ:dZ;dee e/f   d ee/df   d!eejx                     d"e0de2f
d#Z=deded$   fd%Z> G d& d'e      Z?dedee?   fd(Z@e
deeeejx                     f   fd)       ZAed*e/d+e1d,e/d-dde2f
d.       ZBed*e/d+e1d,e/d-e/de3f
d/       ZBd*e/d+e1d,e/d-ee/   dee2e3f   f
d0ZBd1d2dee4ef   fd3ZCd4d5de2fd6ZDd1d2d7ee-e.d8f   dee4e2ef   fd9ZEde!d!ee#   deej                  ee#   f   fd:ZGd4e$de0fd;ZHdej                  de2fd<ZId1e?d7d=deee4e2f   e2ef   fd>ZJdej                  de8fd?ZKde.d@e1ddfdAZLd1e?dee5ef   fdBZMd1e?d7d=deee3e5f   e2ef   fdCZN G dD dE      ZOdFeee"eOf      deee"   eeO   f   fdGZPeeee2e3f   eee2e3e4e5f   ee2e3f   f   f      ZQeeRdH<    G dI dJe      ZSy)Lz+Helpers for interfacing array like objects.    N)ABCabstractmethod)cache)TYPE_CHECKINGAnyCallableDictListLiteralOptionalProtocolTupleType	TypeAlias	TypedDict	TypeGuardUnioncastoverload   )ArrowCatListCNumericPtrDataTypeFeatureTypes
NumpyDTypeNumpyOrCupy)import_cupyimport_pyarrowlazy_isinstancec                       e Zd Zedd       Zy)_ArrayLikeArgc                      y N selfs    ^/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/xgboost/_data_utils.py__array_interface__z!_ArrayLikeArg.__array_interface__/   s    14    N)returnArrayInf)__name__
__module____qualname__propertyr(   r$   r)   r'   r!   r!   .   s    4 4r)   r!   c                       e Zd Zedd       Zy)_CudaArrayLikeArgc                      y r#   r$   r%   s    r'   __cuda_array_interface__z*_CudaArrayLikeArg.__cuda_array_interface__4   s    :=r)   N)r*   CudaArrayInf)r,   r-   r.   r/   r3   r$   r)   r'   r1   r1   3   s    = =r)   r1   r+      .datatypestrversionstridesshapemaskr4   r7   r8   r9   r:   r;   r<   streamStringArrayoffsetsvaluesCudaStringArrayr7   r*   c                 z    t        | d      xr. t        | j                  d      xr | j                  j                  S )z)Whether the numpy array has object dtype.dtype	hasobject)hasattrrE   rF   r7   s    r'   array_hasobjectrI   W   s8     	g 	!DJJ,	!JJ  r)   c                    t        |       rt        d      | j                  }d|v r_|d   Z|d   j                  }|d   }|d   d   }|d   dv r2|d	d d
k(  r*|r(t        |      }d|d<   |f|d<   |j	                  dd       ||d<   |S )z6Returns a dictionary storing the CUDA array interface.<Input data contains `object` dtype.  Expecting numeric data.r<   Nr8   r;   r   r   )uit   1z|t1r:   )rI   
ValueErrorr3   dictpop)r7   ainf	mask_ainfr8   	n_sampless        r'   cuda_array_interface_dictrW   `   s    tWXX((D~$v,2L99	 I&M!$	1:(WQR[C-?IYI#(Ii "+IgMM)T* VKr)   c                 Z    t        |       }t        t        j                  |      d      }|S )zMake cuda array interface str.utf-8)rW   bytesjsondumpsr7   	interfaceinterface_strs      r'   cuda_array_interfacer`   t   s'    )$/I$**Y/9Mr)   r^   	zero_copyc                     G d d      } |       }d| v ri| |_         t               }|j                  dk(  r3|j                  |j                  t        j                  | d               S |j                  ||       }|S | |_        |j                  dk(  r7t        j                  |j                  t        j                  | d               S t        j                  ||       }|S )z.Convert array interface to numpy or cupy arrayc                      e Zd ZU dZdZee   ed<   edee   fd       Z	e	j                  deddfd       Z	edee   fd       Zej                  deddfd	       Zedeed
f   fd       Zedej                   fd       Zy)#from_array_interface.<locals>.Arrayz3Wrapper type for communicating with numpy and cupy.N
_interfacer*   c                     | j                   S r#   )re   r%   s    r'   r(   z7from_array_interface.<locals>.Array.__array_interface__   s    ??"r)   r^   c                 R   t        j                   |      | _        t        | j                  d         | j                  d<   | j                  d   d   | j                  d   d   f| j                  d<   | j                  j                  dd       }|t        |      | j                  d<   y y )Nr;   r7   r   r   r:   )copyre   tupleget)r&   r^   r:   s      r'   r(   z7from_array_interface.<locals>.Array.__array_interface__   s    "ii	2DO',T__W-E'FDOOG$'*'*'DOOF# oo)))T:G"-27^	* #r)   c                     | j                   S r#   r(   r%   s    r'   r3   z<from_array_interface.<locals>.Array.__cuda_array_interface__   s    +++r)   c                     || _         y r#   rl   )r&   r^   s     r'   r3   z<from_array_interface.<locals>.Array.__cuda_array_interface__   s
    '0D$r)   .c                 ,    | j                   }|J |d   S )zShape of the input array.r;   rl   )r&   aifs     r'   r;   z)from_array_interface.<locals>.Array.shape   s#     **C?"?w<r)   c                 @    t        j                  | j                        S )zTotal size of the input array.)npprodr;   r%   s    r'   sizez(from_array_interface.<locals>.Array.size   s     774::&&r)   )r,   r-   r.   __doc__re   r   r+   __annotations__r/   r(   setterr3   r   intr;   rq   signedintegerrs   r$   r)   r'   Arrayrd   ~   s    A)-
HX&-		#(); 	# 
	# 
	#	#
	< 
	<d 
	< 
$
	< 
	,hx.@ 	, 
	, 
"	(	(	1h 	14 	1 
)	1 
	 5c? 	  
	  
	'"** 	' 
	'r)   ry   r>   r   r8   r;   rE   rh   )	r3   r   rs   emptyr;   rq   rE   arrayr(   )r^   ra   ry   arrcpouts         r'   from_array_interfacer   {   s    (' ('T 'C 9'0$]88q=88#))288Ii<P3Q8RRhhsYh/ J #,88q=88#))288Ii<P3QRRhhsY/Jr)   rO   ptrr;   rE   is_cudac                    |r(t               j                  d|      }|j                  }n#t        j                  d|      }|j                  }t        | t              s/t        j                  | t        j                        j                  }n| }t        t        j                  |            }||dk(  sJ ||S |df|d<   |rd|vr	t        |d<   ||d<   d|d	<   |S )
z2Make an __(cuda)_array_interface__ from a pointer.)r   rz   Nr   Tr7   r>   r;   r:   )r   r|   r3   rq   r(   
isinstancerw   ctypesr   c_void_pvaluerr   STREAM_PER_THREAD)r   r;   rE   r   r|   r}   addrlengths           r'   make_array_interfacer      s     ##$e#<..t51))c3{{3066 Fv{**|4LE&M85(+hE'NE)Lr)   zpa.DictionaryArrayc                     t        | dd      S )z"Is this an arrow dictionary array?zpyarrow.libDictionaryArray)r   rH   s    r'   is_arrow_dictr      s    40ABBr)   c                       e Zd ZdZe	 	 dd       Zedd       Zedej                  fd       Zedej                  fd       Z
ded   fdZedefd	       Zedefd
       Zy)DfCatAccessorz!Protocol for pandas cat accessor.r*   c                      y r#   r$   r%   s    r'   
categorieszDfCatAccessor.categories   s     r)   c                      y r#   r$   r%   s    r'   codeszDfCatAccessor.codes       $'r)   c                      y r#   r$   r%   s    r'   rE   zDfCatAccessor.dtype   s    !$r)   c                      y r#   r$   r%   s    r'   rB   zDfCatAccessor.values   r   r)   )pa.StringArraypa.IntegerArrayc                      y r#   r$   r%   s    r'   to_arrowzDfCatAccessor.to_arrow   s    58r)   c                      y r#   r$   r%   s    r'   r3   z&DfCatAccessor.__cuda_array_interface__   s    8;r)   c                      y r#   r$   r%   s    r'   _columnzDfCatAccessor._column  s    !r)   N)r*   zpd.Index)r*   	pd.Series)r,   r-   r.   rt   r/   r   r   rq   rE   ndarrayrB   r   r   r4   r3   r   r   r$   r)   r'   r   r      s    +	  ' '$rxx$ $'

' '9	2	39 ;,; ;!! !r)   r   c                 6    t        | d      xr t        | d      S )Nr   r   )rG   rH   s    r'   
_is_df_catr     s    4&A74+AAr)   c                     dd l }  | j                         t        j                   | j                         t        j                   | j                         t        j                   | j
                         t        j
                   | j                         t        j                   | j                         t        j                   | j                         t        j                   | j                         t        j                   | j                         t        j                   | j                         t        j                   | j                         t        j                  i}|S )Nr   )pyarrowint8rq   int16int32int64uint8uint16uint32uint64float16float32float64)pamappings     r'   _arrow_npdtyper   
  s     		277
BHH
BHH
BHH
BHH		RYY		RYY		RYY

bjj

bjj

bjj+G Nr)   addressr8   rs   r>   c                      y r#   r$   r   r8   rs   r>   s       r'   _arrow_buf_infr     s    UXr)   c                      y r#   r$   r   s       r'   r   r   #  s     r)   c                 @    || df|dd |fd |d}|S | df|dd |fd d}|S )NTr5   r=   r6   r$   )r   r8   rs   r>   jcuaifjaifs         r'   r   r   )  s[     dOW 
  $D Kr)   catsr   c                    t         s
t               }| j                  dk(  sJ | j                         }|\  }}}|j                  sJ t        |       dz   dt        dt        ffd}|j                   |t        j                        k(  rgt        | j                        sd}t        |dt        |        dz         | j                   |j                                }|j                         \  }}}|j                   |t        j"                        k7  rt        d	      t%        |j&                  d
d       }	t%        |j&                  d|j                  d       }
|J |	|
d}||||ffS )Nr   r   typr*   c                 N    t        j                  |       j                  dz  z  S )N   )rq   iinfobits)r   off_lens    r'   get_n_bytesz)_arrow_cat_names_inf.<locals>.get_n_bytesP  s!    "((3-,,122r)   zExpecting a `pyarrow.Array`.z Got: .z?Arrow dictionary type offsets is required to be 32-bit integer.<i4|i1r@   )r   r   offsetbuffersis_cpulenr   rw   rs   rq   r   r   LargeStringArray	TypeErrortyper   stringr   r   r   )r   r   r   r<   r   r7   r   arrow_str_errori32catsjoffsetjdatajnamesr   s               @r'   _arrow_cat_names_infr   C  sR    ;;!#||~G D&$===$i!mG3 3# 3 {{k"((++$ 3 34<OOtDzl!.DDEE ))IBIIK($__.fd{{k"((++M
 	
 V^^UGTBG4<<		4@E<<&-?FD&$'''r)   r}   zpa.Arrayc                    t         s
t               }t        | j                        st	        dt        |              | j                         \  }}t        |j                  t        |       ft               | j
                     |j                         }|5|j                  dfdddt        |       fdd}|j                  st        |d<   nd}||d	<   |S )
z&Helper for handling categorical codes.zInvalid input type: )r;   rE   r   NTz<t1r5   r6   r>   r<   )r   r   r   ry   r   r   r   r   r   r   r   r   r   )r}   r   r<   r7   r   jmasks         r'   _arrow_array_infr   j  s     eRXX&.tE{m<==JD$ 5zmuzz*KK	E \\4(%j]%
 {{/E(OE&MLr)   r   r   c                 B    t        |       \  }}t        |      }|||dffS )zHGet the array interface representation of a string-based category array.N)r   r   )r   r   r   cats_tmpjcodess        r'   arrow_cat_infr     s/    
 ,D1FHe$F6Hd+++r)   c                    t        |       s,| j                  t        j                  t        j                  fv r#t        j
                  }| j                  |d      } | j                  j                  st        j                  | d      } | |fS )z7Ensure the np array has correct type and is contiguous.Fr{   A)requirements)
rI   rE   rq   r   bool_r   astypeflagsalignedrequire)r7   rE   s     r'   _ensure_np_dtyper     sf     t

rzz288.D D

{{5u{-::zz$S1;r)   c                 Z    t        | j                        dk(  xs | j                  d   dk(  S )Nr   )r   r;   )r}   s    r'   _is_flattenr     s(    u{{q 7EKKNa$77r)   c                     t        |       rt        d      | j                  }d|v r|d   j                  |d<   t        t        |      S )z*Returns an array interface from the input.rK   r<   )rI   rQ   r(   r   r+   )r7   rT   s     r'   array_interface_dictr     sH    tWXX##D~F|77V$r)   r   c                     |j                  dt        j                        }dt        f fd} |       r5 j                  }t        |      }|j                  }t        |      }||||ffS dt        dt        t        j                  t        f   fd} | j                        \  }}	t        |t        j                        \  }}
t        |      }|	j                  d      }t        j                  j                  t        j                   |            j"                  }|J |dfd	t%        |	      fdd
dd}||d}|j                  }t        |      }||	||f}|||fS )zCGet the array interface representation of pandas category accessor.r*   c                      j                   } 	 t        j                  | t        j                        xs$ t        j                  | t        j                        S # t
        $ r Y yw xY w)NF)rE   rq   
issubdtypefloatingintegerr   )rE   r   s    r'   is_primzpd_cat_inf.<locals>.is_prim  sL    

	==4XeRZZ8XX 		s   A	A 	A%$A%strarrc                    t        | t        j                        s>t        | d      r| j	                  t
              } nt        j                  | t
              } t        j                  t              }t        j                  t        j                  t        j                  dgt        j                         ||       g            }| j                  j                  dk(  r-| j                         D cg c]  }|j!                  d       }}n&| j                         D cg c]  }t#        |       }}dj%                  |      }d|v rt'        j(                  dt*               |j-                  t        j.                        |fS c c}w c c}w )	z5Convert a string-like array to an arrow string array.to_numpy)rE   r   SrY     zzFound embedded NUL (\0) characters in string categories. Arrow used to strip these characters, but they are now preserved.)r   rq   r   rG   r   objectasarray	vectorizer   cumsumconcatenater}   r   rE   kindtolistdecodestrjoinwarningswarnUserWarningr   r   )r   lenarrrA   sstr_listrB   s         r'   npstr_to_arrow_strarrz)pd_cat_inf.<locals>.npstr_to_arrow_strarr  s   &"**-vz*v6F&9c"))NNBHHaS96&>JK
 <<#39==?Ca)CHC(.81A8H8"6>MMX  ~~bhh'// D8s   1FFrY   NTr   r5   )r7   r8   r;   r:   r9   r<   r@   )replacerq   nanboolrB   r   r   r   r   r   r   r   encoder   r   from_bufferc_char_pr   r   )r   r   r   name_values_numjarr_valuescode_values
jarr_codesr  name_offsetsname_values_joffsetsbvaluesr   jvaluesr   r   bufs   `                 r'   
pd_cat_infr    s^   
 MM"bff%ET  y++*?;ll)+6
J+(FFF0c 0eBJJO.D 08 !6dkk BL+&|RXX>OL!#L1H  )G
//
%
%foog&>
?
E
EC?? dk"$G '/'BF,,K!+.F 		C 63r)   c                 Z    t        |       }t        t        j                  |      d      }|S )zMake array interface str.rY   )r   rZ   r[   r\   r]   s      r'   array_interfacer    s'    $T*I$**Y/9Mr)   fieldc                 \    d| j                   v r| j                   d   t        d|       yy)z(Make sure no missing value in meta data.r<   Nz"Missing value is not allowed for: )r3   rQ   )r7   r  s     r'   check_cudf_metar  	  s@     	$///))&1==eWEFF > 	0r)   c                    ddl }| j                  j                         }|j                         j	                         |j
                  j                  k7  rt        d      |j                         dk(  sJ |j                         dk(  sJ |j                         d   }|j                         j	                         |j
                  j                  k(  sJ d       t        |j                         j                  d   d   ddt              }t        |j                         j                  d   d   d|j!                         t              }||d}||ffS )	z7String category index path for :py:func:`cudf_cat_inf`.r   NzDUnexpected type for category index. It's neither numeric nor string.zExpected INT32 string offsets.r7   r   r   r@   )	pylibcudfr   to_pylibcudfr   idTypeIdSTRINGr   
null_countr   childrenINT32r   r7   r3   r   rs   )r   plcplc_col	off_childr   r   r   s          r'   _cudf_str_cat_infr+    s8    ll'')G||~cjj///R
 	
 1$$$>>q     "1%I>> CJJ$4$44V6VV4 )//7:		E +11&9!<	G +2UCF G:r)   c                 ,   t               }|j                  | j                  |j                        xs& |j                  | j                  |j                        }|rt        |       }t        |      }||| |ffS t        |       \  }}t        |      }|||fS )z4Obtain the cuda array interface for cuDF categories.)r   r   rE   r   r   rW   r+  )	r   r   r   
is_num_idx	cats_ainf
codes_ainfr   r  r   s	            r'   cudf_cat_infr0  6  s     
Btzz2;;7 2==

BJJ<J -d3	.u5
*tUm33#D)KFC&u-F63r)   c                   ~    e Zd ZdZdeej                  eg df   f   dee	   ddfdZ
de	fdZdefdZdefd	Zdd
Zy)
Categoriesa  An internal storage class for categories returned by the DMatrix and the
    Booster. This class is designed to be opaque. It is intended to be used exclusively
    by XGBoost as an intermediate storage for re-coding categorical data.

    The categories are saved along with the booster object. As a result, users don't
    need to preserve this class for re-coding. Use the booster model IO instead if you
    want to preserve the categories in a stable format.

    .. versionadded:: 3.1.0

    .. warning::

        This class is internal.

    .. code-block:: python

        Xy = xgboost.QuantileDMatrix(X, y, enable_categorical=True)
        booster = xgboost.train({}, Xy)

        categories = booster.get_categories() # Get categories

        # Use categories as a reference for re-coding
        Xy_new = xgboost.QuantileDMatrix(
            X_new, y_new, feature_types=categories, enable_categorical=True, ref=Xy
        )

        # Categories will be part of the `model.json`.
        booster.save_model("model.json")

    handleNarrow_arraysr*   c                 0    |\  | _         | _        || _        y r#   )_handle_free_arrow_arrays)r&   r3  r4  s      r'   __init__zCategories.__init__h  s     $* dj)r)   c                 H    | j                   t        d      | j                   S )ao  Get the categories in the dataset. The results are stored in a list of
        (feature name, arrow array) pairs, with one array for each categorical
        feature. If a feature is numerical, then the corresponding column in the list is
        None. A value error will be raised if this container was created without the
        `export_to_arrow` option.

        zHThe `export_to_arrow` option of the `get_categories` method is required.)r8  rQ   r%   s    r'   r   zCategories.to_arrowu  s0     %   !!!r)   c                 2    | j                   j                  du S )z$Returns True if there's no category.Nr6  r   r%   s    r'   r|   zCategories.empty  s    ||!!T))r)   c                 ^    | j                   j                  sJ | j                   j                  S )z*Internal method for retrieving the handle.r<  r%   s    r'   
get_handlezCategories.get_handle  s%    ||!!!!||!!!r)   c                 R    | j                   j                  y | j                          y r#   )r6  r   r7  r%   s    r'   __del__zCategories.__del__  s    <<%

r)   )r*   N)r,   r-   r.   rt   r   r   r   r   r   r   r9  r   r
  r|   rw   r>  r@  r$   r)   r'   r2  r2  H  sm    >*fooxD'99:* |,* 
	*", "*t *"C "
r)   r2  feature_typesc                 >    t        | t              r| }d} | |fS d}| |fS )zGet the optional reference categories from the `feature_types`. This is used by
    various `DMatrix` where the `feature_types` is reused for specifying the reference
    categories.

    N)r   r2  )rA  ref_categoriess     r'   get_ref_categoriesrD    s7     -,& .(( .((r)   AifTypec                   j    e Zd ZdZdee   dedee   ddfdZ	de
fdZeedeeef   fd	              Zy)
TransformedDfzInternal class for storing transformed dataframe.

    Parameters
    ----------
    ref_categories :
        Optional reference categories used for re-coding.

    aitfs :
        Array interface for each column.

    rC  aitfstemporary_buffersr*   Nc                     || _         |(|j                         |j                         }|| _        nd | _        || _        || _        y r#   )rC  r>  ref_aifrH  rI  )r&   rC  rH  rI  ro   s        r'   r9  zTransformedDf.__init__  sK     -%.*C*C*E*Q ++-C*-DLDL
!2r)   c                     | j                   :| j                  | j                  d}t        t	        j
                  |      d      }|S t        t	        j
                  | j                        d      }|S )z6Return a byte string for JSON encoded array interface.)rC  columnsrY   )rC  rK  rH  rZ   r[   r\   )r&   ref_infinfs      r'   r  zTransformedDf.array_interface  s\    */3||

SG

7+W5C 
 

4::.8C
r)   c                      y)z"Return the shape of the dataframe.Nr$   r%   s    r'   r;   zTransformedDf.shape  s    r)   )r,   r-   r.   rt   r   r2  rE  r
   r   r9  rZ   r  r/   r   rw   r;   r$   r)   r'   rG  rG    sr    
3 ,3 3  ;	3
 
3   1uS#X 1  1r)   rG  )F)Trt   rh   r   r[   r  abcr   r   	functoolsr   fcachetypingr   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   numpyrq   _typingr   r   r   r   r   r   compatr   r   r   pandaspdr   r   r!   r1   rw   r
  r   r+   r4   r?   rC   rI   rW   rZ   r`   r   r   numberr   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r+  r0  r2  rD  rE  ru   rG  r$   r)   r'   <module>r[     s/   1     # %    &   A @5H 5
> >
 c4i 1:E#s(O,sCxj$56
 c4i 1:E#s(O,sCxj$56 8x'PQ<<H
( t $5 , (0 U >H > >+ >D  
	{C	 c? 		? 	
 DC C	*> ? C
"H "8BS BY}5 B
 S$ryy/12  ( 
 XC X# XS X$ X8 X 
 X 
&)36 

&)3;C=
8\!"4$(/ $(E+s:J4K $(N   F,
, 13DDE, ;%'(,	
	#J/	
2::x
++,	8{ 8t 8 rzz  h  M
M +M
5h&'589M`"**  G+ GC GD G!M !eOU4J.K !H
 +
5./5@A$H HV)E,
":;<)
8L!8J#778)" 	h$%(L+FG(L()+	
		 )1C )1r)   