'''simple docstring'''
import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_video_inputs


if is_torch_available():
      import torch

if is_vision_available():
      from PIL import Image

      from transformers import VivitImageProcessor







class lowerCamelCase							(	unittest.TestCase					):




  '''simple docstring'''



  def __init__(      self			:       Dict  ,							UpperCAmelCase__			:       Optional[int]  ,							UpperCAmelCase__			:       Any=7  ,							UpperCAmelCase__			:       Optional[Any]=3  ,							UpperCAmelCase__			:       Optional[int]=10  ,							UpperCAmelCase__			:       Optional[Any]=18  ,							UpperCAmelCase__			:       Any=30  ,							UpperCAmelCase__			:       Any=400  ,							UpperCAmelCase__			:       str=True  ,							UpperCAmelCase__			:       Tuple=None  ,							UpperCAmelCase__			:       int=True  ,							UpperCAmelCase__			:       List[str]=[0.5, 0.5, 0.5]  ,							UpperCAmelCase__			:       List[Any]=[0.5, 0.5, 0.5]  ,							UpperCAmelCase__			:       List[Any]=None  ,							)				->Any:
        UpperCAmelCase_            =  size if size is not None else {'''shortest_edge''': 18}
        UpperCAmelCase_            =  crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}

        UpperCAmelCase_            =  parent
        UpperCAmelCase_            =  batch_size
        UpperCAmelCase_            =  num_channels
        UpperCAmelCase_            =  num_frames
        UpperCAmelCase_            =  image_size
        UpperCAmelCase_            =  min_resolution
        UpperCAmelCase_            =  max_resolution
        UpperCAmelCase_            =  do_resize
        UpperCAmelCase_            =  size
        UpperCAmelCase_            =  do_normalize
        UpperCAmelCase_            =  image_mean
        UpperCAmelCase_            =  image_std
        UpperCAmelCase_            =  crop_size






  def  lowerCAmelCase__ (      self			:       Any  )				->str:
        return {
            "image_mean": self.image_mean,
            "image_std": self.image_std,
            "do_normalize": self.do_normalize,
            "do_resize": self.do_resize,
            "size": self.size,
            "crop_size": self.crop_size,
        }







@require_torch
@require_vision
class lowerCamelCase							(	lowerCamelCase     ,		unittest.TestCase					):




  '''simple docstring'''



  lowerCAmelCase__										=	VivitImageProcessor if is_vision_available() else None






  def  lowerCAmelCase__ (      self			:       List[str]  )				->int:
        UpperCAmelCase_            =  VivitImageProcessingTester(self  )






  @property
  def  lowerCAmelCase__ (      self			:       Union[str, Any]  )				->Any:
        return self.image_processor_tester.prepare_image_processor_dict()






  def  lowerCAmelCase__ (      self			:       List[str]  )				->Union[str, Any]:
        UpperCAmelCase_            =  self.image_processing_class(**self.image_processor_dict  )
        self.assertTrue(hasattr(UpperCAmelCase__  ,							'''image_mean'''  )  )
        self.assertTrue(hasattr(UpperCAmelCase__  ,							'''image_std'''  )  )
        self.assertTrue(hasattr(UpperCAmelCase__  ,							'''do_normalize'''  )  )
        self.assertTrue(hasattr(UpperCAmelCase__  ,							'''do_resize'''  )  )
        self.assertTrue(hasattr(UpperCAmelCase__  ,							'''do_center_crop'''  )  )
        self.assertTrue(hasattr(UpperCAmelCase__  ,							'''size'''  )  )






  def  lowerCAmelCase__ (      self			:       Tuple  )				->str:
        UpperCAmelCase_            =  self.image_processing_class.from_dict(self.image_processor_dict  )
        self.assertEqual(image_processor.size  ,							{'''shortest_edge''': 18}  )
        self.assertEqual(image_processor.crop_size  ,							{'''height''': 18, '''width''': 18}  )

        UpperCAmelCase_            =  self.image_processing_class.from_dict(self.image_processor_dict  ,							size=42  ,							crop_size=84  )
        self.assertEqual(image_processor.size  ,							{'''shortest_edge''': 42}  )
        self.assertEqual(image_processor.crop_size  ,							{'''height''': 84, '''width''': 84}  )






  def  lowerCAmelCase__ (      self			:       Any  )				->Dict:
        # Initialize image_processing
        UpperCAmelCase_            =  self.image_processing_class(**self.image_processor_dict  )
        # create random PIL videos
        UpperCAmelCase_            =  prepare_video_inputs(self.image_processor_tester  ,							equal_resolution=UpperCAmelCase__  )
        for video in video_inputs:
              self.assertIsInstance(UpperCAmelCase__  ,							UpperCAmelCase__  )
              self.assertIsInstance(video[0]  ,							Image.Image  )

        # Test not batched input
        UpperCAmelCase_            =  image_processing(video_inputs[0]  ,							return_tensors='''pt'''  ).pixel_values
        self.assertEqual(
            encoded_videos.shape  ,							(
                1,
                self.image_processor_tester.num_frames,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size['''height'''],
                self.image_processor_tester.crop_size['''width'''],
            )  ,							)

        # Test batched
        UpperCAmelCase_            =  image_processing(UpperCAmelCase__  ,							return_tensors='''pt'''  ).pixel_values
        self.assertEqual(
            encoded_videos.shape  ,							(
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_frames,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size['''height'''],
                self.image_processor_tester.crop_size['''width'''],
            )  ,							)






  def  lowerCAmelCase__ (      self			:       Optional[int]  )				->Union[str, Any]:
        # Initialize image_processing
        UpperCAmelCase_            =  self.image_processing_class(**self.image_processor_dict  )
        # create random numpy tensors
        UpperCAmelCase_            =  prepare_video_inputs(self.image_processor_tester  ,							equal_resolution=UpperCAmelCase__  ,							numpify=UpperCAmelCase__  )
        for video in video_inputs:
              self.assertIsInstance(UpperCAmelCase__  ,							UpperCAmelCase__  )
              self.assertIsInstance(video[0]  ,							np.ndarray  )

        # Test not batched input
        UpperCAmelCase_            =  image_processing(video_inputs[0]  ,							return_tensors='''pt'''  ).pixel_values
        self.assertEqual(
            encoded_videos.shape  ,							(
                1,
                self.image_processor_tester.num_frames,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size['''height'''],
                self.image_processor_tester.crop_size['''width'''],
            )  ,							)

        # Test batched
        UpperCAmelCase_            =  image_processing(UpperCAmelCase__  ,							return_tensors='''pt'''  ).pixel_values
        self.assertEqual(
            encoded_videos.shape  ,							(
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_frames,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size['''height'''],
                self.image_processor_tester.crop_size['''width'''],
            )  ,							)






  def  lowerCAmelCase__ (      self			:       int  )				->int:
        # Initialize image_processing
        UpperCAmelCase_            =  self.image_processing_class(**self.image_processor_dict  )
        # create random PyTorch tensors
        UpperCAmelCase_            =  prepare_video_inputs(self.image_processor_tester  ,							equal_resolution=UpperCAmelCase__  ,							torchify=UpperCAmelCase__  )
        for video in video_inputs:
              self.assertIsInstance(UpperCAmelCase__  ,							UpperCAmelCase__  )
              self.assertIsInstance(video[0]  ,							torch.Tensor  )

        # Test not batched input
        UpperCAmelCase_            =  image_processing(video_inputs[0]  ,							return_tensors='''pt'''  ).pixel_values
        self.assertEqual(
            encoded_videos.shape  ,							(
                1,
                self.image_processor_tester.num_frames,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size['''height'''],
                self.image_processor_tester.crop_size['''width'''],
            )  ,							)

        # Test batched
        UpperCAmelCase_            =  image_processing(UpperCAmelCase__  ,							return_tensors='''pt'''  ).pixel_values
        self.assertEqual(
            encoded_videos.shape  ,							(
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_frames,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size['''height'''],
                self.image_processor_tester.crop_size['''width'''],
            )  ,							)






'''simple docstring'''
from heapq import heappop, heappush

import numpy as np



def  __lowerCamelCase       (      _UpperCamelCase  :  np.ndarray							,    _UpperCamelCase  :  tuple[int, int]							,    _UpperCamelCase  :  tuple[int, int]							,    _UpperCamelCase  :  bool							,    ):


      '''simple docstring'''


      UpperCAmelCase_      ,		UpperCAmelCase_            =  grid.shape
      UpperCAmelCase_            =  [-1, 1, 0, 0]
      UpperCAmelCase_            =  [0, 0, -1, 1]
      if allow_diagonal:
            dx += [-1, -1, 1, 1]
            dy += [-1, 1, -1, 1]

      UpperCAmelCase_      ,		UpperCAmelCase_            =  [(0, source)], set()
      UpperCAmelCase_            =  np.full((rows, cols)							,    np.inf					)
      UpperCAmelCase_            =  0
      UpperCAmelCase_            =  np.empty((rows, cols)							,    dtype=_UpperCamelCase					)
      UpperCAmelCase_            =  None

      while queue:
            ((UpperCAmelCase_)      ,		(UpperCAmelCase_))            =  heappop(_UpperCamelCase					)
            if (x, y) in visited:
                  continue
            visited.add((x, y)					)

            if (x, y) == destination:
                  UpperCAmelCase_            =  []
                  while (x, y) != source:
                        path.append((x, y)					)
                        UpperCAmelCase_      ,		UpperCAmelCase_            =  predecessors[x, y]
                  path.append(_UpperCamelCase					)  # add the source manually
                  path.reverse()
                  return matrix[destination], path

            for i in range(len(_UpperCamelCase					)					):
                  UpperCAmelCase_      ,		UpperCAmelCase_            =  x + dx[i], y + dy[i]
                  if 0 <= nx < rows and 0 <= ny < cols:
                        UpperCAmelCase_            =  grid[nx][ny]
                        if next_node == 1 and matrix[nx, ny] > dist + 1:
                              heappush(_UpperCamelCase							,    (dist + 1, (nx, ny))					)
                              UpperCAmelCase_            =  dist + 1
                              UpperCAmelCase_            =  (x, y)

      return np.inf, []


if __name__ == "__main__":
      import doctest

      doctest.testmod()





