






import math
import time

from transformers import Trainer, is_torch_tpu_available
from transformers.trainer_utils import PredictionOutput, speed_metrics


if is_torch_tpu_available(check_device=False):
					import torch_xla.core.xla_model as xm
					import torch_xla.debug.metrics as met
class     lowerCamelCase_					(   lowerCamelCase			):







				def __init__(    self							,       *__lowerCAmelCase							,       __lowerCAmelCase=None							,       __lowerCAmelCase=None							,       **__lowerCAmelCase  ):
								"""simple docstring"""
								super().__init__(*__lowerCAmelCase							,       **__lowerCAmelCase  )
								__magic_name__		:Dict   =   eval_examples
								__magic_name__		:Tuple   =   post_process_function







				def       A						(    self							,       __lowerCAmelCase=None							,       __lowerCAmelCase=None							,       __lowerCAmelCase=None							,       __lowerCAmelCase = "eval"  ):
								"""simple docstring"""
								__magic_name__		:Union[str, Any]   =   self.eval_dataset if eval_dataset is None else eval_dataset
								__magic_name__		:str   =   self.get_eval_dataloader(__lowerCAmelCase  )
								__magic_name__		:Any   =   self.eval_examples if eval_examples is None else eval_examples

								# Temporarily disable metric computation, we will do it in the loop here.
								__magic_name__		:Union[str, Any]   =   self.compute_metrics
								__magic_name__		:List[str]   =   None
								__magic_name__		:int   =   self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
								__magic_name__		:Tuple   =   time.time()
								try:
												__magic_name__		:Union[str, Any]   =   eval_loop(
												    __lowerCAmelCase							,       description='''Evaluation'''							,       prediction_loss_only=True if compute_metrics is None else None							,       ignore_keys=__lowerCAmelCase							,       metric_key_prefix=__lowerCAmelCase							,       )
								finally:
												__magic_name__		:Optional[Any]   =   compute_metrics
								__magic_name__		:Union[str, Any]   =   self.args.eval_batch_size * self.args.world_size
								if F'''{metric_key_prefix}_jit_compilation_time''' in output.metrics:
												start_time += output.metrics[F'''{metric_key_prefix}_jit_compilation_time''']
								output.metrics.update(
								    speed_metrics(
								        __lowerCAmelCase							,       __lowerCAmelCase							,       num_samples=output.num_samples							,       num_steps=math.ceil(output.num_samples / total_batch_size  )							,       )  )
								if self.post_process_function is not None and self.compute_metrics is not None and self.args.should_save:
												# Only the main node write the results by default
												__magic_name__		:Dict   =   self.post_process_function(__lowerCAmelCase							,       __lowerCAmelCase							,       output.predictions  )
												__magic_name__		:List[Any]   =   self.compute_metrics(__lowerCAmelCase  )

												# Prefix all keys with metric_key_prefix + '_'
												for key in list(metrics.keys()  ):
																if not key.startswith(F'''{metric_key_prefix}_'''  ):
																				__magic_name__		:List[str]   =   metrics.pop(__lowerCAmelCase  )
												metrics.update(output.metrics  )
								else:
												__magic_name__		:str   =   output.metrics

								if self.args.should_log:
												# Only the main node log the results by default
												self.log(__lowerCAmelCase  )

								if self.args.tpu_metrics_debug or self.args.debug:
												# tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.)
												xm.master_print(met.metrics_report()  )

								__magic_name__		:List[Any]   =   self.callback_handler.on_evaluate(self.args							,       self.state							,       self.control							,       __lowerCAmelCase  )
								return metrics







				def       A						(    self							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase=None							,       __lowerCAmelCase = "test"  ):
								"""simple docstring"""
								__magic_name__		:Tuple   =   self.get_test_dataloader(__lowerCAmelCase  )

								# Temporarily disable metric computation, we will do it in the loop here.
								__magic_name__		:List[str]   =   self.compute_metrics
								__magic_name__		:List[Any]   =   None
								__magic_name__		:Union[str, Any]   =   self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
								__magic_name__		:List[Any]   =   time.time()
								try:
												__magic_name__		:Union[str, Any]   =   eval_loop(
												    __lowerCAmelCase							,       description='''Prediction'''							,       prediction_loss_only=True if compute_metrics is None else None							,       ignore_keys=__lowerCAmelCase							,       metric_key_prefix=__lowerCAmelCase							,       )
								finally:
												__magic_name__		:Union[str, Any]   =   compute_metrics
								__magic_name__		:str   =   self.args.eval_batch_size * self.args.world_size
								if F'''{metric_key_prefix}_jit_compilation_time''' in output.metrics:
												start_time += output.metrics[F'''{metric_key_prefix}_jit_compilation_time''']
								output.metrics.update(
								    speed_metrics(
								        __lowerCAmelCase							,       __lowerCAmelCase							,       num_samples=output.num_samples							,       num_steps=math.ceil(output.num_samples / total_batch_size  )							,       )  )

								if self.post_process_function is None or self.compute_metrics is None:
												return output

								__magic_name__		:List[str]   =   self.post_process_function(__lowerCAmelCase							,       __lowerCAmelCase							,       output.predictions							,       '''predict'''  )
								__magic_name__		:Dict   =   self.compute_metrics(__lowerCAmelCase  )

								# Prefix all keys with metric_key_prefix + '_'
								for key in list(metrics.keys()  ):
												if not key.startswith(F'''{metric_key_prefix}_'''  ):
																__magic_name__		:str   =   metrics.pop(__lowerCAmelCase  )
								metrics.update(output.metrics  )
								return PredictionOutput(predictions=predictions.predictions							,       label_ids=predictions.label_ids							,       metrics=__lowerCAmelCase  )














from sklearn.metrics import matthews_corrcoef

import datasets


SCREAMING_SNAKE_CASE__   :      Optional[Any]				=		"""
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]
"""

SCREAMING_SNAKE_CASE__   :      Union[str, Any]				=		"""
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
"""

SCREAMING_SNAKE_CASE__   :      int				=		"""\
@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     lowerCamelCase_					(   datasets.Metric			):







				def       A						(    self  ):
								"""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       A						(    self							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase=None  ):
								"""simple docstring"""
								return {
								    "matthews_correlation": float(matthews_corrcoef(__lowerCAmelCase							,       __lowerCAmelCase							,       sample_weight=__lowerCAmelCase  )  ),
								}






