






'''simple docstring'''





# Lint as: python3
import os
import re
import urllib.parse
from pathlib import Path
from typing import Callable, List, Optional, Union
from zipfile import ZipFile

from ..utils.file_utils import cached_path, hf_github_url
from ..utils.logging import get_logger
from ..utils.version import Version


__lowerCamelCase	:		List[str]										=   get_logger(__name__)







class  _lowercase   :
      _a							:						str										=							'dummy_data'
      _a							:						Optional[int]										=							'datasets'
      _a							:						Optional[Any]										=							False
      def __init__(     self				,			a				,			a				,			a				,			a = None				,			a = False				,			a = True				,			a = None				,			):
       snake_case__  :    List[Any]							       =0
       snake_case__  :    str							       =dataset_name
       snake_case__  :    int							       =cache_dir
       snake_case__  :    int							       =use_local_dummy_data
       snake_case__  :    int							       =config
       # download_callbacks take a single url as input
       snake_case__  :    List[Callable]							       =download_callbacks or []
       # if False, it doesn't load existing files and it returns the paths of the dummy files relative
       # to the dummy_data zip file root
       snake_case__  :    int							       =load_existing_dummy_data

       # TODO(PVP, QL) might need to make this more general
       snake_case__  :    List[Any]							       =str(a   )
       # to be downloaded
       snake_case__  :    Optional[int]							       =None
       snake_case__  :    Dict							       =None
      @property
      def 						lowercase__					(     self   ):
       if self._dummy_file is None:
        snake_case__  :    Dict							       =self.download_dummy_data()
       return self._dummy_file
      @property
      def 						lowercase__					(     self   ):
       if self.config is not None:
        # structure is dummy / config_name / version_name
        return os.path.join("""dummy"""				,			self.config.name				,			self.version_name   )
       # structure is dummy / version_name
       return os.path.join("""dummy"""				,			self.version_name   )
      @property
      def 						lowercase__					(     self   ):
       return os.path.join(self.dummy_data_folder				,			"""dummy_data.zip"""   )
      def 						lowercase__					(     self   ):
       snake_case__  :    Optional[Any]							       =(
           self.local_path_to_dummy_data if self.use_local_dummy_data is True else self.github_path_to_dummy_data
       )

       snake_case__  :    Union[str, Any]							       =cached_path(
           a				,			cache_dir=self.cache_dir				,			extract_compressed_file=a				,			force_extract=a   )

       return os.path.join(a				,			self.dummy_file_name   )
      @property
      def 						lowercase__					(     self   ):
       return os.path.join(self.datasets_scripts_dir				,			self.dataset_name				,			self.dummy_zip_file   )
      @property
      def 						lowercase__					(     self   ):
       if self._bucket_url is None:
        snake_case__  :    Any							       =hf_github_url(self.dataset_name				,			self.dummy_zip_file.replace(os.sep				,			"""/"""   )   )
       return self._bucket_url
      @property
      def 						lowercase__					(     self   ):
       # return full path if its a dir
       if os.path.isdir(self.dummy_file   ):
        return self.dummy_file
       # else cut off path to file -> example `xsum`.
       return "/".join(self.dummy_file.replace(os.sep				,			"""/"""   ).split("""/"""   )[:-1]   )
      def 						lowercase__					(     self				,			a				,			*a   ):
       if self.load_existing_dummy_data:
        # dummy data is downloaded and tested
        snake_case__  :    int							       =self.dummy_file
       else:
        # dummy data cannot be downloaded and only the path to dummy file is returned
        snake_case__  :    Optional[Any]							       =self.dummy_file_name

       # special case when data_url is a dict
       if isinstance(a				,			a   ):
        return self.create_dummy_data_dict(a				,			a   )
       elif isinstance(a				,			(list, tuple)   ):
        return self.create_dummy_data_list(a				,			a   )
       else:
        return self.create_dummy_data_single(a				,			a   )
      def 						lowercase__					(     self				,			a				,			*a   ):
       return self.download_and_extract(a   )
      def 						lowercase__					(     self				,			a				,			a   ):
       return self.download_and_extract(a   )
      def 						lowercase__					(     self				,			a				,			*a				,			**a   ):
       return path
      def 						lowercase__					(     self   ):
       return {}
      def 						lowercase__					(     self				,			a				,			a   ):
       snake_case__  :    Union[str, Any]							       ={}
       for key, single_urls in data_url.items():
        for download_callback in self.download_callbacks:
         if isinstance(a				,			a   ):
          for single_url in single_urls:
           download_callback(a   )
         else:
          snake_case__  :    List[str]							       =single_urls
          download_callback(a   )
            # we force the name of each key to be the last file / folder name of the url path
            # if the url has arguments, we need to encode them with urllib.parse.quote_plus
        if isinstance(a				,			a   ):
         snake_case__  :    int							       =[os.path.join(a				,			urllib.parse.quote_plus(Path(a   ).name   )   ) for x in single_urls]
        else:
         snake_case__  :    Union[str, Any]							       =single_urls
         snake_case__  :    Optional[int]							       =os.path.join(a				,			urllib.parse.quote_plus(Path(a   ).name   )   )
        snake_case__  :    str							       =value

       # make sure that values are unique
       if all(isinstance(a				,			a   ) for i in dummy_data_dict.values()   ) and len(set(dummy_data_dict.values()   )   ) < len(
           dummy_data_dict.values()   ):
        # append key to value to make its name unique
        snake_case__  :    List[str]							       ={key: value + key for key, value in dummy_data_dict.items()}

       return dummy_data_dict
      def 						lowercase__					(     self				,			a				,			a   ):
       snake_case__  :    Tuple							       =[]
       # trick: if there are many shards named like `data.txt-000001-of-00300`, only use the first one
       snake_case__  :    List[Any]							       =all(bool(re.findall("""[0-9]{3,}-of-[0-9]{3,}"""				,			a   )   ) for url in data_url   )
       snake_case__  :    str							       =all(
           url.startswith("""https://ftp.ncbi.nlm.nih.gov/pubmed/baseline/pubmed"""   ) for url in data_url   )
       if data_url and (is_tf_records or is_pubmed_records):
        snake_case__  :    int							       =[data_url[0]] * len(a   )
       for single_url in data_url:
        for download_callback in self.download_callbacks:
         download_callback(a   )
        # we force the name of each key to be the last file / folder name of the url path
        # if the url has arguments, we need to encode them with urllib.parse.quote_plus
        snake_case__  :    List[Any]							       =os.path.join(a				,			urllib.parse.quote_plus(single_url.split("""/"""   )[-1]   )   )
        dummy_data_list.append(a   )
       return dummy_data_list
      def 						lowercase__					(     self				,			a				,			a   ):
       for download_callback in self.download_callbacks:
        download_callback(a   )
       # we force the name of each key to be the last file / folder name of the url path
       # if the url has arguments, we need to encode them with urllib.parse.quote_plus
       snake_case__  :    str							       =os.path.join(a				,			urllib.parse.quote_plus(data_url.split("""/"""   )[-1]   )   )
       if os.path.exists(a   ) or not self.load_existing_dummy_data:
        return value
       else:
        # Backward compatibility, maybe deprecate at one point.
        # For many datasets with single url calls to dl_manager.download_and_extract,
        # the dummy_data.zip file is actually the zipped downloaded file
        # while now we expected the dummy_data.zip file to be a directory containing
        # the downloaded file.
        return path_to_dummy_data
      def 						lowercase__					(     self   ):
       pass
      def 						lowercase__					(     self   ):
       pass
      def 						lowercase__					(     self				,			a   ):
       def _iter_archive_members(a   ):
        # this preserves the order of the members inside the ZIP archive
        snake_case__  :    int							       =Path(self.dummy_file   ).parent
        snake_case__  :    int							       =path.relative_to(a   )
        with ZipFile(self.local_path_to_dummy_data   ) as zip_file:
         snake_case__  :    List[str]							       =zip_file.namelist()
        for member in members:
         if member.startswith(relative_path.as_posix()   ):
          yield dummy_parent_path.joinpath(a   )

       snake_case__  :    List[Any]							       =Path(a   )
       snake_case__  :    Dict							       =_iter_archive_members(a   ) if self.use_local_dummy_data else path.rglob("""*"""   )
       for file_path in file_paths:
        if file_path.is_file() and not file_path.name.startswith((""".""", """__""")   ):
         yield file_path.relative_to(a   ).as_posix(), file_path.open("""rb"""   )



      def 						lowercase__					(     self				,			a   ):
       if not isinstance(a				,			a   ):
        snake_case__  :    List[Any]							       =[paths]
       for path in paths:
        if os.path.isfile(a   ):
         if os.path.basename(a   ).startswith((""".""", """__""")   ):
          return
         yield path
        else:
         for dirpath, dirnames, filenames in os.walk(a   ):
          if os.path.basename(a   ).startswith((""".""", """__""")   ):
           continue
          dirnames.sort()
          for filename in sorted(a   ):
           if filename.startswith((""".""", """__""")   ):
            continue
           yield os.path.join(a				,			a   )













import math
from typing import Dict, Iterable, List, Optional, Tuple, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import normalize, rescale, resize, to_channel_dimension_format
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    get_image_size,
    is_torch_available,
    is_torch_tensor,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


if is_torch_available():
       import torch

if is_vision_available():
       import PIL


__lowerCamelCase	:		Union[str, Any]										=   logging.get_logger(__name__)







def 			A__							(      _a  :					np.ndarray		,  _a  :					Union[int, Iterable[int]]		,  _a  :					bool		,  _a  :					int	):





 '''simple docstring'''







 def constraint_to_multiple_of(_a  :					Union[str, Any]		,  _a  :					List[str]		,  _a  :					str=0		,  _a  :					Any=None	):
  snake_case__  :    Any							       =round(val / multiple	) * multiple

  if max_val is not None and x > max_val:
   snake_case__  :    int							       =math.floor(val / multiple	) * multiple

  if x < min_val:
   snake_case__  :    Dict							       =math.ceil(val / multiple	) * multiple

  return x

 snake_case__  :    str							       =(output_size, output_size) if isinstance(_a		,  _a	) else output_size

 snake_case__       ,   snake_case__  :    Dict							       =get_image_size(_a	)
 snake_case__       ,   snake_case__  :    int							       =output_size

 # determine new height and width
 snake_case__  :    Tuple							       =output_height / input_height
 snake_case__  :    Optional[Any]							       =output_width / input_width

 if keep_aspect_ratio:
  # scale as little as possible
  if abs(1 - scale_width	) < abs(1 - scale_height	):
   # fit width
   snake_case__  :    Optional[int]							       =scale_width
  else:
   # fit height
   snake_case__  :    Any							       =scale_height

 snake_case__  :    Optional[int]							       =constraint_to_multiple_of(scale_height * input_height		,  multiple=_a	)
 snake_case__  :    str							       =constraint_to_multiple_of(scale_width * input_width		,  multiple=_a	)

 return (new_height, new_width)







class  _lowercase   (   _A						):
      _a							:						List[Any]										=							['pixel_values']
      def __init__(     self				,			a = True				,			a = None				,			a = PILImageResampling.BILINEAR				,			a = False				,			a = 1				,			a = True				,			a = 1 / 2_5_5				,			a = True				,			a = None				,			a = None				,			**a				,			):
       super().__init__(**a   )
       snake_case__  :    Any							       =size if size is not None else {"""height""": 3_8_4, """width""": 3_8_4}
       snake_case__  :    List[Any]							       =get_size_dict(a   )
       snake_case__  :    Tuple							       =do_resize
       snake_case__  :    Tuple							       =size
       snake_case__  :    Any							       =keep_aspect_ratio
       snake_case__  :    List[Any]							       =ensure_multiple_of
       snake_case__  :    Tuple							       =resample
       snake_case__  :    str							       =do_rescale
       snake_case__  :    int							       =rescale_factor
       snake_case__  :    Tuple							       =do_normalize
       snake_case__  :    int							       =image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
       snake_case__  :    Any							       =image_std if image_std is not None else IMAGENET_STANDARD_STD
      def 						lowercase__					(     self				,			a				,			a				,			a = False				,			a = 1				,			a = PILImageResampling.BICUBIC				,			a = None				,			**a				,			):
       snake_case__  :    Tuple							       =get_size_dict(a   )
       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()}"   )
       snake_case__  :    Optional[int]							       =get_resize_output_image_size(
           a				,			output_size=(size["""height"""], size["""width"""])				,			keep_aspect_ratio=a				,			multiple=a				,			)
       return resize(a				,			size=a				,			resample=a				,			data_format=a				,			**a   )
      def 						lowercase__					(     self				,			a				,			a				,			a = None				,			**a				,			):
       return rescale(a				,			scale=a				,			data_format=a				,			**a   )
      def 						lowercase__					(     self				,			a				,			a				,			a				,			a = None				,			**a				,			):
       return normalize(a				,			mean=a				,			std=a				,			data_format=a				,			**a   )
      def 						lowercase__					(     self				,			a				,			a = None				,			a = None				,			a = None				,			a = None				,			a = None				,			a = None				,			a = None				,			a = None				,			a = None				,			a = None				,			a = None				,			a = ChannelDimension.FIRST				,			**a				,			):
       snake_case__  :    Optional[int]							       =do_resize if do_resize is not None else self.do_resize
       snake_case__  :    List[Any]							       =size if size is not None else self.size
       snake_case__  :    int							       =get_size_dict(a   )
       snake_case__  :    Optional[int]							       =keep_aspect_ratio if keep_aspect_ratio is not None else self.keep_aspect_ratio
       snake_case__  :    int							       =ensure_multiple_of if ensure_multiple_of is not None else self.ensure_multiple_of
       snake_case__  :    Optional[Any]							       =resample if resample is not None else self.resample
       snake_case__  :    Optional[int]							       =do_rescale if do_rescale is not None else self.do_rescale
       snake_case__  :    str							       =rescale_factor if rescale_factor is not None else self.rescale_factor
       snake_case__  :    Optional[Any]							       =do_normalize if do_normalize is not None else self.do_normalize
       snake_case__  :    Tuple							       =image_mean if image_mean is not None else self.image_mean
       snake_case__  :    int							       =image_std if image_std is not None else self.image_std

       snake_case__  :    int							       =make_list_of_images(a   )

       if not valid_images(a   ):
        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."""   )

       # All transformations expect numpy arrays.
       snake_case__  :    int							       =[to_numpy_array(a   ) for image in images]

       if do_resize:
        snake_case__  :    List[str]							       =[self.resize(image=a				,			size=a				,			resample=a   ) for image in images]

       if do_rescale:
        snake_case__  :    List[Any]							       =[self.rescale(image=a				,			scale=a   ) for image in images]

       if do_normalize:
        snake_case__  :    str							       =[self.normalize(image=a				,			mean=a				,			std=a   ) for image in images]

       snake_case__  :    List[Any]							       =[to_channel_dimension_format(a				,			a   ) for image in images]

       snake_case__  :    Union[str, Any]							       ={"""pixel_values""": images}
       return BatchFeature(data=a				,			tensor_type=a   )



      def 						lowercase__					(     self				,			a				,			a = None   ):
       snake_case__  :    Optional[Any]							       =outputs.logits

       # Resize logits and compute semantic segmentation maps
       if target_sizes is not None:
        if len(a   ) != len(a   ):
         raise ValueError(
             """Make sure that you pass in as many target sizes as the batch dimension of the logits"""   )

        if is_torch_tensor(a   ):
         snake_case__  :    Optional[Any]							       =target_sizes.numpy()

        snake_case__  :    Optional[Any]							       =[]

        for idx in range(len(a   )   ):
         snake_case__  :    List[str]							       =torch.nn.functional.interpolate(
             logits[idx].unsqueeze(dim=0   )				,			size=target_sizes[idx]				,			mode="""bilinear"""				,			align_corners=a   )
         snake_case__  :    List[Any]							       =resized_logits[0].argmax(dim=0   )
         semantic_segmentation.append(a   )
       else:
        snake_case__  :    List[str]							       =logits.argmax(dim=1   )
        snake_case__  :    Optional[int]							       =[semantic_segmentation[i] for i in range(semantic_segmentation.shape[0]   )]

       return semantic_segmentation





