



import builtins
import sys

from ...utils.imports import _is_package_available
from . import cursor, input
from .helpers import Direction, clear_line, forceWrite, linebreak, move_cursor, reset_cursor, writeColor
from .keymap import KEYMAP


_lowercase			:			int										=						False
try:
		_lowercase			:			Tuple										=						_is_package_available("google.colab")
except ModuleNotFoundError:
		pass

@input.register
class 	__SCREAMING_SNAKE_CASE     :


					'''simple docstring'''





					def __init__(	self							:  Optional[int],  lowerCamelCase							:  str = None,  lowerCamelCase							:  list = []					)-> int:
								lowerCamelCase__		:		Tuple		     =0
								lowerCamelCase__		:		Optional[int]		     =choices
								lowerCamelCase__		:		Optional[int]		     =prompt
								if sys.platform == "win32":
											lowerCamelCase__		:		List[Any]		     ='''*'''
								else:
											lowerCamelCase__		:		Tuple		     ='''➔ '''



					def 		snake_case	(	self							:  Dict,  lowerCamelCase							:  Tuple,  lowerCamelCase							:  str = ""					)-> List[Any]:






								if sys.platform != "win32":
											writeColor(self.choices[index],  32,  _UpperCAmelCase					)
								else:
											forceWrite(self.choices[index],  _UpperCAmelCase					)



					def 		snake_case	(	self							:  List[str],  lowerCamelCase							:  int					)-> int:






								if index == self.position:
											forceWrite(F''' {self.arrow_char} '''					)
											self.write_choice(_UpperCAmelCase					)
								else:
											forceWrite(F'''    {self.choices[index]}'''					)
								reset_cursor()



					def 		snake_case	(	self							:  Optional[Any],  lowerCamelCase							:  Direction,  lowerCamelCase							:  int = 1					)-> List[Any]:
								lowerCamelCase__		:		str		     =self.position
								if direction == Direction.DOWN:
											if self.position + 1 >= len(self.choices					):
														return
											self.position += num_spaces
								else:
											if self.position - 1 < 0:
														return
											self.position -= num_spaces
								clear_line()
								self.print_choice(_UpperCAmelCase					)
								move_cursor(_UpperCAmelCase,  direction.name					)
								self.print_choice(self.position					)



					@input.mark(KEYMAP['''up''']					)
					def 		snake_case	(	self							:  Union[str, Any]					)-> int:
								self.move_direction(Direction.UP					)



					@input.mark(KEYMAP['''down''']					)
					def 		snake_case	(	self							:  Dict					)-> str:
								self.move_direction(Direction.DOWN					)



					@input.mark(KEYMAP['''newline''']					)
					def 		snake_case	(	self							:  Optional[Any]					)-> Dict:
								move_cursor(len(self.choices					) - self.position,  '''DOWN'''					)
								return self.position



					@input.mark(KEYMAP['''interrupt''']					)
					def 		snake_case	(	self							:  Optional[int]					)-> str:
								move_cursor(len(self.choices					) - self.position,  '''DOWN'''					)
								raise KeyboardInterrupt



					@input.mark_multiple(*[KEYMAP[str(_UpperCAmelCase					)] for number in range(10					)]					)
					def 		snake_case	(	self							:  Tuple					)-> Dict:
								lowerCamelCase__		:		str		     =int(chr(self.current_selection					)					)
								lowerCamelCase__		:		Optional[Any]		     =index - self.position
								if index == self.position:
											return
								if index < len(self.choices					):
											if self.position > index:
														self.move_direction(Direction.UP,  -movement					)
											elif self.position < index:
														self.move_direction(Direction.DOWN,  _UpperCAmelCase					)
											else:
														return
								else:
											return




					def 		snake_case	(	self							:  int,  lowerCamelCase							:  int = 0					)-> Union[str, Any]:






								if self.prompt:
											linebreak()
											forceWrite(self.prompt,  '''\n'''					)
											if in_colab:
														forceWrite('''Please input a choice index (starting from 0), and press enter''',  '''\n'''					)
											else:
														forceWrite('''Please select a choice using the arrow or number keys, and selecting with enter''',  '''\n'''					)
								lowerCamelCase__		:		List[str]		     =default_choice
								for i in range(len(self.choices					)					):
											self.print_choice(_UpperCAmelCase					)
											forceWrite('''\n'''					)
								move_cursor(len(self.choices					) - self.position,  '''UP'''					)
								with cursor.hide():
											while True:
														if in_colab:
																	try:
																				lowerCamelCase__		:		Union[str, Any]		     =int(builtins.input()					)
																	except ValueError:
																				lowerCamelCase__		:		str		     =default_choice
														else:
																	lowerCamelCase__		:		Dict		     =self.handle_input()
														if choice is not None:
																	reset_cursor()
																	for _ in range(len(self.choices					) + 1					):
																				move_cursor(1,  '''UP'''					)
																				clear_line()
																	self.write_choice(_UpperCAmelCase,  '''\n'''					)
																	return choice









"""simple docstring"""






import numpy as np
from PIL import Image






def 					snake_case__       (		__lowerCamelCase    :   np.ndarray							,		__lowerCamelCase    :   int							,		__lowerCamelCase    :   int     ):
			"""simple docstring"""
			lowerCamelCase__		:		List[Any]		     =np.array(__lowerCamelCase     )
			if arr.shape[0] != arr.shape[1]:
						raise ValueError('''The input array is not a square matrix'''     )
			lowerCamelCase__		:		int		     =0
			lowerCamelCase__		:		int		     =0
			lowerCamelCase__		:		Optional[int]		     =0
			lowerCamelCase__		:		List[Any]		     =0

			# compute the shape of the output matrix
			lowerCamelCase__		:		Union[str, Any]		     =(arr.shape[0] - size) // stride + 1
			# initialize the output matrix with zeros of shape maxpool_shape
			lowerCamelCase__		:		Union[str, Any]		     =np.zeros((maxpool_shape, maxpool_shape)     )

			while i < arr.shape[0]:
						if i + size > arr.shape[0]:
									# if the end of the matrix is reached, break
									break
						while j < arr.shape[1]:
									# if the end of the matrix is reached, break
									if j + size > arr.shape[1]:
												break
									# compute the maximum of the pooling matrix
									lowerCamelCase__		:		str		     =np.max(arr[i : i + size, j : j + size]     )
									# shift the pooling matrix by stride of column pixels
									j += stride
									mat_j += 1

						# shift the pooling matrix by stride of row pixels
						i += stride
						mat_i += 1

						# reset the column index to 0
						lowerCamelCase__		:		Optional[int]		     =0
						lowerCamelCase__		:		Optional[int]		     =0

			return updated_arr






def 					snake_case__       (		__lowerCamelCase    :   np.ndarray							,		__lowerCamelCase    :   int							,		__lowerCamelCase    :   int     ):
			"""simple docstring"""
			lowerCamelCase__		:		List[Any]		     =np.array(__lowerCamelCase     )
			if arr.shape[0] != arr.shape[1]:
						raise ValueError('''The input array is not a square matrix'''     )
			lowerCamelCase__		:		str		     =0
			lowerCamelCase__		:		List[Any]		     =0
			lowerCamelCase__		:		Optional[int]		     =0
			lowerCamelCase__		:		List[Any]		     =0

			# compute the shape of the output matrix
			lowerCamelCase__		:		Dict		     =(arr.shape[0] - size) // stride + 1
			# initialize the output matrix with zeros of shape avgpool_shape
			lowerCamelCase__		:		Optional[Any]		     =np.zeros((avgpool_shape, avgpool_shape)     )

			while i < arr.shape[0]:
						# if the end of the matrix is reached, break
						if i + size > arr.shape[0]:
									break
						while j < arr.shape[1]:
									# if the end of the matrix is reached, break
									if j + size > arr.shape[1]:
												break
									# compute the average of the pooling matrix
									lowerCamelCase__		:		Optional[int]		     =int(np.average(arr[i : i + size, j : j + size]     )     )
									# shift the pooling matrix by stride of column pixels
									j += stride
									mat_j += 1

						# shift the pooling matrix by stride of row pixels
						i += stride
						mat_i += 1
						# reset the column index to 0
						lowerCamelCase__		:		Optional[Any]		     =0
						lowerCamelCase__		:		int		     =0

			return updated_arr


# Main Function
if __name__ == "__main__":
		from doctest import testmod

		testmod(name="avgpooling", verbose=True)

		# Loading the image
		_lowercase			:			int										=						Image.open("path_to_image")

		# Converting the image to numpy array and maxpooling, displaying the result
		# Ensure that the image is a square matrix

		Image.fromarray(maxpooling(np.array(image), size=3, stride=2)).show()

		# Converting the image to numpy array and averagepooling, displaying the result
		# Ensure that the image is a square matrix

		Image.fromarray(avgpooling(np.array(image), size=3, stride=2)).show()




