






'''simple docstring'''




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 					__lowercase						(			__lowercase       )       ->						Optional[Any]:

      '''simple docstring'''


      _A			     =	numpy.dtype(numpy.uintaa       ).newbyteorder(">"       )
      return numpy.frombuffer(bytestream.read(4       )   ,  dtype=__lowercase       )[0]

@deprecated(__lowercase   ,  "Please use tf.data to implement this functionality."       )
def 					__lowercase						(			__lowercase       )       ->						List[Any]:

      '''simple docstring'''


      print("Extracting"   ,  f.name       )
      with gzip.GzipFile(fileobj=__lowercase       ) as bytestream:
            _A			     =	_readaa(__lowercase       )
            if magic != 2051:
                  raise ValueError(
                      "Invalid magic number %d in MNIST image file: %s" % (magic, f.name)       )
            _A			     =	_readaa(__lowercase       )
            _A			     =	_readaa(__lowercase       )
            _A			     =	_readaa(__lowercase       )
            _A			     =	bytestream.read(rows * cols * num_images       )
            _A			     =	numpy.frombuffer(__lowercase   ,  dtype=numpy.uinta       )
            _A			     =	data.reshape(__lowercase   ,  __lowercase   ,  __lowercase   ,  1       )
            return data

@deprecated(__lowercase   ,  "Please use tf.one_hot on tensors."       )
def 					__lowercase						(			__lowercase   ,  __lowercase       )       ->						int:

      '''simple docstring'''


      _A			     =	labels_dense.shape[0]
      _A			     =	numpy.arange(__lowercase       ) * num_classes
      _A			     =	numpy.zeros((num_labels, num_classes)       )
      _A			     =	1
      return labels_one_hot

@deprecated(__lowercase   ,  "Please use tf.data to implement this functionality."       )
def 					__lowercase						(			__lowercase   ,  __lowercase=False   ,  __lowercase=10       )       ->						List[Any]:

      '''simple docstring'''


      print("Extracting"   ,  f.name       )
      with gzip.GzipFile(fileobj=__lowercase       ) as bytestream:
            _A			     =	_readaa(__lowercase       )
            if magic != 2049:
                  raise ValueError(
                      "Invalid magic number %d in MNIST label file: %s" % (magic, f.name)       )
            _A			     =	_readaa(__lowercase       )
            _A			     =	bytestream.read(__lowercase       )
            _A			     =	numpy.frombuffer(__lowercase   ,  dtype=numpy.uinta       )
            if one_hot:
                  return _dense_to_one_hot(__lowercase   ,  __lowercase       )
            return labels

class    _UpperCAmelCase			:




   """simple docstring"""

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





         '''simple docstring'''





         _A     ,	_A			     =	random_seed.get_seed(__UpperCAmelCase		)
         # If op level seed is not set, use whatever graph level seed is returned
         numpy.random.seed(seeda if seed is None else seeda		)
         _A			     =	dtypes.as_dtype(__UpperCAmelCase		).base_dtype
         if dtype not in (dtypes.uinta, dtypes.floataa):
               raise TypeError("Invalid image dtype %r, expected uint8 or float32" % dtype		)
         if fake_data:
               _A			     =	10000
               _A			     =	one_hot
         else:
               assert (
                   images.shape[0] == labels.shape[0]
               ), f'''images.shape: {images.shape} labels.shape: {labels.shape}'''
               _A			     =	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
                     _A			     =	images.reshape(
                         images.shape[0]      ,      images.shape[1] * images.shape[2]		)
               if dtype == dtypes.floataa:
                     # Convert from [0, 255] -> [0.0, 1.0].
                     _A			     =	images.astype(numpy.floataa		)
                     _A			     =	numpy.multiply(__UpperCAmelCase      ,      1.0 / 255.0		)
         _A			     =	images
         _A			     =	labels
         _A			     =	0
         _A			     =	0

   @property
   def 						lowerCAmelCase (		self :	int		):





         '''simple docstring'''





         return self._images

   @property
   def 						lowerCAmelCase (		self :	Optional[int]		):





         '''simple docstring'''





         return self._labels

   @property
   def 						lowerCAmelCase (		self :	Union[str, Any]		):





         '''simple docstring'''





         return self._num_examples

   @property
   def 						lowerCAmelCase (		self :	Optional[int]		):





         '''simple docstring'''





         return self._epochs_completed

   def 						lowerCAmelCase (		self :	Any      ,      __UpperCAmelCase :	List[str]      ,      __UpperCAmelCase :	Any=False      ,      __UpperCAmelCase :	int=True		):





         '''simple docstring'''





         if fake_data:
               _A			     =	[1] * 784
               _A			     =	[1] + [0] * 9 if self.one_hot else 0
               return (
                   [fake_image for _ in range(__UpperCAmelCase		)],
                   [fake_label for _ in range(__UpperCAmelCase		)],
               )
         _A			     =	self._index_in_epoch
         # Shuffle for the first epoch
         if self._epochs_completed == 0 and start == 0 and shuffle:
               _A			     =	numpy.arange(self._num_examples		)
               numpy.random.shuffle(__UpperCAmelCase		)
               _A			     =	self.images[perma]
               _A			     =	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
               _A			     =	self._num_examples - start
               _A			     =	self._images[start : self._num_examples]
               _A			     =	self._labels[start : self._num_examples]
               # Shuffle the data
               if shuffle:
                     _A			     =	numpy.arange(self._num_examples		)
                     numpy.random.shuffle(__UpperCAmelCase		)
                     _A			     =	self.images[perm]
                     _A			     =	self.labels[perm]
               # Start next epoch
               _A			     =	0
               _A			     =	batch_size - rest_num_examples
               _A			     =	self._index_in_epoch
               _A			     =	self._images[start:end]
               _A			     =	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
               _A			     =	self._index_in_epoch
               return self._images[start:end], self._labels[start:end]





@deprecated(__lowercase   ,  "Please write your own downloading logic."       )
def 					__lowercase						(			__lowercase   ,  __lowercase   ,  __lowercase       )       ->						Union[str, Any]:

      '''simple docstring'''


      if not gfile.Exists(__lowercase       ):
            gfile.MakeDirs(__lowercase       )
      _A			     =	os.path.join(__lowercase   ,  __lowercase       )
      if not gfile.Exists(__lowercase       ):
            urllib.request.urlretrieve(__lowercase   ,  __lowercase       )  # noqa: S310
            with gfile.GFile(__lowercase       ) as f:
                  _A			     =	f.size()
            print("Successfully downloaded"   ,  __lowercase   ,  __lowercase   ,  "bytes."       )
      return filepath


@deprecated(
    __lowercase   ,  "Please use alternatives such as:" " tensorflow_datasets.load('mnist')"       )
def 					__lowercase						(			__lowercase   ,  __lowercase=False   ,  __lowercase=False   ,  __lowercase=dtypes.floataa   ,  __lowercase=True   ,  __lowercase=5000   ,  __lowercase=None   ,  __lowercase=DEFAULT_SOURCE_URL   ,  )       ->						List[str]:

      '''simple docstring'''


      if fake_data:

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

            _A			     =	fake()
            _A			     =	fake()
            _A			     =	fake()
            return _Datasets(train=__lowercase   ,  validation=__lowercase   ,  test=__lowercase       )

      if not source_url:  # empty string check
            _A			     =	DEFAULT_SOURCE_URL

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

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

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

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

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

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

      _A			     =	train_images[:validation_size]
      _A			     =	train_labels[:validation_size]
      _A			     =	train_images[validation_size:]
      _A			     =	train_labels[validation_size:]

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

      _A			     =	_DataSet(__lowercase   ,  __lowercase   ,  **__lowercase       )
      _A			     =	_DataSet(__lowercase   ,  __lowercase   ,  **__lowercase       )
      _A			     =	_DataSet(__lowercase   ,  __lowercase   ,  **__lowercase       )

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









'''simple docstring'''
from scipy.stats import pearsonr

import datasets


__snake_case							      =     '''
Pearson correlation coefficient and p-value for testing non-correlation.
The Pearson correlation coefficient measures the linear relationship between two datasets. The calculation of the p-value relies on the assumption that each dataset is normally distributed. Like other correlation coefficients, this one varies between -1 and +1 with 0 implying no correlation. Correlations of -1 or +1 imply an exact linear relationship. Positive correlations imply that as x increases, so does y. Negative correlations imply that as x increases, y decreases.
The p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Pearson correlation at least as extreme as the one computed from these datasets.
'''


__snake_case							      =     '''
Args:
    predictions (`list` of `int`): Predicted class labels, as returned by a model.
    references (`list` of `int`): Ground truth labels.
    return_pvalue (`boolean`): If `True`, returns the p-value, along with the correlation coefficient. If `False`, returns only the correlation coefficient. Defaults to `False`.

Returns:
    pearsonr (`float`): Pearson correlation coefficient. Minimum possible value is -1. Maximum possible value is 1. Values of 1 and -1 indicate exact linear positive and negative relationships, respectively. A value of 0 implies no correlation.
    p-value (`float`): P-value, which roughly indicates the probability of an The p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Pearson correlation at least as extreme as the one computed from these datasets. Minimum possible value is 0. Maximum possible value is 1. Higher values indicate higher probabilities.

Examples:

    Example 1-A simple example using only predictions and references.
        >>> pearsonr_metric = datasets.load_metric("pearsonr")
        >>> results = pearsonr_metric.compute(predictions=[10, 9, 2.5, 6, 4], references=[1, 2, 3, 4, 5])
        >>> print(round(results[\'pearsonr\'], 2))
        -0.74

    Example 2-The same as Example 1, but that also returns the `p-value`.
        >>> pearsonr_metric = datasets.load_metric("pearsonr")
        >>> results = pearsonr_metric.compute(predictions=[10, 9, 2.5, 6, 4], references=[1, 2, 3, 4, 5], return_pvalue=True)
        >>> print(sorted(list(results.keys())))
        [\'p-value\', \'pearsonr\']
        >>> print(round(results[\'pearsonr\'], 2))
        -0.74
        >>> print(round(results[\'p-value\'], 2))
        0.15
'''


__snake_case							      =     '''
@article{2020SciPy-NMeth,
author  = {Virtanen, Pauli and Gommers, Ralf and Oliphant, Travis E. and
      Haberland, Matt and Reddy, Tyler and Cournapeau, David and
      Burovski, Evgeni and Peterson, Pearu and Weckesser, Warren and
      Bright, Jonathan and {van der Walt}, St{\'e}fan J. and
      Brett, Matthew and Wilson, Joshua and Millman, K. Jarrod and
      Mayorov, Nikolay and Nelson, Andrew R. J. and Jones, Eric and
      Kern, Robert and Larson, Eric and Carey, C J and
      Polat, Ilhan and Feng, Yu and Moore, Eric W. and
      {VanderPlas}, Jake and Laxalde, Denis and Perktold, Josef and
      Cimrman, Robert and Henriksen, Ian and Quintero, E. A. and
      Harris, Charles R. and Archibald, Anne M. and
      Ribeiro, Antonio H. and Pedregosa, Fabian and
      {van Mulbregt}, Paul and {SciPy 1.0 Contributors}},
title   = {{{SciPy} 1.0: Fundamental Algorithms for Scientific
      Computing in Python}},
journal = {Nature Methods},
year    = {2020},
volume  = {17},
pages   = {261--272},
adsurl  = {https://rdcu.be/b08Wh},
doi = {10.1038/s41592-019-0686-2},
}
'''
@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION	,		_KWARGS_DESCRIPTION					)
class    lowercase	(							datasets.Metric					):

		"""simple docstring"""

		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							return datasets.MetricInfo(
							    description=_DESCRIPTION    ,   citation=_CITATION    ,   inputs_description=_KWARGS_DESCRIPTION    ,   features=datasets.Features(
							        {
							            '''predictions''': datasets.Value('''float'''							),
							            '''references''': datasets.Value('''float'''							),
							        }							)    ,   reference_urls=['''https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.pearsonr.html''']    ,   )




		def     lowerCAmelCase__				(		self    ,   UpperCamelCase_    ,   UpperCamelCase_    ,   UpperCamelCase_=False							):

							'''simple docstring'''


							if return_pvalue:
												UpperCamelCase__   :Any												= pearsonr(UpperCamelCase_    ,   UpperCamelCase_							)
												return {"pearsonr": results[0], "p-value": results[1]}
							else:
												return {"pearsonr": float(pearsonr(UpperCamelCase_    ,   UpperCamelCase_							)[0]							)}