

'''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


A			      =      WebClient(token=os.environ['CI_SLACK_BOT_TOKEN'])





def    UpperCAmelCase							(  UpperCAmelCase__							:							Tuple):
				lowerCamelCase					:			List[str]									= test_results.split(' ')

				lowerCamelCase					:			Optional[Any]									= 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					:			Optional[Any]									= expressions[-2] if '=' in expressions[-1] else expressions[-1]

				for i, expression in enumerate(UpperCAmelCase__):
								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							(  UpperCAmelCase__							:							Optional[int]):
				lowerCamelCase					:			Union[str, Any]									= {}
				lowerCamelCase					:			str									= None
				lowerCamelCase					:			Dict									= False
				for line in failures_short_lines.split('\n'):
								if re.search(R'_ \[doctest\]'       ,							UpperCAmelCase__):
												lowerCamelCase					:			Any									= True
												lowerCamelCase					:			Dict									= line.split(' ')[2]
								elif in_error and not line.split(' ')[0].isdigit():
												lowerCamelCase					:			int									= line
												lowerCamelCase					:			Dict									= False

				return failures




class     __snake_case		:
					def __init__(     self,    A,    A      ):

									"""simple docstring"""


									lowerCamelCase					:			Union[str, Any]									= title

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

									# Failures and success of the modeling tests
									lowerCamelCase					:			str									= doc_test_results
					@property
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			List[str]									= [self._time_spent]
									lowerCamelCase					:			List[str]									= 0

									for time in time_spent:
													lowerCamelCase					:			str									= 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(A      ) == 1:
																	lowerCamelCase					:			int									= [0, 0, time_parts[0]]

													lowerCamelCase					:			Optional[int]									= int(time_parts[0]      ), int(time_parts[1]      ), float(time_parts[2]      )
													total_secs += hours * 3600 + minutes * 60 + seconds

									lowerCamelCase					:			Any									= total_secs // 3600, (total_secs % 3600) // 60, total_secs % 60
									return F'''{int(A      )}h{int(A      )}m{int(A      )}s'''
					@property
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


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

									"""simple docstring"""


									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     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									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     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			Optional[int]									= 40
									lowerCamelCase					:			int									= {k: v['failed'] for k, v in doc_test_results.items() if isinstance(A,    A      )}

									lowerCamelCase					:			Dict									= ''
									for category, failures in category_failures.items():
													if len(A      ) == 0:
																	continue

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

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

									return {
									    "type": "section",
									    "text": {
									        "type": "mrkdwn",
									        "text": F'''The following examples had failures:\n\n\n{report}\n''',
									    },
									}
					@property
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			Dict									= [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(A      )
					@staticmethod
					def     UpperCAmelCase_							(     ):

									"""simple docstring"""


									lowerCamelCase					:			str									= [
									    {
									        '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(A      )}      )      )

									client.chat_postMessage(
									    channel=os.environ['CI_SLACK_CHANNEL_ID_DAILY'],    text='There was an issue running the tests.',    blocks=A,    )
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


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

									lowerCamelCase					:			Optional[Any]									= F'''{self.n_failures} failures out of {self.n_tests} tests,''' if self.n_failures else 'All tests passed.'

									lowerCamelCase					:			List[Any]									= client.chat_postMessage(
									    channel=os.environ['CI_SLACK_CHANNEL_ID_DAILY'],    blocks=self.payload,    text=A,    )
					def     UpperCAmelCase_							(     self,    A,    A,    A,    A      ):

									"""simple docstring"""


									lowerCamelCase					:			Any									= ''
									for key, value in failures.items():
													lowerCamelCase					:			Dict									= value[:200] + ' [Truncated]' if len(A      ) > 250 else value
													failures_text += F'''*{key}*\n_{value}_\n\n'''

									lowerCamelCase					:			Tuple									= job_name
									lowerCamelCase					:			List[Any]									= {'type': 'section', 'text': {'type': 'mrkdwn', 'text': text}}

									if job_link is not None:
													lowerCamelCase					:			Any									= {
													    '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     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									if self.thread_ts is None:
													raise ValueError('Can only post reply if a post has been made.'      )

									lowerCamelCase					:			Tuple									= 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 A      : t[0]      )
									for job, job_result in sorted_dict:
													if len(job_result['failures']      ):
																	lowerCamelCase					:			str									= F'''*Num failures* :{len(job_result['failed']      )} \n'''
																	lowerCamelCase					:			Optional[int]									= job_result['failures']
																	lowerCamelCase					:			Optional[int]									= self.get_reply_blocks(A,    A,    A,    text=A      )

																	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=A,    thread_ts=self.thread_ts['ts'],    )

																	time.sleep(1      )





def    UpperCAmelCase							(  ):
				lowerCamelCase					:			List[str]									= os.environ['GITHUB_RUN_ID']
				lowerCamelCase					:			Tuple									= F'''https://api.github.com/repos/huggingface/transformers/actions/runs/{run_id}/jobs?per_page=100'''
				lowerCamelCase					:			Optional[int]									= requests.get(UpperCAmelCase__).json()
				lowerCamelCase					:			Dict									= {}

				try:
								jobs.update({job['name']: job['html_url'] for job in result['jobs']})
								lowerCamelCase					:			Optional[int]									= math.ceil((result['total_count'] - 1_00) / 1_00)

								for i in range(UpperCAmelCase__):
												lowerCamelCase					:			List[Any]									= 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.'       ,							UpperCAmelCase__)

				return {}





def    UpperCAmelCase							(  UpperCAmelCase__							:							str):
				lowerCamelCase					:			Any									= {}

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

				return _artifact





def    UpperCAmelCase							(  ):




				class     __snake_case		:
									def __init__(     self,    A      ):

													"""simple docstring"""


													lowerCamelCase					:			Union[str, Any]									= name
													lowerCamelCase					:			Optional[Any]									= []
									def __str__(     self      ):

													"""simple docstring"""


													return self.name




									def     UpperCAmelCase_							(     self,    A      ):

													"""simple docstring"""


													self.paths.append({'name': self.name, 'path': path}      )





				lowerCamelCase					:			Dict[str, Artifact]									= {}

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

												_available_artifacts[artifact_name].add_path(UpperCAmelCase__)

				return _available_artifacts


if __name__ == "__main__":
					A			      =      get_job_links()
					A			      =      retrieve_available_artifacts()

					A			      =      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'
					A			      =      {
					    v: {
					        'failed': [],
					        'failures': {},
					    }
					    for v in docs.values()
					}

					# Link to the GitHub Action job
					A			      =      github_actions_job_links.get('run_doctests')

					A			      =      available_artifacts['doc_tests_gpu_test_reports'].paths[0]
					A			      =      retrieve_artifact(artifact_path['name'])
					if "stats" in artifact:
										A      ,			A      ,			A			      =      handle_test_results(artifact['stats'])
										A			      =      failed
										A			      =      success
										A			      =      time_spent[1:-1] + ', '

										A			      =      extract_first_line_failure(artifact['failures_short'])
										for line in artifact["summary_short"].split('\n'):
															if re.search('FAILED', line):
																				A			      =      line.replace('FAILED ', '')
																				A			      =      line.split()[0].replace('\n', '')

																				if "::" in line:
																									A      ,			A			      =      line.split('::')
																				else:
																									A      ,			A			      =      line, line

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

																														A			      =      all_failures[test] if test in all_failures else 'N/A'
																														A			      =      failure
																														break

					A			      =      Message('🤗 Results of the doc tests.', doc_test_results)
					message.post()
					message.post_reply()


'''simple docstring'''






# Imports
import numpy as np




class     __snake_case		:
					def __init__(     self,    A=None,    A=None,    A=None,    A=None,    A=None      ):

									"""simple docstring"""


									self.set_matricies(red=A,    green=A,    blue=A,    red_edge=A,    nir=A      )
					def     UpperCAmelCase_							(     self,    A=None,    A=None,    A=None,    A=None,    A=None      ):

									"""simple docstring"""


									if red is not None:
													lowerCamelCase					:			Optional[int]									= red
									if green is not None:
													lowerCamelCase					:			Optional[int]									= green
									if blue is not None:
													lowerCamelCase					:			List[str]									= blue
									if red_edge is not None:
													lowerCamelCase					:			Tuple									= red_edge
									if nir is not None:
													lowerCamelCase					:			Any									= nir
									return True
					def     UpperCAmelCase_							(     self,    A="",    A=None,    A=None,    A=None,    A=None,    A=None      ):

									"""simple docstring"""


									self.set_matricies(red=A,    green=A,    blue=A,    red_edge=A,    nir=A      )
									lowerCamelCase					:			Optional[int]									= {
									    'ARVI2': self.arvaa,
									    'CCCI': self.ccci,
									    'CVI': self.cvi,
									    'GLI': self.gli,
									    'NDVI': self.ndvi,
									    'BNDVI': self.bndvi,
									    'redEdgeNDVI': self.red_edge_ndvi,
									    'GNDVI': self.gndvi,
									    'GBNDVI': self.gbndvi,
									    'GRNDVI': self.grndvi,
									    'RBNDVI': self.rbndvi,
									    'PNDVI': self.pndvi,
									    'ATSAVI': self.atsavi,
									    'BWDRVI': self.bwdrvi,
									    'CIgreen': self.ci_green,
									    'CIrededge': self.ci_rededge,
									    'CI': self.ci,
									    'CTVI': self.ctvi,
									    'GDVI': self.gdvi,
									    'EVI': self.evi,
									    'GEMI': self.gemi,
									    'GOSAVI': self.gosavi,
									    'GSAVI': self.gsavi,
									    'Hue': self.hue,
									    'IVI': self.ivi,
									    'IPVI': self.ipvi,
									    'I': self.i,
									    'RVI': self.rvi,
									    'MRVI': self.mrvi,
									    'MSAVI': self.m_savi,
									    'NormG': self.norm_g,
									    'NormNIR': self.norm_nir,
									    'NormR': self.norm_r,
									    'NGRDI': self.ngrdi,
									    'RI': self.ri,
									    'S': self.s,
									    'IF': self._if,
									    'DVI': self.dvi,
									    'TVI': self.tvi,
									    'NDRE': self.ndre,
									}

									try:
													return funcs[index]()
									except KeyError:
													print('Index not in the list!'      )
													return False
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return -0.18 + (1.17 * ((self.nir - self.red) / (self.nir + self.red)))
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return ((self.nir - self.redEdge) / (self.nir + self.redEdge)) / (
									    (self.nir - self.red) / (self.nir + self.red)
									)
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return self.nir * (self.red / (self.green**2))
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return (2 * self.green - self.red - self.blue) / (
									    2 * self.green + self.red + self.blue
									)
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return (self.nir - self.red) / (self.nir + self.red)
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return (self.nir - self.blue) / (self.nir + self.blue)
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return (self.redEdge - self.red) / (self.redEdge + self.red)
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return (self.nir - self.green) / (self.nir + self.green)
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return (self.nir - (self.green + self.blue)) / (
									    self.nir + (self.green + self.blue)
									)
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return (self.nir - (self.green + self.red)) / (
									    self.nir + (self.green + self.red)
									)
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return (self.nir - (self.blue + self.red)) / (self.nir + (self.blue + self.red))
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return (self.nir - (self.green + self.red + self.blue)) / (
									    self.nir + (self.green + self.red + self.blue)
									)
					def     UpperCAmelCase_							(     self,    A=0.08,    A=1.22,    A=0.03      ):

									"""simple docstring"""


									return a * (
									    (self.nir - a * self.red - b)
									    / (a * self.nir + self.red - a * b + x * (1 + a**2))
									)
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return (0.1 * self.nir - self.blue) / (0.1 * self.nir + self.blue)
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return (self.nir / self.green) - 1
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return (self.nir / self.redEdge) - 1
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return (self.red - self.blue) / self.red
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			Union[str, Any]									= self.ndvi()
									return ((ndvi + 0.5) / (abs(ndvi + 0.5      ))) * (abs(ndvi + 0.5      ) ** (1 / 2))
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return self.nir - self.green
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return 2.5 * (
									    (self.nir - self.red) / (self.nir + 6 * self.red - 7.5 * self.blue + 1)
									)
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			str									= (2 * (self.nir**2 - self.red**2) + 1.5 * self.nir + 0.5 * self.red) / (
									    self.nir + self.red + 0.5
									)
									return n * (1 - 0.25 * n) - (self.red - 0.125) / (1 - self.red)
					def     UpperCAmelCase_							(     self,    A=0.16      ):

									"""simple docstring"""


									return (self.nir - self.green) / (self.nir + self.green + y)
					def     UpperCAmelCase_							(     self,    A=0.5      ):

									"""simple docstring"""


									return ((self.nir - self.green) / (self.nir + self.green + n)) * (1 + n)
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return np.arctan(
									    ((2 * self.red - self.green - self.blue) / 30.5) * (self.green - self.blue)      )
					def     UpperCAmelCase_							(     self,    A=None,    A=None      ):

									"""simple docstring"""


									return (self.nir - b) / (a * self.red)
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return (self.nir / ((self.nir + self.red) / 2)) * (self.ndvi() + 1)
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return (self.red + self.green + self.blue) / 30.5
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return self.nir / self.red
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return (self.rvi() - 1) / (self.rvi() + 1)
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return (
									    (2 * self.nir + 1)
									    - ((2 * self.nir + 1) ** 2 - 8 * (self.nir - self.red)) ** (1 / 2)
									) / 2
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return self.green / (self.nir + self.red + self.green)
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return self.nir / (self.nir + self.red + self.green)
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return self.red / (self.nir + self.red + self.green)
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return (self.green - self.red) / (self.green + self.red)
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return (self.red - self.green) / (self.red + self.green)
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			Optional[Any]									= np.max([np.max(self.red      ), np.max(self.green      ), np.max(self.blue      )]      )
									lowerCamelCase					:			Tuple									= np.min([np.min(self.red      ), np.min(self.green      ), np.min(self.blue      )]      )
									return (max_value - min_value) / max_value
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return (2 * self.red - self.green - self.blue) / (self.green - self.blue)
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return self.nir / self.red
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return (self.ndvi() + 0.5) ** (1 / 2)




					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return (self.nir - self.redEdge) / (self.nir + self.redEdge)
