
"""simple docstring"""
# Lint as: python3
import sys
from collections.abc import Mapping
from typing import TYPE_CHECKING

import numpy as np
import pyarrow as pa

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


if TYPE_CHECKING:
    import tensorflow as tf






class   _UpperCAmelCase					(       TensorFormatter[Mapping, """tf.Tensor""", Mapping]):
    def __init__(     self    ,     snake_case_=None    ,     **snake_case_  ):
      super().__init__(features=snake_case_  )
      _snake_case  :    List[str]            = tf_tensor_kwargs
      import tensorflow as tf  # noqa: import tf at initialization
    def        lowerCamelCase__						(     self    ,     snake_case_  ):
      import tensorflow as tf

      if isinstance(snake_case_    ,     snake_case_  ) and column:
        if all(
            isinstance(snake_case_    ,     tf.Tensor  ) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column  ):
          return tf.stack(snake_case_  )
        elif all(
            isinstance(snake_case_    ,     (tf.Tensor, tf.RaggedTensor)  ) and x.ndim == 1 and x.dtype == column[0].dtype
            for x in column  ):
          # only rag 1-D tensors, otherwise some dimensions become ragged even though they were consolidated
          return tf.ragged.stack(snake_case_  )

      return column
    def        lowerCamelCase__						(     self    ,     snake_case_  ):
      import tensorflow as tf

      if value is None:
        return value

      _snake_case  :    List[Any]            = {}

      if isinstance(snake_case_    ,     (np.number, np.ndarray)  ) and np.issubdtype(value.dtype    ,     np.integer  ):
        _snake_case  :    Optional[int]            = {"dtype": tf.intaa}
      elif isinstance(snake_case_    ,     (np.number, np.ndarray)  ) and np.issubdtype(value.dtype    ,     np.floating  ):
        _snake_case  :    str            = {"dtype": tf.floataa}
      elif config.PIL_AVAILABLE and "PIL" in sys.modules:
        import PIL.Image

        if isinstance(snake_case_    ,     PIL.Image.Image  ):
          _snake_case  :    Dict            = np.asarray(snake_case_  )

      return tf.convert_to_tensor(snake_case_    ,     **{**default_dtype, **self.tf_tensor_kwargs}  )
    def        lowerCamelCase__						(     self    ,     snake_case_  ):
      import tensorflow as tf

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

        if isinstance(snake_case_    ,     torch.Tensor  ):
          return self._tensorize(data_struct.detach().cpu().numpy()[()]  )
      if hasattr(snake_case_    ,     "__array__"  ) and not isinstance(snake_case_    ,     tf.Tensor  ):
        _snake_case  :    Any            = data_struct.__array__()
      # support for nested types like struct of list of struct
      if isinstance(snake_case_    ,     np.ndarray  ):
        if data_struct.dtype == object:  # tf tensors cannot be instantied from an array of objects
          return self._consolidate([self.recursive_tensorize(snake_case_  ) for substruct in data_struct]  )
      elif isinstance(snake_case_    ,     (list, tuple)  ):
        return self._consolidate([self.recursive_tensorize(snake_case_  ) for substruct in data_struct]  )
      return self._tensorize(snake_case_  )
    def        lowerCamelCase__						(     self    ,     snake_case_  ):
      return map_nested(self._recursive_tensorize    ,     snake_case_    ,     map_list=snake_case_  )
    def        lowerCamelCase__						(     self    ,     snake_case_  ):
      _snake_case  :    Optional[Any]            = self.numpy_arrow_extractor().extract_row(snake_case_  )
      _snake_case  :    List[str]            = self.python_features_decoder.decode_row(snake_case_  )
      return self.recursive_tensorize(snake_case_  )
    def        lowerCamelCase__						(     self    ,     snake_case_  ):
      _snake_case  :    List[Any]            = self.numpy_arrow_extractor().extract_column(snake_case_  )
      _snake_case  :    str            = self.python_features_decoder.decode_column(snake_case_    ,     pa_table.column_names[0]  )
      _snake_case  :    List[Any]            = self.recursive_tensorize(snake_case_  )
      _snake_case  :    Dict            = self._consolidate(snake_case_  )
      return column


    def        lowerCamelCase__						(     self    ,     snake_case_  ):
      _snake_case  :    str            = self.numpy_arrow_extractor().extract_batch(snake_case_  )
      _snake_case  :    Any            = self.python_features_decoder.decode_batch(snake_case_  )
      _snake_case  :    Optional[Any]            = self.recursive_tensorize(snake_case_  )
      for column_name in batch:
        _snake_case  :    Any            = self._consolidate(batch[column_name]  )
      return batch








import json
import os
import tempfile

import datasets
from datasets.arrow_writer import ArrowWriter
from datasets.features import ArrayaD
from utils import generate_examples, get_duration


__lowercase				    =      (30, 487)
__lowercase				    =      (36, 1_024)
__lowercase				    =      (100, 100)
__lowercase				    =      100

__lowercase				    =      datasets.Features(
    {"""text""": ArrayaD(SHAPE_TEST_1, dtype="""float32"""), """image""": ArrayaD(SHAPE_TEST_2, dtype="""float32""")}
)

__lowercase    ,							__lowercase				    =      os.path.split(__file__)
__lowercase				    =      os.path.join(RESULTS_BASEPATH, """results""", RESULTS_FILENAME.replace(""".py""", """.json"""))


@get_duration
def        __SCREAMING_SNAKE_CASE	(				__lowercase:		Any  ,				__lowercase:		Optional[Any]  ,				__lowercase:		List[Any]):



  '''simple docstring'''






  with ArrowWriter(features=__lowercase  ,				path=os.path.join(__lowercase  ,				'''beta.arrow''')) as writer:
    for key, record in dummy_data:
      __lowerCAmelCase								=					my_features.encode_example(__lowercase)
      writer.write(__lowercase)
    __lowerCAmelCase						,    __lowerCAmelCase								=					writer.finalize()


@get_duration
def        __SCREAMING_SNAKE_CASE	(				__lowercase:		str  ,				__lowercase:		str):



  '''simple docstring'''






  __lowerCAmelCase								=					datasets.Dataset.from_file(
      filename=os.path.join(__lowercase  ,				'''beta.arrow''')  ,				info=datasets.DatasetInfo(features=__lowercase))
  for _ in dataset:
    pass


@get_duration
def        __SCREAMING_SNAKE_CASE	(				__lowercase:		Tuple  ,				__lowercase:		str):



  '''simple docstring'''






  __lowerCAmelCase								=					datasets.Dataset.from_file(
      filename=os.path.join(__lowercase  ,				'''beta.arrow''')  ,				info=datasets.DatasetInfo(features=__lowercase))
  dataset.set_format('''numpy''')
  for _ in dataset:
    pass


@get_duration
def        __SCREAMING_SNAKE_CASE	(				__lowercase:		str  ,				__lowercase:		Dict):



  '''simple docstring'''






  __lowerCAmelCase								=					10
  __lowerCAmelCase								=					datasets.Dataset.from_file(
      filename=os.path.join(__lowercase  ,				'''beta.arrow''')  ,				info=datasets.DatasetInfo(features=__lowercase))
  for i in range(0  ,				len(__lowercase)  ,				__lowercase):
    __lowerCAmelCase								=					dataset[i : i + batch_size]


@get_duration
def        __SCREAMING_SNAKE_CASE	(				__lowercase:		List[Any]  ,				__lowercase:		Union[str, Any]):



  '''simple docstring'''






  __lowerCAmelCase								=					10
  __lowerCAmelCase								=					datasets.Dataset.from_file(
      filename=os.path.join(__lowercase  ,				'''beta.arrow''')  ,				info=datasets.DatasetInfo(features=__lowercase))
  dataset.set_format('''numpy''')
  for i in range(0  ,				len(__lowercase)  ,				__lowercase):
    __lowerCAmelCase								=					dataset[i : i + batch_size]


@get_duration
def        __SCREAMING_SNAKE_CASE	(				__lowercase:		Union[str, Any]  ,				__lowercase:		str):



  '''simple docstring'''






  __lowerCAmelCase								=					datasets.Dataset.from_file(
      filename=os.path.join(__lowercase  ,				'''beta.arrow''')  ,				info=datasets.DatasetInfo(features=__lowercase))
  for col in feats:
    __lowerCAmelCase								=					dataset[col]


@get_duration
def        __SCREAMING_SNAKE_CASE	(				__lowercase:		Optional[int]  ,				__lowercase:		Optional[Any]):



  '''simple docstring'''






  __lowerCAmelCase								=					datasets.Dataset.from_file(
      filename=os.path.join(__lowercase  ,				'''beta.arrow''')  ,				info=datasets.DatasetInfo(features=__lowercase))
  dataset.set_format('''numpy''')
  for col in feats:
    __lowerCAmelCase								=					dataset[col]


def        __SCREAMING_SNAKE_CASE	(				):



  '''simple docstring'''






  __lowerCAmelCase								=					{}
  __lowerCAmelCase								=					(
      read_unformated,
      read_formatted_as_numpy,
      read_batch_unformated,
      read_batch_formatted_as_numpy,
      read_col_unformated,
      read_col_formatted_as_numpy,
  )
  with tempfile.TemporaryDirectory() as tmp_dir:
    __lowerCAmelCase								=					datasets.Features({'''image''': ArrayaD(__lowercase  ,				dtype='''float32''')})
    __lowerCAmelCase								=					generate_examples(features=__lowercase  ,				num_examples=__lowercase)
    __lowerCAmelCase								=					write(__lowercase  ,				__lowercase  ,				__lowercase)
    for read_func in read_functions:
      __lowerCAmelCase								=					read_func(__lowercase  ,				__lowercase)

  with tempfile.TemporaryDirectory() as tmp_dir:
    # don't use fixed length for fair comparison
    # feats = datasets.Features(
    #     {"image": datasets.Sequence(datasets.Sequence(datasets.Value("float32"), SPEED_TEST_SHAPE[1]), SPEED_TEST_SHAPE[0])}
    # )
    __lowerCAmelCase								=					datasets.Features({'''image''': datasets.Sequence(datasets.Sequence(datasets.Value('''float32''')))})
    __lowerCAmelCase								=					generate_examples(
        features=__lowercase  ,				num_examples=__lowercase  ,				seq_shapes={'''image''': SPEED_TEST_SHAPE})
    __lowerCAmelCase								=					write(__lowercase  ,				__lowercase  ,				__lowercase)
    for read_func in read_functions:
      __lowerCAmelCase								=					read_func(__lowercase  ,				__lowercase)

  with tempfile.TemporaryDirectory() as tmp_dir:
    # don't use fixed length for fair comparison
    # feats = datasets.Features(
    #     {"image": datasets.Sequence(datasets.Value("float32"), SPEED_TEST_SHAPE[0] * SPEED_TEST_SHAPE[1])}
    # )
    __lowerCAmelCase								=					datasets.Features({'''image''': datasets.Sequence(datasets.Value('''float32'''))})
    __lowerCAmelCase								=					generate_examples(
        features=__lowercase  ,				num_examples=__lowercase  ,				seq_shapes={'''image''': [SPEED_TEST_SHAPE[0] * SPEED_TEST_SHAPE[1]]}  ,				)
    __lowerCAmelCase								=					write(__lowercase  ,				__lowercase  ,				__lowercase)
    for read_func in read_functions:
      __lowerCAmelCase								=					read_func(__lowercase  ,				__lowercase)

  with open(__lowercase  ,				'''wb''') as f:
    f.write(json.dumps(__lowercase).encode('''utf-8'''))


if __name__ == "__main__":  # useful to run the profiler
      benchmark_array_xd()
