


import sys
from collections.abc import Mapping

import numpy as np
import pyarrow as pa

from .. import config
from ..utils.py_utils import map_nested
from .formatting import TensorFormatter


class       __lowerCAmelCase       (	TensorFormatter[Mapping, np.ndarray, Mapping]   ):






  """simple docstring"""





  def __init__(       self    :    Tuple      ,			_lowerCAmelCase    :    str=None      ,			**_lowerCAmelCase    :    Union[str, Any]					)				->	List[Any]:




     """simple docstring"""
     super().__init__(features=_lowerCAmelCase					)
     snake_case_         =   np_array_kwargs



  def    lowerCAmelCase__					(       self    :    List[Any]      ,			_lowerCAmelCase    :    str					)				->	List[Any]:




     """simple docstring"""
     if isinstance(_lowerCAmelCase      ,			_lowerCAmelCase					):
        if column and all(
            isinstance(_lowerCAmelCase      ,			np.ndarray					) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column					):
           return np.stack(_lowerCAmelCase					)
        else:
           # don't use np.array(column, dtype=object)
           # since it fails in certain cases
           # see https://stackoverflow.com/q/51005699
           snake_case_         =   np.empty(len(_lowerCAmelCase					)      ,			dtype=_lowerCAmelCase					)
           snake_case_         =   column
           return out
     return column



  def    lowerCAmelCase__					(       self    :    Optional[Any]      ,			_lowerCAmelCase    :    Any					)				->	Optional[Any]:




     """simple docstring"""
     if isinstance(_lowerCAmelCase      ,			(str, bytes, type(_lowerCAmelCase					))					):
        return value
     elif isinstance(_lowerCAmelCase      ,			(np.character, np.ndarray)					) and np.issubdtype(value.dtype      ,			np.character					):
        return value
     elif isinstance(_lowerCAmelCase      ,			np.number					):
        return value

     snake_case_         =   {}

     if isinstance(_lowerCAmelCase      ,			np.ndarray					) and np.issubdtype(value.dtype      ,			np.integer					):
        snake_case_         =   {"dtype": np.intaa}
     elif isinstance(_lowerCAmelCase      ,			np.ndarray					) and np.issubdtype(value.dtype      ,			np.floating					):
        snake_case_         =   {"dtype": np.floataa}
     elif config.PIL_AVAILABLE and "PIL" in sys.modules:
        import PIL.Image

        if isinstance(_lowerCAmelCase      ,			PIL.Image.Image					):
           return np.asarray(_lowerCAmelCase      ,			**self.np_array_kwargs					)

     return np.asarray(_lowerCAmelCase      ,			**{**default_dtype, **self.np_array_kwargs}					)



  def    lowerCAmelCase__					(       self    :    Optional[Any]      ,			_lowerCAmelCase    :    str					)				->	Union[str, Any]:




     """simple docstring"""
     # support for torch, tf, jax etc.
     if config.TORCH_AVAILABLE and "torch" in sys.modules:
        import torch

        if isinstance(_lowerCAmelCase      ,			torch.Tensor					):
           return self._tensorize(data_struct.detach().cpu().numpy()[()]					)
     if hasattr(_lowerCAmelCase      ,			"__array__"					) and not isinstance(_lowerCAmelCase      ,			(np.ndarray, np.character, np.number)					):
        snake_case_         =   data_struct.__array__()
     # support for nested types like struct of list of struct
     if isinstance(_lowerCAmelCase      ,			np.ndarray					):
        if data_struct.dtype == object:
           return self._consolidate([self.recursive_tensorize(_lowerCAmelCase					) for substruct in data_struct]					)
     if isinstance(_lowerCAmelCase      ,			(list, tuple)					):
        return self._consolidate([self.recursive_tensorize(_lowerCAmelCase					) for substruct in data_struct]					)
     return self._tensorize(_lowerCAmelCase					)



  def    lowerCAmelCase__					(       self    :    Dict      ,			_lowerCAmelCase    :    dict					)				->	Any:




     """simple docstring"""
     return map_nested(self._recursive_tensorize      ,			_lowerCAmelCase      ,			map_list=_lowerCAmelCase					)



  def    lowerCAmelCase__					(       self    :    List[str]      ,			_lowerCAmelCase    :    pa.Table					)				->	Mapping:




     """simple docstring"""
     snake_case_         =   self.numpy_arrow_extractor().extract_row(_lowerCAmelCase					)
     snake_case_         =   self.python_features_decoder.decode_row(_lowerCAmelCase					)
     return self.recursive_tensorize(_lowerCAmelCase					)



  def    lowerCAmelCase__					(       self    :    Union[str, Any]      ,			_lowerCAmelCase    :    pa.Table					)				->	np.ndarray:




     """simple docstring"""
     snake_case_         =   self.numpy_arrow_extractor().extract_column(_lowerCAmelCase					)
     snake_case_         =   self.python_features_decoder.decode_column(_lowerCAmelCase      ,			pa_table.column_names[0]					)
     snake_case_         =   self.recursive_tensorize(_lowerCAmelCase					)
     snake_case_         =   self._consolidate(_lowerCAmelCase					)
     return column



  def    lowerCAmelCase__					(       self    :    Dict      ,			_lowerCAmelCase    :    pa.Table					)				->	Mapping:




     """simple docstring"""
     snake_case_         =   self.numpy_arrow_extractor().extract_batch(_lowerCAmelCase					)
     snake_case_         =   self.python_features_decoder.decode_batch(_lowerCAmelCase					)
     snake_case_         =   self.recursive_tensorize(_lowerCAmelCase					)
     for column_name in batch:
        snake_case_         =   self._consolidate(batch[column_name]					)
     return batch








from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available


SCREAMING_SNAKE_CASE		:str											=       {'''configuration_cvt''': ['''CVT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''CvtConfig''']}


try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   SCREAMING_SNAKE_CASE		:Any											=       [
       '''CVT_PRETRAINED_MODEL_ARCHIVE_LIST''',
       '''CvtForImageClassification''',
       '''CvtModel''',
       '''CvtPreTrainedModel''',
   ]

try:
   if not is_tf_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   SCREAMING_SNAKE_CASE		:Any											=       [
       '''TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST''',
       '''TFCvtForImageClassification''',
       '''TFCvtModel''',
       '''TFCvtPreTrainedModel''',
   ]

if TYPE_CHECKING:
   from .configuration_cvt import CVT_PRETRAINED_CONFIG_ARCHIVE_MAP, CvtConfig

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_cvt import (
          CVT_PRETRAINED_MODEL_ARCHIVE_LIST,
          CvtForImageClassification,
          CvtModel,
          CvtPreTrainedModel,
      )

   try:
      if not is_tf_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_tf_cvt import (
          TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST,
          TFCvtForImageClassification,
          TFCvtModel,
          TFCvtPreTrainedModel,
      )


else:
   import sys

   SCREAMING_SNAKE_CASE		:str											=       _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)




