class_docstr: Check the performance of the model on different segments of the data.
class_docstr_tok: ["Check","the","performance","of","the","model","on","different","segments","of","the","data","."]
docstr: Run check.
docstr_tok: ["Run","check","."]
code: def run_logic(self, context: Context, dataset_kind) -> CheckResult:
      """Run check."""
      context.raise_if_token_classification_task(self)
  
      text_data = context.get_data_by_kind(dataset_kind)
      text_data = text_data.sample(
          self.n_samples,
          random_state=context.random_state,
          drop_na_label=True,
      )
  
      if text_data.n_samples < MIN_TEXT_SAMPLES:
          raise NotEnoughSamplesError(
              f"Not enough samples to find weak {self.segment_by} segments."
              f" Minimum {MIN_TEXT_SAMPLES} samples required."
          )
      features, cat_features = get_relevant_data_table(
          text_data,
          data_type=self.segment_by,
          columns=self.columns,
          ignore_columns=self.ignore_columns,
          n_top_features=self.n_top_features,
      )
  
      # Decide which scorer and score_per_sample to use in the algorithm run
      is_multilabel = text_data.is_multi_label_classification()
      if is_multilabel:
          if self.alternative_scorer is None:
              self.alternative_scorer = {"F1 Macro": "f1_macro"}
  
          # TODO: make weak segments work with multilabel directly without reducing to single dimension
          # For multilabel, we reduce the label to a single dimension using TruncatedSVD, which is better in handling
          # dimensionality reduction of sparse matrices
          label = (
              TruncatedSVD(1).fit_transform(text_data.label).squeeze()
          )
          original_label = [list(x) for x in text_data.label]
      else:
          label = text_data.label
          original_label = text_data.label
      encoded_dataset = (
          self._target_encode_categorical_features_fill_na(
              features,
              label,
              cat_features,
              is_cat_label=not is_multilabel,
          )
      )
  
      if self.score_per_sample is not None:
          score_per_sample = self.score_per_sample[list(features.index)]
          scorer, dummy_model = None, None
          avg_score = round(score_per_sample.mean(), 3)
      else:
          predictions = context.model.predict(text_data)
          if not hasattr(context.model, "predict_proba"):
              raise DeepchecksNotSupportedError(
                  "Predicted probabilities not supplied. The weak segment checks relies"
                  " on cross entropy error that requires predicted probabilities, "
                  "rather than only predicted classes."
              )
          y_proba = context.model.predict_proba(text_data)
          score_per_sample = calculate_neg_cross_entropy_per_sample(
              text_data.label,
              np.asarray(y_proba),
              is_multilabel=is_multilabel,
              model_classes=context.model_classes,
          )
          dummy_model = _DummyModel(
              test=encoded_dataset,
              y_pred_test=predictions,
              y_proba_test=y_proba,
              validate_data_on_predict=False,
          )
          scorer = context.get_single_scorer(self.alternative_scorer)
          avg_score = round(
              scorer.run_on_data_and_label(
                  dummy_model,
                  encoded_dataset.features_columns,
                  original_label,
              ),
              3,
          )
  
      # Running the logic
      weak_segments = self._weak_segments_search(
          data=encoded_dataset.data,
          score_per_sample=score_per_sample,
          label_col=pd.Series(
              original_label, index=score_per_sample.index
          ),
          feature_rank_for_search=np.asarray(encoded_dataset.features),
          dummy_model=dummy_model,
          scorer=scorer,
          multiple_segments_per_feature=self.multiple_segments_per_feature,
      )
  
      if len(weak_segments) == 0:
          display_msg = (
              "WeakSegmentsPerformance was unable to train an error model to find weak segments."
              f"Try supplying additional {self.segment_by}."
          )
          return CheckResult(
              value={"message": display_msg}, display=[display_msg]
          )
  
      if context.with_display:
          display = self._create_heatmap_display(
              data=encoded_dataset.data,
              weak_segments=weak_segments,
              score_per_sample=score_per_sample,
              avg_score=avg_score,
              label_col=original_label,
              dummy_model=dummy_model,
              scorer=scorer,
          )
      else:
          display = []
  
      check_result_value = self._generate_check_result_value(
          weak_segments, cat_features, avg_score
      )
      display_msg = (
          f"Showcasing intersections of {self.segment_by} with weakest detected segments.<br> The full "
          "list of weak segments can be observed in the check result value. "
      )
      return CheckResult(
          value=check_result_value,
          display=[display_msg, DisplayMap(display)],
      )
  
imports: from typing import Callable, Dict, List, Optional, Union
  import numpy
  import pandas
  from sklearn.decomposition import TruncatedSVD
  from deepchecks.core import CheckResult
  from deepchecks.core.check_result import DisplayMap
  from deepchecks.core.errors import DeepchecksNotSupportedError, NotEnoughSamplesError
  from deepchecks.nlp import Context, SingleDatasetCheck
  from deepchecks.nlp.utils.weak_segments import get_relevant_data_table
  from deepchecks.tabular.context import _DummyModel
  from deepchecks.utils.abstracts.weak_segment_abstract import WeakSegmentAbstract
  from deepchecks.utils.single_sample_metrics import calculate_neg_cross_entropy_per_sample
  from deepchecks.utils.typing import Hashable
label_desc: Use image node_id 2 for calling the WeakSegmentsAbstractText obj's underlying member method code with example usage: obj.run_logic(context, dataset_kind) and returns: CheckResult, CheckResult