code: def _filter_nonfinite(
      trials: list[FrozenTrial],
      target: Callable[[FrozenTrial], float] | None = None,
      with_message: bool = True,
  ) -> list[FrozenTrial]:
      # For multi-objective optimization target must be specified to select
      # one of objective values to filter trials by (and plot by later on).
      # This function is not raising when target is missing, since we're
      # assuming plot args have been sanitized before.
      if target is None:
  
          def _target(t: FrozenTrial) -> float:
              return cast(float, t.value)
  
          target = _target
  
      filtered_trials: list[FrozenTrial] = []
      for trial in trials:
          value = target(trial)
  
          try:
              value = float(value)
          except (
              ValueError,
              TypeError,
          ):
              warnings.warn(
                  f"Trial{trial.number}'s target value {repr(value)} could not be cast to float."
              )
              raise
  
          # Not a Number, positive infinity and negative infinity are considered to be non-finite.
          if not np.isfinite(value):
              if with_message:
                  _logger.warning(
                      f"Trial {trial.number} is omitted in visualization "
                      "because its objective value is inf or nan."
                  )
          else:
              filtered_trials.append(trial)
  
      return filtered_trials
  
imports: from __future__ import annotations
  import json
  from typing import Any
  from typing import Callable
  from typing import cast
  from typing import Sequence
  import warnings
  import numpy
  import optuna
  from optuna.distributions import CategoricalDistribution
  from optuna.distributions import FloatDistribution
  from optuna.distributions import IntDistribution
  from optuna.study import Study
  from optuna.study._study_direction import StudyDirection
  from optuna.trial import FrozenTrial
  from optuna.visualization import _plotly_imports
label_desc: Use image node_id 8 for calling a global function with example usage: _filter_nonfinite(trials, target, with_message) and returns: filtered_trials, cast