





"""simple docstring"""






import shutil
import tempfile
import unittest

from transformers import ClapFeatureExtractor, ClapProcessor, RobertaTokenizer, RobertaTokenizerFast
from transformers.testing_utils import require_sentencepiece, require_torchaudio

from .test_feature_extraction_clap import floats_list



@require_torchaudio
@require_sentencepiece
class 					__UpperCAmelCase							(						unittest.TestCase  ):
				def       UpperCAmelCase      (		self    :			int    )	->	List[Any]:



							'''simple docstring'''



							a__  :	Dict		 =			"laion/clap-htsat-unfused"
							a__  :	Optional[Any]		 =			tempfile.mkdtemp()
				def       UpperCAmelCase      (		self    :			List[Any]	, **a_    :			Union[str, Any]    )	->	Union[str, Any]:



							'''simple docstring'''



							return RobertaTokenizer.from_pretrained(self.checkpoint	, **a_    )
				def       UpperCAmelCase      (		self    :			int	, **a_    :			Optional[int]    )	->	int:



							'''simple docstring'''



							return ClapFeatureExtractor.from_pretrained(self.checkpoint	, **a_    )
				def       UpperCAmelCase      (		self    :			List[str]    )	->	int:



							'''simple docstring'''



							shutil.rmtree(self.tmpdirname    )
				def       UpperCAmelCase      (		self    :			str    )	->	int:



							'''simple docstring'''



							a__  :	List[str]		 =			self.get_tokenizer()
							a__  :	Any		 =			self.get_feature_extractor()

							a__  :	Union[str, Any]		 =			ClapProcessor(tokenizer=a_	, feature_extractor=a_    )

							processor.save_pretrained(self.tmpdirname    )
							a__  :	Dict		 =			ClapProcessor.from_pretrained(self.tmpdirname    )

							self.assertEqual(processor.tokenizer.get_vocab()	, tokenizer.get_vocab()    )
							self.assertIsInstance(processor.tokenizer	, a_    )

							self.assertEqual(processor.feature_extractor.to_json_string()	, feature_extractor.to_json_string()    )
							self.assertIsInstance(processor.feature_extractor	, a_    )
				def       UpperCAmelCase      (		self    :			List[Any]    )	->	int:



							'''simple docstring'''



							a__  :	Any		 =			ClapProcessor(tokenizer=self.get_tokenizer()	, feature_extractor=self.get_feature_extractor()    )
							processor.save_pretrained(self.tmpdirname    )

							a__  :	List[str]		 =			self.get_tokenizer(bos_token="(BOS)"	, eos_token="(EOS)"    )
							a__  :	Optional[int]		 =			self.get_feature_extractor(do_normalize=a_	, padding_value=1.0    )

							a__  :	str		 =			ClapProcessor.from_pretrained(
							    self.tmpdirname	, bos_token="(BOS)"	, eos_token="(EOS)"	, do_normalize=a_	, padding_value=1.0    )

							self.assertEqual(processor.tokenizer.get_vocab()	, tokenizer_add_kwargs.get_vocab()    )
							self.assertIsInstance(processor.tokenizer	, a_    )

							self.assertEqual(processor.feature_extractor.to_json_string()	, feature_extractor_add_kwargs.to_json_string()    )
							self.assertIsInstance(processor.feature_extractor	, a_    )
				def       UpperCAmelCase      (		self    :			Union[str, Any]    )	->	Union[str, Any]:



							'''simple docstring'''



							a__  :	Optional[int]		 =			self.get_feature_extractor()
							a__  :	Tuple		 =			self.get_tokenizer()

							a__  :	int		 =			ClapProcessor(tokenizer=a_	, feature_extractor=a_    )

							a__  :	Union[str, Any]		 =			floats_list((3, 10_00)    )

							a__  :	Optional[int]		 =			feature_extractor(a_	, return_tensors="np"    )
							a__  :	List[str]		 =			processor(audios=a_	, return_tensors="np"    )

							for key in input_feat_extract.keys():
										self.assertAlmostEqual(input_feat_extract[key].sum()	, input_processor[key].sum()	, delta=1E-2    )
				def       UpperCAmelCase      (		self    :			int    )	->	str:



							'''simple docstring'''



							a__  :	Union[str, Any]		 =			self.get_feature_extractor()
							a__  :	Dict		 =			self.get_tokenizer()

							a__  :	Any		 =			ClapProcessor(tokenizer=a_	, feature_extractor=a_    )

							a__  :	Tuple		 =			"This is a test string"

							a__  :	int		 =			processor(text=a_    )

							a__  :	List[str]		 =			tokenizer(a_    )

							for key in encoded_tok.keys():
										self.assertListEqual(encoded_tok[key]	, encoded_processor[key]    )
				def       UpperCAmelCase      (		self    :			Optional[Any]    )	->	Tuple:



							'''simple docstring'''



							a__  :	Optional[int]		 =			self.get_feature_extractor()
							a__  :	Union[str, Any]		 =			self.get_tokenizer()

							a__  :	Optional[int]		 =			ClapProcessor(tokenizer=a_	, feature_extractor=a_    )

							a__  :	Tuple		 =			[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

							a__  :	Any		 =			processor.batch_decode(a_    )
							a__  :	Optional[int]		 =			tokenizer.batch_decode(a_    )

							self.assertListEqual(a_	, a_    )






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



							'''simple docstring'''



							a__  :	List[str]		 =			self.get_feature_extractor()
							a__  :	Union[str, Any]		 =			self.get_tokenizer()

							a__  :	List[str]		 =			ClapProcessor(tokenizer=a_	, feature_extractor=a_    )

							self.assertListEqual(
							    processor.model_input_names[2:]	, feature_extractor.model_input_names	, msg="`processor` and `feature_extractor` model input names do not match"	, )






"""simple docstring"""






from sklearn.metrics import matthews_corrcoef

import datasets


__UpperCAmelCase						=			'''
Compute the Matthews correlation coefficient (MCC)

The Matthews correlation coefficient is used in machine learning as a
measure of the quality of binary and multiclass classifications. It takes
into account true and false positives and negatives and is generally
regarded as a balanced measure which can be used even if the classes are of
very different sizes. The MCC is in essence a correlation coefficient value
between -1 and +1. A coefficient of +1 represents a perfect prediction, 0
an average random prediction and -1 an inverse prediction.  The statistic
is also known as the phi coefficient. [source: Wikipedia]
'''

__UpperCAmelCase						=			'''
Args:
    predictions (list of int): Predicted labels, as returned by a model.
    references (list of int): Ground truth labels.
    sample_weight (list of int, float, or bool): Sample weights. Defaults to `None`.
Returns:
    matthews_correlation (dict containing float): Matthews correlation.
Examples:
    Example 1, a basic example with only predictions and references as inputs:
        >>> matthews_metric = datasets.load_metric("matthews_correlation")
        >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],
        ...                                     predictions=[1, 2, 2, 0, 3, 3])
        >>> print(round(results[\'matthews_correlation\'], 2))
        0.54

    Example 2, the same example as above, but also including sample weights:
        >>> matthews_metric = datasets.load_metric("matthews_correlation")
        >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],
        ...                                     predictions=[1, 2, 2, 0, 3, 3],
        ...                                     sample_weight=[0.5, 3, 1, 1, 1, 2])
        >>> print(round(results[\'matthews_correlation\'], 2))
        0.1

    Example 3, the same example as above, but with sample weights that cause a negative correlation:
        >>> matthews_metric = datasets.load_metric("matthews_correlation")
        >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],
        ...                                     predictions=[1, 2, 2, 0, 3, 3],
        ...                                     sample_weight=[0.5, 1, 0, 0, 0, 1])
        >>> print(round(results[\'matthews_correlation\'], 2))
        -0.25
'''

__UpperCAmelCase						=			'''\
@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  ):
				def       UpperCAmelCase      (		self    :			Optional[int]    )	->	str:



							'''simple docstring'''



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






				def       UpperCAmelCase      (		self    :			Tuple	, a_    :			Optional[Any]	, a_    :			Optional[Any]	, a_    :			Any=None    )	->	Union[str, Any]:



							'''simple docstring'''



							return {
							    "matthews_correlation": float(matthews_corrcoef(a_	, a_	, sample_weight=a_    )    ),
							}