




"""simple docstring"""

from typing import List, Optional, Union

import numpy as np

from ....audio_utils import mel_filter_bank, optimal_fft_length, spectrogram, window_function
from ....feature_extraction_sequence_utils import SequenceFeatureExtractor
from ....feature_extraction_utils import BatchFeature
from ....file_utils import PaddingStrategy, TensorType
from ....utils import logging


a							      =     logging.get_logger(__name__)





class 							lowercase_			(	__lowerCAmelCase				):



 '''simple docstring'''
 UpperCAmelCase       :	Tuple     =      ['''input_features''', '''attention_mask''']

 def __init__(					self			:			Optional[Any]       ,				_UpperCAmelCase			:			int=80       ,				_UpperCAmelCase			:			Any=16_000       ,				_UpperCAmelCase			:			Dict=0.0       ,				_UpperCAmelCase			:			Optional[Any]=10       ,				_UpperCAmelCase			:			Optional[int]=25       ,				_UpperCAmelCase			:			List[str]="hamming_window"       ,				_UpperCAmelCase			:			Dict=3_2768.0       ,				_UpperCAmelCase			:			int=0.97       ,				_UpperCAmelCase			:			List[str]=1.0       ,				_UpperCAmelCase			:			Tuple=True       ,				_UpperCAmelCase			:			Union[str, Any]=True       ,				_UpperCAmelCase			:			int=False       ,				**_UpperCAmelCase			:			List[Any]       ,				):
       super().__init__(feature_size=_UpperCAmelCase       ,				sampling_rate=_UpperCAmelCase       ,				padding_value=_UpperCAmelCase       ,				**_UpperCAmelCase						)

       _A     =  feature_size
       _A     =  sampling_rate
       _A     =  padding_value
       _A     =  hop_length
       _A     =  win_length
       _A     =  frame_signal_scale
       _A     =  preemphasis_coeff
       _A     =  mel_floor
       _A     =  normalize_means
       _A     =  normalize_vars
       _A     =  win_function
       _A     =  return_attention_mask

       _A     =  win_length * sampling_rate // 1_000
       _A     =  hop_length * sampling_rate // 1_000

       _A     =  optimal_fft_length(self.sample_size						)
       _A     =  (self.n_fft // 2) + 1

 def      lowerCAmelCase_				(					self			:			List[Any]       ,				_UpperCAmelCase			:			np.array						):
       if self.win_function == "hamming_window":
             _A     =  window_function(window_length=self.sample_size       ,				name=self.win_function       ,				periodic=_UpperCAmelCase						)
       else:
             _A     =  window_function(window_length=self.sample_size       ,				name=self.win_function						)

       _A     =  mel_filter_bank(
           num_frequency_bins=self.n_freqs       ,				num_mel_filters=self.feature_size       ,				min_frequency=0.0       ,				max_frequency=self.sampling_rate / 2.0       ,				sampling_rate=self.sampling_rate       ,				)

       _A     =  spectrogram(
           one_waveform * self.frame_signal_scale       ,				window=_UpperCAmelCase       ,				frame_length=self.sample_size       ,				hop_length=self.sample_stride       ,				fft_length=self.n_fft       ,				center=_UpperCAmelCase       ,				preemphasis=self.preemphasis_coeff       ,				mel_filters=_UpperCAmelCase       ,				mel_floor=self.mel_floor       ,				log_mel='log'       ,				)
       return msfc_features.T

 def      lowerCAmelCase_				(					self			:			Optional[int]       ,				_UpperCAmelCase			:			Dict       ,				_UpperCAmelCase			:			List[str]       ,				_UpperCAmelCase			:			List[Any]						):
       # make sure we normalize float32 arrays
       if self.normalize_means:
             _A     =  x[:input_length].mean(axis=0						)
             _A     =  np.subtract(_UpperCAmelCase       ,				_UpperCAmelCase						)
       if self.normalize_vars:
             _A     =  x[:input_length].std(axis=0						)
             _A     =  np.divide(_UpperCAmelCase       ,				_UpperCAmelCase						)

       if input_length < x.shape[0]:
             _A     =  padding_value

       # make sure array is in float32
       _A     =  x.astype(np.floataa						)

       return x

 def      lowerCAmelCase_				(					self			:			Dict       ,				_UpperCAmelCase			:			List[np.ndarray]       ,				_UpperCAmelCase			:			Optional[np.ndarray] = None						):
       _A     =  attention_mask.sum(-1						) if attention_mask is not None else [x.shape[0] for x in input_features]
       return [self._normalize_one(_UpperCAmelCase       ,				_UpperCAmelCase       ,				self.padding_value						) for x, n in zip(_UpperCAmelCase       ,				_UpperCAmelCase						)]



 def __call__(					self			:			Dict       ,				_UpperCAmelCase			:			Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]       ,				_UpperCAmelCase			:			Union[bool, str, PaddingStrategy] = False       ,				_UpperCAmelCase			:			Optional[int] = None       ,				_UpperCAmelCase			:			bool = False       ,				_UpperCAmelCase			:			Optional[int] = None       ,				_UpperCAmelCase			:			Optional[bool] = None       ,				_UpperCAmelCase			:			Optional[Union[str, TensorType]] = None       ,				_UpperCAmelCase			:			Optional[int] = None       ,				**_UpperCAmelCase			:			Optional[int]       ,				):

       if sampling_rate is not None:
             if sampling_rate != self.sampling_rate:
                   raise ValueError(
                       F'''The model corresponding to this feature extractor: {self} was trained using a sampling rate of'''
                       F''' {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled with'''
                       F''' {self.sampling_rate} and not {sampling_rate}.'''						)
       else:
             logger.warning(
                 'It is strongly recommended to pass the ``sampling_rate`` argument to this function. '
                 'Failing to do so can result in silent errors that might be hard to debug.'						)

       _A     =  isinstance(_UpperCAmelCase       ,				np.ndarray						) and len(raw_speech.shape						) > 1
       if is_batched_numpy and len(raw_speech.shape						) > 2:
             raise ValueError(F'''Only mono-channel audio is supported for input to {self}'''						)
       _A     =  is_batched_numpy or (
           isinstance(_UpperCAmelCase       ,				(list, tuple)						) and (isinstance(raw_speech[0]       ,				(np.ndarray, tuple, list)						))
       )

       if is_batched:
             _A     =  [np.asarray(_UpperCAmelCase       ,				dtype=np.floataa						) for speech in raw_speech]
       elif not is_batched and not isinstance(_UpperCAmelCase       ,				np.ndarray						):
             _A     =  np.asarray(_UpperCAmelCase       ,				dtype=np.floataa						)
       elif isinstance(_UpperCAmelCase       ,				np.ndarray						) and raw_speech.dtype is np.dtype(np.floataa						):
             _A     =  raw_speech.astype(np.floataa						)

       # always return batch
       if not is_batched:
             _A     =  [raw_speech]

       # extract fbank features
       _A     =  [self._extract_mfsc_features(_UpperCAmelCase						) for one_waveform in raw_speech]

       # convert into correct format for padding
       _A     =  BatchFeature({'input_features': features}						)

       _A     =  self.pad(
           _UpperCAmelCase       ,				padding=_UpperCAmelCase       ,				max_length=_UpperCAmelCase       ,				truncation=_UpperCAmelCase       ,				pad_to_multiple_of=_UpperCAmelCase       ,				return_attention_mask=_UpperCAmelCase       ,				**_UpperCAmelCase       ,				)
       # make sure list is in array format
       _A     =  padded_inputs.get('input_features'						)
       if isinstance(input_features[0]       ,				_UpperCAmelCase						):
             _A     =  [np.asarray(_UpperCAmelCase       ,				dtype=np.floataa						) for feature in input_features]

       _A     =  padded_inputs.get('attention_mask'						)
       if attention_mask is not None:
             _A     =  [np.asarray(_UpperCAmelCase       ,				dtype=np.intaa						) for array in attention_mask]

       if self.normalize_means or self.normalize_vars:
             _A     =  (
                 np.array(_UpperCAmelCase       ,				dtype=np.intaa						)
                 if self._get_padding_strategies(_UpperCAmelCase       ,				max_length=_UpperCAmelCase						) is not PaddingStrategy.DO_NOT_PAD
                 and padding
                 else None
             )
             _A     =  self.normalize(
                 padded_inputs['input_features']       ,				attention_mask=_UpperCAmelCase						)

       if return_tensors is not None:
             _A     =  padded_inputs.convert_to_tensors(_UpperCAmelCase						)

       return padded_inputs








import unittest

import numpy as np

from transformers.testing_utils import is_flaky, require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
    import torch

if is_vision_available():
    from PIL import Image

    from transformers import DonutImageProcessor

class   lowerCAmelCase				(   unittest.TestCase     ):
 def __init__( self    : Any     ,	UpperCAmelCase    : Optional[int]     ,	UpperCAmelCase    : Dict=7     ,	UpperCAmelCase    : Union[str, Any]=3     ,	UpperCAmelCase    : List[str]=18     ,	UpperCAmelCase    : Dict=30     ,	UpperCAmelCase    : List[Any]=400     ,	UpperCAmelCase    : Optional[Any]=True     ,	UpperCAmelCase    : Tuple=None     ,	UpperCAmelCase    : str=True     ,	UpperCAmelCase    : Tuple=False     ,	UpperCAmelCase    : List[str]=True     ,	UpperCAmelCase    : Optional[int]=True     ,	UpperCAmelCase    : Optional[Any]=[0.5, 0.5, 0.5]     ,	UpperCAmelCase    : List[str]=[0.5, 0.5, 0.5]     ,	)     ->      List[str]:
     lowerCamelCase__       : List[Any]   					=						parent
     lowerCamelCase__       : str   					=						batch_size
     lowerCamelCase__       : List[str]   					=						num_channels
     lowerCamelCase__       : str   					=						image_size
     lowerCamelCase__       : List[Any]   					=						min_resolution
     lowerCamelCase__       : int   					=						max_resolution
     lowerCamelCase__       : int   					=						do_resize
     lowerCamelCase__       : int   					=						size if size is not None else {'height': 18, 'width': 20}
     lowerCamelCase__       : Tuple   					=						do_thumbnail
     lowerCamelCase__       : str   					=						do_align_axis
     lowerCamelCase__       : str   					=						do_pad
     lowerCamelCase__       : Optional[Any]   					=						do_normalize
     lowerCamelCase__       : List[str]   					=						image_mean
     lowerCamelCase__       : Dict   					=						image_std







 def 	A_      ( self    : str      )     ->      Any:
     return {
         "do_resize": self.do_resize,
         "size": self.size,
         "do_thumbnail": self.do_thumbnail,
         "do_align_long_axis": self.do_align_axis,
         "do_pad": self.do_pad,
         "do_normalize": self.do_normalize,
         "image_mean": self.image_mean,
         "image_std": self.image_std,
     }


@require_torch
@require_vision
class   lowerCAmelCase				(   __UpperCamelCase,    unittest.TestCase     ):
 UpperCAmelCase__						=						DonutImageProcessor if is_vision_available() else None
 def 	A_      ( self    : List[Any]      )     ->      int:
     lowerCamelCase__       : Union[str, Any]   					=						DonutImageProcessingTester(self      )
 @property
 def 	A_      ( self    : Dict      )     ->      Union[str, Any]:
     return self.image_processor_tester.prepare_image_processor_dict()
 def 	A_      ( self    : Dict      )     ->      Any:
     lowerCamelCase__       : Tuple   					=						self.image_processing_class(**self.image_processor_dict      )
     self.assertTrue(hasattr(UpperCAmelCase     ,	'do_resize'      )      )
     self.assertTrue(hasattr(UpperCAmelCase     ,	'size'      )      )
     self.assertTrue(hasattr(UpperCAmelCase     ,	'do_thumbnail'      )      )
     self.assertTrue(hasattr(UpperCAmelCase     ,	'do_align_long_axis'      )      )
     self.assertTrue(hasattr(UpperCAmelCase     ,	'do_pad'      )      )
     self.assertTrue(hasattr(UpperCAmelCase     ,	'do_normalize'      )      )
     self.assertTrue(hasattr(UpperCAmelCase     ,	'image_mean'      )      )
     self.assertTrue(hasattr(UpperCAmelCase     ,	'image_std'      )      )
 def 	A_      ( self    : Tuple      )     ->      Union[str, Any]:
     lowerCamelCase__       : List[Any]   					=						self.image_processing_class.from_dict(self.image_processor_dict      )
     self.assertEqual(image_processor.size     ,	{'height': 18, 'width': 20}      )

     lowerCamelCase__       : int   					=						self.image_processing_class.from_dict(self.image_processor_dict     ,	size=42      )
     self.assertEqual(image_processor.size     ,	{'height': 42, 'width': 42}      )

     # Previous config had dimensions in (width, height) order
     lowerCamelCase__       : Dict   					=						self.image_processing_class.from_dict(self.image_processor_dict     ,	size=(42, 84)      )
     self.assertEqual(image_processor.size     ,	{'height': 84, 'width': 42}      )
 def 	A_      ( self    : Optional[Any]      )     ->      List[str]:
     pass
 @is_flaky()
 def 	A_      ( self    : List[str]      )     ->      Any:
     # Initialize image_processing
     lowerCamelCase__       : str   					=						self.image_processing_class(**self.image_processor_dict      )
     # create random PIL images
     lowerCamelCase__       : List[str]   					=						prepare_image_inputs(self.image_processor_tester     ,	equal_resolution=UpperCAmelCase      )
     for image in image_inputs:
         self.assertIsInstance(UpperCAmelCase     ,	Image.Image      )

     # Test not batched input
     lowerCamelCase__       : List[Any]   					=						image_processing(image_inputs[0]     ,	return_tensors='pt'      ).pixel_values
     self.assertEqual(
         encoded_images.shape     ,	(
             1,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.size['height'],
             self.image_processor_tester.size['width'],
         )     ,	)

     # Test batched
     lowerCamelCase__       : Tuple   					=						image_processing(UpperCAmelCase     ,	return_tensors='pt'      ).pixel_values
     self.assertEqual(
         encoded_images.shape     ,	(
             self.image_processor_tester.batch_size,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.size['height'],
             self.image_processor_tester.size['width'],
         )     ,	)
 @is_flaky()
 def 	A_      ( self    : int      )     ->      Tuple:
     # Initialize image_processing
     lowerCamelCase__       : Tuple   					=						self.image_processing_class(**self.image_processor_dict      )
     # create random numpy tensors
     lowerCamelCase__       : List[str]   					=						prepare_image_inputs(self.image_processor_tester     ,	equal_resolution=UpperCAmelCase     ,	numpify=UpperCAmelCase      )
     for image in image_inputs:
         self.assertIsInstance(UpperCAmelCase     ,	np.ndarray      )

     # Test not batched input
     lowerCamelCase__       : Optional[Any]   					=						image_processing(image_inputs[0]     ,	return_tensors='pt'      ).pixel_values
     self.assertEqual(
         encoded_images.shape     ,	(
             1,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.size['height'],
             self.image_processor_tester.size['width'],
         )     ,	)

     # Test batched
     lowerCamelCase__       : Tuple   					=						image_processing(UpperCAmelCase     ,	return_tensors='pt'      ).pixel_values
     self.assertEqual(
         encoded_images.shape     ,	(
             self.image_processor_tester.batch_size,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.size['height'],
             self.image_processor_tester.size['width'],
         )     ,	)







 @is_flaky()
 def 	A_      ( self    : Any      )     ->      Tuple:
     # Initialize image_processing
     lowerCamelCase__       : int   					=						self.image_processing_class(**self.image_processor_dict      )
     # create random PyTorch tensors
     lowerCamelCase__       : Tuple   					=						prepare_image_inputs(self.image_processor_tester     ,	equal_resolution=UpperCAmelCase     ,	torchify=UpperCAmelCase      )
     for image in image_inputs:
         self.assertIsInstance(UpperCAmelCase     ,	torch.Tensor      )

     # Test not batched input
     lowerCamelCase__       : List[str]   					=						image_processing(image_inputs[0]     ,	return_tensors='pt'      ).pixel_values
     self.assertEqual(
         encoded_images.shape     ,	(
             1,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.size['height'],
             self.image_processor_tester.size['width'],
         )     ,	)

     # Test batched
     lowerCamelCase__       : Dict   					=						image_processing(UpperCAmelCase     ,	return_tensors='pt'      ).pixel_values
     self.assertEqual(
         encoded_images.shape     ,	(
             self.image_processor_tester.batch_size,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.size['height'],
             self.image_processor_tester.size['width'],
         )     ,	)





