code: def test_detect_anomalies(monkeypatch, add_explainability):
      # np.int64 is required for running tests on Windows
      df_train = pd.DataFrame(
          {"A": np.arange(4), "B": np.arange(4)}
      ).astype(np.int64)
      df_test = pd.DataFrame(
          {"A": np.arange(5), "B": np.arange(5)}
      ).astype(np.int64)
  
      train_data_scores = pd.Series(
          [0.13, 0.01, 0.08, 0.76], name="score"
      )
      test_data_scores = pd.Series(
          [0.60, 0.20, 0.91, 0.60, 0.23], name="score"
      )
      assert len(df_train) == len(train_data_scores)
      assert len(df_test) == len(test_data_scores)
  
      call_md_render = MagicMock()
      call_df_render = MagicMock()
      call_anomaly_viz_render = MagicMock()
  
      def verify_explain_fn(anomaly_idx):
          return {"called_vals": list(anomaly_idx)}
  
      def call_anomaly_anlz_fit_mock(
          state: AnalysisState, args: AnalysisState, **fit_kwargs
      ):
          assert state == {"problem_type": "regression"}
          assert args.train_data.equals(df_train)
          assert args.test_data.equals(df_test)
          assert args.label == "B"
          assert args.feature_generator is True
  
          state.anomaly_detection = {
              "scores": {
                  "train_data": train_data_scores,
                  "test_data": test_data_scores,
              },
              "explain_rows_fns": {
                  "train_data": verify_explain_fn,
                  "test_data": verify_explain_fn,
              },
          }
  
      call_anomaly_anlz_fit = MagicMock(
          side_effect=call_anomaly_anlz_fit_mock
      )
      call_explain_rows = MagicMock()
  
      with monkeypatch.context() as m:
          m.setattr(
              AnomalyDetectorAnalysis, "_fit", call_anomaly_anlz_fit
          )
          m.setattr(
              MarkdownSectionComponent,
              "render_markdown",
              call_md_render,
          )
          m.setattr(
              PropertyRendererComponent, "display_obj", call_df_render
          )
          m.setattr(
              AnomalyScoresVisualization,
              "render",
              call_anomaly_viz_render,
          )
          m.setattr(
              "autogluon.eda.auto.simple.explain_rows",
              call_explain_rows,
          )
          state = detect_anomalies(
              train_data=df_train,
              test_data=df_test,
              label="B",
              threshold_stds=2,
              explain_top_n_anomalies=1 if add_explainability else None,
              show_help_text=False,
              return_state=True,
          )
  
      assert list(state.anomaly_detection.scores.keys()) == [
          "train_data",
          "test_data",
      ]
      assert state.anomaly_detection.scores.train_data.equals(
          train_data_scores
      )
      assert state.anomaly_detection.scores.test_data.equals(
          test_data_scores
      )
      state.anomaly_detection.pop("scores")
  
      assert list(state.anomaly_detection.anomalies.keys()) == [
          "train_data",
          "test_data",
      ]
      # np.int64 is required for running tests on Windows
      assert state.anomaly_detection.anomalies.train_data.equals(
          pd.DataFrame(
              {"A": [3], "B": [3], "score": 0.76}, index=[3]
          ).astype({"A": np.int64, "B": np.int64})
      )
      assert state.anomaly_detection.anomalies.test_data.equals(
          pd.DataFrame(
              {"A": [2], "B": [2], "score": 0.91}, index=[2]
          ).astype({"A": np.int64, "B": np.int64})
      )
      state.anomaly_detection.pop("anomalies")
  
      assert state == {
          "problem_type": "regression",
          "anomaly_detection": {
              "anomaly_score_threshold": 0.693685807840985,
              "explain_rows_fns": {
                  "train_data": verify_explain_fn,
                  "test_data": verify_explain_fn,
              },
          },
      }
  
      # Markdown rendering
      calls = [c[0][0] for c in call_md_render.call_args_list]
      expected_calls = [
          "### Anomaly Detection Report",
          # train_data
          "**Top-10 `train_data` anomalies (total: 1)**",
          # test_data
          "**Top-10 `test_data` anomalies (total: 1)**",
      ]
      if add_explainability:
          expected_calls.insert(
              2,
              "⚠️ Please note that the feature values shown on the charts below are transformed "
              "into an internal representation; they may be encoded or modified based on internal preprocessing. "
              "Refer to the original datasets for the actual feature values.",
          )
          expected_calls.insert(
              3,
              "⚠️ The detector has seen this dataset; the may result in overly optimistic estimates. "
              "Although the anomaly score in the explanation might not match, the magnitude of the feature scores "
              "can still be utilized to evaluate the impact of the feature on the anomaly score.",
          )
          expected_calls.append(
              "⚠️ Please note that the feature values shown on the charts below are transformed "
              "into an internal representation; they may be encoded or modified based on internal preprocessing. "
              "Refer to the original datasets for the actual feature values."
          )
      assert calls == expected_calls
  
      # Tables rendering
      calls = [c[0][0].to_dict() for c in call_df_render.call_args_list]
      assert calls == [
          {"A": {3: 3}, "B": {3: 3}, "score": {3: 0.76}},
          {"A": {2: 2}, "B": {2: 2}, "score": {2: 0.91}},
      ]
  
      # Explainability data
      if add_explainability:
          calls = [c.kwargs for c in call_explain_rows.call_args_list]
          assert calls == [
              {"called_vals": [3], "plot": "waterfall"},
              {"called_vals": [2], "plot": "waterfall"},
          ]
      else:
          call_explain_rows.assert_not_called()
  
imports: import os
  import re
  import tempfile
  from sys import platform
  from unittest.mock import ANY, MagicMock, call
  import numpy
  import pandas
  import pytest
  import autogluon.eda
  from autogluon.eda import AnalysisState
  from autogluon.eda.analysis import AnomalyDetectorAnalysis, Namespace, ShapAnalysis
  from autogluon.eda.analysis.base import BaseAnalysis
  from autogluon.eda.auto import analyze, analyze_interaction, covariate_shift_detection, dataset_overview, detect_anomalies, explain_rows, partial_dependence_plots, quick_fit, target_analysis
  from autogluon.eda.auto.simple import _is_single_numeric_variable, _prepare_pdp_data, _validate_and_normalize_pdp_args, get_default_estimator_if_not_specified, get_empty_dict_if_none
  from autogluon.eda.visualization import AnomalyScoresVisualization, ConfusionMatrix, CorrelationVisualization, DatasetStatistics, DatasetTypeMismatch, ExplainForcePlot, ExplainWaterfallPlot, FeatureImportance, FeatureInteractionVisualization, LabelInsightsVisualization, MarkdownSectionComponent, ModelLeaderboard, PropertyRendererComponent, RegressionEvaluation, XShiftSummary
  from autogluon.eda.visualization.base import AbstractVisualization
  from autogluon.eda.visualization.interaction import FeatureDistanceAnalysisVisualization, PDPInteractions
label_desc: Use image node_id 25 for calling a global function with example usage: test_detect_anomalies(monkeypatch, add_explainability) and returns: dict