


import random
import unittest

import torch

from diffusers import IFSuperResolutionPipeline
from diffusers.utils import floats_tensor
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import skip_mps, torch_device

from ..pipeline_params import TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin
from . import IFPipelineTesterMixin







@skip_mps
class 					snake_case__(				UpperCAmelCase__,    UpperCAmelCase__,    unittest.TestCase				):







						'''simple docstring'''




						SCREAMING_SNAKE_CASE__   :	Optional[Any]       =				IFSuperResolutionPipeline
						SCREAMING_SNAKE_CASE__   :	Optional[Any]       =				TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"""width""", """height"""}
						SCREAMING_SNAKE_CASE__   :	List[str]       =				TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
						SCREAMING_SNAKE_CASE__   :	int       =				PipelineTesterMixin.required_optional_params - {"""latents"""}




						def     lowercase_			(					self				)       ->     int:
										return self._get_superresolution_dummy_components()




						def     lowercase_			(					self ,    __lowercase ,    __lowercase=0				)       ->     int:
										if str(__lowercase				).startswith('''mps'''				):
														lowerCAmelCase_			:   Tuple     					=					torch.manual_seed(__lowercase				)
										else:
														lowerCAmelCase_			:   str     					=					torch.Generator(device=__lowercase				).manual_seed(__lowercase				)

										lowerCAmelCase_			:   Dict     					=					floats_tensor((1, 3, 3_2, 3_2) ,    rng=random.Random(__lowercase				)				).to(__lowercase				)

										lowerCAmelCase_			:   Optional[int]     					=					{
										    '''prompt''': '''A painting of a squirrel eating a burger''',
										    '''image''': image,
										    '''generator''': generator,
										    '''num_inference_steps''': 2,
										    '''output_type''': '''numpy''',
										}

										return inputs




						@unittest.skipIf(
						    torch_device != '''cuda''' or not is_xformers_available() ,    reason='''XFormers attention is only available with CUDA and `xformers` installed''' ,    )
						def     lowercase_			(					self				)       ->     Dict:
										self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3				)




						def     lowercase_			(					self				)       ->     List[str]:
										self._test_save_load_optional_components()




						@unittest.skipIf(torch_device != '''cuda''' ,    reason='''float16 requires CUDA'''				)
						def     lowercase_			(					self				)       ->     Dict:
										# Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder
										super().test_save_load_floataa(expected_max_diff=1e-1				)




						def     lowercase_			(					self				)       ->     Optional[int]:
										self._test_attention_slicing_forward_pass(expected_max_diff=1e-2				)




						def     lowercase_			(					self				)       ->     Any:
										self._test_save_load_local()




						def     lowercase_			(					self				)       ->     Any:
										self._test_inference_batch_single_identical(
										    expected_max_diff=1e-2 ,    )



from __future__ import annotations

from PIL import Image

# Define the first generation of cells
# fmt: off
_UpperCAmelCase      :	int  =[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]



def       lowerCAmelCase			(		lowerCAmelCase_			)->			list[int]:
				return [int(lowerCAmelCase_			) for c in f"""{ruleset:08}"""[:8]]



def       lowerCAmelCase			(		lowerCAmelCase_			, lowerCAmelCase_			, lowerCAmelCase_			)->			list[int]:
				lowerCAmelCase_			:   Optional[Any]     					=					len(cells[0]			)  # 31
				lowerCAmelCase_			:   int     					=					[]
				for i in range(lowerCAmelCase_			):
								# Get the neighbors of each cell
								# Handle neighbours outside bounds by using 0 as their value
								lowerCAmelCase_			:   Union[str, Any]     					=					0 if i == 0 else cells[time][i - 1]
								lowerCAmelCase_			:   List[str]     					=					0 if i == population - 1 else cells[time][i + 1]
								# Define a new cell and add it to the new generation
								lowerCAmelCase_			:   List[Any]     					=					7 - int(f"""{left_neighbor}{cells[time][i]}{right_neighbor}"""			, 2			)
								next_generation.append(rule[situation]			)
				return next_generation



def       lowerCAmelCase			(		lowerCAmelCase_			)->			Image.Image:
				lowerCAmelCase_			:   Optional[Any]     					=					Image.new('''RGB'''			, (len(cells[0]			), len(lowerCAmelCase_			))			)
				lowerCAmelCase_			:   str     					=					img.load()
				# Generates image
				for w in range(img.width			):
								for h in range(img.height			):
												lowerCAmelCase_			:   Optional[Any]     					=					255 - int(255 * cells[h][w]			)
												lowerCAmelCase_			:   List[Any]     					=					(color, color, color)
				return img


if __name__ == "__main__":
						_UpperCAmelCase      :	str  =bin(int(input("""Rule:\n""").strip()))[2:]
						_UpperCAmelCase      :	List[str]  =format_ruleset(int(rule_num))
						for time in range(16):
												CELLS.append(new_generation(CELLS, rule, time))
						_UpperCAmelCase      :	Dict  =generate_image(CELLS)
						# Uncomment to save the image
						# img.save(f"rule_{rule_num}.png")
						img.show()