



"""simple docstring"""




import warnings
from typing import List, Optional, Union

import numpy as np
import PIL
import torch
from PIL import Image

from .configuration_utils import ConfigMixin, register_to_config
from .utils import CONFIG_NAME, PIL_INTERPOLATION, deprecate






class 	UpperCAmelCase_  (				a_						):
      __SCREAMING_SNAKE_CASE						:   int      = CONFIG_NAME



      @register_to_config
      def __init__(     self     :	Union[str, Any]     ,	A     :	List[Any] = True     ,	A     :	List[str] = 8     ,	A     :	Union[str, Any] = "lanczos"     ,	A     :	Optional[Any] = True     ,	A     :	Union[str, Any] = False     ,	):
           super().__init__()



      @staticmethod
      def 						snake_case_					(     A     :	Dict						):

           if images.ndim == 3:
                _UpperCAmelCase			:	int								=    images[None, ...]
           _UpperCAmelCase			:	Tuple								=    (images * 2_5_5).round().astype("uint8"						)
           if images.shape[-1] == 1:
                # special case for grayscale (single channel) images
                _UpperCAmelCase			:	Tuple								=    [Image.fromarray(image.squeeze()     ,	mode="L"						) for image in images]
           else:
                _UpperCAmelCase			:	Tuple								=    [Image.fromarray(lowercase_						) for image in images]

           return pil_images



      @staticmethod
      def 						snake_case_					(     A     :	str						):

           if not isinstance(lowercase_     ,	lowercase_						):
                _UpperCAmelCase			:	List[Any]								=    [images]
           _UpperCAmelCase			:	List[Any]								=    [np.array(lowercase_						).astype(np.floataa						) / 2_5_5.0 for image in images]
           _UpperCAmelCase			:	List[Any]								=    np.stack(lowercase_     ,	axis=0						)

           return images



      @staticmethod
      def 						snake_case_					(     A     :	List[Any]						):

           if images.ndim == 3:
                _UpperCAmelCase			:	Any								=    images[..., None]

           _UpperCAmelCase			:	Dict								=    torch.from_numpy(images.transpose(0     ,	3     ,	1     ,	2						)						)
           return images



      @staticmethod
      def 						snake_case_					(     A     :	Union[str, Any]						):
           _UpperCAmelCase			:	Any								=    images.cpu().permute(0     ,	2     ,	3     ,	1						).float().numpy()
           return images



      @staticmethod
      def 						snake_case_					(     A     :	Dict						):
           return 2.0 * images - 1.0



      @staticmethod
      def 						snake_case_					(     A     :	Optional[Any]						):
           return (images / 2 + 0.5).clamp(0     ,	1						)



      @staticmethod
      def 						snake_case_					(     A     :	Optional[Any]						):
           _UpperCAmelCase			:	Tuple								=    image.convert("RGB"						)
           return image



      def 						snake_case_					(     self     :	List[str]     ,	A     :	int     ,	A     :	Union[str, Any] = None     ,	A     :	Union[str, Any] = None     ,	):

           if height is None:
                _UpperCAmelCase			:	List[Any]								=    image.height
           if width is None:
                _UpperCAmelCase			:	str								=    image.width

           _UpperCAmelCase				,       _UpperCAmelCase			:	Tuple								=    (
               x - x % self.config.vae_scale_factor for x in (width, height)
           )  # resize to integer multiple of vae_scale_factor
           _UpperCAmelCase			:	int								=    image.resize((width, height)     ,	resample=PIL_INTERPOLATION[self.config.resample]						)
           return image



      def 						snake_case_					(     self     :	Tuple     ,	A     :	Any     ,	A     :	str = None     ,	A     :	Tuple = None     ,	):
           _UpperCAmelCase			:	Dict								=    (PIL.Image.Image, np.ndarray, torch.Tensor)
           if isinstance(lowercase_     ,	lowercase_						):
                _UpperCAmelCase			:	Optional[int]								=    [image]
           elif not (isinstance(lowercase_     ,	lowercase_						) and all(isinstance(lowercase_     ,	lowercase_						) for i in image						)):
                raise ValueError(
                    f'Input is in incorrect format: {[type(lowercase_						) for i in image]}. Currently, we only support {", ".join(lowercase_						)}'						)

           if isinstance(image[0]     ,	PIL.Image.Image						):
                if self.config.do_convert_rgb:
                     _UpperCAmelCase			:	List[Any]								=    [self.convert_to_rgb(lowercase_						) for i in image]
                if self.config.do_resize:
                     _UpperCAmelCase			:	Dict								=    [self.resize(lowercase_     ,	lowercase_     ,	lowercase_						) for i in image]
                _UpperCAmelCase			:	List[str]								=    self.pil_to_numpy(lowercase_						)  # to np
                _UpperCAmelCase			:	Union[str, Any]								=    self.numpy_to_pt(lowercase_						)  # to pt

           elif isinstance(image[0]     ,	np.ndarray						):
                _UpperCAmelCase			:	Union[str, Any]								=    np.concatenate(lowercase_     ,	axis=0						) if image[0].ndim == 4 else np.stack(lowercase_     ,	axis=0						)
                _UpperCAmelCase			:	List[Any]								=    self.numpy_to_pt(lowercase_						)
                _UpperCAmelCase				,       _UpperCAmelCase				,       _UpperCAmelCase				,       _UpperCAmelCase			:	Optional[int]								=    image.shape
                if self.config.do_resize and (
                    height % self.config.vae_scale_factor != 0 or width % self.config.vae_scale_factor != 0
                ):
                     raise ValueError(
                         f'Currently we only support resizing for PIL image - please resize your numpy array to be divisible by {self.config.vae_scale_factor}'
                         f'currently the sizes are {height} and {width}. You can also pass a PIL image instead to use resize option in VAEImageProcessor'						)

           elif isinstance(image[0]     ,	torch.Tensor						):
                _UpperCAmelCase			:	int								=    torch.cat(lowercase_     ,	axis=0						) if image[0].ndim == 4 else torch.stack(lowercase_     ,	axis=0						)
                _UpperCAmelCase				,       _UpperCAmelCase				,       _UpperCAmelCase				,       _UpperCAmelCase			:	Optional[Any]								=    image.shape

                # don't need any preprocess if the image is latents
                if channel == 4:
                     return image

                if self.config.do_resize and (
                    height % self.config.vae_scale_factor != 0 or width % self.config.vae_scale_factor != 0
                ):
                     raise ValueError(
                         f'Currently we only support resizing for PIL image - please resize your pytorch tensor to be divisible by {self.config.vae_scale_factor}'
                         f'currently the sizes are {height} and {width}. You can also pass a PIL image instead to use resize option in VAEImageProcessor'						)

        # expected range [0,1], normalize to [-1,1]
           _UpperCAmelCase			:	List[Any]								=    self.config.do_normalize
           if image.min() < 0:
                warnings.warn(
                    "Passing `image` as torch tensor with value range in [-1,1] is deprecated. The expected value range for image tensor is [0,1] "
                    f'when passing as pytorch tensor or numpy Array. You passed `image` with value range [{image.min()},{image.max()}]'     ,	lowercase_     ,	)
                _UpperCAmelCase			:	Tuple								=    False

           if do_normalize:
                _UpperCAmelCase			:	List[Any]								=    self.normalize(lowercase_						)

           return image



      def 						snake_case_					(     self     :	str     ,	A     :	int     ,	A     :	int = "pil"     ,	A     :	Optional[Any] = None     ,	):

           if not isinstance(lowercase_     ,	torch.Tensor						):
                raise ValueError(
                    f'Input for postprocessing is in incorrect format: {type(lowercase_						)}. We only support pytorch tensor'						)
           if output_type not in ["latent", "pt", "np", "pil"]:
                _UpperCAmelCase			:	Union[str, Any]								=    (
                    f'the output_type {output_type} is outdated and has been set to `np`. Please make sure to set it to one of these instead: '
                    "`pil`, `np`, `pt`, `latent`"
                )
                deprecate("Unsupported output_type"     ,	"1.0.0"     ,	lowercase_     ,	standard_warn=lowercase_						)
                _UpperCAmelCase			:	Dict								=    "np"

           if output_type == "latent":
                return image

           if do_denormalize is None:
                _UpperCAmelCase			:	Union[str, Any]								=    [self.config.do_normalize] * image.shape[0]

           _UpperCAmelCase			:	Any								=    torch.stack(
               [self.denormalize(image[i]						) if do_denormalize[i] else image[i] for i in range(image.shape[0]						)]						)

           if output_type == "pt":
                return image

           _UpperCAmelCase			:	List[str]								=    self.pt_to_numpy(lowercase_						)

           if output_type == "np":
                return image

           if output_type == "pil":
                return self.numpy_to_pil(lowercase_						)







class 	UpperCAmelCase_  (				a_						):
      __SCREAMING_SNAKE_CASE						:   List[str]      = CONFIG_NAME



      @register_to_config
      def __init__(     self     :	Any     ,	A     :	Optional[int] = True     ,	A     :	Union[str, Any] = 8     ,	A     :	List[str] = "lanczos"     ,	A     :	Union[str, Any] = True     ,	):
           super().__init__()



      @staticmethod
      def 						snake_case_					(     A     :	Any						):

           if images.ndim == 3:
                _UpperCAmelCase			:	Union[str, Any]								=    images[None, ...]
           _UpperCAmelCase			:	Dict								=    (images * 2_5_5).round().astype("uint8"						)
           if images.shape[-1] == 1:
                # special case for grayscale (single channel) images
                _UpperCAmelCase			:	Union[str, Any]								=    [Image.fromarray(image.squeeze()     ,	mode="L"						) for image in images]
           else:
                _UpperCAmelCase			:	Optional[Any]								=    [Image.fromarray(image[:, :, :3]						) for image in images]

           return pil_images



      @staticmethod
      def 						snake_case_					(     A     :	str						):
           return image[:, :, 1] * 2**8 + image[:, :, 2]



      def 						snake_case_					(     self     :	Optional[int]     ,	A     :	List[Any]						):

           if images.ndim == 3:
                _UpperCAmelCase			:	Optional[int]								=    images[None, ...]
           _UpperCAmelCase			:	Tuple								=    images[:, :, :, 3:]
           if images.shape[-1] == 6:
                _UpperCAmelCase			:	int								=    (images_depth * 2_5_5).round().astype("uint8"						)
                _UpperCAmelCase			:	Optional[Any]								=    [
                    Image.fromarray(self.rgblike_to_depthmap(lowercase_						)     ,	mode="I;16"						) for image_depth in images_depth
                ]
           elif images.shape[-1] == 4:
                _UpperCAmelCase			:	Optional[Any]								=    (images_depth * 6_5_5_3_5.0).astype(np.uintaa						)
                _UpperCAmelCase			:	List[str]								=    [Image.fromarray(lowercase_     ,	mode="I;16"						) for image_depth in images_depth]
           else:
                raise Exception("Not supported"						)

           return pil_images



      def 						snake_case_					(     self     :	int     ,	A     :	Union[str, Any]     ,	A     :	Any = "pil"     ,	A     :	Optional[Any] = None     ,	):

           if not isinstance(lowercase_     ,	torch.Tensor						):
                raise ValueError(
                    f'Input for postprocessing is in incorrect format: {type(lowercase_						)}. We only support pytorch tensor'						)
           if output_type not in ["latent", "pt", "np", "pil"]:
                _UpperCAmelCase			:	List[str]								=    (
                    f'the output_type {output_type} is outdated and has been set to `np`. Please make sure to set it to one of these instead: '
                    "`pil`, `np`, `pt`, `latent`"
                )
                deprecate("Unsupported output_type"     ,	"1.0.0"     ,	lowercase_     ,	standard_warn=lowercase_						)
                _UpperCAmelCase			:	Optional[Any]								=    "np"

           if do_denormalize is None:
                _UpperCAmelCase			:	Dict								=    [self.config.do_normalize] * image.shape[0]

           _UpperCAmelCase			:	Tuple								=    torch.stack(
               [self.denormalize(image[i]						) if do_denormalize[i] else image[i] for i in range(image.shape[0]						)]						)

           _UpperCAmelCase			:	List[str]								=    self.pt_to_numpy(lowercase_						)

           if output_type == "np":
                if image.shape[-1] == 6:
                     _UpperCAmelCase			:	Optional[Any]								=    np.stack([self.rgblike_to_depthmap(im[:, :, 3:]						) for im in image]     ,	axis=0						)
                else:
                     _UpperCAmelCase			:	Any								=    image[:, :, :, 3:]
                return image[:, :, :, :3], image_depth

           if output_type == "pil":
                return self.numpy_to_pil(lowercase_						), self.numpy_to_depth(lowercase_						)
           else:
                raise Exception(f'This type {output_type} is not supported'						)








from doctest import testmod
from math import sqrt
def      lowerCamelCase					(						a_	)	->			list:
							lowerCAmelCase_										=     []
							if num < 1:
														return facs
							facs.append(1	)
							if num == 1:
														return facs
							facs.append(a_	)
							for i in range(2 ,						int(sqrt(a_	)	) + 1	):
														if num % i == 0:  # If i is a factor of num
																					facs.append(a_	)
																					lowerCAmelCase_										=     num // i  # num//i is the other factor of num
																					if d != i:  # If d and i are distinct
																												facs.append(a_	)  # we have found another factor
							facs.sort()
							return facs


if __name__ == "__main__":
		testmod(name="""factors_of_a_number""", verbose=True)



