





'''simple docstring'''




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       UpperCamelCase__			(			TensorFormatter[Mapping, np.ndarray, Mapping]	):




					"""simple docstring"""


					def __init__(		self							:	Any     ,		lowerCamelCase_							:	Tuple=None     ,		**lowerCamelCase_							:	int		):

							'''simple docstring'''



							super().__init__(features=lowerCamelCase_		)
							SCREAMING_SNAKE_CASE				:							Any						=	np_array_kwargs


					def lowerCamelCase_	(		self							:	Any     ,		lowerCamelCase_							:	int		):

							'''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
											SCREAMING_SNAKE_CASE				:							Any						=	np.empty(len(lowerCamelCase_		)     ,		dtype=lowerCamelCase_		)
											SCREAMING_SNAKE_CASE				:							int						=	column
											return out
							return column


					def lowerCamelCase_	(		self							:	Any     ,		lowerCamelCase_							:	Tuple		):

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

							SCREAMING_SNAKE_CASE				:							Optional[int]						=	{}

							if isinstance(lowerCamelCase_     ,		np.ndarray		) and np.issubdtype(value.dtype     ,		np.integer		):
									SCREAMING_SNAKE_CASE				:							Any						=	{"""dtype""": np.intaa}
							elif isinstance(lowerCamelCase_     ,		np.ndarray		) and np.issubdtype(value.dtype     ,		np.floating		):
									SCREAMING_SNAKE_CASE				:							int						=	{"""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							:	Union[str, Any]     ,		lowerCamelCase_							:	str		):

							'''simple docstring'''



							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)		):
									SCREAMING_SNAKE_CASE				:							Dict						=	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		):

							'''simple docstring'''



							return map_nested(self._recursive_tensorize     ,		lowerCamelCase_     ,		map_list=lowerCamelCase_		)


					def lowerCamelCase_	(		self							:	int     ,		lowerCamelCase_							:	pa.Table		):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE				:							Tuple						=	self.numpy_arrow_extractor().extract_row(lowerCamelCase_		)
							SCREAMING_SNAKE_CASE				:							Optional[Any]						=	self.python_features_decoder.decode_row(lowerCamelCase_		)
							return self.recursive_tensorize(lowerCamelCase_		)


					def lowerCamelCase_	(		self							:	int     ,		lowerCamelCase_							:	pa.Table		):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE				:							Tuple						=	self.numpy_arrow_extractor().extract_column(lowerCamelCase_		)
							SCREAMING_SNAKE_CASE				:							Tuple						=	self.python_features_decoder.decode_column(lowerCamelCase_     ,		pa_table.column_names[0]		)
							SCREAMING_SNAKE_CASE				:							Optional[Any]						=	self.recursive_tensorize(lowerCamelCase_		)
							SCREAMING_SNAKE_CASE				:							Optional[int]						=	self._consolidate(lowerCamelCase_		)
							return column



					def lowerCamelCase_	(		self							:	str     ,		lowerCamelCase_							:	pa.Table		):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE				:							Dict						=	self.numpy_arrow_extractor().extract_batch(lowerCamelCase_		)
							SCREAMING_SNAKE_CASE				:							int						=	self.python_features_decoder.decode_batch(lowerCamelCase_		)
							SCREAMING_SNAKE_CASE				:							Dict						=	self.recursive_tensorize(lowerCamelCase_		)
							for column_name in batch:
									SCREAMING_SNAKE_CASE				:							Any						=	self._consolidate(batch[column_name]		)
							return batch










'''simple docstring'''




import unittest
from pathlib import Path

from transformers import is_vision_available, load_tool
from transformers.testing_utils import get_tests_dir

from .test_tools_common import ToolTesterMixin


if is_vision_available():
							from PIL import Image




class       UpperCamelCase__			(			unittest.TestCase		,			lowercase_	):




					"""simple docstring"""


					def lowerCamelCase_	(		self							:	Any		):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE				:							int						=	load_tool("""image-captioning"""		)
							self.tool.setup()
							SCREAMING_SNAKE_CASE				:							str						=	load_tool("""image-captioning"""     ,		remote=lowerCamelCase_		)


					def lowerCamelCase_	(		self							:	List[Any]		):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE				:							Union[str, Any]						=	Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""		)		) / """000000039769.png"""		)
							SCREAMING_SNAKE_CASE				:							Optional[int]						=	self.tool(lowerCamelCase_		)
							self.assertEqual(lowerCamelCase_     ,		"""two cats sleeping on a couch"""		)


					def lowerCamelCase_	(		self							:	str		):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE				:							Optional[Any]						=	Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""		)		) / """000000039769.png"""		)
							SCREAMING_SNAKE_CASE				:							Union[str, Any]						=	self.remote_tool(lowerCamelCase_		)
							self.assertEqual(lowerCamelCase_     ,		"""two cats sleeping on a couch"""		)


					def lowerCamelCase_	(		self							:	List[Any]		):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE				:							Union[str, Any]						=	Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""		)		) / """000000039769.png"""		)
							SCREAMING_SNAKE_CASE				:							str						=	self.tool(image=lowerCamelCase_		)
							self.assertEqual(lowerCamelCase_     ,		"""two cats sleeping on a couch"""		)



					def lowerCamelCase_	(		self							:	Any		):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE				:							Union[str, Any]						=	Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""		)		) / """000000039769.png"""		)
							SCREAMING_SNAKE_CASE				:							Optional[Any]						=	self.remote_tool(image=lowerCamelCase_		)
							self.assertEqual(lowerCamelCase_     ,		"""two cats sleeping on a couch"""		)



