






from unittest import TestCase

from datasets import Sequence, Value
from datasets.arrow_dataset import Dataset


class     __SCREAMING_SNAKE_CASE					(   __lowercase):






						def       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												return [
												    {"col_1": 3, "col_2": "a"},
												    {"col_1": 2, "col_2": "b"},
												    {"col_1": 1, "col_2": "c"},
												    {"col_1": 0, "col_2": "d"},
												]






						def       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						{'col_1': [3, 2, 1, 0], 'col_2': ['a', 'b', 'c', 'd']}
												return Dataset.from_dict(_UpperCamelCase   )






						def       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						self._create_example_records()
												lowerCAmelCase__    						=						Dataset.from_list(_UpperCamelCase   )
												self.assertListEqual(dset.column_names     ,						['col_1', 'col_2']   )
												for i, r in enumerate(_UpperCamelCase   ):
																		self.assertDictEqual(_UpperCamelCase     ,						example_records[i]   )






						def       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						self._create_example_records()
												lowerCAmelCase__    						=						Dataset.from_list(_UpperCamelCase   )
												lowerCAmelCase__    						=						Dataset.from_dict({k: [r[k] for r in example_records] for k in example_records[0]}   )
												self.assertEqual(dset.info     ,						dset_from_dict.info   )






						def       UpperCamelCase__      (	self   ):  # checks what happens with missing columns






												"""simple docstring"""
												lowerCAmelCase__    						=						[{'col_1': 1}, {'col_2': 'x'}]
												lowerCAmelCase__    						=						Dataset.from_list(_UpperCamelCase   )
												self.assertDictEqual(dset[0]     ,						{'col_1': 1}   )
												self.assertDictEqual(dset[1]     ,						{'col_1': None}   )  # NB: first record is used for columns






						def       UpperCamelCase__      (	self   ):  # checks if the type can be inferred from the second record






												"""simple docstring"""
												lowerCAmelCase__    						=						[{'col_1': []}, {'col_1': [1, 2]}]
												lowerCAmelCase__    						=						Dataset.from_list(_UpperCamelCase   )
												self.assertEqual(dset.info.features['col_1']     ,						Sequence(Value('int64'   )   )   )







						def       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						Dataset.from_list([]   )
												self.assertEqual(len(_UpperCamelCase   )     ,						0   )
												self.assertListEqual(dset.column_names     ,						[]   )













import numpy as np
from PIL import Image







def 	_UpperCamelCase   (    UpperCamelCase_       :					np.ndarray    ,       UpperCamelCase_       :					int    ,       UpperCamelCase_       :					int		)  ->   np.ndarray:

						"""simple docstring"""



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

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

						return updated_arr







def 	_UpperCamelCase   (    UpperCamelCase_       :					np.ndarray    ,       UpperCamelCase_       :					int    ,       UpperCamelCase_       :					int		)  ->   np.ndarray:

						"""simple docstring"""



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

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

						return updated_arr


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

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

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





