from __future__ import annotations

import copy
import inspect
import json
import math
import os
import tempfile
import unittest
from importlib import import_module

import numpy as np

from transformers import ViTMAEConfig
from transformers.file_utils import cached_property, is_tf_available, is_vision_available
from transformers.testing_utils import require_tf, require_vision, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
       import tensorflow as tf

       from transformers import TFViTMAEForPreTraining, TFViTMAEModel


if is_vision_available():
       from PIL import Image

       from transformers import ViTImageProcessor


class 			UpperCAmelCase__    :



  """simple docstring"""






  def __init__(				self:			str				,				__lowerCAmelCase:			List[Any]				,				__lowerCAmelCase:			Optional[Any]=13				,				__lowerCAmelCase:			Tuple=30				,				__lowerCAmelCase:			Tuple=2				,				__lowerCAmelCase:			Any=3				,				__lowerCAmelCase:			List[str]=True				,				__lowerCAmelCase:			Dict=True				,				__lowerCAmelCase:			List[Any]=32				,				__lowerCAmelCase:			Any=2				,				__lowerCAmelCase:			Any=4				,				__lowerCAmelCase:			Union[str, Any]=37				,				__lowerCAmelCase:			str="gelu"				,				__lowerCAmelCase:			Optional[int]=0.1				,				__lowerCAmelCase:			str=0.1				,				__lowerCAmelCase:			Union[str, Any]=10				,				__lowerCAmelCase:			Union[str, Any]=0.02				,				__lowerCAmelCase:			Optional[Any]=3				,				__lowerCAmelCase:			int=0.6				,				__lowerCAmelCase:			Optional[int]=None				,				) -> Dict:




    '''simple docstring'''




    __UpperCAmelCase						    =       parent
    __UpperCAmelCase						    =       batch_size
    __UpperCAmelCase						    =       image_size
    __UpperCAmelCase						    =       patch_size
    __UpperCAmelCase						    =       num_channels
    __UpperCAmelCase						    =       is_training
    __UpperCAmelCase						    =       use_labels
    __UpperCAmelCase						    =       hidden_size
    __UpperCAmelCase						    =       num_hidden_layers
    __UpperCAmelCase						    =       num_attention_heads
    __UpperCAmelCase						    =       intermediate_size
    __UpperCAmelCase						    =       hidden_act
    __UpperCAmelCase						    =       hidden_dropout_prob
    __UpperCAmelCase						    =       attention_probs_dropout_prob
    __UpperCAmelCase						    =       type_sequence_label_size
    __UpperCAmelCase						    =       initializer_range
    __UpperCAmelCase						    =       mask_ratio
    __UpperCAmelCase						    =       scope

    # in ViTMAE, the expected sequence length = (num_patches + 1) * (1 - config.mask_ratio), rounded above
    # (we add 1 for the [CLS] token)
    __UpperCAmelCase						    =       (image_size // patch_size) ** 2
    __UpperCAmelCase						    =       int(math.ceil((1 - mask_ratio) * (num_patches + 1)   )   )






  def 	_UpperCAmelCase				(				self:			Dict   ) -> Optional[int]:




    '''simple docstring'''




    __UpperCAmelCase						    =       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

    __UpperCAmelCase						    =       None
    if self.use_labels:
      __UpperCAmelCase						    =       ids_tensor([self.batch_size]				,				self.type_sequence_label_size   )

    __UpperCAmelCase						    =       self.get_config()

    return config, pixel_values, labels






  def 	_UpperCAmelCase				(				self:			str   ) -> Tuple:




    '''simple docstring'''




    return ViTMAEConfig(
        image_size=self.image_size				,				patch_size=self.patch_size				,				num_channels=self.num_channels				,				hidden_size=self.hidden_size				,				num_hidden_layers=self.num_hidden_layers				,				num_attention_heads=self.num_attention_heads				,				intermediate_size=self.intermediate_size				,				decoder_hidden_size=self.hidden_size				,				decoder_num_hidden_layers=self.num_hidden_layers				,				decoder_num_attention_heads=self.num_attention_heads				,				decoder_intermediate_size=self.intermediate_size				,				hidden_act=self.hidden_act				,				hidden_dropout_prob=self.hidden_dropout_prob				,				attention_probs_dropout_prob=self.attention_probs_dropout_prob				,				is_decoder=__lowerCAmelCase				,				initializer_range=self.initializer_range				,				mask_ratio=self.mask_ratio				,				)






  def 	_UpperCAmelCase				(				self:			Any				,				__lowerCAmelCase:			Union[str, Any]				,				__lowerCAmelCase:			Any				,				__lowerCAmelCase:			str   ) -> Optional[Any]:




    '''simple docstring'''




    __UpperCAmelCase						    =       TFViTMAEModel(config=__lowerCAmelCase   )
    __UpperCAmelCase						    =       model(__lowerCAmelCase				,				training=__lowerCAmelCase   )
    self.parent.assertEqual(result.last_hidden_state.shape				,				(self.batch_size, self.seq_length, self.hidden_size)   )






  def 	_UpperCAmelCase				(				self:			Optional[Any]				,				__lowerCAmelCase:			Any				,				__lowerCAmelCase:			str				,				__lowerCAmelCase:			str   ) -> Union[str, Any]:




    '''simple docstring'''




    __UpperCAmelCase						    =       TFViTMAEForPreTraining(__lowerCAmelCase   )
    __UpperCAmelCase						    =       model(__lowerCAmelCase				,				training=__lowerCAmelCase   )
    # expected sequence length = num_patches
    __UpperCAmelCase						    =       (self.image_size // self.patch_size) ** 2
    __UpperCAmelCase						    =       self.patch_size**2 * self.num_channels
    self.parent.assertEqual(result.logits.shape				,				(self.batch_size, num_patches, expected_num_channels)   )

    # test greyscale images
    __UpperCAmelCase						    =       1
    __UpperCAmelCase						    =       TFViTMAEForPreTraining(__lowerCAmelCase   )

    __UpperCAmelCase						    =       floats_tensor([self.batch_size, 1, self.image_size, self.image_size]   )
    __UpperCAmelCase						    =       model(__lowerCAmelCase				,				training=__lowerCAmelCase   )
    __UpperCAmelCase						    =       self.patch_size**2
    self.parent.assertEqual(result.logits.shape				,				(self.batch_size, num_patches, expected_num_channels)   )






  def 	_UpperCAmelCase				(				self:			Union[str, Any]   ) -> List[Any]:




    '''simple docstring'''




    __UpperCAmelCase						    =       self.prepare_config_and_inputs()
    ((__UpperCAmelCase)    ,		(__UpperCAmelCase)    ,		(__UpperCAmelCase))						    =       config_and_inputs
    __UpperCAmelCase						    =       {"pixel_values": pixel_values}
    return config, inputs_dict






@require_tf
class 			UpperCAmelCase__    (		snake_case		,	snake_case		,	unittest.TestCase					):



  """simple docstring"""






  lowerCAmelCase__		:	Optional[Any]    		= (TFViTMAEModel, TFViTMAEForPreTraining) if is_tf_available() else ()
  lowerCAmelCase__		:	Any    		= {'feature-extraction': TFViTMAEModel} if is_tf_available() else {}

  lowerCAmelCase__		:	Optional[int]    		= False
  lowerCAmelCase__		:	Any    		= False
  lowerCAmelCase__		:	Union[str, Any]    		= False
  lowerCAmelCase__		:	int    		= False






  def 	_UpperCAmelCase				(				self:			Dict   ) -> Dict:




    '''simple docstring'''




    __UpperCAmelCase						    =       TFViTMAEModelTester(self   )
    __UpperCAmelCase						    =       ConfigTester(self				,				config_class=__lowerCAmelCase				,				has_text_modality=__lowerCAmelCase				,				hidden_size=37   )






  def 	_UpperCAmelCase				(				self:			int   ) -> Dict:




    '''simple docstring'''




    self.config_tester.run_common_tests()






  @unittest.skip(reason="ViTMAE does not use inputs_embeds"   )
  def 	_UpperCAmelCase				(				self:			Optional[int]   ) -> int:




    '''simple docstring'''




    pass






  def 	_UpperCAmelCase				(				self:			int   ) -> List[Any]:




    '''simple docstring'''




    __UpperCAmelCase    ,		__UpperCAmelCase						    =       self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
      __UpperCAmelCase						    =       model_class(__lowerCAmelCase   )
      self.assertIsInstance(model.get_input_embeddings()				,				(tf.keras.layers.Layer)   )
      __UpperCAmelCase						    =       model.get_output_embeddings()
      self.assertTrue(x is None or isinstance(__lowerCAmelCase				,				tf.keras.layers.Layer   )   )






  def 	_UpperCAmelCase				(				self:			str   ) -> Optional[int]:




    '''simple docstring'''




    __UpperCAmelCase    ,		__UpperCAmelCase						    =       self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
      __UpperCAmelCase						    =       model_class(__lowerCAmelCase   )
      __UpperCAmelCase						    =       inspect.signature(model.call   )
      # signature.parameters is an OrderedDict => so arg_names order is deterministic
      __UpperCAmelCase						    =       [*signature.parameters.keys()]

      __UpperCAmelCase						    =       ["pixel_values"]
      self.assertListEqual(arg_names[:1]				,				__lowerCAmelCase   )






  def 	_UpperCAmelCase				(				self:			Any   ) -> Any:




    '''simple docstring'''




    __UpperCAmelCase						    =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*__lowerCAmelCase   )






  def 	_UpperCAmelCase				(				self:			int   ) -> Optional[int]:




    '''simple docstring'''




    __UpperCAmelCase						    =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_pretraining(*__lowerCAmelCase   )






  def 	_UpperCAmelCase				(				self:			int   ) -> int:




    '''simple docstring'''




    np.random.seed(2   )

    __UpperCAmelCase    ,		__UpperCAmelCase						    =       self.model_tester.prepare_config_and_inputs_for_common()

    __UpperCAmelCase						    =       int((config.image_size // config.patch_size) ** 2   )
    __UpperCAmelCase						    =       np.random.uniform(size=(self.model_tester.batch_size, num_patches)   )

    for model_class in self.all_model_classes:
      __UpperCAmelCase						    =       model_class(__lowerCAmelCase   )
      __UpperCAmelCase						    =       self._prepare_for_class(__lowerCAmelCase				,				__lowerCAmelCase   )

      __UpperCAmelCase						    =       model(__lowerCAmelCase				,				noise=__lowerCAmelCase   )

      __UpperCAmelCase						    =       copy.deepcopy(self._prepare_for_class(__lowerCAmelCase				,				__lowerCAmelCase   )   )
      __UpperCAmelCase						    =       model(**__lowerCAmelCase				,				noise=__lowerCAmelCase   )
      __UpperCAmelCase						    =       outputs_dict[0].numpy()
      __UpperCAmelCase						    =       outputs_keywords[0].numpy()

      self.assertLess(np.sum(np.abs(output_dict - output_keywords   )   )				,				1E-6   )






  def 	_UpperCAmelCase				(				self:			List[str]   ) -> List[str]:




    '''simple docstring'''




    np.random.seed(2   )

    __UpperCAmelCase    ,		__UpperCAmelCase						    =       self.model_tester.prepare_config_and_inputs_for_common()

    __UpperCAmelCase						    =       int((config.image_size // config.patch_size) ** 2   )
    __UpperCAmelCase						    =       np.random.uniform(size=(self.model_tester.batch_size, num_patches)   )

    def prepare_numpy_arrays(__lowerCAmelCase:			Optional[Any]   ):
      __UpperCAmelCase						    =       {}
      for k, v in inputs_dict.items():
        if tf.is_tensor(__lowerCAmelCase   ):
          __UpperCAmelCase						    =       v.numpy()
        else:
          __UpperCAmelCase						    =       np.array(__lowerCAmelCase   )

      return inputs_np_dict

    for model_class in self.all_model_classes:
      __UpperCAmelCase						    =       model_class(__lowerCAmelCase   )

      __UpperCAmelCase						    =       self._prepare_for_class(__lowerCAmelCase				,				__lowerCAmelCase   )
      __UpperCAmelCase						    =       prepare_numpy_arrays(__lowerCAmelCase   )

      __UpperCAmelCase						    =       model(__lowerCAmelCase				,				noise=__lowerCAmelCase   )
      __UpperCAmelCase						    =       model(**__lowerCAmelCase				,				noise=__lowerCAmelCase   )
      self.assert_outputs_same(__lowerCAmelCase				,				__lowerCAmelCase   )






  def 	_UpperCAmelCase				(				self:			Optional[int]				,				__lowerCAmelCase:			Union[str, Any]				,				__lowerCAmelCase:			str				,				__lowerCAmelCase:			str   ) -> Tuple:




    '''simple docstring'''




    np.random.seed(2   )

    __UpperCAmelCase						    =       int((tf_model.config.image_size // tf_model.config.patch_size) ** 2   )
    __UpperCAmelCase						    =       np.random.uniform(size=(self.model_tester.batch_size, num_patches)   )
    __UpperCAmelCase						    =       tf.constant(__lowerCAmelCase   )

    # Add `noise` argument.
    # PT inputs will be prepared in `super().check_pt_tf_models()` with this added `noise` argument
    __UpperCAmelCase						    =       tf_noise

    super().check_pt_tf_models(__lowerCAmelCase				,				__lowerCAmelCase				,				__lowerCAmelCase   )






  def 	_UpperCAmelCase				(				self:			Optional[Any]   ) -> Any:




    '''simple docstring'''




    np.random.seed(2   )

    __UpperCAmelCase    ,		__UpperCAmelCase						    =       self.model_tester.prepare_config_and_inputs_for_common()

    __UpperCAmelCase						    =       {
        module_member
        for model_class in self.all_model_classes
        for module in (import_module(model_class.__module__   ),)
        for module_member_name in dir(__lowerCAmelCase   )
        if module_member_name.endswith("MainLayer"   )
        # This condition is required, since `modeling_tf_clip.py` has 3 classes whose names end with `MainLayer`.
        and module_member_name[: -len("MainLayer"   )] == model_class.__name__[: -len("Model"   )]
        for module_member in (getattr(__lowerCAmelCase				,				__lowerCAmelCase   ),)
        if isinstance(__lowerCAmelCase				,				__lowerCAmelCase   )
        and tf.keras.layers.Layer in module_member.__bases__
        and getattr(__lowerCAmelCase				,				"_keras_serializable"				,				__lowerCAmelCase   )
    }

    __UpperCAmelCase						    =       int((config.image_size // config.patch_size) ** 2   )
    __UpperCAmelCase						    =       np.random.uniform(size=(self.model_tester.batch_size, num_patches)   )
    __UpperCAmelCase						    =       tf.convert_to_tensor(__lowerCAmelCase   )
    inputs_dict.update({"noise": noise}   )

    for main_layer_class in tf_main_layer_classes:
      __UpperCAmelCase						    =       main_layer_class(__lowerCAmelCase   )

      __UpperCAmelCase						    =       {
          name: tf.keras.Input(tensor.shape[1:]				,				dtype=tensor.dtype   ) for name, tensor in inputs_dict.items()
      }

      __UpperCAmelCase						    =       tf.keras.Model(__lowerCAmelCase				,				outputs=main_layer(__lowerCAmelCase   )   )
      __UpperCAmelCase						    =       model(__lowerCAmelCase   )

      with tempfile.TemporaryDirectory() as tmpdirname:
        __UpperCAmelCase						    =       os.path.join(__lowerCAmelCase				,				"keras_model.h5"   )
        model.save(__lowerCAmelCase   )
        __UpperCAmelCase						    =       tf.keras.models.load_model(
            __lowerCAmelCase				,				custom_objects={main_layer_class.__name__: main_layer_class}   )
        assert isinstance(__lowerCAmelCase				,				tf.keras.Model   )
        __UpperCAmelCase						    =       model(__lowerCAmelCase   )
        self.assert_outputs_same(__lowerCAmelCase				,				__lowerCAmelCase   )






  @slow
  def 	_UpperCAmelCase				(				self:			Optional[int]   ) -> Dict:




    '''simple docstring'''




    np.random.seed(2   )

    __UpperCAmelCase    ,		__UpperCAmelCase						    =       self.model_tester.prepare_config_and_inputs_for_common()

    __UpperCAmelCase						    =       int((config.image_size // config.patch_size) ** 2   )
    __UpperCAmelCase						    =       np.random.uniform(size=(self.model_tester.batch_size, num_patches)   )

    for model_class in self.all_model_classes:
      __UpperCAmelCase						    =       model_class(__lowerCAmelCase   )
      __UpperCAmelCase						    =       self._prepare_for_class(__lowerCAmelCase				,				__lowerCAmelCase   )
      __UpperCAmelCase						    =       model(__lowerCAmelCase				,				noise=__lowerCAmelCase   )

      if model_class.__name__ == "TFViTMAEModel":
        __UpperCAmelCase						    =       outputs.last_hidden_state.numpy()
        __UpperCAmelCase						    =       0
      else:
        __UpperCAmelCase						    =       outputs.logits.numpy()
        __UpperCAmelCase						    =       0

      with tempfile.TemporaryDirectory() as tmpdirname:
        model.save_pretrained(__lowerCAmelCase				,				saved_model=__lowerCAmelCase   )
        __UpperCAmelCase						    =       model_class.from_pretrained(__lowerCAmelCase   )
        __UpperCAmelCase						    =       model(__lowerCAmelCase				,				noise=__lowerCAmelCase   )

        if model_class.__name__ == "TFViTMAEModel":
          __UpperCAmelCase						    =       after_outputs["last_hidden_state"].numpy()
          __UpperCAmelCase						    =       0
        else:
          __UpperCAmelCase						    =       after_outputs["logits"].numpy()
          __UpperCAmelCase						    =       0

        __UpperCAmelCase						    =       np.amax(np.abs(out_a - out_a   )   )
        self.assertLessEqual(__lowerCAmelCase				,				1E-5   )






  def 	_UpperCAmelCase				(				self:			Optional[Any]   ) -> str:




    '''simple docstring'''




    np.random.seed(2   )

    __UpperCAmelCase    ,		__UpperCAmelCase						    =       self.model_tester.prepare_config_and_inputs_for_common()

    __UpperCAmelCase						    =       int((config.image_size // config.patch_size) ** 2   )
    __UpperCAmelCase						    =       np.random.uniform(size=(self.model_tester.batch_size, num_patches)   )

    for model_class in self.all_model_classes:
      __UpperCAmelCase						    =       model_class(__lowerCAmelCase   )
      __UpperCAmelCase						    =       self._prepare_for_class(__lowerCAmelCase				,				__lowerCAmelCase   )

      __UpperCAmelCase						    =       model(__lowerCAmelCase				,				noise=__lowerCAmelCase   )
      __UpperCAmelCase						    =       model.get_config()
      # make sure that returned config is jsonifiable, which is required by keras
      json.dumps(__lowerCAmelCase   )
      __UpperCAmelCase						    =       model_class.from_config(model.get_config()   )
      # make sure it also accepts a normal config
      __UpperCAmelCase						    =       model_class.from_config(model.config   )
      __UpperCAmelCase						    =       new_model(__lowerCAmelCase   )  # Build model
      new_model.set_weights(model.get_weights()   )
      __UpperCAmelCase						    =       new_model(__lowerCAmelCase				,				noise=__lowerCAmelCase   )

      self.assert_outputs_same(__lowerCAmelCase				,				__lowerCAmelCase   )






  @unittest.skip(
      reason="ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load\n    to get deterministic results."   )
  def 	_UpperCAmelCase				(				self:			Union[str, Any]   ) -> Tuple:




    '''simple docstring'''




    pass






  @unittest.skip(reason="ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load"   )
  def 	_UpperCAmelCase				(				self:			Any   ) -> List[Any]:




    '''simple docstring'''




    pass






  @slow
  def 	_UpperCAmelCase				(				self:			Any   ) -> Union[str, Any]:




    '''simple docstring'''




    __UpperCAmelCase						    =       TFViTMAEModel.from_pretrained("google/vit-base-patch16-224"   )
    self.assertIsNotNone(__lowerCAmelCase   )






def       __lowerCAmelCase      (						)    ->  str:
  __UpperCAmelCase						    =       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"					)
  return image





@require_tf
@require_vision
class 			UpperCAmelCase__    (		unittest.TestCase					):



  """simple docstring"""






  @cached_property
  def 	_UpperCAmelCase				(				self:			Tuple   ) -> List[str]:




    '''simple docstring'''




    return ViTImageProcessor.from_pretrained("facebook/vit-mae-base"   ) if is_vision_available() else None






  @slow
  def 	_UpperCAmelCase				(				self:			List[Any]   ) -> Union[str, Any]:




    '''simple docstring'''




    np.random.seed(2   )

    __UpperCAmelCase						    =       TFViTMAEForPreTraining.from_pretrained("facebook/vit-mae-base"   )

    __UpperCAmelCase						    =       self.default_image_processor
    __UpperCAmelCase						    =       prepare_img()
    __UpperCAmelCase						    =       image_processor(images=__lowerCAmelCase				,				return_tensors="tf"   )

    # prepare a noise vector that will be also used for testing the TF model
    # (this way we can ensure that the PT and TF models operate on the same inputs)
    __UpperCAmelCase						    =       ViTMAEConfig()
    __UpperCAmelCase						    =       int((vit_mae_config.image_size // vit_mae_config.patch_size) ** 2   )
    __UpperCAmelCase						    =       np.random.uniform(size=(1, num_patches)   )

    # forward pass
    __UpperCAmelCase						    =       model(**__lowerCAmelCase				,				noise=__lowerCAmelCase   )

    # verify the logits
    __UpperCAmelCase						    =       tf.convert_to_tensor([1, 196, 768]   )
    self.assertEqual(outputs.logits.shape				,				__lowerCAmelCase   )

    __UpperCAmelCase						    =       tf.convert_to_tensor(
        [[-0.0548, -1.7023, -0.9325], [0.3721, -0.5670, -0.2233], [0.8235, -1.3878, -0.3524]]   )

    tf.debugging.assert_near(outputs.logits[0, :3, :3]				,				__lowerCAmelCase				,				atol=1E-4   )




from sklearn.metrics import fa_score

import datasets


a_							=      """
The F1 score is the harmonic mean of the precision and recall. It can be computed with the equation:
F1 = 2 * (precision * recall) / (precision + recall)
"""


a_							=      """
Args:
    predictions (`list` of `int`): Predicted labels.
    references (`list` of `int`): Ground truth labels.
    labels (`list` of `int`): The set of labels to include when `average` is not set to `'binary'`, and the order of the labels if `average` is `None`. Labels present in the data can be excluded, for example to calculate a multiclass average ignoring a majority negative class. Labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in `predictions` and `references` are used in sorted order. Defaults to None.
    pos_label (`int`): The class to be considered the positive class, in the case where `average` is set to `binary`. Defaults to 1.
    average (`string`): This parameter is required for multiclass/multilabel targets. If set to `None`, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Defaults to `'binary'`.

        - 'binary': Only report results for the class specified by `pos_label`. This is applicable only if the classes found in `predictions` and `references` are binary.
        - 'micro': Calculate metrics globally by counting the total true positives, false negatives and false positives.
        - 'macro': Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account.
        - 'weighted': Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters `'macro'` to account for label imbalance. This option can result in an F-score that is not between precision and recall.
        - 'samples': Calculate metrics for each instance, and find their average (only meaningful for multilabel classification).
    sample_weight (`list` of `float`): Sample weights Defaults to None.

Returns:
    f1 (`float` or `array` of `float`): F1 score or list of f1 scores, depending on the value passed to `average`. Minimum possible value is 0. Maximum possible value is 1. Higher f1 scores are better.

Examples:

    Example 1-A simple binary example
        >>> f1_metric = datasets.load_metric(\"f1\")
        >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0])
        >>> print(results)
        {'f1': 0.5}

    Example 2-The same simple binary example as in Example 1, but with `pos_label` set to `0`.
        >>> f1_metric = datasets.load_metric(\"f1\")
        >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], pos_label=0)
        >>> print(round(results['f1'], 2))
        0.67

    Example 3-The same simple binary example as in Example 1, but with `sample_weight` included.
        >>> f1_metric = datasets.load_metric(\"f1\")
        >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], sample_weight=[0.9, 0.5, 3.9, 1.2, 0.3])
        >>> print(round(results['f1'], 2))
        0.35

    Example 4-A multiclass example, with different values for the `average` input.
        >>> predictions = [0, 2, 1, 0, 0, 1]
        >>> references = [0, 1, 2, 0, 1, 2]
        >>> results = f1_metric.compute(predictions=predictions, references=references, average=\"macro\")
        >>> print(round(results['f1'], 2))
        0.27
        >>> results = f1_metric.compute(predictions=predictions, references=references, average=\"micro\")
        >>> print(round(results['f1'], 2))
        0.33
        >>> results = f1_metric.compute(predictions=predictions, references=references, average=\"weighted\")
        >>> print(round(results['f1'], 2))
        0.27
        >>> results = f1_metric.compute(predictions=predictions, references=references, average=None)
        >>> print(results)
        {'f1': array([0.8, 0. , 0. ])}
"""


a_							=      """
@article{scikit-learn,
    title={Scikit-learn: Machine Learning in {P}ython},
    author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.
           and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.
           and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and
           Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},
    journal={Journal of Machine Learning Research},
    volume={12},
    pages={2825--2830},
    year={2011}
}
"""


@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION		,	_KWARGS_DESCRIPTION					)
class 			UpperCAmelCase__    (		datasets.Metric					):



  """simple docstring"""






  def 	_UpperCAmelCase				(				self:			int   ) -> Dict:




    '''simple docstring'''




    return datasets.MetricInfo(
        description=_DESCRIPTION				,				citation=_CITATION				,				inputs_description=_KWARGS_DESCRIPTION				,				features=datasets.Features(
            {
                "predictions": datasets.Sequence(datasets.Value("int32"   )   ),
                "references": datasets.Sequence(datasets.Value("int32"   )   ),
            }
            if self.config_name == "multilabel"
            else {
                "predictions": datasets.Value("int32"   ),
                "references": datasets.Value("int32"   ),
            }   )				,				reference_urls=["https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html"]				,				)






  def 	_UpperCAmelCase				(				self:			Any				,				__lowerCAmelCase:			Tuple				,				__lowerCAmelCase:			Any				,				__lowerCAmelCase:			List[Any]=None				,				__lowerCAmelCase:			Optional[Any]=1				,				__lowerCAmelCase:			List[Any]="binary"				,				__lowerCAmelCase:			List[Any]=None   ) -> Optional[int]:




    '''simple docstring'''




    __UpperCAmelCase						    =       fa_score(
        __lowerCAmelCase				,				__lowerCAmelCase				,				labels=__lowerCAmelCase				,				pos_label=__lowerCAmelCase				,				average=__lowerCAmelCase				,				sample_weight=__lowerCAmelCase   )
    return {"f1": float(__lowerCAmelCase   ) if score.size == 1 else score}



