'''simple docstring'''


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"""





     UpperCAmelCase_				:  int  = np.stack((horizontal_flow, vertical_flow)      ,	2  )

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

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

     UpperCAmelCase_				:  List[str]  = image[grid[:, :, 1], grid[:, :, 0]]

     # Set pixels at invalid locations to 0
     UpperCAmelCase_				:  Union[str, Any]  = 0

     return warped


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



     """simple docstring"""





     if alpha is None:
          UpperCAmelCase_				:  Any  = 0.1

     # Initialize flow
     UpperCAmelCase_				:  Tuple  = np.zeros_like(lowerCamelCase_  )
     UpperCAmelCase_				:  Union[str, Any]  = np.zeros_like(lowerCamelCase_  )

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

          UpperCAmelCase_				:  Optional[Any]  = convolve(lowerCamelCase_      ,	lowerCamelCase_  )
          UpperCAmelCase_				:  List[str]  = convolve(lowerCamelCase_      ,	lowerCamelCase_  )

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

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

     return horizontal_flow, vertical_flow


if __name__ == "__main__":
 import doctest

 doctest.testmod()











import json

import requests

from .fetch_github_info import AUTHENTICATED_USER_ENDPOINT, fetch_github_info







def 							A     (						__UpperCAmelCase      )			->			Dict:



						'''simple docstring'''






						class 				a_    :

												def __init__(       self      :Optional[int]  ,		_lowercase      :Any)						->   None:
																		assert isinstance(_lowercase  ,		(bytes, str))
																		UpperCAmelCase_					=	content




												def       __a			(       self      :List[str])						->   Tuple:
																		return json.loads(self.content)

						def mock_response(*__UpperCAmelCase				,   **__UpperCAmelCase      ):
												assert args[0] == AUTHENTICATED_USER_ENDPOINT
												assert "Authorization" in kwargs["headers"]
												assert kwargs["headers"]["Authorization"].startswith('''token '''      )
												assert "Accept" in kwargs["headers"]
												return FakeResponse(b'''{"login":"test","id":1}'''      )

						monkeypatch.setattr(__UpperCAmelCase				,   '''get'''				,   __UpperCAmelCase      )
						UpperCAmelCase_					=	fetch_github_info('''token'''      )
						assert result["login"] == "test"
						assert result["id"] == 1





