




# 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 torch




class 					snake_case__							(TensorFormatter[Mapping, """torch.Tensor""", Mapping]     ):
    """simple docstring"""



    def __init__(   self       :  str      ,   __lowerCamelCase       :  List[Any]=None      ,   **__lowerCamelCase       :  Union[str, Any]							)      ->  int:
       super().__init__(features=__lowerCamelCase							)
       a					=    torch_tensor_kwargs
       import torch  # noqa import torch at initialization





    def 							__UpperCAmelCase							(   self       :  int      ,   __lowerCamelCase       :  Union[str, Any]							)      ->  str:
       import torch

       if isinstance(__lowerCamelCase      ,   __lowerCamelCase							) and column:
          if all(
              isinstance(__lowerCamelCase      ,   torch.Tensor							) and x.shape == column[0].shape and x.dtype == column[0].dtype
              for x in column							):
             return torch.stack(__lowerCamelCase							)
       return column





    def 							__UpperCAmelCase							(   self       :  List[str]      ,   __lowerCamelCase       :  Optional[Any]							)      ->  List[Any]:
       import torch

       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.tolist()

       a					=    {}

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

          if isinstance(__lowerCamelCase      ,   PIL.Image.Image							):
             a					=    np.asarray(__lowerCamelCase							)
       return torch.tensor(__lowerCamelCase      ,   **{**default_dtype, **self.torch_tensor_kwargs}							)





    def 							__UpperCAmelCase							(   self       :  Tuple      ,   __lowerCamelCase       :  Optional[int]							)      ->  List[Any]:
       import torch

       # support for torch, tf, jax etc.
       if hasattr(__lowerCamelCase      ,   "__array__"							) and not isinstance(__lowerCamelCase      ,   torch.Tensor							):
          a					=    data_struct.__array__()
       # support for nested types like struct of list of struct
       if isinstance(__lowerCamelCase      ,   np.ndarray							):
          if data_struct.dtype == object:  # torch tensors cannot be instantied from an array of objects
             return self._consolidate([self.recursive_tensorize(__lowerCamelCase							) for substruct in data_struct]							)
       elif isinstance(__lowerCamelCase      ,   (list, tuple)							):
          return self._consolidate([self.recursive_tensorize(__lowerCamelCase							) for substruct in data_struct]							)
       return self._tensorize(__lowerCamelCase							)





    def 							__UpperCAmelCase							(   self       :  List[str]      ,   __lowerCamelCase       :  dict							)      ->  Union[str, Any]:
       return map_nested(self._recursive_tensorize      ,   __lowerCamelCase      ,   map_list=__lowerCamelCase							)





    def 							__UpperCAmelCase							(   self       :  List[str]      ,   __lowerCamelCase       :  pa.Table							)      ->  Mapping:
       a					=    self.numpy_arrow_extractor().extract_row(__lowerCamelCase							)
       a					=    self.python_features_decoder.decode_row(__lowerCamelCase							)
       return self.recursive_tensorize(__lowerCamelCase							)





    def 							__UpperCAmelCase							(   self       :  List[Any]      ,   __lowerCamelCase       :  pa.Table							)      ->  "torch.Tensor":
       a					=    self.numpy_arrow_extractor().extract_column(__lowerCamelCase							)
       a					=    self.python_features_decoder.decode_column(__lowerCamelCase      ,   pa_table.column_names[0]							)
       a					=    self.recursive_tensorize(__lowerCamelCase							)
       a					=    self._consolidate(__lowerCamelCase							)
       return column





    def 							__UpperCAmelCase							(   self       :  Dict      ,   __lowerCamelCase       :  pa.Table							)      ->  Mapping:
       a					=    self.numpy_arrow_extractor().extract_batch(__lowerCamelCase							)
       a					=    self.python_features_decoder.decode_batch(__lowerCamelCase							)
       a					=    self.recursive_tensorize(__lowerCamelCase							)
       for column_name in batch:
          a					=    self._consolidate(batch[column_name]							)
       return batch












# this script reports modified .py files under the desired list of top-level sub-dirs passed as a list of arguments, e.g.:
#   python ./utils/get_modified_files.py utils src tests examples
#
# it uses git to find the forking point and which files were modified - i.e. files not under git won't be considered
# since the output of this script is fed into Makefile commands it doesn't print a newline after the results

import re
import subprocess
import sys


__lowerCAmelCase	:       Tuple     =	subprocess.check_output('git merge-base main HEAD'.split()).decode('utf-8')
__lowerCAmelCase	:       Tuple     =	subprocess.check_output(F'''git diff --name-only {fork_point_sha}'''.split()).decode('utf-8').split()

__lowerCAmelCase	:       Dict     =	'|'.join(sys.argv[1:])
__lowerCAmelCase	:       List[Any]     =	re.compile(rF'''^({joined_dirs}).*?\.py$''')

__lowerCAmelCase	:       List[Any]     =	[x for x in modified_files if regex.match(x)]
print(' '.join(relevant_modified_files), end='')






