import unittest
from musket_core import coders
import numpy as np
import pandas as pd
import os
import math

fl=__file__

fl=os.path.dirname(fl)
class TestCoders(unittest.TestCase):

    def test_binary_num(self):
        a=np.array([0,1,0,1])
        bc=coders.get_coder("binary",a, None)
        self.assertEqual(bc[0], 0, "should be zero")
        self.assertEqual(bc[1], 1, "should be one")
        v=bc._decode(np.array([0.6]))
        self.assertEqual(v, 1, "should be one")
        v=bc._decode(np.array([0.2]))
        self.assertEqual(v, 0, "should be zero")
        pass
    def test_binary_str(self):
        a=np.array(["0","1","0","1"])
        bc=coders.get_coder("binary",a, None)
        self.assertEqual(bc[0], 0, "should be zero")
        self.assertEqual(bc[1], 1, "should be one")
        v=bc._decode(np.array([0.6]))
        self.assertEqual(v, "1", "should be one")
        v=bc._decode(np.array([0.2]))
        self.assertEqual(v, "0", "should be zero")
        pass
    def test_binary_str2(self):
        a=np.array(["","1","","1"])
        bc=coders.get_coder("binary",a, None)
        self.assertEqual(bc[0], 0, "should be zero")
        self.assertEqual(bc[1], 1, "should be one")
        v=bc._decode(np.array([0.6]))
        self.assertEqual(v, "1", "should be one")
        v=bc._decode(np.array([0.2]))
        self.assertEqual(v, "", "should be zero")
        pass
    def test_binary_bool(self):
        a=np.array([True,False,True,False])
        bc=coders.get_coder("binary",a, None)
        self.assertEqual(bc[0], 1, "should be zero")
        self.assertEqual(bc[1], 0, "should be one")
        v=bc._decode(np.array([0.6]))
        self.assertEqual(v, True, "should be one")
        v=bc._decode(np.array([0.2]))
        self.assertEqual(v, False, "should be zero")
        pass
    
    def test_categorical_num(self):
        a=np.array([0,1,2,1])
        bc=coders.get_coder("categorical_one_hot",a, None)
        
        self.assertEqual(bc[0][0], True, "should be zero")
        self.assertEqual(bc[0][1], False, "should be one")
        v=bc._decode(np.array([0.3,0.4,0.45]))
        self.assertEqual(v, 2, "should be one")
        v=bc._decode(np.array([0.2,0.1,0.1]))
        self.assertEqual(v, 0, "should be zero")
        pass
    
    def test_categorical_str(self):
        a=np.array(["a","b","c","b"])
        bc=coders.get_coder("categorical_one_hot",a, None)
        
        self.assertEqual(bc[0][0], True, "should be zero")
        self.assertEqual(bc[0][1], False, "should be one")
        v=bc._decode(np.array([0.3,0.4,0.45]))
        self.assertEqual(v, "c", "should be one")
        v=bc._decode(np.array([0.2,0.1,0.1]))
        self.assertEqual(v, "a", "should be zero")
        pass
    
    def test_categorical_str2(self):
        a=np.array(["","b","c","b"])
        bc=coders.get_coder("categorical_one_hot",a, None)
        
        self.assertEqual(bc[0][0], True, "should be zero")
        self.assertEqual(bc[0][1], False, "should be one")
        v=bc._decode(np.array([0.3,0.4,0.45]))
        self.assertEqual(v, "c", "should be one")
        v=bc._decode(np.array([0.2,0.1,0.1]))
        self.assertEqual(v, "", "should be zero")
        pass
    
    def test_categorical_pd(self):
        a=np.array([math.nan,1,2,1])
        bc=coders.get_coder("categorical_one_hot",a, None)
        
        self.assertEqual(bc[0][2], True, "should be zero")
        self.assertEqual(bc[0][1], False, "should be one")
        v=bc._decode(np.array([0.3,0.4,0.45]))
        self.assertEqual(math.isnan(v),True, "should be one")
        v=bc._decode(np.array([0.2,0.1,0.1]))
        self.assertEqual(v, 1, "should be zero")
        pass
    
    def test_multiclass(self):
        a=np.array(["1 2","0 2","0",""])
        bc=coders.get_coder("multi_class",a, None)
        
        val=bc[0]
        self.assertEqual((val==np.array([False,True,True])).sum(), 3,"Fixing format")
        for i in range(len(a)):
            val=bc[i]
            r=bc._decode(val)
            self.assertEqual(r, a[i], "Decoding should work also")
        pass
    
    def test_multiclass1(self):
        a=np.array(["1_2","0_2","0",""])
        bc=coders.get_coder("multi_class",a, None)
        
        val=bc[0]
        self.assertEqual((val==np.array([False,True,True])).sum(), 3,"Fixing format")
        for i in range(len(a)):
            val=bc[i]
            r=bc._decode(val)
            self.assertEqual(r, a[i], "Decoding should work also")
        pass
    
    def test_multiclass2(self):
        a=np.array(["1","","",""])
        bc=coders.get_coder("multi_class",a, None)
        
        val=bc[0]
        self.assertEqual((val==np.array([True])).sum(), 1,"Fixing format")
        for i in range(len(a)):
            val=bc[i]
            r=bc._decode(val)
            self.assertEqual(r, a[i], "Decoding should work also")
        pass
[((134, 153), 'os.path.dirname', 'os.path.dirname', (['fl'], {}), '(fl)\n', (149, 153), False, 'import os\n'), ((237, 259), 'numpy.array', 'np.array', (['[0, 1, 0, 1]'], {}), '([0, 1, 0, 1])\n', (245, 259), True, 'import numpy as np\n'), ((269, 304), 'musket_core.coders.get_coder', 'coders.get_coder', (['"""binary"""', 'a', 'None'], {}), "('binary', a, None)\n", (285, 304), False, 'from musket_core import coders\n'), ((645, 675), 'numpy.array', 'np.array', (["['0', '1', '0', '1']"], {}), "(['0', '1', '0', '1'])\n", (653, 675), True, 'import numpy as np\n'), ((685, 720), 'musket_core.coders.get_coder', 'coders.get_coder', (['"""binary"""', 'a', 'None'], {}), "('binary', a, None)\n", (701, 720), False, 'from musket_core import coders\n'), ((1066, 1094), 'numpy.array', 'np.array', (["['', '1', '', '1']"], {}), "(['', '1', '', '1'])\n", (1074, 1094), True, 'import numpy as np\n'), ((1104, 1139), 'musket_core.coders.get_coder', 'coders.get_coder', (['"""binary"""', 'a', 'None'], {}), "('binary', a, None)\n", (1120, 1139), False, 'from musket_core import coders\n'), ((1484, 1520), 'numpy.array', 'np.array', (['[True, False, True, False]'], {}), '([True, False, True, False])\n', (1492, 1520), True, 'import numpy as np\n'), ((1530, 1565), 'musket_core.coders.get_coder', 'coders.get_coder', (['"""binary"""', 'a', 'None'], {}), "('binary', a, None)\n", (1546, 1565), False, 'from musket_core import coders\n'), ((1924, 1946), 'numpy.array', 'np.array', (['[0, 1, 2, 1]'], {}), '([0, 1, 2, 1])\n', (1932, 1946), True, 'import numpy as np\n'), ((1956, 2004), 'musket_core.coders.get_coder', 'coders.get_coder', (['"""categorical_one_hot"""', 'a', 'None'], {}), "('categorical_one_hot', a, None)\n", (1972, 2004), False, 'from musket_core import coders\n'), ((2396, 2426), 'numpy.array', 'np.array', (["['a', 'b', 'c', 'b']"], {}), "(['a', 'b', 'c', 'b'])\n", (2404, 2426), True, 'import numpy as np\n'), ((2436, 2484), 'musket_core.coders.get_coder', 'coders.get_coder', (['"""categorical_one_hot"""', 'a', 'None'], {}), "('categorical_one_hot', a, None)\n", (2452, 2484), False, 'from musket_core import coders\n'), ((2881, 2910), 'numpy.array', 'np.array', (["['', 'b', 'c', 'b']"], {}), "(['', 'b', 'c', 'b'])\n", (2889, 2910), True, 'import numpy as np\n'), ((2920, 2968), 'musket_core.coders.get_coder', 'coders.get_coder', (['"""categorical_one_hot"""', 'a', 'None'], {}), "('categorical_one_hot', a, None)\n", (2936, 2968), False, 'from musket_core import coders\n'), ((3362, 3391), 'numpy.array', 'np.array', (['[math.nan, 1, 2, 1]'], {}), '([math.nan, 1, 2, 1])\n', (3370, 3391), True, 'import numpy as np\n'), ((3401, 3449), 'musket_core.coders.get_coder', 'coders.get_coder', (['"""categorical_one_hot"""', 'a', 'None'], {}), "('categorical_one_hot', a, None)\n", (3417, 3449), False, 'from musket_core import coders\n'), ((3850, 3883), 'numpy.array', 'np.array', (["['1 2', '0 2', '0', '']"], {}), "(['1 2', '0 2', '0', ''])\n", (3858, 3883), True, 'import numpy as np\n'), ((3893, 3933), 'musket_core.coders.get_coder', 'coders.get_coder', (['"""multi_class"""', 'a', 'None'], {}), "('multi_class', a, None)\n", (3909, 3933), False, 'from musket_core import coders\n'), ((4268, 4301), 'numpy.array', 'np.array', (["['1_2', '0_2', '0', '']"], {}), "(['1_2', '0_2', '0', ''])\n", (4276, 4301), True, 'import numpy as np\n'), ((4311, 4351), 'musket_core.coders.get_coder', 'coders.get_coder', (['"""multi_class"""', 'a', 'None'], {}), "('multi_class', a, None)\n", (4327, 4351), False, 'from musket_core import coders\n'), ((4686, 4713), 'numpy.array', 'np.array', (["['1', '', '', '']"], {}), "(['1', '', '', ''])\n", (4694, 4713), True, 'import numpy as np\n'), ((4723, 4763), 'musket_core.coders.get_coder', 'coders.get_coder', (['"""multi_class"""', 'a', 'None'], {}), "('multi_class', a, None)\n", (4739, 4763), False, 'from musket_core import coders\n'), ((433, 448), 'numpy.array', 'np.array', (['[0.6]'], {}), '([0.6])\n', (441, 448), True, 'import numpy as np\n'), ((521, 536), 'numpy.array', 'np.array', (['[0.2]'], {}), '([0.2])\n', (529, 536), True, 'import numpy as np\n'), ((849, 864), 'numpy.array', 'np.array', (['[0.6]'], {}), '([0.6])\n', (857, 864), True, 'import numpy as np\n'), ((939, 954), 'numpy.array', 'np.array', (['[0.2]'], {}), '([0.2])\n', (947, 954), True, 'import numpy as np\n'), ((1268, 1283), 'numpy.array', 'np.array', (['[0.6]'], {}), '([0.6])\n', (1276, 1283), True, 'import numpy as np\n'), ((1358, 1373), 'numpy.array', 'np.array', (['[0.2]'], {}), '([0.2])\n', (1366, 1373), True, 'import numpy as np\n'), ((1694, 1709), 'numpy.array', 'np.array', (['[0.6]'], {}), '([0.6])\n', (1702, 1709), True, 'import numpy as np\n'), ((1785, 1800), 'numpy.array', 'np.array', (['[0.2]'], {}), '([0.2])\n', (1793, 1800), True, 'import numpy as np\n'), ((2156, 2182), 'numpy.array', 'np.array', (['[0.3, 0.4, 0.45]'], {}), '([0.3, 0.4, 0.45])\n', (2164, 2182), True, 'import numpy as np\n'), ((2253, 2278), 'numpy.array', 'np.array', (['[0.2, 0.1, 0.1]'], {}), '([0.2, 0.1, 0.1])\n', (2261, 2278), True, 'import numpy as np\n'), ((2636, 2662), 'numpy.array', 'np.array', (['[0.3, 0.4, 0.45]'], {}), '([0.3, 0.4, 0.45])\n', (2644, 2662), True, 'import numpy as np\n'), ((2735, 2760), 'numpy.array', 'np.array', (['[0.2, 0.1, 0.1]'], {}), '([0.2, 0.1, 0.1])\n', (2743, 2760), True, 'import numpy as np\n'), ((3120, 3146), 'numpy.array', 'np.array', (['[0.3, 0.4, 0.45]'], {}), '([0.3, 0.4, 0.45])\n', (3128, 3146), True, 'import numpy as np\n'), ((3219, 3244), 'numpy.array', 'np.array', (['[0.2, 0.1, 0.1]'], {}), '([0.2, 0.1, 0.1])\n', (3227, 3244), True, 'import numpy as np\n'), ((3601, 3627), 'numpy.array', 'np.array', (['[0.3, 0.4, 0.45]'], {}), '([0.3, 0.4, 0.45])\n', (3609, 3627), True, 'import numpy as np\n'), ((3653, 3666), 'math.isnan', 'math.isnan', (['v'], {}), '(v)\n', (3663, 3666), False, 'import math\n'), ((3712, 3737), 'numpy.array', 'np.array', (['[0.2, 0.1, 0.1]'], {}), '([0.2, 0.1, 0.1])\n', (3720, 3737), True, 'import numpy as np\n'), ((3994, 4023), 'numpy.array', 'np.array', (['[False, True, True]'], {}), '([False, True, True])\n', (4002, 4023), True, 'import numpy as np\n'), ((4412, 4441), 'numpy.array', 'np.array', (['[False, True, True]'], {}), '([False, True, True])\n', (4420, 4441), True, 'import numpy as np\n'), ((4824, 4840), 'numpy.array', 'np.array', (['[True]'], {}), '([True])\n', (4832, 4840), True, 'import numpy as np\n')]