





from sklearn.metrics import recall_score

import datasets


__A             =       "\nRecall is the fraction of the positive examples that were correctly labeled by the model as positive. It can be computed with the equation:\nRecall = TP / (TP + FN)\nWhere TP is the true positives and FN is the false negatives.\n"


__A             =       "\nArgs:\n- **predictions** (`list` of `int`): The predicted labels.\n- **references** (`list` of `int`): The ground truth labels.\n- **labels** (`list` of `int`): The set of labels to include when `average` is not set to `binary`, and their order when average is `None`. Labels present in the data can be excluded in this input, for example to calculate a multiclass average ignoring a majority negative class, while 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 y_true and y_pred are used in sorted order. Defaults to None.\n- **pos_label** (`int`): The class label to use as the \'positive class\' when calculating the recall. Defaults to `1`.\n- **average** (`string`): This parameter is required for multiclass/multilabel targets. If None, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Defaults to `\'binary\'`.\n    - `\'binary\'`: Only report results for the class specified by `pos_label`. This is applicable only if the target labels and predictions are binary.\n    - `\'micro\'`: Calculate metrics globally by counting the total true positives, false negatives, and false positives.\n    - `\'macro\'`: Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account.\n    - `\'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. Note that it can result in an F-score that is not between precision and recall.\n    - `\'samples\'`: Calculate metrics for each instance, and find their average (only meaningful for multilabel classification).\n- **sample_weight** (`list` of `float`): Sample weights Defaults to `None`.\n- **zero_division** (): Sets the value to return when there is a zero division. Defaults to .\n    - `\'warn\'`: If there is a zero division, the return value is `0`, but warnings are also raised.\n    - `0`: If there is a zero division, the return value is `0`.\n    - `1`: If there is a zero division, the return value is `1`.\n\nReturns:\n- **recall** (`float`, or `array` of `float`): Either the general recall score, or the recall scores for individual classes, depending on the values input to `labels` and `average`. Minimum possible value is 0. Maximum possible value is 1. A higher recall means that more of the positive examples have been labeled correctly. Therefore, a higher recall is generally considered better.\n\nExamples:\n\n    Example 1-A simple example with some errors\n        >>> recall_metric = datasets.load_metric(\'recall\')\n        >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1])\n        >>> print(results)\n        {\'recall\': 0.6666666666666666}\n\n    Example 2-The same example as Example 1, but with `pos_label=0` instead of the default `pos_label=1`.\n        >>> recall_metric = datasets.load_metric(\'recall\')\n        >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1], pos_label=0)\n        >>> print(results)\n        {\'recall\': 0.5}\n\n    Example 3-The same example as Example 1, but with `sample_weight` included.\n        >>> recall_metric = datasets.load_metric(\'recall\')\n        >>> sample_weight = [0.9, 0.2, 0.9, 0.3, 0.8]\n        >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1], sample_weight=sample_weight)\n        >>> print(results)\n        {\'recall\': 0.55}\n\n    Example 4-A multiclass example, using different averages.\n        >>> recall_metric = datasets.load_metric(\'recall\')\n        >>> predictions = [0, 2, 1, 0, 0, 1]\n        >>> references = [0, 1, 2, 0, 1, 2]\n        >>> results = recall_metric.compute(predictions=predictions, references=references, average=\'macro\')\n        >>> print(results)\n        {\'recall\': 0.3333333333333333}\n        >>> results = recall_metric.compute(predictions=predictions, references=references, average=\'micro\')\n        >>> print(results)\n        {\'recall\': 0.3333333333333333}\n        >>> results = recall_metric.compute(predictions=predictions, references=references, average=\'weighted\')\n        >>> print(results)\n        {\'recall\': 0.3333333333333333}\n        >>> results = recall_metric.compute(predictions=predictions, references=references, average=None)\n        >>> print(results)\n        {\'recall\': array([1., 0., 0.])}\n"


__A             =       "\n@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}\n"



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







 '''simple docstring'''

 def 							SCREAMING_SNAKE_CASE_      (self       :     List[Any]) ->str:



   '''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.recall_score.html"] ,				)







 def 							SCREAMING_SNAKE_CASE_      (self       :     Union[str, Any] ,				UpperCAmelCase_       :     List[str] ,				UpperCAmelCase_       :     Dict ,				UpperCAmelCase_       :     Any=None ,				UpperCAmelCase_       :     Dict=1 ,				UpperCAmelCase_       :     List[str]="binary" ,				UpperCAmelCase_       :     Dict=None ,				UpperCAmelCase_       :     List[str]="warn" ,				) ->int:



   '''simple docstring'''



   lowerCamelCase__:   int    						=recall_score(
       _SCREAMING_SNAKE_CASE ,				_SCREAMING_SNAKE_CASE ,				labels=_SCREAMING_SNAKE_CASE ,				pos_label=_SCREAMING_SNAKE_CASE ,				average=_SCREAMING_SNAKE_CASE ,				sample_weight=_SCREAMING_SNAKE_CASE ,				zero_division=_SCREAMING_SNAKE_CASE ,				)
   return {"recall": float(_SCREAMING_SNAKE_CASE) if score.size == 1 else score}








import inspect
import unittest

import numpy as np

from transformers import BeitConfig
from transformers.testing_utils import require_flax, require_vision, slow
from transformers.utils import cached_property, is_flax_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor


if is_flax_available():
     import jax

     from transformers import FlaxBeitForImageClassification, FlaxBeitForMaskedImageModeling, FlaxBeitModel

if is_vision_available():
     from PIL import Image

     from transformers import BeitImageProcessor



class      _SCREAMING_SNAKE_CASE		(			unittest.TestCase					):







 '''simple docstring'''

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



   '''simple docstring'''



   lowerCamelCase__:   List[Any]    						=parent
   lowerCamelCase__:   List[Any]    						=vocab_size
   lowerCamelCase__:   List[str]    						=batch_size
   lowerCamelCase__:   str    						=image_size
   lowerCamelCase__:   Any    						=patch_size
   lowerCamelCase__:   int    						=num_channels
   lowerCamelCase__:   str    						=is_training
   lowerCamelCase__:   Tuple    						=use_labels
   lowerCamelCase__:   Optional[int]    						=hidden_size
   lowerCamelCase__:   Union[str, Any]    						=num_hidden_layers
   lowerCamelCase__:   List[Any]    						=num_attention_heads
   lowerCamelCase__:   Optional[Any]    						=intermediate_size
   lowerCamelCase__:   Tuple    						=hidden_act
   lowerCamelCase__:   Optional[Any]    						=hidden_dropout_prob
   lowerCamelCase__:   Union[str, Any]    						=attention_probs_dropout_prob
   lowerCamelCase__:   List[Any]    						=type_sequence_label_size
   lowerCamelCase__:   List[str]    						=initializer_range

   # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
   lowerCamelCase__:   Union[str, Any]    						=(image_size // patch_size) ** 2
   lowerCamelCase__:   str    						=num_patches + 1

 def 							SCREAMING_SNAKE_CASE_      (self       :     List[str]) ->Tuple:



   '''simple docstring'''



   lowerCamelCase__:   Union[str, Any]    						=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

   lowerCamelCase__:   Any    						=None
   if self.use_labels:
     lowerCamelCase__:   Union[str, Any]    						=ids_tensor([self.batch_size] ,				self.type_sequence_label_size)

   lowerCamelCase__:   Dict    						=BeitConfig(
       vocab_size=self.vocab_size ,				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 ,				hidden_act=self.hidden_act ,				hidden_dropout_prob=self.hidden_dropout_prob ,				attention_probs_dropout_prob=self.attention_probs_dropout_prob ,				is_decoder=UpperCAmelCase_ ,				initializer_range=self.initializer_range ,				)

   return config, pixel_values, labels

 def 							SCREAMING_SNAKE_CASE_      (self       :     Optional[Any] ,				UpperCAmelCase_       :     int ,				UpperCAmelCase_       :     Any ,				UpperCAmelCase_       :     Optional[Any]) ->List[Any]:



   '''simple docstring'''



   lowerCamelCase__:   Dict    						=FlaxBeitModel(config=UpperCAmelCase_)
   lowerCamelCase__:   Any    						=model(UpperCAmelCase_)
   self.parent.assertEqual(result.last_hidden_state.shape ,				(self.batch_size, self.seq_length, self.hidden_size))

 def 							SCREAMING_SNAKE_CASE_      (self       :     Optional[Any] ,				UpperCAmelCase_       :     int ,				UpperCAmelCase_       :     List[Any] ,				UpperCAmelCase_       :     Optional[Any]) ->List[Any]:



   '''simple docstring'''



   lowerCamelCase__:   Any    						=FlaxBeitForMaskedImageModeling(config=UpperCAmelCase_)
   lowerCamelCase__:   Any    						=model(UpperCAmelCase_)
   self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.seq_length - 1, self.vocab_size))

 def 							SCREAMING_SNAKE_CASE_      (self       :     int ,				UpperCAmelCase_       :     Optional[Any] ,				UpperCAmelCase_       :     int ,				UpperCAmelCase_       :     Any) ->List[str]:



   '''simple docstring'''



   lowerCamelCase__:   str    						=self.type_sequence_label_size
   lowerCamelCase__:   Any    						=FlaxBeitForImageClassification(config=UpperCAmelCase_)
   lowerCamelCase__:   List[str]    						=model(UpperCAmelCase_)
   self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.type_sequence_label_size))

   # test greyscale images
   lowerCamelCase__:   int    						=1
   lowerCamelCase__:   int    						=FlaxBeitForImageClassification(UpperCAmelCase_)

   lowerCamelCase__:   Optional[Any]    						=floats_tensor([self.batch_size, 1, self.image_size, self.image_size])
   lowerCamelCase__:   List[str]    						=model(UpperCAmelCase_)







 def 							SCREAMING_SNAKE_CASE_      (self       :     int) ->str:



   '''simple docstring'''



   lowerCamelCase__:   List[Any]    						=self.prepare_config_and_inputs()
   (
       (
       lowerCamelCase__
   )						,      (
       lowerCamelCase__
   )						,      (
       lowerCamelCase__
   )						,      
   ):   List[Any]    						=config_and_inputs
   lowerCamelCase__:   Dict    						={"pixel_values": pixel_values}
   return config, inputs_dict



@require_flax
class      _SCREAMING_SNAKE_CASE		(			__SCREAMING_SNAKE_CASE					,							unittest.TestCase					):







 '''simple docstring'''
 lowercase_	=    (
     (FlaxBeitModel, FlaxBeitForImageClassification, FlaxBeitForMaskedImageModeling) if is_flax_available() else ()
 )

 def 							SCREAMING_SNAKE_CASE_      (self       :     Dict) ->None:



   '''simple docstring'''



   lowerCamelCase__:   Optional[Any]    						=FlaxBeitModelTester(self)
   lowerCamelCase__:   Any    						=ConfigTester(self ,				config_class=UpperCAmelCase_ ,				has_text_modality=UpperCAmelCase_ ,				hidden_size=37)

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



   '''simple docstring'''



   self.config_tester.run_common_tests()

 def 							SCREAMING_SNAKE_CASE_      (self       :     Any) ->List[Any]:



   '''simple docstring'''



   lowerCamelCase__						,      lowerCamelCase__:   Union[str, Any]    						=self.model_tester.prepare_config_and_inputs_for_common()

   for model_class in self.all_model_classes:
     lowerCamelCase__:   int    						=model_class(UpperCAmelCase_)
     lowerCamelCase__:   Union[str, Any]    						=inspect.signature(model.__call__)
     # signature.parameters is an OrderedDict => so arg_names order is deterministic
     lowerCamelCase__:   Dict    						=[*signature.parameters.keys()]

     lowerCamelCase__:   Dict    						=["pixel_values"]
     self.assertListEqual(arg_names[:1] ,				UpperCAmelCase_)

 def 							SCREAMING_SNAKE_CASE_      (self       :     int) ->List[str]:



   '''simple docstring'''



   lowerCamelCase__						,      lowerCamelCase__:   Dict    						=self.model_tester.prepare_config_and_inputs_for_common()

   for model_class in self.all_model_classes:
     with self.subTest(model_class.__name__):
       lowerCamelCase__:   List[str]    						=self._prepare_for_class(UpperCAmelCase_ ,				UpperCAmelCase_)
       lowerCamelCase__:   Any    						=model_class(UpperCAmelCase_)

       @jax.jit
       def model_jitted(UpperCAmelCase_       :     Dict ,				**UpperCAmelCase_       :     Tuple):
         return model(pixel_values=UpperCAmelCase_ ,				**UpperCAmelCase_)

       with self.subTest("JIT Enabled"):
         lowerCamelCase__:   Tuple    						=model_jitted(**UpperCAmelCase_).to_tuple()

       with self.subTest("JIT Disabled"):
         with jax.disable_jit():
           lowerCamelCase__:   Tuple    						=model_jitted(**UpperCAmelCase_).to_tuple()

       self.assertEqual(len(UpperCAmelCase_) ,				len(UpperCAmelCase_))
       for jitted_output, output in zip(UpperCAmelCase_ ,				UpperCAmelCase_):
         self.assertEqual(jitted_output.shape ,				output.shape)

 def 							SCREAMING_SNAKE_CASE_      (self       :     str) ->str:



   '''simple docstring'''



   lowerCamelCase__:   Tuple    						=self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_model(*UpperCAmelCase_)

 def 							SCREAMING_SNAKE_CASE_      (self       :     str) ->List[Any]:



   '''simple docstring'''



   lowerCamelCase__:   Optional[Any]    						=self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_masked_lm(*UpperCAmelCase_)

 def 							SCREAMING_SNAKE_CASE_      (self       :     str) ->str:



   '''simple docstring'''



   lowerCamelCase__:   List[Any]    						=self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_image_classification(*UpperCAmelCase_)







 @slow
 def 							SCREAMING_SNAKE_CASE_      (self       :     str) ->Any:



   '''simple docstring'''



   for model_class_name in self.all_model_classes:
     lowerCamelCase__:   Any    						=model_class_name.from_pretrained("microsoft/beit-base-patch16-224")
     lowerCamelCase__:   Dict    						=model(np.ones((1, 3, 224, 224)))
     self.assertIsNotNone(UpperCAmelCase_)





def   lowerCAmelCase_				( )  ->	Dict:







  """simple docstring"""





  lowerCamelCase__:   Any    						=Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"	)
  return image






@require_vision
@require_flax
class      _SCREAMING_SNAKE_CASE		(			unittest.TestCase					):







 '''simple docstring'''

 @cached_property
 def 							SCREAMING_SNAKE_CASE_      (self       :     Optional[Any]) ->List[str]:



   '''simple docstring'''



   return BeitImageProcessor.from_pretrained("microsoft/beit-base-patch16-224") if is_vision_available() else None

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



   '''simple docstring'''



   lowerCamelCase__:   Any    						=FlaxBeitForMaskedImageModeling.from_pretrained("microsoft/beit-base-patch16-224-pt22k")

   lowerCamelCase__:   Optional[int]    						=self.default_image_processor
   lowerCamelCase__:   int    						=prepare_img()
   lowerCamelCase__:   Optional[Any]    						=image_processor(images=UpperCAmelCase_ ,				return_tensors="np").pixel_values

   # prepare bool_masked_pos
   lowerCamelCase__:   List[str]    						=np.ones((1, 196) ,				dtype=UpperCAmelCase_)

   # forward pass
   lowerCamelCase__:   List[str]    						=model(pixel_values=UpperCAmelCase_ ,				bool_masked_pos=UpperCAmelCase_)
   lowerCamelCase__:   Optional[Any]    						=outputs.logits

   # verify the logits
   lowerCamelCase__:   str    						=(1, 196, 8_192)
   self.assertEqual(logits.shape ,				UpperCAmelCase_)

   lowerCamelCase__:   Optional[Any]    						=np.array(
       [[-3.2437, 0.5072, -13.9174], [-3.2456, 0.4948, -13.9401], [-3.2033, 0.5121, -13.8550]])

   self.assertTrue(np.allclose(logits[bool_masked_pos][:3, :3] ,				UpperCAmelCase_ ,				atol=1E-2))

 @slow
 def 							SCREAMING_SNAKE_CASE_      (self       :     str) ->Any:



   '''simple docstring'''



   lowerCamelCase__:   List[Any]    						=FlaxBeitForImageClassification.from_pretrained("microsoft/beit-base-patch16-224")

   lowerCamelCase__:   int    						=self.default_image_processor
   lowerCamelCase__:   str    						=prepare_img()
   lowerCamelCase__:   List[str]    						=image_processor(images=UpperCAmelCase_ ,				return_tensors="np")

   # forward pass
   lowerCamelCase__:   Union[str, Any]    						=model(**UpperCAmelCase_)
   lowerCamelCase__:   int    						=outputs.logits

   # verify the logits
   lowerCamelCase__:   Dict    						=(1, 1_000)
   self.assertEqual(logits.shape ,				UpperCAmelCase_)

   lowerCamelCase__:   Any    						=np.array([-1.2385, -1.0987, -1.0108])

   self.assertTrue(np.allclose(logits[0, :3] ,				UpperCAmelCase_ ,				atol=1E-4))

   lowerCamelCase__:   List[str]    						=281
   self.assertEqual(logits.argmax(-1).item() ,				UpperCAmelCase_)







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



   '''simple docstring'''



   lowerCamelCase__:   Optional[int]    						=FlaxBeitForImageClassification.from_pretrained("microsoft/beit-large-patch16-224-pt22k-ft22k")

   lowerCamelCase__:   List[str]    						=self.default_image_processor
   lowerCamelCase__:   Dict    						=prepare_img()
   lowerCamelCase__:   List[Any]    						=image_processor(images=UpperCAmelCase_ ,				return_tensors="np")

   # forward pass
   lowerCamelCase__:   Optional[Any]    						=model(**UpperCAmelCase_)
   lowerCamelCase__:   Any    						=outputs.logits

   # verify the logits
   lowerCamelCase__:   Any    						=(1, 21_841)
   self.assertEqual(logits.shape ,				UpperCAmelCase_)

   lowerCamelCase__:   str    						=np.array([1.6881, -0.2787, 0.5901])

   self.assertTrue(np.allclose(logits[0, :3] ,				UpperCAmelCase_ ,				atol=1E-4))

   lowerCamelCase__:   List[str]    						=2_396
   self.assertEqual(logits.argmax(-1).item() ,				UpperCAmelCase_)

