





"""simple docstring"""
import json
import os
import pickle
import subprocess
from hashlib import mda
from pathlib import Path
from tempfile import gettempdir
from textwrap import dedent
from types import CodeType, FunctionType
from unittest import TestCase
from unittest.mock import patch

import pytest
from multiprocess import Pool
from packaging import version

import datasets
from datasets.fingerprint import Hasher, fingerprint_transform
from datasets.table import InMemoryTable

from .utils import (
    require_regex,
    require_spacy,
    require_spacy_model,
    require_tiktoken,
    require_torch,
    require_transformers,
)







class 	a				:



 def __init__(			self							,    _snake_case			):







  """simple docstring"""






  lowerCAmelCase  							=      foo



 def __call__(			self			):







  """simple docstring"""






  return self.foo







class 	a				(					datasets.Dataset   ):



 @fingerprint_transform(inplace=_snake_case			)
 def 					UpperCamelCase__   (			self							,    _snake_case							,    *_snake_case							,    **_snake_case			):







  """simple docstring"""






  return DatasetChild(self.data							,    fingerprint=_snake_case			)



 @fingerprint_transform(inplace=_snake_case			)
 def 					UpperCamelCase__   (			self							,    _snake_case							,    *_snake_case							,    **_snake_case			):







  """simple docstring"""






  return DatasetChild(self.data							,    fingerprint=_snake_case			)







class 	a				:



 def __init__(			self							,    _snake_case			):







  """simple docstring"""






  lowerCAmelCase  							=      callable



 def __call__(			self							,    *_snake_case							,    **_snake_case			):







  """simple docstring"""






  if self.callable is not None:
   return self.callable(*_snake_case							,    **_snake_case			)



 def __getstate__(			self			):







  """simple docstring"""






  raise pickle.PicklingError()







class 	a				(					a__   ):



 @require_transformers
 @pytest.mark.integration
 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  from transformers import AutoTokenizer

  def encode(_snake_case			):
   return tokenizer(_snake_case			)

  # TODO: add hash consistency tests across sessions
  lowerCAmelCase  							=      AutoTokenizer.from_pretrained('bert-base-uncased'			)
  lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(_snake_case			)			).hexdigest()
  lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(lambda _snake_case			: tokenizer(_snake_case			)			)			).hexdigest()
  lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(_snake_case			)			).hexdigest()
  lowerCAmelCase  							=      AutoTokenizer.from_pretrained('bert-base-cased'			)
  lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(_snake_case			)			).hexdigest()
  lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(lambda _snake_case			: tokenizer(_snake_case			)			)			).hexdigest()
  lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(_snake_case			)			).hexdigest()
  lowerCAmelCase  							=      AutoTokenizer.from_pretrained('bert-base-uncased'			)
  lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(_snake_case			)			).hexdigest()
  lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(lambda _snake_case			: tokenizer(_snake_case			)			)			).hexdigest()
  lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(_snake_case			)			).hexdigest()
  self.assertEqual(_snake_case							,    _snake_case			)
  self.assertNotEqual(_snake_case							,    _snake_case			)
  self.assertEqual(_snake_case							,    _snake_case			)
  self.assertNotEqual(_snake_case							,    _snake_case			)
  self.assertEqual(_snake_case							,    _snake_case			)
  self.assertNotEqual(_snake_case							,    _snake_case			)



 @require_transformers
 @pytest.mark.integration
 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  from transformers import AutoTokenizer

  lowerCAmelCase  							=      AutoTokenizer.from_pretrained('gpt2'			)
  lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(_snake_case			)			).hexdigest()
  tokenizer('Hello world !'			)  # call once to change the tokenizer's cache
  lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(_snake_case			)			).hexdigest()
  self.assertEqual(_snake_case							,    _snake_case			)



 @require_regex
 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  import regex

  lowerCAmelCase  							=      regex.Regex('foo'			)
  lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(_snake_case			)			).hexdigest()
  lowerCAmelCase  							=      regex.Regex('bar'			)
  lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(_snake_case			)			).hexdigest()
  lowerCAmelCase  							=      regex.Regex('foo'			)
  lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(_snake_case			)			).hexdigest()
  self.assertEqual(_snake_case							,    _snake_case			)
  self.assertNotEqual(_snake_case							,    _snake_case			)







class 	a				(					a__   ):



 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  def func():
   return foo

  lowerCAmelCase  							=      [0]
  lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(_snake_case			)			).hexdigest()
  lowerCAmelCase  							=      [1]
  lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(_snake_case			)			).hexdigest()
  lowerCAmelCase  							=      [0]
  lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(_snake_case			)			).hexdigest()
  self.assertEqual(_snake_case							,    _snake_case			)
  self.assertNotEqual(_snake_case							,    _snake_case			)



 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  def func():
   pass

  lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(_snake_case			)			).hexdigest()

  def func():
   pass

  lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(_snake_case			)			).hexdigest()
  self.assertEqual(_snake_case							,    _snake_case			)



 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(Foo([0]			)			)			).hexdigest()
  lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(Foo([1]			)			)			).hexdigest()
  lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(Foo([0]			)			)			).hexdigest()
  self.assertEqual(_snake_case							,    _snake_case			)
  self.assertNotEqual(_snake_case							,    _snake_case			)



 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(Foo([0]			).__call__			)			).hexdigest()
  lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(Foo([1]			).__call__			)			).hexdigest()
  lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(Foo([0]			).__call__			)			).hexdigest()
  self.assertEqual(_snake_case							,    _snake_case			)
  self.assertNotEqual(_snake_case							,    _snake_case			)



 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  lowerCAmelCase  							=      (
      'co_argcount',
      'co_kwonlyargcount',
      'co_nlocals',
      'co_stacksize',
      'co_flags',
      'co_code',
      'co_consts',
      'co_names',
      'co_varnames',
      'co_filename',
      'co_name',
      'co_firstlineno',
      'co_lnotab',
      'co_freevars',
      'co_cellvars',
  )

  def _create_code_pyaa(*_snake_case			):
   if hasattr(args[-3]							,    'encode'			):
    lowerCAmelCase  							=      list(_snake_case			)
    lowerCAmelCase  							=      args[-3].encode()  # co_lnotab
    lowerCAmelCase  							=      args[-10].encode()  # co_code
   if hasattr(_snake_case							,    'co_posonlyargcount'			):
    if len(_snake_case			) == 16:
     return CodeType(*_snake_case			)
    elif len(_snake_case			) == 15:
     return CodeType(args[0]							,    0							,    *args[1:]			)
    return CodeType(args[0]							,    0							,    0							,    *args[1:]			)
   elif hasattr(_snake_case							,    'co_kwonlyargcount'			):
    if len(_snake_case			) == 16:
     return CodeType(args[0]							,    *args[2:]			)
    elif len(_snake_case			) == 15:
     return CodeType(*_snake_case			)
    return CodeType(args[0]							,    0							,    *args[1:]			)
   if len(_snake_case			) == 16:
    return CodeType(args[0]							,    *args[3:]			)
   elif len(_snake_case			) == 15:
    return CodeType(args[0]							,    *args[2:]			)
   return CodeType(*_snake_case			)

  def create_ipython_func(_snake_case							,    _snake_case			):
   def func():
    return returned_obj

   lowerCAmelCase  							=      func.__code__
   # Use _create_code from dill in order to make it work for different python versions
   if datasets.config.PY_VERSION < version.parse('3.8'			):
    lowerCAmelCase  							=      _create_code_pyaa(
        *[getattr(_snake_case							,    _snake_case			) if k != 'co_filename' else co_filename for k in code_args_pyaa]			)
   else:
    lowerCAmelCase  							=      code.replace(co_filename=_snake_case			)
   return FunctionType(_snake_case							,    func.__globals__							,    func.__name__							,    func.__defaults__							,    func.__closure__			)

  lowerCAmelCase							,lowerCAmelCase  							=      '<ipython-input-2-e0383a102aae>', [0]
  lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(create_ipython_func(_snake_case							,    _snake_case			)			)			).hexdigest()
  lowerCAmelCase							,lowerCAmelCase  							=      '<ipython-input-2-e0383a102aae>', [1]
  lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(create_ipython_func(_snake_case							,    _snake_case			)			)			).hexdigest()
  lowerCAmelCase							,lowerCAmelCase  							=      '<ipython-input-5-713f6613acf3>', [0]
  lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(create_ipython_func(_snake_case							,    _snake_case			)			)			).hexdigest()
  self.assertEqual(_snake_case							,    _snake_case			)
  self.assertNotEqual(_snake_case							,    _snake_case			)

  lowerCAmelCase							,lowerCAmelCase  							=      os.path.join(gettempdir()							,    'ipykernel_12345'							,    '321456789.py'			), [0]
  lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(create_ipython_func(_snake_case							,    _snake_case			)			)			).hexdigest()
  lowerCAmelCase							,lowerCAmelCase  							=      os.path.join(gettempdir()							,    'ipykernel_12345'							,    '321456789.py'			), [1]
  lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(create_ipython_func(_snake_case							,    _snake_case			)			)			).hexdigest()
  lowerCAmelCase							,lowerCAmelCase  							=      os.path.join(gettempdir()							,    'ipykernel_12345'							,    '654123987.py'			), [0]
  lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(create_ipython_func(_snake_case							,    _snake_case			)			)			).hexdigest()
  self.assertEqual(_snake_case							,    _snake_case			)
  self.assertNotEqual(_snake_case							,    _snake_case			)



 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  lowerCAmelCase							,lowerCAmelCase  							=      [0], [1]

  def func():
   return foo, bar

  lowerCAmelCase  							=      '__main__'

  def globalvars_mocka_side_effect(_snake_case							,    *_snake_case							,    **_snake_case			):
   return {"foo": foo, "bar": bar}

  def globalvars_mocka_side_effect(_snake_case							,    *_snake_case							,    **_snake_case			):
   return {"bar": bar, "foo": foo}

  with patch('dill.detect.globalvars'							,    side_effect=_snake_case			) as globalvars_mocka:
   lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(_snake_case			)			).hexdigest()
   self.assertGreater(globalvars_mocka.call_count							,    0			)
  with patch('dill.detect.globalvars'							,    side_effect=_snake_case			) as globalvars_mocka:
   lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(_snake_case			)			).hexdigest()
   self.assertGreater(globalvars_mocka.call_count							,    0			)
  self.assertEqual(_snake_case							,    _snake_case			)







class 	a				(					a__   ):



 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  lowerCAmelCase  							=      Hasher.hash('hello'			)
  lowerCAmelCase  							=      Hasher.hash('hello'			)
  lowerCAmelCase  							=      Hasher.hash('there'			)
  self.assertEqual(_snake_case							,    _snake_case			)
  self.assertNotEqual(_snake_case							,    _snake_case			)



 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  lowerCAmelCase  							=      Hasher.hash(Foo('hello'			)			)
  lowerCAmelCase  							=      Hasher.hash(Foo('hello'			)			)
  lowerCAmelCase  							=      Hasher.hash(Foo('there'			)			)
  self.assertEqual(_snake_case							,    _snake_case			)
  self.assertNotEqual(_snake_case							,    _snake_case			)



 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  lowerCAmelCase  							=      Hasher()
  for x in ["hello", Foo('hello'			)]:
   hasher.update(_snake_case			)
  lowerCAmelCase  							=      hasher.hexdigest()
  lowerCAmelCase  							=      Hasher()
  for x in ["hello", Foo('hello'			)]:
   hasher.update(_snake_case			)
  lowerCAmelCase  							=      hasher.hexdigest()
  lowerCAmelCase  							=      Hasher()
  for x in ["there", Foo('there'			)]:
   hasher.update(_snake_case			)
  lowerCAmelCase  							=      hasher.hexdigest()
  self.assertEqual(_snake_case							,    _snake_case			)
  self.assertNotEqual(_snake_case							,    _snake_case			)



 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  with self.assertRaises(pickle.PicklingError			):
   Hasher.hash(UnpicklableCallable(Foo('hello'			)			)			)



 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  lowerCAmelCase  							=      'abc'
  lowerCAmelCase  							=      [string, string]  # two strings have the same ids
  lowerCAmelCase  							=      [string, string]
  lowerCAmelCase  							=      json.loads(F'["{string}", "{string}"]'			)  # two strings have different ids
  self.assertIs(obja[0]							,    _snake_case			)
  self.assertIs(obja[0]							,    obja[1]			)
  self.assertIs(obja[0]							,    _snake_case			)
  self.assertIs(obja[0]							,    obja[1]			)
  self.assertIsNot(obja[0]							,    _snake_case			)
  self.assertIsNot(obja[0]							,    obja[1]			)
  lowerCAmelCase  							=      Hasher.hash(_snake_case			)
  lowerCAmelCase  							=      Hasher.hash(_snake_case			)
  lowerCAmelCase  							=      Hasher.hash(_snake_case			)
  self.assertEqual(_snake_case							,    _snake_case			)
  self.assertEqual(_snake_case							,    _snake_case			)



 @require_tiktoken
 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  import tiktoken

  lowerCAmelCase  							=      tiktoken.get_encoding('gpt2'			)
  lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(_snake_case			)			).hexdigest()
  lowerCAmelCase  							=      tiktoken.get_encoding('r50k_base'			)
  lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(_snake_case			)			).hexdigest()
  lowerCAmelCase  							=      tiktoken.get_encoding('gpt2'			)
  lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(_snake_case			)			).hexdigest()
  self.assertEqual(_snake_case							,    _snake_case			)
  self.assertNotEqual(_snake_case							,    _snake_case			)



 @require_torch
 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  import torch

  lowerCAmelCase  							=      torch.tensor([1.0]			)
  lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(_snake_case			)			).hexdigest()
  lowerCAmelCase  							=      torch.tensor([2.0]			)
  lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(_snake_case			)			).hexdigest()
  lowerCAmelCase  							=      torch.tensor([1.0]			)
  lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(_snake_case			)			).hexdigest()
  self.assertEqual(_snake_case							,    _snake_case			)
  self.assertNotEqual(_snake_case							,    _snake_case			)



 @require_spacy
 @require_spacy_model('en_core_web_sm'			)
 @require_spacy_model('fr_core_news_sm'			)
 @pytest.mark.integration
 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  import spacy

  lowerCAmelCase  							=      spacy.load('en_core_web_sm'			)
  lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(_snake_case			)			).hexdigest()
  lowerCAmelCase  							=      spacy.load('fr_core_news_sm'			)
  lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(_snake_case			)			).hexdigest()
  lowerCAmelCase  							=      spacy.load('en_core_web_sm'			)
  lowerCAmelCase  							=      mda(datasets.utils.py_utils.dumps(_snake_case			)			).hexdigest()
  self.assertEqual(_snake_case							,    _snake_case			)
  self.assertNotEqual(_snake_case							,    _snake_case			)


@pytest.mark.integration
def 						_SCREAMING_SNAKE_CASE       (_UpperCAmelCase :  Path       ):
 lowerCAmelCase  							=      tmp_path / 'dir1'
 lowerCAmelCase  							=      tmp_path / 'dir2'
 dira.mkdir()
 dira.mkdir()
 lowerCAmelCase  							=      'script.py'
 lowerCAmelCase  							=      dedent(
     '\n    from datasets.fingerprint import Hasher\n    def foo():\n        pass\n    print(Hasher.hash(foo))\n    '       )
 lowerCAmelCase  							=      dira / script_filename
 lowerCAmelCase  							=      dira / script_filename
 with script_patha.open('w'       ) as f:
  f.write(_UpperCAmelCase       )
 with script_patha.open('w'       ) as f:
  f.write(_UpperCAmelCase       )
 lowerCAmelCase  							=      subprocess.check_output(['python', str(_UpperCAmelCase       )]       )
 lowerCAmelCase  							=      subprocess.check_output(['python', str(_UpperCAmelCase       )]       )
 assert fingerprinta == fingerprinta

def 						_SCREAMING_SNAKE_CASE       ():
 lowerCAmelCase  							=      {'a': [0, 1, 2]}
 lowerCAmelCase  							=      DatasetChild(InMemoryTable.from_pydict(_UpperCAmelCase       )       )
 lowerCAmelCase  							=      dataset.funca()._fingerprint
 assert expected_fingerprint == dataset.funca()._fingerprint
 assert expected_fingerprint != dataset.funca()._fingerprint

 with Pool(2       ) as p:
  assert expected_fingerprint == p.apply_async(dataset.funca       ).get()._fingerprint
  assert expected_fingerprint != p.apply_async(dataset.funca       ).get()._fingerprint

def 						_SCREAMING_SNAKE_CASE       ():
 lowerCAmelCase  							=      {'a': [0, 1, 2]}







 class 	a				(					datasets.Dataset   ):



  @fingerprint_transform(inplace=_snake_case			)
  def 					UpperCamelCase__   (			self							,    _snake_case			):







   """simple docstring"""






   return DummyDatasetChild(self.data							,    fingerprint=_snake_case			)


 lowerCAmelCase  							=      DummyDatasetChild(InMemoryTable.from_pydict(_UpperCAmelCase       )       ).func()







 class 	a				(					datasets.Dataset   ):



  @fingerprint_transform(inplace=_snake_case							,    version='1.0.0'			)
  def 					UpperCamelCase__   (			self							,    _snake_case			):







   """simple docstring"""






   return DummyDatasetChild(self.data							,    fingerprint=_snake_case			)


 lowerCAmelCase  							=      DummyDatasetChild(InMemoryTable.from_pydict(_UpperCAmelCase       )       ).func()







 class 	a				(					datasets.Dataset   ):



  @fingerprint_transform(inplace=_snake_case							,    version='2.0.0'			)
  def 					UpperCamelCase__   (			self							,    _snake_case			):







   """simple docstring"""






   return DummyDatasetChild(self.data							,    fingerprint=_snake_case			)


 lowerCAmelCase  							=      DummyDatasetChild(InMemoryTable.from_pydict(_UpperCAmelCase       )       ).func()

 assert len({fingeprint_no_version, fingeprint_a, fingeprint_a}       ) == 3




def 						_SCREAMING_SNAKE_CASE       ():
 # AttributeError: module 'dill._dill' has no attribute 'stack'
 lowerCAmelCase  							=      Hasher()
 hasher.update(lambda _UpperCAmelCase       : x       )







"""simple docstring"""

from typing import SupportsIndex

import numpy as np
from scipy.ndimage import convolve




def 			lowercase__  (    lowerCAmelCase :    np.ndarray       , lowerCAmelCase :    np.ndarray       , lowerCAmelCase :    np.ndarray       )       ->					np.ndarray:






    """simple docstring"""




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

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

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

    # Set pixels at invalid locations to 0
    UpperCAmelCase  	=    0

    return warped




def 			lowercase__  (    lowerCAmelCase :    np.ndarray       , lowerCAmelCase :    np.ndarray       , lowerCAmelCase :    SupportsIndex       , lowerCAmelCase :    float | None = None       , )       ->					tuple[np.ndarray, np.ndarray]:






    """simple docstring"""




    if alpha is None:
        UpperCAmelCase  	=    0.1

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

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

        UpperCAmelCase  	=    convolve(lowerCAmelCase       , lowerCAmelCase       )
        UpperCAmelCase  	=    convolve(lowerCAmelCase       , lowerCAmelCase       )

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

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

    return horizontal_flow, vertical_flow


if __name__ == "__main__":
   import doctest

   doctest.testmod()



