import json
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
from transformers import (
    CONFIG_MAPPING,
    FEATURE_EXTRACTOR_MAPPING,
    AutoConfig,
    AutoFeatureExtractor,
    WavaVecaConfig,
    WavaVecaFeatureExtractor,
)
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir


sys.path.append(str(Path(__file__).parent.parent.parent.parent / '''utils'''))

from test_module.custom_configuration import CustomConfig  # noqa E402
from test_module.custom_feature_extraction import CustomFeatureExtractor  # noqa E402


__lowerCamelCase      :			List[Any]							       =		get_tests_dir('''fixtures''')
__lowerCamelCase      :			Optional[int]							       =		get_tests_dir('''fixtures/dummy_feature_extractor_config.json''')
__lowerCamelCase      :			Any							       =		get_tests_dir('''fixtures/dummy-config.json''')







class __snake_case    (						unittest.TestCase		):




   def      __a						(   self      :    Optional[Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				0




   def      __a						(   self      :    List[Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				AutoFeatureExtractor.from_pretrained("""facebook/wav2vec2-base-960h"""				)
     self.assertIsInstance(_lowercase	,     _lowercase				)




   def      __a						(   self      :    Union[str, Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				AutoFeatureExtractor.from_pretrained(_lowercase				)
     self.assertIsInstance(_lowercase	,     _lowercase				)




   def      __a						(   self      :    Optional[int]				):



     """simple docstring"""






     with tempfile.TemporaryDirectory() as tmpdirname:
       SCREAMING_SNAKE_CASE__      							=				WavaVecaConfig()

       # remove feature_extractor_type to make sure config.json alone is enough to load feature processor locally
       SCREAMING_SNAKE_CASE__      							=				AutoFeatureExtractor.from_pretrained(_lowercase				).to_dict()

       config_dict.pop("""feature_extractor_type"""				)
       SCREAMING_SNAKE_CASE__      							=				WavaVecaFeatureExtractor(**_lowercase				)

       # save in new folder
       model_config.save_pretrained(_lowercase				)
       config.save_pretrained(_lowercase				)

       SCREAMING_SNAKE_CASE__      							=				AutoFeatureExtractor.from_pretrained(_lowercase				)

       # make sure private variable is not incorrectly saved
       SCREAMING_SNAKE_CASE__      							=				json.loads(config.to_json_string()				)
       self.assertTrue("""_processor_class""" not in dict_as_saved				)

     self.assertIsInstance(_lowercase	,     _lowercase				)




   def      __a						(   self      :    Optional[Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				AutoFeatureExtractor.from_pretrained(_lowercase				)
     self.assertIsInstance(_lowercase	,     _lowercase				)




   def      __a						(   self      :    Dict				):



     """simple docstring"""






     with self.assertRaisesRegex(
         _lowercase	,     """bert-base is not a local folder and is not a valid model identifier"""				):
       SCREAMING_SNAKE_CASE__      							=				AutoFeatureExtractor.from_pretrained("""bert-base"""				)




   def      __a						(   self      :    Union[str, Any]				):



     """simple docstring"""






     with self.assertRaisesRegex(
         _lowercase	,     R"""aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"""				):
       SCREAMING_SNAKE_CASE__      							=				AutoFeatureExtractor.from_pretrained(_lowercase	,     revision="""aaaaaa"""				)




   def      __a						(   self      :    List[Any]				):



     """simple docstring"""






     with self.assertRaisesRegex(
         _lowercase	,     """hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json."""	,     ):
       SCREAMING_SNAKE_CASE__      							=				AutoFeatureExtractor.from_pretrained("""hf-internal-testing/config-no-model"""				)




   def      __a						(   self      :    str				):



     """simple docstring"""






     with self.assertRaises(_lowercase				):
       SCREAMING_SNAKE_CASE__      							=				AutoFeatureExtractor.from_pretrained(
           """hf-internal-testing/test_dynamic_feature_extractor"""				)
     # If remote code is disabled, we can't load this config.
     with self.assertRaises(_lowercase				):
       SCREAMING_SNAKE_CASE__      							=				AutoFeatureExtractor.from_pretrained(
           """hf-internal-testing/test_dynamic_feature_extractor"""	,     trust_remote_code=_lowercase				)

     SCREAMING_SNAKE_CASE__      							=				AutoFeatureExtractor.from_pretrained(
         """hf-internal-testing/test_dynamic_feature_extractor"""	,     trust_remote_code=_lowercase				)
     self.assertEqual(feature_extractor.__class__.__name__	,     """NewFeatureExtractor"""				)

     # Test feature extractor can be reloaded.
     with tempfile.TemporaryDirectory() as tmp_dir:
       feature_extractor.save_pretrained(_lowercase				)
       SCREAMING_SNAKE_CASE__      							=				AutoFeatureExtractor.from_pretrained(_lowercase	,     trust_remote_code=_lowercase				)
     self.assertEqual(reloaded_feature_extractor.__class__.__name__	,     """NewFeatureExtractor"""				)




   def      __a						(   self      :    Union[str, Any]				):



     """simple docstring"""






     try:
       AutoConfig.register("""custom"""	,     _lowercase				)
       AutoFeatureExtractor.register(_lowercase	,     _lowercase				)
       # Trying to register something existing in the Transformers library will raise an error
       with self.assertRaises(_lowercase				):
         AutoFeatureExtractor.register(_lowercase	,     _lowercase				)

       # Now that the config is registered, it can be used as any other config with the auto-API
       SCREAMING_SNAKE_CASE__      							=				CustomFeatureExtractor.from_pretrained(_lowercase				)
       with tempfile.TemporaryDirectory() as tmp_dir:
         feature_extractor.save_pretrained(_lowercase				)
         SCREAMING_SNAKE_CASE__      							=				AutoFeatureExtractor.from_pretrained(_lowercase				)
         self.assertIsInstance(_lowercase	,     _lowercase				)

     finally:
       if "custom" in CONFIG_MAPPING._extra_content:
         del CONFIG_MAPPING._extra_content["custom"]
       if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content:
         del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig]




   def      __a						(   self      :    Any				):



     """simple docstring"""






     class __snake_case    (						lowerCamelCase_		):
        lowerCAmelCase_														=							True

     try:
       AutoConfig.register("""custom"""	,     _lowercase				)
       AutoFeatureExtractor.register(_lowercase	,     _lowercase				)
       # If remote code is not set, the default is to use local
       SCREAMING_SNAKE_CASE__      							=				AutoFeatureExtractor.from_pretrained(
           """hf-internal-testing/test_dynamic_feature_extractor"""				)
       self.assertEqual(feature_extractor.__class__.__name__	,     """NewFeatureExtractor"""				)
       self.assertTrue(feature_extractor.is_local				)

       # If remote code is disabled, we load the local one.
       SCREAMING_SNAKE_CASE__      							=				AutoFeatureExtractor.from_pretrained(
           """hf-internal-testing/test_dynamic_feature_extractor"""	,     trust_remote_code=_lowercase				)
       self.assertEqual(feature_extractor.__class__.__name__	,     """NewFeatureExtractor"""				)
       self.assertTrue(feature_extractor.is_local				)

       # If remote is enabled, we load from the Hub
       SCREAMING_SNAKE_CASE__      							=				AutoFeatureExtractor.from_pretrained(
           """hf-internal-testing/test_dynamic_feature_extractor"""	,     trust_remote_code=_lowercase				)
       self.assertEqual(feature_extractor.__class__.__name__	,     """NewFeatureExtractor"""				)
       self.assertTrue(not hasattr(_lowercase	,     """is_local"""				)				)

     finally:
       if "custom" in CONFIG_MAPPING._extra_content:
         del CONFIG_MAPPING._extra_content["custom"]
       if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content:
         del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig]

import unittest

from .lib import (
    Matrix,
    Vector,
    axpy,
    square_zero_matrix,
    unit_basis_vector,
    zero_vector,
)







class __snake_case    (						unittest.TestCase		):




   def      __a						(   self      :    Dict				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Vector([1, 2, 3]				)
     self.assertEqual(x.component(0				)	,     1				)
     self.assertEqual(x.component(2				)	,     3				)
     SCREAMING_SNAKE_CASE__      							=				Vector()




   def      __a						(   self      :    int				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Vector([0, 0, 0, 0, 0, 1]				)
     self.assertEqual(str(_lowercase				)	,     """(0,0,0,0,0,1)"""				)




   def      __a						(   self      :    Tuple				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Vector([1, 2, 3, 4]				)
     self.assertEqual(len(_lowercase				)	,     4				)




   def      __a						(   self      :    Optional[Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Vector([1, 2]				)
     SCREAMING_SNAKE_CASE__      							=				Vector([1, 2, 3, 4, 5]				)
     SCREAMING_SNAKE_CASE__      							=				Vector([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]				)
     SCREAMING_SNAKE_CASE__      							=				Vector([1, -1, 1, -1, 2, -3, 4, -5]				)
     self.assertAlmostEqual(x.euclidean_length()	,     2.2_36	,     3				)
     self.assertAlmostEqual(y.euclidean_length()	,     7.4_16	,     3				)
     self.assertEqual(z.euclidean_length()	,     0				)
     self.assertAlmostEqual(w.euclidean_length()	,     7.6_16	,     3				)




   def      __a						(   self      :    Optional[int]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Vector([1, 2, 3]				)
     SCREAMING_SNAKE_CASE__      							=				Vector([1, 1, 1]				)
     self.assertEqual((x + y).component(0				)	,     2				)
     self.assertEqual((x + y).component(1				)	,     3				)
     self.assertEqual((x + y).component(2				)	,     4				)




   def      __a						(   self      :    Any				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Vector([1, 2, 3]				)
     SCREAMING_SNAKE_CASE__      							=				Vector([1, 1, 1]				)
     self.assertEqual((x - y).component(0				)	,     0				)
     self.assertEqual((x - y).component(1				)	,     1				)
     self.assertEqual((x - y).component(2				)	,     2				)




   def      __a						(   self      :    str				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Vector([1, 2, 3]				)
     SCREAMING_SNAKE_CASE__      							=				Vector([2, -1, 4]				)  # for test of dot product
     SCREAMING_SNAKE_CASE__      							=				Vector([1, -2, -1]				)
     self.assertEqual(str(x * 3.0				)	,     """(3.0,6.0,9.0)"""				)
     self.assertEqual((a * b)	,     0				)




   def      __a						(   self      :    Union[str, Any]				):



     """simple docstring"""






     self.assertEqual(str(zero_vector(10				)				).count("""0"""				)	,     10				)




   def      __a						(   self      :    str				):



     """simple docstring"""






     self.assertEqual(str(unit_basis_vector(3	,     1				)				)	,     """(0,1,0)"""				)




   def      __a						(   self      :    Any				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Vector([1, 2, 3]				)
     SCREAMING_SNAKE_CASE__      							=				Vector([1, 0, 1]				)
     self.assertEqual(str(axpy(2	,     _lowercase	,     _lowercase				)				)	,     """(3,4,7)"""				)




   def      __a						(   self      :    str				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Vector([1, 0, 0, 0, 0, 0]				)
     SCREAMING_SNAKE_CASE__      							=				x.copy()
     self.assertEqual(str(_lowercase				)	,     str(_lowercase				)				)




   def      __a						(   self      :    str				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Vector([1, 0, 0]				)
     x.change_component(0	,     0				)
     x.change_component(1	,     1				)
     self.assertEqual(str(_lowercase				)	,     """(0,1,0)"""				)




   def      __a						(   self      :    Optional[Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]	,     3	,     3				)
     self.assertEqual("""|1,2,3|\n|2,4,5|\n|6,7,8|\n"""	,     str(_lowercase				)				)




   def      __a						(   self      :    List[Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]	,     3	,     3				)
     SCREAMING_SNAKE_CASE__      							=				[[-3, -14, -10], [-5, -10, -5], [-2, -1, 0]]
     for x in range(a.height()				):
       for y in range(a.width()				):
         self.assertEqual(minors[x][y]	,     a.minor(_lowercase	,     _lowercase				)				)




   def      __a						(   self      :    Dict				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]	,     3	,     3				)
     SCREAMING_SNAKE_CASE__      							=				[[-3, 14, -10], [5, -10, 5], [-2, 1, 0]]
     for x in range(a.height()				):
       for y in range(a.width()				):
         self.assertEqual(cofactors[x][y]	,     a.cofactor(_lowercase	,     _lowercase				)				)




   def      __a						(   self      :    Union[str, Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]	,     3	,     3				)
     self.assertEqual(-5	,     a.determinant()				)




   def      __a						(   self      :    List[Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]	,     3	,     3				)
     SCREAMING_SNAKE_CASE__      							=				Vector([1, 2, 3]				)
     self.assertEqual("""(14,32,50)"""	,     str(a * x				)				)
     self.assertEqual("""|2,4,6|\n|8,10,12|\n|14,16,18|\n"""	,     str(a * 2				)				)




   def      __a						(   self      :    Optional[Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]	,     3	,     3				)
     a.change_component(0	,     2	,     5				)
     self.assertEqual("""|1,2,5|\n|2,4,5|\n|6,7,8|\n"""	,     str(_lowercase				)				)




   def      __a						(   self      :    Union[str, Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]	,     3	,     3				)
     self.assertEqual(7	,     a.component(2	,     1				)	,     0.01				)




   def      __a						(   self      :    List[Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]	,     3	,     3				)
     SCREAMING_SNAKE_CASE__      							=				Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 10]]	,     3	,     3				)
     self.assertEqual("""|2,4,10|\n|4,8,10|\n|12,14,18|\n"""	,     str(a + b				)				)




   def      __a						(   self      :    Optional[Any]				):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__      							=				Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]	,     3	,     3				)
     SCREAMING_SNAKE_CASE__      							=				Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 10]]	,     3	,     3				)
     self.assertEqual("""|0,0,-4|\n|0,0,0|\n|0,0,-2|\n"""	,     str(a - b				)				)




   def      __a						(   self      :    Any				):



     """simple docstring"""






     self.assertEqual(
         """|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n"""	,     str(square_zero_matrix(5				)				)	,     )


if __name__ == "__main__":
   unittest.main()
