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
[((10, 3, 10, 22), 'os.path.dirname', 'os.path.dirname', ({(10, 19, 10, 21): 'fl'}, {}), '(fl)', False, 'import os\n'), ((14, 10, 14, 29), 'numpy.array', 'np.array', ({(14, 19, 14, 28): '[0, 1, 0, 1]'}, {}), '([0, 1, 0, 1])', True, 'import numpy as np\n'), ((15, 11, 15, 45), 'musket_core.coders.get_coder', 'coders.get_coder', ({(15, 28, 15, 36): '"""binary"""', (15, 37, 15, 38): 'a', (15, 40, 15, 44): 'None'}, {}), "('binary', a, None)", False, 'from musket_core import coders\n'), ((24, 10, 24, 37), 'numpy.array', 'np.array', ({(24, 19, 24, 36): "['0', '1', '0', '1']"}, {}), "(['0', '1', '0', '1'])", True, 'import numpy as np\n'), ((25, 11, 25, 45), 'musket_core.coders.get_coder', 'coders.get_coder', ({(25, 28, 25, 36): '"""binary"""', (25, 37, 25, 38): 'a', (25, 40, 25, 44): 'None'}, {}), "('binary', a, None)", False, 'from musket_core import coders\n'), ((34, 10, 34, 35), 'numpy.array', 'np.array', ({(34, 19, 34, 34): "['', '1', '', '1']"}, {}), "(['', '1', '', '1'])", True, 'import numpy as np\n'), ((35, 11, 35, 45), 'musket_core.coders.get_coder', 'coders.get_coder', ({(35, 28, 35, 36): '"""binary"""', (35, 37, 35, 38): 'a', (35, 40, 35, 44): 'None'}, {}), "('binary', a, None)", False, 'from musket_core import coders\n'), ((44, 10, 44, 43), 'numpy.array', 'np.array', ({(44, 19, 44, 42): '[True, False, True, False]'}, {}), '([True, False, True, False])', True, 'import numpy as np\n'), ((45, 11, 45, 45), 'musket_core.coders.get_coder', 'coders.get_coder', ({(45, 28, 45, 36): '"""binary"""', (45, 37, 45, 38): 'a', (45, 40, 45, 44): 'None'}, {}), "('binary', a, None)", False, 'from musket_core import coders\n'), ((55, 10, 55, 29), 'numpy.array', 'np.array', ({(55, 19, 55, 28): '[0, 1, 2, 1]'}, {}), '([0, 1, 2, 1])', True, 'import numpy as np\n'), ((56, 11, 56, 58), 'musket_core.coders.get_coder', 'coders.get_coder', ({(56, 28, 56, 49): '"""categorical_one_hot"""', (56, 50, 56, 51): 'a', (56, 53, 56, 57): 'None'}, {}), "('categorical_one_hot', a, None)", False, 'from musket_core import coders\n'), ((67, 10, 67, 37), 'numpy.array', 'np.array', ({(67, 19, 67, 36): "['a', 'b', 'c', 'b']"}, {}), "(['a', 'b', 'c', 'b'])", True, 'import numpy as np\n'), ((68, 11, 68, 58), 'musket_core.coders.get_coder', 'coders.get_coder', ({(68, 28, 68, 49): '"""categorical_one_hot"""', (68, 50, 68, 51): 'a', (68, 53, 68, 57): 'None'}, {}), "('categorical_one_hot', a, None)", False, 'from musket_core import coders\n'), ((79, 10, 79, 36), 'numpy.array', 'np.array', ({(79, 19, 79, 35): "['', 'b', 'c', 'b']"}, {}), "(['', 'b', 'c', 'b'])", True, 'import numpy as np\n'), ((80, 11, 80, 58), 'musket_core.coders.get_coder', 'coders.get_coder', ({(80, 28, 80, 49): '"""categorical_one_hot"""', (80, 50, 80, 51): 'a', (80, 53, 80, 57): 'None'}, {}), "('categorical_one_hot', a, None)", False, 'from musket_core import coders\n'), ((91, 10, 91, 36), 'numpy.array', 'np.array', ({(91, 19, 91, 35): '[math.nan, 1, 2, 1]'}, {}), '([math.nan, 1, 2, 1])', True, 'import numpy as np\n'), ((92, 11, 92, 58), 'musket_core.coders.get_coder', 'coders.get_coder', ({(92, 28, 92, 49): '"""categorical_one_hot"""', (92, 50, 92, 51): 'a', (92, 53, 92, 57): 'None'}, {}), "('categorical_one_hot', a, None)", False, 'from musket_core import coders\n'), ((103, 10, 103, 40), 'numpy.array', 'np.array', ({(103, 19, 103, 39): "['1 2', '0 2', '0', '']"}, {}), "(['1 2', '0 2', '0', ''])", True, 'import numpy as np\n'), ((104, 11, 104, 50), 'musket_core.coders.get_coder', 'coders.get_coder', ({(104, 28, 104, 41): '"""multi_class"""', (104, 42, 104, 43): 'a', (104, 45, 104, 49): 'None'}, {}), "('multi_class', a, None)", False, 'from musket_core import coders\n'), ((115, 10, 115, 40), 'numpy.array', 'np.array', ({(115, 19, 115, 39): "['1_2', '0_2', '0', '']"}, {}), "(['1_2', '0_2', '0', ''])", True, 'import numpy as np\n'), ((116, 11, 116, 50), 'musket_core.coders.get_coder', 'coders.get_coder', ({(116, 28, 116, 41): '"""multi_class"""', (116, 42, 116, 43): 'a', (116, 45, 116, 49): 'None'}, {}), "('multi_class', a, None)", False, 'from musket_core import coders\n'), ((127, 10, 127, 34), 'numpy.array', 'np.array', ({(127, 19, 127, 33): "['1', '', '', '']"}, {}), "(['1', '', '', ''])", True, 'import numpy as np\n'), ((128, 11, 128, 50), 'musket_core.coders.get_coder', 'coders.get_coder', ({(128, 28, 128, 41): '"""multi_class"""', (128, 42, 128, 43): 'a', (128, 45, 128, 49): 'None'}, {}), "('multi_class', a, None)", False, 'from musket_core import coders\n'), ((18, 21, 18, 36), 'numpy.array', 'np.array', ({(18, 30, 18, 35): '[0.6]'}, {}), '([0.6])', True, 'import numpy as np\n'), ((20, 21, 20, 36), 'numpy.array', 'np.array', ({(20, 30, 20, 35): '[0.2]'}, {}), '([0.2])', True, 'import numpy as np\n'), ((28, 21, 28, 36), 'numpy.array', 'np.array', ({(28, 30, 28, 35): '[0.6]'}, {}), '([0.6])', True, 'import numpy as np\n'), ((30, 21, 30, 36), 'numpy.array', 'np.array', ({(30, 30, 30, 35): '[0.2]'}, {}), '([0.2])', True, 'import numpy as np\n'), ((38, 21, 38, 36), 'numpy.array', 'np.array', ({(38, 30, 38, 35): '[0.6]'}, {}), '([0.6])', True, 'import numpy as np\n'), ((40, 21, 40, 36), 'numpy.array', 'np.array', ({(40, 30, 40, 35): '[0.2]'}, {}), '([0.2])', True, 'import numpy as np\n'), ((48, 21, 48, 36), 'numpy.array', 'np.array', ({(48, 30, 48, 35): '[0.6]'}, {}), '([0.6])', True, 'import numpy as np\n'), ((50, 21, 50, 36), 'numpy.array', 'np.array', ({(50, 30, 50, 35): '[0.2]'}, {}), '([0.2])', True, 'import numpy as np\n'), ((60, 21, 60, 45), 'numpy.array', 'np.array', ({(60, 30, 60, 44): '[0.3, 0.4, 0.45]'}, {}), '([0.3, 0.4, 0.45])', True, 'import numpy as np\n'), ((62, 21, 62, 44), 'numpy.array', 'np.array', ({(62, 30, 62, 43): '[0.2, 0.1, 0.1]'}, {}), '([0.2, 0.1, 0.1])', True, 'import numpy as np\n'), ((72, 21, 72, 45), 'numpy.array', 'np.array', ({(72, 30, 72, 44): '[0.3, 0.4, 0.45]'}, {}), '([0.3, 0.4, 0.45])', True, 'import numpy as np\n'), ((74, 21, 74, 44), 'numpy.array', 'np.array', ({(74, 30, 74, 43): '[0.2, 0.1, 0.1]'}, {}), '([0.2, 0.1, 0.1])', True, 'import numpy as np\n'), ((84, 21, 84, 45), 'numpy.array', 'np.array', ({(84, 30, 84, 44): '[0.3, 0.4, 0.45]'}, {}), '([0.3, 0.4, 0.45])', True, 'import numpy as np\n'), ((86, 21, 86, 44), 'numpy.array', 'np.array', ({(86, 30, 86, 43): '[0.2, 0.1, 0.1]'}, {}), '([0.2, 0.1, 0.1])', True, 'import numpy as np\n'), ((96, 21, 96, 45), 'numpy.array', 'np.array', ({(96, 30, 96, 44): '[0.3, 0.4, 0.45]'}, {}), '([0.3, 0.4, 0.45])', True, 'import numpy as np\n'), ((97, 25, 97, 38), 'math.isnan', 'math.isnan', ({(97, 36, 97, 37): 'v'}, {}), '(v)', False, 'import math\n'), ((98, 21, 98, 44), 'numpy.array', 'np.array', ({(98, 30, 98, 43): '[0.2, 0.1, 0.1]'}, {}), '([0.2, 0.1, 0.1])', True, 'import numpy as np\n'), ((107, 31, 107, 58), 'numpy.array', 'np.array', ({(107, 40, 107, 57): '[False, True, True]'}, {}), '([False, True, True])', True, 'import numpy as np\n'), ((119, 31, 119, 58), 'numpy.array', 'np.array', ({(119, 40, 119, 57): '[False, True, True]'}, {}), '([False, True, True])', True, 'import numpy as np\n'), ((131, 31, 131, 47), 'numpy.array', 'np.array', ({(131, 40, 131, 46): '[True]'}, {}), '([True])', True, 'import numpy as np\n')]