


"""simple docstring"""







import numpy as np
from PIL import Image




def    UpperCAmelCase__       (lowerCAmelCase_  ,						lowerCAmelCase_  ,						lowerCAmelCase_   ):




					'''simple docstring'''


					__SCREAMING_SNAKE_CASE						   =				np.array(lowerCAmelCase_   )
					if arr.shape[0] != arr.shape[1]:
										raise ValueError("The input array is not a square matrix"   )
					__SCREAMING_SNAKE_CASE						   =				0
					__SCREAMING_SNAKE_CASE						   =				0
					__SCREAMING_SNAKE_CASE						   =				0
					__SCREAMING_SNAKE_CASE						   =				0

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

					return updated_arr




def    UpperCAmelCase__       (lowerCAmelCase_  ,						lowerCAmelCase_  ,						lowerCAmelCase_   ):




					'''simple docstring'''


					__SCREAMING_SNAKE_CASE						   =				np.array(lowerCAmelCase_   )
					if arr.shape[0] != arr.shape[1]:
										raise ValueError("The input array is not a square matrix"   )
					__SCREAMING_SNAKE_CASE						   =				0
					__SCREAMING_SNAKE_CASE						   =				0
					__SCREAMING_SNAKE_CASE						   =				0
					__SCREAMING_SNAKE_CASE						   =				0

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

					return updated_arr


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

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

		# Loading the image
		a__				:							Optional[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()




"""simple docstring"""







import unittest

from transformers.testing_utils import CaptureStdout
from transformers.tools.python_interpreter import evaluate




def    UpperCAmelCase__       (lowerCAmelCase_   ):




					'''simple docstring'''


					return x + 2




class  UpperCamelCase_	( unittest.TestCase):

	"""simple docstring"""





	def   UpperCAmelCase_      ( self			:				Any   )       ->	Any:
						__SCREAMING_SNAKE_CASE						   =				"x = 3"
						__SCREAMING_SNAKE_CASE						   =				{}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{}			,			state=UpperCAmelCase__   )
						assert result == 3
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 3}   )

						__SCREAMING_SNAKE_CASE						   =				"x = y"
						__SCREAMING_SNAKE_CASE						   =				{"y": 5}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{}			,			state=UpperCAmelCase__   )
						# evaluate returns the value of the last assignment.
						assert result == 5
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 5, "y": 5}   )




	def   UpperCAmelCase_      ( self			:				Dict   )       ->	List[str]:
						__SCREAMING_SNAKE_CASE						   =				"y = add_two(x)"
						__SCREAMING_SNAKE_CASE						   =				{"x": 3}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{"add_two": add_two}			,			state=UpperCAmelCase__   )
						assert result == 5
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 3, "y": 5}   )

						# Won't work without the tool
						with CaptureStdout() as out:
											__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{}			,			state=UpperCAmelCase__   )
						assert result is None
						assert "tried to execute add_two" in out.out




	def   UpperCAmelCase_      ( self			:				List[Any]   )       ->	List[Any]:
						__SCREAMING_SNAKE_CASE						   =				"x = 3"
						__SCREAMING_SNAKE_CASE						   =				{}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{}			,			state=UpperCAmelCase__   )
						assert result == 3
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 3}   )




	def   UpperCAmelCase_      ( self			:				str   )       ->	Any:
						__SCREAMING_SNAKE_CASE						   =				"test_dict = {'x': x, 'y': add_two(x)}"
						__SCREAMING_SNAKE_CASE						   =				{"x": 3}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{"add_two": add_two}			,			state=UpperCAmelCase__   )
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 3, "y": 5}   )
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 3, "test_dict": {"x": 3, "y": 5}}   )




	def   UpperCAmelCase_      ( self			:				int   )       ->	Optional[Any]:
						__SCREAMING_SNAKE_CASE						   =				"x = 3\ny = 5"
						__SCREAMING_SNAKE_CASE						   =				{}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{}			,			state=UpperCAmelCase__   )
						# evaluate returns the value of the last assignment.
						assert result == 5
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 3, "y": 5}   )




	def   UpperCAmelCase_      ( self			:				Any   )       ->	Any:
						__SCREAMING_SNAKE_CASE						   =				"text = f'This is x: {x}.'"
						__SCREAMING_SNAKE_CASE						   =				{"x": 3}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{}			,			state=UpperCAmelCase__   )
						# evaluate returns the value of the last assignment.
						assert result == "This is x: 3."
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 3, "text": "This is x: 3."}   )




	def   UpperCAmelCase_      ( self			:				Union[str, Any]   )       ->	Optional[int]:
						__SCREAMING_SNAKE_CASE						   =				"if x <= 3:\n    y = 2\nelse:\n    y = 5"
						__SCREAMING_SNAKE_CASE						   =				{"x": 3}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{}			,			state=UpperCAmelCase__   )
						# evaluate returns the value of the last assignment.
						assert result == 2
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 3, "y": 2}   )

						__SCREAMING_SNAKE_CASE						   =				{"x": 8}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{}			,			state=UpperCAmelCase__   )
						# evaluate returns the value of the last assignment.
						assert result == 5
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 8, "y": 5}   )




	def   UpperCAmelCase_      ( self			:				Tuple   )       ->	str:
						__SCREAMING_SNAKE_CASE						   =				"test_list = [x, add_two(x)]"
						__SCREAMING_SNAKE_CASE						   =				{"x": 3}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{"add_two": add_two}			,			state=UpperCAmelCase__   )
						self.assertListEqual(UpperCAmelCase__			,			[3, 5]   )
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 3, "test_list": [3, 5]}   )




	def   UpperCAmelCase_      ( self			:				Any   )       ->	int:
						__SCREAMING_SNAKE_CASE						   =				"y = x"
						__SCREAMING_SNAKE_CASE						   =				{"x": 3}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{}			,			state=UpperCAmelCase__   )
						assert result == 3
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 3, "y": 3}   )




	def   UpperCAmelCase_      ( self			:				Tuple   )       ->	int:
						__SCREAMING_SNAKE_CASE						   =				"test_list = [x, add_two(x)]\ntest_list[1]"
						__SCREAMING_SNAKE_CASE						   =				{"x": 3}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{"add_two": add_two}			,			state=UpperCAmelCase__   )
						assert result == 5
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 3, "test_list": [3, 5]}   )

						__SCREAMING_SNAKE_CASE						   =				"test_dict = {'x': x, 'y': add_two(x)}\ntest_dict['y']"
						__SCREAMING_SNAKE_CASE						   =				{"x": 3}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{"add_two": add_two}			,			state=UpperCAmelCase__   )
						assert result == 5
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 3, "test_dict": {"x": 3, "y": 5}}   )




	def   UpperCAmelCase_      ( self			:				List[str]   )       ->	List[str]:
						__SCREAMING_SNAKE_CASE						   =				"x = 0\nfor i in range(3):\n    x = i"
						__SCREAMING_SNAKE_CASE						   =				{}
						__SCREAMING_SNAKE_CASE						   =				evaluate(UpperCAmelCase__			,			{"range": range}			,			state=UpperCAmelCase__   )
						assert result == 2
						self.assertDictEqual(UpperCAmelCase__			,			{"x": 2, "i": 2}   )
