
'''simple docstring'''







import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin


if is_torch_available():
							import torch

if is_vision_available():
							from PIL import Image

							from transformers import SwinaSRImageProcessor
							from transformers.image_transforms import get_image_size






class 					lowerCAmelCase__  (    unittest.TestCase	):






					def __init__(  self							,				__SCREAMING_SNAKE_CASE							,				__SCREAMING_SNAKE_CASE=7							,				__SCREAMING_SNAKE_CASE=3							,				__SCREAMING_SNAKE_CASE=18							,				__SCREAMING_SNAKE_CASE=30							,				__SCREAMING_SNAKE_CASE=4_00							,				__SCREAMING_SNAKE_CASE=True							,				__SCREAMING_SNAKE_CASE=1 / 2_55							,				__SCREAMING_SNAKE_CASE=True							,				__SCREAMING_SNAKE_CASE=8							,				):


											"""simple docstring"""





											lowercase_      :	Union[str, Any]						=			parent
											lowercase_      :	List[Any]						=			batch_size
											lowercase_      :	Tuple						=			num_channels
											lowercase_      :	Optional[Any]						=			image_size
											lowercase_      :	int						=			min_resolution
											lowercase_      :	Union[str, Any]						=			max_resolution
											lowercase_      :	Union[str, Any]						=			do_rescale
											lowercase_      :	List[str]						=			rescale_factor
											lowercase_      :	Tuple						=			do_pad
											lowercase_      :	List[Any]						=			pad_size






					def       _snake_case       (  self						):


											"""simple docstring"""





											return {
											    "do_rescale": self.do_rescale,
											    "rescale_factor": self.rescale_factor,
											    "do_pad": self.do_pad,
											    "pad_size": self.pad_size,
											}






					def       _snake_case       (  self							,				__SCREAMING_SNAKE_CASE=False							,				__SCREAMING_SNAKE_CASE=False							,				__SCREAMING_SNAKE_CASE=False						):


											"""simple docstring"""





											assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

											if equal_resolution:
																	lowercase_      :	List[Any]						=			[]
																	for i in range(self.batch_size						):
																							image_inputs.append(
																							    np.random.randint(
																							        2_55							,				size=(self.num_channels, self.max_resolution, self.max_resolution)							,				dtype=np.uinta						)						)
											else:
																	lowercase_      :	List[str]						=			[]
																	for i in range(self.batch_size						):
																							lowercase_		,					lowercase_      :	Optional[Any]						=			np.random.choice(np.arange(self.min_resolution							,				self.max_resolution						)							,				2						)
																							image_inputs.append(np.random.randint(2_55							,				size=(self.num_channels, width, height)							,				dtype=np.uinta						)						)

											if not numpify and not torchify:
																	# PIL expects the channel dimension as last dimension
																	lowercase_      :	Optional[Any]						=			[Image.fromarray(np.moveaxis(__SCREAMING_SNAKE_CASE							,				0							,				-1						)						) for x in image_inputs]

											if torchify:
																	lowercase_      :	List[str]						=			[torch.from_numpy(__SCREAMING_SNAKE_CASE						) for x in image_inputs]

											return image_inputs







@require_torch
@require_vision
class 					lowerCAmelCase__  (    lowerCamelCase_       ,		unittest.TestCase	):
					lowerCAmelCase_	  =	SwinaSRImageProcessor if is_vision_available() else None






					def       _snake_case       (  self						):


											"""simple docstring"""





											lowercase_      :	str						=			SwinaSRImageProcessingTester(self						)






					@property
					def       _snake_case       (  self						):


											"""simple docstring"""





											return self.image_processor_tester.prepare_image_processor_dict()






					def       _snake_case       (  self						):


											"""simple docstring"""





											lowercase_      :	int						=			self.image_processing_class(**self.image_processor_dict						)
											self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE							,				'''do_rescale'''						)						)
											self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE							,				'''rescale_factor'''						)						)
											self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE							,				'''do_pad'''						)						)
											self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE							,				'''pad_size'''						)						)






					def       _snake_case       (  self						):


											"""simple docstring"""





											pass






					def       _snake_case       (  self							,				__SCREAMING_SNAKE_CASE						):


											"""simple docstring"""





											lowercase_		,					lowercase_      :	Dict						=			get_image_size(__SCREAMING_SNAKE_CASE						)
											lowercase_      :	Optional[int]						=			self.image_processor_tester.pad_size

											lowercase_      :	Dict						=			(old_height // size + 1) * size - old_height
											lowercase_      :	Any						=			(old_width // size + 1) * size - old_width
											return old_height + pad_height, old_width + pad_width






					def       _snake_case       (  self						):


											"""simple docstring"""





											lowercase_      :	Tuple						=			self.image_processing_class(**self.image_processor_dict						)
											# create random PIL images
											lowercase_      :	Tuple						=			self.image_processor_tester.prepare_inputs(equal_resolution=__SCREAMING_SNAKE_CASE						)
											for image in image_inputs:
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE							,				Image.Image						)

											# Test not batched input
											lowercase_      :	str						=			image_processor(image_inputs[0]							,				return_tensors='''pt'''						).pixel_values
											lowercase_		,					lowercase_      :	Optional[Any]						=			self.calculate_expected_size(np.array(image_inputs[0]						)						)
											self.assertEqual(
											    encoded_images.shape							,				(
											        1,
											        self.image_processor_tester.num_channels,
											        expected_height,
											        expected_width,
											    )							,				)






					def       _snake_case       (  self						):


											"""simple docstring"""





											lowercase_      :	List[Any]						=			self.image_processing_class(**self.image_processor_dict						)
											# create random numpy tensors
											lowercase_      :	Tuple						=			self.image_processor_tester.prepare_inputs(equal_resolution=__SCREAMING_SNAKE_CASE							,				numpify=__SCREAMING_SNAKE_CASE						)
											for image in image_inputs:
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE							,				np.ndarray						)

											# Test not batched input
											lowercase_      :	Dict						=			image_processor(image_inputs[0]							,				return_tensors='''pt'''						).pixel_values
											lowercase_		,					lowercase_      :	Optional[int]						=			self.calculate_expected_size(image_inputs[0]						)
											self.assertEqual(
											    encoded_images.shape							,				(
											        1,
											        self.image_processor_tester.num_channels,
											        expected_height,
											        expected_width,
											    )							,				)






					def       _snake_case       (  self						):


											"""simple docstring"""





											lowercase_      :	Optional[Any]						=			self.image_processing_class(**self.image_processor_dict						)
											# create random PyTorch tensors
											lowercase_      :	Union[str, Any]						=			self.image_processor_tester.prepare_inputs(equal_resolution=__SCREAMING_SNAKE_CASE							,				torchify=__SCREAMING_SNAKE_CASE						)
											for image in image_inputs:
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE							,				torch.Tensor						)

											# Test not batched input
											lowercase_      :	List[str]						=			image_processor(image_inputs[0]							,				return_tensors='''pt'''						).pixel_values
											lowercase_		,					lowercase_      :	int						=			self.calculate_expected_size(image_inputs[0]						)
											self.assertEqual(
											    encoded_images.shape							,				(
											        1,
											        self.image_processor_tester.num_channels,
											        expected_height,
											        expected_width,
											    )							,				)





'''simple docstring'''







import posixpath
from pathlib import Path
from unittest.mock import patch

import pytest
from fsspec.implementations.local import AbstractFileSystem, LocalFileSystem, stringify_path
from fsspec.registry import _registry as _fsspec_registry






class 					lowerCAmelCase__  (    lowerCamelCase_	):
					lowerCAmelCase_	  =	'''mock'''






					def __init__(  self							,				*__SCREAMING_SNAKE_CASE							,				__SCREAMING_SNAKE_CASE							,				**__SCREAMING_SNAKE_CASE						):


											"""simple docstring"""





											super().__init__()
											lowercase_      :	int						=			LocalFileSystem(*__SCREAMING_SNAKE_CASE							,				**__SCREAMING_SNAKE_CASE						)
											lowercase_      :	Tuple						=			Path(__SCREAMING_SNAKE_CASE						).resolve().as_posix() + '''/'''






					def       _snake_case       (  self							,				__SCREAMING_SNAKE_CASE							,				*__SCREAMING_SNAKE_CASE							,				**__SCREAMING_SNAKE_CASE						):


											"""simple docstring"""





											lowercase_      :	List[str]						=			posixpath.join(self.local_root_dir							,				self._strip_protocol(__SCREAMING_SNAKE_CASE						)						)
											return self._fs.mkdir(__SCREAMING_SNAKE_CASE							,				*__SCREAMING_SNAKE_CASE							,				**__SCREAMING_SNAKE_CASE						)






					def       _snake_case       (  self							,				__SCREAMING_SNAKE_CASE							,				*__SCREAMING_SNAKE_CASE							,				**__SCREAMING_SNAKE_CASE						):


											"""simple docstring"""





											lowercase_      :	Optional[int]						=			posixpath.join(self.local_root_dir							,				self._strip_protocol(__SCREAMING_SNAKE_CASE						)						)
											return self._fs.makedirs(__SCREAMING_SNAKE_CASE							,				*__SCREAMING_SNAKE_CASE							,				**__SCREAMING_SNAKE_CASE						)






					def       _snake_case       (  self							,				__SCREAMING_SNAKE_CASE						):


											"""simple docstring"""





											lowercase_      :	Any						=			posixpath.join(self.local_root_dir							,				self._strip_protocol(__SCREAMING_SNAKE_CASE						)						)
											return self._fs.rmdir(__SCREAMING_SNAKE_CASE						)






					def       _snake_case       (  self							,				__SCREAMING_SNAKE_CASE							,				__SCREAMING_SNAKE_CASE=True							,				*__SCREAMING_SNAKE_CASE							,				**__SCREAMING_SNAKE_CASE						):


											"""simple docstring"""





											lowercase_      :	str						=			posixpath.join(self.local_root_dir							,				self._strip_protocol(__SCREAMING_SNAKE_CASE						)						)
											lowercase_      :	Union[str, Any]						=			self._fs.ls(__SCREAMING_SNAKE_CASE							,				detail=__SCREAMING_SNAKE_CASE							,				*__SCREAMING_SNAKE_CASE							,				**__SCREAMING_SNAKE_CASE						)
											if detail:
																	return [{**info, "name": info["name"][len(self.local_root_dir						) :]} for info in out]
											else:
																	return [name[len(self.local_root_dir						) :] for name in out]






					def       _snake_case       (  self							,				__SCREAMING_SNAKE_CASE							,				*__SCREAMING_SNAKE_CASE							,				**__SCREAMING_SNAKE_CASE						):


											"""simple docstring"""





											lowercase_      :	Optional[int]						=			posixpath.join(self.local_root_dir							,				self._strip_protocol(__SCREAMING_SNAKE_CASE						)						)
											lowercase_      :	Optional[Any]						=			dict(self._fs.info(__SCREAMING_SNAKE_CASE							,				*__SCREAMING_SNAKE_CASE							,				**__SCREAMING_SNAKE_CASE						)						)
											lowercase_      :	Dict						=			out['''name'''][len(self.local_root_dir						) :]
											return out






					def       _snake_case       (  self							,				__SCREAMING_SNAKE_CASE							,				__SCREAMING_SNAKE_CASE							,				*__SCREAMING_SNAKE_CASE							,				**__SCREAMING_SNAKE_CASE						):


											"""simple docstring"""





											lowercase_      :	List[str]						=			posixpath.join(self.local_root_dir							,				self._strip_protocol(__SCREAMING_SNAKE_CASE						)						)
											lowercase_      :	str						=			posixpath.join(self.local_root_dir							,				self._strip_protocol(__SCREAMING_SNAKE_CASE						)						)
											return self._fs.cp_file(__SCREAMING_SNAKE_CASE							,				__SCREAMING_SNAKE_CASE							,				*__SCREAMING_SNAKE_CASE							,				**__SCREAMING_SNAKE_CASE						)






					def       _snake_case       (  self							,				__SCREAMING_SNAKE_CASE							,				*__SCREAMING_SNAKE_CASE							,				**__SCREAMING_SNAKE_CASE						):


											"""simple docstring"""





											lowercase_      :	List[Any]						=			posixpath.join(self.local_root_dir							,				self._strip_protocol(__SCREAMING_SNAKE_CASE						)						)
											return self._fs.rm_file(__SCREAMING_SNAKE_CASE							,				*__SCREAMING_SNAKE_CASE							,				**__SCREAMING_SNAKE_CASE						)






					def       _snake_case       (  self							,				__SCREAMING_SNAKE_CASE							,				*__SCREAMING_SNAKE_CASE							,				**__SCREAMING_SNAKE_CASE						):


											"""simple docstring"""





											lowercase_      :	Union[str, Any]						=			posixpath.join(self.local_root_dir							,				self._strip_protocol(__SCREAMING_SNAKE_CASE						)						)
											return self._fs.rm(__SCREAMING_SNAKE_CASE							,				*__SCREAMING_SNAKE_CASE							,				**__SCREAMING_SNAKE_CASE						)






					def       _snake_case       (  self							,				__SCREAMING_SNAKE_CASE							,				*__SCREAMING_SNAKE_CASE							,				**__SCREAMING_SNAKE_CASE						):


											"""simple docstring"""





											lowercase_      :	Union[str, Any]						=			posixpath.join(self.local_root_dir							,				self._strip_protocol(__SCREAMING_SNAKE_CASE						)						)
											return self._fs._open(__SCREAMING_SNAKE_CASE							,				*__SCREAMING_SNAKE_CASE							,				**__SCREAMING_SNAKE_CASE						)






					def       _snake_case       (  self							,				__SCREAMING_SNAKE_CASE						):


											"""simple docstring"""





											lowercase_      :	Dict						=			posixpath.join(self.local_root_dir							,				self._strip_protocol(__SCREAMING_SNAKE_CASE						)						)
											return self._fs.created(__SCREAMING_SNAKE_CASE						)






					def       _snake_case       (  self							,				__SCREAMING_SNAKE_CASE						):


											"""simple docstring"""





											lowercase_      :	Optional[Any]						=			posixpath.join(self.local_root_dir							,				self._strip_protocol(__SCREAMING_SNAKE_CASE						)						)
											return self._fs.modified(__SCREAMING_SNAKE_CASE						)






					@classmethod
					def       _snake_case       (  cls							,				__SCREAMING_SNAKE_CASE						):


											"""simple docstring"""





											lowercase_      :	Optional[Any]						=			stringify_path(__SCREAMING_SNAKE_CASE						)
											if path.startswith('''mock://'''						):
																	lowercase_      :	int						=			path[7:]
											return path







class 					lowerCAmelCase__  (    lowerCamelCase_	):
					lowerCAmelCase_	  =	'''tmp'''
					lowerCAmelCase_	  =	None






					def __init__(  self							,				*__SCREAMING_SNAKE_CASE							,				**__SCREAMING_SNAKE_CASE						):


											"""simple docstring"""





											assert self.tmp_dir is not None, "TmpDirFileSystem.tmp_dir is not set"
											super().__init__(*__SCREAMING_SNAKE_CASE							,				**__SCREAMING_SNAKE_CASE							,				local_root_dir=self.tmp_dir							,				auto_mkdir=__SCREAMING_SNAKE_CASE						)






					@classmethod
					def       _snake_case       (  cls							,				__SCREAMING_SNAKE_CASE						):


											"""simple docstring"""





											lowercase_      :	str						=			stringify_path(__SCREAMING_SNAKE_CASE						)
											if path.startswith('''tmp://'''						):
																	lowercase_      :	Union[str, Any]						=			path[6:]
											return path







@pytest.fixture
def    snake_case_						(							):



						"""simple docstring"""





						lowercase_      :	List[Any]						=			MockFileSystem
						lowercase_      :	Tuple						=			TmpDirFileSystem
						yield
						del _fsspec_registry["mock"]
						del _fsspec_registry["tmp"]
@pytest.fixture
def    snake_case_						(							__SCREAMING_SNAKE_CASE   :					List[Any] , __SCREAMING_SNAKE_CASE   :					List[Any]				):



						"""simple docstring"""





						lowercase_      :	List[str]						=			tmp_path_factory.mktemp('''mockfs'''				)
						return MockFileSystem(local_root_dir=__SCREAMING_SNAKE_CASE , auto_mkdir=__SCREAMING_SNAKE_CASE				)




@pytest.fixture
def    snake_case_						(							__SCREAMING_SNAKE_CASE   :					Union[str, Any] , __SCREAMING_SNAKE_CASE   :					Any				):



						"""simple docstring"""





						lowercase_      :	Union[str, Any]						=			tmp_path_factory.mktemp('''tmpfs'''				)
						with patch.object(__SCREAMING_SNAKE_CASE , '''tmp_dir''' , __SCREAMING_SNAKE_CASE				):
												yield TmpDirFileSystem()



