






"""simple docstring"""





from typing import List, Optional, Tuple, Union

import torch

from ...models import UNetaDModel
from ...schedulers import PNDMScheduler
from ...utils import randn_tensor
from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput


class   _lowercase			(  lowerCAmelCase    ):
      _a :  UNetaDModel
      _a :  PNDMScheduler


      def __init__(     self					:						Union[str, Any]				, a					:						UNetaDModel				, a					:						PNDMScheduler    ):





            """simple docstring"""







            super().__init__()

            __snake_case   :     List[Any]	      =PNDMScheduler.from_config(scheduler.config    )

            self.register_modules(unet=a				, scheduler=a    )







      @torch.no_grad()
      def __call__(     self					:						str				, a					:						int = 1				, a					:						int = 5_0				, a					:						Optional[Union[torch.Generator, List[torch.Generator]]] = None				, a					:						Optional[str] = "pil"				, a					:						bool = True				, **a					:						int				, ):





            """simple docstring"""







            __snake_case   :     str	      =randn_tensor(
                (batch_size, self.unet.config.in_channels, self.unet.config.sample_size, self.unet.config.sample_size)				, generator=a				, device=self.device				, )

            self.scheduler.set_timesteps(a    )
            for t in self.progress_bar(self.scheduler.timesteps    ):
                  __snake_case   :     Dict	      =self.unet(a				, a    ).sample

                  __snake_case   :     Tuple	      =self.scheduler.step(a				, a				, a    ).prev_sample

            __snake_case   :     Optional[int]	      =(image / 2 + 0.5).clamp(0				, 1    )
            __snake_case   :     Union[str, Any]	      =image.cpu().permute(0				, 2				, 3				, 1    ).numpy()
            if output_type == "pil":
                  __snake_case   :     Dict	      =self.numpy_to_pil(a    )

            if not return_dict:
                  return (image,)

            return ImagePipelineOutput(images=a    )










"""simple docstring"""





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      __lowercase       (     a			:					int			)       ->       list[int]:
      return [int(a			) for c in f'''{ruleset:08}'''[:8]]



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



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


if __name__ == "__main__":
    UpperCamelCase_						:					Optional[Any]      				=			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_						:					List[Any]      				=			generate_image(CELLS)
    # Uncomment to save the image
    # img.save(f"rule_{rule_num}.png")
    img.show()


