
    ה9iZ                         d Z ddlmZmZ ddlm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	gZ G d
 de	e      ZddZy)zUtilities for meta-estimators.    )ABCMetaabstractmethod)suppressN)BaseEstimator)_safe_indexing)available_if)get_tagsr   c                   J     e Zd ZdZed        Zd fd	Z fdZd Zd Z	 xZ
S )_BaseCompositiona  Base class for estimators that are composed of named sub-estimators.

    This abstract class provides parameter management functionality for
    meta-estimators that contain collections of named estimators. It handles
    the complex logic for getting and setting parameters on nested estimators
    using the "estimator_name__parameter" syntax.

    The class is designed to work with any attribute containing a list of
    (name, estimator) tuples.
    c                      y N )selfs    f/var/www/html/backtest/airagagent/rag_env/lib/python3.12/site-packages/sklearn/utils/metaestimators.py__init__z_BaseComposition.__init__   s        c                 ,   t         	|   |      }|s|S t        | |      }	 |j                  |       |D ]D  \  }}t        |d      s|j                  d      j                         D ]  \  }}|||d|<    F |S # t        t
        f$ r |cY S w xY w)Ndeep
get_paramsT__)superr   getattrupdate	TypeError
ValueErrorhasattritems)
r   attrr   out
estimatorsname	estimatorkeyvalue	__class__s
            r   _get_paramsz_BaseComposition._get_params#   s    g d +JT4(
	JJz"  * 	8OD)y,/"+"6"6D"6"A"G"G"I 8JC27CD#./8	8 
 :& 	 J	s   A? ?BBc           	         ||v rt        | ||j                  |             t        | |      }t        |t              rp|rnt        t              5  t        | \  }}t	        |j                               D ].  }d|vs||v s| j                  |||j                  |             0 	 d d d        t        | ,  di | | S # 1 sw Y   xY w)Nr   r   )setattrpopr   
isinstancelistr   r   zipkeys_replace_estimatorr   
set_params)r   r   paramsr   
item_names_r"   r&   s          r   _set_paramsz_BaseComposition._set_params9   s     6>D$

4 01d#eT"u )$ N #U
A / ND4'DJ,>//dFJJt<LMNN 	$V$N Ns   ,B?<B?$B??Cc                     t        t        | |            }t        |      D ]  \  }\  }}||k(  s||f||<    n t        | ||       y r   )r,   r   	enumerater)   )r   r   r"   new_valnew_estimatorsiestimator_namer3   s           r   r/   z#_BaseComposition._replace_estimatorN   sX    gdD12&/&? 	"A"%%)7Oq!	 	dN+r   c                    t        t        |            t        |      k7  r#t        dj                  t	        |                  t        |      j                  | j                  d            }|r#t        dj                  t        |                  |D cg c]	  }d|v s| }}|rt        dj                  |            y c c}w )Nz$Names provided are not unique: {0!r}Fr   z:Estimator names conflict with constructor arguments: {0!r}r   z.Estimator names must not contain __: got {0!r})lensetr   formatr,   intersectionr   sorted)r   namesinvalid_namesr"   s       r   _validate_namesz _BaseComposition._validate_namesW   s    s5z?c%j(CJJ4PU;WXXE
//U0KLLSS=) 
 +0@$44<@@@GGV   As   	C!C)T)__name__
__module____qualname____doc__r   r   r'   r4   r/   rC   __classcell__)r&   s   @r   r   r      s0    	  ,*,r   r   )	metaclassc                 v   t        |       j                  j                  rwt        |d      st	        d      |j
                  d   |j
                  d   k7  rt	        d      ||t        j                  ||         }n&|t        j                  ||         }nt        ||      }|t        ||      }||fS d}||fS )a  Create subset of dataset and properly handle kernels.

    Slice X, y according to indices for cross-validation, but take care of
    precomputed kernel-matrices or pairwise affinities / distances.

    If ``estimator._pairwise is True``, X needs to be square and
    we slice rows and columns. If ``train_indices`` is not None,
    we slice rows using ``indices`` (assumed the test set) and columns
    using ``train_indices``, indicating the training set.

    Labels y will always be indexed only along the first axis.

    Parameters
    ----------
    estimator : object
        Estimator to determine whether we should slice only rows or rows and
        columns.

    X : array-like, sparse matrix or iterable
        Data to be indexed. If ``estimator._pairwise is True``,
        this needs to be a square array-like or sparse matrix.

    y : array-like, sparse matrix or iterable
        Targets to be indexed.

    indices : array of int
        Rows to select from X and y.
        If ``estimator._pairwise is True`` and ``train_indices is None``
        then ``indices`` will also be used to slice columns.

    train_indices : array of int or None, default=None
        If ``estimator._pairwise is True`` and ``train_indices is not None``,
        then ``train_indices`` will be use to slice the columns of X.

    Returns
    -------
    X_subset : array-like, sparse matrix or list
        Indexed data.

    y_subset : array-like, sparse matrix or list
        Indexed targets.

    shapezXPrecomputed kernels or affinity matrices have to be passed as arrays or sparse matrices.r      z"X should be a square kernel matrixN)	r	   
input_tagspairwiser   r   rK   npix_r   )r#   Xyindicestrain_indicesX_subsety_subsets          r   _safe_splitrW   h   s    X 	%%..q'"= 
 771:#ABB 12H78H!!W-}!!W- X Xr   r   )rG   abcr   r   
contextlibr   numpyrO   sklearn.baser   sklearn.utilsr   sklearn.utils._available_ifr   sklearn.utils._tagsr	   __all__r   rW   r   r   r   <module>r`      s>    $
 (   & ( 4 (
R} RjAr   