Ë
    þÍ:jÏ.  ã                  óÈ   — d dl mZ d dlZd dl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 erd dlmZ  G d	„ d
«      Z	 	 	 	 	 	 	 	 dd„Z	 	 	 	 	 	 	 	 dd„Z	 	 	 	 	 	 	 	 dd„Zy)é    )ÚannotationsN)ÚAny)ÚTYPE_CHECKING)ÚCategoricalDistribution)ÚFloatDistribution)ÚIntDistribution)ÚBaseDistributionc                  óx   — e Zd ZdZ	 	 	 d		 	 	 	 	 	 	 	 	 d
d„Zedd„«       Zedd„«       Zedd„«       Zdd„Z	dd„Z
y)Ú_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.

    c                óz   — t        |||«      \  }}}|| _        || _        || _        || _        || _        || _        y ©N)Ú_transform_search_spaceÚ_raw_boundsÚ_column_to_encoded_columnsÚ_encoded_column_to_columnÚ_search_spaceÚ_transform_logÚ_transform_0_1)ÚselfÚsearch_spaceÚtransform_logÚtransform_stepÚtransform_0_1ÚboundsÚcolumn_to_encoded_columnsÚencoded_column_to_columns           úf/home/mcse/projects/srt_converter/srt-converter-venv/lib/python3.12/site-packages/optuna/_transform.pyÚ__init__z_SearchSpaceTransform.__init__A   sS   € ô G^Ø˜-¨óG
ÑCˆÐ)Ð+Cð "ˆÔØ*CˆÔ'Ø)AˆÔ&Ø)ˆÔØ+ˆÔØ+ˆÕó    c                ó–   — | j                   r2t        j                  ddgg| j                  j                  d   z  «      S | j                  S )Nç        g      ð?r   )r   ÚnpÚarrayr   Úshape©r   s    r   r   z_SearchSpaceTransform.boundsR   sB   € à×ÒÜ—8‘8˜c 3˜Z˜L¨4×+;Ñ+;×+AÑ+AÀ!Ñ+DÑDÓEÐEà×#Ñ#Ð#r   c                ó   — | j                   S r   )r   r%   s    r   r   z/_SearchSpaceTransform.column_to_encoded_columnsY   s   € à×.Ñ.Ð.r   c                ó   — | j                   S r   )r   r%   s    r   r   z._SearchSpaceTransform.encoded_column_to_column]   s   € à×-Ñ-Ð-r   c                ó°  — t        j                  | j                  j                  d   t         j                  ¬«      }d}| j
                  j                  «       D ]  \  }}||v sJ d«       ‚||   }t        |t        «      r;t        |j                  |«      «      }d|||z   <   |t        |j                  «      z  }Œat        ||| j                  «      ||<   |dz  }Œ� | j                  ro| j                  dd…df   | j                  dd…df   k(  }d||<   ||    | j                  | df   z
  | j                  | df   | j                  | df   z
  z  || <   |S )a5  Transform 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"   Úzerosr   r$   Úfloat64r   ÚitemsÚ
isinstancer   ÚintÚto_internal_reprÚlenÚchoicesÚ_transform_numerical_paramr   r   )	r   ÚparamsÚtrans_paramsÚ	bound_idxÚnameÚdistributionÚparamÚ
choice_idxÚsingle_masks	            r   Ú	transformz_SearchSpaceTransform.transforma   sv  € ô —x‘x × 0Ñ 0× 6Ñ 6°qÑ 9ÄÇÁÔLˆàˆ	Ø"&×"4Ñ"4×":Ñ":Ó"<ò 	ÑˆD�,Ø˜6‘>Ð\Ð#\Ó\�>Ø˜4‘LˆEä˜,Ô(?Ô@Ü  ×!>Ñ!>¸uÓ!EÓF�
Ø78�˜Y¨Ñ3Ñ4ØœS ×!5Ñ!5Ó6Ñ6‘	ä*DØ˜<¨×)<Ñ)<ó+�˜YÑ'ð ˜Q‘‘	ð	ð ×ÒØ×*Ñ*ª1¨a¨4Ñ0°D×4DÑ4DÂQÈÀTÑ4JÑJˆKØ(+ˆL˜Ñ%à˜k˜\Ñ*¨T×-=Ñ-=¸{¸lÈA¸oÑ-NÑNØ×!Ñ! ; ,° /Ñ2°T×5EÑ5EÀ{ÀlÐTUÀoÑ5VÑVñ*XˆL˜+˜Ñ&ð Ðr   c                ó   — |j                   | j                  j                   d   fk(  sJ ‚| j                  r>| j                  dd…df   || j                  dd…df   | j                  dd…df   z
  z  z   }i }t        | j                  j                  «       | j                  «      D ]g  \  \  }}}||   }t        |t        «      r |j                  |j                  «       «      }n%t        |j                  «       || j                  «      }|||<   Œi |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.

        r   Nr+   )r$   r   r   Úzipr   r/   r   r0   r   Úto_external_reprÚargmaxÚ_untransform_numerical_paramÚitemr   )r   r7   r6   r9   r:   Úencoded_columnsÚtrans_paramr;   s           r   Úuntransformz!_SearchSpaceTransform.untransform‡   s  € ð ×!Ñ! d×&6Ñ&6×&<Ñ&<¸QÑ&?Ð%AÒAÐAÐAà×ÒØ×+Ñ+ªA¨q¨DÑ1°LØ× Ñ ¢ A Ñ&¨×)9Ñ)9º!¸Q¸$Ñ)?Ñ?ñ5ñ ˆLð ˆä58Ø×Ñ×$Ñ$Ó&¨×(FÑ(Fó6
ò 	!Ñ1Ñ ˆT�< /ð ' Ñ7ˆKä˜,Ô(?Ô@à$×5Ñ5°k×6HÑ6HÓ6JÓK‘ä4Ø×$Ñ$Ó&¨°d×6IÑ6Ió�ð !ˆF�4ŠLð	!ð ˆr   N)TTF)
r   údict[str, BaseDistribution]r   Úboolr   rI   r   rI   ÚreturnÚNone)rJ   ú
np.ndarray)rJ   zlist[np.ndarray])r6   údict[str, Any]rJ   rL   )r7   rL   rJ   rM   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr   r   r   r>   rG   © r   r   r   r      s�   „ ñ,ðb #Ø#Ø#ð,à1ð,ð ð,ð ð	,ð
 ð,ð 
ó,ð" ò$ó ð$ð ò/ó ð/ð ò.ó ð.ó$ôL%r   r   c                óJ  — t        | «      dkD  sJ d«       ‚t        d„ | j                  «       D «       «      }t        j                  |dft        j
                  ¬«      }g }t        j                  |t        j                  ¬«      }d}| j                  «       D �]ÿ  }|}	t        |	t        «      r[t        |	j                  «      }
d||||
z    t        j                  |||
z   «      }t        |«      ||<   |j                  |«       ||
z  }Œqt        |	t        t        f«      �rst        |	t        «      rƒ|	j                  �H|rd|	j                  z  nd}t        |	j                   |	|«      |z
  t        |	j"                  |	|«      |z   f}nÏt        |	j                   |	|«      t        |	j"                  |	|«      f}n t        |	t        «      r‰|rd|	j                  z  nd}|	j$                  r5t        |	j                   |z
  |	|«      t        |	j"                  |z   |	|«      f}n<t        |	j                   |	|«      |z
  t        |	j"                  |	|«      |z   f}nJ d	«       ‚|||<   t        j&                  |«      }t        |«      ||<   |j                  |«       |d
z  }�ŒûJ d	«       ‚ ||k(  sJ ‚|||fS )Nr   z/Cannot transform if no distributions are given.c              3  ój   K  — | ]+  }t        |t        «      rt        |j                  «      nd –— Œ- y­w)r+   N)r0   r   r3   r4   )Ú.0Úds     r   ú	<genexpr>z*_transform_search_space.<locals>.<genexpr>´   s/   è ø€ ò àô % QÔ(?Ô@ŒˆA�I‰IŒÀaÓGñùs   ‚13é   r)   )r   r+   r,   r!   ú*Should not reach. Unexpected distribution.r+   )r3   ÚsumÚvaluesr"   Úemptyr.   Úint64r0   r   r4   ÚarangeÚappendr   r   Ústepr5   ÚlowÚhighÚlogÚ
atleast_1d)r   r   r   Ún_boundsr   r   r   r8   r:   rW   Ú	n_choicesrE   Ú	half_stepÚbdsÚencoded_columns                  r   r   r   ¯   s—  € ô ˆ|Ó˜qÒ ÐSÐ"SÓSÐ äñ à×$Ñ$Ó&ôó €Hô
 �X‰X�x �m¬2¯:©:Ô6€FØ24ÐÜ!Ÿx™x¨¼¿¹ÔAÐà€IØ$×+Ñ+Ó-ó 1GˆØˆÜ�aÔ0Ô1Ü˜AŸI™I›ˆIØ8>ˆF�9˜y¨9Ñ4Ð5Ü Ÿi™i¨	°9¸yÑ3HÓIˆOÜ8;Ð<UÓ8VÐ$ _Ñ5Ø%×,Ñ,¨_Ô=Ø˜Ñ"‰IÜØä!Üðõ
ô ˜!Ô.Ô/Ø—6‘6Ð%Ù0>  a§f¡f¢ÀC�Iä2°1·5±5¸!¸]ÓKÈiÑWÜ2°1·6±6¸1¸mÓLÈyÑXð‘Cô 3°1·5±5¸!¸]ÓKÜ2°1·6±6¸1¸mÓLð‘Cô ˜AœÔ/Ù,:˜C !§&¡&šLÀ�	Ø—5’5ä2°1·5±5¸9Ñ3DÀaÈÓWÜ2°1·6±6¸IÑ3EÀqÈ-ÓXð‘Cô 3°1·5±5¸!¸]ÓKÈiÑWÜ2°1·6±6¸1¸mÓLÈyÑXð‘Cð
 KÐJÓJ�uà #ˆF�9ÑÜŸ]™]¨9Ó5ˆNÜ7:Ð;TÓ7UÐ$ ^Ñ4Ø%×,Ñ,¨^Ô<Ø˜‰NŠIàFÐFÓF�5ðc1Gðf ˜Ò Ð Ð àÐ,Ð.FÐFÐFr   c                óv  — |}t        |t        «      rJ d«       ‚t        |t        «      r=|j                  r$|rt	        j                  | «      n
t        | «      }|S t        | «      }|S t        |t        «      r=|j                  r$|rt	        j                  | «      n
t        | «      }|S t        | «      }|S J d«       ‚)Nú,Should not reach. Should be one-hot encoded.rZ   )r0   r   r   rd   ÚmathÚfloatr   )r;   r:   r   rW   rF   s        r   r5   r5   ö   s²   € ð 	€Aä�!Ô,Ô-ØDÐDÓDˆuÜ	�AÔ(Ô	)Ø�5Š5Ù-:œ$Ÿ(™( 5œ/ÄÀeÃˆKð Ðô   ›,ˆKð Ðô 
�A”Ô	'Ø�5Š5Ù-:œ$Ÿ(™( 5œ/ÄÀeÃˆKð Ðô	   ›,ˆKð Ðð 	CÐBÓBˆur   c           	     óú  — |}t        |t        «      rJ d«       ‚t        |t        «      �rG|j                  re|rt	        j
                  | «      n| }|j                  «       r	 |S t        |t        j                  |j                  |j                  dz
  «      «      }|S |j                  �}t        t        j                  t        j                  | |j                  z
  |j                  z  «      |j                  z  |j                  z   |j                  |j                  «      «      }|S |j                  «       r| }|S t        | t        j                  |j                  |j                  dz
  «      «      }|S t        |t         «      rô|j                  rk|r\t#        t        j                  t        j                  t	        j
                  | «      «      |j                  |j                  «      «      }|S t#        | «      }|S t#        t        j                  t        j                  | |j                  z
  |j                  z  «      |j                  z  |j                  z   |j                  |j                  «      «      }|S J d«       ‚)Nrl   r+   rZ   )r0   r   r   rd   rm   ÚexpÚsingleÚminr"   Ú	nextafterrc   ra   rn   ÚclipÚroundrb   r   r1   )rF   r:   r   rW   r;   s        r   rC   rC     sú  € ð 	€Aä�!Ô,Ô-ØDÐDÓDˆuÜ	�AÔ(Õ	)Ø�5Š5Ù-:”D—H‘H˜[Ô)ÀˆEØ�x‰xŒzØð2 €Lô/ ˜E¤2§<¡<°·±¸¿¹À¹
Ó#CÓD�ð. €Lð- �V‰VÐÜÜ—‘œŸ™ +°·±Ñ"5¸¿¹Ñ!?Ó@À1Ç6Á6ÑIÈAÏEÉEÑQÐST×SXÑSXÐZ[×Z`ÑZ`ÓaóˆEð* €Lð# �x‰xŒzØ#�ð  €Lô ˜K¬¯©°a·f±f¸a¿f¹fÀq¹jÓ)IÓJ�ð €Lô 
�A”Ô	'Ø�5Š5ÙÜœBŸG™G¤B§H¡H¬T¯X©X°kÓ-BÓ$CÀQÇUÁUÈAÏFÉFÓSÓT�ð €Lô ˜KÓ(�ð €Lô Ü—‘œŸ™ +°·±Ñ"5¸¿¹Ñ!?Ó@À1Ç6Á6ÑIÈAÏEÉEÑQÐST×SXÑSXÐZ[×Z`ÑZ`ÓaóˆEð €Lð 	CÐBÓBˆur   )r   rH   r   rI   r   rI   rJ   z/tuple[np.ndarray, list[np.ndarray], np.ndarray])r;   úint | floatr:   r	   r   rI   rJ   rn   )rF   rn   r:   r	   r   rI   rJ   rv   )Ú
__future__r   rm   Útypingr   r   Únumpyr"   Úoptuna.distributionsr   r   r   r	   r   r   r5   rC   rS   r   r   ú<module>r{      s¬   ðÝ "ã Ý Ý  ã å 8Ý 2Ý 0ñ Ý5÷Zñ ZðzDGØ-ðDGØ>BðDGØTXðDGà4óDGðNØðØ&6ðØGKðà
óð.$Øð$Ø&6ð$ØGKð$àô$r   