
    -ji.                        S SK Jr  S SKrS SKJr  S SKrS SKJr  S SKJ	r	  S SKJ
r
  S SKJr   " S S	5      r        SS
 jr        SS jr        SS jrg)    )annotationsN)Any)BaseDistribution)CategoricalDistribution)FloatDistribution)IntDistributionc                      \ rS rSrSr   S         SS jjr\SS j5       r\SS j5       r\SS j5       r	SS jr
SS jrS	rg
)_SearchSpaceTransform   a  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                j    [        XU5      u  pVnXPl        X`l        Xpl        Xl        X l        X@l        g 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           K/home/james-whalen/.local/lib/python3.13/site-packages/optuna/_transform.py__init___SearchSpaceTransform.__init__=   sB     G^G
C+C "*C')A&)++    c                    U R                   (       a3  [        R                  " SS//U R                  R                  S   -  5      $ U R                  $ )N        g      ?r   )r   nparrayr   shaper   s    r   r   _SearchSpaceTransform.boundsN   sB    88c3ZL4+;+;+A+A!+DDEE###r    c                    U R                   $ r   )r   r&   s    r   r   /_SearchSpaceTransform.column_to_encoded_columnsU   s    ...r    c                    U R                   $ r   )r   r&   s    r   r   ._SearchSpaceTransform.encoded_column_to_columnY   s    ---r    c                   [         R                  " U R                  R                  S   [         R                  S9nSnU R
                  R                  5        H  u  pEXA;   d   S5       eX   n[        U[        5      (       a;  [        UR                  U5      5      nSX#U-   '   U[        UR                  5      -  nMe  [        XeU R                  5      X#'   US-  nM     U R                  (       al  U R                  SS2S4   U R                  SS2S4   :H  nSX('   X()    U R                  U) S4   -
  U R                  U) S4   U R                  U) S4   -
  -  X() '   U$ )zTransform 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   	transform_SearchSpaceTransform.transform]   sb    xx 0 0 6 6q 9L	"&"4"4":":"<D>\#\\>LE,(?@@ !>!>u!EF
7834S!5!566	*D)<)<+' Q	 #= **1a40D4D4DQT4JJK(+L%\*T-=-={lAo-NN!!;,/2T5E5E{lTUo5VV*XL& r    c                2   UR                   U R                  R                   S   4:X  d   eU R                  (       a>  U R                  SS2S4   UU R                  SS2S4   U R                  SS2S4   -
  -  -   n0 n[        U R                  R                  5       U R                  5       Hi  u  u  p4nX   n[        U[        5      (       a   UR                  UR                  5       5      nO$[        UR                  5       X@R                  5      nXrU'   Mk     U$ )ap  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   r3   r   r4   r   to_external_reprargmax_untransform_numerical_paramitemr   )r   r;   r:   r=   r>   encoded_columnstrans_paramr?   s           r   untransform!_SearchSpaceTransform.untransform   s    !!d&6&6&<&<Q&?%AAAA++AqD1L  A&)9)9!Q$)??5 L 58$$&(F(F6
1 T/ '7K,(?@@$55k6H6H6JK4$$&6I6I !4L6
 r    )r   r   r   r   r   r   N)TTF)
r   dict[str, BaseDistribution]r   boolr   rO   r   rO   returnNone)rP   
np.ndarray)rP   zlist[np.ndarray])r:   dict[str, Any]rP   rR   )r;   rR   rP   rS   )__name__
__module____qualname____firstlineno____doc__r   propertyr   r   r   rB   rL   __static_attributes__ r    r   r
   r
      s    ,b ###,1, , 	,
 , 
," $ $ / / . .$L%r    r
   c                v   [        U 5      S:  d   S5       e[        S U R                  5        5       5      n[        R                  " US4[        R
                  S9n/ n[        R                  " U[        R                  S9nSnU R                  5        GH  nUn	[        U	[        5      (       aX  [        U	R                  5      n
SXGXz-   & [        R                  " XwU
-   5      n[        U5      Xk'   UR                  U5        Xz-  nMs  [        U	[        [        45      (       Ga  [        U	[        5      (       a  U	R                  bK  U(       a  SU	R                  -  OSn[        U	R                   X5      U-
  [        U	R"                  X5      U-   4nO[        U	R                   X5      [        U	R"                  X5      4nO[        U	[        5      (       a  U(       a  SU	R                  -  OSnU	R$                  (       a3  [        U	R                   U-
  X5      [        U	R"                  U-   X5      4nO:[        U	R                   X5      U-
  [        U	R"                  X5      U-   4nO S	5       eXU'   [        R&                  " U5      n[        U5      Xn'   UR                  U5        US
-  nGM   S	5       e   Xs:X  d   eXEU4$ )Nr   z/Cannot transform if no distributions are given.c              3  z   #    U  H1  n[        U[        5      (       a  [        UR                  5      OS v   M3     g7f)r/   N)r4   r   r7   r8   ).0ds     r   	<genexpr>*_transform_search_space.<locals>.<genexpr>   s1      &A %Q(?@@AIIaG&s   9;   r-   )r   r/   r0   r"   *Should not reach. Unexpected distribution.r/   )r7   sumvaluesr#   emptyr2   int64r4   r   r8   arangeappendr   r   stepr9   lowhighlog
atleast_1d)r   r   r   n_boundsr   r   r   r<   r>   r_   	n_choicesrJ   	half_stepbdsencoded_columns                  r   r   r      s{    |q S"SS  $$& H
 XXxm2::6F24!xxAI$++-a011AIII8>Fy45 ii	y3HIO8;<U8V$5%,,_="I!
 
 !.//66%0>affCI2155!KiW21661LyXC 3155!K21661LC A//,:C!&&L	55215593DaW2166I3EqXC 3155!KiW21661LyXC
 KJJu #9]]95N7:;T7U$4%,,^<NIFFF5c .f    .FFFr    c                   Un[        U[        5      (       a   S5       e[        U[        5      (       aI  UR                  (       a*  U(       a  [        R                  " U 5      O
[        U 5      nU$ [        U 5      n U$ [        U[        5      (       aI  UR                  (       a*  U(       a  [        R                  " U 5      O
[        U 5      nU$ [        U 5      n U$  S5       e)N,Should not reach. Should be one-hot encoded.rc   )r4   r   r   rm   mathfloatr   )r?   r>   r   r_   rK   s        r   r9   r9      s     	A!,--DDDu	A(	)	)55-:$((5/eK   ,K  
A	'	'55-:$((5/eK 	  ,K  	CBBur    c           	     p   Un[        U[        5      (       a   S5       e[        U[        5      (       Gab  UR                  (       ar  U(       a  [        R
                  " U 5      OU nUR                  5       (       a   U$ [        U[        R                  " UR                  UR                  S-
  5      5      n U$ UR                  b~  [        [        R                  " [        R                  " XR                  -
  UR                  -  5      UR                  -  UR                  -   UR                  UR                  5      5      nU$ UR                  5       (       a  U nU$ [        U [        R                  " UR                  UR                  S-
  5      5      n U$ [        U[         5      (       Ga  UR                  (       at  U(       a_  [#        [        R                  " [        R                  " [        R
                  " U 5      5      UR                  UR                  5      5      nU$ [#        U 5      n U$ [#        [        R                  " [        R                  " XR                  -
  UR                  -  5      UR                  -  UR                  -   UR                  UR                  5      5      n U$  S5       e)Nru   r/   rc   )r4   r   r   rm   rv   expsingleminr#   	nextafterrl   rj   rw   cliproundrk   r   r5   )rK   r>   r   r_   r?   s        r   rH   rH   	  s    	A!,--DDDu	A(	)	)55-:DHH[)Exxzz2 L/ E2<<
#CD. L- VV+"5!?@166IAEEQSTSXSXZ[Z`Z`aE* L# xxzz#  L Kaffaffqj)IJ L 
A	'	'55BGGBHHTXXk-B$CQUUAFFST L K( L +"5!?@166IAEEQSTSXSXZ[Z`Z`aE L 	CBBur    )r   rN   r   rO   r   rO   rP   z/tuple[np.ndarray, list[np.ndarray], np.ndarray])r?   int | floatr>   r   r   rO   rP   rw   )rK   rw   r>   r   r   rO   rP   r   )
__future__r   rv   typingr   numpyr#   optuna.distributionsr   r   r   r   r
   r   r9   rH   r[   r    r   <module>r      s    "    1 8 2 0Z ZzDG-DG>BDGTXDG4DGN&6GK
.$$&6$GK$$r    