
"""simple docstring"""



import logging
import os

from .state import PartialState




class 		a_    (			logging.LoggerAdapter						):







    @staticmethod
    def   _snake_case       (      __UpperCamelCase      :							Any  )		->str:






        '''simple docstring'''






        _UpperCAmelCase        = PartialState()
        return not main_process_only or (main_process_only and state.is_main_process)







    def   _snake_case       (      self      :							List[str]  ,       __UpperCamelCase      :							Any  ,       __UpperCamelCase      :							List[str]  ,       *__UpperCamelCase      :							List[str]  ,       **__UpperCamelCase      :							Optional[Any]  )		->str:






        '''simple docstring'''






        if PartialState._shared_state == {}:
            raise RuntimeError(
                """You must initialize the accelerate state by calling either `PartialState()` or `Accelerator()` before using the logging utility."""  )
        _UpperCAmelCase        = kwargs.pop("""main_process_only"""  ,       __UpperCamelCase  )
        _UpperCAmelCase        = kwargs.pop("""in_order"""  ,       __UpperCamelCase  )

        if self.isEnabledFor(__UpperCamelCase  ):
            if self._should_log(__UpperCamelCase  ):
                _UpperCAmelCase     ,_UpperCAmelCase        = self.process(__UpperCamelCase  ,       __UpperCamelCase  )
                self.logger.log(__UpperCamelCase  ,       __UpperCamelCase  ,       *__UpperCamelCase  ,       **__UpperCamelCase  )

            elif in_order:
                _UpperCAmelCase        = PartialState()
                for i in range(state.num_processes  ):
                    if i == state.process_index:
                        _UpperCAmelCase     ,_UpperCAmelCase        = self.process(__UpperCamelCase  ,       __UpperCamelCase  )
                        self.logger.log(__UpperCamelCase  ,       __UpperCamelCase  ,       *__UpperCamelCase  ,       **__UpperCamelCase  )
                    state.wait_for_everyone()







def 			_UpperCamelCase						(    _A       ,   _A = None   )     ->	str:





    """simple docstring"""

    if log_level is None:
        _UpperCAmelCase        = os.environ.get("""ACCELERATE_LOG_LEVEL"""       ,   _A   )
    _UpperCAmelCase        = logging.getLogger(_A   )
    if log_level is not None:
        logger.setLevel(log_level.upper()   )
        logger.root.setLevel(log_level.upper()   )
    return MultiProcessAdapter(_A       ,   {}   )

"""simple docstring"""



import math




class 		a_    :







    def   _snake_case       (      self      :							List[Any]  ,       __UpperCamelCase      :							list[list[float]]  ,       __UpperCamelCase      :							list[int]  )		->int:






        '''simple docstring'''






        _UpperCAmelCase        = 0.0
        _UpperCAmelCase        = 0.0
        for i in range(len(__UpperCamelCase  )  ):
            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   _snake_case       (      self      :							Dict  ,       __UpperCamelCase      :							list[list[int | float]]  ,       __UpperCamelCase      :							list[int]  ,       __UpperCamelCase      :							int  ,       __UpperCamelCase      :							float  )		->list[list[int | float]]:






        '''simple docstring'''






        for i in range(len(__UpperCamelCase  )  ):
            weights[j][i] += alpha * (sample[i] - weights[j][i])
        return weights







def 			_UpperCamelCase						(    )     ->	None:





    """simple docstring"""

    _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(_A   ):
        for j in range(len(_A   )   ):
            # training sample
            _UpperCAmelCase        = training_samples[j]

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

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

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

    # 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()