



"""simple docstring"""






import contextlib
import os
import sqlitea

import pytest

from datasets import Dataset, Features, Value
from datasets.io.sql import SqlDatasetReader, SqlDatasetWriter

from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases, require_sqlalchemy






def 					snake_case__       (		__lowerCamelCase    :   Any							,		__lowerCamelCase    :   Optional[int]     ):
			"""simple docstring"""
			assert isinstance(__lowerCamelCase							,		__lowerCamelCase     )
			assert dataset.num_rows == 4
			assert dataset.num_columns == 3
			assert dataset.column_names == ["col_1", "col_2", "col_3"]
			for feature, expected_dtype in expected_features.items():
						assert dataset.features[feature].dtype == expected_dtype






@require_sqlalchemy
@pytest.mark.parametrize('''keep_in_memory'''							,		[False, True]     )
def 					snake_case__       (		__lowerCamelCase    :   Dict							,		__lowerCamelCase    :   str							,		__lowerCamelCase    :   str							,		__lowerCamelCase    :   str     ):
			"""simple docstring"""
			lowerCamelCase__		:		Optional[Any]		     =tmp_path / '''cache'''
			lowerCamelCase__		:		Dict		     ={'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
			with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
						lowerCamelCase__		:		Dict		     =SqlDatasetReader(
						    '''dataset'''							,		'''sqlite:///''' + sqlite_path							,		cache_dir=__lowerCamelCase							,		keep_in_memory=__lowerCamelCase     ).read()
			_check_sql_dataset(__lowerCamelCase							,		__lowerCamelCase     )






@require_sqlalchemy
@pytest.mark.parametrize(
    '''features'''							,		[
        None,
        {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''},
        {'''col_1''': '''string''', '''col_2''': '''string''', '''col_3''': '''string'''},
        {'''col_1''': '''int32''', '''col_2''': '''int32''', '''col_3''': '''int32'''},
        {'''col_1''': '''float32''', '''col_2''': '''float32''', '''col_3''': '''float32'''},
    ]							,		)
def 					snake_case__       (		__lowerCamelCase    :   Optional[int]							,		__lowerCamelCase    :   Tuple							,		__lowerCamelCase    :   str							,		__lowerCamelCase    :   Union[str, Any]     ):
			"""simple docstring"""
			lowerCamelCase__		:		Optional[Any]		     =tmp_path / '''cache'''
			lowerCamelCase__		:		Optional[int]		     ={'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
			lowerCamelCase__		:		Any		     =features.copy() if features else default_expected_features
			lowerCamelCase__		:		List[str]		     =(
			    Features({feature: Value(__lowerCamelCase     ) for feature, dtype in features.items()}     ) if features is not None else None
			)
			lowerCamelCase__		:		List[str]		     =SqlDatasetReader('''dataset'''							,		'''sqlite:///''' + sqlite_path							,		features=__lowerCamelCase							,		cache_dir=__lowerCamelCase     ).read()
			_check_sql_dataset(__lowerCamelCase							,		__lowerCamelCase     )






def 					snake_case__       (		__lowerCamelCase    :   Optional[int]     ):
			"""simple docstring"""
			with contextlib.closing(sqlitea.connect(__lowerCamelCase     )     ) as con:
						lowerCamelCase__		:		Union[str, Any]		     =con.cursor()
						cur.execute('''SELECT * FROM dataset'''     )
						for row in cur:
									yield row






@require_sqlalchemy
def 					snake_case__       (		__lowerCamelCase    :   str							,		__lowerCamelCase    :   Union[str, Any]							,		__lowerCamelCase    :   List[str]     ):
			"""simple docstring"""
			lowerCamelCase__		:		Union[str, Any]		     =tmp_path / '''cache'''
			lowerCamelCase__		:		Tuple		     =os.path.join(__lowerCamelCase							,		'''tmp.sql'''     )
			lowerCamelCase__		:		List[Any]		     =SqlDatasetReader('''dataset'''							,		'''sqlite:///''' + sqlite_path							,		cache_dir=__lowerCamelCase     ).read()
			SqlDatasetWriter(__lowerCamelCase							,		'''dataset'''							,		'''sqlite:///''' + output_sqlite_path							,		num_proc=1     ).write()

			lowerCamelCase__		:		Optional[int]		     =iter_sql_file(__lowerCamelCase     )
			lowerCamelCase__		:		str		     =iter_sql_file(__lowerCamelCase     )

			for rowa, rowa in zip(__lowerCamelCase							,		__lowerCamelCase     ):
						assert rowa == rowa






@require_sqlalchemy
def 					snake_case__       (		__lowerCamelCase    :   str							,		__lowerCamelCase    :   Union[str, Any]							,		__lowerCamelCase    :   Tuple     ):
			"""simple docstring"""
			lowerCamelCase__		:		str		     =tmp_path / '''cache'''
			lowerCamelCase__		:		Any		     =os.path.join(__lowerCamelCase							,		'''tmp.sql'''     )
			lowerCamelCase__		:		Optional[Any]		     =SqlDatasetReader('''dataset'''							,		'''sqlite:///''' + sqlite_path							,		cache_dir=__lowerCamelCase     ).read()
			SqlDatasetWriter(__lowerCamelCase							,		'''dataset'''							,		'''sqlite:///''' + output_sqlite_path							,		num_proc=2     ).write()

			lowerCamelCase__		:		Tuple		     =iter_sql_file(__lowerCamelCase     )
			lowerCamelCase__		:		Tuple		     =iter_sql_file(__lowerCamelCase     )

			for rowa, rowa in zip(__lowerCamelCase							,		__lowerCamelCase     ):
						assert rowa == rowa






@require_sqlalchemy
def 					snake_case__       (		__lowerCamelCase    :   Optional[int]							,		__lowerCamelCase    :   Dict							,		__lowerCamelCase    :   List[str]     ):
			"""simple docstring"""
			lowerCamelCase__		:		Dict		     =tmp_path / '''cache'''
			lowerCamelCase__		:		Optional[Any]		     =os.path.join(__lowerCamelCase							,		'''tmp.sql'''     )
			lowerCamelCase__		:		str		     =SqlDatasetReader('''dataset'''							,		'''sqlite:///''' + sqlite_path							,		cache_dir=__lowerCamelCase     ).read()
			with pytest.raises(__lowerCamelCase     ):
						SqlDatasetWriter(__lowerCamelCase							,		'''dataset'''							,		'''sqlite:///''' + output_sqlite_path							,		num_proc=0     ).write()









"""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()




