


import itertools
import random
import unittest

import numpy as np

from transformers import is_speech_available
from transformers.testing_utils import require_torch, require_torchaudio

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_speech_available():
		from transformers import SpeechaTextFeatureExtractor

lowerCamelCase						=		random.Random()






def 					lowerCamelCase_   (	_a     ,       _a=1.0     ,       _a=None     ,       _a=None	):




		"""simple docstring"""






		if rng is None:
				lowerCAmelCase__       :      List[Any]   					= global_rng

		lowerCAmelCase__       :      Tuple   					= []
		for batch_idx in range(shape[0]	):
				values.append([]	)
				for _ in range(shape[1]	):
						values[-1].append(rng.random() * scale	)

		return values





@require_torch
@require_torchaudio
class  _a							(    unittest.TestCase):




	def __init__( self						:		Optional[Any]		, _SCREAMING_SNAKE_CASE						:		Union[str, Any]		, _SCREAMING_SNAKE_CASE						:		str=7		, _SCREAMING_SNAKE_CASE						:		Optional[Any]=400		, _SCREAMING_SNAKE_CASE						:		int=2000		, _SCREAMING_SNAKE_CASE						:		Optional[Any]=24		, _SCREAMING_SNAKE_CASE						:		Optional[int]=24		, _SCREAMING_SNAKE_CASE						:		Union[str, Any]=0.0		, _SCREAMING_SNAKE_CASE						:		str=1_6000		, _SCREAMING_SNAKE_CASE						:		Tuple=True		, _SCREAMING_SNAKE_CASE						:		Tuple=True		, )->	Optional[Any]:
			lowerCAmelCase__       :      Tuple   					= parent
			lowerCAmelCase__       :      int   					= batch_size
			lowerCAmelCase__       :      int   					= min_seq_length
			lowerCAmelCase__       :      Optional[int]   					= max_seq_length
			lowerCAmelCase__       :      Any   					= (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
			lowerCAmelCase__       :      List[Any]   					= feature_size
			lowerCAmelCase__       :      int   					= num_mel_bins
			lowerCAmelCase__       :      Union[str, Any]   					= padding_value
			lowerCAmelCase__       :      str   					= sampling_rate
			lowerCAmelCase__       :      Any   					= return_attention_mask
			lowerCAmelCase__       :      Dict   					= do_normalize




	def 							UpperCAmelCase__( self						:		Dict		)->	int:
			return {
			    "feature_size": self.feature_size,
			    "num_mel_bins": self.num_mel_bins,
			    "padding_value": self.padding_value,
			    "sampling_rate": self.sampling_rate,
			    "return_attention_mask": self.return_attention_mask,
			    "do_normalize": self.do_normalize,
			}




	def 							UpperCAmelCase__( self						:		Optional[int]		, _SCREAMING_SNAKE_CASE						:		Union[str, Any]=False		, _SCREAMING_SNAKE_CASE						:		Dict=False		)->	Any:
			def _flatten(_SCREAMING_SNAKE_CASE						:		List[Any]		):
					return list(itertools.chain(*_SCREAMING_SNAKE_CASE		)		)

			if equal_length:
					lowerCAmelCase__       :      List[Any]   					= [floats_list((self.max_seq_length, self.feature_size)		) for _ in range(self.batch_size		)]
			else:
					# make sure that inputs increase in size
					lowerCAmelCase__       :      Any   					= [
					    floats_list((x, self.feature_size)		)
					    for x in range(self.min_seq_length		, self.max_seq_length		, self.seq_length_diff		)
					]
			if numpify:
					lowerCAmelCase__       :      List[str]   					= [np.asarray(_SCREAMING_SNAKE_CASE		) for x in speech_inputs]
			return speech_inputs





@require_torch
@require_torchaudio
class  _a							(    _lowercase					,						unittest.TestCase):
	_a  :  Tuple				   =							SpeechaTextFeatureExtractor if is_speech_available() else None




	def 							UpperCAmelCase__( self						:		Union[str, Any]		)->	Any:
			lowerCAmelCase__       :      str   					= SpeechaTextFeatureExtractionTester(self		)




	def 							UpperCAmelCase__( self						:		List[Any]		, _SCREAMING_SNAKE_CASE						:		str		)->	Optional[int]:
			self.assertTrue(np.all(np.mean(_SCREAMING_SNAKE_CASE		, axis=0		) < 1E-3		)		)
			self.assertTrue(np.all(np.abs(np.var(_SCREAMING_SNAKE_CASE		, axis=0		) - 1		) < 1E-3		)		)




	def 							UpperCAmelCase__( self						:		Optional[int]		)->	int:
			# Tests that all call wrap to encode_plus and batch_encode_plus
			lowerCAmelCase__       :      Optional[int]   					= self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
			# create three inputs of length 800, 1000, and 1200
			lowerCAmelCase__       :      Optional[int]   					= [floats_list((1, x)		)[0] for x in range(800		, 1400		, 200		)]
			lowerCAmelCase__       :      Optional[int]   					= [np.asarray(_SCREAMING_SNAKE_CASE		) for speech_input in speech_inputs]

			# Test feature size
			lowerCAmelCase__       :      Dict   					= feature_extractor(_SCREAMING_SNAKE_CASE		, padding=_SCREAMING_SNAKE_CASE		, return_tensors='''np'''		).input_features
			self.assertTrue(input_features.ndim == 3		)
			self.assertTrue(input_features.shape[-1] == feature_extractor.feature_size		)

			# Test not batched input
			lowerCAmelCase__       :      str   					= feature_extractor(speech_inputs[0]		, return_tensors='''np'''		).input_features
			lowerCAmelCase__       :      Optional[Any]   					= feature_extractor(np_speech_inputs[0]		, return_tensors='''np'''		).input_features
			self.assertTrue(np.allclose(_SCREAMING_SNAKE_CASE		, _SCREAMING_SNAKE_CASE		, atol=1E-3		)		)

			# Test batched
			lowerCAmelCase__       :      int   					= feature_extractor(_SCREAMING_SNAKE_CASE		, return_tensors='''np'''		).input_features
			lowerCAmelCase__       :      Union[str, Any]   					= feature_extractor(_SCREAMING_SNAKE_CASE		, return_tensors='''np'''		).input_features
			for enc_seq_a, enc_seq_a in zip(_SCREAMING_SNAKE_CASE		, _SCREAMING_SNAKE_CASE		):
					self.assertTrue(np.allclose(_SCREAMING_SNAKE_CASE		, _SCREAMING_SNAKE_CASE		, atol=1E-3		)		)

			# Test 2-D numpy arrays are batched.
			lowerCAmelCase__       :      Optional[int]   					= [floats_list((1, x)		)[0] for x in (800, 800, 800)]
			lowerCAmelCase__       :      str   					= np.asarray(_SCREAMING_SNAKE_CASE		)
			lowerCAmelCase__       :      Tuple   					= feature_extractor(_SCREAMING_SNAKE_CASE		, return_tensors='''np'''		).input_features
			lowerCAmelCase__       :      List[Any]   					= feature_extractor(_SCREAMING_SNAKE_CASE		, return_tensors='''np'''		).input_features
			for enc_seq_a, enc_seq_a in zip(_SCREAMING_SNAKE_CASE		, _SCREAMING_SNAKE_CASE		):
					self.assertTrue(np.allclose(_SCREAMING_SNAKE_CASE		, _SCREAMING_SNAKE_CASE		, atol=1E-3		)		)




	def 							UpperCAmelCase__( self						:		Any		)->	Union[str, Any]:
			lowerCAmelCase__       :      Optional[int]   					= self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
			lowerCAmelCase__       :      Optional[Any]   					= [floats_list((1, x)		)[0] for x in range(800		, 1400		, 200		)]

			lowerCAmelCase__       :      Dict   					= ['''longest''', '''max_length''', '''do_not_pad''']
			lowerCAmelCase__       :      Any   					= [None, 16, None]
			for max_length, padding in zip(_SCREAMING_SNAKE_CASE		, _SCREAMING_SNAKE_CASE		):
					lowerCAmelCase__       :      Union[str, Any]   					= feature_extractor(
					    _SCREAMING_SNAKE_CASE		, padding=_SCREAMING_SNAKE_CASE		, max_length=_SCREAMING_SNAKE_CASE		, return_attention_mask=_SCREAMING_SNAKE_CASE		)
					lowerCAmelCase__       :      int   					= inputs.input_features
					lowerCAmelCase__       :      Optional[int]   					= inputs.attention_mask
					lowerCAmelCase__       :      str   					= [np.sum(_SCREAMING_SNAKE_CASE		) for x in attention_mask]

					self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]]		)
					self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]]		)
					self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]]		)




	def 							UpperCAmelCase__( self						:		List[Any]		)->	int:
			lowerCAmelCase__       :      Optional[int]   					= self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
			lowerCAmelCase__       :      Dict   					= [floats_list((1, x)		)[0] for x in range(800		, 1400		, 200		)]

			lowerCAmelCase__       :      Optional[Any]   					= ['''longest''', '''max_length''', '''do_not_pad''']
			lowerCAmelCase__       :      Optional[int]   					= [None, 16, None]
			for max_length, padding in zip(_SCREAMING_SNAKE_CASE		, _SCREAMING_SNAKE_CASE		):
					lowerCAmelCase__       :      str   					= feature_extractor(
					    _SCREAMING_SNAKE_CASE		, max_length=_SCREAMING_SNAKE_CASE		, padding=_SCREAMING_SNAKE_CASE		, return_tensors='''np'''		, return_attention_mask=_SCREAMING_SNAKE_CASE		)
					lowerCAmelCase__       :      Union[str, Any]   					= inputs.input_features
					lowerCAmelCase__       :      Optional[int]   					= inputs.attention_mask
					lowerCAmelCase__       :      str   					= [np.sum(_SCREAMING_SNAKE_CASE		) for x in attention_mask]

					self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]]		)
					self.assertTrue(input_features[0][fbank_feat_lengths[0] :].sum() < 1E-6		)
					self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]]		)
					self.assertTrue(input_features[0][fbank_feat_lengths[1] :].sum() < 1E-6		)
					self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]]		)




	def 							UpperCAmelCase__( self						:		str		)->	Optional[int]:
			lowerCAmelCase__       :      Optional[Any]   					= self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
			lowerCAmelCase__       :      List[str]   					= [floats_list((1, x)		)[0] for x in range(800		, 1400		, 200		)]
			lowerCAmelCase__       :      Optional[Any]   					= feature_extractor(
			    _SCREAMING_SNAKE_CASE		, padding='''max_length'''		, max_length=4		, truncation=_SCREAMING_SNAKE_CASE		, return_tensors='''np'''		, return_attention_mask=_SCREAMING_SNAKE_CASE		, )
			lowerCAmelCase__       :      Union[str, Any]   					= inputs.input_features
			lowerCAmelCase__       :      Any   					= inputs.attention_mask
			lowerCAmelCase__       :      str   					= np.sum(attention_mask == 1		, axis=1		)

			self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]]		)
			self._check_zero_mean_unit_variance(input_features[1]		)
			self._check_zero_mean_unit_variance(input_features[2]		)




	def 							UpperCAmelCase__( self						:		Dict		)->	Optional[int]:
			lowerCAmelCase__       :      Any   					= self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
			lowerCAmelCase__       :      List[str]   					= [floats_list((1, x)		)[0] for x in range(800		, 1400		, 200		)]
			lowerCAmelCase__       :      Tuple   					= feature_extractor(
			    _SCREAMING_SNAKE_CASE		, padding='''longest'''		, max_length=4		, truncation=_SCREAMING_SNAKE_CASE		, return_tensors='''np'''		, return_attention_mask=_SCREAMING_SNAKE_CASE		, )
			lowerCAmelCase__       :      Optional[int]   					= inputs.input_features
			lowerCAmelCase__       :      Optional[Any]   					= inputs.attention_mask
			lowerCAmelCase__       :      Tuple   					= np.sum(attention_mask == 1		, axis=1		)

			self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]]		)
			self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]]		)
			self._check_zero_mean_unit_variance(input_features[2]		)

			# make sure that if max_length < longest -> then pad to max_length
			self.assertEqual(input_features.shape		, (3, 4, 24)		)

			lowerCAmelCase__       :      List[Any]   					= [floats_list((1, x)		)[0] for x in range(800		, 1400		, 200		)]
			lowerCAmelCase__       :      Optional[int]   					= feature_extractor(
			    _SCREAMING_SNAKE_CASE		, padding='''longest'''		, max_length=16		, truncation=_SCREAMING_SNAKE_CASE		, return_tensors='''np'''		, return_attention_mask=_SCREAMING_SNAKE_CASE		, )
			lowerCAmelCase__       :      Any   					= inputs.input_features
			lowerCAmelCase__       :      int   					= inputs.attention_mask
			lowerCAmelCase__       :      List[Any]   					= np.sum(attention_mask == 1		, axis=1		)

			self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]]		)
			self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]]		)
			self._check_zero_mean_unit_variance(input_features[2]		)

			# make sure that if max_length < longest -> then pad to max_length
			self.assertEqual(input_features.shape		, (3, 6, 24)		)




	def 							UpperCAmelCase__( self						:		str		)->	Union[str, Any]:
			import torch

			lowerCAmelCase__       :      Optional[int]   					= self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
			lowerCAmelCase__       :      Optional[Any]   					= np.random.rand(100		, 32		).astype(np.floataa		)
			lowerCAmelCase__       :      Any   					= np_speech_inputs.tolist()

			for inputs in [py_speech_inputs, np_speech_inputs]:
					lowerCAmelCase__       :      List[Any]   					= feature_extractor.pad([{'''input_features''': inputs}]		, return_tensors='''np'''		)
					self.assertTrue(np_processed.input_features.dtype == np.floataa		)
					lowerCAmelCase__       :      int   					= feature_extractor.pad([{'''input_features''': inputs}]		, return_tensors='''pt'''		)
					self.assertTrue(pt_processed.input_features.dtype == torch.floataa		)




	def 							UpperCAmelCase__( self						:		List[str]		, _SCREAMING_SNAKE_CASE						:		Tuple		)->	List[str]:
			from datasets import load_dataset

			lowerCAmelCase__       :      str   					= load_dataset('''hf-internal-testing/librispeech_asr_dummy'''		, '''clean'''		, split='''validation'''		)
			# automatic decoding with librispeech
			lowerCAmelCase__       :      List[str]   					= ds.sort('''id'''		).select(range(_SCREAMING_SNAKE_CASE		)		)[:num_samples]['''audio''']

			return [x["array"] for x in speech_samples]




	def 							UpperCAmelCase__( self						:		Optional[int]		)->	int:
			# fmt: off
			lowerCAmelCase__       :      Optional[Any]   					= np.array([
			    -1.5745, -1.7713, -1.7020, -1.6069, -1.2250, -1.1105, -0.9072, -0.8241,
			    -1.2310, -0.8098, -0.3320, -0.4101, -0.7985, -0.4996, -0.8213, -0.9128,
			    -1.0420, -1.1286, -1.0440, -0.7999, -0.8405, -1.2275, -1.5443, -1.4625,
			]		)
			# fmt: on

			lowerCAmelCase__       :      str   					= self._load_datasamples(1		)
			lowerCAmelCase__       :      int   					= self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
			lowerCAmelCase__       :      int   					= feature_extractor(_SCREAMING_SNAKE_CASE		, return_tensors='''pt'''		).input_features
			self.assertEquals(input_features.shape		, (1, 584, 24)		)
			self.assertTrue(np.allclose(input_features[0, 0, :30]		, _SCREAMING_SNAKE_CASE		, atol=1E-4		)		)








import math





class  _a							:




	def __init__( self						:		List[Any]		, _SCREAMING_SNAKE_CASE						:		Any=0		)->	Optional[Any]:  # a graph with Node 0,1,...,N-1
			lowerCAmelCase__       :      Optional[int]   					= n
			lowerCAmelCase__       :      List[Any]   					= [
			    [math.inf for j in range(0		, _SCREAMING_SNAKE_CASE		)] for i in range(0		, _SCREAMING_SNAKE_CASE		)
			]  # adjacency matrix for weight
			lowerCAmelCase__       :      str   					= [
			    [math.inf for j in range(0		, _SCREAMING_SNAKE_CASE		)] for i in range(0		, _SCREAMING_SNAKE_CASE		)
			]  # dp[i][j] stores minimum distance from i to j




	def 							UpperCAmelCase__( self						:		Optional[Any]		, _SCREAMING_SNAKE_CASE						:		Optional[int]		, _SCREAMING_SNAKE_CASE						:		List[str]		, _SCREAMING_SNAKE_CASE						:		str		)->	List[str]:
			lowerCAmelCase__       :      Optional[int]   					= w




	def 							UpperCAmelCase__( self						:		List[Any]		)->	Optional[int]:
			for k in range(0		, self.n		):
					for i in range(0		, self.n		):
							for j in range(0		, self.n		):
									lowerCAmelCase__       :      Dict   					= min(self.dp[i][j]		, self.dp[i][k] + self.dp[k][j]		)




	def 							UpperCAmelCase__( self						:		str		, _SCREAMING_SNAKE_CASE						:		Any		, _SCREAMING_SNAKE_CASE						:		str		)->	str:
			return self.dp[u][v]


if __name__ == "__main__":
		lowerCamelCase						=		Graph(5)
		graph.add_edge(0, 2, 9)
		graph.add_edge(0, 4, 10)
		graph.add_edge(1, 3, 5)
		graph.add_edge(2, 3, 7)
		graph.add_edge(3, 0, 10)
		graph.add_edge(3, 1, 2)
		graph.add_edge(3, 2, 1)
		graph.add_edge(3, 4, 6)
		graph.add_edge(4, 1, 3)
		graph.add_edge(4, 2, 4)
		graph.add_edge(4, 3, 9)
		graph.floyd_warshall()
		graph.show_min(1, 4)
		graph.show_min(0, 3)




