"""simple docstring"""


import math


class 				a :


    def    lowerCAmelCase_     (    self		:     Tuple      , __lowerCAmelCase		:     list[list[float]]      , __lowerCAmelCase		:     list[int]    ):
      _UpperCAmelCase         =					0.0
      _UpperCAmelCase         =					0.0
      for i in range(len(__lowerCAmelCase    )    ):
        da += math.pow((sample[i] - weights[0][i])      , 2    )
        da += math.pow((sample[i] - weights[1][i])      , 2    )
        return 0 if da > da else 1
      return 0


    def    lowerCAmelCase_     (    self		:     Union[str, Any]      , __lowerCAmelCase		:     list[list[int | float]]      , __lowerCAmelCase		:     list[int]      , __lowerCAmelCase		:     int      , __lowerCAmelCase		:     float    ):
      for i in range(len(__lowerCAmelCase    )    ):
        weights[j][i] += alpha * (sample[i] - weights[j][i])
      return weights






def 			__UpperCAmelCase    (	):







  """simple docstring"""
  # Training Examples ( m, n )
  _UpperCAmelCase         =					[[1, 1, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 1, 1]]

  # weight initialization ( n, C )
  _UpperCAmelCase         =					[[0.2, 0.6, 0.5, 0.9], [0.8, 0.4, 0.7, 0.3]]

  # training
  _UpperCAmelCase         =					SelfOrganizingMap()
  _UpperCAmelCase         =					3
  _UpperCAmelCase         =					0.5

  for _ in range(lowercase						):
    for j in range(len(lowercase						)						):
      # training sample
      _UpperCAmelCase         =					training_samples[j]

      # Compute the winning vector
      _UpperCAmelCase         =					self_organizing_map.get_winner(lowercase      ,lowercase						)

      # Update the winning vector
      _UpperCAmelCase         =					self_organizing_map.update(lowercase      ,lowercase      ,lowercase      ,lowercase						)

    # classify test sample
  _UpperCAmelCase         =					[0, 0, 0, 1]
  _UpperCAmelCase         =					self_organizing_map.get_winner(lowercase      ,lowercase						)

  # results
  print(f'''Clusters that the test sample belongs to : {winner}'''						)
  print(f'''Weights that have been trained : {weights}'''						)


# running the main() function
if __name__ == "__main__":
    main()

"""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, prepare_image_inputs


if is_torch_available():
    import torch

if is_vision_available():
    from PIL import Image

    from transformers import MobileViTImageProcessor


class 				a (						unittest.TestCase      ):


    def __init__(    self		:     Dict      , __lowerCAmelCase		:     Tuple      , __lowerCAmelCase		:     Optional[Any]=7      , __lowerCAmelCase		:     Optional[Any]=3      , __lowerCAmelCase		:     Optional[Any]=18      , __lowerCAmelCase		:     str=30      , __lowerCAmelCase		:     List[str]=400      , __lowerCAmelCase		:     Union[str, Any]=True      , __lowerCAmelCase		:     str=None      , __lowerCAmelCase		:     Optional[int]=True      , __lowerCAmelCase		:     int=None      , __lowerCAmelCase		:     List[str]=True      , ):
      _UpperCAmelCase         =					size if size is not None else {"""shortest_edge""": 20}
      _UpperCAmelCase         =					crop_size if crop_size is not None else {"""height""": 18, """width""": 18}
      _UpperCAmelCase         =					parent
      _UpperCAmelCase         =					batch_size
      _UpperCAmelCase         =					num_channels
      _UpperCAmelCase         =					image_size
      _UpperCAmelCase         =					min_resolution
      _UpperCAmelCase         =					max_resolution
      _UpperCAmelCase         =					do_resize
      _UpperCAmelCase         =					size
      _UpperCAmelCase         =					do_center_crop
      _UpperCAmelCase         =					crop_size
      _UpperCAmelCase         =					do_flip_channel_order


    def    lowerCAmelCase_     (    self		:     List[str]    ):
      return {
          "do_resize": self.do_resize,
          "size": self.size,
          "do_center_crop": self.do_center_crop,
          "crop_size": self.crop_size,
          "do_flip_channel_order": self.do_flip_channel_order,
      }



@require_torch
@require_vision
class 				a (						lowerCAmelCase_		,							unittest.TestCase      ):
    _snake_case   :     Optional[int]   							=							MobileViTImageProcessor if is_vision_available() else None


    def    lowerCAmelCase_     (    self		:     Optional[Any]    ):
      _UpperCAmelCase         =					MobileViTImageProcessingTester(self    )


    @property
    def    lowerCAmelCase_     (    self		:     Tuple    ):
      return self.image_processor_tester.prepare_image_processor_dict()


    def    lowerCAmelCase_     (    self		:     Dict    ):
      _UpperCAmelCase         =					self.image_processing_class(**self.image_processor_dict    )
      self.assertTrue(hasattr(__lowerCAmelCase      , """do_resize"""    )    )
      self.assertTrue(hasattr(__lowerCAmelCase      , """size"""    )    )
      self.assertTrue(hasattr(__lowerCAmelCase      , """do_center_crop"""    )    )
      self.assertTrue(hasattr(__lowerCAmelCase      , """center_crop"""    )    )
      self.assertTrue(hasattr(__lowerCAmelCase      , """do_flip_channel_order"""    )    )


    def    lowerCAmelCase_     (    self		:     Union[str, Any]    ):
      _UpperCAmelCase         =					self.image_processing_class.from_dict(self.image_processor_dict    )
      self.assertEqual(image_processor.size      , {"""shortest_edge""": 20}    )
      self.assertEqual(image_processor.crop_size      , {"""height""": 18, """width""": 18}    )

      _UpperCAmelCase         =					self.image_processing_class.from_dict(self.image_processor_dict      , size=42      , crop_size=84    )
      self.assertEqual(image_processor.size      , {"""shortest_edge""": 42}    )
      self.assertEqual(image_processor.crop_size      , {"""height""": 84, """width""": 84}    )


    def    lowerCAmelCase_     (    self		:     List[str]    ):
      pass


    def    lowerCAmelCase_     (    self		:     Dict    ):
      # Initialize image_processing
      _UpperCAmelCase         =					self.image_processing_class(**self.image_processor_dict    )
      # create random PIL images
      _UpperCAmelCase         =					prepare_image_inputs(self.image_processor_tester      , equal_resolution=__lowerCAmelCase    )
      for image in image_inputs:
        self.assertIsInstance(__lowerCAmelCase      , Image.Image    )

      # Test not batched input
      _UpperCAmelCase         =					image_processing(image_inputs[0]      , return_tensors="""pt"""    ).pixel_values
      self.assertEqual(
          encoded_images.shape      , (
              1,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size["""height"""],
              self.image_processor_tester.crop_size["""width"""],
          )      , )

      # Test batched
      _UpperCAmelCase         =					image_processing(__lowerCAmelCase      , return_tensors="""pt"""    ).pixel_values
      self.assertEqual(
          encoded_images.shape      , (
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size["""height"""],
              self.image_processor_tester.crop_size["""width"""],
          )      , )


    def    lowerCAmelCase_     (    self		:     str    ):
      # Initialize image_processing
      _UpperCAmelCase         =					self.image_processing_class(**self.image_processor_dict    )
      # create random numpy tensors
      _UpperCAmelCase         =					prepare_image_inputs(self.image_processor_tester      , equal_resolution=__lowerCAmelCase      , numpify=__lowerCAmelCase    )
      for image in image_inputs:
        self.assertIsInstance(__lowerCAmelCase      , np.ndarray    )

      # Test not batched input
      _UpperCAmelCase         =					image_processing(image_inputs[0]      , return_tensors="""pt"""    ).pixel_values
      self.assertEqual(
          encoded_images.shape      , (
              1,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size["""height"""],
              self.image_processor_tester.crop_size["""width"""],
          )      , )

      # Test batched
      _UpperCAmelCase         =					image_processing(__lowerCAmelCase      , return_tensors="""pt"""    ).pixel_values
      self.assertEqual(
          encoded_images.shape      , (
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size["""height"""],
              self.image_processor_tester.crop_size["""width"""],
          )      , )


    def    lowerCAmelCase_     (    self		:     Optional[int]    ):
      # Initialize image_processing
      _UpperCAmelCase         =					self.image_processing_class(**self.image_processor_dict    )
      # create random PyTorch tensors
      _UpperCAmelCase         =					prepare_image_inputs(self.image_processor_tester      , equal_resolution=__lowerCAmelCase      , torchify=__lowerCAmelCase    )
      for image in image_inputs:
        self.assertIsInstance(__lowerCAmelCase      , torch.Tensor    )

      # Test not batched input
      _UpperCAmelCase         =					image_processing(image_inputs[0]      , return_tensors="""pt"""    ).pixel_values
      self.assertEqual(
          encoded_images.shape      , (
              1,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size["""height"""],
              self.image_processor_tester.crop_size["""width"""],
          )      , )

      # Test batched
      _UpperCAmelCase         =					image_processing(__lowerCAmelCase      , return_tensors="""pt"""    ).pixel_values
      self.assertEqual(
          encoded_images.shape      , (
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size["""height"""],
              self.image_processor_tester.crop_size["""width"""],
          )      , )
