
'''simple docstring'''
import unittest
from dataclasses import dataclass
from typing import List, Union

import numpy as np
import PIL.Image

from diffusers.utils.outputs import BaseOutput






@dataclass
class  UpperCAmelCase_					(						_snake_case       ):
 UpperCAmelCase_		     =    42






class  UpperCAmelCase_					(						unittest.TestCase       ):





 def 							snake_case__		(     self):
     snake_case_			:     Tuple        =    CustomOutput(images=np.random.rand(1			,							3			,							4			,							4))

     # check every way of getting the attribute
     assert isinstance(outputs.images			,							np.ndarray)
     assert outputs.images.shape == (1, 3, 4, 4)
     assert isinstance(outputs["images"]			,							np.ndarray)
     assert outputs["images"].shape == (1, 3, 4, 4)
     assert isinstance(outputs[0]			,							np.ndarray)
     assert outputs[0].shape == (1, 3, 4, 4)

     # test with a non-tensor attribute
     snake_case_			:     Union[str, Any]        =    CustomOutput(images=[PIL.Image.new("RGB"			,							(4, 4))])

     # check every way of getting the attribute
     assert isinstance(outputs.images			,							A_)
     assert isinstance(outputs.images[0]			,							PIL.Image.Image)
     assert isinstance(outputs["images"]			,							A_)
     assert isinstance(outputs["images"][0]			,							PIL.Image.Image)
     assert isinstance(outputs[0]			,							A_)
     assert isinstance(outputs[0][0]			,							PIL.Image.Image)





 def 							snake_case__		(     self):
     # test output reinitialization with a `dict` for compatibility with `accelerate`
     snake_case_			:     Optional[int]        =    CustomOutput({"images": np.random.rand(1			,							3			,							4			,							4)})

     # check every way of getting the attribute
     assert isinstance(outputs.images			,							np.ndarray)
     assert outputs.images.shape == (1, 3, 4, 4)
     assert isinstance(outputs["images"]			,							np.ndarray)
     assert outputs["images"].shape == (1, 3, 4, 4)
     assert isinstance(outputs[0]			,							np.ndarray)
     assert outputs[0].shape == (1, 3, 4, 4)

     # test with a non-tensor attribute
     snake_case_			:     Any        =    CustomOutput({"images": [PIL.Image.new("RGB"			,							(4, 4))]})

     # check every way of getting the attribute
     assert isinstance(outputs.images			,							A_)
     assert isinstance(outputs.images[0]			,							PIL.Image.Image)
     assert isinstance(outputs["images"]			,							A_)
     assert isinstance(outputs["images"][0]			,							PIL.Image.Image)
     assert isinstance(outputs[0]			,							A_)
     assert isinstance(outputs[0][0]			,							PIL.Image.Image)







from typing import SupportsIndex

import numpy as np
from scipy.ndimage import convolve





def 				__a							(       __A			,							__A			,							__A						):
    SCREAMING_SNAKE_CASE__         =    np.stack((horizontal_flow, vertical_flow)			,							2						)

    # Create a grid of all pixel coordinates and subtract the flow to get the
    # target pixels coordinates
    SCREAMING_SNAKE_CASE__         =    np.stack(
        np.meshgrid(np.arange(0			,							image.shape[1]						)			,							np.arange(0			,							image.shape[0]						)						)			,							2						)
    SCREAMING_SNAKE_CASE__         =    np.round(grid - flow						).astype(np.intaa						)

    # Find the locations outside of the original image
    SCREAMING_SNAKE_CASE__         =    (grid < 0) | (grid >= np.array([image.shape[1], image.shape[0]]						))
    SCREAMING_SNAKE_CASE__         =    0

    SCREAMING_SNAKE_CASE__         =    image[grid[:, :, 1], grid[:, :, 0]]

    # Set pixels at invalid locations to 0
    SCREAMING_SNAKE_CASE__         =    0

    return warped





def 				__a							(       __A			,							__A			,							__A			,							__A = None			,							):
    if alpha is None:
        SCREAMING_SNAKE_CASE__         =    0.1

    # Initialize flow
    SCREAMING_SNAKE_CASE__         =    np.zeros_like(__A						)
    SCREAMING_SNAKE_CASE__         =    np.zeros_like(__A						)

    # Prepare kernels for the calculation of the derivatives and the average velocity
    SCREAMING_SNAKE_CASE__         =    np.array([[-1, 1], [-1, 1]]						) * 0.25
    SCREAMING_SNAKE_CASE__         =    np.array([[-1, -1], [1, 1]]						) * 0.25
    SCREAMING_SNAKE_CASE__         =    np.array([[1, 1], [1, 1]]						) * 0.25
    SCREAMING_SNAKE_CASE__         =    np.array(
        [[1 / 12, 1 / 6, 1 / 12], [1 / 6, 0, 1 / 6], [1 / 12, 1 / 6, 1 / 12]]						)

    # Iteratively refine the flow
    for _ in range(__A						):
        SCREAMING_SNAKE_CASE__         =    warp(__A			,							__A			,							__A						)
        SCREAMING_SNAKE_CASE__         =    convolve(__A			,							__A						) + convolve(__A			,							__A						)
        SCREAMING_SNAKE_CASE__         =    convolve(__A			,							__A						) + convolve(__A			,							__A						)
        SCREAMING_SNAKE_CASE__         =    convolve(__A			,							__A						) + convolve(__A			,							-kernel_t						)

        SCREAMING_SNAKE_CASE__         =    convolve(__A			,							__A						)
        SCREAMING_SNAKE_CASE__         =    convolve(__A			,							__A						)

        # This updates the flow as proposed in the paper (Step 12)
        SCREAMING_SNAKE_CASE__         =    (
            derivative_x * avg_horizontal_velocity
            + derivative_y * avg_vertical_velocity
            + derivative_t
        )
        SCREAMING_SNAKE_CASE__         =    update / (alpha**2 + derivative_x**2 + derivative_y**2)

        SCREAMING_SNAKE_CASE__         =    avg_horizontal_velocity - derivative_x * update
        SCREAMING_SNAKE_CASE__         =    avg_vertical_velocity - derivative_y * update

    return horizontal_flow, vertical_flow


if __name__ == "__main__":
  import doctest

  doctest.testmod()


