


"""simple docstring"""







from sklearn.metrics import fa_score

import datasets


lowerCamelCase_       :       Dict      						=  """
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)
"""


lowerCamelCase_       :       List[str]      						=  """
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. ])}
"""


lowerCamelCase_       :       List[Any]      						=  """
@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 				__A    (	datasets.Metric      ):






 """simple docstring"""




 def 				SCREAMING_SNAKE_CASE							(		self						) ->					List[str]:
     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 				SCREAMING_SNAKE_CASE							(		self			,   __A			,   __A			,   __A=None			,   __A=1			,   __A="binary"			,   __A=None						) ->					Any:
     a					=fa_score(
         __A			,   __A			,   labels=__A			,   pos_label=__A			,   average=__A			,   sample_weight=__A						)
     return {"f1": float(__A						) if score.size == 1 else score}



"""simple docstring"""







import itertools
import os
import random
import tempfile
import unittest

import numpy as np
from datasets import load_dataset

from transformers import is_speech_available
from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_speech_available():
       from transformers import WhisperFeatureExtractor

if is_torch_available():
       import torch

lowerCamelCase_       :       Any      						=  random.Random()
def _A						(						lowercase	,   lowercase=1.0	,   lowercase=None	,   lowercase=None		):







    """simple docstring"""
    if rng is None:
        a					=global_rng

    a					=[]
    for batch_idx in range(shape[0]		):
        values.append([]		)
        for _ in range(shape[1]		):
            values[-1].append(rng.random() * scale		)

    return values






@require_torch
@require_torchaudio
class 				__A    (	unittest.TestCase      ):






 """simple docstring"""




 def __init__(		self			,   __A			,   __A=7			,   __A=400			,   __A=2000			,   __A=10			,   __A=160			,   __A=8			,   __A=0.0			,   __A=4000			,   __A=False			,   __A=True			,   ) ->					Optional[Any]:
     a					=parent
     a					=batch_size
     a					=min_seq_length
     a					=max_seq_length
     a					=(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
     a					=padding_value
     a					=sampling_rate
     a					=return_attention_mask
     a					=do_normalize
     a					=feature_size
     a					=chunk_length
     a					=hop_length


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					str:
     return {
         "feature_size": self.feature_size,
         "hop_length": self.hop_length,
         "chunk_length": self.chunk_length,
         "padding_value": self.padding_value,
         "sampling_rate": self.sampling_rate,
         "return_attention_mask": self.return_attention_mask,
         "do_normalize": self.do_normalize,
     }





 def 				SCREAMING_SNAKE_CASE							(		self			,   __A=False			,   __A=False						) ->					str:
     def _flatten(__A						):
         return list(itertools.chain(*__A						)						)

     if equal_length:
         a					=[floats_list((self.max_seq_length, self.feature_size)						) for _ in range(self.batch_size						)]
     else:
         # make sure that inputs increase in size
         a					=[
             floats_list((x, self.feature_size)						)
             for x in range(self.min_seq_length			,   self.max_seq_length			,   self.seq_length_diff						)
         ]
     if numpify:
         a					=[np.asarray(__A						) for x in speech_inputs]
     return speech_inputs






@require_torch
@require_torchaudio
class 				__A    (	_SCREAMING_SNAKE_CASE,   unittest.TestCase      ):






 """simple docstring"""




 __lowerCAmelCase			 =  WhisperFeatureExtractor if is_speech_available() else None


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					Dict:
     a					=WhisperFeatureExtractionTester(self						)


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					Union[str, Any]:
     a					=self.feature_extraction_class(**self.feat_extract_dict						)

     with tempfile.TemporaryDirectory() as tmpdirname:
         a					=feat_extract_first.save_pretrained(__A						)[0]
         check_json_file_has_correct_format(__A						)
         a					=self.feature_extraction_class.from_pretrained(__A						)

     a					=feat_extract_first.to_dict()
     a					=feat_extract_second.to_dict()
     a					=feat_extract_first.mel_filters
     a					=feat_extract_second.mel_filters
     self.assertTrue(np.allclose(__A			,   __A						)						)
     self.assertEqual(__A			,   __A						)


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					str:
     a					=self.feature_extraction_class(**self.feat_extract_dict						)

     with tempfile.TemporaryDirectory() as tmpdirname:
         a					=os.path.join(__A			,   '''feat_extract.json'''						)
         feat_extract_first.to_json_file(__A						)
         a					=self.feature_extraction_class.from_json_file(__A						)

     a					=feat_extract_first.to_dict()
     a					=feat_extract_second.to_dict()
     a					=feat_extract_first.mel_filters
     a					=feat_extract_second.mel_filters
     self.assertTrue(np.allclose(__A			,   __A						)						)
     self.assertEqual(__A			,   __A						)


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					Optional[int]:
     # Tests that all call wrap to encode_plus and batch_encode_plus
     a					=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()						)
     # create three inputs of length 800, 1000, and 1200
     a					=[floats_list((1, x)						)[0] for x in range(800			,   1400			,   200						)]
     a					=[np.asarray(__A						) for speech_input in speech_inputs]

     # Test feature size
     a					=feature_extractor(__A			,   padding='''max_length'''			,   return_tensors='''np'''						).input_features
     self.assertTrue(input_features.ndim == 3						)
     self.assertTrue(input_features.shape[-1] == feature_extractor.nb_max_frames						)
     self.assertTrue(input_features.shape[-2] == feature_extractor.feature_size						)

     # Test not batched input
     a					=feature_extractor(speech_inputs[0]			,   return_tensors='''np'''						).input_features
     a					=feature_extractor(np_speech_inputs[0]			,   return_tensors='''np'''						).input_features
     self.assertTrue(np.allclose(__A			,   __A			,   atol=1E-3						)						)

     # Test batched
     a					=feature_extractor(__A			,   return_tensors='''np'''						).input_features
     a					=feature_extractor(__A			,   return_tensors='''np'''						).input_features
     for enc_seq_a, enc_seq_a in zip(__A			,   __A						):
         self.assertTrue(np.allclose(__A			,   __A			,   atol=1E-3						)						)

     # Test 2-D numpy arrays are batched.
     a					=[floats_list((1, x)						)[0] for x in (800, 800, 800)]
     a					=np.asarray(__A						)
     a					=feature_extractor(__A			,   return_tensors='''np'''						).input_features
     a					=feature_extractor(__A			,   return_tensors='''np'''						).input_features
     for enc_seq_a, enc_seq_a in zip(__A			,   __A						):
         self.assertTrue(np.allclose(__A			,   __A			,   atol=1E-3						)						)

     # Test truncation required
     a					=[floats_list((1, x)						)[0] for x in range(200			,   (feature_extractor.n_samples + 500)			,   200						)]
     a					=[np.asarray(__A						) for speech_input in speech_inputs]

     a					=[x[: feature_extractor.n_samples] for x in speech_inputs]
     a					=[np.asarray(__A						) for speech_input in speech_inputs_truncated]

     a					=feature_extractor(__A			,   return_tensors='''np'''						).input_features
     a					=feature_extractor(__A			,   return_tensors='''np'''						).input_features
     for enc_seq_a, enc_seq_a in zip(__A			,   __A						):
         self.assertTrue(np.allclose(__A			,   __A			,   atol=1E-3						)						)


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					Tuple:
     import torch

     a					=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()						)
     a					=np.random.rand(100			,   32						).astype(np.floataa						)
     a					=np_speech_inputs.tolist()

     for inputs in [py_speech_inputs, np_speech_inputs]:
         a					=feature_extractor.pad([{'''input_features''': inputs}]			,   return_tensors='''np'''						)
         self.assertTrue(np_processed.input_features.dtype == np.floataa						)
         a					=feature_extractor.pad([{'''input_features''': inputs}]			,   return_tensors='''pt'''						)
         self.assertTrue(pt_processed.input_features.dtype == torch.floataa						)


 def 				SCREAMING_SNAKE_CASE							(		self			,   __A						) ->					Dict:
     a					=load_dataset('''hf-internal-testing/librispeech_asr_dummy'''			,   '''clean'''			,   split='''validation'''						)
     # automatic decoding with librispeech
     a					=ds.sort('''id'''						).select(range(__A						)						)[:num_samples]['''audio''']

     return [x["array"] for x in speech_samples]


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					Any:
     # fmt: off
     a					=torch.tensor(
         [
             0.1_193, -0.0_946, -0.1_098, -0.0_196, 0.0_225, -0.0_690, -0.1_736, 0.0_951,
             0.0_971, -0.0_817, -0.0_702, 0.0_162, 0.0_260, 0.0_017, -0.0_192, -0.1_678,
             0.0_709, -0.1_867, -0.0_655, -0.0_274, -0.0_234, -0.1_884, -0.0_516, -0.0_554,
             -0.0_274, -0.1_425, -0.1_423, 0.0_837, 0.0_377, -0.0_854
         ]						)
     # fmt: on

     a					=self._load_datasamples(1						)
     a					=WhisperFeatureExtractor()
     a					=feature_extractor(__A			,   return_tensors='''pt'''						).input_features
     self.assertEqual(input_features.shape			,   (1, 80, 3000)						)
     self.assertTrue(torch.allclose(input_features[0, 0, :30]			,   __A			,   atol=1E-4						)						)





 def 				SCREAMING_SNAKE_CASE							(		self						) ->					Union[str, Any]:
     a					=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()						)
     a					=self._load_datasamples(1						)[0]
     a					=((audio - audio.min()) / (audio.max() - audio.min())) * 6_5535  # Rescale to [0, 65535] to show issue
     a					=feat_extract.zero_mean_unit_var_norm([audio]			,   attention_mask=__A						)[0]

     self.assertTrue(np.all(np.mean(__A						) < 1E-3						)						)
     self.assertTrue(np.all(np.abs(np.var(__A						) - 1						) < 1E-3						)						)