# Path: prompt_tuning/data/metrics.py
# def metric_with_examples(metric_fn,
#                          targets,
#                          predictions,
#                          prediction_field: str = constants.PREDICTION):
# def format_classification(indices: Sequence[int],
#                           examples: List[Mapping[str, Any]],
#                           input_field: str = constants.INPUT_TEXT,
#                           target_field: str = constants.TARGET_TEXT,
#                           prediction_field: str = constants.PREDICTION_TEXT,
#                           **kwargs) -> str:
# def format_qa(indices: Sequence[int],
#               examples: List[Mapping[str, Any]],
#               context_field: str = constants.CONTEXT_TEXT,
#               question_field: str = constants.QUESTION_TEXT,
#               answers_field: str = constants.ANSWERS_TEXT,
#               prediction_field: str = constants.PREDICTION_TEXT,
#               **kwargs) -> str:
# def label_set_stats(
#     targets,
#     predictions,
#     task_name: str,
#     target_field: str = constants.TARGET_TEXT,
#     prediction_field: str = constants.PREDICTION_TEXT,
#     display_sets: bool = False,
# ) -> Dict[str, seqio.metrics.Text]:
# def safe_sample(num_examples: int,
#                 population: List[Mapping[str, Any]],
#                 seed: Optional[int] = None) -> Sequence[int]:
# def text_examples(
#     targets,  # pylint: disable=unused-argument
#     predictions,
#     task_name: str,
#     num_examples: int = 8,
#     seed: Optional[int] = None,
#     find_negative: bool = True,
#     format_fn=format_classification,
#     input_field: str = constants.INPUT_TEXT,
#     target_field: str = constants.TARGET_TEXT,
#     prediction_field: str = constants.PREDICTION,
#     prediction_text_field: str = constants.PREDICTION_TEXT,
#     context_field: str = constants.CONTEXT_TEXT,
#     question_field: str = constants.QUESTION_TEXT,
#     answers_field: str = constants.ANSWERS_TEXT,
# ) -> Dict[str, seqio.metrics.MetricValue]:


import ast
import unittest.mock as mock
from absl.testing import absltest
from prompt_tuning.data import metrics
# See the License for the specific language governing permissions and
# limitations under the License.

"""Tests for metrics."""



class MetricsTest(absltest.TestCase):

  def test_metric_with_examples(self):
    gold = "my_gold_value"
    metric_fn = mock.MagicMock(return_value=gold)
    targets = list(range(0, 100))
    predictions = list(range(100, 0, -1))
    prediction_field = "my_prediction_field"
    prediction_wrappers = [{prediction_field: p} for p in predictions]

    metric = metrics.metric_with_examples(metric_fn, targets,
                                          prediction_wrappers, prediction_field)
    self.assertEqual(metric, gold)
    metric_fn.assert_called_once_with(targets, predictions)

  @mock.patch("prompt_tuning.data.metrics.random.seed", autospec=True)
  def test_safe_sample_seeded(self, seed_mock):
    seed = 100
    metrics.safe_sample(12, [], seed=seed)
    seed_mock.assert_called_once_with(seed)

  def test_safe_sample_full(self):
    pop = [1, 2, 3, 4, 5, 10]

# Path: prompt_tuning/data/metrics.py
# def metric_with_examples(metric_fn,
#                          targets,
#                          predictions,
#                          prediction_field: str = constants.PREDICTION):
# def format_classification(indices: Sequence[int],
#                           examples: List[Mapping[str, Any]],
#                           input_field: str = constants.INPUT_TEXT,
#                           target_field: str = constants.TARGET_TEXT,
#                           prediction_field: str = constants.PREDICTION_TEXT,
#                           **kwargs) -> str:
# def format_qa(indices: Sequence[int],
#               examples: List[Mapping[str, Any]],
#               context_field: str = constants.CONTEXT_TEXT,
#               question_field: str = constants.QUESTION_TEXT,
#               answers_field: str = constants.ANSWERS_TEXT,
#               prediction_field: str = constants.PREDICTION_TEXT,
#               **kwargs) -> str:
# def label_set_stats(
#     targets,
#     predictions,
#     task_name: str,
#     target_field: str = constants.TARGET_TEXT,
#     prediction_field: str = constants.PREDICTION_TEXT,
#     display_sets: bool = False,
# ) -> Dict[str, seqio.metrics.Text]:
# def safe_sample(num_examples: int,
#                 population: List[Mapping[str, Any]],
#                 seed: Optional[int] = None) -> Sequence[int]:
# def text_examples(
#     targets,  # pylint: disable=unused-argument
#     predictions,
#     task_name: str,
#     num_examples: int = 8,
#     seed: Optional[int] = None,
#     find_negative: bool = True,
#     format_fn=format_classification,
#     input_field: str = constants.INPUT_TEXT,
#     target_field: str = constants.TARGET_TEXT,
#     prediction_field: str = constants.PREDICTION,
#     prediction_text_field: str = constants.PREDICTION_TEXT,
#     context_field: str = constants.CONTEXT_TEXT,
#     question_field: str = constants.QUESTION_TEXT,
#     answers_field: str = constants.ANSWERS_TEXT,
# ) -> Dict[str, seqio.metrics.MetricValue]:


# Path: prompt_tuning/data/metrics_test.py
import ast
import unittest.mock as mock
from absl.testing import absltest
from prompt_tuning.data import metrics

# See the License for the specific language governing permissions and
# limitations under the License.

"""Tests for metrics."""



class MetricsTest(absltest.TestCase):

  def test_metric_with_examples(self):
    gold = "my_gold_value"
    metric_fn = mock.MagicMock(return_value=gold)
    targets = list(range(0, 100))
    predictions = list(range(100, 0, -1))
    prediction_field = "my_prediction_field"
    prediction_wrappers = [{prediction_field: p} for p in predictions]

    metric = metrics.metric_with_examples(metric_fn, targets,
                                          prediction_wrappers, prediction_field)
    self.assertEqual(metric, gold)
    metric_fn.assert_called_once_with(targets, predictions)

  @mock.patch("prompt_tuning.data.metrics.random.seed", autospec=True)
  def test_safe_sample_seeded(self, seed_mock):
    seed = 100
    metrics.safe_sample(12, [], seed=seed)
    seed_mock.assert_called_once_with(seed)

  def test_safe_sample_full(self):
    pop = [1, 2, 3, 4, 5, 10]

    result = metrics.safe_sample(-1, pop, None)