





"""simple docstring"""
from typing import SupportsIndex

import numpy as np
from scipy.ndimage import convolve

def 						_SCREAMING_SNAKE_CASE       (_UpperCAmelCase :  np.ndarray   ,			_UpperCAmelCase :  np.ndarray   ,			_UpperCAmelCase :  np.ndarray       ):
 lowerCAmelCase  							=      np.stack((horizontal_flow, vertical_flow)   ,			2       )

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

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

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

 # Set pixels at invalid locations to 0
 lowerCAmelCase  							=      0

 return warped

def 						_SCREAMING_SNAKE_CASE       (_UpperCAmelCase :  np.ndarray   ,			_UpperCAmelCase :  np.ndarray   ,			_UpperCAmelCase :  SupportsIndex   ,			_UpperCAmelCase :  float | None = None   ,			):
 if alpha is None:
  lowerCAmelCase  							=      0.1

 # Initialize flow
 lowerCAmelCase  							=      np.zeros_like(_UpperCAmelCase       )
 lowerCAmelCase  							=      np.zeros_like(_UpperCAmelCase       )

 # Prepare kernels for the calculation of the derivatives and the average velocity
 lowerCAmelCase  							=      np.array([[-1, 1], [-1, 1]]       ) * 0.25
 lowerCAmelCase  							=      np.array([[-1, -1], [1, 1]]       ) * 0.25
 lowerCAmelCase  							=      np.array([[1, 1], [1, 1]]       ) * 0.25
 lowerCAmelCase  							=      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(_UpperCAmelCase       ):
  lowerCAmelCase  							=      warp(_UpperCAmelCase   ,			_UpperCAmelCase   ,			_UpperCAmelCase       )
  lowerCAmelCase  							=      convolve(_UpperCAmelCase   ,			_UpperCAmelCase       ) + convolve(_UpperCAmelCase   ,			_UpperCAmelCase       )
  lowerCAmelCase  							=      convolve(_UpperCAmelCase   ,			_UpperCAmelCase       ) + convolve(_UpperCAmelCase   ,			_UpperCAmelCase       )
  lowerCAmelCase  							=      convolve(_UpperCAmelCase   ,			_UpperCAmelCase       ) + convolve(_UpperCAmelCase   ,			-kernel_t       )

  lowerCAmelCase  							=      convolve(_UpperCAmelCase   ,			_UpperCAmelCase       )
  lowerCAmelCase  							=      convolve(_UpperCAmelCase   ,			_UpperCAmelCase       )

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

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

 return horizontal_flow, vertical_flow


if __name__ == "__main__":
       import doctest

       doctest.testmod()












"""simple docstring"""
from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


__UpperCamelCase			:		List[Any]		    =   {'''configuration_yoso''': ['''YOSO_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''YosoConfig''']}

try:
       if not is_torch_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       __UpperCamelCase			:		Dict		    =   [
           '''YOSO_PRETRAINED_MODEL_ARCHIVE_LIST''',
           '''YosoForMaskedLM''',
           '''YosoForMultipleChoice''',
           '''YosoForQuestionAnswering''',
           '''YosoForSequenceClassification''',
           '''YosoForTokenClassification''',
           '''YosoLayer''',
           '''YosoModel''',
           '''YosoPreTrainedModel''',
       ]


if TYPE_CHECKING:
       from .configuration_yoso import YOSO_PRETRAINED_CONFIG_ARCHIVE_MAP, YosoConfig

       try:
              if not is_torch_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_yoso import (
                  YOSO_PRETRAINED_MODEL_ARCHIVE_LIST,
                  YosoForMaskedLM,
                  YosoForMultipleChoice,
                  YosoForQuestionAnswering,
                  YosoForSequenceClassification,
                  YosoForTokenClassification,
                  YosoLayer,
                  YosoModel,
                  YosoPreTrainedModel,
              )


else:
       import sys

       __UpperCamelCase			:		Any		    =   _LazyModule(__name__, globals()['''__file__'''], _import_structure)





