

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



class 				_snake_case					(   unittest.TestCase			):

  def __init__(				self      , _SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE=7      , _SCREAMING_SNAKE_CASE=3      , _SCREAMING_SNAKE_CASE=30      , _SCREAMING_SNAKE_CASE=4_00      , _SCREAMING_SNAKE_CASE=True      , _SCREAMING_SNAKE_CASE=None      , _SCREAMING_SNAKE_CASE=0.9      , _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]      , ):





      '''simple docstring'''

      lowerCAmelCase 						=							size if size is not None else {'shortest_edge': 30}
      lowerCAmelCase 						=							crop_size if crop_size is not None else {'height': 30, 'width': 30}
      lowerCAmelCase 						=							parent
      lowerCAmelCase 						=							batch_size
      lowerCAmelCase 						=							num_channels
      lowerCAmelCase 						=							min_resolution
      lowerCAmelCase 						=							max_resolution
      lowerCAmelCase 						=							do_resize_and_center_crop
      lowerCAmelCase 						=							size
      lowerCAmelCase 						=							crop_pct
      lowerCAmelCase 						=							crop_size
      lowerCAmelCase 						=							do_normalize
      lowerCAmelCase 						=							image_mean
      lowerCAmelCase 						=							image_std


  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      return {
          "size": self.size,
          "do_resize_and_center_crop": self.do_resize_and_center_crop,
          "crop_pct": self.crop_pct,
          "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 				_snake_case					(   a_     , unittest.TestCase			):
  SCREAMING_SNAKE_CASE			:       int				=     PoolFormerImageProcessor if is_vision_available() else None

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							PoolFormerImageProcessingTester(self	)

  @property
  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      return self.image_processor_tester.prepare_image_processor_dict()

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							self.image_processing_class(**self.image_processor_dict	)
      self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE      , 'do_resize_and_center_crop'	)	)
      self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE      , 'size'	)	)
      self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE      , 'crop_pct'	)	)
      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   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							self.image_processing_class.from_dict(self.image_processor_dict	)
      self.assertEqual(image_processor.size      , {'shortest_edge': 30}	)
      self.assertEqual(image_processor.crop_size      , {'height': 30, 'width': 30}	)

      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   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      pass

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							self.image_processing_class(**self.image_processor_dict	)
      # create random PIL images
      lowerCAmelCase 						=							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
      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   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							self.image_processing_class(**self.image_processor_dict	)
      # create random numpy tensors
      lowerCAmelCase 						=							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
      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   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      lowerCAmelCase 						=							self.image_processing_class(**self.image_processor_dict	)
      # create random PyTorch tensors
      lowerCAmelCase 						=							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
      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'],
          )      , )








'''simple docstring'''



import itertools
from dataclasses import dataclass
from typing import Optional

import pandas as pd
import pyarrow as pa

import datasets
from datasets.table import table_cast



@dataclass
class 				_snake_case					(   datasets.BuilderConfig			):
  SCREAMING_SNAKE_CASE			:       Optional[datasets.Features]				=     None



class 				_snake_case					(   datasets.ArrowBasedBuilder			):
  SCREAMING_SNAKE_CASE			:       Tuple				=     PandasConfig

  def   _SCREAMING_SNAKE_CASE				(				self	):





      '''simple docstring'''

      return datasets.DatasetInfo(features=self.config.features	)

  def   _SCREAMING_SNAKE_CASE				(				self      , _SCREAMING_SNAKE_CASE	):





      '''simple docstring'''

      if not self.config.data_files:
          raise ValueError(F'At least one data file must be specified, but got data_files={self.config.data_files}'	)
      lowerCAmelCase 						=							dl_manager.download_and_extract(self.config.data_files	)
      if isinstance(_SCREAMING_SNAKE_CASE      , (str, list, tuple)	):
          lowerCAmelCase 						=							data_files
          if isinstance(_SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE	):
              lowerCAmelCase 						=							[files]
          # Use `dl_manager.iter_files` to skip hidden files in an extracted archive
          lowerCAmelCase 						=							[dl_manager.iter_files(_SCREAMING_SNAKE_CASE	) for file in files]
          return [datasets.SplitGenerator(name=datasets.Split.TRAIN      , gen_kwargs={'files': files}	)]
      lowerCAmelCase 						=							[]
      for split_name, files in data_files.items():
          if isinstance(_SCREAMING_SNAKE_CASE      , _SCREAMING_SNAKE_CASE	):
              lowerCAmelCase 						=							[files]
          # Use `dl_manager.iter_files` to skip hidden files in an extracted archive
          lowerCAmelCase 						=							[dl_manager.iter_files(_SCREAMING_SNAKE_CASE	) for file in files]
          splits.append(datasets.SplitGenerator(name=_SCREAMING_SNAKE_CASE      , gen_kwargs={'files': files}	)	)
      return splits

  def   _SCREAMING_SNAKE_CASE				(				self      , _SCREAMING_SNAKE_CASE	):





      '''simple docstring'''

      if self.config.features is not None:
          # more expensive cast to support nested features with keys in a different order
          # allows str <-> int/float or str to Audio for example
          lowerCAmelCase 						=							table_cast(_SCREAMING_SNAKE_CASE      , self.config.features.arrow_schema	)
      return pa_table


  def   _SCREAMING_SNAKE_CASE				(				self      , _SCREAMING_SNAKE_CASE	):





      '''simple docstring'''

      for i, file in enumerate(itertools.chain.from_iterable(_SCREAMING_SNAKE_CASE	)	):
          with open(_SCREAMING_SNAKE_CASE      , 'rb'	) as f:
              lowerCAmelCase 						=							pa.Table.from_pandas(pd.read_pickle(_SCREAMING_SNAKE_CASE	)	)
              yield i, self._cast_table(_SCREAMING_SNAKE_CASE	)





