




"""simple docstring"""







import os
from typing import Optional

import fsspec
from fsspec.archive import AbstractArchiveFileSystem
from fsspec.utils import DEFAULT_BLOCK_SIZE

class 				snake_case_			(					snake_case__			):







   """simple docstring"""




   A_				      =       ''
   A_				      =       (
       None  # protocol passed in prefix to the url. ex: "gzip", for gzip://file.txt::http://foo.bar/file.txt.gz
   )
   A_				      =       None  # compression type in fsspec. ex: "gzip"
   A_				      =       None  # extension of the filename to strip. ex: "".gz" to get file.txt from file.txt.gz







   def __init__(		self       ,					lowerCamelCase_ = ""       ,					lowerCamelCase_ = None       ,					lowerCamelCase_ = None       ,					**lowerCamelCase_)				->   List[Any]:
       super().__init__(self       ,					**lowerCamelCase_)
       # always open as "rb" since fsspec can then use the TextIOWrapper to make it work for "r" mode
       UpperCamelCase              =	fsspec.open(
           lowerCamelCase_       ,					mode='''rb'''       ,					protocol=lowerCamelCase_       ,					compression=self.compression       ,					client_kwargs={
               '''requote_redirect_url''': False,  # see https://github.com/huggingface/datasets/pull/5459
               '''trust_env''': True,  # Enable reading proxy env variables.
               **(target_options or {}).pop('''client_kwargs'''       ,					{}),  # To avoid issues if it was already passed.
           }       ,					**(target_options or {})       ,					)
       UpperCamelCase              =	os.path.basename(self.file.path.split('''::''')[0])
       UpperCamelCase              =	(
           self.compressed_name[: self.compressed_name.rindex('''.''')]
           if '.' in self.compressed_name
           else self.compressed_name
       )
       UpperCamelCase              =	None







   @classmethod
   def   UpperCAmelCase__			(		cls       ,					lowerCamelCase_)				->   int:
       # compressed file paths are always relative to the archive root
       return super()._strip_protocol(lowerCamelCase_).lstrip('''/''')







   def   UpperCAmelCase__			(		self)				->   Union[str, Any]:
       if self.dir_cache is None:
           UpperCamelCase              =	{**self.file.fs.info(self.file.path), 'name': self.uncompressed_name}
           UpperCamelCase              =	{f['name']: f}







   def   UpperCAmelCase__			(		self       ,					lowerCamelCase_)				->   Dict:
       return self.file.open().read()







   def   UpperCAmelCase__			(		self       ,					lowerCamelCase_       ,					lowerCamelCase_ = "rb"       ,					lowerCamelCase_=None       ,					lowerCamelCase_=True       ,					lowerCamelCase_=None       ,					**lowerCamelCase_       ,					)				->   List[Any]:
       UpperCamelCase              =	self._strip_protocol(lowerCamelCase_)
       if mode != "rb":
           raise ValueError(F'Tried to read with mode {mode} on file {self.file.path} opened with mode \'rb\'')
       return self.file.open()

class 				snake_case_			(					snake_case__			):







   """simple docstring"""




   A_				      =       'bz2'
   A_				      =       'bz2'
   A_				      =       '.bz2'

class 				snake_case_			(					snake_case__			):







   """simple docstring"""




   A_				      =       'gzip'
   A_				      =       'gzip'
   A_				      =       '.gz'

class 				snake_case_			(					snake_case__			):







   """simple docstring"""




   A_				      =       'lz4'
   A_				      =       'lz4'
   A_				      =       '.lz4'

class 				snake_case_			(					snake_case__			):







   """simple docstring"""




   A_				      =       'xz'
   A_				      =       'xz'
   A_				      =       '.xz'

class 				snake_case_			(					snake_case__			):







   """simple docstring"""




   A_				      =       'zstd'
   A_				      =       'zstd'
   A_				      =       '.zst'







   def __init__(		self       ,					lowerCamelCase_       ,					lowerCamelCase_ = "rb"       ,					lowerCamelCase_ = None       ,					lowerCamelCase_ = None       ,					lowerCamelCase_ = DEFAULT_BLOCK_SIZE       ,					**lowerCamelCase_       ,					)				->   Dict:
       super().__init__(
           fo=lowerCamelCase_       ,					mode=lowerCamelCase_       ,					target_protocol=lowerCamelCase_       ,					target_options=lowerCamelCase_       ,					block_size=lowerCamelCase_       ,					**lowerCamelCase_       ,					)
       # We need to wrap the zstd decompressor to avoid this error in fsspec==2021.7.0 and zstandard==0.15.2:
       #
       # File "/Users/user/.virtualenvs/hf-datasets/lib/python3.7/site-packages/fsspec/core.py", line 145, in open
       #     out.close = close
       # AttributeError: 'zstd.ZstdDecompressionReader' object attribute 'close' is read-only
       #
       # see https://github.com/intake/filesystem_spec/issues/725
       UpperCamelCase              =	self.file.__enter__

       class 				snake_case_			:







          """simple docstring"""




          def __init__(		self       ,					lowerCamelCase_)				->   Tuple:
              UpperCamelCase              =	file_







          def __enter__(		self)				->   Optional[int]:
              self._file.__enter__()
              return self







          def __exit__(		self       ,					*lowerCamelCase_       ,					**lowerCamelCase_)				->   Tuple:
              self._file.__exit__(*lowerCamelCase_       ,					**lowerCamelCase_)







          def __iter__(		self)				->   Any:
              return iter(self._file)







          def   UpperCAmelCase__			(		self)				->   Any:
              return next(self._file)







          def __getattr__(		self       ,					lowerCamelCase_)				->   Dict:
              return getattr(self._file       ,					lowerCamelCase_)

       def fixed_enter(*lowerCamelCase_       ,					**lowerCamelCase_):
           return WrappedFile(_enter(*lowerCamelCase_       ,					**lowerCamelCase_))

       UpperCamelCase              =	fixed_enter






from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import convert_to_rgb, normalize, rescale, resize, to_channel_dimension_format
from ...image_utils import (
    OPENAI_CLIP_MEAN,
    OPENAI_CLIP_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


if is_vision_available():
		import PIL


_lowercase		:  Tuple			       =logging.get_logger(__name__)






class 						UpperCamelCase_  (  snake_case__							):
					_a      :						List[str]						    =					['pixel_values']



					def __init__(		self     :     List[Any]       ,      lowerCamelCase     :     bool = True       ,      lowerCamelCase     :     Dict[str, int] = None       ,      lowerCamelCase     :     PILImageResampling = PILImageResampling.BICUBIC       ,      lowerCamelCase     :     bool = True       ,      lowerCamelCase     :     Union[int, float] = 1 / 2_55       ,      lowerCamelCase     :     bool = True       ,      lowerCamelCase     :     Optional[Union[float, List[float]]] = None       ,      lowerCamelCase     :     Optional[Union[float, List[float]]] = None       ,      lowerCamelCase     :     bool = True       ,      **lowerCamelCase     :     Optional[Any]       ,      ):
							super().__init__(**lowerCamelCase						)
							lowerCamelCase_	:     List[Any] 						=      size if size is not None else {'height': 3_84, 'width': 3_84}
							lowerCamelCase_	:     List[str] 						=      get_size_dict(lowerCamelCase       ,      default_to_square=lowerCamelCase						)

							lowerCamelCase_	:     Dict 						=      do_resize
							lowerCamelCase_	:     int 						=      size
							lowerCamelCase_	:     Union[str, Any] 						=      resample
							lowerCamelCase_	:     Tuple 						=      do_rescale
							lowerCamelCase_	:     Dict 						=      rescale_factor
							lowerCamelCase_	:     int 						=      do_normalize
							lowerCamelCase_	:     Any 						=      image_mean if image_mean is not None else OPENAI_CLIP_MEAN
							lowerCamelCase_	:     str 						=      image_std if image_std is not None else OPENAI_CLIP_STD
							lowerCamelCase_	:     int 						=      do_convert_rgb



					def   __a     (		self     :     Tuple       ,      lowerCamelCase     :     np.ndarray       ,      lowerCamelCase     :     Dict[str, int]       ,      lowerCamelCase     :     PILImageResampling = PILImageResampling.BICUBIC       ,      lowerCamelCase     :     Optional[Union[str, ChannelDimension]] = None       ,      **lowerCamelCase     :     List[Any]       ,      ):
							lowerCamelCase_	:     List[Any] 						=      get_size_dict(lowerCamelCase       ,      default_to_square=lowerCamelCase						)
							if "height" not in size or "width" not in size:
									raise ValueError(F"The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}"						)
							lowerCamelCase_	:     Optional[Any] 						=      (size['height'], size['width'])
							return resize(lowerCamelCase       ,      size=lowerCamelCase       ,      resample=lowerCamelCase       ,      data_format=lowerCamelCase       ,      **lowerCamelCase						)



					def   __a     (		self     :     Union[str, Any]       ,      lowerCamelCase     :     np.ndarray       ,      lowerCamelCase     :     Union[int, float]       ,      lowerCamelCase     :     Optional[Union[str, ChannelDimension]] = None       ,      **lowerCamelCase     :     Union[str, Any]       ,      ):
							return rescale(lowerCamelCase       ,      scale=lowerCamelCase       ,      data_format=lowerCamelCase       ,      **lowerCamelCase						)



					def   __a     (		self     :     str       ,      lowerCamelCase     :     np.ndarray       ,      lowerCamelCase     :     Union[float, List[float]]       ,      lowerCamelCase     :     Union[float, List[float]]       ,      lowerCamelCase     :     Optional[Union[str, ChannelDimension]] = None       ,      **lowerCamelCase     :     Optional[int]       ,      ):
							return normalize(lowerCamelCase       ,      mean=lowerCamelCase       ,      std=lowerCamelCase       ,      data_format=lowerCamelCase       ,      **lowerCamelCase						)





					def   __a     (		self     :     List[Any]       ,      lowerCamelCase     :     ImageInput       ,      lowerCamelCase     :     Optional[bool] = None       ,      lowerCamelCase     :     Optional[Dict[str, int]] = None       ,      lowerCamelCase     :     PILImageResampling = None       ,      lowerCamelCase     :     Optional[bool] = None       ,      lowerCamelCase     :     Optional[float] = None       ,      lowerCamelCase     :     Optional[bool] = None       ,      lowerCamelCase     :     Optional[Union[float, List[float]]] = None       ,      lowerCamelCase     :     Optional[Union[float, List[float]]] = None       ,      lowerCamelCase     :     Optional[Union[str, TensorType]] = None       ,      lowerCamelCase     :     bool = None       ,      lowerCamelCase     :     ChannelDimension = ChannelDimension.FIRST       ,      **lowerCamelCase     :     Any       ,      ):
							lowerCamelCase_	:     Optional[Any] 						=      do_resize if do_resize is not None else self.do_resize
							lowerCamelCase_	:     int 						=      resample if resample is not None else self.resample
							lowerCamelCase_	:     Dict 						=      do_rescale if do_rescale is not None else self.do_rescale
							lowerCamelCase_	:     Any 						=      rescale_factor if rescale_factor is not None else self.rescale_factor
							lowerCamelCase_	:     str 						=      do_normalize if do_normalize is not None else self.do_normalize
							lowerCamelCase_	:     List[str] 						=      image_mean if image_mean is not None else self.image_mean
							lowerCamelCase_	:     str 						=      image_std if image_std is not None else self.image_std
							lowerCamelCase_	:     int 						=      do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb

							lowerCamelCase_	:     Union[str, Any] 						=      size if size is not None else self.size
							lowerCamelCase_	:     Optional[int] 						=      get_size_dict(lowerCamelCase       ,      default_to_square=lowerCamelCase						)

							lowerCamelCase_	:     Dict 						=      make_list_of_images(lowerCamelCase						)

							if not valid_images(lowerCamelCase						):
									raise ValueError(
									    'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, '
									    'torch.Tensor, tf.Tensor or jax.ndarray.'						)

							if do_resize and size is None or resample is None:
									raise ValueError('Size and resample must be specified if do_resize is True.'						)

							if do_rescale and rescale_factor is None:
									raise ValueError('Rescale factor must be specified if do_rescale is True.'						)

							if do_normalize and (image_mean is None or image_std is None):
									raise ValueError('Image mean and std must be specified if do_normalize is True.'						)

							# PIL RGBA images are converted to RGB
							if do_convert_rgb:
									lowerCamelCase_	:     int 						=      [convert_to_rgb(lowerCamelCase						) for image in images]

							# All transformations expect numpy arrays.
							lowerCamelCase_	:     List[Any] 						=      [to_numpy_array(lowerCamelCase						) for image in images]

							if do_resize:
									lowerCamelCase_	:     Optional[int] 						=      [self.resize(image=lowerCamelCase       ,      size=lowerCamelCase       ,      resample=lowerCamelCase						) for image in images]

							if do_rescale:
									lowerCamelCase_	:     Dict 						=      [self.rescale(image=lowerCamelCase       ,      scale=lowerCamelCase						) for image in images]

							if do_normalize:
									lowerCamelCase_	:     Optional[int] 						=      [self.normalize(image=lowerCamelCase       ,      mean=lowerCamelCase       ,      std=lowerCamelCase						) for image in images]

							lowerCamelCase_	:     Optional[int] 						=      [to_channel_dimension_format(lowerCamelCase       ,      lowerCamelCase						) for image in images]

							lowerCamelCase_	:     Optional[int] 						=      BatchFeature(data={'pixel_values': images}       ,      tensor_type=lowerCamelCase						)

							return encoded_outputs
