




'''simple docstring'''


import itertools
import os
import random
import tempfile
import unittest

import numpy as np
from datasets import load_dataset

from transformers import is_speech_available
from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_speech_available():
  from transformers import WhisperFeatureExtractor

if is_torch_available():
  import torch

UpperCAmelCase_ 		=      random.Random()


def 						SCREAMING_SNAKE_CASE       (      a_	:							List[str]				,					a_	:							Optional[int]=1.0				,					a_	:							Union[str, Any]=None				,					a_	:							Optional[int]=None			):
  if rng is None:
    __a			=					global_rng

  __a			=					[]
  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    __lowercase						(  unittest.TestCase ):







   def __init__(	self      ,							UpperCamelCase      ,							UpperCamelCase=7      ,							UpperCamelCase=400      ,							UpperCamelCase=2000      ,							UpperCamelCase=10      ,							UpperCamelCase=160      ,							UpperCamelCase=8      ,							UpperCamelCase=0.0      ,							UpperCamelCase=4000      ,							UpperCamelCase=False      ,							UpperCamelCase=True      ,							)				->    str:
     __a			=					parent
     __a			=					batch_size
     __a			=					min_seq_length
     __a			=					max_seq_length
     __a			=					(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
     __a			=					padding_value
     __a			=					sampling_rate
     __a			=					return_attention_mask
     __a			=					do_normalize
     __a			=					feature_size
     __a			=					chunk_length
     __a			=					hop_length







   def 		UpperCamelCase__			(	self      )				->    Union[str, Any]:
     return {
         "feature_size": self.feature_size,
         "hop_length": self.hop_length,
         "chunk_length": self.chunk_length,
         "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      ,							UpperCamelCase=False      ,							UpperCamelCase=False      )				->    Union[str, Any]:
     def _flatten(UpperCamelCase      ):
       return list(itertools.chain(*_A      )      )

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





@require_torch
@require_torchaudio
class    __lowercase						(  snake_case__       ,							unittest.TestCase ):
   _a							=      WhisperFeatureExtractor if is_speech_available() else None







   def 		UpperCamelCase__			(	self      )				->    List[Any]:
     __a			=					WhisperFeatureExtractionTester(self      )







   def 		UpperCamelCase__			(	self      )				->    List[Any]:
     __a			=					self.feature_extraction_class(**self.feat_extract_dict      )

     with tempfile.TemporaryDirectory() as tmpdirname:
       __a			=					feat_extract_first.save_pretrained(_A      )[0]
       check_json_file_has_correct_format(_A      )
       __a			=					self.feature_extraction_class.from_pretrained(_A      )

     __a			=					feat_extract_first.to_dict()
     __a			=					feat_extract_second.to_dict()
     __a			=					feat_extract_first.mel_filters
     __a			=					feat_extract_second.mel_filters
     self.assertTrue(np.allclose(_A      ,							_A      )      )
     self.assertEqual(_A      ,							_A      )







   def 		UpperCamelCase__			(	self      )				->    Union[str, Any]:
     __a			=					self.feature_extraction_class(**self.feat_extract_dict      )

     with tempfile.TemporaryDirectory() as tmpdirname:
       __a			=					os.path.join(_A      ,							'feat_extract.json'      )
       feat_extract_first.to_json_file(_A      )
       __a			=					self.feature_extraction_class.from_json_file(_A      )

     __a			=					feat_extract_first.to_dict()
     __a			=					feat_extract_second.to_dict()
     __a			=					feat_extract_first.mel_filters
     __a			=					feat_extract_second.mel_filters
     self.assertTrue(np.allclose(_A      ,							_A      )      )
     self.assertEqual(_A      ,							_A      )







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

     # Test feature size
     __a			=					feature_extractor(_A      ,							padding='max_length'      ,							return_tensors='np'      ).input_features
     self.assertTrue(input_features.ndim == 3      )
     self.assertTrue(input_features.shape[-1] == feature_extractor.nb_max_frames      )
     self.assertTrue(input_features.shape[-2] == feature_extractor.feature_size      )

     # Test not batched input
     __a			=					feature_extractor(speech_inputs[0]      ,							return_tensors='np'      ).input_features
     __a			=					feature_extractor(np_speech_inputs[0]      ,							return_tensors='np'      ).input_features
     self.assertTrue(np.allclose(_A      ,							_A      ,							atol=1e-3      )      )

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

     # Test 2-D numpy arrays are batched.
     __a			=					[floats_list((1, x)      )[0] for x in (800, 800, 800)]
     __a			=					np.asarray(_A      )
     __a			=					feature_extractor(_A      ,							return_tensors='np'      ).input_features
     __a			=					feature_extractor(_A      ,							return_tensors='np'      ).input_features
     for enc_seq_a, enc_seq_a in zip(_A      ,							_A      ):
       self.assertTrue(np.allclose(_A      ,							_A      ,							atol=1e-3      )      )

     # Test truncation required
     __a			=					[floats_list((1, x)      )[0] for x in range(200      ,							(feature_extractor.n_samples + 500)      ,							200      )]
     __a			=					[np.asarray(_A      ) for speech_input in speech_inputs]

     __a			=					[x[: feature_extractor.n_samples] for x in speech_inputs]
     __a			=					[np.asarray(_A      ) for speech_input in speech_inputs_truncated]

     __a			=					feature_extractor(_A      ,							return_tensors='np'      ).input_features
     __a			=					feature_extractor(_A      ,							return_tensors='np'      ).input_features
     for enc_seq_a, enc_seq_a in zip(_A      ,							_A      ):
       self.assertTrue(np.allclose(_A      ,							_A      ,							atol=1e-3      )      )







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

     __a			=					self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()      )
     __a			=					np.random.rand(100      ,							32      ).astype(np.floataa      )
     __a			=					np_speech_inputs.tolist()

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







   def 		UpperCamelCase__			(	self      ,							UpperCamelCase      )				->    int:
     __a			=					load_dataset('hf-internal-testing/librispeech_asr_dummy'      ,							'clean'      ,							split='validation'      )
     # automatic decoding with librispeech
     __a			=					ds.sort('id'      ).select(range(_A      )      )[:num_samples]['audio']

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







   def 		UpperCamelCase__			(	self      )				->    Optional[Any]:
     # fmt: off
     __a			=					torch.tensor(
         [
             0.1_193, -0.0_946, -0.1_098, -0.0_196, 0.0_225, -0.0_690, -0.1_736, 0.0_951,
             0.0_971, -0.0_817, -0.0_702, 0.0_162, 0.0_260, 0.0_017, -0.0_192, -0.1_678,
             0.0_709, -0.1_867, -0.0_655, -0.0_274, -0.0_234, -0.1_884, -0.0_516, -0.0_554,
             -0.0_274, -0.1_425, -0.1_423, 0.0_837, 0.0_377, -0.0_854
         ]      )
     # fmt: on

     __a			=					self._load_datasamples(1      )
     __a			=					WhisperFeatureExtractor()
     __a			=					feature_extractor(_A      ,							return_tensors='pt'      ).input_features
     self.assertEqual(input_features.shape      ,							(1, 80, 3000)      )
     self.assertTrue(torch.allclose(input_features[0, 0, :30]      ,							_A      ,							atol=1e-4      )      )







   def 		UpperCamelCase__			(	self      )				->    Tuple:
     __a			=					self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()      )
     __a			=					self._load_datasamples(1      )[0]
     __a			=					((audio - audio.min()) / (audio.max() - audio.min())) * 6_5535  # Rescale to [0, 65535] to show issue
     __a			=					feat_extract.zero_mean_unit_var_norm([audio]      ,							attention_mask=_A      )[0]

     self.assertTrue(np.all(np.mean(_A      ) < 1e-3      )      )
     self.assertTrue(np.all(np.abs(np.var(_A      ) - 1      ) < 1e-3      )      )









'''simple docstring'''


def 						SCREAMING_SNAKE_CASE       (      a_	:							int = 10**12			):
  __a			=					1
  __a			=					0

  __a			=					1
  __a			=					1

  while numerator <= 2 * min_total - 1:
    prev_numerator += 2 * numerator
    numerator += 2 * prev_numerator

    prev_denominator += 2 * denominator
    denominator += 2 * prev_denominator

  return (denominator + 1) // 2


if __name__ == "__main__":
  print(f"""{solution() = }""")



