



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

__snake_case       :	Tuple            =random.Random()






def 			lowerCAmelCase__	(					lowerCamelCase_	:	Union[str, Any] ,lowerCamelCase_	:	List[str]=1.0 ,lowerCamelCase_	:	Optional[int]=None ,lowerCamelCase_	:	int=None):





				'''simple docstring'''

				if rng is None:
								lowerCAmelCase__  :       Dict				=  global_rng

				lowerCAmelCase__  :       str				=  []
				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  lowerCamelCase__    (							unittest.TestCase):




		'''simple docstring'''



		def __init__(self	,__lowerCamelCase	,__lowerCamelCase=7	,__lowerCamelCase=4_00	,__lowerCamelCase=20_00	,__lowerCamelCase=24	,__lowerCamelCase=24	,__lowerCamelCase=0.0	,__lowerCamelCase=1_60_00	,__lowerCamelCase=True	,__lowerCamelCase=True	,)  ->    Optional[int]:

						"""simple docstring"""






						lowerCAmelCase__  :       Tuple				=  parent
						lowerCAmelCase__  :       str				=  batch_size
						lowerCAmelCase__  :       Optional[Any]				=  min_seq_length
						lowerCAmelCase__  :       List[Any]				=  max_seq_length
						lowerCAmelCase__  :       Optional[int]				=  (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
						lowerCAmelCase__  :       str				=  feature_size
						lowerCAmelCase__  :       str				=  num_mel_bins
						lowerCAmelCase__  :       Tuple				=  padding_value
						lowerCAmelCase__  :       Tuple				=  sampling_rate
						lowerCAmelCase__  :       Optional[Any]				=  return_attention_mask
						lowerCAmelCase__  :       Union[str, Any]				=  do_normalize



		def 	lowerCAmelCase__   (self				)  ->    int:

						"""simple docstring"""






						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 	lowerCAmelCase__   (self	,__lowerCamelCase=False	,__lowerCamelCase=False				)  ->    List[str]:

						"""simple docstring"""






						def _flatten(__lowerCamelCase				):
										return list(itertools.chain(*__lowerCamelCase				)				)

						if equal_length:
										lowerCAmelCase__  :       Tuple				=  [floats_list((self.max_seq_length, self.feature_size)				) for _ in range(self.batch_size				)]
						else:
										# make sure that inputs increase in size
										lowerCAmelCase__  :       Tuple				=  [
										    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__  :       Union[str, Any]				=  [np.asarray(__lowerCamelCase				) for x in speech_inputs]
						return speech_inputs



@require_torch
@require_torchaudio
class  lowerCamelCase__    (							lowerCamelCase__   ,       unittest.TestCase):




		'''simple docstring'''



		snake_case_			=SpeechaTextFeatureExtractor if is_speech_available() else None



		def 	lowerCAmelCase__   (self				)  ->    List[Any]:

						"""simple docstring"""






						lowerCAmelCase__  :       List[Any]				=  SpeechaTextFeatureExtractionTester(self				)



		def 	lowerCAmelCase__   (self	,__lowerCamelCase				)  ->    int:

						"""simple docstring"""






						self.assertTrue(np.all(np.mean(__lowerCamelCase	,axis=0				) < 1e-3				)				)
						self.assertTrue(np.all(np.abs(np.var(__lowerCamelCase	,axis=0				) - 1				) < 1e-3				)				)



		def 	lowerCAmelCase__   (self				)  ->    Any:

						"""simple docstring"""






						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__  :       Any				=  [floats_list((1, x)				)[0] for x in range(8_00	,14_00	,2_00				)]
						lowerCAmelCase__  :       Optional[Any]				=  [np.asarray(__lowerCamelCase				) for speech_input in speech_inputs]

						# Test feature size
						lowerCAmelCase__  :       str				=  feature_extractor(__lowerCamelCase	,padding=__lowerCamelCase	,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__  :       Tuple				=  feature_extractor(speech_inputs[0]	,return_tensors='''np'''				).input_features
						lowerCAmelCase__  :       Tuple				=  feature_extractor(np_speech_inputs[0]	,return_tensors='''np'''				).input_features
						self.assertTrue(np.allclose(__lowerCamelCase	,__lowerCamelCase	,atol=1e-3				)				)

						# Test batched
						lowerCAmelCase__  :       Dict				=  feature_extractor(__lowerCamelCase	,return_tensors='''np'''				).input_features
						lowerCAmelCase__  :       Dict				=  feature_extractor(__lowerCamelCase	,return_tensors='''np'''				).input_features
						for enc_seq_a, enc_seq_a in zip(__lowerCamelCase	,__lowerCamelCase				):
										self.assertTrue(np.allclose(__lowerCamelCase	,__lowerCamelCase	,atol=1e-3				)				)

						# Test 2-D numpy arrays are batched.
						lowerCAmelCase__  :       Optional[int]				=  [floats_list((1, x)				)[0] for x in (8_00, 8_00, 8_00)]
						lowerCAmelCase__  :       Union[str, Any]				=  np.asarray(__lowerCamelCase				)
						lowerCAmelCase__  :       Dict				=  feature_extractor(__lowerCamelCase	,return_tensors='''np'''				).input_features
						lowerCAmelCase__  :       Dict				=  feature_extractor(__lowerCamelCase	,return_tensors='''np'''				).input_features
						for enc_seq_a, enc_seq_a in zip(__lowerCamelCase	,__lowerCamelCase				):
										self.assertTrue(np.allclose(__lowerCamelCase	,__lowerCamelCase	,atol=1e-3				)				)



		def 	lowerCAmelCase__   (self				)  ->    List[str]:

						"""simple docstring"""






						lowerCAmelCase__  :       Any				=  self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
						lowerCAmelCase__  :       str				=  [floats_list((1, x)				)[0] for x in range(8_00	,14_00	,2_00				)]

						lowerCAmelCase__  :       List[Any]				=  ['''longest''', '''max_length''', '''do_not_pad''']
						lowerCAmelCase__  :       int				=  [None, 16, None]
						for max_length, padding in zip(__lowerCamelCase	,__lowerCamelCase				):
										lowerCAmelCase__  :       Optional[int]				=  feature_extractor(
										    __lowerCamelCase	,padding=__lowerCamelCase	,max_length=__lowerCamelCase	,return_attention_mask=__lowerCamelCase				)
										lowerCAmelCase__  :       Tuple				=  inputs.input_features
										lowerCAmelCase__  :       Optional[Any]				=  inputs.attention_mask
										lowerCAmelCase__  :       int				=  [np.sum(__lowerCamelCase				) 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 	lowerCAmelCase__   (self				)  ->    Union[str, Any]:

						"""simple docstring"""






						lowerCAmelCase__  :       Optional[Any]				=  self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
						lowerCAmelCase__  :       str				=  [floats_list((1, x)				)[0] for x in range(8_00	,14_00	,2_00				)]

						lowerCAmelCase__  :       str				=  ['''longest''', '''max_length''', '''do_not_pad''']
						lowerCAmelCase__  :       Optional[int]				=  [None, 16, None]
						for max_length, padding in zip(__lowerCamelCase	,__lowerCamelCase				):
										lowerCAmelCase__  :       Optional[Any]				=  feature_extractor(
										    __lowerCamelCase	,max_length=__lowerCamelCase	,padding=__lowerCamelCase	,return_tensors='''np'''	,return_attention_mask=__lowerCamelCase				)
										lowerCAmelCase__  :       List[Any]				=  inputs.input_features
										lowerCAmelCase__  :       Optional[int]				=  inputs.attention_mask
										lowerCAmelCase__  :       Optional[Any]				=  [np.sum(__lowerCamelCase				) 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 	lowerCAmelCase__   (self				)  ->    List[Any]:

						"""simple docstring"""






						lowerCAmelCase__  :       Optional[Any]				=  self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
						lowerCAmelCase__  :       Optional[int]				=  [floats_list((1, x)				)[0] for x in range(8_00	,14_00	,2_00				)]
						lowerCAmelCase__  :       Union[str, Any]				=  feature_extractor(
						    __lowerCamelCase	,padding='''max_length'''	,max_length=4	,truncation=__lowerCamelCase	,return_tensors='''np'''	,return_attention_mask=__lowerCamelCase	,)
						lowerCAmelCase__  :       Optional[Any]				=  inputs.input_features
						lowerCAmelCase__  :       Any				=  inputs.attention_mask
						lowerCAmelCase__  :       Dict				=  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 	lowerCAmelCase__   (self				)  ->    str:

						"""simple docstring"""






						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(8_00	,14_00	,2_00				)]
						lowerCAmelCase__  :       Dict				=  feature_extractor(
						    __lowerCamelCase	,padding='''longest'''	,max_length=4	,truncation=__lowerCamelCase	,return_tensors='''np'''	,return_attention_mask=__lowerCamelCase	,)
						lowerCAmelCase__  :       Optional[Any]				=  inputs.input_features
						lowerCAmelCase__  :       int				=  inputs.attention_mask
						lowerCAmelCase__  :       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, 4, 24)				)

						lowerCAmelCase__  :       List[Any]				=  [floats_list((1, x)				)[0] for x in range(8_00	,14_00	,2_00				)]
						lowerCAmelCase__  :       Any				=  feature_extractor(
						    __lowerCamelCase	,padding='''longest'''	,max_length=16	,truncation=__lowerCamelCase	,return_tensors='''np'''	,return_attention_mask=__lowerCamelCase	,)
						lowerCAmelCase__  :       Union[str, Any]				=  inputs.input_features
						lowerCAmelCase__  :       List[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, : 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 	lowerCAmelCase__   (self				)  ->    List[Any]:

						"""simple docstring"""






						import torch

						lowerCAmelCase__  :       Dict				=  self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
						lowerCAmelCase__  :       Union[str, Any]				=  np.random.rand(1_00	,32				).astype(np.floataa				)
						lowerCAmelCase__  :       Union[str, Any]				=  np_speech_inputs.tolist()

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



		def 	lowerCAmelCase__   (self	,__lowerCamelCase				)  ->    Dict:

						"""simple docstring"""






						from datasets import load_dataset

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

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



		def 	lowerCAmelCase__   (self				)  ->    Any:

						"""simple docstring"""






						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__  :       List[Any]				=  self._load_datasamples(1				)
						lowerCAmelCase__  :       Optional[Any]				=  self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
						lowerCAmelCase__  :       Optional[Any]				=  feature_extractor(__lowerCamelCase	,return_tensors='''pt'''				).input_features
						self.assertEquals(input_features.shape	,(1, 5_84, 24)				)
						self.assertTrue(np.allclose(input_features[0, 0, :30]	,__lowerCamelCase	,atol=1e-4				)				)






from typing import Dict

import numpy as np
import torch

from . import residue_constants as rc
from .tensor_utils import tensor_tree_map, tree_map






def 			lowerCAmelCase__	(					lowerCamelCase_	:	Dict[str, torch.Tensor]):





				'''simple docstring'''

				lowerCAmelCase__  :       List[Any]				=  []
				lowerCAmelCase__  :       int				=  []
				lowerCAmelCase__  :       Optional[int]				=  []

				for rt in rc.restypes:
								lowerCAmelCase__  :       int				=  rc.restype_name_to_atomaa_names[rc.restype_atoa[rt]]
								restype_atomaa_to_atomaa_list.append([(rc.atom_order[name] if name else 0) for name in atom_names])
								lowerCAmelCase__  :       Any				=  {name: i for i, name in enumerate(lowerCamelCase_)}
								restype_atomaa_to_atomaa_list.append(
								    [(atom_name_to_idxaa[name] if name in atom_name_to_idxaa else 0) for name in rc.atom_types])

								restype_atomaa_mask_list.append([(1.0 if name else 0.0) for name in atom_names])

				# Add dummy mapping for restype 'UNK'
				restype_atomaa_to_atomaa_list.append([0] * 14)
				restype_atomaa_to_atomaa_list.append([0] * 37)
				restype_atomaa_mask_list.append([0.0] * 14)

				lowerCAmelCase__  :       Optional[Any]				=  torch.tensor(
				    lowerCamelCase_ ,dtype=torch.intaa ,device=protein['''aatype'''].device ,)
				lowerCAmelCase__  :       Optional[int]				=  torch.tensor(
				    lowerCamelCase_ ,dtype=torch.intaa ,device=protein['''aatype'''].device ,)
				lowerCAmelCase__  :       List[str]				=  torch.tensor(
				    lowerCamelCase_ ,dtype=torch.floataa ,device=protein['''aatype'''].device ,)
				lowerCAmelCase__  :       Optional[Any]				=  protein['''aatype'''].to(torch.long)

				# create the mapping for (residx, atom14) --> atom37, i.e. an array
				# with shape (num_res, 14) containing the atom37 indices for this protein
				lowerCAmelCase__  :       Optional[int]				=  restype_atomaa_to_atomaa[protein_aatype]
				lowerCAmelCase__  :       List[str]				=  restype_atomaa_mask[protein_aatype]

				lowerCAmelCase__  :       Tuple				=  residx_atomaa_mask
				lowerCAmelCase__  :       Dict				=  residx_atomaa_to_atomaa.long()

				# create the gather indices for mapping back
				lowerCAmelCase__  :       Union[str, Any]				=  restype_atomaa_to_atomaa[protein_aatype]
				lowerCAmelCase__  :       int				=  residx_atomaa_to_atomaa.long()

				# create the corresponding mask
				lowerCAmelCase__  :       List[str]				=  torch.zeros([21, 37] ,dtype=torch.floataa ,device=protein['''aatype'''].device)
				for restype, restype_letter in enumerate(rc.restypes):
								lowerCAmelCase__  :       List[str]				=  rc.restype_atoa[restype_letter]
								lowerCAmelCase__  :       List[str]				=  rc.residue_atoms[restype_name]
								for atom_name in atom_names:
												lowerCAmelCase__  :       List[Any]				=  rc.atom_order[atom_name]
												lowerCAmelCase__  :       Any				=  1

				lowerCAmelCase__  :       int				=  restype_atomaa_mask[protein_aatype]
				lowerCAmelCase__  :       int				=  residx_atomaa_mask

				return protein






def 			lowerCAmelCase__	(					lowerCamelCase_	:	Dict[str, torch.Tensor]):





				'''simple docstring'''

				lowerCAmelCase__  :       Optional[Any]				=  tree_map(lambda lowerCamelCase_: torch.tensor(lowerCamelCase_ ,device=batch['''aatype'''].device) ,lowerCamelCase_ ,np.ndarray)
				lowerCAmelCase__  :       Union[str, Any]				=  tensor_tree_map(lambda lowerCamelCase_: np.array(lowerCamelCase_) ,make_atomaa_masks(lowerCamelCase_))
				return out

