

'''simple docstring'''
from typing import List, Optional, Union

import numpy as np
import torch
import torchaudio.compliance.kaldi as ta_kaldi

from ...feature_extraction_sequence_utils import SequenceFeatureExtractor
from ...feature_extraction_utils import BatchFeature
from ...utils import PaddingStrategy, TensorType, logging


lowerCamelCase :      Union[str, Any]					 =     logging.get_logger(__name__)







class 							A__    (       A__  ):
  A__		 =				['input_features', 'attention_mask']





  def __init__(		self      :	Any							,					_a      :	List[str]=80							,					_a      :	List[Any]=1_6000							,					_a      :	Any=80							,					_a      :	Dict=0.0							,					_a      :	Optional[Any]=True							,					_a      :	Tuple=True							,					_a      :	Any=True							,					**_a      :	List[Any]							,					)	->    Optional[int]:





       '''simple docstring'''






       super().__init__(feature_size=_a							,					sampling_rate=_a							,					padding_value=_a							,					**_a )
       _SCREAMING_SNAKE_CASE							    =num_mel_bins
       _SCREAMING_SNAKE_CASE							    =do_ceptral_normalize
       _SCREAMING_SNAKE_CASE							    =normalize_means
       _SCREAMING_SNAKE_CASE							    =normalize_vars
       _SCREAMING_SNAKE_CASE							    =True





  def 						A  (		self      :	Optional[Any]							,					_a      :	np.ndarray							,					)	->    np.ndarray:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =waveform * (2**15)  # Kaldi compliance: 16-bit signed integers
       _SCREAMING_SNAKE_CASE							    =torch.from_numpy(_a ).unsqueeze(0 )
       _SCREAMING_SNAKE_CASE							    =ta_kaldi.fbank(_a							,					num_mel_bins=self.num_mel_bins							,					sample_frequency=self.sampling_rate )
       return features.numpy()





  @staticmethod
  def 						A  (		_a      :	np.ndarray							,					_a      :	int							,					_a      :	Optional[bool] = True							,					_a      :	Optional[bool] = True							,					_a      :	float = 0.0							,					)	->    np.ndarray:





       '''simple docstring'''






       if normalize_means:
            _SCREAMING_SNAKE_CASE							    =x[:input_length].mean(axis=0 )
            _SCREAMING_SNAKE_CASE							    =np.subtract(_a							,					_a )
       if normalize_vars:
            _SCREAMING_SNAKE_CASE							    =x[:input_length].std(axis=0 )
            _SCREAMING_SNAKE_CASE							    =np.divide(_a							,					_a )

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

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

       return x





  def 						A  (		self      :	int							,					_a      :	List[np.ndarray]							,					_a      :	Optional[np.ndarray] = None )	->    List[np.ndarray]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =attention_mask.sum(-1 ) if attention_mask is not None else [x.shape[0] for x in input_features]
       return [
           self.utterance_cmvn(_a							,					_a							,					self.normalize_means							,					self.normalize_vars							,					self.padding_value )
           for x, n in zip(_a							,					_a )
       ]





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





       '''simple docstring'''






       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.' )

       _SCREAMING_SNAKE_CASE							    =isinstance(_a							,					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}" )
       _SCREAMING_SNAKE_CASE							    =is_batched_numpy or (
           isinstance(_a							,					(list, tuple) ) and (isinstance(raw_speech[0]							,					(np.ndarray, tuple, list) ))
       )

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

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

       # extract fbank features
       _SCREAMING_SNAKE_CASE							    =[self._extract_fbank_features(_a ) for waveform in raw_speech]

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

       _SCREAMING_SNAKE_CASE							    =self.pad(
           _a							,					padding=_a							,					max_length=_a							,					truncation=_a							,					pad_to_multiple_of=_a							,					return_attention_mask=_a							,					**_a							,					)

       # make sure list is in array format
       _SCREAMING_SNAKE_CASE							    =padded_inputs.get('input_features' )
       if isinstance(input_features[0]							,					_a ):
            _SCREAMING_SNAKE_CASE							    =[np.asarray(_a							,					dtype=np.floataa ) for feature in input_features]

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

       # Utterance-level cepstral mean and variance normalization
       if self.do_ceptral_normalize:
            _SCREAMING_SNAKE_CASE							    =(
                np.array(_a							,					dtype=np.intaa )
                if self._get_padding_strategies(_a							,					max_length=_a ) is not PaddingStrategy.DO_NOT_PAD
                else None
            )
            _SCREAMING_SNAKE_CASE							    =self.normalize(
                padded_inputs['input_features']							,					attention_mask=_a )

       if return_tensors is not None:
            _SCREAMING_SNAKE_CASE							    =padded_inputs.convert_to_tensors(_a )

       return padded_inputs



'''simple docstring'''
import unittest

import numpy as np
import requests

from transformers.testing_utils import 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

     from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11
else:
     lowerCamelCase :      int					 =     False

if is_vision_available():
     from PIL import Image

     from transformers import PixaStructImageProcessor







class 							A__    (       unittest.TestCase  ):





  def __init__(		self      :	List[str]							,					_a      :	List[Any]							,					_a      :	List[str]=7							,					_a      :	List[str]=3							,					_a      :	Tuple=18							,					_a      :	Tuple=30							,					_a      :	str=400							,					_a      :	Tuple=None							,					_a      :	Union[str, Any]=True							,					_a      :	List[str]=True							,					_a      :	Optional[int]=None							,					)	->    List[str]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =size if size is not None else {'height': 20, 'width': 20}
       _SCREAMING_SNAKE_CASE							    =parent
       _SCREAMING_SNAKE_CASE							    =batch_size
       _SCREAMING_SNAKE_CASE							    =num_channels
       _SCREAMING_SNAKE_CASE							    =image_size
       _SCREAMING_SNAKE_CASE							    =min_resolution
       _SCREAMING_SNAKE_CASE							    =max_resolution
       _SCREAMING_SNAKE_CASE							    =size
       _SCREAMING_SNAKE_CASE							    =do_normalize
       _SCREAMING_SNAKE_CASE							    =do_convert_rgb
       _SCREAMING_SNAKE_CASE							    =[512, 1024, 2048, 4096]
       _SCREAMING_SNAKE_CASE							    =patch_size if patch_size is not None else {'height': 16, 'width': 16}





  def 						A  (		self      :	Any )	->    List[str]:





       '''simple docstring'''






       return {"do_normalize": self.do_normalize, "do_convert_rgb": self.do_convert_rgb}





  def 						A  (		self      :	int )	->    str:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    ='https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/australia.jpg'
       _SCREAMING_SNAKE_CASE							    =Image.open(requests.get(_a							,					stream=_a ).raw ).convert('RGB' )
       return raw_image







@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11     ,   reason='`Pix2StructImageProcessor` requires `torch>=1.11.0`.'     ,   )
@require_torch
@require_vision
class 							A__    (       A__     ,   unittest.TestCase  ):
  A__		 =				PixaStructImageProcessor if is_vision_available() else None





  def 						A  (		self      :	Dict )	->    Tuple:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =PixaStructImageProcessingTester(self )





  @property
  def 						A  (		self      :	Optional[Any] )	->    int:





       '''simple docstring'''






       return self.image_processor_tester.prepare_image_processor_dict()





  def 						A  (		self      :	Any )	->    Tuple:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =self.image_processing_class(**self.image_processor_dict )
       self.assertTrue(hasattr(_a							,					'do_normalize' ) )
       self.assertTrue(hasattr(_a							,					'do_convert_rgb' ) )





  def 						A  (		self      :	Any )	->    List[str]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =self.image_processor_tester.prepare_dummy_image()

       _SCREAMING_SNAKE_CASE							    =self.image_processing_class(**self.image_processor_dict )
       _SCREAMING_SNAKE_CASE							    =2048

       _SCREAMING_SNAKE_CASE							    =image_processor(_a							,					return_tensors='pt'							,					max_patches=_a )
       self.assertTrue(torch.allclose(inputs.flattened_patches.mean()							,					torch.tensor(0.06_06 )							,					atol=1e-3							,					rtol=1e-3 ) )





  def 						A  (		self      :	Any )	->    Any:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =self.image_processing_class(**self.image_processor_dict )
       # create random PIL images
       _SCREAMING_SNAKE_CASE							    =prepare_image_inputs(self.image_processor_tester							,					equal_resolution=_a )
       for image in image_inputs:
            self.assertIsInstance(_a							,					Image.Image )

       # Test not batched input
       _SCREAMING_SNAKE_CASE							    =(
           (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
           * self.image_processor_tester.num_channels
       ) + 2

       for max_patch in self.image_processor_tester.max_patches:
            # Test not batched input
            _SCREAMING_SNAKE_CASE							    =image_processor(
                image_inputs[0]							,					return_tensors='pt'							,					max_patches=_a ).flattened_patches
            self.assertEqual(
                encoded_images.shape							,					(1, max_patch, expected_hidden_dim)							,					)

            # Test batched
            _SCREAMING_SNAKE_CASE							    =image_processor(
                _a							,					return_tensors='pt'							,					max_patches=_a ).flattened_patches
            self.assertEqual(
                encoded_images.shape							,					(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)							,					)





  def 						A  (		self      :	List[str] )	->    Optional[Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =self.image_processing_class(**self.image_processor_dict )
       # create random PIL images
       _SCREAMING_SNAKE_CASE							    =prepare_image_inputs(self.image_processor_tester							,					equal_resolution=_a )
       for image in image_inputs:
            self.assertIsInstance(_a							,					Image.Image )

       # Test not batched input
       _SCREAMING_SNAKE_CASE							    =(
           (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
           * self.image_processor_tester.num_channels
       ) + 2

       _SCREAMING_SNAKE_CASE							    =True

       for max_patch in self.image_processor_tester.max_patches:
            # Test not batched input
            with self.assertRaises(_a ):
                 _SCREAMING_SNAKE_CASE							    =image_processor(
                     image_inputs[0]							,					return_tensors='pt'							,					max_patches=_a ).flattened_patches

            _SCREAMING_SNAKE_CASE							    ='Hello'

            _SCREAMING_SNAKE_CASE							    =image_processor(
                image_inputs[0]							,					return_tensors='pt'							,					max_patches=_a							,					header_text=_a ).flattened_patches
            self.assertEqual(
                encoded_images.shape							,					(1, max_patch, expected_hidden_dim)							,					)

            # Test batched
            _SCREAMING_SNAKE_CASE							    =image_processor(
                _a							,					return_tensors='pt'							,					max_patches=_a							,					header_text=_a ).flattened_patches
            self.assertEqual(
                encoded_images.shape							,					(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)							,					)





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





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =self.image_processing_class(**self.image_processor_dict )
       # create random numpy tensors
       _SCREAMING_SNAKE_CASE							    =prepare_image_inputs(self.image_processor_tester							,					equal_resolution=_a							,					numpify=_a )
       for image in image_inputs:
            self.assertIsInstance(_a							,					np.ndarray )

       _SCREAMING_SNAKE_CASE							    =(
           (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
           * self.image_processor_tester.num_channels
       ) + 2

       for max_patch in self.image_processor_tester.max_patches:
            # Test not batched input
            _SCREAMING_SNAKE_CASE							    =image_processor(
                image_inputs[0]							,					return_tensors='pt'							,					max_patches=_a ).flattened_patches
            self.assertEqual(
                encoded_images.shape							,					(1, max_patch, expected_hidden_dim)							,					)

            # Test batched
            _SCREAMING_SNAKE_CASE							    =image_processor(
                _a							,					return_tensors='pt'							,					max_patches=_a ).flattened_patches
            self.assertEqual(
                encoded_images.shape							,					(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)							,					)





  def 						A  (		self      :	Union[str, Any] )	->    Tuple:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =self.image_processing_class(**self.image_processor_dict )
       # create random PyTorch tensors
       _SCREAMING_SNAKE_CASE							    =prepare_image_inputs(self.image_processor_tester							,					equal_resolution=_a							,					torchify=_a )
       for image in image_inputs:
            self.assertIsInstance(_a							,					torch.Tensor )

       # Test not batched input
       _SCREAMING_SNAKE_CASE							    =(
           (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
           * self.image_processor_tester.num_channels
       ) + 2

       for max_patch in self.image_processor_tester.max_patches:
            # Test not batched input
            _SCREAMING_SNAKE_CASE							    =image_processor(
                image_inputs[0]							,					return_tensors='pt'							,					max_patches=_a ).flattened_patches
            self.assertEqual(
                encoded_images.shape							,					(1, max_patch, expected_hidden_dim)							,					)

            # Test batched
            _SCREAMING_SNAKE_CASE							    =image_processor(
                _a							,					return_tensors='pt'							,					max_patches=_a ).flattened_patches
            self.assertEqual(
                encoded_images.shape							,					(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)							,					)







@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11     ,   reason='`Pix2StructImageProcessor` requires `torch>=1.11.0`.'     ,   )
@require_torch
@require_vision
class 							A__    (       A__     ,   unittest.TestCase  ):
  A__		 =				PixaStructImageProcessor if is_vision_available() else None





  def 						A  (		self      :	str )	->    Dict:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =PixaStructImageProcessingTester(self							,					num_channels=4 )
       _SCREAMING_SNAKE_CASE							    =3





  @property
  def 						A  (		self      :	List[str] )	->    Optional[Any]:





       '''simple docstring'''






       return self.image_processor_tester.prepare_image_processor_dict()





  def 						A  (		self      :	List[str] )	->    Tuple:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =self.image_processing_class(**self.image_processor_dict )
       self.assertTrue(hasattr(_a							,					'do_normalize' ) )
       self.assertTrue(hasattr(_a							,					'do_convert_rgb' ) )





  def 						A  (		self      :	Dict )	->    int:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =self.image_processing_class(**self.image_processor_dict )
       # create random PIL images
       _SCREAMING_SNAKE_CASE							    =prepare_image_inputs(self.image_processor_tester							,					equal_resolution=_a )
       for image in image_inputs:
            self.assertIsInstance(_a							,					Image.Image )

       # Test not batched input
       _SCREAMING_SNAKE_CASE							    =(
           (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
           * (self.image_processor_tester.num_channels - 1)
       ) + 2

       for max_patch in self.image_processor_tester.max_patches:
            # Test not batched input
            _SCREAMING_SNAKE_CASE							    =image_processor(
                image_inputs[0]							,					return_tensors='pt'							,					max_patches=_a ).flattened_patches
            self.assertEqual(
                encoded_images.shape							,					(1, max_patch, expected_hidden_dim)							,					)

            # Test batched
            _SCREAMING_SNAKE_CASE							    =image_processor(
                _a							,					return_tensors='pt'							,					max_patches=_a ).flattened_patches
            self.assertEqual(
                encoded_images.shape							,					(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)							,					)
