






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


UpperCamelCase      					=logging.get_logger(__name__)




class      A     (   SCREAMING_SNAKE_CASE__    ):






 """simple docstring"""





 __a      :    List[Any]			  =    ['''input_features''', '''attention_mask''']
 def __init__(				self      ,  __lowerCAmelCase=80      ,  __lowerCAmelCase=1_60_00      ,  __lowerCAmelCase=80      ,  __lowerCAmelCase=0.0      ,  __lowerCAmelCase=True      ,  __lowerCAmelCase=True      ,  __lowerCAmelCase=True      ,  **__lowerCAmelCase      ,  ):
       super().__init__(feature_size=__lowerCAmelCase      ,  sampling_rate=__lowerCAmelCase      ,  padding_value=__lowerCAmelCase      ,  **__lowerCAmelCase			)
       UpperCamelCase_					: int            =				num_mel_bins
       UpperCamelCase_					: Union[str, Any]            =				do_ceptral_normalize
       UpperCamelCase_					: Any            =				normalize_means
       UpperCamelCase_					: int            =				normalize_vars
       UpperCamelCase_					: int            =				True
 def 	_UpperCAmelCase							(				self      ,  __lowerCAmelCase      ,  ):
       UpperCamelCase_					: Optional[Any]            =				waveform * (2**15)  # Kaldi compliance: 16-bit signed integers
       UpperCamelCase_					: Optional[int]            =				torch.from_numpy(__lowerCAmelCase			).unsqueeze(0			)
       UpperCamelCase_					: Tuple            =				ta_kaldi.fbank(__lowerCAmelCase      ,  num_mel_bins=self.num_mel_bins      ,  sample_frequency=self.sampling_rate			)
       return features.numpy()
 @staticmethod
 def 	_UpperCAmelCase							(				__lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase = True      ,  __lowerCAmelCase = True      ,  __lowerCAmelCase = 0.0      ,  ):
       # make sure we normalize float32 arrays
       if normalize_means:
             UpperCamelCase_					: int            =				x[:input_length].mean(axis=0			)
             UpperCamelCase_					: str            =				np.subtract(__lowerCAmelCase      ,  __lowerCAmelCase			)
       if normalize_vars:
             UpperCamelCase_					: Optional[int]            =				x[:input_length].std(axis=0			)
             UpperCamelCase_					: Optional[Any]            =				np.divide(__lowerCAmelCase      ,  __lowerCAmelCase			)

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

       # make sure array is in float32
       UpperCamelCase_					: str            =				x.astype(np.floataa			)

       return x
 def 	_UpperCAmelCase							(				self      ,  __lowerCAmelCase      ,  __lowerCAmelCase = None			):
       UpperCamelCase_					: List[str]            =				attention_mask.sum(-1			) if attention_mask is not None else [x.shape[0] for x in input_features]
       return [
           self.utterance_cmvn(__lowerCAmelCase      ,  __lowerCAmelCase      ,  self.normalize_means      ,  self.normalize_vars      ,  self.padding_value			)
           for x, n in zip(__lowerCAmelCase      ,  __lowerCAmelCase			)
       ]





 def __call__(				self      ,  __lowerCAmelCase      ,  __lowerCAmelCase = False      ,  __lowerCAmelCase = None      ,  __lowerCAmelCase = False      ,  __lowerCAmelCase = None      ,  __lowerCAmelCase = None      ,  __lowerCAmelCase = None      ,  __lowerCAmelCase = None      ,  **__lowerCAmelCase      ,  ):

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

       UpperCamelCase_					: Optional[int]            =				isinstance(__lowerCAmelCase      ,  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}"			)
       UpperCamelCase_					: List[str]            =				is_batched_numpy or (
           isinstance(__lowerCAmelCase      ,  (list, tuple)			) and (isinstance(raw_speech[0]      ,  (np.ndarray, tuple, list)			))
       )

       if is_batched:
             UpperCamelCase_					: str            =				[np.asarray(__lowerCAmelCase      ,  dtype=np.floataa			) for speech in raw_speech]
       elif not is_batched and not isinstance(__lowerCAmelCase      ,  np.ndarray			):
             UpperCamelCase_					: Optional[Any]            =				np.asarray(__lowerCAmelCase      ,  dtype=np.floataa			)
       elif isinstance(__lowerCAmelCase      ,  np.ndarray			) and raw_speech.dtype is np.dtype(np.floataa			):
             UpperCamelCase_					: int            =				raw_speech.astype(np.floataa			)

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

       # extract fbank features
       UpperCamelCase_					: Any            =				[self._extract_fbank_features(__lowerCAmelCase			) for waveform in raw_speech]

       # convert into correct format for padding
       UpperCamelCase_					: Tuple            =				BatchFeature({"""input_features""": features}			)

       UpperCamelCase_					: Tuple            =				self.pad(
           __lowerCAmelCase      ,  padding=__lowerCAmelCase      ,  max_length=__lowerCAmelCase      ,  truncation=__lowerCAmelCase      ,  pad_to_multiple_of=__lowerCAmelCase      ,  return_attention_mask=__lowerCAmelCase      ,  **__lowerCAmelCase      ,  )

       # make sure list is in array format
       UpperCamelCase_					: str            =				padded_inputs.get("""input_features"""			)
       if isinstance(input_features[0]      ,  __lowerCAmelCase			):
             UpperCamelCase_					: Any            =				[np.asarray(__lowerCAmelCase      ,  dtype=np.floataa			) for feature in input_features]

       UpperCamelCase_					: Union[str, Any]            =				padded_inputs.get("""attention_mask"""			)
       if attention_mask is not None:
             UpperCamelCase_					: Union[str, Any]            =				[np.asarray(__lowerCAmelCase      ,  dtype=np.intaa			) for array in attention_mask]

       # Utterance-level cepstral mean and variance normalization
       if self.do_ceptral_normalize:
             UpperCamelCase_					: List[str]            =				(
                 np.array(__lowerCAmelCase      ,  dtype=np.intaa			)
                 if self._get_padding_strategies(__lowerCAmelCase      ,  max_length=__lowerCAmelCase			) is not PaddingStrategy.DO_NOT_PAD
                 else None
             )
             UpperCamelCase_					: Optional[int]            =				self.normalize(
                 padded_inputs["""input_features"""]      ,  attention_mask=__lowerCAmelCase			)

       if return_tensors is not None:
             UpperCamelCase_					: Optional[Any]            =				padded_inputs.convert_to_tensors(__lowerCAmelCase			)

       return padded_inputs














'''simple docstring'''

import json
import os
from datetime import date
from pathlib import Path

from tabulate import DataRow, TableFormat, tabulate


UpperCamelCase      					=TableFormat(
    lineabove=None,
    linebelowheader=None,
    linebetweenrows=None,
    linebelow=None,
    headerrow=DataRow("", "|", "|"),
    datarow=DataRow("", "|", "|"),
    padding=1,
    with_header_hide=None,
)


UpperCamelCase      					=[]
UpperCamelCase      					=[]

UpperCamelCase      					={"type": "section", "text": {"type": "plain_text", "text": "No failed tests! 🤗", "emoji": True}}

UpperCamelCase      					=[
    {
        "type": "header",
        "text": {
            "type": "plain_text",
            "text": f"🤗 Accelerate nightly {os.environ.get('TEST_TYPE', '')} test results",
            "emoji": True,
        },
    }
]

UpperCamelCase      					=0
for log in Path().glob("*.log"):
      UpperCamelCase      					=0
      with open(log, "r") as f:
            for line in f:
                  UpperCamelCase      					=json.loads(line)
                  if line.get("nodeid", "") != "":
                        UpperCamelCase      					=line["nodeid"]
                        if line.get("duration", None) is not None:
                              UpperCamelCase      					=f"{line['duration']:.4f}"
                              if line.get("outcome", "") == "failed":
                                    section_num_failed += 1
                                    failed.append([test, duration, log.name.split("_")[0]])
                                    total_num_failed += 1
      group_info.append([str(log), section_num_failed, failed])
      UpperCamelCase      					=[]
      log.unlink()

UpperCamelCase      					=""
UpperCamelCase      					=[]
if total_num_failed > 0:
      for name, num_failed, failed_tests in group_info:
            if num_failed > 0:
                  if num_failed == 1:
                        message += f"*{name[1:]}: {num_failed} failed test*\n"
                  else:
                        message += f"*{name[1:]}: {num_failed} failed tests*\n"
                  UpperCamelCase      					=[]
                  UpperCamelCase      					={}
                  for test in failed_tests:
                        UpperCamelCase      					=test[0].split("::")
                        UpperCamelCase      					=data[0].split("/")[-1]
                        if data[0] not in filesafailed:
                              UpperCamelCase      					=[data[1:]]
                        else:
                              filesafailed[data[0]] += [data[1:]]
                        failed_table.append(data)

                  UpperCamelCase      					=[test[0] for test in failed_table]
                  UpperCamelCase      					=list(set(files))
                  # Count number of instances in failed_tests
                  UpperCamelCase      					=[]
                  for file in individual_files:
                        table.append([file, len(filesafailed[file])])

                  UpperCamelCase      					=tabulate(
                      table,
                      headers=["Test Location", "Num Failed"],
                      tablefmt=hf_table_format,
                      stralign="right",
                  )
                  message += f"\n```\n{failed_table}\n```"
                  all_filesafailed.append(filesafailed)
      if len(message) > 3000:
            UpperCamelCase      					="Too many failed tests, please see the full report in the Action results."
            UpperCamelCase      					=len(err) + 10
            UpperCamelCase      					=message[: 3000 - offset] + f"\n...\n```\n{err}"
      print(f"### {message}")
else:
      UpperCamelCase      					="No failed tests! 🤗"
      print(f"## {message}")
      payload.append(no_error_payload)

if os.environ.get("TEST_TYPE", "") != "":
      from slack_sdk import WebClient

      UpperCamelCase      					=WebClient(token=os.environ["SLACK_API_TOKEN"])
      if message != "No failed tests! 🤗":
            UpperCamelCase      					={
                "type": "section",
                "text": {
                    "type": "mrkdwn",
                    "text": message,
                },
            }
            payload.append(md_report)
            UpperCamelCase      					={
                "type": "section",
                "text": {
                    "type": "mrkdwn",
                    "text": "*For more details:*",
                },
                "accessory": {
                    "type": "button",
                    "text": {
                        "type": "plain_text",
                        "text": "Check Action results",
                        "emoji": True,
                    },
                    "url": f"https://github.com/{os.environ['GITHUB_REPOSITORY']}/actions/runs/{os.environ['GITHUB_RUN_ID']}",
                },
            }
            payload.append(action_button)
            UpperCamelCase      					={
                "type": "context",
                "elements": [
                    {
                        "type": "plain_text",
                        "text": f"Nightly {os.environ.get('TEST_TYPE')} test results for {date.today()}",
                    }
                ],
            }
            payload.append(date_report)
      UpperCamelCase      					=client.chat_postMessage(channel="#accelerate-ci-daily", text=message, blocks=payload)
      UpperCamelCase      					=response.data["ts"]
      for failed_file in all_filesafailed:
            for test_location, test_failures in failed_file.items():
                  # Keep only the first instance of the test name
                  UpperCamelCase      					=""
                  for i, row in enumerate(test_failures):
                        if row[0] != test_class:
                              UpperCamelCase      					=row[0]
                        else:
                              UpperCamelCase      					=""

                  UpperCamelCase      					={
                      "type": "section",
                      "text": {
                          "type": "mrkdwn",
                          "text": f"Test location: {test_location}\n```\n{tabulate(test_failures, headers=['Class', 'Test'], tablefmt=hf_table_format, stralign='right')}\n```",
                      },
                  }

                  client.chat_postMessage(
                      channel="#accelerate-ci-daily",
                      thread_ts=ts,
                      blocks=[payload],
                  )






