
'''simple docstring'''
import unittest

import numpy as np

from transformers import MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING, TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
from transformers.pipelines import AudioClassificationPipeline, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_torchaudio,
    slow,
)

from .test_pipelines_common import ANY






@is_pipeline_test
class    UpperCAmelCase_				(	unittest.TestCase					):
 UpperCAmelCase_		    =						MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
 UpperCAmelCase_		    =						TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING





 def        snake_case__		(    self ,       lowercase_ ,       lowercase_ ,       lowercase_):
     snake_case_ :    Dict     			=						AudioClassificationPipeline(model=lowercase_ ,       feature_extractor=lowercase_)

     # test with a raw waveform
     snake_case_ :    int     			=						np.zeros((3_40_00,))
     snake_case_ :    Union[str, Any]     			=						np.zeros((1_40_00,))
     return audio_classifier, [audioa, audio]





 def        snake_case__		(    self ,       lowercase_ ,       lowercase_):
     snake_case_ :    Tuple     			=						examples
     snake_case_ :    List[str]     			=						audio_classifier(lowercase_)
     # by default a model is initialized with num_labels=2
     self.assertEqual(
         lowercase_ ,       [
             {"score": ANY(lowercase_), "label": ANY(lowercase_)},
             {"score": ANY(lowercase_), "label": ANY(lowercase_)},
         ] ,       )
     snake_case_ :    Tuple     			=						audio_classifier(lowercase_ ,       top_k=1)
     self.assertEqual(
         lowercase_ ,       [
             {"score": ANY(lowercase_), "label": ANY(lowercase_)},
         ] ,       )

     self.run_torchaudio(lowercase_)





 @require_torchaudio
 def        snake_case__		(    self ,       lowercase_):
     import datasets

     # test with a local file
     snake_case_ :    List[Any]     			=						datasets.load_dataset("hf-internal-testing/librispeech_asr_dummy" ,       "clean" ,       split="validation")
     snake_case_ :    int     			=						dataset[0]["audio"]["array"]
     snake_case_ :    Optional[int]     			=						audio_classifier(lowercase_)
     self.assertEqual(
         lowercase_ ,       [
             {"score": ANY(lowercase_), "label": ANY(lowercase_)},
             {"score": ANY(lowercase_), "label": ANY(lowercase_)},
         ] ,       )





 @require_torch
 def        snake_case__		(    self):
     snake_case_ :    Tuple     			=						"anton-l/wav2vec2-random-tiny-classifier"

     snake_case_ :    str     			=						pipeline("audio-classification" ,       model=lowercase_)

     snake_case_ :    Any     			=						np.ones((80_00,))
     snake_case_ :    Dict     			=						audio_classifier(lowercase_ ,       top_k=4)

     snake_case_ :    str     			=						[
         {"score": 0.0_842, "label": "no"},
         {"score": 0.0_838, "label": "up"},
         {"score": 0.0_837, "label": "go"},
         {"score": 0.0_834, "label": "right"},
     ]
     snake_case_ :    Optional[Any]     			=						[
         {"score": 0.0_845, "label": "stop"},
         {"score": 0.0_844, "label": "on"},
         {"score": 0.0_841, "label": "right"},
         {"score": 0.0_834, "label": "left"},
     ]
     self.assertIn(nested_simplify(lowercase_ ,       decimals=4) ,       [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2])

     snake_case_ :    List[Any]     			=						{"array": np.ones((80_00,)), "sampling_rate": audio_classifier.feature_extractor.sampling_rate}
     snake_case_ :    Optional[Any]     			=						audio_classifier(lowercase_ ,       top_k=4)
     self.assertIn(nested_simplify(lowercase_ ,       decimals=4) ,       [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2])





 @require_torch
 @slow
 def        snake_case__		(    self):
     import datasets

     snake_case_ :    Optional[Any]     			=						"superb/wav2vec2-base-superb-ks"

     snake_case_ :    Tuple     			=						pipeline("audio-classification" ,       model=lowercase_)
     snake_case_ :    List[str]     			=						datasets.load_dataset("anton-l/superb_dummy" ,       "ks" ,       split="test")

     snake_case_ :    List[Any]     			=						np.array(dataset[3]["speech"] ,       dtype=np.floataa)
     snake_case_ :    str     			=						audio_classifier(lowercase_ ,       top_k=4)
     self.assertEqual(
         nested_simplify(lowercase_ ,       decimals=3) ,       [
             {"score": 0.981, "label": "go"},
             {"score": 0.007, "label": "up"},
             {"score": 0.006, "label": "_unknown_"},
             {"score": 0.001, "label": "down"},
         ] ,       )





 @require_tf
 @unittest.skip("Audio classification is not implemented for TF")
 def        snake_case__		(    self):
     pass






'''simple docstring'''
import doctest
from collections import deque

import numpy as np






class    UpperCAmelCase_				:





 def __init__(    self):
     snake_case_ :    List[Any]     			=						[2, 1, 2, -1]
     snake_case_ :    int     			=						[1, 2, 3, 4]





 def        snake_case__		(    self):
     snake_case_ :    str     			=						len(self.first_signal)
     snake_case_ :    Any     			=						len(self.second_signal)

     snake_case_ :    List[Any]     			=						max(lowercase_ ,       lowercase_)

     # create a zero matrix of max_length x max_length
     snake_case_ :    Dict     			=						[[0] * max_length for i in range(lowercase_)]

     # fills the smaller signal with zeros to make both signals of same length
     if length_first_signal < length_second_signal:
         self.first_signal += [0] * (max_length - length_first_signal)
     elif length_first_signal > length_second_signal:
         self.second_signal += [0] * (max_length - length_second_signal)
     for i in range(lowercase_):
         snake_case_ :    List[str]     			=						deque(self.second_signal)
         rotated_signal.rotate(lowercase_)
         for j, item in enumerate(lowercase_):
             matrix[i][j] += item

        # multiply the matrix with the first signal
     snake_case_ :    Any     			=						np.matmul(np.transpose(lowercase_) ,       np.transpose(self.first_signal))

     # rounding-off to two decimal places
     return [round(lowercase_ ,       2) for i in final_signal]


if __name__ == "__main__":
  doctest.testmod()




