


"""simple docstring"""






import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
					import torch

if is_vision_available():
					from PIL import Image

					from transformers import DeiTImageProcessor






class     A(     unittest.TestCase		):

		"""simple docstring"""





		def __init__(   self				,							SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__=7				,							SCREAMING_SNAKE_CASE__=3				,							SCREAMING_SNAKE_CASE__=18				,							SCREAMING_SNAKE_CASE__=30				,							SCREAMING_SNAKE_CASE__=4_00				,							SCREAMING_SNAKE_CASE__=True				,							SCREAMING_SNAKE_CASE__=None				,							SCREAMING_SNAKE_CASE__=True				,							SCREAMING_SNAKE_CASE__=None				,							SCREAMING_SNAKE_CASE__=True				,							SCREAMING_SNAKE_CASE__=[0.5, 0.5, 0.5]				,							SCREAMING_SNAKE_CASE__=[0.5, 0.5, 0.5]				,							)			->  Optional[int]:

					"""simple docstring"""



					_UpperCamelCase				:Tuple				    =  size if size is not None else {'''height''': 20, '''width''': 20}
					_UpperCamelCase				:Optional[Any]				    =  crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}

					_UpperCamelCase				:Any				    =  parent
					_UpperCamelCase				:Optional[int]				    =  batch_size
					_UpperCamelCase				:List[str]				    =  num_channels
					_UpperCamelCase				:Union[str, Any]				    =  image_size
					_UpperCamelCase				:Optional[Any]				    =  min_resolution
					_UpperCamelCase				:int				    =  max_resolution
					_UpperCamelCase				:List[str]				    =  do_resize
					_UpperCamelCase				:List[Any]				    =  size
					_UpperCamelCase				:int				    =  do_center_crop
					_UpperCamelCase				:Any				    =  crop_size
					_UpperCamelCase				:Union[str, Any]				    =  do_normalize
					_UpperCamelCase				:Union[str, Any]				    =  image_mean
					_UpperCamelCase				:Optional[int]				    =  image_std





		def      _UpperCamelCase(   self      )			->  str:

					"""simple docstring"""



					return {
					    "do_resize": self.do_resize,
					    "size": self.size,
					    "do_center_crop": self.do_center_crop,
					    "crop_size": self.crop_size,
					    "do_normalize": self.do_normalize,
					    "image_mean": self.image_mean,
					    "image_std": self.image_std,
					}






@require_torch
@require_vision
class     A(     lowerCamelCase__							,       unittest.TestCase		):

		"""simple docstring"""





		A         =       DeiTImageProcessor if is_vision_available() else None
		A         =       True
		def      _UpperCamelCase(   self      )			->  int:

					"""simple docstring"""



					_UpperCamelCase				:Union[str, Any]				    =  DeiTImageProcessingTester(self      )
		@property
		def      _UpperCamelCase(   self      )			->  Dict:

					"""simple docstring"""



					return self.image_processor_tester.prepare_image_processor_dict()
		def      _UpperCamelCase(   self      )			->  Dict:

					"""simple docstring"""



					_UpperCamelCase				:str				    =  self.image_processing_class(**self.image_processor_dict      )
					self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__				,							'''do_resize'''      )      )
					self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__				,							'''size'''      )      )
					self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__				,							'''do_center_crop'''      )      )
					self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__				,							'''center_crop'''      )      )
					self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__				,							'''do_normalize'''      )      )
					self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__				,							'''image_mean'''      )      )
					self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__				,							'''image_std'''      )      )
		def      _UpperCamelCase(   self      )			->  Optional[Any]:

					"""simple docstring"""



					_UpperCamelCase				:str				    =  self.image_processing_class.from_dict(self.image_processor_dict      )
					self.assertEqual(image_processor.size				,							{'''height''': 20, '''width''': 20}      )
					self.assertEqual(image_processor.crop_size				,							{'''height''': 18, '''width''': 18}      )

					_UpperCamelCase				:List[str]				    =  self.image_processing_class.from_dict(self.image_processor_dict				,							size=42				,							crop_size=84      )
					self.assertEqual(image_processor.size				,							{'''height''': 42, '''width''': 42}      )
					self.assertEqual(image_processor.crop_size				,							{'''height''': 84, '''width''': 84}      )
		def      _UpperCamelCase(   self      )			->  Union[str, Any]:

					"""simple docstring"""



					pass
		def      _UpperCamelCase(   self      )			->  Dict:

					"""simple docstring"""



					_UpperCamelCase				:Tuple				    =  self.image_processing_class(**self.image_processor_dict      )
					# create random PIL images
					_UpperCamelCase				:List[str]				    =  prepare_image_inputs(self.image_processor_tester				,							equal_resolution=SCREAMING_SNAKE_CASE__      )
					for image in image_inputs:
								self.assertIsInstance(SCREAMING_SNAKE_CASE__				,							Image.Image      )

					# Test not batched input
					_UpperCamelCase				:List[Any]				    =  image_processing(image_inputs[0]				,							return_tensors='''pt'''      ).pixel_values
					self.assertEqual(
					    encoded_images.shape				,							(
					        1,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.crop_size['''height'''],
					        self.image_processor_tester.crop_size['''width'''],
					    )				,							)

					# Test batched
					_UpperCamelCase				:Tuple				    =  image_processing(SCREAMING_SNAKE_CASE__				,							return_tensors='''pt'''      ).pixel_values
					self.assertEqual(
					    encoded_images.shape				,							(
					        self.image_processor_tester.batch_size,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.crop_size['''height'''],
					        self.image_processor_tester.crop_size['''width'''],
					    )				,							)
		def      _UpperCamelCase(   self      )			->  List[str]:

					"""simple docstring"""



					_UpperCamelCase				:List[Any]				    =  self.image_processing_class(**self.image_processor_dict      )
					# create random numpy tensors
					_UpperCamelCase				:Any				    =  prepare_image_inputs(self.image_processor_tester				,							equal_resolution=SCREAMING_SNAKE_CASE__				,							numpify=SCREAMING_SNAKE_CASE__      )
					for image in image_inputs:
								self.assertIsInstance(SCREAMING_SNAKE_CASE__				,							np.ndarray      )

					# Test not batched input
					_UpperCamelCase				:Optional[int]				    =  image_processing(image_inputs[0]				,							return_tensors='''pt'''      ).pixel_values
					self.assertEqual(
					    encoded_images.shape				,							(
					        1,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.crop_size['''height'''],
					        self.image_processor_tester.crop_size['''width'''],
					    )				,							)

					# Test batched
					_UpperCamelCase				:Tuple				    =  image_processing(SCREAMING_SNAKE_CASE__				,							return_tensors='''pt'''      ).pixel_values
					self.assertEqual(
					    encoded_images.shape				,							(
					        self.image_processor_tester.batch_size,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.crop_size['''height'''],
					        self.image_processor_tester.crop_size['''width'''],
					    )				,							)





		def      _UpperCamelCase(   self      )			->  List[Any]:

					"""simple docstring"""



					_UpperCamelCase				:Tuple				    =  self.image_processing_class(**self.image_processor_dict      )
					# create random PyTorch tensors
					_UpperCamelCase				:List[Any]				    =  prepare_image_inputs(self.image_processor_tester				,							equal_resolution=SCREAMING_SNAKE_CASE__				,							torchify=SCREAMING_SNAKE_CASE__      )
					for image in image_inputs:
								self.assertIsInstance(SCREAMING_SNAKE_CASE__				,							torch.Tensor      )

					# Test not batched input
					_UpperCamelCase				:Tuple				    =  image_processing(image_inputs[0]				,							return_tensors='''pt'''      ).pixel_values
					self.assertEqual(
					    encoded_images.shape				,							(
					        1,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.crop_size['''height'''],
					        self.image_processor_tester.crop_size['''width'''],
					    )				,							)

					# Test batched
					_UpperCamelCase				:Any				    =  image_processing(SCREAMING_SNAKE_CASE__				,							return_tensors='''pt'''      ).pixel_values
					self.assertEqual(
					    encoded_images.shape				,							(
					        self.image_processor_tester.batch_size,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.crop_size['''height'''],
					        self.image_processor_tester.crop_size['''width'''],
					    )				,							)





"""simple docstring"""






import textwrap

import pyarrow as pa
import pytest

from datasets import Features, Value
from datasets.packaged_modules.json.json import Json





@pytest.fixture
def 							A_					(   snake_case__		)				->     Union[str, Any]:
			_UpperCamelCase				:int				    =  tmp_path / '''file.jsonl'''
			_UpperCamelCase				:Dict				    =  textwrap.dedent(
			    '''\
        {"col_1": -1}
        {"col_1": 1, "col_2": 2}
        {"col_1": 10, "col_2": 20}
        '''		)
			with open(snake_case__					,				'''w'''		) as f:
						f.write(snake_case__		)
			return str(snake_case__		)





@pytest.fixture
def 							A_					(   snake_case__		)				->     Optional[int]:
			_UpperCamelCase				:Dict				    =  tmp_path / '''file_utf16_encoded.jsonl'''
			_UpperCamelCase				:Dict				    =  textwrap.dedent(
			    '''\
        {"col_1": -1}
        {"col_1": 1, "col_2": 2}
        {"col_1": 10, "col_2": 20}
        '''		)
			with open(snake_case__					,				'''w'''					,				encoding='''utf-16'''		) as f:
						f.write(snake_case__		)
			return str(snake_case__		)





@pytest.fixture
def 							A_					(   snake_case__		)				->     Optional[int]:
			_UpperCamelCase				:Optional[Any]				    =  tmp_path / '''file_with_list_of_dicts.json'''
			_UpperCamelCase				:str				    =  textwrap.dedent(
			    '''\
        [
            {"col_1": -1},
            {"col_1": 1, "col_2": 2},
            {"col_1": 10, "col_2": 20}
        ]
        '''		)
			with open(snake_case__					,				'''w'''		) as f:
						f.write(snake_case__		)
			return str(snake_case__		)





@pytest.fixture
def 							A_					(   snake_case__		)				->     Union[str, Any]:
			_UpperCamelCase				:Dict				    =  tmp_path / '''file_with_list_of_dicts_field.json'''
			_UpperCamelCase				:Optional[int]				    =  textwrap.dedent(
			    '''\
        {
            "field1": 1,
            "field2": "aabb",
            "field3": [
                {"col_1": -1},
                {"col_1": 1, "col_2": 2},
                {"col_1": 10, "col_2": 20}
            ]
        }
        '''		)
			with open(snake_case__					,				'''w'''		) as f:
						f.write(snake_case__		)
			return str(snake_case__		)





@pytest.mark.parametrize(
    '''file_fixture, config_kwargs'''					,				[
        ('''jsonl_file''', {}),
        ('''jsonl_file_utf16_encoded''', {'''encoding''': '''utf-16'''}),
        ('''json_file_with_list_of_dicts''', {}),
        ('''json_file_with_list_of_dicts_field''', {'''field''': '''field3'''}),
    ]					,				)
def 							A_					(   snake_case__					,				snake_case__					,				snake_case__		)				->     int:
			_UpperCamelCase				:Any				    =  Json(**snake_case__		)
			_UpperCamelCase				:str				    =  json._generate_tables([[request.getfixturevalue(snake_case__		)]]		)
			_UpperCamelCase				:List[Any]				    =  pa.concat_tables([table for _, table in generator]		)
			assert pa_table.to_pydict() == {"col_1": [-1, 1, 10], "col_2": [None, 2, 20]}





@pytest.mark.parametrize(
    '''file_fixture, config_kwargs'''					,				[
        (
            '''jsonl_file''',
            {'''features''': Features({'''col_1''': Value('''int64'''		), '''col_2''': Value('''int64'''		), '''missing_col''': Value('''string'''		)}		)},
        ),
        (
            '''json_file_with_list_of_dicts''',
            {'''features''': Features({'''col_1''': Value('''int64'''		), '''col_2''': Value('''int64'''		), '''missing_col''': Value('''string'''		)}		)},
        ),
        (
            '''json_file_with_list_of_dicts_field''',
            {
                '''field''': '''field3''',
                '''features''': Features(
                    {'''col_1''': Value('''int64'''		), '''col_2''': Value('''int64'''		), '''missing_col''': Value('''string'''		)}		),
            },
        ),
    ]					,				)
def 							A_					(   snake_case__					,				snake_case__					,				snake_case__		)				->     Optional[int]:
			_UpperCamelCase				:Union[str, Any]				    =  Json(**snake_case__		)
			_UpperCamelCase				:List[str]				    =  json._generate_tables([[request.getfixturevalue(snake_case__		)]]		)
			_UpperCamelCase				:str				    =  pa.concat_tables([table for _, table in generator]		)
			assert pa_table.to_pydict() == {"col_1": [-1, 1, 10], "col_2": [None, 2, 20], "missing_col": [None, None, None]}

