o �J�h�.�@s�ddlmZddlZddlmZddlZddlmZddlm Z ddlm Z ddlm Z Gdd �d �Z ddd�Z ddd�Zddd�ZdS) �)� annotationsN)�Any)�BaseDistribution)�CategoricalDistribution)�FloatDistribution)�IntDistributionc@s`eZdZdZ   ddd d �Zed dd��Zed!dd��Zed dd��Zd"dd�Z d#dd�Z dS)$�_SearchSpaceTransformai Transform a search space and parameter configurations to continuous space. The search space bounds and parameter configurations are represented as ``numpy.ndarray``s and transformed into continuous space. Bounds and parameters associated with categorical distributions are one-hot encoded. Parameter configurations in this space can additionally be untransformed, or mapped back to the original space. This type of transformation/untransformation is useful for e.g. implementing samplers without having to condition on distribution types before sampling parameter values. Args: search_space: The search space. If any transformations are to be applied, parameter configurations are assumed to hold parameter values for all of the distributions defined in this search space. Otherwise, assertion failures will be raised. transform_log: If :obj:`True`, apply log/exp operations to the bounds and parameters with corresponding distributions in log space during transformation/untransformation. Should always be :obj:`True` if any parameters are going to be sampled from the transformed space. transform_step: If :obj:`True`, offset the lower and higher bounds by a half step each, increasing the space by one step. This allows fair sampling for values close to the bounds. Should always be :obj:`True` if any parameters are going to be sampled from the transformed space. transform_0_1: If :obj:`True`, apply a linear transformation to the bounds and parameters so that they are in the unit cube. Attributes: bounds: Constructed bounds from the given search space. column_to_encoded_columns: Constructed mapping from original parameter column index to encoded column indices. encoded_column_to_column: Constructed mapping from encoded column index to original parameter column index. Note: Parameter values are not scaled to the unit cube. Note: ``transform_log`` and ``transform_step`` are useful for constructing bounds and parameters without any actual transformations by setting those arguments to :obj:`False`. This is needed for e.g. the hyperparameter importance assessments. TF� search_space�dict[str, BaseDistribution]� transform_log�bool�transform_step� transform_0_1�return�NonecCs:t|||�\}}}||_||_||_||_||_||_dS�N)�_transform_search_space� _raw_bounds�_column_to_encoded_columns�_encoded_column_to_column� _search_space�_transform_log�_transform_0_1)�selfr r r r�bounds�column_to_encoded_columns�encoded_column_to_column�r�OC:\pinokio\api\whisper-webui.git\app\env\lib\site-packages\optuna\_transform.py�__init__=s � z_SearchSpaceTransform.__init__� np.ndarraycCs(|jrt�ddgg|jjd�S|jS)N�g�?r)r�np�arrayr�shape�rrrrrNsz_SearchSpaceTransform.bounds�list[np.ndarray]cC�|jSr)rr%rrrrU�z/_SearchSpaceTransform.column_to_encoded_columnscCr'r)rr%rrrrYr(z._SearchSpaceTransform.encoded_column_to_column�params�dict[str, Any]c Cs tj|jjdtjd�}d}|j��D]7\}}||vsJd��||}t|t�r=t |� |��}d|||<|t |j �7}qt |||j�||<|d7}q|jr�|jdd�df|jdd�dfk}d||<|||j|df|j|df|j|df||<|S)a5Transform a parameter configuration from actual values to continuous space. Args: params: A parameter configuration to transform. Returns: A 1-dimensional ``numpy.ndarray`` holding the transformed parameters in the configuration. r��dtypez7Parameter configuration must contain all distributions.�N��?)r"�zerosrr$�float64r�items� isinstancer�int�to_internal_repr�len�choices�_transform_numerical_paramrr) rr)� trans_params� bound_idx�name� distribution�paramZ choice_idxZ single_maskrrr� transform]s(   � $ �z_SearchSpaceTransform.transformr8cCs�|j|jjdfks J�|jr,|jdd�df||jdd�df|jdd�df}i}t|j��|j�D]$\\}}}||}t|t�rN|� |� ��}n t |� �||j �}|||<q7|S)a�Untransform a parameter configuration from continuous space to actual values. Args: trans_params: A 1-dimensional ``numpy.ndarray`` in the transformed space corresponding to a parameter configuration. Returns: A dictionary of an untransformed parameter configuration. Keys are parameter names. Values are untransformed parameter values. rNr-)r$rr�ziprr1rr2r�to_external_repr�argmax�_untransform_numerical_param�itemr)rr8r)r:r;�encoded_columns� trans_paramr<rrr� untransform�s" "� �  � z!_SearchSpaceTransform.untransformN)TTF) r r r r r r rr rr)rr )rr&)r)r*rr )r8r rr*) �__name__� __module__� __qualname__�__doc__r�propertyrrrr=rErrrrrs1 �    &rr r r r r r�/tuple[np.ndarray, list[np.ndarray], np.ndarray]cCs�t|�dks Jd��tdd�|��D��}tj|dftjd�}g}tj|tjd�}d}|��D]�}|} t| t�r]t| j �} d|||| �<t� ||| �} t|�|| <|� | �|| 7}q/t| t t f�r�t| t �r�| jdur�|rud| jnd } t| j| |�| t| j| |�| f} nJt| j| |�t| j| |�f} n;t| t �r�|r�d| jnd } | jr�t| j| | |�t| j| | |�f} nt| j| |�| t| j| |�| f} nJd ��| ||<t�|�}t|�||<|� |�|d 7}q/Jd ��||ks�J�|||fS) Nrz/Cannot transform if no distributions are given.css(�|]}t|t�rt|j�ndVqdS)r-N)r2rr5r6)�.0�drrr� <genexpr>�s �� �z*_transform_search_space.<locals>.<genexpr>�r+)rr-r.r!F�*Should not reach. Unexpected distribution.r-)r5�sum�valuesr"�emptyr0�int64r2rr6�arange�appendrr�stepr7�low�high�log� atleast_1d)r r r �n_boundsrrrr9r;rMZ n_choicesrCZ half_stepZbdsZencoded_columnrrrr�sd�      ��  �  � ��      rr<� int | floatr;r�floatcCs�|}t|t�r Jd��t|t�r&|jr |rt�|�nt|�}|St|�}|St|t�rA|jr;|r5t�|�nt|�}|St|�}|SJd��)NF�,Should not reach. Should be one-hot encoded.rP)r2rrrZ�mathr^r)r<r;r rMrDrrrr7�s    � ���r7rDcCsZ|}t|t�r Jd��t|t�rj|jr2|rt�|�n|}|��r# |St|t� |j |j d��}|S|j durSt t� t�||j|j �|j |j|j|j ��}|S|��r[|}|St|t� |j |j d��}|St|t�r�|jr�|r�tt� t�t�|��|j|j ��}|St|�}|Stt� t�||j|j �|j |j|j|j ��}|SJd��)NFr_r-rP)r2rrrZr`�exp�single�minr"� nextafterrYrWr^�clip�roundrXrr3)rDr;r rMr<rrrrA sB  � �.��� �" ��.��rA)r r r r r r rrK)r<r]r;rr r rr^)rDr^r;rr r rr])� __future__rr`�typingr�numpyr"�optuna.distributionsrrrrrrr7rArrrr�<module>s        G
Memory