"""simple docstring"""
from typing import List, Optional, TypeVar

from .arrow_dataset import Dataset, _concatenate_map_style_datasets, _interleave_map_style_datasets
from .dataset_dict import DatasetDict, IterableDatasetDict
from .info import DatasetInfo
from .iterable_dataset import IterableDataset, _concatenate_iterable_datasets, _interleave_iterable_datasets
from .splits import NamedSplit
from .utils import logging
from .utils.py_utils import Literal


SCREAMING_SNAKE_CASE__      						=  logging.get_logger(__name__)


SCREAMING_SNAKE_CASE__      						=  TypeVar("DatasetType", Dataset, IterableDataset)





def    lowerCAmelCase__    (			_UpperCamelCase       :				List[DatasetType] ,			_UpperCamelCase       :				Optional[List[float]] = None ,			_UpperCamelCase       :				Optional[int] = None ,			_UpperCamelCase       :				Optional[DatasetInfo] = None ,			_UpperCamelCase       :				Optional[NamedSplit] = None ,			_UpperCamelCase       :				Literal["first_exhausted", "all_exhausted"] = "first_exhausted" ,			)					->				DatasetType:





						"""simple docstring"""






						from .arrow_dataset import Dataset
						from .iterable_dataset import IterableDataset

						if not datasets:
												raise ValueError('Unable to interleave an empty list of datasets.'					)
						for i, dataset in enumerate(_UpperCamelCase					):
												if not isinstance(_UpperCamelCase ,			(Dataset, IterableDataset)					):
																		if isinstance(_UpperCamelCase ,			(DatasetDict, IterableDatasetDict)					):
																								if not dataset:
																														raise ValueError(
																														    f"""Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} """
																														    'is an empty dataset dictionary.'					)
																								raise ValueError(
																								    f"""Dataset at position {i} has at least one split: {list(_UpperCamelCase					)}\n"""
																								    f"""Please pick one to interleave with the other datasets, for example: dataset['{next(iter(_UpperCamelCase					)					)}']"""					)
																		raise ValueError(
																		    f"""Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} is a {type(_UpperCamelCase					).__name__}."""					)
												if i == 0:
																		snake_case					,snake_case								=			(
																		    (Dataset, IterableDataset) if isinstance(_UpperCamelCase ,			_UpperCamelCase					) else (IterableDataset, Dataset)
																		)
												elif not isinstance(_UpperCamelCase ,			_UpperCamelCase					):
																		raise ValueError(
																		    f"""Unable to interleave a {dataset_type.__name__} (at position 0) with a {other_type.__name__} (at position {i}). Expected a list of Dataset objects or a list of IterableDataset objects."""					)
						if stopping_strategy not in ["first_exhausted", "all_exhausted"]:
												raise ValueError(f"""{stopping_strategy} is not supported. Please enter a valid stopping_strategy."""					)
						if dataset_type is Dataset:
												return _interleave_map_style_datasets(
												    _UpperCamelCase ,			_UpperCamelCase ,			_UpperCamelCase ,			info=_UpperCamelCase ,			split=_UpperCamelCase ,			stopping_strategy=_UpperCamelCase					)
						else:
												return _interleave_iterable_datasets(
												    _UpperCamelCase ,			_UpperCamelCase ,			_UpperCamelCase ,			info=_UpperCamelCase ,			split=_UpperCamelCase ,			stopping_strategy=_UpperCamelCase					)





def    lowerCAmelCase__    (			_UpperCamelCase       :				List[DatasetType] ,			_UpperCamelCase       :				Optional[DatasetInfo] = None ,			_UpperCamelCase       :				Optional[NamedSplit] = None ,			_UpperCamelCase       :				int = 0 ,			)					->				DatasetType:





						"""simple docstring"""






						if not dsets:
												raise ValueError('Unable to concatenate an empty list of datasets.'					)
						for i, dataset in enumerate(_UpperCamelCase					):
												if not isinstance(_UpperCamelCase ,			(Dataset, IterableDataset)					):
																		if isinstance(_UpperCamelCase ,			(DatasetDict, IterableDatasetDict)					):
																								if not dataset:
																														raise ValueError(
																														    f"""Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} """
																														    'is an empty dataset dictionary.'					)
																								raise ValueError(
																								    f"""Dataset at position {i} has at least one split: {list(_UpperCamelCase					)}\n"""
																								    f"""Please pick one to interleave with the other datasets, for example: dataset['{next(iter(_UpperCamelCase					)					)}']"""					)
																		raise ValueError(
																		    f"""Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} is a {type(_UpperCamelCase					).__name__}."""					)
												if i == 0:
																		snake_case					,snake_case								=			(
																		    (Dataset, IterableDataset) if isinstance(_UpperCamelCase ,			_UpperCamelCase					) else (IterableDataset, Dataset)
																		)
												elif not isinstance(_UpperCamelCase ,			_UpperCamelCase					):
																		raise ValueError(
																		    f"""Unable to interleave a {dataset_type.__name__} (at position 0) with a {other_type.__name__} (at position {i}). Expected a list of Dataset objects or a list of IterableDataset objects."""					)
						if dataset_type is Dataset:
												return _concatenate_map_style_datasets(_UpperCamelCase ,			info=_UpperCamelCase ,			split=_UpperCamelCase ,			axis=_UpperCamelCase					)
						else:
												return _concatenate_iterable_datasets(_UpperCamelCase ,			info=_UpperCamelCase ,			split=_UpperCamelCase ,			axis=_UpperCamelCase					)



"""simple docstring"""
import numpy as np
from PIL import Image





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





						"""simple docstring"""






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

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

						return updated_arr





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





						"""simple docstring"""






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

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

						return updated_arr


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

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

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


