





import json
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
from transformers import (
    CONFIG_MAPPING,
    IMAGE_PROCESSOR_MAPPING,
    AutoConfig,
    AutoImageProcessor,
    CLIPConfig,
    CLIPImageProcessor,
)
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER


sys.path.append(str(Path(__file__).parent.parent.parent.parent / """utils"""))

from test_module.custom_configuration import CustomConfig  # noqa E402
from test_module.custom_image_processing import CustomImageProcessor  # noqa E402

class      A       (	unittest.TestCase						):



      def 	lowerCAmelCase__    (	self:						Any       )						->    Any:
         '''simple docstring'''

         UpperCAmelCase_     :   List[Any]											=0



      def 	lowerCAmelCase__    (	self:						Any       )						->    Optional[int]:
         '''simple docstring'''

         UpperCAmelCase_     :   Union[str, Any]											=AutoImageProcessor.from_pretrained("openai/clip-vit-base-patch32"       )
         self.assertIsInstance(A_		,  A_       )



      def 	lowerCAmelCase__    (	self:						Any       )						->    Any:
         '''simple docstring'''

         with tempfile.TemporaryDirectory() as tmpdirname:
            UpperCAmelCase_     :   Tuple											=Path(A_       ) / "preprocessor_config.json"
            UpperCAmelCase_     :   Union[str, Any]											=Path(A_       ) / "config.json"
            json.dump(
                {"image_processor_type": "CLIPImageProcessor", "processor_class": "CLIPProcessor"}		,  open(A_		,  "w"       )		,  )
            json.dump({"model_type": "clip"}		,  open(A_		,  "w"       )       )

            UpperCAmelCase_     :   List[str]											=AutoImageProcessor.from_pretrained(A_       )
            self.assertIsInstance(A_		,  A_       )



      def 	lowerCAmelCase__    (	self:						Optional[Any]       )						->    Union[str, Any]:
         '''simple docstring'''

         with tempfile.TemporaryDirectory() as tmpdirname:
            UpperCAmelCase_     :   Union[str, Any]											=Path(A_       ) / "preprocessor_config.json"
            UpperCAmelCase_     :   int											=Path(A_       ) / "config.json"
            json.dump(
                {"feature_extractor_type": "CLIPFeatureExtractor", "processor_class": "CLIPProcessor"}		,  open(A_		,  "w"       )		,  )
            json.dump({"model_type": "clip"}		,  open(A_		,  "w"       )       )

            UpperCAmelCase_     :   Tuple											=AutoImageProcessor.from_pretrained(A_       )
            self.assertIsInstance(A_		,  A_       )



      def 	lowerCAmelCase__    (	self:						List[str]       )						->    Union[str, Any]:
         '''simple docstring'''

         with tempfile.TemporaryDirectory() as tmpdirname:
            UpperCAmelCase_     :   List[str]											=CLIPConfig()

            # Create a dummy config file with image_proceesor_type
            UpperCAmelCase_     :   List[str]											=Path(A_       ) / "preprocessor_config.json"
            UpperCAmelCase_     :   Tuple											=Path(A_       ) / "config.json"
            json.dump(
                {"image_processor_type": "CLIPImageProcessor", "processor_class": "CLIPProcessor"}		,  open(A_		,  "w"       )		,  )
            json.dump({"model_type": "clip"}		,  open(A_		,  "w"       )       )

            # remove image_processor_type to make sure config.json alone is enough to load image processor locally
            UpperCAmelCase_     :   Optional[Any]											=AutoImageProcessor.from_pretrained(A_       ).to_dict()

            config_dict.pop("image_processor_type"       )
            UpperCAmelCase_     :   Any											=CLIPImageProcessor(**A_       )

            # save in new folder
            model_config.save_pretrained(A_       )
            config.save_pretrained(A_       )

            UpperCAmelCase_     :   Any											=AutoImageProcessor.from_pretrained(A_       )

            # make sure private variable is not incorrectly saved
            UpperCAmelCase_     :   List[str]											=json.loads(config.to_json_string()       )
            self.assertTrue("_processor_class" not in dict_as_saved       )

         self.assertIsInstance(A_		,  A_       )



      def 	lowerCAmelCase__    (	self:						Dict       )						->    Union[str, Any]:
         '''simple docstring'''

         with tempfile.TemporaryDirectory() as tmpdirname:
            UpperCAmelCase_     :   Optional[int]											=Path(A_       ) / "preprocessor_config.json"
            json.dump(
                {"image_processor_type": "CLIPImageProcessor", "processor_class": "CLIPProcessor"}		,  open(A_		,  "w"       )		,  )

            UpperCAmelCase_     :   List[str]											=AutoImageProcessor.from_pretrained(A_       )
            self.assertIsInstance(A_		,  A_       )



      def 	lowerCAmelCase__    (	self:						Tuple       )						->    Optional[Any]:
         '''simple docstring'''

         with self.assertRaisesRegex(
             A_		,  "clip-base is not a local folder and is not a valid model identifier"       ):
            UpperCAmelCase_     :   Any											=AutoImageProcessor.from_pretrained("clip-base"       )



      def 	lowerCAmelCase__    (	self:						List[Any]       )						->    Dict:
         '''simple docstring'''

         with self.assertRaisesRegex(
             A_		,  r"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"       ):
            UpperCAmelCase_     :   Tuple											=AutoImageProcessor.from_pretrained(A_		,  revision="aaaaaa"       )



      def 	lowerCAmelCase__    (	self:						Optional[Any]       )						->    int:
         '''simple docstring'''

         with self.assertRaisesRegex(
             A_		,  "hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json."		,  ):
            UpperCAmelCase_     :   Union[str, Any]											=AutoImageProcessor.from_pretrained("hf-internal-testing/config-no-model"       )



      def 	lowerCAmelCase__    (	self:						List[Any]       )						->    int:
         '''simple docstring'''

         with self.assertRaises(A_       ):
            UpperCAmelCase_     :   List[Any]											=AutoImageProcessor.from_pretrained("hf-internal-testing/test_dynamic_image_processor"       )
         # If remote code is disabled, we can't load this config.
         with self.assertRaises(A_       ):
            UpperCAmelCase_     :   str											=AutoImageProcessor.from_pretrained(
                "hf-internal-testing/test_dynamic_image_processor"		,  trust_remote_code=A_       )

         UpperCAmelCase_     :   str											=AutoImageProcessor.from_pretrained(
             "hf-internal-testing/test_dynamic_image_processor"		,  trust_remote_code=A_       )
         self.assertEqual(image_processor.__class__.__name__		,  "NewImageProcessor"       )

         # Test image processor can be reloaded.
         with tempfile.TemporaryDirectory() as tmp_dir:
            image_processor.save_pretrained(A_       )
            UpperCAmelCase_     :   str											=AutoImageProcessor.from_pretrained(A_		,  trust_remote_code=A_       )
         self.assertEqual(reloaded_image_processor.__class__.__name__		,  "NewImageProcessor"       )



      def 	lowerCAmelCase__    (	self:						int       )						->    List[str]:
         '''simple docstring'''

         try:
            AutoConfig.register("custom"		,  A_       )
            AutoImageProcessor.register(A_		,  A_       )
            # Trying to register something existing in the Transformers library will raise an error
            with self.assertRaises(A_       ):
               AutoImageProcessor.register(A_		,  A_       )

            with tempfile.TemporaryDirectory() as tmpdirname:
               UpperCAmelCase_     :   List[Any]											=Path(A_       ) / "preprocessor_config.json"
               UpperCAmelCase_     :   Optional[Any]											=Path(A_       ) / "config.json"
               json.dump(
                   {"feature_extractor_type": "CLIPFeatureExtractor", "processor_class": "CLIPProcessor"}		,  open(A_		,  "w"       )		,  )
               json.dump({"model_type": "clip"}		,  open(A_		,  "w"       )       )

               UpperCAmelCase_     :   List[str]											=CustomImageProcessor.from_pretrained(A_       )

            # Now that the config is registered, it can be used as any other config with the auto-API
            with tempfile.TemporaryDirectory() as tmp_dir:
               image_processor.save_pretrained(A_       )
               UpperCAmelCase_     :   Optional[int]											=AutoImageProcessor.from_pretrained(A_       )
               self.assertIsInstance(A_		,  A_       )

         finally:
            if "custom" in CONFIG_MAPPING._extra_content:
               del CONFIG_MAPPING._extra_content["custom"]
            if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content:
               del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig]






      def 	lowerCAmelCase__    (	self:						Dict       )						->    Optional[Any]:
         '''simple docstring'''

         class      A       (	_lowercase						):
               _snake_case							     =True

         try:
            AutoConfig.register("custom"		,  A_       )
            AutoImageProcessor.register(A_		,  A_       )
            # If remote code is not set, the default is to use local
            UpperCAmelCase_     :   Dict											=AutoImageProcessor.from_pretrained("hf-internal-testing/test_dynamic_image_processor"       )
            self.assertEqual(image_processor.__class__.__name__		,  "NewImageProcessor"       )
            self.assertTrue(image_processor.is_local       )

            # If remote code is disabled, we load the local one.
            UpperCAmelCase_     :   int											=AutoImageProcessor.from_pretrained(
                "hf-internal-testing/test_dynamic_image_processor"		,  trust_remote_code=A_       )
            self.assertEqual(image_processor.__class__.__name__		,  "NewImageProcessor"       )
            self.assertTrue(image_processor.is_local       )

            # If remote is enabled, we load from the Hub
            UpperCAmelCase_     :   Dict											=AutoImageProcessor.from_pretrained(
                "hf-internal-testing/test_dynamic_image_processor"		,  trust_remote_code=A_       )
            self.assertEqual(image_processor.__class__.__name__		,  "NewImageProcessor"       )
            self.assertTrue(not hasattr(A_		,  "is_local"       )       )

         finally:
            if "custom" in CONFIG_MAPPING._extra_content:
               del CONFIG_MAPPING._extra_content["custom"]
            if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content:
               del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig]











from __future__ import annotations

from sys import maxsize
from typing import Generic, TypeVar

__lowercase  :   Optional[int]						      =TypeVar("""T""")




def     a__ (					lowercase__  ):






   '''simple docstring'''






   return (position - 1) // 2




def     a__ (					lowercase__  ):






   '''simple docstring'''






   return (2 * position) + 1




def     a__ (					lowercase__  ):






   '''simple docstring'''






   return (2 * position) + 2

class      A       (	Generic[T]						):



      def __init__(	self:						List[str]       )						->    None:
         '''simple docstring'''

         UpperCAmelCase_											=[]
         UpperCAmelCase_											={}
         UpperCAmelCase_											=0



      def __len__(	self:						Union[str, Any]       )						->    int:
         '''simple docstring'''

         return self.elements



      def __repr__(	self:						Dict       )						->    str:
         '''simple docstring'''

         return str(self.heap       )



      def 	lowerCAmelCase__    (	self:						Any       )						->    bool:
         '''simple docstring'''

         return self.elements == 0



      def 	lowerCAmelCase__    (	self:						Optional[int]		,  _lowerCAmelCase:						T		,  _lowerCAmelCase:						int       )						->    None:
         '''simple docstring'''

         self.heap.append((elem, weight)       )
         UpperCAmelCase_											=self.elements
         self.elements += 1
         self._bubble_up(_lowerCAmelCase       )



      def 	lowerCAmelCase__    (	self:						Tuple       )						->    T:
         '''simple docstring'''

         if self.elements > 1:
            self._swap_nodes(0		,  self.elements - 1       )
         UpperCAmelCase_						,       UpperCAmelCase_											=self.heap.pop()
         del self.position_map[elem]
         self.elements -= 1
         if self.elements > 0:
            UpperCAmelCase_						,       UpperCAmelCase_											=self.heap[0]
            self._bubble_down(_lowerCAmelCase       )
         return elem



      def 	lowerCAmelCase__    (	self:						Optional[Any]		,  _lowerCAmelCase:						T		,  _lowerCAmelCase:						int       )						->    None:
         '''simple docstring'''

         UpperCAmelCase_											=self.position_map[elem]
         UpperCAmelCase_											=(elem, weight)
         if position > 0:
            UpperCAmelCase_											=get_parent_position(_lowerCAmelCase       )
            UpperCAmelCase_						,       UpperCAmelCase_											=self.heap[parent_position]
            if parent_weight > weight:
               self._bubble_up(_lowerCAmelCase       )
            else:
               self._bubble_down(_lowerCAmelCase       )
         else:
            self._bubble_down(_lowerCAmelCase       )



      def 	lowerCAmelCase__    (	self:						Any		,  _lowerCAmelCase:						T       )						->    None:
         '''simple docstring'''

         UpperCAmelCase_											=self.position_map[elem]
         if curr_pos == 0:
            return None
         UpperCAmelCase_											=get_parent_position(_lowerCAmelCase       )
         UpperCAmelCase_						,       UpperCAmelCase_											=self.heap[curr_pos]
         UpperCAmelCase_						,       UpperCAmelCase_											=self.heap[parent_position]
         if parent_weight > weight:
            self._swap_nodes(_lowerCAmelCase		,  _lowerCAmelCase       )
            return self._bubble_up(_lowerCAmelCase       )
         return None



      def 	lowerCAmelCase__    (	self:						Optional[Any]		,  _lowerCAmelCase:						T       )						->    None:
         '''simple docstring'''

         UpperCAmelCase_											=self.position_map[elem]
         UpperCAmelCase_						,       UpperCAmelCase_											=self.heap[curr_pos]
         UpperCAmelCase_											=get_child_left_position(_lowerCAmelCase       )
         UpperCAmelCase_											=get_child_right_position(_lowerCAmelCase       )
         if child_left_position < self.elements and child_right_position < self.elements:
            UpperCAmelCase_						,       UpperCAmelCase_											=self.heap[child_left_position]
            UpperCAmelCase_						,       UpperCAmelCase_											=self.heap[child_right_position]
            if child_right_weight < child_left_weight and child_right_weight < weight:
               self._swap_nodes(_lowerCAmelCase		,  _lowerCAmelCase       )
               return self._bubble_down(_lowerCAmelCase       )
         if child_left_position < self.elements:
            UpperCAmelCase_						,       UpperCAmelCase_											=self.heap[child_left_position]
            if child_left_weight < weight:
               self._swap_nodes(_lowerCAmelCase		,  _lowerCAmelCase       )
               return self._bubble_down(_lowerCAmelCase       )
         else:
            return None
         if child_right_position < self.elements:
            UpperCAmelCase_						,       UpperCAmelCase_											=self.heap[child_right_position]
            if child_right_weight < weight:
               self._swap_nodes(_lowerCAmelCase		,  _lowerCAmelCase       )
               return self._bubble_down(_lowerCAmelCase       )
         return None






      def 	lowerCAmelCase__    (	self:						Optional[Any]		,  _lowerCAmelCase:						int		,  _lowerCAmelCase:						int       )						->    None:
         '''simple docstring'''

         UpperCAmelCase_											=self.heap[nodea_pos][0]
         UpperCAmelCase_											=self.heap[nodea_pos][0]
         UpperCAmelCase_						,       UpperCAmelCase_											=(
             self.heap[nodea_pos],
             self.heap[nodea_pos],
         )
         UpperCAmelCase_											=nodea_pos
         UpperCAmelCase_											=nodea_pos

class      A       (	Generic[T]						):



      def __init__(	self:						Tuple       )						->    None:
         '''simple docstring'''

         UpperCAmelCase_											={}
         UpperCAmelCase_											=0



      def __repr__(	self:						List[str]       )						->    str:
         '''simple docstring'''

         return str(self.connections       )



      def __len__(	self:						Optional[Any]       )						->    int:
         '''simple docstring'''

         return self.nodes



      def 	lowerCAmelCase__    (	self:						Union[str, Any]		,  _lowerCAmelCase:						T       )						->    None:
         '''simple docstring'''

         if node not in self.connections:
            UpperCAmelCase_											={}
            self.nodes += 1






      def 	lowerCAmelCase__    (	self:						Optional[Any]		,  _lowerCAmelCase:						T		,  _lowerCAmelCase:						T		,  _lowerCAmelCase:						int       )						->    None:
         '''simple docstring'''

         self.add_node(_lowerCAmelCase       )
         self.add_node(_lowerCAmelCase       )
         UpperCAmelCase_											=weight
         UpperCAmelCase_											=weight




def     a__ (					lowercase__  ,    ):






   '''simple docstring'''






   UpperCAmelCase_											={node: maxsize for node in graph.connections}
   UpperCAmelCase_											={node: None for node in graph.connections}

   UpperCAmelCase_											=MinPriorityQueue()
   for node, weight in dist.items():
      priority_queue.push(lowercase__  ,    lowercase__  )

   if priority_queue.is_empty():
      return dist, parent

   # initialization
   UpperCAmelCase_											=priority_queue.extract_min()
   UpperCAmelCase_											=0
   for neighbour in graph.connections[node]:
      if dist[neighbour] > dist[node] + graph.connections[node][neighbour]:
         UpperCAmelCase_											=dist[node] + graph.connections[node][neighbour]
         priority_queue.update_key(lowercase__  ,    dist[neighbour]  )
         UpperCAmelCase_											=node

    # running prim's algorithm
   while not priority_queue.is_empty():
      UpperCAmelCase_											=priority_queue.extract_min()
      for neighbour in graph.connections[node]:
         if dist[neighbour] > dist[node] + graph.connections[node][neighbour]:
            UpperCAmelCase_											=dist[node] + graph.connections[node][neighbour]
            priority_queue.update_key(lowercase__  ,    dist[neighbour]  )
            UpperCAmelCase_											=node
   return dist, parent




