'''simple docstring'''

import gc
import unittest

import numpy as np
import torch

from diffusers import StableDiffusionKDiffusionPipeline
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()


@slow
@require_torch_gpu
class     __UpperCAmelCase       ( unittest.TestCase		):





  def 						UpperCAmelCase_	(	self ):
     # clean up the VRAM after each test
     super().tearDown()
     gc.collect()
     torch.cuda.empty_cache()





  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      StableDiffusionKDiffusionPipeline.from_pretrained('''CompVis/stable-diffusion-v1-4''' )
     lowerCAmelCase_			=      sd_pipe.to(_lowerCamelCase )
     sd_pipe.set_progress_bar_config(disable=_lowerCamelCase )

     sd_pipe.set_scheduler('''sample_euler''' )

     lowerCAmelCase_			=      '''A painting of a squirrel eating a burger'''
     lowerCAmelCase_			=      torch.manual_seed(0 )
     lowerCAmelCase_			=      sd_pipe([prompt]    ,     generator=_lowerCamelCase    ,     guidance_scale=9.0    ,     num_inference_steps=20    ,     output_type='''np''' )

     lowerCAmelCase_			=      output.images

     lowerCAmelCase_			=      image[0, -3:, -3:, -1]

     assert image.shape == (1, 512, 512, 3)
     lowerCAmelCase_			=      np.array([0.04_47, 0.04_92, 0.04_68, 0.04_08, 0.03_83, 0.04_08, 0.03_54, 0.03_80, 0.03_39] )

     assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2





  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      StableDiffusionKDiffusionPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base''' )
     lowerCAmelCase_			=      sd_pipe.to(_lowerCamelCase )
     sd_pipe.set_progress_bar_config(disable=_lowerCamelCase )

     sd_pipe.set_scheduler('''sample_euler''' )

     lowerCAmelCase_			=      '''A painting of a squirrel eating a burger'''
     lowerCAmelCase_			=      torch.manual_seed(0 )
     lowerCAmelCase_			=      sd_pipe([prompt]    ,     generator=_lowerCamelCase    ,     guidance_scale=9.0    ,     num_inference_steps=20    ,     output_type='''np''' )

     lowerCAmelCase_			=      output.images

     lowerCAmelCase_			=      image[0, -3:, -3:, -1]

     assert image.shape == (1, 512, 512, 3)
     lowerCAmelCase_			=      np.array([0.12_37, 0.13_20, 0.14_38, 0.13_59, 0.13_90, 0.11_32, 0.12_77, 0.11_75, 0.11_12] )

     assert np.abs(image_slice.flatten() - expected_slice ).max() < 5E-1







  def 						UpperCAmelCase_	(	self ):
     lowerCAmelCase_			=      StableDiffusionKDiffusionPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base''' )
     lowerCAmelCase_			=      sd_pipe.to(_lowerCamelCase )
     sd_pipe.set_progress_bar_config(disable=_lowerCamelCase )

     sd_pipe.set_scheduler('''sample_dpmpp_2m''' )

     lowerCAmelCase_			=      '''A painting of a squirrel eating a burger'''
     lowerCAmelCase_			=      torch.manual_seed(0 )
     lowerCAmelCase_			=      sd_pipe(
         [prompt]    ,     generator=_lowerCamelCase    ,     guidance_scale=7.5    ,     num_inference_steps=15    ,     output_type='''np'''    ,     use_karras_sigmas=_lowerCamelCase    ,     )

     lowerCAmelCase_			=      output.images

     lowerCAmelCase_			=      image[0, -3:, -3:, -1]

     assert image.shape == (1, 512, 512, 3)
     lowerCAmelCase_			=      np.array(
         [0.11_38_16_89, 0.12_11_29_21, 0.1_38_94_57, 0.12_54_96_06, 0.1_24_49_64, 0.10_83_15_17, 0.11_56_28_66, 0.10_86_78_16, 0.10_49_90_48] )

     assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2






'''simple docstring'''

import colorsys

from PIL import Image  # type: ignore



def snake_case_      (					__snake_case  :							float	,   __snake_case  :							float	,   __snake_case  :							int)			->					float:
   lowerCAmelCase_			=      x
   lowerCAmelCase_			=      y
   for step in range(__snake_case):  # noqa: B007
      lowerCAmelCase_			=      a * a - b * b + x
      lowerCAmelCase_			=      2 * a * b + y
      lowerCAmelCase_			=      a_new

      # divergence happens for all complex number with an absolute value
      # greater than 4
      if a * a + b * b > 4:
         break
   return step / (max_step - 1)



def snake_case_      (					__snake_case  :							float)			->					tuple:
   if distance == 1:
      return (0, 0, 0)
   else:
      return (255, 255, 255)



def snake_case_      (					__snake_case  :							float)			->					tuple:
   if distance == 1:
      return (0, 0, 0)
   else:
      return tuple(round(i * 255) for i in colorsys.hsv_to_rgb(__snake_case	,   1	,   1))



def snake_case_      (					__snake_case  :							int = 800	,   __snake_case  :							int = 600	,   __snake_case  :							float = -0.6	,   __snake_case  :							float = 0	,   __snake_case  :							float = 3.2	,   __snake_case  :							int = 50	,   __snake_case  :							bool = True	,   )			->					Image.Image:
   lowerCAmelCase_			=      Image.new('''RGB'''	,   (image_width, image_height))
   lowerCAmelCase_			=      img.load()

   # loop through the image-coordinates
   for image_x in range(__snake_case):
      for image_y in range(__snake_case):
         # determine the figure-coordinates based on the image-coordinates
         lowerCAmelCase_			=      figure_width / image_width * image_height
         lowerCAmelCase_			=      figure_center_x + (image_x / image_width - 0.5) * figure_width
         lowerCAmelCase_			=      figure_center_y + (image_y / image_height - 0.5) * figure_height

         lowerCAmelCase_			=      get_distance(__snake_case	,   __snake_case	,   __snake_case)

         # color the corresponding pixel based on the selected coloring-function
         if use_distance_color_coding:
            lowerCAmelCase_			=      get_color_coded_rgb(__snake_case)
         else:
            lowerCAmelCase_			=      get_black_and_white_rgb(__snake_case)

   return img


if __name__ == "__main__":
 import doctest

 doctest.testmod()

 # colored version, full figure
 A_     :						List[str]    							=get_image()

 # uncomment for colored version, different section, zoomed in
 # img = get_image(figure_center_x = -0.6, figure_center_y = -0.4,
 # figure_width = 0.8)

 # uncomment for black and white version, full figure
 # img = get_image(use_distance_color_coding = False)

 # uncomment to save the image
 # img.save("mandelbrot.png")

 img.show()





