






'''simple docstring'''





import numpy as np
from PIL import Image

def      __lowercase					( snake_case,  snake_case,  snake_case ):







				"""simple docstring"""






				__magic_name__		:Optional[int]        =      np.array(lowerCamelCase_ )
				if arr.shape[0] != arr.shape[1]:
								raise ValueError('''The input array is not a square matrix''' )
				__magic_name__		:Tuple        =      0
				__magic_name__		:Optional[Any]        =      0
				__magic_name__		:str        =      0
				__magic_name__		:str        =      0

				# compute the shape of the output matrix
				__magic_name__		:Any        =      (arr.shape[0] - size) // stride + 1
				# initialize the output matrix with zeros of shape maxpool_shape
				__magic_name__		:List[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
												__magic_name__		:Optional[Any]        =      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
								__magic_name__		:List[str]        =      0
								__magic_name__		:Optional[Any]        =      0

				return updated_arr

def      __lowercase					( snake_case,  snake_case,  snake_case ):







				"""simple docstring"""






				__magic_name__		:Any        =      np.array(lowerCamelCase_ )
				if arr.shape[0] != arr.shape[1]:
								raise ValueError('''The input array is not a square matrix''' )
				__magic_name__		:Optional[Any]        =      0
				__magic_name__		:int        =      0
				__magic_name__		:int        =      0
				__magic_name__		:Union[str, Any]        =      0

				# compute the shape of the output matrix
				__magic_name__		:Any        =      (arr.shape[0] - size) // stride + 1
				# initialize the output matrix with zeros of shape avgpool_shape
				__magic_name__		: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
												__magic_name__		:List[Any]        =      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
								__magic_name__		:List[Any]        =      0
								__magic_name__		:Any        =      0

				return updated_arr


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

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

					# Loading the image
					SCREAMING_SNAKE_CASE__      :			str							=		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()














from typing import List

from .keymap import KEYMAP, get_character

def      __lowercase					( snake_case ):







				"""simple docstring"""






				def decorator(snake_case ):
								__magic_name__		:int        =      getattr(snake_case,  '''handle_key''',  [] )
								handle += [key]
								setattr(snake_case,  '''handle_key''',  snake_case )
								return func

				return decorator

def      __lowercase					( *snake_case ):







				"""simple docstring"""






				def decorator(snake_case ):
								__magic_name__		:Union[str, Any]        =      getattr(snake_case,  '''handle_key''',  [] )
								handle += keys
								setattr(snake_case,  '''handle_key''',  snake_case )
								return func

				return decorator
class 						lowerCamelCase_				(      lowerCamelCase ):







				def __new__(						cls       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase     ):
								"""simple docstring"""
								__magic_name__		:Optional[int]        =      super().__new__(cls       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase     )
								if not hasattr(__lowerCAmelCase       ,					'''key_handler'''     ):
												setattr(__lowerCAmelCase       ,					'''key_handler'''       ,					{}     )
								setattr(__lowerCAmelCase       ,					'''handle_input'''       ,					KeyHandler.handle_input     )

								for value in attrs.values():
												__magic_name__		:Dict        =      getattr(__lowerCAmelCase       ,					'''handle_key'''       ,					[]     )
												for key in handled_keys:
																__magic_name__		:List[Any]        =      value
								return new_cls







				@staticmethod
				def 			A	(						cls     ):
								"""simple docstring"""
								__magic_name__		:Tuple        =      get_character()
								if char != KEYMAP["undefined"]:
												__magic_name__		:Dict        =      ord(__lowerCAmelCase     )
								__magic_name__		:str        =      cls.key_handler.get(__lowerCAmelCase     )
								if handler:
												__magic_name__		:Optional[Any]        =      char
												return handler(cls     )
								else:
												return None







def      __lowercase					( cls ):







				"""simple docstring"""






				return KeyHandler(cls.__name__,  cls.__bases__,  cls.__dict__.copy() )






