





import inspect
import unittest

from transformers import ConvNextVaConfig
from transformers.models.auto import get_values
from transformers.models.auto.modeling_auto import MODEL_FOR_BACKBONE_MAPPING_NAMES, MODEL_MAPPING_NAMES
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
     import torch

     from transformers import ConvNextVaBackbone, ConvNextVaForImageClassification, ConvNextVaModel
     from transformers.models.convnextva.modeling_convnextva import CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
     from PIL import Image

     from transformers import AutoImageProcessor

class   __a:






    """simple docstring"""





    def __init__(				self       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE=13       ,_SCREAMING_SNAKE_CASE=32       ,_SCREAMING_SNAKE_CASE=3       ,_SCREAMING_SNAKE_CASE=4       ,_SCREAMING_SNAKE_CASE=[10, 20, 30, 40]       ,_SCREAMING_SNAKE_CASE=[2, 2, 3, 2]       ,_SCREAMING_SNAKE_CASE=True       ,_SCREAMING_SNAKE_CASE=True       ,_SCREAMING_SNAKE_CASE=37       ,_SCREAMING_SNAKE_CASE="gelu"       ,_SCREAMING_SNAKE_CASE=10       ,_SCREAMING_SNAKE_CASE=0.02       ,_SCREAMING_SNAKE_CASE=["stage2", "stage3", "stage4"]       ,_SCREAMING_SNAKE_CASE=[2, 3, 4]       ,_SCREAMING_SNAKE_CASE=None       ,)			->		Dict:
         UpperCAmelCase_      :							Optional[int]       =      parent
         UpperCAmelCase_      :							List[str]       =      batch_size
         UpperCAmelCase_      :							List[str]       =      image_size
         UpperCAmelCase_      :							int       =      num_channels
         UpperCAmelCase_      :							Tuple       =      num_stages
         UpperCAmelCase_      :							Optional[int]       =      hidden_sizes
         UpperCAmelCase_      :							List[Any]       =      depths
         UpperCAmelCase_      :							Any       =      is_training
         UpperCAmelCase_      :							str       =      use_labels
         UpperCAmelCase_      :							int       =      intermediate_size
         UpperCAmelCase_      :							Any       =      hidden_act
         UpperCAmelCase_      :							Optional[Any]       =      num_labels
         UpperCAmelCase_      :							Union[str, Any]       =      initializer_range
         UpperCAmelCase_      :							Any       =      out_features
         UpperCAmelCase_      :							Dict       =      out_indices
         UpperCAmelCase_      :							Union[str, Any]       =      scope





    def 							a__				(				self  )			->		Tuple:
         UpperCAmelCase_      :							Any       =      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

         UpperCAmelCase_      :							Optional[int]       =      None
         if self.use_labels:
              UpperCAmelCase_      :							Any       =      ids_tensor([self.batch_size]       ,self.num_labels  )

         UpperCAmelCase_      :							Tuple       =      self.get_config()

         return config, pixel_values, labels





    def 							a__				(				self  )			->		Optional[Any]:
         return ConvNextVaConfig(
             num_channels=self.num_channels       ,hidden_sizes=self.hidden_sizes       ,depths=self.depths       ,num_stages=self.num_stages       ,hidden_act=self.hidden_act       ,is_decoder=_SCREAMING_SNAKE_CASE       ,initializer_range=self.initializer_range       ,out_features=self.out_features       ,out_indices=self.out_indices       ,num_labels=self.num_labels       ,)





    def 							a__				(				self       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE  )			->		Dict:
         UpperCAmelCase_      :							Union[str, Any]       =      ConvNextVaModel(config=_SCREAMING_SNAKE_CASE  )
         model.to(_SCREAMING_SNAKE_CASE  )
         model.eval()
         UpperCAmelCase_      :							Optional[Any]       =      model(_SCREAMING_SNAKE_CASE  )
         # expected last hidden states: B, C, H // 32, W // 32
         self.parent.assertEqual(
             result.last_hidden_state.shape       ,(self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)       ,)





    def 							a__				(				self       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE  )			->		Optional[Any]:
         UpperCAmelCase_      :							Any       =      ConvNextVaForImageClassification(_SCREAMING_SNAKE_CASE  )
         model.to(_SCREAMING_SNAKE_CASE  )
         model.eval()
         UpperCAmelCase_      :							Union[str, Any]       =      model(_SCREAMING_SNAKE_CASE       ,labels=_SCREAMING_SNAKE_CASE  )
         self.parent.assertEqual(result.logits.shape       ,(self.batch_size, self.num_labels)  )





    def 							a__				(				self       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE  )			->		Dict:
         UpperCAmelCase_      :							Optional[int]       =      ConvNextVaBackbone(config=_SCREAMING_SNAKE_CASE  )
         model.to(_SCREAMING_SNAKE_CASE  )
         model.eval()
         UpperCAmelCase_      :							List[str]       =      model(_SCREAMING_SNAKE_CASE  )

         # verify hidden states
         self.parent.assertEqual(len(result.feature_maps  )       ,len(config.out_features  )  )
         self.parent.assertListEqual(list(result.feature_maps[0].shape  )       ,[self.batch_size, self.hidden_sizes[1], 4, 4]  )

         # verify channels
         self.parent.assertEqual(len(model.channels  )       ,len(config.out_features  )  )
         self.parent.assertListEqual(model.channels       ,config.hidden_sizes[1:]  )

         # verify backbone works with out_features=None
         UpperCAmelCase_      :							Union[str, Any]       =      None
         UpperCAmelCase_      :							str       =      ConvNextVaBackbone(config=_SCREAMING_SNAKE_CASE  )
         model.to(_SCREAMING_SNAKE_CASE  )
         model.eval()
         UpperCAmelCase_      :							str       =      model(_SCREAMING_SNAKE_CASE  )

         # verify feature maps
         self.parent.assertEqual(len(result.feature_maps  )       ,1  )
         self.parent.assertListEqual(list(result.feature_maps[0].shape  )       ,[self.batch_size, self.hidden_sizes[-1], 1, 1]  )

         # verify channels
         self.parent.assertEqual(len(model.channels  )       ,1  )
         self.parent.assertListEqual(model.channels       ,[config.hidden_sizes[-1]]  )





    def 							a__				(				self  )			->		Dict:
         UpperCAmelCase_      :							Union[str, Any]       =      self.prepare_config_and_inputs()
         UpperCAmelCase_      :							List[Any]       =      config_and_inputs
         UpperCAmelCase_      :							Dict       =      {'''pixel_values''': pixel_values}
         return config, inputs_dict





    def 							a__				(				self  )			->		int:
         UpperCAmelCase_      :							Optional[Any]       =      self.prepare_config_and_inputs()
         UpperCAmelCase_      :							Union[str, Any]       =      config_and_inputs
         UpperCAmelCase_      :							Optional[Any]       =      {'''pixel_values''': pixel_values, '''labels''': labels}
         return config, inputs_dict


@require_torch
class   __a(					_a    ,						_a    ,						unittest.TestCase       ):






    """simple docstring"""





    lowerCAmelCase        =				(
        (
            ConvNextVaModel,
            ConvNextVaForImageClassification,
            ConvNextVaBackbone,
        )
        if is_torch_available()
        else ()
    )
    lowerCAmelCase        =				(
        {'''feature-extraction''': ConvNextVaModel, '''image-classification''': ConvNextVaForImageClassification}
        if is_torch_available()
        else {}
    )

    lowerCAmelCase        =				False
    lowerCAmelCase        =				False
    lowerCAmelCase        =				False
    lowerCAmelCase        =				False
    lowerCAmelCase        =				False





    def 							a__				(				self  )			->		Optional[Any]:
         UpperCAmelCase_      :							List[str]       =      ConvNextVaModelTester(self  )
         UpperCAmelCase_      :							Optional[Any]       =      ConfigTester(self       ,config_class=_SCREAMING_SNAKE_CASE       ,has_text_modality=_SCREAMING_SNAKE_CASE       ,hidden_size=37  )





    def 							a__				(				self  )			->		List[Any]:
         self.create_and_test_config_common_properties()
         self.config_tester.create_and_test_config_to_json_string()
         self.config_tester.create_and_test_config_to_json_file()
         self.config_tester.create_and_test_config_from_and_save_pretrained()
         self.config_tester.create_and_test_config_with_num_labels()
         self.config_tester.check_config_can_be_init_without_params()
         self.config_tester.check_config_arguments_init()





    def 							a__				(				self  )			->		Optional[Any]:
         return





    @unittest.skip(reason='''ConvNextV2 does not use inputs_embeds'''  )
    def 							a__				(				self  )			->		Dict:
         pass





    @unittest.skip(reason='''ConvNextV2 does not support input and output embeddings'''  )
    def 							a__				(				self  )			->		Optional[int]:
         pass





    @unittest.skip(reason='''ConvNextV2 does not use feedforward chunking'''  )
    def 							a__				(				self  )			->		Union[str, Any]:
         pass





    def 							a__				(				self  )			->		Dict:
         if not self.model_tester.is_training:
              return

         for model_class in self.all_model_classes:
              UpperCAmelCase_      :							List[str]       =      self.model_tester.prepare_config_and_inputs_with_labels()
              UpperCAmelCase_      :							Optional[Any]       =      True

              if model_class.__name__ in [
                  *get_values(_SCREAMING_SNAKE_CASE  ),
                  *get_values(_SCREAMING_SNAKE_CASE  ),
              ]:
                   continue

              UpperCAmelCase_      :							Union[str, Any]       =      model_class(_SCREAMING_SNAKE_CASE  )
              model.to(_SCREAMING_SNAKE_CASE  )
              model.train()
              UpperCAmelCase_      :							List[str]       =      self._prepare_for_class(_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE       ,return_labels=_SCREAMING_SNAKE_CASE  )
              UpperCAmelCase_      :							Optional[int]       =      model(**_SCREAMING_SNAKE_CASE  ).loss
              loss.backward()





    def 							a__				(				self  )			->		str:
         if not self.model_tester.is_training:
              return

         for model_class in self.all_model_classes:
              UpperCAmelCase_      :							Dict       =      self.model_tester.prepare_config_and_inputs_with_labels()
              UpperCAmelCase_      :							Optional[int]       =      False
              UpperCAmelCase_      :							List[Any]       =      True

              if (
                  model_class.__name__
                  in [*get_values(_SCREAMING_SNAKE_CASE  ), *get_values(_SCREAMING_SNAKE_CASE  )]
                  or not model_class.supports_gradient_checkpointing
              ):
                   continue

              UpperCAmelCase_      :							int       =      model_class(_SCREAMING_SNAKE_CASE  )
              model.to(_SCREAMING_SNAKE_CASE  )
              model.gradient_checkpointing_enable()
              model.train()
              UpperCAmelCase_      :							Optional[int]       =      self._prepare_for_class(_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE       ,return_labels=_SCREAMING_SNAKE_CASE  )
              UpperCAmelCase_      :							Union[str, Any]       =      model(**_SCREAMING_SNAKE_CASE  ).loss
              loss.backward()





    def 							a__				(				self  )			->		Dict:
         UpperCAmelCase_      :							int       =      self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
              UpperCAmelCase_      :							Tuple       =      model_class(_SCREAMING_SNAKE_CASE  )
              UpperCAmelCase_      :							Union[str, Any]       =      inspect.signature(model.forward  )
              # signature.parameters is an OrderedDict => so arg_names order is deterministic
              UpperCAmelCase_      :							Optional[Any]       =      [*signature.parameters.keys()]

              UpperCAmelCase_      :							List[Any]       =      ['''pixel_values''']
              self.assertListEqual(arg_names[:1]       ,_SCREAMING_SNAKE_CASE  )





    def 							a__				(				self  )			->		Any:
         UpperCAmelCase_      :							Dict       =      self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE  )





    def 							a__				(				self  )			->		Any:
         def check_hidden_states_output(_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE  ):
              UpperCAmelCase_      :							int       =      model_class(_SCREAMING_SNAKE_CASE  )
              model.to(_SCREAMING_SNAKE_CASE  )
              model.eval()

              with torch.no_grad():
                   UpperCAmelCase_      :							Any       =      model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE  )  )

              UpperCAmelCase_      :							Any       =      outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

              UpperCAmelCase_      :							Any       =      self.model_tester.num_stages
              self.assertEqual(len(_SCREAMING_SNAKE_CASE  )       ,expected_num_stages + 1  )

              # ConvNextV2's feature maps are of shape (batch_size, num_channels, height, width)
              self.assertListEqual(
                  list(hidden_states[0].shape[-2:]  )       ,[self.model_tester.image_size // 4, self.model_tester.image_size // 4]       ,)

         UpperCAmelCase_      :							Tuple       =      self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
              UpperCAmelCase_      :							Union[str, Any]       =      True
              check_hidden_states_output(_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE  )

              # check that output_hidden_states also work using config
              del inputs_dict["output_hidden_states"]
              UpperCAmelCase_      :							List[Any]       =      True

              check_hidden_states_output(_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE  )





    def 							a__				(				self  )			->		Tuple:
         UpperCAmelCase_      :							Optional[Any]       =      self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*_SCREAMING_SNAKE_CASE  )





    @slow
    def 							a__				(				self  )			->		str:
         for model_name in CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              UpperCAmelCase_      :							int       =      ConvNextVaModel.from_pretrained(_SCREAMING_SNAKE_CASE  )
              self.assertIsNotNone(_SCREAMING_SNAKE_CASE  )







def lowerCamelCase__       (      ):

     '''simple docstring'''


     UpperCAmelCase_      :							Optional[int]       =      Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''				)
     return image



@require_torch
@require_vision
class   __a(					unittest.TestCase       ):






    """simple docstring"""





    @cached_property
    def 							a__				(				self  )			->		Optional[int]:
         return AutoImageProcessor.from_pretrained('''facebook/convnextv2-tiny-1k-224'''  ) if is_vision_available() else None





    @slow
    def 							a__				(				self  )			->		str:
         UpperCAmelCase_      :							Optional[Any]       =      ConvNextVaForImageClassification.from_pretrained('''facebook/convnextv2-tiny-1k-224'''  ).to(_SCREAMING_SNAKE_CASE  )

         UpperCAmelCase_      :							Tuple       =      self.default_image_processor
         UpperCAmelCase_      :							Optional[int]       =      prepare_img()
         UpperCAmelCase_      :							Optional[int]       =      preprocessor(images=_SCREAMING_SNAKE_CASE       ,return_tensors='''pt'''  ).to(_SCREAMING_SNAKE_CASE  )

         # forward pass
         with torch.no_grad():
              UpperCAmelCase_      :							Optional[Any]       =      model(**_SCREAMING_SNAKE_CASE  )

         # verify the logits
         UpperCAmelCase_      :							List[str]       =      torch.Size((1, 1_000)  )
         self.assertEqual(outputs.logits.shape       ,_SCREAMING_SNAKE_CASE  )

         UpperCAmelCase_      :							Dict       =      torch.tensor([0.99_96, 0.19_66, -0.43_86]  ).to(_SCREAMING_SNAKE_CASE  )
         self.assertTrue(torch.allclose(outputs.logits[0, :3]       ,_SCREAMING_SNAKE_CASE       ,atol=1e-4  )  )





import os
import sys
import warnings
from dataclasses import dataclass, field
from io import BytesIO
from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union

import numpy as np
import pyarrow as pa

from .. import config
from ..download.streaming_download_manager import xopen
from ..table import array_cast
from ..utils.file_utils import is_local_path
from ..utils.py_utils import first_non_null_value, no_op_if_value_is_null, string_to_dict


if TYPE_CHECKING:
     import PIL.Image

     from .features import FeatureType


__a          =			None
__a          =			'<' if sys.byteorder == 'little' else '>'
# Origin: https://github.com/python-pillow/Pillow/blob/698951e19e19972aeed56df686868f1329981c12/src/PIL/Image.py#L3126 minus "|i1" which values are not preserved correctly when saving and loading an image
__a          =			[
    np.dtype('|b1'),
    np.dtype('|u1'),
    np.dtype('<u2'),
    np.dtype('>u2'),
    np.dtype('<i2'),
    np.dtype('>i2'),
    np.dtype('<u4'),
    np.dtype('>u4'),
    np.dtype('<i4'),
    np.dtype('>i4'),
    np.dtype('<f4'),
    np.dtype('>f4'),
    np.dtype('<f8'),
    np.dtype('>f8'),
]

@dataclass
class   __a:






    """simple docstring"""





    lowerCAmelCase        =				True
    lowerCAmelCase        =				None
    # Automatically constructed
    lowerCAmelCase        =				"PIL.Image.Image"
    lowerCAmelCase        =				pa.struct({'''bytes''': pa.binary(), '''path''': pa.string()}       )
    lowerCAmelCase        =				field(default='''Image'''    ,						init=_a    ,						repr=_a       )





    def __call__(				self  )			->		Optional[Any]:
         return self.pa_type





    def 							a__				(				self       ,_SCREAMING_SNAKE_CASE  )			->		dict:
         if config.PIL_AVAILABLE:
              import PIL.Image
         else:
              raise ImportError('''To support encoding images, please install \'Pillow\'.'''  )

         if isinstance(_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE  ):
              UpperCAmelCase_      :							Optional[int]       =      np.array(_SCREAMING_SNAKE_CASE  )

         if isinstance(_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE  ):
              return {"path": value, "bytes": None}
         elif isinstance(_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE  ):
              return {"path": None, "bytes": value}
         elif isinstance(_SCREAMING_SNAKE_CASE       ,np.ndarray  ):
              # convert the image array to PNG/TIFF bytes
              return encode_np_array(_SCREAMING_SNAKE_CASE  )
         elif isinstance(_SCREAMING_SNAKE_CASE       ,PIL.Image.Image  ):
              # convert the PIL image to bytes (default format is PNG/TIFF)
              return encode_pil_image(_SCREAMING_SNAKE_CASE  )
         elif value.get('''path'''  ) is not None and os.path.isfile(value['''path''']  ):
              # we set "bytes": None to not duplicate the data if they're already available locally
              return {"bytes": None, "path": value.get('''path'''  )}
         elif value.get('''bytes'''  ) is not None or value.get('''path'''  ) is not None:
              # store the image bytes, and path is used to infer the image format using the file extension
              return {"bytes": value.get('''bytes'''  ), "path": value.get('''path'''  )}
         else:
              raise ValueError(
                  f'''An image sample should have one of \'path\' or \'bytes\' but they are missing or None in {value}.'''  )





    def 							a__				(				self       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE=None  )			->		"PIL.Image.Image":
         if not self.decode:
              raise RuntimeError('''Decoding is disabled for this feature. Please use Image(decode=True) instead.'''  )

         if config.PIL_AVAILABLE:
              import PIL.Image
         else:
              raise ImportError('''To support decoding images, please install \'Pillow\'.'''  )

         if token_per_repo_id is None:
              UpperCAmelCase_      :							str       =      {}

         UpperCAmelCase_,   UpperCAmelCase_      :							List[str]       =      value['''path'''], value['''bytes''']
         if bytes_ is None:
              if path is None:
                   raise ValueError(f'''An image should have one of \'path\' or \'bytes\' but both are None in {value}.'''  )
              else:
                   if is_local_path(_SCREAMING_SNAKE_CASE  ):
                        UpperCAmelCase_      :							int       =      PIL.Image.open(_SCREAMING_SNAKE_CASE  )
                   else:
                        UpperCAmelCase_      :							int       =      path.split('''::'''  )[-1]
                        try:
                             UpperCAmelCase_      :							str       =      string_to_dict(_SCREAMING_SNAKE_CASE       ,config.HUB_DATASETS_URL  )['''repo_id''']
                             UpperCAmelCase_      :							Optional[Any]       =      token_per_repo_id.get(_SCREAMING_SNAKE_CASE  )
                        except ValueError:
                             UpperCAmelCase_      :							Any       =      None
                        with xopen(_SCREAMING_SNAKE_CASE       ,'''rb'''       ,use_auth_token=_SCREAMING_SNAKE_CASE  ) as f:
                             UpperCAmelCase_      :							List[str]       =      BytesIO(f.read()  )
                        UpperCAmelCase_      :							Optional[int]       =      PIL.Image.open(bytes_  )
         else:
              UpperCAmelCase_      :							Dict       =      PIL.Image.open(BytesIO(bytes_  )  )
         image.load()  # to avoid "Too many open files" errors
         return image





    def 							a__				(				self  )			->		Union["FeatureType", Dict[str, "FeatureType"]]:
         from .features import Value

         return (
             self
             if self.decode
             else {
                 "bytes": Value('''binary'''  ),
                 "path": Value('''string'''  ),
             }
         )





    def 							a__				(				self       ,_SCREAMING_SNAKE_CASE  )			->		pa.StructArray:
         if pa.types.is_string(storage.type  ):
              UpperCAmelCase_      :							Union[str, Any]       =      pa.array([None] * len(_SCREAMING_SNAKE_CASE  )       ,type=pa.binary()  )
              UpperCAmelCase_      :							int       =      pa.StructArray.from_arrays([bytes_array, storage]       ,['''bytes''', '''path''']       ,mask=storage.is_null()  )
         elif pa.types.is_binary(storage.type  ):
              UpperCAmelCase_      :							Tuple       =      pa.array([None] * len(_SCREAMING_SNAKE_CASE  )       ,type=pa.string()  )
              UpperCAmelCase_      :							int       =      pa.StructArray.from_arrays([storage, path_array]       ,['''bytes''', '''path''']       ,mask=storage.is_null()  )
         elif pa.types.is_struct(storage.type  ):
              if storage.type.get_field_index('''bytes'''  ) >= 0:
                   UpperCAmelCase_      :							str       =      storage.field('''bytes'''  )
              else:
                   UpperCAmelCase_      :							Union[str, Any]       =      pa.array([None] * len(_SCREAMING_SNAKE_CASE  )       ,type=pa.binary()  )
              if storage.type.get_field_index('''path'''  ) >= 0:
                   UpperCAmelCase_      :							List[str]       =      storage.field('''path'''  )
              else:
                   UpperCAmelCase_      :							List[Any]       =      pa.array([None] * len(_SCREAMING_SNAKE_CASE  )       ,type=pa.string()  )
              UpperCAmelCase_      :							Any       =      pa.StructArray.from_arrays([bytes_array, path_array]       ,['''bytes''', '''path''']       ,mask=storage.is_null()  )
         elif pa.types.is_list(storage.type  ):
              UpperCAmelCase_      :							Optional[Any]       =      pa.array(
                  [encode_np_array(np.array(_SCREAMING_SNAKE_CASE  )  )['''bytes'''] if arr is not None else None for arr in storage.to_pylist()]       ,type=pa.binary()       ,)
              UpperCAmelCase_      :							Any       =      pa.array([None] * len(_SCREAMING_SNAKE_CASE  )       ,type=pa.string()  )
              UpperCAmelCase_      :							Any       =      pa.StructArray.from_arrays(
                  [bytes_array, path_array]       ,['''bytes''', '''path''']       ,mask=bytes_array.is_null()  )
         return array_cast(_SCREAMING_SNAKE_CASE       ,self.pa_type  )





    def 							a__				(				self       ,_SCREAMING_SNAKE_CASE  )			->		pa.StructArray:

         @no_op_if_value_is_null
         def path_to_bytes(_SCREAMING_SNAKE_CASE  ):
              with xopen(_SCREAMING_SNAKE_CASE       ,'''rb'''  ) as f:
                   UpperCAmelCase_      :							List[Any]       =      f.read()
              return bytes_

         UpperCAmelCase_      :							Dict       =      pa.array(
             [
                 (path_to_bytes(x['''path''']  ) if x['''bytes'''] is None else x['''bytes''']) if x is not None else None
                 for x in storage.to_pylist()
             ]       ,type=pa.binary()       ,)
         UpperCAmelCase_      :							Union[str, Any]       =      pa.array(
             [os.path.basename(_SCREAMING_SNAKE_CASE  ) if path is not None else None for path in storage.field('''path'''  ).to_pylist()]       ,type=pa.string()       ,)
         UpperCAmelCase_      :							Tuple       =      pa.StructArray.from_arrays([bytes_array, path_array]       ,['''bytes''', '''path''']       ,mask=bytes_array.is_null()  )
         return array_cast(_SCREAMING_SNAKE_CASE       ,self.pa_type  )







def lowerCamelCase__       (      ):

     '''simple docstring'''


     if config.PIL_AVAILABLE:
          import PIL.Image
     else:
          raise ImportError('''To support encoding images, please install \'Pillow\'.'''				)

     global _IMAGE_COMPRESSION_FORMATS
     if _IMAGE_COMPRESSION_FORMATS is None:
          PIL.Image.init()
          UpperCAmelCase_      :							Optional[Any]       =      list(set(PIL.Image.OPEN.keys()				) & set(PIL.Image.SAVE.keys()				)				)
     return _IMAGE_COMPRESSION_FORMATS







def lowerCamelCase__       (      _lowercase				):

     '''simple docstring'''


     UpperCAmelCase_      :							Optional[int]       =      BytesIO()
     if image.format in list_image_compression_formats():
          UpperCAmelCase_      :							List[str]       =      image.format
     else:
          UpperCAmelCase_      :							int       =      '''PNG''' if image.mode in ['''1''', '''L''', '''LA''', '''RGB''', '''RGBA'''] else '''TIFF'''
     image.save(_lowercase			,						format=_lowercase				)
     return buffer.getvalue()







def lowerCamelCase__       (      _lowercase				):

     '''simple docstring'''


     if hasattr(_lowercase			,						'''filename'''				) and image.filename != "":
          return {"path": image.filename, "bytes": None}
     else:
          return {"path": None, "bytes": image_to_bytes(_lowercase				)}







def lowerCamelCase__       (      _lowercase				):

     '''simple docstring'''


     if config.PIL_AVAILABLE:
          import PIL.Image
     else:
          raise ImportError('''To support encoding images, please install \'Pillow\'.'''				)

     UpperCAmelCase_      :							int       =      array.dtype
     UpperCAmelCase_      :							Optional[Any]       =      dtype.byteorder if dtype.byteorder != '''=''' else _NATIVE_BYTEORDER
     UpperCAmelCase_      :							str       =      dtype.kind
     UpperCAmelCase_      :							int       =      dtype.itemsize

     UpperCAmelCase_      :							Optional[int]       =      None

     # Multi-channel array case (only np.dtype("|u1") is allowed)
     if array.shape[2:]:
          UpperCAmelCase_      :							str       =      np.dtype('''|u1'''				)
          if dtype_kind not in ["u", "i"]:
               raise TypeError(
                   f'''Unsupported array dtype {dtype} for image encoding. Only {dest_dtype} is supported for multi-channel arrays.'''				)
          if dtype is not dest_dtype:
               warnings.warn(f'''Downcasting array dtype {dtype} to {dest_dtype} to be compatible with \'Pillow\''''				)
    # Exact match
     elif dtype in _VALID_IMAGE_ARRAY_DTPYES:
          UpperCAmelCase_      :							Dict       =      dtype
     else:  # Downcast the type within the kind (np.can_cast(from_type, to_type, casting="same_kind") doesn't behave as expected, so do it manually)
          while dtype_itemsize >= 1:
               UpperCAmelCase_      :							Optional[int]       =      dtype_byteorder + dtype_kind + str(_lowercase				)
               UpperCAmelCase_      :							Dict       =      np.dtype(_lowercase				)
               if dest_dtype in _VALID_IMAGE_ARRAY_DTPYES:
                    warnings.warn(f'''Downcasting array dtype {dtype} to {dest_dtype} to be compatible with \'Pillow\''''				)
                    break
               else:
                    dtype_itemsize //= 2

     if dest_dtype is None:
          raise TypeError(
              f'''Cannot convert dtype {dtype} to a valid image dtype. Valid image dtypes: {_VALID_IMAGE_ARRAY_DTPYES}'''				)
     UpperCAmelCase_      :							Dict       =      PIL.Image.fromarray(array.astype(_lowercase				)				)
     return {"path": None, "bytes": image_to_bytes(_lowercase				)}







def lowerCamelCase__       (      _lowercase				):

     '''simple docstring'''


     if config.PIL_AVAILABLE:
          import PIL.Image
     else:
          raise ImportError('''To support encoding images, please install \'Pillow\'.'''				)

     if objs:
          UpperCAmelCase_,   UpperCAmelCase_      :							List[Any]       =      first_non_null_value(_lowercase				)
          if isinstance(_lowercase			,						_lowercase				):
               return [{"path": obj, "bytes": None} if obj is not None else None for obj in objs]
          if isinstance(_lowercase			,						np.ndarray				):
               UpperCAmelCase_      :							List[str]       =      no_op_if_value_is_null(_lowercase				)
               return [obj_to_image_dict_func(_lowercase				) for obj in objs]
          elif isinstance(_lowercase			,						PIL.Image.Image				):
               UpperCAmelCase_      :							Union[str, Any]       =      no_op_if_value_is_null(_lowercase				)
               return [obj_to_image_dict_func(_lowercase				) for obj in objs]
          else:
               return objs
     else:
          return objs