
    -ji                        S SK Jr  S SKJr  S SKrS SKrS SKrS SKrS SK	J
r
  S SKJr  S SKJr        SS jr            SS jr          SS	 jr " S
 S\
5      rg)    )annotations)KeysViewN)
BasePruner)StudyDirection)
TrialStatec                    [         R                  " [        U R                  R	                  5       5      [
        S9nU[        R                  :X  a  [         R                  " U5      $ [         R                  " U5      $ )Ndtype)
npasarraylistintermediate_valuesvaluesfloatr   MAXIMIZEnanmaxnanmin)trial	directionr   s      T/home/james-whalen/.local/lib/python3.13/site-packages/optuna/pruners/_percentile.py(_get_best_intermediate_result_over_stepsr      sT     ZZU66==?@NFN+++yy  99V    c           	     ~   [        U 5      S:X  a  [        S5      eU  Vs/ s H#  oRUR                  ;   d  M  UR                  U   PM%     nn[        U5      U:  a  [        R                  $ U[
        R                  :X  a  SU-
  n[        [        R                  " [        R                  " U[        S9U5      5      $ s  snf )Nr   zNo trials have been completed.d   r	   )len
ValueErrorr   mathnanr   r   r   r   nanpercentilearray)completed_trialsr   step
percentilen_min_trialstr   s          r   /_get_percentile_intermediate_result_over_trialsr&      s     !9:: .>-=I^I^A^#d#-=   ,.xxN+++:%

HH(6	
 s
   B:B:c                p   ^  T U-
  U-  U-  U-   nUS:  d   e[         R                  " U 4S jUS5      nXT:  $ )Nr   c                "   > X:  a  UT:w  a  U$ U $ )N )second_last_stepsr"   s     r   <lambda>,_is_first_in_interval_step.<locals>.<lambda>>   s    )=!t)A$aQa$ar   )	functoolsreduce)r"   intermediate_stepsn_warmup_stepsinterval_stepsnearest_lower_pruning_stepr*   s   `     r   _is_first_in_interval_stepr5   4   s_     	~	"(")+9": &*** !''a
 88r   c                  T    \ rS rSrSr   S	SS.           S
S jjjrSS jrSrg)PercentilePrunerF   a	  Pruner to keep the specified percentile of the trials.

Prune if the best intermediate value is in the bottom percentile among trials at the same step.

Example:

    .. testcode::

        import numpy as np
        from sklearn.datasets import load_iris
        from sklearn.linear_model import SGDClassifier
        from sklearn.model_selection import train_test_split

        import optuna

        X, y = load_iris(return_X_y=True)
        X_train, X_valid, y_train, y_valid = train_test_split(X, y)
        classes = np.unique(y)


        def objective(trial):
            alpha = trial.suggest_float("alpha", 0.0, 1.0)
            clf = SGDClassifier(alpha=alpha)
            n_train_iter = 100

            for step in range(n_train_iter):
                clf.partial_fit(X_train, y_train, classes=classes)

                intermediate_value = clf.score(X_valid, y_valid)
                trial.report(intermediate_value, step)

                if trial.should_prune():
                    raise optuna.TrialPruned()

            return clf.score(X_valid, y_valid)


        study = optuna.create_study(
            direction="maximize",
            pruner=optuna.pruners.PercentilePruner(
                25.0, n_startup_trials=5, n_warmup_steps=30, interval_steps=10
            ),
        )
        study.optimize(objective, n_trials=20)

Args:
    percentile:
        Percentile which must be between 0 and 100 inclusive
        (e.g., When given 25.0, top of 25th percentile trials are kept).
    n_startup_trials:
        Pruning is disabled until the given number of trials finish in the same study.
    n_warmup_steps:
        Pruning is disabled until the trial exceeds the given number of step. Note that
        this feature assumes that ``step`` starts at zero.
    interval_steps:
        Interval in number of steps between the pruning checks, offset by the warmup steps.
        If no value has been reported at the time of a pruning check, that particular check
        will be postponed until a value is reported. Value must be at least 1.
    n_min_trials:
        Minimum number of reported trial results at a step to judge whether to prune.
        If the number of reported intermediate values from all trials at the current step
        is less than ``n_min_trials``, the trial will not be pruned. This can be used to ensure
        that a minimum number of trials are run to completion without being pruned.
   )r$   c               .   SUs=::  a  S::  d  O  [        SU< S35      eUS:  a  [        SU< S35      eUS:  a  [        SU< S35      eUS:  a  [        S	U< S35      eUS:  a  [        S
U< S35      eXl        X l        X0l        X@l        XPl        g )Ng        r   zCPercentile must be between 0 and 100 inclusive, but got percentile=.r   zFNumber of startup trials cannot be negative, but got n_startup_trials=zBNumber of warmup steps cannot be negative, but got n_warmup_steps=r9   zBPruning interval steps must be at least 1, but got interval_steps=zFNumber of trials for pruning must be at least 1, but got n_min_trials=)r   _percentile_n_startup_trials_n_warmup_steps_interval_steps_n_min_trials)selfr#   n_startup_trialsr2   r3   r$   s         r   __init__PercentilePruner.__init__   s     j'C'V:-WXY  aYHXGZZ[\  AUnEVVWX  AUnEVVWX  !YL?Z[\  &!1--)r   c                H   UR                  S[        R                  4S9n[        U5      nUS:X  a  gX@R                  :  a  gUR
                  nUc  gU R                  nXV:  a  g[        XRR                  R                  5       X`R                  5      (       d  gUR                  n[        X'5      n[        R                  " U5      (       a  g[        X7XPR                   U R"                  5      n	[        R                  " U	5      (       a  gU[$        R&                  :X  a  X:  $ X:  $ )NF)deepcopystatesr   T)
get_trialsr   COMPLETEr   r=   	last_stepr>   r5   r   keysr?   r   r   r   isnanr&   r<   r@   r   r   )
rA   studyr   r!   n_trialsr"   r2   r   best_intermediate_resultps
             r   prunePercentilePruner.prune   s    ++UJDWDWCY+Z'(q=,,,<-- )++002NDXDX
 
 OO	#KE#] ::.//;/?/?ASAS
 ::a==///+//'++r   )r?   r@   r=   r>   r<   N)   r   r9   )r#   r   rB   intr2   rT   r3   rT   r$   rT   returnNone)rM   z'optuna.study.Study'r   'optuna.trial.FrozenTrial'rU   bool)__name__
__module____qualname____firstlineno____doc__rC   rQ   __static_attributes__r)   r   r   r7   r7   F   sb    ?H !""* "*"* "* 	"*
 "* "* 
"*H$,r   r7   )r   rW   r   r   rU   r   )r!   z list['optuna.trial.FrozenTrial']r   r   r"   rT   r#   r   r$   rT   rU   r   )
r"   rT   r1   zKeysView[int]r2   rT   r3   rT   rU   rX   )
__future__r   collections.abcr   r/   r   numpyr   optunaoptuna.prunersr   optuna.study._study_directionr   optuna.trial._stater   r   r&   r5   r7   r)   r   r   <module>rf      s    " $     % 8 *%2@
6  	
  89
9#09BE9WZ9	9$J,z J,r   