


import logging

import numpy as np
import pytest
from scipy.linalg import eigh

logging.basicConfig(level=logging.INFO, format='%(message)s')

def 						_UpperCAmelCase				(			SCREAMING_SNAKE_CASE__			:					np.ndarray				):
     return input_array.reshape((input_array.size, 1)				)

def 						_UpperCAmelCase				(			SCREAMING_SNAKE_CASE__			:					np.ndarray			,     SCREAMING_SNAKE_CASE__			:					np.ndarray			,     SCREAMING_SNAKE_CASE__			:					int				):
     __UpperCamelCase    							=np.nan
     for i in range(SCREAMING_SNAKE_CASE__				):
          __UpperCamelCase    							=features[:, labels == i]
          __UpperCamelCase    							=data.mean(1				)
          # Centralize the data of class i
          __UpperCamelCase    							=data - column_reshape(SCREAMING_SNAKE_CASE__				)
          if i > 0:
               # If covariance_sum is not None
               covariance_sum += np.dot(SCREAMING_SNAKE_CASE__			,     centered_data.T				)
          else:
               # If covariance_sum is np.nan (i.e. first loop)
               __UpperCamelCase    							=np.dot(SCREAMING_SNAKE_CASE__			,     centered_data.T				)

     return covariance_sum / features.shape[1]

def 						_UpperCAmelCase				(			SCREAMING_SNAKE_CASE__			:					np.ndarray			,     SCREAMING_SNAKE_CASE__			:					np.ndarray			,     SCREAMING_SNAKE_CASE__			:					int				):
     __UpperCamelCase    							=features.mean(1				)
     __UpperCamelCase    							=np.nan
     for i in range(SCREAMING_SNAKE_CASE__				):
          __UpperCamelCase    							=features[:, labels == i]
          __UpperCamelCase    							=data.shape[1]
          __UpperCamelCase    							=data.mean(1				)
          if i > 0:
               # If covariance_sum is not None
               covariance_sum += device_data * np.dot(
                   column_reshape(SCREAMING_SNAKE_CASE__				) - column_reshape(SCREAMING_SNAKE_CASE__				)			,     (column_reshape(SCREAMING_SNAKE_CASE__				) - column_reshape(SCREAMING_SNAKE_CASE__				)).T			,     )
          else:
               # If covariance_sum is np.nan (i.e. first loop)
               __UpperCamelCase    							=device_data * np.dot(
                   column_reshape(SCREAMING_SNAKE_CASE__				) - column_reshape(SCREAMING_SNAKE_CASE__				)			,     (column_reshape(SCREAMING_SNAKE_CASE__				) - column_reshape(SCREAMING_SNAKE_CASE__				)).T			,     )

     return covariance_sum / features.shape[1]

def 						_UpperCAmelCase				(			SCREAMING_SNAKE_CASE__			:					np.ndarray			,     SCREAMING_SNAKE_CASE__			:					int				):

     # Check if the features have been loaded
     if features.any():
          __UpperCamelCase    							=features.mean(1				)
          # Center the dataset
          __UpperCamelCase    							=features - np.reshape(SCREAMING_SNAKE_CASE__			,     (data_mean.size, 1)				)
          __UpperCamelCase    							=np.dot(SCREAMING_SNAKE_CASE__			,     centered_data.T				) / features.shape[1]
          __UpperCamelCase							,      __UpperCamelCase    							=np.linalg.eigh(SCREAMING_SNAKE_CASE__				)
          # Take all the columns in the reverse order (-1), and then takes only the first
          __UpperCamelCase    							=eigenvectors[:, ::-1][:, 0:dimensions]
          # Project the database on the new space
          __UpperCamelCase    							=np.dot(filtered_eigenvectors.T			,     SCREAMING_SNAKE_CASE__				)
          logging.info('Principal Component Analysis computed'				)

          return projected_data
     else:
          logging.basicConfig(level=logging.ERROR			,     format='%(message)s'			,     force=SCREAMING_SNAKE_CASE__				)
          logging.error('Dataset empty'				)
          raise AssertionError

def 						_UpperCAmelCase				(			SCREAMING_SNAKE_CASE__			:					np.ndarray			,     SCREAMING_SNAKE_CASE__			:					np.ndarray			,     SCREAMING_SNAKE_CASE__			:					int			,     SCREAMING_SNAKE_CASE__			:					int				):
     assert classes > dimensions

     # Check if features have been already loaded
     if features.any:
          __UpperCamelCase							,      __UpperCamelCase    							=eigh(
              covariance_between_classes(SCREAMING_SNAKE_CASE__			,     SCREAMING_SNAKE_CASE__			,     SCREAMING_SNAKE_CASE__				)			,     covariance_within_classes(SCREAMING_SNAKE_CASE__			,     SCREAMING_SNAKE_CASE__			,     SCREAMING_SNAKE_CASE__				)			,     )
          __UpperCamelCase    							=eigenvectors[:, ::-1][:, :dimensions]
          __UpperCamelCase							,      __UpperCamelCase							,      __UpperCamelCase    							=np.linalg.svd(SCREAMING_SNAKE_CASE__				)
          __UpperCamelCase    							=svd_matrix[:, 0:dimensions]
          __UpperCamelCase    							=np.dot(filtered_svd_matrix.T			,     SCREAMING_SNAKE_CASE__				)
          logging.info('Linear Discriminant Analysis computed'				)

          return projected_data
     else:
          logging.basicConfig(level=logging.ERROR			,     format='%(message)s'			,     force=SCREAMING_SNAKE_CASE__				)
          logging.error('Dataset empty'				)
          raise AssertionError

def 						_UpperCAmelCase				(			):
     # Create dummy dataset with 2 classes and 3 features
     __UpperCamelCase    							=np.array([[1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [3, 4, 5, 6, 7]]				)
     __UpperCamelCase    							=np.array([0, 0, 0, 1, 1]				)
     __UpperCamelCase    							=2
     __UpperCamelCase    							=2

     # Assert that the function raises an AssertionError if dimensions > classes
     with pytest.raises(SCREAMING_SNAKE_CASE__				) as error_info:
          __UpperCamelCase    							=linear_discriminant_analysis(
              SCREAMING_SNAKE_CASE__			,     SCREAMING_SNAKE_CASE__			,     SCREAMING_SNAKE_CASE__			,     SCREAMING_SNAKE_CASE__				)
          if isinstance(SCREAMING_SNAKE_CASE__			,     np.ndarray				):
               raise AssertionError(
                   'Did not raise AssertionError for dimensions > classes'				)
          assert error_info.type is AssertionError

def 						_UpperCAmelCase				(			):
     __UpperCamelCase    							=np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]				)
     __UpperCamelCase    							=2
     __UpperCamelCase    							=np.array([[6.92820323, 8.66025404, 10.39230485], [3.0, 3.0, 3.0]]				)

     with pytest.raises(SCREAMING_SNAKE_CASE__				) as error_info:
          __UpperCamelCase    							=principal_component_analysis(SCREAMING_SNAKE_CASE__			,     SCREAMING_SNAKE_CASE__				)
          if not np.allclose(SCREAMING_SNAKE_CASE__			,     SCREAMING_SNAKE_CASE__				):
               raise AssertionError
          assert error_info.type is AssertionError


if __name__ == "__main__":
   import doctest

   doctest.testmod()









import os
import unittest

from transformers.models.transfo_xl.tokenization_transfo_xl import VOCAB_FILES_NAMES, TransfoXLTokenizer

from ...test_tokenization_common import TokenizerTesterMixin






class     UpperCAmelCase__       (					A_   ,  unittest.TestCase		):



    """simple docstring"""






    UpperCAmelCase__						:		Tuple      =  TransfoXLTokenizer
    UpperCAmelCase__						:		str      =  False
    UpperCAmelCase__						:		Tuple      =  False






    def _a					(			self    )  ->       Union[str, Any]:
         super().setUp()

         __UpperCamelCase    							=[
             '<unk>',
             '[CLS]',
             '[SEP]',
             'want',
             'unwanted',
             'wa',
             'un',
             'running',
             ',',
             'low',
             'l',
         ]
         __UpperCamelCase    							=os.path.join(self.tmpdirname					,   VOCAB_FILES_NAMES['vocab_file']    )
         with open(self.vocab_file					,   'w'					,   encoding='utf-8'    ) as vocab_writer:
              vocab_writer.write(''.join([x + '\n' for x in vocab_tokens]    )    )






    def _a					(			self					,   **A_    )  ->       Optional[int]:
         __UpperCamelCase    							=True
         return TransfoXLTokenizer.from_pretrained(self.tmpdirname					,   **A_    )






    def _a					(			self					,   A_    )  ->       Tuple:
         __UpperCamelCase    							='<unk> UNwanted , running'
         __UpperCamelCase    							='<unk> unwanted, running'
         return input_text, output_text






    def _a					(			self    )  ->       str:
         __UpperCamelCase    							=TransfoXLTokenizer(vocab_file=self.vocab_file					,   lower_case=A_    )

         __UpperCamelCase    							=tokenizer.tokenize('<unk> UNwanted , running'    )
         self.assertListEqual(A_					,   ['<unk>', 'unwanted', ',', 'running']    )

         self.assertListEqual(tokenizer.convert_tokens_to_ids(A_    )					,   [0, 4, 8, 7]    )






    def _a					(			self    )  ->       Any:
         __UpperCamelCase    							=TransfoXLTokenizer(lower_case=A_    )

         self.assertListEqual(
             tokenizer.tokenize(' \tHeLLo ! how  \n Are yoU ?  '    )					,   ['hello', '!', 'how', 'are', 'you', '?']    )






    def _a					(			self    )  ->       Optional[int]:
         __UpperCamelCase    							=TransfoXLTokenizer(lower_case=A_    )

         self.assertListEqual(
             tokenizer.tokenize(' \tHeLLo ! how  \n Are yoU ?  '    )					,   ['HeLLo', '!', 'how', 'Are', 'yoU', '?']    )






    def _a					(			self    )  ->       int:
         __UpperCamelCase    							=TransfoXLTokenizer(lower_case=A_    )
         __UpperCamelCase    							='Hello (bracket) and side-scrolled [and] Henry\'s $5,000 with 3.34 m. What\'s up!?'
         __UpperCamelCase    							=[
             'Hello',
             '(',
             'bracket',
             ')',
             'and',
             'side',
             '@-@',
             'scrolled',
             '[',
             'and',
             ']',
             'Henry',
             '\'s',
             '$',
             '5',
             '@,@',
             '000',
             'with',
             '3',
             '@.@',
             '34',
             'm',
             '.',
             'What',
             '\'s',
             'up',
             '!',
             '?',
         ]

         self.assertListEqual(tokenizer.tokenize(A_    )					,   A_    )

         self.assertEqual(tokenizer.convert_tokens_to_string(A_    )					,   A_    )






    def _a					(			self    )  ->       Optional[int]:
         __UpperCamelCase    							=self.get_tokenizer()
         __UpperCamelCase    							=len(A_    )

         tokenizer.add_tokens(['new1', 'new2']    )
         tokenizer.move_added_token('new1'					,   1    )

         # Check that moved token is not copied (duplicate)
         self.assertEqual(len(A_    )					,   original_len + 2    )
         # Check that token is moved to specified id
         self.assertEqual(tokenizer.encode('new1'    )					,   [1]    )
         self.assertEqual(tokenizer.decode([1]    )					,   'new1'    )





