




'''simple docstring'''



import unittest

from transformers import (
    MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
    TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
    PreTrainedTokenizer,
    is_vision_available,
)
from transformers.pipelines import ImageClassificationPipeline, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_torch_or_tf,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
 from PIL import Image
else:







 class 		lowercase_    :


    """simple docstring"""

    @staticmethod
    def 							lowerCAmelCase_			(    *__lowerCamelCase					:	int  ,      **__lowerCamelCase					:	Optional[Any]    ):

       """simple docstring"""
       pass







@is_pipeline_test
@require_torch_or_tf
@require_vision
class 		lowercase_    ( unittest.TestCase   ):


   """simple docstring"""

   lowerCamelCase_				  =						MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
   lowerCamelCase_				  =						TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING



   def 							lowerCAmelCase_			(    self					:	Tuple  ,      __lowerCamelCase					:	Tuple  ,      __lowerCamelCase					:	Dict  ,      __lowerCamelCase					:	List[Any]    ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE       =      ImageClassificationPipeline(model=_lowercase  ,      image_processor=_lowercase  ,      top_k=2    )
      _SCREAMING_SNAKE_CASE       =      [
          Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"    ),
          "http://images.cocodataset.org/val2017/000000039769.jpg",
      ]
      return image_classifier, examples



   def 							lowerCAmelCase_			(    self					:	List[str]  ,      __lowerCamelCase					:	Optional[int]  ,      __lowerCamelCase					:	List[str]    ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE       =      image_classifier("./tests/fixtures/tests_samples/COCO/000000039769.png"    )

      self.assertEqual(
          _lowercase  ,      [
              {"score": ANY(_lowercase    ), "label": ANY(_lowercase    )},
              {"score": ANY(_lowercase    ), "label": ANY(_lowercase    )},
          ]  ,      )

      import datasets

      _SCREAMING_SNAKE_CASE       =      datasets.load_dataset("hf-internal-testing/fixtures_image_utils"  ,      "image"  ,      split="test"    )

      # Accepts URL + PIL.Image + lists
      _SCREAMING_SNAKE_CASE       =      image_classifier(
          [
              Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"    ),
              "http://images.cocodataset.org/val2017/000000039769.jpg",
              # RGBA
              dataset[0]["file"],
              # LA
              dataset[1]["file"],
              # L
              dataset[2]["file"],
          ]    )
      self.assertEqual(
          _lowercase  ,      [
              [
                  {"score": ANY(_lowercase    ), "label": ANY(_lowercase    )},
                  {"score": ANY(_lowercase    ), "label": ANY(_lowercase    )},
              ],
              [
                  {"score": ANY(_lowercase    ), "label": ANY(_lowercase    )},
                  {"score": ANY(_lowercase    ), "label": ANY(_lowercase    )},
              ],
              [
                  {"score": ANY(_lowercase    ), "label": ANY(_lowercase    )},
                  {"score": ANY(_lowercase    ), "label": ANY(_lowercase    )},
              ],
              [
                  {"score": ANY(_lowercase    ), "label": ANY(_lowercase    )},
                  {"score": ANY(_lowercase    ), "label": ANY(_lowercase    )},
              ],
              [
                  {"score": ANY(_lowercase    ), "label": ANY(_lowercase    )},
                  {"score": ANY(_lowercase    ), "label": ANY(_lowercase    )},
              ],
          ]  ,      )



   @require_torch
   def 							lowerCAmelCase_			(    self					:	int    ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE       =      "hf-internal-testing/tiny-random-vit"
      _SCREAMING_SNAKE_CASE       =      pipeline("image-classification"  ,      model=_lowercase    )

      _SCREAMING_SNAKE_CASE       =      image_classifier("http://images.cocodataset.org/val2017/000000039769.jpg"    )
      self.assertEqual(
          nested_simplify(_lowercase  ,      decimals=4    )  ,      [{"label": "LABEL_1", "score": 0.5_7_4}, {"label": "LABEL_0", "score": 0.4_2_6}]  ,      )

      _SCREAMING_SNAKE_CASE       =      image_classifier(
          [
              "http://images.cocodataset.org/val2017/000000039769.jpg",
              "http://images.cocodataset.org/val2017/000000039769.jpg",
          ]  ,      top_k=2  ,      )
      self.assertEqual(
          nested_simplify(_lowercase  ,      decimals=4    )  ,      [
              [{"label": "LABEL_1", "score": 0.5_7_4}, {"label": "LABEL_0", "score": 0.4_2_6}],
              [{"label": "LABEL_1", "score": 0.5_7_4}, {"label": "LABEL_0", "score": 0.4_2_6}],
          ]  ,      )



   @require_tf
   def 							lowerCAmelCase_			(    self					:	int    ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE       =      "hf-internal-testing/tiny-random-vit"
      _SCREAMING_SNAKE_CASE       =      pipeline("image-classification"  ,      model=_lowercase  ,      framework="tf"    )

      _SCREAMING_SNAKE_CASE       =      image_classifier("http://images.cocodataset.org/val2017/000000039769.jpg"    )
      self.assertEqual(
          nested_simplify(_lowercase  ,      decimals=4    )  ,      [{"label": "LABEL_1", "score": 0.5_7_4}, {"label": "LABEL_0", "score": 0.4_2_6}]  ,      )

      _SCREAMING_SNAKE_CASE       =      image_classifier(
          [
              "http://images.cocodataset.org/val2017/000000039769.jpg",
              "http://images.cocodataset.org/val2017/000000039769.jpg",
          ]  ,      top_k=2  ,      )
      self.assertEqual(
          nested_simplify(_lowercase  ,      decimals=4    )  ,      [
              [{"label": "LABEL_1", "score": 0.5_7_4}, {"label": "LABEL_0", "score": 0.4_2_6}],
              [{"label": "LABEL_1", "score": 0.5_7_4}, {"label": "LABEL_0", "score": 0.4_2_6}],
          ]  ,      )



   def 							lowerCAmelCase_			(    self					:	Any    ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE       =      PreTrainedTokenizer()

      # Assert that the pipeline can be initialized with a feature extractor that is not in any mapping
      _SCREAMING_SNAKE_CASE       =      pipeline(
          "image-classification"  ,      model="hf-internal-testing/tiny-random-vit"  ,      tokenizer=_lowercase    )

      self.assertIs(image_classifier.tokenizer  ,      _lowercase    )



   @slow
   @require_torch
   def 							lowerCAmelCase_			(    self					:	str    ):

      """simple docstring"""
      # Perceiver is not tested by `run_pipeline_test` properly.
      # That is because the type of feature_extractor and model preprocessor need to be kept
      # in sync, which is not the case in the current design
      _SCREAMING_SNAKE_CASE       =      pipeline("image-classification"  ,      model="deepmind/vision-perceiver-conv"    )
      _SCREAMING_SNAKE_CASE       =      image_classifier("http://images.cocodataset.org/val2017/000000039769.jpg"    )
      self.assertEqual(
          nested_simplify(_lowercase  ,      decimals=4    )  ,      [
              {"score": 0.4_3_8_5, "label": "tabby, tabby cat"},
              {"score": 0.3_2_1, "label": "tiger cat"},
              {"score": 0.0_5_0_2, "label": "Egyptian cat"},
              {"score": 0.0_1_3_7, "label": "crib, cot"},
              {"score": 0.0_0_7, "label": "radiator"},
          ]  ,      )

      _SCREAMING_SNAKE_CASE       =      pipeline("image-classification"  ,      model="deepmind/vision-perceiver-fourier"    )
      _SCREAMING_SNAKE_CASE       =      image_classifier("http://images.cocodataset.org/val2017/000000039769.jpg"    )
      self.assertEqual(
          nested_simplify(_lowercase  ,      decimals=4    )  ,      [
              {"score": 0.5_6_5_8, "label": "tabby, tabby cat"},
              {"score": 0.1_3_0_9, "label": "tiger cat"},
              {"score": 0.0_7_2_2, "label": "Egyptian cat"},
              {"score": 0.0_7_0_7, "label": "remote control, remote"},
              {"score": 0.0_0_8_2, "label": "computer keyboard, keypad"},
          ]  ,      )

      _SCREAMING_SNAKE_CASE       =      pipeline("image-classification"  ,      model="deepmind/vision-perceiver-learned"    )
      _SCREAMING_SNAKE_CASE       =      image_classifier("http://images.cocodataset.org/val2017/000000039769.jpg"    )
      self.assertEqual(
          nested_simplify(_lowercase  ,      decimals=4    )  ,      [
              {"score": 0.3_0_2_2, "label": "tabby, tabby cat"},
              {"score": 0.2_3_6_2, "label": "Egyptian cat"},
              {"score": 0.1_8_5_6, "label": "tiger cat"},
              {"score": 0.0_3_2_4, "label": "remote control, remote"},
              {"score": 0.0_0_9_6, "label": "quilt, comforter, comfort, puff"},
          ]  ,      )




from __future__ import annotations

from queue import Queue




def        lowerCAmelCase(	SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_       )->		tuple[int, int]:



    """simple docstring"""






    a ^= b
    b ^= a
    a ^= b
    return a, b




def        lowerCAmelCase(	SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_       )->		list[list[int]]:



    """simple docstring"""






    UpperCamelCase_          =		1
    while (1 << j) < max_node:
        for i in range(1       ,		max_node + 1       ):
            UpperCamelCase_          =		parent[j - 1][parent[j - 1][i]]
        j += 1
    return parent




def        lowerCAmelCase(	SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_       )->		int:



    """simple docstring"""






    if level[u] < level[v]:
        UpperCamelCase_   ,    UpperCamelCase_          =		swap(SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_       )
    # making depth of u same as depth of v
    for i in range(1_8       ,		-1       ,		-1       ):
        if level[u] - (1 << i) >= level[v]:
            UpperCamelCase_          =		parent[i][u]
    # at the same depth if u==v that mean lca is found
    if u == v:
        return u
    # moving both nodes upwards till lca in found
    for i in range(1_8       ,		-1       ,		-1       ):
        if parent[i][u] not in [0, parent[i][v]]:
            UpperCamelCase_   ,    UpperCamelCase_          =		parent[i][u], parent[i][v]
    # returning longest common ancestor of u,v
    return parent[0][u]




def        lowerCAmelCase(	SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_ = 1       ,		)->		tuple[list[int], list[list[int]]]:



    """simple docstring"""






    UpperCamelCase_          =		0
    UpperCamelCase_          =		Queue(maxsize=SCREAMING_SNAKE_CASE_       )
    q.put(SCREAMING_SNAKE_CASE_       )
    while q.qsize() != 0:
        UpperCamelCase_          =		q.get()
        for v in graph[u]:
            if level[v] == -1:
                UpperCamelCase_          =		level[u] + 1
                q.put(SCREAMING_SNAKE_CASE_       )
                UpperCamelCase_          =		u
    return level, parent




def        lowerCAmelCase(	)->		None:



    """simple docstring"""






    UpperCamelCase_          =		1_3
    # initializing with 0
    UpperCamelCase_          =		[[0 for _ in range(max_node + 1_0       )] for _ in range(2_0       )]
    # initializing with -1 which means every node is unvisited
    UpperCamelCase_          =		[-1 for _ in range(max_node + 1_0       )]
    UpperCamelCase_          =		{
        1: [2, 3, 4],
        2: [5],
        3: [6, 7],
        4: [8],
        5: [9, 1_0],
        6: [1_1],
        7: [],
        8: [1_2, 1_3],
        9: [],
        1_0: [],
        1_1: [],
        1_2: [],
        1_3: [],
    }
    UpperCamelCase_   ,    UpperCamelCase_          =		breadth_first_search(SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_       ,		1       )
    UpperCamelCase_          =		create_sparse(SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_       )
    print("LCA of node 1 and 3 is: "       ,		lowest_common_ancestor(1       ,		3       ,		SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_       )       )
    print("LCA of node 5 and 6 is: "       ,		lowest_common_ancestor(5       ,		6       ,		SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_       )       )
    print("LCA of node 7 and 11 is: "       ,		lowest_common_ancestor(7       ,		1_1       ,		SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_       )       )
    print("LCA of node 6 and 7 is: "       ,		lowest_common_ancestor(6       ,		7       ,		SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_       )       )
    print("LCA of node 4 and 12 is: "       ,		lowest_common_ancestor(4       ,		1_2       ,		SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_       )       )
    print("LCA of node 8 and 8 is: "       ,		lowest_common_ancestor(8       ,		8       ,		SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_       )       )


if __name__ == "__main__":
 main()
