'''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


if is_torch_available():
 import torch

if is_vision_available():
 from PIL import Image

 from transformers import ChineseCLIPImageProcessor







class 				_lowercase     (				unittest.TestCase			):


 '''simple docstring'''






 def __init__(      self   :	List[Any]      ,       SCREAMING_SNAKE_CASE__   :	Any      ,       SCREAMING_SNAKE_CASE__   :	str=7      ,       SCREAMING_SNAKE_CASE__   :	Union[str, Any]=3      ,       SCREAMING_SNAKE_CASE__   :	Optional[Any]=18      ,       SCREAMING_SNAKE_CASE__   :	Any=30      ,       SCREAMING_SNAKE_CASE__   :	str=4_00      ,       SCREAMING_SNAKE_CASE__   :	List[Any]=True      ,       SCREAMING_SNAKE_CASE__   :	int=None      ,       SCREAMING_SNAKE_CASE__   :	Optional[Any]=True      ,       SCREAMING_SNAKE_CASE__   :	Optional[Any]=None      ,       SCREAMING_SNAKE_CASE__   :	Dict=True      ,       SCREAMING_SNAKE_CASE__   :	int=[0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3]      ,       SCREAMING_SNAKE_CASE__   :	int=[0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1]      ,       SCREAMING_SNAKE_CASE__   :	Tuple=True      ,       )  ->    int:
  __lowerCAmelCase											=						size if size is not None else {"""height""": 2_24, """width""": 2_24}
  __lowerCAmelCase											=						crop_size if crop_size is not None else {"""height""": 18, """width""": 18}
  __lowerCAmelCase											=						parent
  __lowerCAmelCase											=						batch_size
  __lowerCAmelCase											=						num_channels
  __lowerCAmelCase											=						image_size
  __lowerCAmelCase											=						min_resolution
  __lowerCAmelCase											=						max_resolution
  __lowerCAmelCase											=						do_resize
  __lowerCAmelCase											=						size
  __lowerCAmelCase											=						do_center_crop
  __lowerCAmelCase											=						crop_size
  __lowerCAmelCase											=						do_normalize
  __lowerCAmelCase											=						image_mean
  __lowerCAmelCase											=						image_std
  __lowerCAmelCase											=						do_convert_rgb
 def a					(      self   :	Optional[int]							)  ->    Union[str, Any]:
  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,
      "do_convert_rgb": self.do_convert_rgb,
  }





 def a					(      self   :	Union[str, Any]      ,       SCREAMING_SNAKE_CASE__   :	Union[str, Any]=False      ,       SCREAMING_SNAKE_CASE__   :	List[str]=False      ,       SCREAMING_SNAKE_CASE__   :	Optional[int]=False							)  ->    Tuple:
  assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

  if equal_resolution:
   __lowerCAmelCase											=						[]
   for i in range(self.batch_size							):
    image_inputs.append(
        np.random.randint(
            2_55      ,       size=(self.num_channels, self.max_resolution, self.max_resolution)      ,       dtype=np.uinta							)							)
  else:
   __lowerCAmelCase											=						[]
   for i in range(self.batch_size							):
    __lowerCAmelCase							,       __lowerCAmelCase											=						np.random.choice(np.arange(self.min_resolution      ,       self.max_resolution							)      ,       2							)
    image_inputs.append(np.random.randint(2_55      ,       size=(self.num_channels, width, height)      ,       dtype=np.uinta							)							)

  if not numpify and not torchify:
   # PIL expects the channel dimension as last dimension
   __lowerCAmelCase											=						[Image.fromarray(np.moveaxis(SCREAMING_SNAKE_CASE__      ,       0      ,       -1							)							) for x in image_inputs]

  if torchify:
   __lowerCAmelCase											=						[torch.from_numpy(SCREAMING_SNAKE_CASE__							) for x in image_inputs]

  return image_inputs







@require_torch
@require_vision
class 				_lowercase     (				UpperCAmelCase__  ,							unittest.TestCase			):


 '''simple docstring'''






 _SCREAMING_SNAKE_CASE     :       Optional[int]      	=					ChineseCLIPImageProcessor if is_vision_available() else None
 def a					(      self   :	Dict							)  ->    Union[str, Any]:
  __lowerCAmelCase											=						ChineseCLIPImageProcessingTester(self      ,       do_center_crop=SCREAMING_SNAKE_CASE__							)
 @property
 def a					(      self   :	List[str]							)  ->    Dict:
  return self.image_processor_tester.prepare_image_processor_dict()
 def a					(      self   :	Tuple							)  ->    Optional[Any]:
  __lowerCAmelCase											=						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"""							)							)
  self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__      ,       """do_convert_rgb"""							)							)
 def a					(      self   :	int							)  ->    List[str]:
  __lowerCAmelCase											=						self.image_processing_class.from_dict(self.image_processor_dict							)
  self.assertEqual(image_processor.size      ,       {"""height""": 2_24, """width""": 2_24}							)
  self.assertEqual(image_processor.crop_size      ,       {"""height""": 18, """width""": 18}							)

  __lowerCAmelCase											=						self.image_processing_class.from_dict(self.image_processor_dict      ,       size=42      ,       crop_size=84							)
  self.assertEqual(image_processor.size      ,       {"""shortest_edge""": 42}							)
  self.assertEqual(image_processor.crop_size      ,       {"""height""": 84, """width""": 84}							)
 def a					(      self   :	str							)  ->    Optional[Any]:
  pass
 def a					(      self   :	Optional[int]							)  ->    str:
  # Initialize image_processing
  __lowerCAmelCase											=						self.image_processing_class(**self.image_processor_dict							)
  # create random PIL images
  __lowerCAmelCase											=						self.image_processor_tester.prepare_inputs(equal_resolution=SCREAMING_SNAKE_CASE__							)
  for image in image_inputs:
   self.assertIsInstance(SCREAMING_SNAKE_CASE__      ,       Image.Image							)

  # Test not batched input
  __lowerCAmelCase											=						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
  __lowerCAmelCase											=						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 a					(      self   :	List[Any]							)  ->    Union[str, Any]:
  # Initialize image_processing
  __lowerCAmelCase											=						self.image_processing_class(**self.image_processor_dict							)
  # create random numpy tensors
  __lowerCAmelCase											=						self.image_processor_tester.prepare_inputs(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
  __lowerCAmelCase											=						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
  __lowerCAmelCase											=						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 a					(      self   :	Dict							)  ->    Optional[Any]:
  # Initialize image_processing
  __lowerCAmelCase											=						self.image_processing_class(**self.image_processor_dict							)
  # create random PyTorch tensors
  __lowerCAmelCase											=						self.image_processor_tester.prepare_inputs(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
  __lowerCAmelCase											=						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
  __lowerCAmelCase											=						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"""],
      )      ,       )







@require_torch
@require_vision
class 				_lowercase     (				UpperCAmelCase__  ,							unittest.TestCase			):


 '''simple docstring'''






 _SCREAMING_SNAKE_CASE     :       Union[str, Any]      	=					ChineseCLIPImageProcessor if is_vision_available() else None
 def a					(      self   :	int							)  ->    Tuple:
  __lowerCAmelCase											=						ChineseCLIPImageProcessingTester(self      ,       num_channels=4      ,       do_center_crop=SCREAMING_SNAKE_CASE__							)
  __lowerCAmelCase											=						3
 @property
 def a					(      self   :	Any							)  ->    Dict:
  return self.image_processor_tester.prepare_image_processor_dict()
 def a					(      self   :	Dict							)  ->    Tuple:
  __lowerCAmelCase											=						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"""							)							)
  self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__      ,       """do_convert_rgb"""							)							)
 def a					(      self   :	str							)  ->    Tuple:
  pass





 def a					(      self   :	Optional[int]							)  ->    List[str]:
  # Initialize image_processing
  __lowerCAmelCase											=						self.image_processing_class(**self.image_processor_dict							)
  # create random PIL images
  __lowerCAmelCase											=						self.image_processor_tester.prepare_inputs(equal_resolution=SCREAMING_SNAKE_CASE__							)
  for image in image_inputs:
   self.assertIsInstance(SCREAMING_SNAKE_CASE__      ,       Image.Image							)

  # Test not batched input
  __lowerCAmelCase											=						image_processing(image_inputs[0]      ,       return_tensors="""pt"""							).pixel_values
  self.assertEqual(
      encoded_images.shape      ,       (
          1,
          self.expected_encoded_image_num_channels,
          self.image_processor_tester.crop_size["""height"""],
          self.image_processor_tester.crop_size["""width"""],
      )      ,       )

  # Test batched
  __lowerCAmelCase											=						image_processing(SCREAMING_SNAKE_CASE__      ,       return_tensors="""pt"""							).pixel_values
  self.assertEqual(
      encoded_images.shape      ,       (
          self.image_processor_tester.batch_size,
          self.expected_encoded_image_num_channels,
          self.image_processor_tester.crop_size["""height"""],
          self.image_processor_tester.crop_size["""width"""],
      )      ,       )







'''simple docstring'''

from unittest import TestCase

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







class 				_lowercase     (				UpperCAmelCase__			):


 '''simple docstring'''






 def a					(      self   :	int							)  ->    Optional[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 a					(      self   :	List[Any]							)  ->    Any:
  __lowerCAmelCase											=						{"""col_1""": [3, 2, 1, 0], """col_2""": ["""a""", """b""", """c""", """d"""]}
  return Dataset.from_dict(SCREAMING_SNAKE_CASE__							)
 def a					(      self   :	List[Any]							)  ->    Tuple:
  __lowerCAmelCase											=						self._create_example_records()
  __lowerCAmelCase											=						Dataset.from_list(SCREAMING_SNAKE_CASE__							)
  self.assertListEqual(dset.column_names      ,       ["""col_1""", """col_2"""]							)
  for i, r in enumerate(SCREAMING_SNAKE_CASE__							):
   self.assertDictEqual(SCREAMING_SNAKE_CASE__      ,       example_records[i]							)
 def a					(      self   :	Tuple							)  ->    List[str]:
  __lowerCAmelCase											=						self._create_example_records()
  __lowerCAmelCase											=						Dataset.from_list(SCREAMING_SNAKE_CASE__							)
  __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 a					(      self   :	List[str]							)  ->    List[str]:  # checks what happens with missing columns
  __lowerCAmelCase											=						[{"""col_1""": 1}, {"""col_2""": """x"""}]
  __lowerCAmelCase											=						Dataset.from_list(SCREAMING_SNAKE_CASE__							)
  self.assertDictEqual(dset[0]      ,       {"""col_1""": 1}							)
  self.assertDictEqual(dset[1]      ,       {"""col_1""": None}							)  # NB: first record is used for columns
 def a					(      self   :	Dict							)  ->    Optional[int]:  # checks if the type can be inferred from the second record
  __lowerCAmelCase											=						[{"""col_1""": []}, {"""col_1""": [1, 2]}]
  __lowerCAmelCase											=						Dataset.from_list(SCREAMING_SNAKE_CASE__							)
  self.assertEqual(dset.info.features["""col_1"""]      ,       Sequence(Value("""int64"""							)							)							)





 def a					(      self   :	Optional[Any]							)  ->    Tuple:
  __lowerCAmelCase											=						Dataset.from_list([]							)
  self.assertEqual(len(SCREAMING_SNAKE_CASE__							)      ,       0							)
  self.assertListEqual(dset.column_names      ,       []							)






