



import unittest

import numpy as np

from transformers.testing_utils import is_flaky, 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 DonutImageProcessor




class 							lowerCAmelCase__  (	unittest.TestCase						):







     def __init__(       self      :     Union[str, Any]			,		_A      :     List[str]			,		_A      :     Dict=7			,		_A      :     List[str]=3			,		_A      :     Union[str, Any]=18			,		_A      :     int=30			,		_A      :     Optional[Any]=400			,		_A      :     Union[str, Any]=True			,		_A      :     int=None			,		_A      :     List[str]=True			,		_A      :     int=False			,		_A      :     List[str]=True			,		_A      :     Dict=True			,		_A      :     Optional[Any]=[0.5, 0.5, 0.5]			,		_A      :     str=[0.5, 0.5, 0.5]			,		):
           A__	:							int						    = parent
           A__	:							List[str]						    = batch_size
           A__	:							Union[str, Any]						    = num_channels
           A__	:							Optional[int]						    = image_size
           A__	:							List[Any]						    = min_resolution
           A__	:							Optional[int]						    = max_resolution
           A__	:							Union[str, Any]						    = do_resize
           A__	:							Optional[int]						    = size if size is not None else {"height": 18, "width": 20}
           A__	:							Any						    = do_thumbnail
           A__	:							Optional[Any]						    = do_align_axis
           A__	:							Any						    = do_pad
           A__	:							List[str]						    = do_normalize
           A__	:							Any						    = image_mean
           A__	:							int						    = image_std







     def  _lowercase			(       self      :     Optional[Any]):
           return {
               "do_resize": self.do_resize,
               "size": self.size,
               "do_thumbnail": self.do_thumbnail,
               "do_align_long_axis": self.do_align_axis,
               "do_pad": self.do_pad,
               "do_normalize": self.do_normalize,
               "image_mean": self.image_mean,
               "image_std": self.image_std,
           }




@require_torch
@require_vision
class 							lowerCAmelCase__  (	_SCREAMING_SNAKE_CASE					,							unittest.TestCase						):
     __A     :    Optional[int]           =						DonutImageProcessor if is_vision_available() else None







     def  _lowercase			(       self      :     Any):
           A__	:							Tuple						    = DonutImageProcessingTester(self)







     @property
     def  _lowercase			(       self      :     Union[str, Any]):
           return self.image_processor_tester.prepare_image_processor_dict()







     def  _lowercase			(       self      :     List[str]):
           A__	:							List[Any]						    = self.image_processing_class(**self.image_processor_dict)
           self.assertTrue(hasattr(A_			,		"do_resize"))
           self.assertTrue(hasattr(A_			,		"size"))
           self.assertTrue(hasattr(A_			,		"do_thumbnail"))
           self.assertTrue(hasattr(A_			,		"do_align_long_axis"))
           self.assertTrue(hasattr(A_			,		"do_pad"))
           self.assertTrue(hasattr(A_			,		"do_normalize"))
           self.assertTrue(hasattr(A_			,		"image_mean"))
           self.assertTrue(hasattr(A_			,		"image_std"))







     def  _lowercase			(       self      :     List[Any]):
           A__	:							List[Any]						    = self.image_processing_class.from_dict(self.image_processor_dict)
           self.assertEqual(image_processor.size			,		{"height": 18, "width": 20})

           A__	:							Dict						    = self.image_processing_class.from_dict(self.image_processor_dict			,		size=42)
           self.assertEqual(image_processor.size			,		{"height": 42, "width": 42})

           # Previous config had dimensions in (width, height) order
           A__	:							List[str]						    = self.image_processing_class.from_dict(self.image_processor_dict			,		size=(42, 84))
           self.assertEqual(image_processor.size			,		{"height": 84, "width": 42})







     def  _lowercase			(       self      :     List[Any]):
           pass







     @is_flaky()
     def  _lowercase			(       self      :     str):
           A__	:							int						    = self.image_processing_class(**self.image_processor_dict)
           # create random PIL images
           A__	:							Optional[int]						    = prepare_image_inputs(self.image_processor_tester			,		equal_resolution=A_)
           for image in image_inputs:
                 self.assertIsInstance(A_			,		Image.Image)

           # Test not batched input
           A__	:							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.size["height"],
                   self.image_processor_tester.size["width"],
               )			,		)

           # Test batched
           A__	:							List[Any]						    = image_processing(A_			,		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.size["height"],
                   self.image_processor_tester.size["width"],
               )			,		)







     @is_flaky()
     def  _lowercase			(       self      :     Union[str, Any]):
           A__	:							List[str]						    = self.image_processing_class(**self.image_processor_dict)
           # create random numpy tensors
           A__	:							Any						    = prepare_image_inputs(self.image_processor_tester			,		equal_resolution=A_			,		numpify=A_)
           for image in image_inputs:
                 self.assertIsInstance(A_			,		np.ndarray)

           # Test not batched input
           A__	:							Union[str, 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.size["height"],
                   self.image_processor_tester.size["width"],
               )			,		)

           # Test batched
           A__	:							Optional[Any]						    = image_processing(A_			,		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.size["height"],
                   self.image_processor_tester.size["width"],
               )			,		)







     @is_flaky()
     def  _lowercase			(       self      :     List[Any]):
           A__	:							Any						    = self.image_processing_class(**self.image_processor_dict)
           # create random PyTorch tensors
           A__	:							List[Any]						    = prepare_image_inputs(self.image_processor_tester			,		equal_resolution=A_			,		torchify=A_)
           for image in image_inputs:
                 self.assertIsInstance(A_			,		torch.Tensor)

           # Test not batched input
           A__	:							Dict						    = 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.size["height"],
                   self.image_processor_tester.size["width"],
               )			,		)

           # Test batched
           A__	:							List[str]						    = image_processing(A_			,		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.size["height"],
                   self.image_processor_tester.size["width"],
               )			,		)




from unittest import TestCase

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




class 							lowerCAmelCase__  (	UpperCamelCase						):







					def  _lowercase			(       self      :     List[Any]):
											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  _lowercase			(       self      :     Tuple):
											A__	:							Any						    = {"col_1": [3, 2, 1, 0], "col_2": ["a", "b", "c", "d"]}
											return Dataset.from_dict(_A)







					def  _lowercase			(       self      :     Union[str, Any]):
											A__	:							str						    = self._create_example_records()
											A__	:							Dict						    = Dataset.from_list(_A)
											self.assertListEqual(dset.column_names			,		["col_1", "col_2"])
											for i, r in enumerate(_A):
																	self.assertDictEqual(_A			,		example_records[i])







					def  _lowercase			(       self      :     List[str]):
											A__	:							List[str]						    = self._create_example_records()
											A__	:							Any						    = Dataset.from_list(_A)
											A__	:							Optional[int]						    = 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  _lowercase			(       self      :     Any):  # checks what happens with missing columns
											A__	:							Dict						    = [{"col_1": 1}, {"col_2": "x"}]
											A__	:							Any						    = Dataset.from_list(_A)
											self.assertDictEqual(dset[0]			,		{"col_1": 1})
											self.assertDictEqual(dset[1]			,		{"col_1": None})  # NB: first record is used for columns







					def  _lowercase			(       self      :     str):  # checks if the type can be inferred from the second record
											A__	:							Dict						    = [{"col_1": []}, {"col_1": [1, 2]}]
											A__	:							Dict						    = Dataset.from_list(_A)
											self.assertEqual(dset.info.features["col_1"]			,		Sequence(Value("int64")))







					def  _lowercase			(       self      :     Union[str, Any]):
											A__	:							Dict						    = Dataset.from_list([])
											self.assertEqual(len(_A)			,		0)
											self.assertListEqual(dset.column_names			,		[])