
"""simple docstring"""





from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


_a									=				{
    'configuration_fsmt': ['FSMT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'FSMTConfig'],
    'tokenization_fsmt': ['FSMTTokenizer'],
}

try:
    if not is_torch_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    _a									=				['FSMTForConditionalGeneration', 'FSMTModel', 'PretrainedFSMTModel']


if TYPE_CHECKING:
    from .configuration_fsmt import FSMT_PRETRAINED_CONFIG_ARCHIVE_MAP, FSMTConfig
    from .tokenization_fsmt import FSMTTokenizer

    try:
        if not is_torch_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_fsmt import FSMTForConditionalGeneration, FSMTModel, PretrainedFSMTModel

else:
    import sys

    _a									=				_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)










from typing import SupportsIndex

import numpy as np
from scipy.ndimage import convolve



def        _lowerCamelCase(      lowerCAmelCase__					:		np.ndarray     ,     lowerCAmelCase__					:		np.ndarray     ,     lowerCAmelCase__					:		np.ndarray   ):





				'''simple docstring'''







				SCREAMING_SNAKE_CASE_ :						List[Any]    				= 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_ :						List[Any]    				= np.stack(
				    np.meshgrid(np.arange(0     ,     image.shape[1]   )     ,     np.arange(0     ,     image.shape[0]   )   )     ,     2   )
				SCREAMING_SNAKE_CASE_ :						List[str]    				= np.round(grid - flow   ).astype(np.intaa   )

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

				SCREAMING_SNAKE_CASE_ :						Optional[Any]    				= image[grid[:, :, 1], grid[:, :, 0]]

				# Set pixels at invalid locations to 0
				SCREAMING_SNAKE_CASE_ :						Optional[int]    				= 0

				return warped



def        _lowerCamelCase(      lowerCAmelCase__					:		np.ndarray     ,     lowerCAmelCase__					:		np.ndarray     ,     lowerCAmelCase__					:		SupportsIndex     ,     lowerCAmelCase__					:		float | None = None     ,     ):





				'''simple docstring'''







				if alpha is None:
								SCREAMING_SNAKE_CASE_ :						Any    				= 0.1

				# Initialize flow
				SCREAMING_SNAKE_CASE_ :						List[Any]    				= np.zeros_like(lowerCAmelCase__   )
				SCREAMING_SNAKE_CASE_ :						Any    				= np.zeros_like(lowerCAmelCase__   )

				# Prepare kernels for the calculation of the derivatives and the average velocity
				SCREAMING_SNAKE_CASE_ :						Optional[int]    				= np.array([[-1, 1], [-1, 1]]   ) * 0.25
				SCREAMING_SNAKE_CASE_ :						Tuple    				= np.array([[-1, -1], [1, 1]]   ) * 0.25
				SCREAMING_SNAKE_CASE_ :						Optional[int]    				= np.array([[1, 1], [1, 1]]   ) * 0.25
				SCREAMING_SNAKE_CASE_ :						Tuple    				= 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(lowerCAmelCase__   ):
								SCREAMING_SNAKE_CASE_ :						Union[str, Any]    				= warp(lowerCAmelCase__     ,     lowerCAmelCase__     ,     lowerCAmelCase__   )
								SCREAMING_SNAKE_CASE_ :						Any    				= convolve(lowerCAmelCase__     ,     lowerCAmelCase__   ) + convolve(lowerCAmelCase__     ,     lowerCAmelCase__   )
								SCREAMING_SNAKE_CASE_ :						Optional[int]    				= convolve(lowerCAmelCase__     ,     lowerCAmelCase__   ) + convolve(lowerCAmelCase__     ,     lowerCAmelCase__   )
								SCREAMING_SNAKE_CASE_ :						Optional[int]    				= convolve(lowerCAmelCase__     ,     lowerCAmelCase__   ) + convolve(lowerCAmelCase__     ,     -kernel_t   )

								SCREAMING_SNAKE_CASE_ :						List[str]    				= convolve(lowerCAmelCase__     ,     lowerCAmelCase__   )
								SCREAMING_SNAKE_CASE_ :						Optional[Any]    				= convolve(lowerCAmelCase__     ,     lowerCAmelCase__   )

								# This updates the flow as proposed in the paper (Step 12)
								SCREAMING_SNAKE_CASE_ :						Tuple    				= (
								    derivative_x * avg_horizontal_velocity
								    + derivative_y * avg_vertical_velocity
								    + derivative_t
								)
								SCREAMING_SNAKE_CASE_ :						Optional[int]    				= update / (alpha**2 + derivative_x**2 + derivative_y**2)

								SCREAMING_SNAKE_CASE_ :						List[str]    				= avg_horizontal_velocity - derivative_x * update
								SCREAMING_SNAKE_CASE_ :						Optional[int]    				= avg_vertical_velocity - derivative_y * update

				return horizontal_flow, vertical_flow


if __name__ == "__main__":
				import doctest

				doctest.testmod()