code: def compare_frames(
      predicted_frame: Node,
      expected_frame: Node,
      tree_based: bool,
      intent_per_label_confusions: Optional[PerLabelConfusions] = None,
      slot_per_label_confusions: Optional[PerLabelConfusions] = None,
  ) -> IntentSlotConfusions:
      """
      Compares two intent frames and returns TP, FP, FN counts for intents and slots.
      Optionally collects the per label TP, FP, FN counts.
  
      Args:
          predicted_frame: Predicted intent frame.
          expected_frame: Gold intent frame.
          tree_based: Whether to get the tree-based confusions (if True) or bracket-based
              confusions (if False). For details, see the function
              `compute_intent_slot_metrics()`.
          intent_per_label_confusions: If provided, update the per label confusions for
              intents as well. Defaults to None.
          slot_per_label_confusions: If provided, update the per label confusions for
              slots as well. Defaults to None.
  
      Returns:
          IntentSlotConfusions, containing confusion counts for intents and slots.
      """
      predicted_intents_and_slots = _get_intents_and_slots(
          predicted_frame, tree_based=tree_based
      )
      expected_intents_and_slots = _get_intents_and_slots(
          expected_frame, tree_based=tree_based
      )
      return IntentSlotConfusions(
          intent_confusions=_compare_nodes(
              predicted_intents_and_slots.intents,
              expected_intents_and_slots.intents,
              intent_per_label_confusions,
          ),
          slot_confusions=_compare_nodes(
              predicted_intents_and_slots.slots,
              expected_intents_and_slots.slots,
              slot_per_label_confusions,
          ),
      )
  
imports: from collections import Counter, defaultdict
  from copy import deepcopy
  from typing import AbstractSet, Any, Callable, Counter, Dict, List, NamedTuple, Optional, Sequence, Tuple
  from pytext.data.data_structures.annotation import INVALID_TREE_INTENT
  from pytext.data.data_structures.node import Node, Span
  from .None import AllConfusions, Confusions, PerLabelConfusions, PRF1Metrics, PRF1Scores, safe_division
label_desc: Use image node_id 3 for calling a global function with example usage: compare_frames(predicted_frame, expected_frame, tree_based, intent_per_label_confusions, slot_per_label_confusions) and returns: IntentSlotConfusions