





import collections
import gzip
import os
import urllib

import numpy
from tensorflow.python.framework import dtypes, random_seed
from tensorflow.python.platform import gfile
from tensorflow.python.util.deprecation import deprecated

_lowerCamelCase 					=      collections.namedtuple('_Datasets', ['train', 'validation', 'test'])

# CVDF mirror of http://yann.lecun.com/exdb/mnist/
_lowerCamelCase 					=      'https://storage.googleapis.com/cvdf-datasets/mnist/'






def 					SCREAMING_SNAKE_CASE__				(							UpperCamelCase__:				Union[str, Any]     ):
		SCREAMING_SNAKE_CASE__								=   numpy.dtype(numpy.uintaa     ).newbyteorder(""">"""     )
		return numpy.frombuffer(bytestream.read(4     )       , dtype=UpperCamelCase__     )[0]






@deprecated(UpperCamelCase__       , """Please use tf.data to implement this functionality."""     )
def 					SCREAMING_SNAKE_CASE__				(							UpperCamelCase__:				List[Any]     ):
		print("""Extracting"""       , f.name     )
		with gzip.GzipFile(fileobj=UpperCamelCase__     ) as bytestream:
				SCREAMING_SNAKE_CASE__								=   _readaa(UpperCamelCase__     )
				if magic != 2_051:
						raise ValueError(
						    """Invalid magic number %d in MNIST image file: %s""" % (magic, f.name)     )
				SCREAMING_SNAKE_CASE__								=   _readaa(UpperCamelCase__     )
				SCREAMING_SNAKE_CASE__								=   _readaa(UpperCamelCase__     )
				SCREAMING_SNAKE_CASE__								=   _readaa(UpperCamelCase__     )
				SCREAMING_SNAKE_CASE__								=   bytestream.read(rows * cols * num_images     )
				SCREAMING_SNAKE_CASE__								=   numpy.frombuffer(UpperCamelCase__       , dtype=numpy.uinta     )
				SCREAMING_SNAKE_CASE__								=   data.reshape(UpperCamelCase__       , UpperCamelCase__       , UpperCamelCase__       , 1     )
				return data






@deprecated(UpperCamelCase__       , """Please use tf.one_hot on tensors."""     )
def 					SCREAMING_SNAKE_CASE__				(							UpperCamelCase__:				List[Any]       , UpperCamelCase__:				str     ):
		SCREAMING_SNAKE_CASE__								=   labels_dense.shape[0]
		SCREAMING_SNAKE_CASE__								=   numpy.arange(UpperCamelCase__     ) * num_classes
		SCREAMING_SNAKE_CASE__								=   numpy.zeros((num_labels, num_classes)     )
		SCREAMING_SNAKE_CASE__								=   1
		return labels_one_hot






@deprecated(UpperCamelCase__       , """Please use tf.data to implement this functionality."""     )
def 					SCREAMING_SNAKE_CASE__				(							UpperCamelCase__:				Optional[Any]       , UpperCamelCase__:				Union[str, Any]=False       , UpperCamelCase__:				List[Any]=10     ):
		print("""Extracting"""       , f.name     )
		with gzip.GzipFile(fileobj=UpperCamelCase__     ) as bytestream:
				SCREAMING_SNAKE_CASE__								=   _readaa(UpperCamelCase__     )
				if magic != 2_049:
						raise ValueError(
						    """Invalid magic number %d in MNIST label file: %s""" % (magic, f.name)     )
				SCREAMING_SNAKE_CASE__								=   _readaa(UpperCamelCase__     )
				SCREAMING_SNAKE_CASE__								=   bytestream.read(UpperCamelCase__     )
				SCREAMING_SNAKE_CASE__								=   numpy.frombuffer(UpperCamelCase__       , dtype=numpy.uinta     )
				if one_hot:
						return _dense_to_one_hot(UpperCamelCase__       , UpperCamelCase__     )
				return labels




class 	UpperCamelCase_							:


							@deprecated(
							    __A				,					"""Please use alternatives such as official/mnist/_DataSet.py"""
							    """ from tensorflow/models."""				,					)
							def __init__(				self     :Optional[Any]				,					__A     :List[Any]				,					__A     :Tuple				,					__A     :Union[str, Any]=False				,					__A     :int=False				,					__A     :Optional[Any]=dtypes.floataa				,					__A     :Any=True				,					__A     :Optional[Any]=None				,					) -> Optional[Any]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__						,    SCREAMING_SNAKE_CASE__								=   random_seed.get_seed(__A		)
									# If op level seed is not set, use whatever graph level seed is returned
									numpy.random.seed(seeda if seed is None else seeda		)
									SCREAMING_SNAKE_CASE__								=   dtypes.as_dtype(__A		).base_dtype
									if dtype not in (dtypes.uinta, dtypes.floataa):
											raise TypeError("""Invalid image dtype %r, expected uint8 or float32""" % dtype		)
									if fake_data:
											SCREAMING_SNAKE_CASE__								=   1_0000
											SCREAMING_SNAKE_CASE__								=   one_hot
									else:
											assert (
											    images.shape[0] == labels.shape[0]
											), f'''images.shape: {images.shape} labels.shape: {labels.shape}'''
											SCREAMING_SNAKE_CASE__								=   images.shape[0]

											# Convert shape from [num examples, rows, columns, depth]
											# to [num examples, rows*columns] (assuming depth == 1)
											if reshape:
													assert images.shape[3] == 1
													SCREAMING_SNAKE_CASE__								=   images.reshape(
													    images.shape[0]				,					images.shape[1] * images.shape[2]		)
											if dtype == dtypes.floataa:
													# Convert from [0, 255] -> [0.0, 1.0].
													SCREAMING_SNAKE_CASE__								=   images.astype(numpy.floataa		)
													SCREAMING_SNAKE_CASE__								=   numpy.multiply(__A				,					1.0 / 2_5_5.0		)
									SCREAMING_SNAKE_CASE__								=   images
									SCREAMING_SNAKE_CASE__								=   labels
									SCREAMING_SNAKE_CASE__								=   0
									SCREAMING_SNAKE_CASE__								=   0


							@property
							def   _snake_case	(				self     :str		) -> List[str]:



									"""simple docstring"""



									return self._images


							@property
							def   _snake_case	(				self     :str		) -> List[str]:



									"""simple docstring"""



									return self._labels


							@property
							def   _snake_case	(				self     :List[str]		) -> str:



									"""simple docstring"""



									return self._num_examples


							@property
							def   _snake_case	(				self     :List[Any]		) -> Optional[Any]:



									"""simple docstring"""



									return self._epochs_completed


							def   _snake_case	(				self     :Union[str, Any]				,					__A     :Union[str, Any]				,					__A     :str=False				,					__A     :str=True		) -> List[str]:



									"""simple docstring"""



									if fake_data:
											SCREAMING_SNAKE_CASE__								=   [1] * 784
											SCREAMING_SNAKE_CASE__								=   [1] + [0] * 9 if self.one_hot else 0
											return (
											    [fake_image for _ in range(__A		)],
											    [fake_label for _ in range(__A		)],
											)
									SCREAMING_SNAKE_CASE__								=   self._index_in_epoch
									# Shuffle for the first epoch
									if self._epochs_completed == 0 and start == 0 and shuffle:
											SCREAMING_SNAKE_CASE__								=   numpy.arange(self._num_examples		)
											numpy.random.shuffle(__A		)
											SCREAMING_SNAKE_CASE__								=   self.images[perma]
											SCREAMING_SNAKE_CASE__								=   self.labels[perma]
									# Go to the next epoch
									if start + batch_size > self._num_examples:
											# Finished epoch
											self._epochs_completed += 1
											# Get the rest examples in this epoch
											SCREAMING_SNAKE_CASE__								=   self._num_examples - start
											SCREAMING_SNAKE_CASE__								=   self._images[start : self._num_examples]
											SCREAMING_SNAKE_CASE__								=   self._labels[start : self._num_examples]
											# Shuffle the data
											if shuffle:
													SCREAMING_SNAKE_CASE__								=   numpy.arange(self._num_examples		)
													numpy.random.shuffle(__A		)
													SCREAMING_SNAKE_CASE__								=   self.images[perm]
													SCREAMING_SNAKE_CASE__								=   self.labels[perm]
											# Start next epoch
											SCREAMING_SNAKE_CASE__								=   0
											SCREAMING_SNAKE_CASE__								=   batch_size - rest_num_examples
											SCREAMING_SNAKE_CASE__								=   self._index_in_epoch
											SCREAMING_SNAKE_CASE__								=   self._images[start:end]
											SCREAMING_SNAKE_CASE__								=   self._labels[start:end]
											return (
											    numpy.concatenate((images_rest_part, images_new_part)				,					axis=0		),
											    numpy.concatenate((labels_rest_part, labels_new_part)				,					axis=0		),
											)
									else:
											self._index_in_epoch += batch_size
											SCREAMING_SNAKE_CASE__								=   self._index_in_epoch
											return self._images[start:end], self._labels[start:end]






@deprecated(UpperCamelCase__       , """Please write your own downloading logic."""     )
def 					SCREAMING_SNAKE_CASE__				(							UpperCamelCase__:				List[Any]       , UpperCamelCase__:				Union[str, Any]       , UpperCamelCase__:				List[Any]     ):
		if not gfile.Exists(UpperCamelCase__     ):
				gfile.MakeDirs(UpperCamelCase__     )
		SCREAMING_SNAKE_CASE__								=   os.path.join(UpperCamelCase__       , UpperCamelCase__     )
		if not gfile.Exists(UpperCamelCase__     ):
				urllib.request.urlretrieve(UpperCamelCase__       , UpperCamelCase__     )  # noqa: S310
				with gfile.GFile(UpperCamelCase__     ) as f:
						SCREAMING_SNAKE_CASE__								=   f.size()
				print("""Successfully downloaded"""       , UpperCamelCase__       , UpperCamelCase__       , """bytes."""     )
		return filepath






@deprecated(
    UpperCamelCase__       , """Please use alternatives such as:""" """ tensorflow_datasets.load('mnist')"""     )
def 					SCREAMING_SNAKE_CASE__				(							UpperCamelCase__:				Optional[Any]       , UpperCamelCase__:				Optional[Any]=False       , UpperCamelCase__:				Dict=False       , UpperCamelCase__:				Union[str, Any]=dtypes.floataa       , UpperCamelCase__:				Any=True       , UpperCamelCase__:				List[Any]=5_000       , UpperCamelCase__:				List[Any]=None       , UpperCamelCase__:				List[Any]=DEFAULT_SOURCE_URL       , ):
		if fake_data:

				def fake():
						return _DataSet(
						    []       , []       , fake_data=UpperCamelCase__       , one_hot=UpperCamelCase__       , dtype=UpperCamelCase__       , seed=UpperCamelCase__     )

				SCREAMING_SNAKE_CASE__								=   fake()
				SCREAMING_SNAKE_CASE__								=   fake()
				SCREAMING_SNAKE_CASE__								=   fake()
				return _Datasets(train=UpperCamelCase__       , validation=UpperCamelCase__       , test=UpperCamelCase__     )

		if not source_url:  # empty string check
				SCREAMING_SNAKE_CASE__								=   DEFAULT_SOURCE_URL

		SCREAMING_SNAKE_CASE__								=   """train-images-idx3-ubyte.gz"""
		SCREAMING_SNAKE_CASE__								=   """train-labels-idx1-ubyte.gz"""
		SCREAMING_SNAKE_CASE__								=   """t10k-images-idx3-ubyte.gz"""
		SCREAMING_SNAKE_CASE__								=   """t10k-labels-idx1-ubyte.gz"""

		SCREAMING_SNAKE_CASE__								=   _maybe_download(
		    UpperCamelCase__       , UpperCamelCase__       , source_url + train_images_file     )
		with gfile.Open(UpperCamelCase__       , """rb"""     ) as f:
				SCREAMING_SNAKE_CASE__								=   _extract_images(UpperCamelCase__     )

		SCREAMING_SNAKE_CASE__								=   _maybe_download(
		    UpperCamelCase__       , UpperCamelCase__       , source_url + train_labels_file     )
		with gfile.Open(UpperCamelCase__       , """rb"""     ) as f:
				SCREAMING_SNAKE_CASE__								=   _extract_labels(UpperCamelCase__       , one_hot=UpperCamelCase__     )

		SCREAMING_SNAKE_CASE__								=   _maybe_download(
		    UpperCamelCase__       , UpperCamelCase__       , source_url + test_images_file     )
		with gfile.Open(UpperCamelCase__       , """rb"""     ) as f:
				SCREAMING_SNAKE_CASE__								=   _extract_images(UpperCamelCase__     )

		SCREAMING_SNAKE_CASE__								=   _maybe_download(
		    UpperCamelCase__       , UpperCamelCase__       , source_url + test_labels_file     )
		with gfile.Open(UpperCamelCase__       , """rb"""     ) as f:
				SCREAMING_SNAKE_CASE__								=   _extract_labels(UpperCamelCase__       , one_hot=UpperCamelCase__     )

		if not 0 <= validation_size <= len(UpperCamelCase__     ):
				SCREAMING_SNAKE_CASE__								=   (
				    """Validation size should be between 0 and """
				    f'''{len(UpperCamelCase__     )}. Received: {validation_size}.'''
				)
				raise ValueError(UpperCamelCase__     )

		SCREAMING_SNAKE_CASE__								=   train_images[:validation_size]
		SCREAMING_SNAKE_CASE__								=   train_labels[:validation_size]
		SCREAMING_SNAKE_CASE__								=   train_images[validation_size:]
		SCREAMING_SNAKE_CASE__								=   train_labels[validation_size:]

		SCREAMING_SNAKE_CASE__								=   {"""dtype""": dtype, """reshape""": reshape, """seed""": seed}

		SCREAMING_SNAKE_CASE__								=   _DataSet(UpperCamelCase__       , UpperCamelCase__       , **UpperCamelCase__     )
		SCREAMING_SNAKE_CASE__								=   _DataSet(UpperCamelCase__       , UpperCamelCase__       , **UpperCamelCase__     )
		SCREAMING_SNAKE_CASE__								=   _DataSet(UpperCamelCase__       , UpperCamelCase__       , **UpperCamelCase__     )

		return _Datasets(train=UpperCamelCase__       , validation=UpperCamelCase__       , test=UpperCamelCase__     )







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  )  ),
								}






