o
    ^Ti                     @   sv   d Z ddlmZm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 G d	d
 d
ee	ZdS )zuRegressor utilities for MySQL Connector/Python.

Provides a scikit-learn compatible regressor backed by HeatWave ML.
    )OptionalUnionN)RegressorMixin)MyBaseMLModel)ML_TASK)	copy_dict)MySQLConnectionAbstractc                   @   s   e Zd ZdZ				ddedee dee dee dee f
dd	Zd
e	e
jejf dejfddZd
e	e
jejf de
jfddZdS )MyRegressora  
    MySQL HeatWave scikit-learn compatible regressor estimator.

    Provides prediction output from a regression model deployed in MySQL,
    and manages fit, explain, and prediction options as per HeatWave ML interface.

    Attributes:
        predict_extra_options (dict): Optional parameter dict passed to the backend for prediction.
        _model (MyModel): Underlying interface for database model operations.
        fit_extra_options (dict): See MyBaseMLModel.
        explain_extra_options (dict): See MyBaseMLModel.

    Args:
        db_connection (MySQLConnectionAbstract): Active MySQL connector DB connection.
        model_name (str, optional): Custom name for the model.
        fit_extra_options (dict, optional): Extra options for fitting.
        explain_extra_options (dict, optional): Extra options for explanations.
        predict_extra_options (dict, optional): Extra options for predictions.

    Methods:
        predict(X): Predict regression target.
    Ndb_connection
model_namefit_extra_optionsexplain_extra_optionspredict_extra_optionsc                 C   s.   t j| |tj||d t|| _t|| _dS )a  
        Initialize a MyRegressor.

        Args:
            db_connection: Active MySQL connector database connection.
            model_name: Optional, custom model name.
            fit_extra_options: Optional fit options.
            explain_extra_options: Optional explain options.
            predict_extra_options: Optional prediction options.

        Raises:
            DatabaseError:
                If a database connection issue occurs.
                If an operational error occurs during execution.
        )r   r   N)r   __init__r   
REGRESSIONr   r   r   )selfr
   r   r   r   r    r   Q/home/air/sos-pdf/back/venv/lib/python3.10/site-packages/mysql/ai/ml/regressor.pyr   F   s   
zMyRegressor.__init__Xreturnc                 C   s   | j j|| jd}|d  S )aZ  
        Predict a continuous target for the input features using the MySQL model.

        Args:
            X: Input samples as a numpy array or pandas DataFrame.

        Returns:
            ndarray: Array of predicted target values, shape (n_samples,).

        Raises:
            DatabaseError:
                If provided options are invalid or unsupported,
                or if the model is not initialized, i.e., fit or import has not
                been called
                If a database connection issue occurs.
                If an operational error occurs during execution.
        options
Prediction)_modelpredictr   to_numpy)r   r   resultr   r   r   r   h   s   zMyRegressor.predictc                 C   s   | j j|| jd dS )ai  
        Explain model predictions using provided data.

        References:
            https://dev.mysql.com/doc/heatwave/en/mys-hwaml-ml-explain-table.html
                A full list of supported options can be found under "ML_EXPLAIN_TABLE Options"

        Args:
            X: DataFrame for which predictions should be explained.

        Returns:
            DataFrame containing explanation details (feature attributions, etc.)

        Raises:
            DatabaseError:
                If provided options are invalid or unsupported,
                or if the model is not initialized, i.e., fit or import has not
                been called
                If a database connection issue occurs.
                If an operational error occurs during execution.

        Notes:
            Temporary input/output tables are cleaned up after explanation.
        r   N)r   explain_predictionsr   )r   r   r   r   r   r      s   zMyRegressor.explain_predictions)NNNN)__name__
__module____qualname____doc__r   r   strdictr   r   pd	DataFramenpndarrayr   r   r   r   r   r   r	   .   s6    
"
r	   )r!   typingr   r   numpyr&   pandasr$   sklearn.baser   mysql.ai.ml.baser   mysql.ai.ml.modelr   mysql.ai.utilsr   mysql.connector.abstractsr   r	   r   r   r   r   <module>   s   