






'''simple docstring'''

import gc
import random
import unittest

import numpy as np
import torch
from PIL import Image
from transformers import CLIPImageProcessor, CLIPVisionConfig, CLIPVisionModelWithProjection

from diffusers import (
    AutoencoderKL,
    DPMSolverMultistepScheduler,
    PNDMScheduler,
    StableDiffusionImageVariationPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import floats_tensor, load_image, load_numpy, nightly, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import IMAGE_VARIATION_BATCH_PARAMS, IMAGE_VARIATION_PARAMS
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()






class 				__magic_name__  (						UpperCAmelCase_,    UpperCAmelCase_,    UpperCAmelCase_,    unittest.TestCase					):







	"""simple docstring"""



	SCREAMING_SNAKE_CASE_       :   Any   =	StableDiffusionImageVariationPipeline
	SCREAMING_SNAKE_CASE_       :   Optional[Any]   =	IMAGE_VARIATION_PARAMS
	SCREAMING_SNAKE_CASE_       :   Tuple   =	IMAGE_VARIATION_BATCH_PARAMS
	SCREAMING_SNAKE_CASE_       :   int   =	frozenset([]					)
	# TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess
	SCREAMING_SNAKE_CASE_       :   Optional[Any]   =	frozenset([]					)
	def 							lowerCamelCase__		(				self							)    ->	Optional[Any]:
							torch.manual_seed(0							)
							lowercase_   : Optional[Any]   	=      UNetaDConditionModel(
							    block_out_channels=(32, 64)	,   layers_per_block=2	,   sample_size=32	,   in_channels=4	,   out_channels=4	,   down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D')	,   up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D')	,   cross_attention_dim=32	,   )
							lowercase_   : List[Any]   	=      PNDMScheduler(skip_prk_steps=_lowercase							)
							torch.manual_seed(0							)
							lowercase_   : Tuple   	=      AutoencoderKL(
							    block_out_channels=[32, 64]	,   in_channels=3	,   out_channels=3	,   down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D']	,   up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D']	,   latent_channels=4	,   )
							torch.manual_seed(0							)
							lowercase_   : List[str]   	=      CLIPVisionConfig(
							    hidden_size=32	,   projection_dim=32	,   intermediate_size=37	,   layer_norm_eps=1E-0_5	,   num_attention_heads=4	,   num_hidden_layers=5	,   image_size=32	,   patch_size=4	,   )
							lowercase_   : Optional[int]   	=      CLIPVisionModelWithProjection(_lowercase							)
							lowercase_   : int   	=      CLIPImageProcessor(crop_size=32	,   size=32							)

							lowercase_   : str   	=      {
							    'unet': unet,
							    'scheduler': scheduler,
							    'vae': vae,
							    'image_encoder': image_encoder,
							    'feature_extractor': feature_extractor,
							    'safety_checker': None,
							}
							return components
	def 							lowerCamelCase__		(				self	,   _lowercase	,   _lowercase=0							)    ->	Dict:
							lowercase_   : Tuple   	=      floats_tensor((1, 3, 32, 32)	,   rng=random.Random(_lowercase							)							)
							lowercase_   : str   	=      image.cpu().permute(0	,   2	,   3	,   1							)[0]
							lowercase_   : str   	=      Image.fromarray(np.uinta(_lowercase							)							).convert('RGB'							).resize((32, 32)							)
							if str(_lowercase							).startswith('mps'							):
													lowercase_   : int   	=      torch.manual_seed(_lowercase							)
							else:
													lowercase_   : Dict   	=      torch.Generator(device=_lowercase							).manual_seed(_lowercase							)
							lowercase_   : int   	=      {
							    'image': image,
							    'generator': generator,
							    'num_inference_steps': 2,
							    'guidance_scale': 6.0,
							    'output_type': 'numpy',
							}
							return inputs
	def 							lowerCamelCase__		(				self							)    ->	int:
							lowercase_   : Tuple   	=      'cpu'  # ensure determinism for the device-dependent torch.Generator
							lowercase_   : List[Any]   	=      self.get_dummy_components()
							lowercase_   : str   	=      StableDiffusionImageVariationPipeline(**_lowercase							)
							lowercase_   : List[Any]   	=      sd_pipe.to(_lowercase							)
							sd_pipe.set_progress_bar_config(disable=_lowercase							)

							lowercase_   : Optional[int]   	=      self.get_dummy_inputs(_lowercase							)
							lowercase_   : str   	=      sd_pipe(**_lowercase							).images
							lowercase_   : Optional[int]   	=      image[0, -3:, -3:, -1]

							assert image.shape == (1, 64, 64, 3)
							lowercase_   : int   	=      np.array([0.52_39, 0.57_23, 0.47_96, 0.50_49, 0.55_50, 0.46_85, 0.53_29, 0.48_91, 0.49_21]							)

							assert np.abs(image_slice.flatten() - expected_slice							).max() < 1E-3
	def 							lowerCamelCase__		(				self							)    ->	Union[str, Any]:
							lowercase_   : List[str]   	=      'cpu'  # ensure determinism for the device-dependent torch.Generator
							lowercase_   : Optional[Any]   	=      self.get_dummy_components()
							lowercase_   : Any   	=      StableDiffusionImageVariationPipeline(**_lowercase							)
							lowercase_   : Dict   	=      sd_pipe.to(_lowercase							)
							sd_pipe.set_progress_bar_config(disable=_lowercase							)

							lowercase_   : List[Any]   	=      self.get_dummy_inputs(_lowercase							)
							lowercase_   : Optional[int]   	=      2 * [inputs['image']]
							lowercase_   : Tuple   	=      sd_pipe(**_lowercase							)

							lowercase_   : List[str]   	=      output.images

							lowercase_   : Tuple   	=      image[-1, -3:, -3:, -1]

							assert image.shape == (2, 64, 64, 3)
							lowercase_   : Any   	=      np.array([0.68_92, 0.56_37, 0.58_36, 0.57_71, 0.62_54, 0.64_09, 0.55_80, 0.55_69, 0.52_89]							)

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







	def 							lowerCamelCase__		(				self							)    ->	Dict:
							super().test_inference_batch_single_identical(expected_max_diff=3E-3							)






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







	"""simple docstring"""



	def 							lowerCamelCase__		(				self							)    ->	int:
							super().tearDown()
							gc.collect()
							torch.cuda.empty_cache()
	def 							lowerCamelCase__		(				self	,   _lowercase	,   _lowercase="cpu"	,   _lowercase=torch.floataa	,   _lowercase=0							)    ->	str:
							lowercase_   : List[Any]   	=      torch.Generator(device=_lowercase							).manual_seed(_lowercase							)
							lowercase_   : Union[str, Any]   	=      load_image(
							    'https://huggingface.co/datasets/diffusers/test-arrays/resolve/main'
							    '/stable_diffusion_imgvar/input_image_vermeer.png'							)
							lowercase_   : Optional[Any]   	=      np.random.RandomState(_lowercase							).standard_normal((1, 4, 64, 64)							)
							lowercase_   : Optional[Any]   	=      torch.from_numpy(_lowercase							).to(device=_lowercase	,   dtype=_lowercase							)
							lowercase_   : Dict   	=      {
							    'image': init_image,
							    'latents': latents,
							    'generator': generator,
							    'num_inference_steps': 3,
							    'guidance_scale': 7.5,
							    'output_type': 'numpy',
							}
							return inputs
	def 							lowerCamelCase__		(				self							)    ->	List[Any]:
							lowercase_   : int   	=      StableDiffusionImageVariationPipeline.from_pretrained(
							    'lambdalabs/sd-image-variations-diffusers'	,   safety_checker=_lowercase							)
							lowercase_   : Optional[int]   	=      sd_pipe.to(_lowercase							)
							sd_pipe.set_progress_bar_config(disable=_lowercase							)

							lowercase_   : Tuple   	=      self.get_inputs(_lowercase							)
							lowercase_   : Any   	=      sd_pipe(**_lowercase							).images
							lowercase_   : List[Any]   	=      image[0, -3:, -3:, -1].flatten()

							assert image.shape == (1, 512, 512, 3)
							lowercase_   : Dict   	=      np.array([0.8_44_91, 0.9_07_89, 0.7_57_08, 0.7_87_34, 0.8_34_85, 0.7_00_99, 0.6_69_38, 0.6_87_27, 0.6_13_79]							)
							assert np.abs(image_slice - expected_slice							).max() < 6E-3
	def 							lowerCamelCase__		(				self							)    ->	str:
							lowercase_   : Tuple   	=      0

							def callback_fn(_lowercase	,   _lowercase	,   _lowercase							) -> None:
													lowercase_   : Any   	=      True
													nonlocal number_of_steps
													number_of_steps += 1
													if step == 1:
																			lowercase_   : List[Any]   	=      latents.detach().cpu().numpy()
																			assert latents.shape == (1, 4, 64, 64)
																			lowercase_   : List[str]   	=      latents[0, -3:, -3:, -1]
																			lowercase_   : Dict   	=      np.array(
																			    [-0.16_21, 0.28_37, -0.79_79, -0.12_21, -1.30_57, 0.76_81, -2.11_91, 0.04_64, 1.63_09]							)

																			assert np.abs(latents_slice.flatten() - expected_slice							).max() < 5E-2
													elif step == 2:
																			lowercase_   : Union[str, Any]   	=      latents.detach().cpu().numpy()
																			assert latents.shape == (1, 4, 64, 64)
																			lowercase_   : List[Any]   	=      latents[0, -3:, -3:, -1]
																			lowercase_   : Dict   	=      np.array([0.62_99, 1.75_00, 1.19_92, -2.15_82, -1.89_94, 0.73_34, -0.70_90, 1.01_37, 1.52_73]							)

																			assert np.abs(latents_slice.flatten() - expected_slice							).max() < 5E-2

							lowercase_   : Dict   	=      False

							lowercase_   : str   	=      StableDiffusionImageVariationPipeline.from_pretrained(
							    'fusing/sd-image-variations-diffusers'	,   safety_checker=_lowercase	,   torch_dtype=torch.floataa	,   )
							pipe.to(_lowercase							)
							pipe.set_progress_bar_config(disable=_lowercase							)
							pipe.enable_attention_slicing()

							lowercase_   : Any   	=      self.get_inputs(_lowercase	,   dtype=torch.floataa							)
							pipe(**_lowercase	,   callback=_lowercase	,   callback_steps=1							)
							assert callback_fn.has_been_called
							assert number_of_steps == inputs["num_inference_steps"]







	def 							lowerCamelCase__		(				self							)    ->	int:
							torch.cuda.empty_cache()
							torch.cuda.reset_max_memory_allocated()
							torch.cuda.reset_peak_memory_stats()

							lowercase_   : str   	=      'fusing/sd-image-variations-diffusers'
							lowercase_   : Any   	=      StableDiffusionImageVariationPipeline.from_pretrained(
							    _lowercase	,   safety_checker=_lowercase	,   torch_dtype=torch.floataa							)
							lowercase_   : int   	=      pipe.to(_lowercase							)
							pipe.set_progress_bar_config(disable=_lowercase							)
							pipe.enable_attention_slicing(1							)
							pipe.enable_sequential_cpu_offload()

							lowercase_   : List[Any]   	=      self.get_inputs(_lowercase	,   dtype=torch.floataa							)
							lowercase_   : str   	=      pipe(**_lowercase							)

							lowercase_   : Dict   	=      torch.cuda.max_memory_allocated()
							# make sure that less than 2.6 GB is allocated
							assert mem_bytes < 2.6 * 10**9






@nightly
@require_torch_gpu
class 				__magic_name__  (						unittest.TestCase					):







	"""simple docstring"""



	def 							lowerCamelCase__		(				self							)    ->	List[Any]:
							super().tearDown()
							gc.collect()
							torch.cuda.empty_cache()
	def 							lowerCamelCase__		(				self	,   _lowercase	,   _lowercase="cpu"	,   _lowercase=torch.floataa	,   _lowercase=0							)    ->	int:
							lowercase_   : Tuple   	=      torch.Generator(device=_lowercase							).manual_seed(_lowercase							)
							lowercase_   : List[str]   	=      load_image(
							    'https://huggingface.co/datasets/diffusers/test-arrays/resolve/main'
							    '/stable_diffusion_imgvar/input_image_vermeer.png'							)
							lowercase_   : Union[str, Any]   	=      np.random.RandomState(_lowercase							).standard_normal((1, 4, 64, 64)							)
							lowercase_   : List[Any]   	=      torch.from_numpy(_lowercase							).to(device=_lowercase	,   dtype=_lowercase							)
							lowercase_   : Tuple   	=      {
							    'image': init_image,
							    'latents': latents,
							    'generator': generator,
							    'num_inference_steps': 50,
							    'guidance_scale': 7.5,
							    'output_type': 'numpy',
							}
							return inputs
	def 							lowerCamelCase__		(				self							)    ->	int:
							lowercase_   : List[Any]   	=      StableDiffusionImageVariationPipeline.from_pretrained('fusing/sd-image-variations-diffusers'							)
							sd_pipe.to(_lowercase							)
							sd_pipe.set_progress_bar_config(disable=_lowercase							)

							lowercase_   : str   	=      self.get_inputs(_lowercase							)
							lowercase_   : Any   	=      sd_pipe(**_lowercase							).images[0]

							lowercase_   : str   	=      load_numpy(
							    'https://huggingface.co/datasets/diffusers/test-arrays/resolve/main'
							    '/stable_diffusion_imgvar/lambdalabs_variations_pndm.npy'							)
							lowercase_   : Optional[int]   	=      np.abs(expected_image - image							).max()
							assert max_diff < 1E-3







	def 							lowerCamelCase__		(				self							)    ->	Dict:
							lowercase_   : str   	=      StableDiffusionImageVariationPipeline.from_pretrained('fusing/sd-image-variations-diffusers'							)
							lowercase_   : Any   	=      DPMSolverMultistepScheduler.from_config(sd_pipe.scheduler.config							)
							sd_pipe.to(_lowercase							)
							sd_pipe.set_progress_bar_config(disable=_lowercase							)

							lowercase_   : str   	=      self.get_inputs(_lowercase							)
							lowercase_   : Any   	=      25
							lowercase_   : List[Any]   	=      sd_pipe(**_lowercase							).images[0]

							lowercase_   : str   	=      load_numpy(
							    'https://huggingface.co/datasets/diffusers/test-arrays/resolve/main'
							    '/stable_diffusion_imgvar/lambdalabs_variations_dpm_multi.npy'							)
							lowercase_   : List[Any]   	=      np.abs(expected_image - image							).max()
							assert max_diff < 1E-3








'''simple docstring'''

from collections.abc import Callable
from math import log
from os import name, system
from random import gauss, seed
from typing import TypeVar






def 						_UpperCAmelCase     (	a		:						float    ,      a		:						float    ,      a		:						int				)      ->      list:





						"""simple docstring"""
						seed(1				)
						return [gauss(a    ,      a				) for _ in range(a				)]






def 						_UpperCAmelCase     (	a		:						int    ,      a		:						list				)      ->      list:





						"""simple docstring"""
						return [k for k in range(a				) for _ in range(instance_count[k]				)]






def 						_UpperCAmelCase     (	a		:						int    ,      a		:						list				)      ->      float:





						"""simple docstring"""
						return sum(a				) / instance_count






def 						_UpperCAmelCase     (	a		:						int    ,      a		:						int				)      ->      float:





						"""simple docstring"""
						return instance_count / total_count






def 						_UpperCAmelCase     (	a		:						list    ,      a		:						list    ,      a		:						int				)      ->      float:





						"""simple docstring"""
						lowercase_   : str   	=      []  # An empty list to store all squared differences
						# iterate over number of elements in items
						for i in range(len(a				)				):
												# for loop iterates over number of elements in inner layer of items
												for j in range(len(items[i]				)				):
																		# appending squared differences to 'squared_diff' list
																		squared_diff.append((items[i][j] - means[i]) ** 2				)

    # one divided by (the number of all instances - number of classes) multiplied by
    # sum of all squared differences
						lowercase_   : Tuple   	=      len(a				)  # Number of classes in dataset
						return 1 / (total_count - n_classes) * sum(a				)






def 						_UpperCAmelCase     (	a		:						list    ,      a		:						list    ,      a		:						float    ,      a		:						list				)      ->      list:





						"""simple docstring"""
						lowercase_   : str   	=      []
						# for loop iterates over number of elements in list
						for i in range(len(a				)				):
												# for loop iterates over number of inner items of each element
												for j in range(len(x_items[i]				)				):
																		lowercase_   : Any   	=      []  # to store all discriminant values of each item as a list
																		# for loop iterates over number of classes we have in our dataset
																		for k in range(len(a				)				):
																								# appending values of discriminants for each class to 'temp' list
																								temp.append(
																								    x_items[i][j] * (means[k] / variance)
																								    - (means[k] ** 2 / (2 * variance))
																								    + log(probabilities[k]				)				)
																		# appending discriminant values of each item to 'results' list
																		results.append(a				)

						return [result.index(max(a				)				) for result in results]






def 						_UpperCAmelCase     (	a		:						list    ,      a		:						list				)      ->      float:





						"""simple docstring"""
						lowercase_   : Optional[Any]   	=      sum(1 for i, j in zip(a    ,      a				) if i == j				)
						# percentage of accuracy equals to number of correct predictions divided by number
						# of all data and multiplied by 100
						return (correct / len(a				)) * 1_0_0


A:   Union[str, Any]						=					TypeVar("num")






def 						_UpperCAmelCase     (	a		:						Callable[[object], num]    ,      a		:						str    ,      a		:						str    ,      a		:						Callable[[num], bool] = lambda a				: True    ,      a		:						str | None = None    ,      )      ->      num:





						"""simple docstring"""
						while True:
												try:
																		lowercase_   : int   	=      input_type(input(a				).strip() or default				)
																		if condition(a				):
																								return user_input
																		else:
																								print(f"{user_input}: {err_msg}"				)
																								continue
												except ValueError:
																		print(
																		    f"{user_input}: Incorrect input type, expected {input_type.__name__!r}"				)






def 						_UpperCAmelCase     (	)      ->      List[str]:





						"""simple docstring"""
						while True:
												print(' Linear Discriminant Analysis '.center(5_0    ,      '*'				)				)
												print('*' * 5_0    ,      '\n'				)
												print('First of all we should specify the number of classes that'				)
												print('we want to generate as training dataset'				)
												# Trying to get number of classes
												lowercase_   : Dict   	=      valid_input(
												    input_type=a    ,      condition=lambda a				: x > 0    ,      input_msg='Enter the number of classes (Data Groupings): '    ,      err_msg='Number of classes should be positive!'    ,      )

												print('-' * 1_0_0				)

												# Trying to get the value of standard deviation
												lowercase_   : List[Any]   	=      valid_input(
												    input_type=a    ,      condition=lambda a				: x >= 0    ,      input_msg=(
												        'Enter the value of standard deviation'
												        '(Default value is 1.0 for all classes): '
												    )    ,      err_msg='Standard deviation should not be negative!'    ,      default='1.0'    ,      )

												print('-' * 1_0_0				)

												# Trying to get number of instances in classes and theirs means to generate
												# dataset
												lowercase_   : Any   	=      []  # An empty list to store instance counts of classes in dataset
												for i in range(a				):
																		lowercase_   : Union[str, Any]   	=      valid_input(
																		    input_type=a    ,      condition=lambda a				: x > 0    ,      input_msg=(f"Enter The number of instances for class_{i+1}: ")    ,      err_msg='Number of instances should be positive!'    ,      )
																		counts.append(a				)
												print('-' * 1_0_0				)

												# An empty list to store values of user-entered means of classes
												lowercase_   : int   	=      []
												for a in range(a				):
																		lowercase_   : Union[str, Any]   	=      valid_input(
																		    input_type=a    ,      input_msg=(f"Enter the value of mean for class_{a+1}: ")    ,      err_msg='This is an invalid value.'    ,      )
																		user_means.append(a				)
												print('-' * 1_0_0				)

												print('Standard deviation: '    ,      a				)
												# print out the number of instances in classes in separated line
												for i, count in enumerate(a    ,      1				):
																		print(f"Number of instances in class_{i} is: {count}"				)
												print('-' * 1_0_0				)

												# print out mean values of classes separated line
												for i, user_mean in enumerate(a    ,      1				):
																		print(f"Mean of class_{i} is: {user_mean}"				)
												print('-' * 1_0_0				)

												# Generating training dataset drawn from gaussian distribution
												lowercase_   : Optional[int]   	=      [
												    gaussian_distribution(user_means[j]    ,      a    ,      counts[j]				)
												    for j in range(a				)
												]
												print('Generated Normal Distribution: \n'    ,      a				)
												print('-' * 1_0_0				)

												# Generating Ys to detecting corresponding classes
												lowercase_   : Optional[int]   	=      y_generator(a    ,      a				)
												print('Generated Corresponding Ys: \n'    ,      a				)
												print('-' * 1_0_0				)

												# Calculating the value of actual mean for each class
												lowercase_   : str   	=      [calculate_mean(counts[k]    ,      x[k]				) for k in range(a				)]
												# for loop iterates over number of elements in 'actual_means' list and print
												# out them in separated line
												for i, actual_mean in enumerate(a    ,      1				):
																		print(f"Actual(Real) mean of class_{i} is: {actual_mean}"				)
												print('-' * 1_0_0				)

												# Calculating the value of probabilities for each class
												lowercase_   : Union[str, Any]   	=      [
												    calculate_probabilities(counts[i]    ,      sum(a				)				) for i in range(a				)
												]

												# for loop iterates over number of elements in 'probabilities' list and print
												# out them in separated line
												for i, probability in enumerate(a    ,      1				):
																		print(f"Probability of class_{i} is: {probability}"				)
												print('-' * 1_0_0				)

												# Calculating the values of variance for each class
												lowercase_   : List[str]   	=      calculate_variance(a    ,      a    ,      sum(a				)				)
												print('Variance: '    ,      a				)
												print('-' * 1_0_0				)

												# Predicting Y values
												# storing predicted Y values in 'pre_indexes' variable
												lowercase_   : Tuple   	=      predict_y_values(a    ,      a    ,      a    ,      a				)
												print('-' * 1_0_0				)

												# Calculating Accuracy of the model
												print(f"Accuracy: {accuracy(a    ,      a				)}"				)
												print('-' * 1_0_0				)
												print(' DONE '.center(1_0_0    ,      '+'				)				)

												if input('Press any key to restart or \'q\' for quit: '				).strip().lower() == "q":
																		print('\n' + 'GoodBye!'.center(1_0_0    ,      '-'				) + '\n'				)
																		break
												system('cls' if name == 'nt' else 'clear'				)  # noqa: S605


if __name__ == "__main__":
						main()
