






'''simple docstring'''

from __future__ import annotations

from scipy.special import comb  # type: ignore







class 	SCREAMING_SNAKE_CASE	:




       def __init__(					self      : int	,    A__      : list[tuple[float, float]]					):


            """simple docstring"""







            __lowerCamelCase						:       int				   =				list_of_points
            # Degree determines the flexibility of the curve.
            # Degree = 1 will produce a straight line.
            __lowerCamelCase						:       Union[str, Any]				   =				len(_lowerCAmelCase					) - 1




       def 					a_					(					self      : Tuple	,    A__      : float					):


            """simple docstring"""







            assert 0 <= t <= 1, "Time t must be between 0 and 1."
            __lowerCamelCase						:       Dict				   =				[]
            for i in range(len(self.list_of_points					)					):
                 # basis function for each i
                 output_values.append(
                     comb(self.degree	,    _lowerCAmelCase					) * ((1 - t) ** (self.degree - i)) * (t**i)					)
            # the basis must sum up to 1 for it to produce a valid Bezier curve.
            assert round(sum(_lowerCAmelCase					)	,    5					) == 1
            return output_values




       def 					a_					(					self      : List[str]	,    A__      : float					):


            """simple docstring"""







            assert 0 <= t <= 1, "Time t must be between 0 and 1."

            __lowerCamelCase						:       List[str]				   =				self.basis_function(_lowerCAmelCase					)
            __lowerCamelCase						:       Optional[int]				   =				0.0
            __lowerCamelCase						:       Union[str, Any]				   =				0.0
            for i in range(len(self.list_of_points					)					):
                 # For all points, sum up the product of i-th basis function and i-th point.
                 x += basis_function[i] * self.list_of_points[i][0]
                 y += basis_function[i] * self.list_of_points[i][1]
            return (x, y)




       def 					a_					(					self      : Optional[int]	,    A__      : float = 0.01					):


            """simple docstring"""







            from matplotlib import pyplot as plt  # type: ignore

            __lowerCamelCase						:       List[Any]				   =				[]  # x coordinates of points to plot
            __lowerCamelCase						:       Optional[int]				   =				[]  # y coordinates of points to plot

            __lowerCamelCase						:       int				   =				0.0
            while t <= 1:
                 __lowerCamelCase						:       Dict				   =				self.bezier_curve_function(_lowerCAmelCase					)
                 to_plot_x.append(value[0]					)
                 to_plot_y.append(value[1]					)
                 t += step_size

            __lowerCamelCase						:       str				   =				[i[0] for i in self.list_of_points]
            __lowerCamelCase						:       Optional[int]				   =				[i[1] for i in self.list_of_points]

            plt.plot(
                _lowerCAmelCase	,    _lowerCAmelCase	,    color="""blue"""	,    label="""Curve of Degree """ + str(self.degree					)	,    )
            plt.scatter(_lowerCAmelCase	,    _lowerCAmelCase	,    color="""red"""	,    label="""Control Points"""					)
            plt.legend()
            plt.show()


if __name__ == "__main__":
   import doctest

   doctest.testmod()

   BezierCurve([(1, 2), (3, 5)]).plot_curve()  # degree 1
   BezierCurve([(0, 0), (5, 5), (5, 0)]).plot_curve()  # degree 2
   BezierCurve([(0, 0), (5, 5), (5, 0), (2.5, -2.5)]).plot_curve()  # degree 3











'''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 	SCREAMING_SNAKE_CASE	(      unittest.TestCase  ):




       def 					a_					(					self      : Union[str, Any]					):


            """simple docstring"""







            super().tearDown()
            gc.collect()
            torch.cuda.empty_cache()




       def 					a_					(					self      : Optional[int]					):


            """simple docstring"""







            __lowerCamelCase						:       Dict				   =				StableDiffusionKDiffusionPipeline.from_pretrained("""CompVis/stable-diffusion-v1-4"""					)
            __lowerCamelCase						:       Tuple				   =				sd_pipe.to(A__					)
            sd_pipe.set_progress_bar_config(disable=A__					)

            sd_pipe.set_scheduler("""sample_euler"""					)

            __lowerCamelCase						:       Any				   =				"""A painting of a squirrel eating a burger"""
            __lowerCamelCase						:       List[Any]				   =				torch.manual_seed(0					)
            __lowerCamelCase						:       Dict				   =				sd_pipe([prompt]	,    generator=A__	,    guidance_scale=9.0	,    num_inference_steps=20	,    output_type="""np"""					)

            __lowerCamelCase						:       Tuple				   =				output.images

            __lowerCamelCase						:       int				   =				image[0, -3:, -3:, -1]

            assert image.shape == (1, 512, 512, 3)
            __lowerCamelCase						:       List[str]				   =				np.array([0.0447, 0.0492, 0.0468, 0.0408, 0.0383, 0.0408, 0.0354, 0.0380, 0.0339]					)

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




       def 					a_					(					self      : Dict					):


            """simple docstring"""







            __lowerCamelCase						:       Any				   =				StableDiffusionKDiffusionPipeline.from_pretrained("""stabilityai/stable-diffusion-2-1-base"""					)
            __lowerCamelCase						:       int				   =				sd_pipe.to(A__					)
            sd_pipe.set_progress_bar_config(disable=A__					)

            sd_pipe.set_scheduler("""sample_euler"""					)

            __lowerCamelCase						:       List[Any]				   =				"""A painting of a squirrel eating a burger"""
            __lowerCamelCase						:       Union[str, Any]				   =				torch.manual_seed(0					)
            __lowerCamelCase						:       int				   =				sd_pipe([prompt]	,    generator=A__	,    guidance_scale=9.0	,    num_inference_steps=20	,    output_type="""np"""					)

            __lowerCamelCase						:       List[str]				   =				output.images

            __lowerCamelCase						:       Optional[int]				   =				image[0, -3:, -3:, -1]

            assert image.shape == (1, 512, 512, 3)
            __lowerCamelCase						:       Optional[int]				   =				np.array([0.1237, 0.1320, 0.1438, 0.1359, 0.1390, 0.1132, 0.1277, 0.1175, 0.1112]					)

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




       def 					a_					(					self      : str					):


            """simple docstring"""







            __lowerCamelCase						:       List[str]				   =				StableDiffusionKDiffusionPipeline.from_pretrained("""stabilityai/stable-diffusion-2-1-base"""					)
            __lowerCamelCase						:       List[str]				   =				sd_pipe.to(A__					)
            sd_pipe.set_progress_bar_config(disable=A__					)

            sd_pipe.set_scheduler("""sample_dpmpp_2m"""					)

            __lowerCamelCase						:       int				   =				"""A painting of a squirrel eating a burger"""
            __lowerCamelCase						:       Tuple				   =				torch.manual_seed(0					)
            __lowerCamelCase						:       Union[str, Any]				   =				sd_pipe(
                [prompt]	,    generator=A__	,    guidance_scale=7.5	,    num_inference_steps=15	,    output_type="""np"""	,    use_karras_sigmas=A__	,    )

            __lowerCamelCase						:       int				   =				output.images

            __lowerCamelCase						:       Any				   =				image[0, -3:, -3:, -1]

            assert image.shape == (1, 512, 512, 3)
            __lowerCamelCase						:       int				   =				np.array(
                [0.1138_1689, 0.1211_2921, 0.138_9457, 0.1254_9606, 0.124_4964, 0.1083_1517, 0.1156_2866, 0.1086_7816, 0.1049_9048]					)

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



