code: def test_span_metrics_are_computed_correctly(self):
      gold_labels = [
          "O",
          "B-ARG1",
          "I-ARG1",
          "O",
          "B-ARG2",
          "I-ARG2",
          "O",
          "O",
          "O",
      ]
      gold_indices = [
          self.vocab.get_token_index(x, "tags") for x in gold_labels
      ]
  
      gold_tensor = torch.Tensor([gold_indices])
  
      prediction_tensor = torch.rand(
          [2, 9, self.vocab.get_vocab_size("tags")]
      )
  
      # Test that the span measure ignores completely masked sequences by
      # passing a mask with a fully masked row.
      mask = torch.LongTensor(
          [[1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0]]
      )
  
      prediction_tensor[:, 0, 0] = 1
      prediction_tensor[:, 1, 1] = 1  # (True positive - ARG1
      prediction_tensor[:, 2, 2] = 1  # *)
      prediction_tensor[:, 3, 0] = 1
      prediction_tensor[:, 4, 0] = 1  # (False Negative - ARG2
      prediction_tensor[:, 5, 0] = 1  # *)
      prediction_tensor[:, 6, 0] = 1
      prediction_tensor[:, 7, 1] = 1  # (False Positive - ARG1
      prediction_tensor[:, 8, 2] = 1  # *)
  
      metric = SpanBasedF1Measure(self.vocab, "tags")
      metric(prediction_tensor, gold_tensor, mask)
  
      assert metric._true_positives["ARG1"] == 1
      assert metric._true_positives["ARG2"] == 0
      assert "O" not in list(metric._true_positives.keys())
      assert metric._false_negatives["ARG1"] == 0
      assert metric._false_negatives["ARG2"] == 1
      assert "O" not in list(metric._false_negatives.keys())
      assert metric._false_positives["ARG1"] == 1
      assert metric._false_positives["ARG2"] == 0
      assert "O" not in list(metric._false_positives.keys())
  
      # Check things are accumulating correctly.
      metric(prediction_tensor, gold_tensor, mask)
      assert metric._true_positives["ARG1"] == 2
      assert metric._true_positives["ARG2"] == 0
      assert "O" not in list(metric._true_positives.keys())
      assert metric._false_negatives["ARG1"] == 0
      assert metric._false_negatives["ARG2"] == 2
      assert "O" not in list(metric._false_negatives.keys())
      assert metric._false_positives["ARG1"] == 2
      assert metric._false_positives["ARG2"] == 0
      assert "O" not in list(metric._false_positives.keys())
  
      metric_dict = metric.get_metric()
  
      numpy.testing.assert_almost_equal(metric_dict["recall-ARG2"], 0.0)
      numpy.testing.assert_almost_equal(
          metric_dict["precision-ARG2"], 0.0
      )
      numpy.testing.assert_almost_equal(
          metric_dict["f1-measure-ARG2"], 0.0
      )
      numpy.testing.assert_almost_equal(metric_dict["recall-ARG1"], 1.0)
      numpy.testing.assert_almost_equal(
          metric_dict["precision-ARG1"], 0.5
      )
      numpy.testing.assert_almost_equal(
          metric_dict["f1-measure-ARG1"], 0.666666666
      )
      numpy.testing.assert_almost_equal(
          metric_dict["recall-overall"], 0.5
      )
      numpy.testing.assert_almost_equal(
          metric_dict["precision-overall"], 0.5
      )
      numpy.testing.assert_almost_equal(
          metric_dict["f1-measure-overall"], 0.5
      )
  
file_path: magnitude/pymagnitude/third_party/allennlp/tests/training/metrics/span_based_f1_measure_test.py
imports: from __future__ import division
  from __future__ import with_statement
  from __future__ import absolute_import
  import os
  import subprocess
  import torch
  import numpy
  from allennlp.common.testing import AllenNlpTestCase
  from allennlp.data import Vocabulary
  from allennlp.training.metrics import SpanBasedF1Measure, Metric
  from allennlp.models.semantic_role_labeler import write_to_conll_eval_file
  from allennlp.common.params import Params
  from io import open
label_desc: Use image node_id 3 for calling the SpanBasedF1Test obj's underlying member method code with example usage: obj.test_span_metrics_are_computed_correctly() without return types