code: def _get_percentile_intermediate_result_over_trials(
      completed_trials: List["optuna.trial.FrozenTrial"],
      direction: StudyDirection,
      step: int,
      percentile: float,
      n_min_trials: int,
  ) -> float:
      if len(completed_trials) == 0:
          raise ValueError("No trials have been completed.")
  
      intermediate_values = [
          t.intermediate_values[step]
          for t in completed_trials
          if step in t.intermediate_values
      ]
  
      if len(intermediate_values) < n_min_trials:
          return math.nan
  
      if direction == StudyDirection.MAXIMIZE:
          percentile = 100 - percentile
  
      return float(
          np.nanpercentile(
              np.array(intermediate_values, dtype=float),
              percentile,
          )
      )
  
file_path: optuna/optuna/pruners/_percentile.py
imports: import functools
  import math
  from typing import KeysView
  from typing import List
  import numpy
  import optuna
  from optuna.pruners import BasePruner
  from optuna.study._study_direction import StudyDirection
  from optuna.trial._state import TrialState
label_desc: Use image node_id 2 for calling a global function with example usage: _get_percentile_intermediate_result_over_trials(completed_trials, direction, step, percentile, n_min_trials) and returns: float, math