






"""simple docstring"""


import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import BertTokenizer, BertTokenizerFast
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import FEATURE_EXTRACTOR_NAME, is_vision_available


if is_vision_available():
		from PIL import Image

		from transformers import ChineseCLIPImageProcessor, ChineseCLIPProcessor




@require_vision
class 						__UpperCamelCase     (     unittest.TestCase						):
					def        UpperCAmelCase__ (							self							:	Union[str, Any]   )	->       Tuple:
											lowerCAmelCase   :Tuple						=			tempfile.mkdtemp()

											lowerCAmelCase   :List[str]						=			[
											    '[UNK]',
											    '[CLS]',
											    '[SEP]',
											    '[PAD]',
											    '[MASK]',
											    '的',
											    '价',
											    '格',
											    '是',
											    '15',
											    '便',
											    'alex',
											    '##andra',
											    '，',
											    '。',
											    '-',
											    't',
											    'shirt',
											]
											lowerCAmelCase   :int						=			os.path.join(self.tmpdirname     , VOCAB_FILES_NAMES['vocab_file']   )
											with open(self.vocab_file     , 'w'     , encoding='utf-8'   ) as vocab_writer:
																	vocab_writer.write(''.join([x + '\n' for x in vocab_tokens]   )   )

											lowerCAmelCase   :int						=			{
											    'do_resize': True,
											    'size': {'height': 224, 'width': 224},
											    'do_center_crop': True,
											    'crop_size': {'height': 18, 'width': 18},
											    'do_normalize': True,
											    'image_mean': [0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3],
											    'image_std': [0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1],
											    'do_convert_rgb': True,
											}
											lowerCAmelCase   :Dict						=			os.path.join(self.tmpdirname     , UpperCAmelCase   )
											with open(self.image_processor_file     , 'w'     , encoding='utf-8'   ) as fp:
																	json.dump(UpperCAmelCase     , UpperCAmelCase   )
					def        UpperCAmelCase__ (							self							:	List[Any]     , **UpperCAmelCase							:	Union[str, Any]   )	->       Optional[Any]:
											return BertTokenizer.from_pretrained(self.tmpdirname     , **UpperCAmelCase   )
					def        UpperCAmelCase__ (							self							:	Optional[Any]     , **UpperCAmelCase							:	Union[str, Any]   )	->       int:
											return BertTokenizerFast.from_pretrained(self.tmpdirname     , **UpperCAmelCase   )
					def        UpperCAmelCase__ (							self							:	Any     , **UpperCAmelCase							:	List[str]   )	->       Union[str, Any]:
											return ChineseCLIPImageProcessor.from_pretrained(self.tmpdirname     , **UpperCAmelCase   )
					def        UpperCAmelCase__ (							self							:	Tuple   )	->       List[Any]:
											shutil.rmtree(self.tmpdirname   )
					def        UpperCAmelCase__ (							self							:	Dict   )	->       List[Any]:
											lowerCAmelCase   :str						=			[np.random.randint(255     , size=(3, 30, 400)     , dtype=np.uinta   )]

											lowerCAmelCase   :Optional[int]						=			[Image.fromarray(np.moveaxis(UpperCAmelCase     , 0     , -1   )   ) for x in image_inputs]

											return image_inputs
					def        UpperCAmelCase__ (							self							:	Any   )	->       Optional[int]:
											lowerCAmelCase   :Union[str, Any]						=			self.get_tokenizer()
											lowerCAmelCase   :Union[str, Any]						=			self.get_rust_tokenizer()
											lowerCAmelCase   :Any						=			self.get_image_processor()

											lowerCAmelCase   :Optional[Any]						=			ChineseCLIPProcessor(tokenizer=UpperCAmelCase     , image_processor=UpperCAmelCase   )
											processor_slow.save_pretrained(self.tmpdirname   )
											lowerCAmelCase   :Union[str, Any]						=			ChineseCLIPProcessor.from_pretrained(self.tmpdirname     , use_fast=UpperCAmelCase   )

											lowerCAmelCase   :int						=			ChineseCLIPProcessor(tokenizer=UpperCAmelCase     , image_processor=UpperCAmelCase   )
											processor_fast.save_pretrained(self.tmpdirname   )
											lowerCAmelCase   :Any						=			ChineseCLIPProcessor.from_pretrained(self.tmpdirname   )

											self.assertEqual(processor_slow.tokenizer.get_vocab()     , tokenizer_slow.get_vocab()   )
											self.assertEqual(processor_fast.tokenizer.get_vocab()     , tokenizer_fast.get_vocab()   )
											self.assertEqual(tokenizer_slow.get_vocab()     , tokenizer_fast.get_vocab()   )
											self.assertIsInstance(processor_slow.tokenizer     , UpperCAmelCase   )
											self.assertIsInstance(processor_fast.tokenizer     , UpperCAmelCase   )

											self.assertEqual(processor_slow.image_processor.to_json_string()     , image_processor.to_json_string()   )
											self.assertEqual(processor_fast.image_processor.to_json_string()     , image_processor.to_json_string()   )
											self.assertIsInstance(processor_slow.image_processor     , UpperCAmelCase   )
											self.assertIsInstance(processor_fast.image_processor     , UpperCAmelCase   )
					def        UpperCAmelCase__ (							self							:	Tuple   )	->       Any:
											lowerCAmelCase   :Union[str, Any]						=			ChineseCLIPProcessor(tokenizer=self.get_tokenizer()     , image_processor=self.get_image_processor()   )
											processor.save_pretrained(self.tmpdirname   )

											lowerCAmelCase   :Union[str, Any]						=			self.get_tokenizer(cls_token='(CLS)'     , sep_token='(SEP)'   )
											lowerCAmelCase   :Optional[Any]						=			self.get_image_processor(do_normalize=UpperCAmelCase   )

											lowerCAmelCase   :Dict						=			ChineseCLIPProcessor.from_pretrained(
											    self.tmpdirname     , cls_token='(CLS)'     , sep_token='(SEP)'     , do_normalize=UpperCAmelCase   )

											self.assertEqual(processor.tokenizer.get_vocab()     , tokenizer_add_kwargs.get_vocab()   )
											self.assertIsInstance(processor.tokenizer     , UpperCAmelCase   )

											self.assertEqual(processor.image_processor.to_json_string()     , image_processor_add_kwargs.to_json_string()   )
											self.assertIsInstance(processor.image_processor     , UpperCAmelCase   )
					def        UpperCAmelCase__ (							self							:	List[str]   )	->       str:
											lowerCAmelCase   :Tuple						=			self.get_image_processor()
											lowerCAmelCase   :str						=			self.get_tokenizer()

											lowerCAmelCase   :int						=			ChineseCLIPProcessor(tokenizer=UpperCAmelCase     , image_processor=UpperCAmelCase   )

											lowerCAmelCase   :int						=			self.prepare_image_inputs()

											lowerCAmelCase   :Dict						=			image_processor(UpperCAmelCase     , return_tensors='np'   )
											lowerCAmelCase   :Optional[int]						=			processor(images=UpperCAmelCase     , return_tensors='np'   )

											for key in input_feat_extract.keys():
																	self.assertAlmostEqual(input_feat_extract[key].sum()     , input_processor[key].sum()     , delta=1e-2   )
					def        UpperCAmelCase__ (							self							:	List[Any]   )	->       Tuple:
											lowerCAmelCase   :Optional[int]						=			self.get_image_processor()
											lowerCAmelCase   :int						=			self.get_tokenizer()

											lowerCAmelCase   :Union[str, Any]						=			ChineseCLIPProcessor(tokenizer=UpperCAmelCase     , image_processor=UpperCAmelCase   )

											lowerCAmelCase   :Optional[int]						=			'Alexandra，T-shirt的价格是15便士。'

											lowerCAmelCase   :Optional[Any]						=			processor(text=UpperCAmelCase   )

											lowerCAmelCase   :List[str]						=			tokenizer(UpperCAmelCase   )

											for key in encoded_tok.keys():
																	self.assertListEqual(encoded_tok[key]     , encoded_processor[key]   )
					def        UpperCAmelCase__ (							self							:	Tuple   )	->       Optional[int]:
											lowerCAmelCase   :Tuple						=			self.get_image_processor()
											lowerCAmelCase   :Optional[int]						=			self.get_tokenizer()

											lowerCAmelCase   :List[Any]						=			ChineseCLIPProcessor(tokenizer=UpperCAmelCase     , image_processor=UpperCAmelCase   )

											lowerCAmelCase   :Any						=			'Alexandra，T-shirt的价格是15便士。'
											lowerCAmelCase   :Any						=			self.prepare_image_inputs()

											lowerCAmelCase   :Any						=			processor(text=UpperCAmelCase     , images=UpperCAmelCase   )

											self.assertListEqual(list(inputs.keys()   )     , ['input_ids', 'token_type_ids', 'attention_mask', 'pixel_values']   )

											# test if it raises when no input is passed
											with pytest.raises(UpperCAmelCase   ):
																	processor()
					def        UpperCAmelCase__ (							self							:	Dict   )	->       int:
											lowerCAmelCase   :Union[str, Any]						=			self.get_image_processor()
											lowerCAmelCase   :Union[str, Any]						=			self.get_tokenizer()

											lowerCAmelCase   :Any						=			ChineseCLIPProcessor(tokenizer=UpperCAmelCase     , image_processor=UpperCAmelCase   )

											lowerCAmelCase   :Optional[int]						=			[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

											lowerCAmelCase   :Dict						=			processor.batch_decode(UpperCAmelCase   )
											lowerCAmelCase   :List[str]						=			tokenizer.batch_decode(UpperCAmelCase   )

											self.assertListEqual(UpperCAmelCase     , UpperCAmelCase   )
					def        UpperCAmelCase__ (							self							:	int   )	->       str:
											lowerCAmelCase   :Dict						=			self.get_image_processor()
											lowerCAmelCase   :List[str]						=			self.get_tokenizer()

											lowerCAmelCase   :Tuple						=			ChineseCLIPProcessor(tokenizer=UpperCAmelCase     , image_processor=UpperCAmelCase   )

											lowerCAmelCase   :Tuple						=			'Alexandra，T-shirt的价格是15便士。'
											lowerCAmelCase   :Dict						=			self.prepare_image_inputs()

											lowerCAmelCase   :Tuple						=			processor(text=UpperCAmelCase     , images=UpperCAmelCase   )

											self.assertListEqual(list(inputs.keys()   )     , processor.model_input_names   )







"""simple docstring"""


import heapq as hq
import math
from collections.abc import Iterator




class 						__UpperCamelCase     :
					def __init__(							self							:	Union[str, Any]     , UpperCAmelCase							:	List[Any]   )	->       List[str]:
											lowerCAmelCase   :Dict						=			str(id_   )
											lowerCAmelCase   :str						=			None
											lowerCAmelCase   :Any						=			None
											lowerCAmelCase   :List[str]						=			[]
											lowerCAmelCase   :List[Any]						=			{}  # {vertex:distance}
					def __lt__(							self							:	Union[str, Any]     , UpperCAmelCase							:	Any   )	->       str:
											return self.key < other.key
					def __repr__(							self							:	Union[str, Any]   )	->       Dict:
											return self.id
					def        UpperCAmelCase__ (							self							:	Any     , UpperCAmelCase							:	Tuple   )	->       List[str]:
											self.neighbors.append(UpperCAmelCase   )
					def        UpperCAmelCase__ (							self							:	str     , UpperCAmelCase							:	Optional[Any]     , UpperCAmelCase							:	List[Any]   )	->       Optional[int]:
											lowerCAmelCase   :str						=			weight


def        UpperCAmelCase      (					a__     ,      a__     ,      a__     ,      a__ ):





						'''simple docstring'''






						graph[a - 1].add_neighbor(graph[b - 1] )
						graph[b - 1].add_neighbor(graph[a - 1] )
						# add the edges:
						graph[a - 1].add_edge(graph[b - 1]     ,      a__ )
						graph[b - 1].add_edge(graph[a - 1]     ,      a__ )


def        UpperCAmelCase      (					a__     ,      a__ ):





						'''simple docstring'''






						lowerCAmelCase   :List[str]						=			[]
						for u in graph:
												lowerCAmelCase   :Tuple						=			math.inf
												lowerCAmelCase   :Any						=			None
						lowerCAmelCase   :Dict						=			0
						lowerCAmelCase   :int						=			graph[:]
						while q:
												lowerCAmelCase   :str						=			min(a__ )
												q.remove(a__ )
												for v in u.neighbors:
																		if (v in q) and (u.edges[v.id] < v.key):
																								lowerCAmelCase   :List[str]						=			u
																								lowerCAmelCase   :List[Any]						=			u.edges[v.id]
						for i in range(1     ,      len(a__ ) ):
												a.append((int(graph[i].id ) + 1, int(graph[i].pi.id ) + 1) )
						return a


def        UpperCAmelCase      (					a__     ,      a__ ):





						'''simple docstring'''






						for u in graph:
												lowerCAmelCase   :List[str]						=			math.inf
												lowerCAmelCase   :str						=			None
						lowerCAmelCase   :Optional[int]						=			0

						lowerCAmelCase   :Dict						=			list(a__ )
						hq.heapify(a__ )

						while h:
												lowerCAmelCase   :Union[str, Any]						=			hq.heappop(a__ )
												for v in u.neighbors:
																		if (v in h) and (u.edges[v.id] < v.key):
																								lowerCAmelCase   :int						=			u
																								lowerCAmelCase   :List[Any]						=			u.edges[v.id]
																								hq.heapify(a__ )

						for i in range(1     ,      len(a__ ) ):
												yield (int(graph[i].id ) + 1, int(graph[i].pi.id ) + 1)


def        UpperCAmelCase      (					):





						'''simple docstring'''








if __name__ == "__main__":
		import doctest

		doctest.testmod()