



"""simple docstring"""







from scipy.stats import spearmanr

import datasets


lowercase__		:				Optional[int]          =	"""
The Spearman rank-order correlation coefficient is a measure of the
relationship between two datasets. Like other correlation coefficients,
this one varies between -1 and +1 with 0 implying no correlation.
Positive correlations imply that as data in dataset x increases, so
does data in dataset y. Negative correlations imply that as x increases,
y decreases. Correlations of -1 or +1 imply an exact monotonic relationship.

Unlike the Pearson correlation, the Spearman correlation does not
assume that both datasets are normally distributed.

The p-value roughly indicates the probability of an uncorrelated system
producing datasets that have a Spearman correlation at least as extreme
as the one computed from these datasets. The p-values are not entirely
reliable but are probably reasonable for datasets larger than 500 or so.
"""

lowercase__		:				Union[str, Any]          =	"""
Args:
    predictions (`List[float]`): Predicted labels, as returned by a model.
    references (`List[float]`): Ground truth labels.
    return_pvalue (`bool`): If `True`, returns the p-value. If `False`, returns
            only the spearmanr score. Defaults to `False`.
Returns:
    spearmanr (`float`): Spearman correlation coefficient.
    p-value (`float`): p-value. **Note**: is only returned if `return_pvalue=True` is input.
Examples:
    Example 1:
        >>> spearmanr_metric = datasets.load_metric(\"spearmanr\")
        >>> results = spearmanr_metric.compute(references=[1, 2, 3, 4, 5], predictions=[10, 9, 2.5, 6, 4])
        >>> print(results)
        {'spearmanr': -0.7}

    Example 2:
        >>> spearmanr_metric = datasets.load_metric(\"spearmanr\")
        >>> results = spearmanr_metric.compute(references=[1, 2, 3, 4, 5],
        ...                                     predictions=[10, 9, 2.5, 6, 4],
        ...                                     return_pvalue=True)
        >>> print(results['spearmanr'])
        -0.7
        >>> print(round(results['spearmanr_pvalue'], 2))
        0.19
"""

lowercase__		:				Tuple          =	R"""\
@book{kokoska2000crc,
  title={CRC standard probability and statistics tables and formulae},
  author={Kokoska, Stephen and Zwillinger, Daniel},
  year={2000},
  publisher={Crc Press}
}
@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, {\.I}lhan 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, Ant{\^o}nio 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       UpperCamelCase__   (	datasets.Metric  ):




      """simple docstring"""




      def      SCREAMING_SNAKE_CASE__ (      self				:      str       ):
        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.spearmanr.html']    ,			)





      def      SCREAMING_SNAKE_CASE__ (      self				:      Union[str, Any]    ,			SCREAMING_SNAKE_CASE_				:      Dict    ,			SCREAMING_SNAKE_CASE_				:      Tuple    ,			SCREAMING_SNAKE_CASE_				:      int=False       ):
        lowerCAmelCase_   :				List[Any]								= spearmanr(SCREAMING_SNAKE_CASE_    ,			SCREAMING_SNAKE_CASE_       )
        if return_pvalue:
          return {"spearmanr": results[0], "spearmanr_pvalue": results[1]}
        else:
          return {"spearmanr": results[0]}










"""simple docstring"""







import collections
import json
import math
import os
import re
import time
from fnmatch import fnmatch
from typing import Dict

import requests
from slack_sdk import WebClient


lowercase__		:				List[str]          =	WebClient(token=os.environ["""CI_SLACK_BOT_TOKEN"""])

def 				UpperCamelCase_    (    lowerCAmelCase__      :				Dict						)						->   Optional[Any]:

  """simple docstring"""




  lowerCAmelCase_   :				List[Any]								= test_results.split(' '						)

  lowerCAmelCase_   :				Tuple								= 0
  lowerCAmelCase_   :				Any								= 0

  # When the output is short enough, the output is surrounded by = signs: "== OUTPUT =="
  # When it is too long, those signs are not present.
  lowerCAmelCase_   :				Any								= expressions[-2] if '=' in expressions[-1] else expressions[-1]

  for i, expression in enumerate(lowerCAmelCase__						):
    if "failed" in expression:
      failed += int(expressions[i - 1]						)
    if "passed" in expression:
      success += int(expressions[i - 1]						)

  return failed, success, time_spent

def 				UpperCamelCase_    (    lowerCAmelCase__      :				Dict						)						->   str:

  """simple docstring"""




  lowerCAmelCase_   :				Optional[Any]								= {}
  lowerCAmelCase_   :				List[str]								= None
  lowerCAmelCase_   :				List[str]								= False
  for line in failures_short_lines.split('\n'						):
    if re.search(R'_ \[doctest\]'    ,   lowerCAmelCase__						):
      lowerCAmelCase_   :				Optional[Any]								= True
      lowerCAmelCase_   :				Union[str, Any]								= line.split(' '						)[2]
    elif in_error and not line.split(' '						)[0].isdigit():
      lowerCAmelCase_   :				Union[str, Any]								= line
      lowerCAmelCase_   :				int								= False

  return failures







class       UpperCamelCase__   :




      """simple docstring"""




      def __init__(      self				:      Optional[Any]    ,			SCREAMING_SNAKE_CASE_				:      str    ,			SCREAMING_SNAKE_CASE_				:      Dict       ):
        lowerCAmelCase_   :				str								= title

        lowerCAmelCase_   :				Optional[int]								= doc_test_results['time_spent'].split(','       )[0]
        lowerCAmelCase_   :				int								= doc_test_results['success']
        lowerCAmelCase_   :				Dict								= doc_test_results['failures']
        lowerCAmelCase_   :				Optional[int]								= self.n_success + self.n_failures

        # Failures and success of the modeling tests
        lowerCAmelCase_   :				Any								= doc_test_results



      @property
      def      SCREAMING_SNAKE_CASE__ (      self				:      List[str]       ):
        lowerCAmelCase_   :				int								= [self._time_spent]
        lowerCAmelCase_   :				Any								= 0

        for time in time_spent:
          lowerCAmelCase_   :				Optional[Any]								= time.split(':'       )

          # Time can be formatted as xx:xx:xx, as .xx, or as x.xx if the time spent was less than a minute.
          if len(SCREAMING_SNAKE_CASE_       ) == 1:
            lowerCAmelCase_   :				Any								= [0, 0, time_parts[0]]

          lowerCAmelCase_						,lowerCAmelCase_						,lowerCAmelCase_   :				List[str]								= int(time_parts[0]       ), int(time_parts[1]       ), float(time_parts[2]       )
          total_secs += hours * 3_6_0_0 + minutes * 6_0 + seconds

        lowerCAmelCase_						,lowerCAmelCase_						,lowerCAmelCase_   :				Union[str, Any]								= total_secs // 3_6_0_0, (total_secs % 3_6_0_0) // 6_0, total_secs % 6_0
        return F"{int(SCREAMING_SNAKE_CASE_       )}h{int(SCREAMING_SNAKE_CASE_       )}m{int(SCREAMING_SNAKE_CASE_       )}s"



      @property
      def      SCREAMING_SNAKE_CASE__ (      self				:      int       ):
        return {"type": "header", "text": {"type": "plain_text", "text": self.title}}



      @property
      def      SCREAMING_SNAKE_CASE__ (      self				:      str       ):
        return {
            "type": "section",
            "text": {
                "type": "plain_text",
                "text": F"🌞 There were no failures: all {self.n_tests} tests passed. The suite ran in {self.time}.",
                "emoji": True,
            },
            "accessory": {
                "type": "button",
                "text": {"type": "plain_text", "text": "Check Action results", "emoji": True},
                "url": F"https://github.com/huggingface/transformers/actions/runs/{os.environ['GITHUB_RUN_ID']}",
            },
        }



      @property
      def      SCREAMING_SNAKE_CASE__ (      self				:      List[Any]       ):
        return {
            "type": "section",
            "text": {
                "type": "plain_text",
                "text": (
                    F"There were {self.n_failures} failures, out of {self.n_tests} tests.\nThe suite ran in"
                    F" {self.time}."
                ),
                "emoji": True,
            },
            "accessory": {
                "type": "button",
                "text": {"type": "plain_text", "text": "Check Action results", "emoji": True},
                "url": F"https://github.com/huggingface/transformers/actions/runs/{os.environ['GITHUB_RUN_ID']}",
            },
        }



      @property
      def      SCREAMING_SNAKE_CASE__ (      self				:      Tuple       ):
        lowerCAmelCase_   :				int								= 4_0
        lowerCAmelCase_   :				List[Any]								= {k: v['failed'] for k, v in doc_test_results.items() if isinstance(SCREAMING_SNAKE_CASE_    ,			SCREAMING_SNAKE_CASE_       )}

        lowerCAmelCase_   :				List[str]								= ''
        for category, failures in category_failures.items():
          if len(SCREAMING_SNAKE_CASE_       ) == 0:
            continue

          if report != "":
            report += "\n\n"

          report += F"*{category} failures*:".ljust(line_length // 2       ).rjust(line_length // 2       ) + "\n"
          report += "`"
          report += "`\n`".join(SCREAMING_SNAKE_CASE_       )
          report += "`"

        return {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": F"The following examples had failures:\n\n\n{report}\n",
            },
        }



      @property
      def      SCREAMING_SNAKE_CASE__ (      self				:      int       ):
        lowerCAmelCase_   :				int								= [self.header]

        if self.n_failures > 0:
          blocks.append(self.failures       )

        if self.n_failures > 0:
          blocks.extend([self.category_failures]       )

        if self.n_failures == 0:
          blocks.append(self.no_failures       )

        return json.dumps(SCREAMING_SNAKE_CASE_       )



      @staticmethod
      def      SCREAMING_SNAKE_CASE__ (      ):
        lowerCAmelCase_   :				Tuple								= [
            {
                'type': 'section',
                'text': {
                    'type': 'plain_text',
                    'text': 'There was an issue running the tests.',
                },
                'accessory': {
                    'type': 'button',
                    'text': {'type': 'plain_text', 'text': 'Check Action results', 'emoji': True},
                    'url': F"https://github.com/huggingface/transformers/actions/runs/{os.environ['GITHUB_RUN_ID']}",
                },
            }
        ]

        print('Sending the following payload'       )
        print(json.dumps({'blocks': json.loads(SCREAMING_SNAKE_CASE_       )}       )       )

        client.chat_postMessage(
            channel=os.environ['CI_SLACK_CHANNEL_ID_DAILY']    ,			text='There was an issue running the tests.'    ,			blocks=SCREAMING_SNAKE_CASE_    ,			)



      def      SCREAMING_SNAKE_CASE__ (      self				:      Union[str, Any]       ):
        print('Sending the following payload'       )
        print(json.dumps({'blocks': json.loads(self.payload       )}       )       )

        lowerCAmelCase_   :				Any								= F"{self.n_failures} failures out of {self.n_tests} tests," if self.n_failures else 'All tests passed.'

        lowerCAmelCase_   :				str								= client.chat_postMessage(
            channel=os.environ['CI_SLACK_CHANNEL_ID_DAILY']    ,			blocks=self.payload    ,			text=SCREAMING_SNAKE_CASE_    ,			)



      def      SCREAMING_SNAKE_CASE__ (      self				:      str    ,			SCREAMING_SNAKE_CASE_				:      int    ,			SCREAMING_SNAKE_CASE_				:      Any    ,			SCREAMING_SNAKE_CASE_				:      Any    ,			SCREAMING_SNAKE_CASE_				:      Dict       ):
        lowerCAmelCase_   :				List[Any]								= ''
        for key, value in failures.items():
          lowerCAmelCase_   :				List[Any]								= value[:2_0_0] + ' [Truncated]' if len(SCREAMING_SNAKE_CASE_       ) > 2_5_0 else value
          failures_text += F"*{key}*\n_{value}_\n\n"

        lowerCAmelCase_   :				int								= job_name
        lowerCAmelCase_   :				Dict								= {'type': 'section', 'text': {'type': 'mrkdwn', 'text': text}}

        if job_link is not None:
          lowerCAmelCase_   :				str								= {
              'type': 'button',
              'text': {'type': 'plain_text', 'text': 'GitHub Action job', 'emoji': True},
              'url': job_link,
          }

        return [
            {"type": "header", "text": {"type": "plain_text", "text": title.upper(), "emoji": True}},
            content,
            {"type": "section", "text": {"type": "mrkdwn", "text": failures_text}},
        ]





      def      SCREAMING_SNAKE_CASE__ (      self				:      Union[str, Any]       ):
        if self.thread_ts is None:
          raise ValueError('Can only post reply if a post has been made.'       )

        lowerCAmelCase_   :				Dict								= self.doc_test_results.pop('job_link'       )
        self.doc_test_results.pop('failures'       )
        self.doc_test_results.pop('success'       )
        self.doc_test_results.pop('time_spent'       )

        lowerCAmelCase_   :				Dict								= sorted(self.doc_test_results.items()    ,			key=lambda SCREAMING_SNAKE_CASE_       : t[0]       )
        for job, job_result in sorted_dict:
          if len(job_result['failures']       ):
            lowerCAmelCase_   :				Tuple								= F"*Num failures* :{len(job_result['failed']       )} \n"
            lowerCAmelCase_   :				List[str]								= job_result['failures']
            lowerCAmelCase_   :				Union[str, Any]								= self.get_reply_blocks(SCREAMING_SNAKE_CASE_    ,			SCREAMING_SNAKE_CASE_    ,			SCREAMING_SNAKE_CASE_    ,			text=SCREAMING_SNAKE_CASE_       )

            print('Sending the following reply'       )
            print(json.dumps({'blocks': blocks}       )       )

            client.chat_postMessage(
                channel=os.environ['CI_SLACK_CHANNEL_ID_DAILY']    ,			text=F"Results for {job}"    ,			blocks=SCREAMING_SNAKE_CASE_    ,			thread_ts=self.thread_ts['ts']    ,			)

            time.sleep(1       )




def 				UpperCamelCase_    (    )						->   str:

  """simple docstring"""




  lowerCAmelCase_   :				Union[str, Any]								= os.environ['GITHUB_RUN_ID']
  lowerCAmelCase_   :				int								= f"https://api.github.com/repos/huggingface/transformers/actions/runs/{run_id}/jobs?per_page=100"
  lowerCAmelCase_   :				str								= requests.get(lowerCAmelCase__						).json()
  lowerCAmelCase_   :				List[str]								= {}

  try:
    jobs.update({job['name']: job['html_url'] for job in result['jobs']}						)
    lowerCAmelCase_   :				Optional[Any]								= math.ceil((result['total_count'] - 100) / 100						)

    for i in range(lowerCAmelCase__						):
      lowerCAmelCase_   :				int								= requests.get(url + f"&page={i + 2}"						).json()
      jobs.update({job['name']: job['html_url'] for job in result['jobs']}						)

    return jobs
  except Exception as e:
    print('Unknown error, could not fetch links.'    ,   lowerCAmelCase__						)

  return {}

def 				UpperCamelCase_    (    lowerCAmelCase__      :				str						)						->   List[Any]:

  """simple docstring"""




  lowerCAmelCase_   :				int								= {}

  if os.path.exists(lowerCAmelCase__						):
    lowerCAmelCase_   :				Tuple								= os.listdir(lowerCAmelCase__						)
    for file in files:
      try:
        with open(os.path.join(lowerCAmelCase__    ,   lowerCAmelCase__						)    ,   encoding='utf-8'						) as f:
          lowerCAmelCase_   :				List[str]								= f.read()
      except UnicodeDecodeError as e:
        raise ValueError(f"Could not open {os.path.join(lowerCAmelCase__    ,   lowerCAmelCase__						)}."						) from e

  return _artifact

def 				UpperCamelCase_    (    )						->   Dict:

  """simple docstring"""




  class       UpperCamelCase__   :




        """simple docstring"""




        def __init__(      self				:      Dict    ,			SCREAMING_SNAKE_CASE_				:      str       ):
          lowerCAmelCase_   :				List[Any]								= name
          lowerCAmelCase_   :				Tuple								= []



        def __str__(      self				:      Optional[Any]       ):
          return self.name





        def      SCREAMING_SNAKE_CASE__ (      self				:      List[Any]    ,			SCREAMING_SNAKE_CASE_				:      str       ):
          self.paths.append({'name': self.name, 'path': path}       )




  lowerCAmelCase_   :				Dict[str, Artifact]								= {}

  lowerCAmelCase_   :				Any								= filter(os.path.isdir    ,   os.listdir()						)
  for directory in directories:
    lowerCAmelCase_   :				int								= directory
    if artifact_name not in _available_artifacts:
      lowerCAmelCase_   :				Optional[Any]								= Artifact(lowerCAmelCase__						)

      _available_artifacts[artifact_name].add_path(lowerCAmelCase__						)

  return _available_artifacts


if __name__ == "__main__":
     lowercase__		:				Optional[int]          =	get_job_links()
     lowercase__		:				Any          =	retrieve_available_artifacts()

     lowercase__		:				str          =	collections.OrderedDict(
         [
             ("""*.py""", """API Examples"""),
             ("""*.md""", """MD Examples"""),
         ]
     )

     # This dict will contain all the information relative to each doc test category:
     # - failed: list of failed tests
     # - failures: dict in the format 'test': 'error_message'
     lowercase__		:				Dict          =	{
         v: {
             """failed""": [],
             """failures""": {},
         }
         for v in docs.values()
     }

     # Link to the GitHub Action job
     lowercase__		:				str          =	github_actions_job_links.get("""run_doctests""")

     lowercase__		:				int          =	available_artifacts["""doc_tests_gpu_test_reports"""].paths[0]
     lowercase__		:				List[str]          =	retrieve_artifact(artifact_path["""name"""])
     if "stats" in artifact:
          lowercase__    ,     lowercase__    ,     lowercase__		:				str          =	handle_test_results(artifact["""stats"""])
          lowercase__		:				Any          =	failed
          lowercase__		:				str          =	success
          lowercase__		:				int          =	time_spent[1:-1] + """, """

          lowercase__		:				Tuple          =	extract_first_line_failure(artifact["""failures_short"""])
          for line in artifact["summary_short"].split("""\n"""):
               if re.search("""FAILED""", line):
                    lowercase__		:				List[str]          =	line.replace("""FAILED """, """""")
                    lowercase__		:				Union[str, Any]          =	line.split()[0].replace("""\n""", """""")

                    if "::" in line:
                         lowercase__    ,     lowercase__		:				Optional[Any]          =	line.split("""::""")
                    else:
                         lowercase__    ,     lowercase__		:				int          =	line, line

                    for file_regex in docs.keys():
                         if fnmatch(file_path, file_regex):
                              lowercase__		:				str          =	docs[file_regex]
                              doc_test_results[category]["failed"].append(test)

                              lowercase__		:				List[Any]          =	all_failures[test] if test in all_failures else """N/A"""
                              lowercase__		:				List[Any]          =	failure
                              break

     lowercase__		:				Union[str, Any]          =	Message("""🤗 Results of the doc tests.""", doc_test_results)
     message.post()
     message.post_reply()





