code: def calculate_condition_logic(
      result,
      include_classes=None,
      average=False,
      max_gain=None,
      min_allowed_gain=None,
  ) -> ConditionResult:
      scores = result.loc[result["Model"] == "Given Model"]
      perfect_scores = result.loc[result["Model"] == "Perfect Model"]
      simple_scores = result.loc[result["Model"] == "Simple Model"]
      metrics = scores["Metric"].unique()
  
      # Save min gain info to print when condition pass
      min_gain = (np.inf, "")
  
      def update_min_gain(gain, metric, class_name=None):
          nonlocal min_gain
          if gain < min_gain[0]:
              message = f"Found minimal gain of {format_percent(gain)} for metric {metric}"
              if class_name:
                  message += f" and class {class_name}"
              min_gain = gain, message
  
      fails = {}
      if not average:
          for metric in metrics:
              failed_classes = {}
              for _, scores_row in scores.loc[
                  scores["Metric"] == metric
              ].iterrows():
                  curr_class = scores_row["Class"]
                  curr_class_name = scores_row["Class Name"]
                  curr_value = scores_row["Value"]
                  if (
                      include_classes
                      and curr_class not in include_classes
                  ):
                      continue
                  perfect = perfect_scores.loc[
                      (perfect_scores["Metric"] == metric)
                      & (perfect_scores["Class"] == curr_class)
                  ]["Value"].values[0]
                  if curr_value == perfect:
                      continue
  
                  simple_score_value = simple_scores.loc[
                      (simple_scores["Class"] == curr_class)
                      & (simple_scores["Metric"] == metric)
                  ]["Value"].values[0]
                  gain = get_gain(
                      simple_score_value, curr_value, perfect, max_gain
                  )
                  update_min_gain(gain, metric, curr_class_name)
                  if gain <= min_allowed_gain:
                      failed_classes[curr_class_name] = format_percent(
                          gain
                      )
  
              if failed_classes:
                  fails[metric] = failed_classes
      else:
          scores = average_scores(
              scores, simple_scores, include_classes
          )
          for metric, models_scores in scores.items():
              metric_perfect_score = perfect_scores.loc[
                  (perfect_scores["Metric"] == metric)
              ]["Value"].values[0]
              # If origin model is perfect, skip the gain calculation
              if models_scores["Origin"] == metric_perfect_score:
                  continue
              gain = get_gain(
                  models_scores["Simple"],
                  models_scores["Origin"],
                  metric_perfect_score,
                  max_gain,
              )
              update_min_gain(gain, metric)
              if gain <= min_allowed_gain:
                  fails[metric] = format_percent(gain)
  
      if fails:
          msg = f"Found metrics with gain below threshold: {fails}"
          return ConditionResult(ConditionCategory.FAIL, msg)
      else:
          return ConditionResult(ConditionCategory.PASS, min_gain[1])
  
imports: from typing import Any, Callable, Dict, Hashable, List, Optional, Union
  import numpy
  import pandas
  import plotly.express
  from deepchecks.core import CheckResult, ConditionCategory, ConditionResult, DatasetKind
  from deepchecks.core.errors import DeepchecksValueError
  from deepchecks.utils import plot
  from deepchecks.utils.metrics import get_gain
  from deepchecks.utils.strings import format_percent
  from deepchecks.vision._shared_docs import docstrings
  from deepchecks.vision.base_checks import TrainTestCheck
  from deepchecks.vision.context import Context
  from deepchecks.vision.metrics_utils import CustomClassificationScorer, get_scorers_dict, metric_results_to_df
  from deepchecks.vision.metrics_utils.scorers import filter_classes_for_display
  from deepchecks.vision.vision_data import TaskType, VisionData
  from deepchecks.vision.vision_data.batch_wrapper import BatchWrapper
label_desc: Use image node_id 1 for calling a global function with example usage: calculate_condition_logic(result, include_classes, average, max_gain, min_allowed_gain) and returns: ConditionResult, ConditionResult