


import shutil
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import (
    is_pt_tf_cross_test,
    require_tf,
    require_torch,
    require_torchvision,
    require_vision,
)
from transformers.utils import is_tf_available, is_torch_available, is_vision_available


if is_vision_available():
			from PIL import Image

			from transformers import AutoProcessor, SamImageProcessor, SamProcessor

if is_torch_available():
			import torch

if is_tf_available():
			import tensorflow as tf

@require_vision
@require_torchvision
class 							a						(unittest.TestCase ):




							"""simple docstring"""


							def        __snake_case   (				self			:    Tuple							)      ->       Optional[Any]:
														__snake_case						:     Dict									=  tempfile.mkdtemp()
														__snake_case						:     Any									=  SamImageProcessor()
														__snake_case						:     Optional[int]									=  SamProcessor(lowerCamelCase							)
														processor.save_pretrained(self.tmpdirname							)

							def        __snake_case   (				self			:    Optional[Any]       ,      **lowerCamelCase			:    Optional[int]							)      ->       Optional[Any]:
														return AutoProcessor.from_pretrained(self.tmpdirname       ,      **lowerCamelCase							).image_processor

							def        __snake_case   (				self			:    Optional[Any]							)      ->       Dict:
														shutil.rmtree(self.tmpdirname							)

							def        __snake_case   (				self			:    int							)      ->       List[Any]:
														__snake_case						:     List[str]									=  [np.random.randint(255       ,      size=(3, 30, 400)       ,      dtype=np.uinta							)]

														__snake_case						:     int									=  [Image.fromarray(np.moveaxis(lowerCamelCase       ,      0       ,      -1							)							) for x in image_inputs]

														return image_inputs

							def        __snake_case   (				self			:    List[Any]							)      ->       Dict:
														__snake_case						:     int									=  SamProcessor(image_processor=self.get_image_processor()							)
														processor.save_pretrained(self.tmpdirname							)

														__snake_case						:     Dict									=  self.get_image_processor(do_normalize=lowerCamelCase       ,      padding_value=1.0							)

														__snake_case						:     Optional[Any]									=  SamProcessor.from_pretrained(self.tmpdirname       ,      do_normalize=lowerCamelCase       ,      padding_value=1.0							)

														self.assertEqual(processor.image_processor.to_json_string()       ,      image_processor_add_kwargs.to_json_string()							)
														self.assertIsInstance(processor.image_processor       ,      lowerCamelCase							)

							def        __snake_case   (				self			:    List[str]							)      ->       Tuple:
														__snake_case						:     int									=  self.get_image_processor()

														__snake_case						:     str									=  SamProcessor(image_processor=lowerCamelCase							)

														__snake_case						:     Optional[int]									=  self.prepare_image_inputs()

														__snake_case						:     List[str]									=  image_processor(lowerCamelCase       ,      return_tensors="np"							)
														__snake_case						:     Dict									=  processor(images=lowerCamelCase       ,      return_tensors="np"							)

														input_feat_extract.pop("original_sizes"							)  # pop original_sizes as it is popped in the processor
														input_feat_extract.pop("reshaped_input_sizes"							)  # pop original_sizes as it is popped in the processor

														for key in input_feat_extract.keys():
																					self.assertAlmostEqual(input_feat_extract[key].sum()       ,      input_processor[key].sum()       ,      delta=1E-2							)






							@require_torch
							def        __snake_case   (				self			:    Optional[Any]							)      ->       Dict:
														__snake_case						:     Tuple									=  self.get_image_processor()

														__snake_case						:     List[Any]									=  SamProcessor(image_processor=lowerCamelCase							)
														__snake_case						:     List[str]									=  [torch.ones((1, 3, 5, 5)							)]

														__snake_case						:     Tuple									=  [[1764, 2646]]

														__snake_case						:     Optional[int]									=  [[683, 1024]]
														__snake_case						:     int									=  processor.post_process_masks(lowerCamelCase       ,      lowerCamelCase       ,      lowerCamelCase							)
														self.assertEqual(masks[0].shape       ,      (1, 3, 1764, 2646)							)

														__snake_case						:     Optional[Any]									=  processor.post_process_masks(
														    lowerCamelCase       ,      torch.tensor(lowerCamelCase							)       ,      torch.tensor(lowerCamelCase							)							)
														self.assertEqual(masks[0].shape       ,      (1, 3, 1764, 2646)							)

														# should also work with np
														__snake_case						:     List[str]									=  [np.ones((1, 3, 5, 5)							)]
														__snake_case						:     Optional[int]									=  processor.post_process_masks(lowerCamelCase       ,      np.array(lowerCamelCase							)       ,      np.array(lowerCamelCase							)							)

														self.assertEqual(masks[0].shape       ,      (1, 3, 1764, 2646)							)

														__snake_case						:     str									=  [[1, 0], [0, 1]]
														with self.assertRaises(lowerCamelCase							):
																					__snake_case						:     Optional[int]									=  processor.post_process_masks(lowerCamelCase       ,      np.array(lowerCamelCase							)       ,      np.array(lowerCamelCase							)							)

@require_vision
@require_tf
class 							a						(unittest.TestCase ):




							"""simple docstring"""


							def        __snake_case   (				self			:    List[Any]							)      ->       Union[str, Any]:
														__snake_case						:     int									=  tempfile.mkdtemp()
														__snake_case						:     str									=  SamImageProcessor()
														__snake_case						:     List[Any]									=  SamProcessor(lowerCamelCase							)
														processor.save_pretrained(self.tmpdirname							)

							def        __snake_case   (				self			:    str       ,      **lowerCamelCase			:    Any							)      ->       Tuple:
														return AutoProcessor.from_pretrained(self.tmpdirname       ,      **lowerCamelCase							).image_processor

							def        __snake_case   (				self			:    Optional[int]							)      ->       Any:
														shutil.rmtree(self.tmpdirname							)

							def        __snake_case   (				self			:    str							)      ->       List[Any]:
														__snake_case						:     int									=  [np.random.randint(255       ,      size=(3, 30, 400)       ,      dtype=np.uinta							)]

														__snake_case						:     Dict									=  [Image.fromarray(np.moveaxis(lowerCamelCase       ,      0       ,      -1							)							) for x in image_inputs]

														return image_inputs

							def        __snake_case   (				self			:    int							)      ->       List[str]:
														__snake_case						:     List[Any]									=  SamProcessor(image_processor=self.get_image_processor()							)
														processor.save_pretrained(self.tmpdirname							)

														__snake_case						:     Optional[Any]									=  self.get_image_processor(do_normalize=lowerCamelCase       ,      padding_value=1.0							)

														__snake_case						:     Tuple									=  SamProcessor.from_pretrained(self.tmpdirname       ,      do_normalize=lowerCamelCase       ,      padding_value=1.0							)

														self.assertEqual(processor.image_processor.to_json_string()       ,      image_processor_add_kwargs.to_json_string()							)
														self.assertIsInstance(processor.image_processor       ,      lowerCamelCase							)

							def        __snake_case   (				self			:    Union[str, Any]							)      ->       List[Any]:
														__snake_case						:     str									=  self.get_image_processor()

														__snake_case						:     Union[str, Any]									=  SamProcessor(image_processor=lowerCamelCase							)

														__snake_case						:     Dict									=  self.prepare_image_inputs()

														__snake_case						:     int									=  image_processor(lowerCamelCase       ,      return_tensors="np"							)
														__snake_case						:     List[str]									=  processor(images=lowerCamelCase       ,      return_tensors="np"							)

														input_feat_extract.pop("original_sizes"							)  # pop original_sizes as it is popped in the processor
														input_feat_extract.pop("reshaped_input_sizes"							)  # pop reshaped_input_sizes as it is popped in the processor

														for key in input_feat_extract.keys():
																					self.assertAlmostEqual(input_feat_extract[key].sum()       ,      input_processor[key].sum()       ,      delta=1E-2							)






							@require_tf
							def        __snake_case   (				self			:    Any							)      ->       Optional[int]:
														__snake_case						:     List[str]									=  self.get_image_processor()

														__snake_case						:     Dict									=  SamProcessor(image_processor=lowerCamelCase							)
														__snake_case						:     Union[str, Any]									=  [tf.ones((1, 3, 5, 5)							)]

														__snake_case						:     List[Any]									=  [[1764, 2646]]

														__snake_case						:     Dict									=  [[683, 1024]]
														__snake_case						:     List[str]									=  processor.post_process_masks(lowerCamelCase       ,      lowerCamelCase       ,      lowerCamelCase       ,      return_tensors="tf"							)
														self.assertEqual(masks[0].shape       ,      (1, 3, 1764, 2646)							)

														__snake_case						:     Optional[Any]									=  processor.post_process_masks(
														    lowerCamelCase       ,      tf.convert_to_tensor(lowerCamelCase							)       ,      tf.convert_to_tensor(lowerCamelCase							)       ,      return_tensors="tf"       ,      )
														self.assertEqual(masks[0].shape       ,      (1, 3, 1764, 2646)							)

														# should also work with np
														__snake_case						:     Union[str, Any]									=  [np.ones((1, 3, 5, 5)							)]
														__snake_case						:     List[str]									=  processor.post_process_masks(
														    lowerCamelCase       ,      np.array(lowerCamelCase							)       ,      np.array(lowerCamelCase							)       ,      return_tensors="tf"							)

														self.assertEqual(masks[0].shape       ,      (1, 3, 1764, 2646)							)

														__snake_case						:     Tuple									=  [[1, 0], [0, 1]]
														with self.assertRaises(tf.errors.InvalidArgumentError							):
																					__snake_case						:     Dict									=  processor.post_process_masks(
																					    lowerCamelCase       ,      np.array(lowerCamelCase							)       ,      np.array(lowerCamelCase							)       ,      return_tensors="tf"							)

@require_vision
@require_torchvision
class 							a						(unittest.TestCase ):




							"""simple docstring"""


							def        __snake_case   (				self			:    List[str]							)      ->       str:
														__snake_case						:     Optional[int]									=  tempfile.mkdtemp()
														__snake_case						:     str									=  SamImageProcessor()
														__snake_case						:     List[Any]									=  SamProcessor(lowerCamelCase							)
														processor.save_pretrained(self.tmpdirname							)

							def        __snake_case   (				self			:    List[str]       ,      **lowerCamelCase			:    Any							)      ->       Dict:
														return AutoProcessor.from_pretrained(self.tmpdirname       ,      **lowerCamelCase							).image_processor

							def        __snake_case   (				self			:    Optional[int]							)      ->       List[Any]:
														shutil.rmtree(self.tmpdirname							)

							def        __snake_case   (				self			:    Optional[int]							)      ->       Optional[int]:
														__snake_case						:     str									=  [np.random.randint(255       ,      size=(3, 30, 400)       ,      dtype=np.uinta							)]

														__snake_case						:     List[Any]									=  [Image.fromarray(np.moveaxis(lowerCamelCase       ,      0       ,      -1							)							) for x in image_inputs]

														return image_inputs

							@is_pt_tf_cross_test
							def        __snake_case   (				self			:    Union[str, Any]							)      ->       List[str]:
														__snake_case						:     str									=  self.get_image_processor()

														__snake_case						:     str									=  SamProcessor(image_processor=lowerCamelCase							)
														__snake_case						:     List[Any]									=  np.random.randint(0       ,      2       ,      size=(1, 3, 5, 5)							).astype(np.floataa							)
														__snake_case						:     Dict									=  [tf.convert_to_tensor(lowerCamelCase							)]
														__snake_case						:     List[Any]									=  [torch.tensor(lowerCamelCase							)]

														__snake_case						:     Optional[Any]									=  [[1764, 2646]]

														__snake_case						:     Optional[int]									=  [[683, 1024]]
														__snake_case						:     Union[str, Any]									=  processor.post_process_masks(
														    lowerCamelCase       ,      lowerCamelCase       ,      lowerCamelCase       ,      return_tensors="tf"							)
														__snake_case						:     Dict									=  processor.post_process_masks(
														    lowerCamelCase       ,      lowerCamelCase       ,      lowerCamelCase       ,      return_tensors="pt"							)

														self.assertTrue(np.all(tf_masks[0].numpy() == pt_masks[0].numpy()							)							)






							@is_pt_tf_cross_test
							def        __snake_case   (				self			:    List[Any]							)      ->       List[str]:
														__snake_case						:     Any									=  self.get_image_processor()

														__snake_case						:     List[Any]									=  SamProcessor(image_processor=lowerCamelCase							)

														__snake_case						:     Dict									=  self.prepare_image_inputs()

														__snake_case						:     Any									=  image_processor(lowerCamelCase       ,      return_tensors="pt"							)["pixel_values"].numpy()
														__snake_case						:     Optional[Any]									=  processor(images=lowerCamelCase       ,      return_tensors="pt"							)["pixel_values"].numpy()

														__snake_case						:     Tuple									=  image_processor(lowerCamelCase       ,      return_tensors="tf"							)["pixel_values"].numpy()
														__snake_case						:     List[Any]									=  processor(images=lowerCamelCase       ,      return_tensors="tf"							)["pixel_values"].numpy()

														self.assertTrue(np.allclose(lowerCamelCase       ,      lowerCamelCase							)							)
														self.assertTrue(np.allclose(lowerCamelCase       ,      lowerCamelCase							)							)
														self.assertTrue(np.allclose(lowerCamelCase       ,      lowerCamelCase							)							)






"""simple docstring"""







import os
import textwrap

import pyarrow as pa
import pytest

from datasets import ClassLabel, Features, Image
from datasets.packaged_modules.csv.csv import Csv

from ..utils import require_pil

@pytest.fixture
def  __SCREAMING_SNAKE_CASE		(			lowerCamelCase_:							Optional[Any] ):
						"""simple docstring"""






						snake_case   :			str						    = tmp_path / "file.csv"
						snake_case   :			Tuple						    = textwrap.dedent(
						    "\\n        header1,header2\n        1,2\n        10,20\n        " )
						with open(lowerCamelCase_     ,		"w" ) as f:
												f.write(lowerCamelCase_ )
						return str(lowerCamelCase_ )

@pytest.fixture
def  __SCREAMING_SNAKE_CASE		(			lowerCamelCase_:							List[str] ):
						"""simple docstring"""






						snake_case   :			List[Any]						    = tmp_path / "malformed_file.csv"
						snake_case   :			Union[str, Any]						    = textwrap.dedent(
						    "\\n        header1,header2\n        1,2\n        10,20,\n        " )
						with open(lowerCamelCase_     ,		"w" ) as f:
												f.write(lowerCamelCase_ )
						return str(lowerCamelCase_ )

@pytest.fixture
def  __SCREAMING_SNAKE_CASE		(			lowerCamelCase_:							int     ,		lowerCamelCase_:							Optional[Any] ):
						"""simple docstring"""






						snake_case   :			Dict						    = tmp_path / "csv_with_image.csv"
						snake_case   :			List[Any]						    = textwrap.dedent(
						    f'''\
        image
        {image_file}
        ''' )
						with open(lowerCamelCase_     ,		"w" ) as f:
												f.write(lowerCamelCase_ )
						return str(lowerCamelCase_ )

@pytest.fixture
def  __SCREAMING_SNAKE_CASE		(			lowerCamelCase_:							List[str] ):
						"""simple docstring"""






						snake_case   :			Optional[Any]						    = tmp_path / "csv_with_label.csv"
						snake_case   :			Tuple						    = textwrap.dedent(
						    "\\n        label\n        good\n        bad\n        good\n        " )
						with open(lowerCamelCase_     ,		"w" ) as f:
												f.write(lowerCamelCase_ )
						return str(lowerCamelCase_ )

@pytest.fixture
def  __SCREAMING_SNAKE_CASE		(			lowerCamelCase_:							List[Any] ):
						"""simple docstring"""






						snake_case   :			Union[str, Any]						    = tmp_path / "csv_with_int_list.csv"
						snake_case   :			Optional[int]						    = textwrap.dedent(
						    "\\n        int_list\n        1 2 3\n        4 5 6\n        7 8 9\n        " )
						with open(lowerCamelCase_     ,		"w" ) as f:
												f.write(lowerCamelCase_ )
						return str(lowerCamelCase_ )

def  __SCREAMING_SNAKE_CASE		(			lowerCamelCase_:							List[str]     ,		lowerCamelCase_:							Any     ,		lowerCamelCase_:							List[str] ):
						"""simple docstring"""






						snake_case   :			Tuple						    = Csv()
						snake_case   :			str						    = csv._generate_tables([[csv_file, malformed_csv_file]] )
						with pytest.raises(lowerCamelCase_     ,		match="Error tokenizing data" ):
												for _ in generator:
																		pass
						assert any(
						    record.levelname == "ERROR"
						    and "Failed to read file" in record.message
						    and os.path.basename(lowerCamelCase_ ) in record.message
						    for record in caplog.records )

@require_pil
def  __SCREAMING_SNAKE_CASE		(			lowerCamelCase_:							str ):
						"""simple docstring"""






						with open(lowerCamelCase_     ,		encoding="utf-8" ) as f:
												snake_case   :			List[Any]						    = f.read().splitlines()[1]
						snake_case   :			List[str]						    = Csv(encoding="utf-8"     ,		features=Features({"image": Image()} ) )
						snake_case   :			Optional[int]						    = csv._generate_tables([[csv_file_with_image]] )
						snake_case   :			List[Any]						    = pa.concat_tables([table for _, table in generator] )
						assert pa_table.schema.field("image" ).type == Image()()
						snake_case   :			str						    = pa_table.to_pydict()["image"]
						assert generated_content == [{"path": image_file, "bytes": None}]

def  __SCREAMING_SNAKE_CASE		(			lowerCamelCase_:							int ):
						"""simple docstring"""






						with open(lowerCamelCase_     ,		encoding="utf-8" ) as f:
												snake_case   :			Optional[Any]						    = f.read().splitlines()[1:]
						snake_case   :			List[str]						    = Csv(encoding="utf-8"     ,		features=Features({"label": ClassLabel(names=["good", "bad"] )} ) )
						snake_case   :			int						    = csv._generate_tables([[csv_file_with_label]] )
						snake_case   :			Dict						    = pa.concat_tables([table for _, table in generator] )
						assert pa_table.schema.field("label" ).type == ClassLabel(names=["good", "bad"] )()
						snake_case   :			Dict						    = pa_table.to_pydict()["label"]
						assert generated_content == [ClassLabel(names=["good", "bad"] ).straint(lowerCamelCase_ ) for label in labels]

def  __SCREAMING_SNAKE_CASE		(			lowerCamelCase_:							Union[str, Any] ):
						"""simple docstring"""






						snake_case   :			List[Any]						    = Csv(encoding="utf-8"     ,		sep=","     ,		converters={"int_list": lambda lowerCamelCase_ : [int(lowerCamelCase_ ) for i in x.split()]} )
						snake_case   :			Tuple						    = csv._generate_tables([[csv_file_with_int_list]] )
						snake_case   :			str						    = pa.concat_tables([table for _, table in generator] )
						assert pa.types.is_list(pa_table.schema.field("int_list" ).type )
						snake_case   :			Any						    = pa_table.to_pydict()["int_list"]
						assert generated_content == [[1, 2, 3], [4, 5, 6], [7, 8, 9]]



