





import itertools
import random
import unittest

import numpy as np

from transformers import ASTFeatureExtractor
from transformers.testing_utils import require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


_lowerCamelCase			       =      random.Random()

if is_torch_available():
       import torch




def    __UpperCAmelCase(      lowercase_ ,    lowercase_=1.0 ,    lowercase_=None ,    lowercase_=None      ):
       if rng is None:
              _lowerCamelCase							:     Any	=		global_rng

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

       return values







class 			__A       (      unittest.TestCase  ):




      """simple docstring"""







      def __init__(		self      ,							a__      ,							a__=7      ,							a__=400      ,							a__=2000      ,							a__=1      ,							a__=0.0      ,							a__=1_6000      ,							a__=True      ,							a__=True      ,							):



             """simple docstring"""





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


      def 				__snake_case			(		self):



             """simple docstring"""





             return {
                 "feature_size": self.feature_size,
                 "padding_value": self.padding_value,
                 "sampling_rate": self.sampling_rate,
                 "return_attention_mask": self.return_attention_mask,
                 "do_normalize": self.do_normalize,
             }



      def 				__snake_case			(		self      ,							a__=False      ,							a__=False):



             """simple docstring"""





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

             if equal_length:
                    _lowerCamelCase							:     Optional[Any]	=		floats_list((self.batch_size, self.max_seq_length))
             else:
                    # make sure that inputs increase in size
                    _lowerCamelCase							:     Union[str, Any]	=		[
                        _flatten(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							:     Any	=		[np.asarray(a__) for x in speech_inputs]

             return speech_inputs







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




      """simple docstring"""







      UpperCAmelCase__						=      ASTFeatureExtractor


      def 				__snake_case			(		self):



             """simple docstring"""





             _lowerCamelCase							:     Tuple	=		ASTFeatureExtractionTester(self)


      def 				__snake_case			(		self):



             """simple docstring"""





             _lowerCamelCase							:     int	=		self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
             # create three inputs of length 800, 1000, and 1200
             _lowerCamelCase							:     List[Any]	=		[floats_list((1, x))[0] for x in range(800      ,							1400      ,							200)]
             _lowerCamelCase							:     Dict	=		[np.asarray(a__) for speech_input in speech_inputs]

             # Test not batched input
             _lowerCamelCase							:     str	=		feat_extract(speech_inputs[0]      ,							return_tensors='''np''').input_values
             _lowerCamelCase							:     List[Any]	=		feat_extract(np_speech_inputs[0]      ,							return_tensors='''np''').input_values
             self.assertTrue(np.allclose(a__      ,							a__      ,							atol=1e-3))

             # Test batched
             _lowerCamelCase							:     int	=		feat_extract(a__      ,							padding=a__      ,							return_tensors='''np''').input_values
             _lowerCamelCase							:     int	=		feat_extract(a__      ,							padding=a__      ,							return_tensors='''np''').input_values
             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.
             _lowerCamelCase							:     Dict	=		[floats_list((1, x))[0] for x in (800, 800, 800)]
             _lowerCamelCase							:     List[Any]	=		np.asarray(a__)
             _lowerCamelCase							:     Any	=		feat_extract(a__      ,							return_tensors='''np''').input_values
             _lowerCamelCase							:     Any	=		feat_extract(a__      ,							return_tensors='''np''').input_values
             for enc_seq_a, enc_seq_a in zip(a__      ,							a__):
                    self.assertTrue(np.allclose(a__      ,							a__      ,							atol=1e-3))


      @require_torch
      def 				__snake_case			(		self):



             """simple docstring"""





             import torch

             _lowerCamelCase							:     int	=		self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
             _lowerCamelCase							:     List[str]	=		np.random.rand(100).astype(np.floataa)
             _lowerCamelCase							:     str	=		np_speech_inputs.tolist()

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


      def 				__snake_case			(		self      ,							a__):



             """simple docstring"""





             from datasets import load_dataset

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

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



      @require_torch
      def 				__snake_case			(		self):



             """simple docstring"""





             _lowerCamelCase							:     List[Any]	=		torch.tensor(
                 [-0.9894, -1.2776, -0.9066, -1.2776, -0.9349, -1.2609, -1.0386, -1.2776,
                  -1.1561, -1.2776, -1.2052, -1.2723, -1.2190, -1.2132, -1.2776, -1.1133,
                  -1.1953, -1.1343, -1.1584, -1.2203, -1.1770, -1.2474, -1.2381, -1.1936,
                  -0.9270, -0.8317, -0.8049, -0.7706, -0.7565, -0.7869])
             # fmt: on

             _lowerCamelCase							:     List[Any]	=		self._load_datasamples(1)
             _lowerCamelCase							:     List[Any]	=		ASTFeatureExtractor()
             _lowerCamelCase							:     Union[str, Any]	=		feature_extractor(a__      ,							return_tensors='''pt''').input_values
             self.assertEquals(input_values.shape      ,							(1, 1024, 128))
             self.assertTrue(torch.allclose(input_values[0, 0, :30]      ,							a__      ,							atol=1e-4))








def    __UpperCAmelCase(      lowercase_ ,    lowercase_ ,    lowercase_      ):
       return round(float(moles / volume      ) * nfactor      )




def    __UpperCAmelCase(      lowercase_ ,    lowercase_ ,    lowercase_      ):
       return round(float((moles * 0.0_8_2_1 * temperature) / (volume)      )      )




def    __UpperCAmelCase(      lowercase_ ,    lowercase_ ,    lowercase_      ):
       return round(float((moles * 0.0_8_2_1 * temperature) / (pressure)      )      )




def    __UpperCAmelCase(      lowercase_ ,    lowercase_ ,    lowercase_      ):
       return round(float((pressure * volume) / (0.0_8_2_1 * moles)      )      )


if __name__ == "__main__":
       import doctest

       doctest.testmod()

