Beispiel #1
0
    def test_cross_function_d(self):
        """Test case d: outer-producting a vector and a sequence of vectors:
        Options:
        - first vector is a Categorical, second sequence of vectors is a numpy ndarray
        (dtype = object)
        - first vector is a Categorical, second sequence of vectors is a Categorical
        (where self.IS_AOA = True))
        """
        array_path = os.path.join(os.getcwd(), "tests/data/cross_d.mat")
        mat_contents = loadmat(file_name=array_path)
        result_1 = mat_contents["result1"]
        random_vec = Categorical(values=mat_contents["random_vec"])
        states = mat_contents["s"]
        for i in range(len(states)):
            states[i] = states[i].squeeze()

        # first way, where first array is a Categorical, second array is a numpy ndarray
        # (dtype = object)
        result_1a_py = random_vec.cross(states)
        self.assertTrue(np.isclose(result_1, result_1a_py).all())

        # second way, where first array is a Categorical, second array is a Categorical
        # (where self.IS_AOA = True)
        states = Categorical(values=states[0])
        result_1b_py = random_vec.cross(states)
        self.assertTrue(np.isclose(result_1, result_1b_py).all())
Beispiel #2
0
    def test_cross_function_b(self):
        """Test case b: outer-producting two vectors together:
        Options:
        - both vectors are stored in single Categorical (with two entries, where self.AoA == True)
        - first vector is a Categorical (self.AoA = False) and second array is a numpy ndarray
        (non-object array)
        - first vector is a Categorical, second vector is also Categorical
        """
        array_path = os.path.join(os.getcwd(), "tests/data/cross_b.mat")
        mat_contents = loadmat(file_name=array_path)
        result_1 = mat_contents["result1"]
        result_2 = mat_contents["result2"]

        # first way, where both arrays as stored as two entries in a single AoA Categorical
        states = Categorical(values=mat_contents["s"][0])
        result_1_py = states.cross()
        self.assertTrue(np.isclose(result_1, result_1_py).all())

        # second way (type 1), where first array is a Categorical, second array is a
        # straight numpy array
        states_firstFactor = Categorical(values=mat_contents["s"][0][0])
        states_secondFactor = mat_contents["s"][0][1]
        result_2a_py = states_firstFactor.cross(states_secondFactor)
        self.assertTrue(np.isclose(result_2, result_2a_py).all())

        # second way (type 2), where first array is a Categorical, second array
        # is another Categorical
        states_firstFactor = Categorical(values=mat_contents["s"][0][0])
        states_secondFactor = Categorical(values=mat_contents["s"][0][1])
        result_2b_py = states_firstFactor.cross(states_secondFactor)
        self.assertTrue(np.isclose(result_2, result_2b_py).all())
Beispiel #3
0
 def test_normalize_multi_factor(self):
     values_1 = np.random.rand(5)
     values_2 = np.random.rand(4, 3)
     values = np.array([values_1, values_2])
     c = Categorical(values=values)
     c.normalize()
     self.assertTrue(c.is_normalized())
Beispiel #4
0
    def test_dot_function_a(self):
        """ test with vectors and matrices, discrete state / outcomes """

        array_path = os.path.join(os.getcwd(), "tests/data/dot_a.mat")
        mat_contents = loadmat(file_name=array_path)

        A = mat_contents["A"]
        obs = mat_contents["o"]
        states = mat_contents["s"]
        states = np.array(states, dtype=object)

        result_1 = mat_contents["result1"]
        result_2 = mat_contents["result2"]
        result_3 = mat_contents["result3"]

        A = Categorical(values=A)
        result_1_py = A.dot(obs, return_numpy=True)
        self.assertTrue(np.isclose(result_1, result_1_py).all())

        result_2_py = A.dot(states, return_numpy=True)
        result_2_py = result_2_py.astype("float64")[:, np.newaxis]
        self.assertTrue(np.isclose(result_2, result_2_py).all())

        result_3_py = A.dot(states, dims_to_omit=[0], return_numpy=True)
        self.assertTrue(np.isclose(result_3, result_3_py).all())
Beispiel #5
0
 def test_copy(self):
     values = np.random.rand(3, 2)
     c = Categorical(values=values)
     c_copy = c.copy()
     self.assertTrue(np.array_equal(c_copy.values, c.values))
     c_copy.values = c_copy.values * 2
     self.assertFalse(np.array_equal(c_copy.values, c.values))
Beispiel #6
0
    def test_dot_function_e(self):
        """ CONTINUOUS states and outcomes, but add a final (fourth) hidden state factor """
        array_path = os.path.join(os.getcwd(), "tests/data/dot_e.mat")
        mat_contents = loadmat(file_name=array_path)

        A = mat_contents["A"]
        obs = mat_contents["o"]
        states = mat_contents["s"]
        states_array_version = np.empty(states.shape[1], dtype=object)
        for i in range(states.shape[1]):
            states_array_version[i] = states[0][i][0]

        result_1 = mat_contents["result1"]
        result_2 = mat_contents["result2"]
        result_3 = mat_contents["result3"]

        A = Categorical(values=A)
        result_1_py = A.dot(obs, return_numpy=True)
        self.assertTrue(np.isclose(result_1, result_1_py).all())

        result_2_py = A.dot(states_array_version, return_numpy=True)
        result_2_py = result_2_py.astype("float64")[:, np.newaxis]
        self.assertTrue(np.isclose(result_2, result_2_py).all())

        result_3_py = A.dot(states_array_version,
                            dims_to_omit=[0],
                            return_numpy=True)
        self.assertTrue(np.isclose(result_3, result_3_py).all())
Beispiel #7
0
 def test_multi_factor_init_values_expand(self):
     values_1 = np.random.rand(5)
     values_2 = np.random.rand(4)
     values = np.array([values_1, values_2])
     c = Categorical(values=values)
     self.assertEqual(c.shape, (2, ))
     self.assertEqual(c[0].shape, (5, 1))
     self.assertEqual(c[1].shape, (4, 1))
Beispiel #8
0
    def test_dot_function_c_Cat(self):
        """ test with vectors and matrices, discrete state / outcomes but with a
        third hidden state factor. Now, when arguments themselves are
        instances of Categorical
        """

        array_path = os.path.join(os.getcwd(), "tests/data/dot_c.mat")
        mat_contents = loadmat(file_name=array_path)

        A = mat_contents["A"]
        obs = Categorical(values=mat_contents["o"])
        states = mat_contents["s"]
        states_array_version = np.empty(states.shape[1], dtype=object)
        for i in range(states.shape[1]):
            states_array_version[i] = states[0][i][0]
        states_array_version = Categorical(values=states_array_version)
        result_1 = mat_contents["result1"]
        result_2 = mat_contents["result2"]
        result_3 = mat_contents["result3"]

        A = Categorical(values=A)
        result_1_py = A.dot(obs, return_numpy=True)
        self.assertTrue(np.isclose(result_1, result_1_py).all())

        result_2_py = A.dot(states_array_version, return_numpy=True)
        result_2_py = result_2_py.astype("float64")[:, np.newaxis]
        self.assertTrue(np.isclose(result_2, result_2_py).all())

        result_3_py = A.dot(states_array_version,
                            dims_to_omit=[0],
                            return_numpy=True)
        self.assertTrue(np.isclose(result_3, result_3_py).all())
Beispiel #9
0
    def test_cross_function_c(self):
        """Test case c: outer-producting a vector and a matrix together:
        Options:
        - first vector is a Categorical, and the matrix argument is a numpy ndarray
        (non-object array)
        - first vector is a Categorical, and the matrix argument is also a Categorical
        """
        array_path = os.path.join(os.getcwd(), "tests/data/cross_c.mat")
        mat_contents = loadmat(file_name=array_path)
        result_1 = mat_contents["result1"]
        random_vec = Categorical(values=mat_contents["random_vec"])

        # first way, where first array is a Categorical, second array is a numpy ndarray
        random_matrix = mat_contents["random_matrix"]
        result_1a_py = random_vec.cross(random_matrix)
        self.assertTrue(np.isclose(result_1, result_1a_py).all())

        # second way, where first array is a Categorical, second array is a Categorical
        random_matrix = Categorical(values=mat_contents["random_matrix"])
        result_1b_py = random_vec.cross(random_matrix)
        self.assertTrue(np.isclose(result_1, result_1b_py).all())
Beispiel #10
0
 def test_is_normalized(self):
     values = np.array([[0.7, 0.5], [0.3, 0.5]])
     c = Categorical(values=values)
     self.assertTrue(c.is_normalized())
     values = np.array([[0.2, 0.8], [0.3, 0.5]])
     c = Categorical(values=values)
     self.assertFalse(c.is_normalized())
Beispiel #11
0
 def test_contains_zeros(self):
     values = np.array([[1.0, 0.0], [1.0, 1.0]])
     c = Categorical(values=values)
     self.assertTrue(c.contains_zeros())
     values = np.array([[1.0, 1.0], [1.0, 1.0]])
     c = Categorical(values=values)
     self.assertFalse(c.contains_zeros())
Beispiel #12
0
    def test_cross_function_a(self):
        """Test case a: passing in a single-factor hidden state vector -
        under both options of Categorical: where self.AOA is True or False """
        array_path = os.path.join(os.getcwd(), "tests/data/cross_a.mat")
        mat_contents = loadmat(file_name=array_path)
        result_1 = mat_contents["result1"]
        result_2 = mat_contents["result2"]

        states = np.empty(1, dtype=object)
        states[0] = mat_contents["s"][0, 0].squeeze()
        states = Categorical(
            values=states
        )  # this would create a 1-dimensional array of arrays (namely, self.IS_AOA == True)
        result_1_py = states.cross()
        self.assertTrue(np.isclose(result_1, result_1_py).all())

        states = Categorical(
            values=mat_contents["s"][0, 0].squeeze()
        )  # this creates a simple single-factor Categorical (namely, self.IS_AOA == False)
        result_2_py = states.cross()
        self.assertTrue(np.isclose(result_2, result_2_py).all())
Beispiel #13
0
 def test_remove_zeros(self):
     values = np.array([[1.0, 0.0], [1.0, 1.0]])
     c = Categorical(values=values)
     self.assertTrue((c.values == 0.0).any())
     c.remove_zeros()
     self.assertFalse((c.values == 0.0).any())
Beispiel #14
0
from inferactively import Categorical  # nopep8

# %% Debugging for Categorical.dot()
array_path = os.path.join(os.getcwd(), "tests/data/dot_a.mat")
mat_contents = loadmat(file_name=array_path)

A = mat_contents["A"]
obs = mat_contents["o"]
states = mat_contents["s"]
states = np.array(states, dtype=object)

result_1 = mat_contents["result1"]
result_2 = mat_contents["result2"]
result_3 = mat_contents["result3"]

A = Categorical(values=A)

result_1_py = A.dot(obs, return_numpy=True)
print(np.isclose(result_1, result_1_py).all())

result_2_py = A.dot(states, return_numpy=True)
result_2_py = result_2_py.astype("float64")[:, np.newaxis]
print(np.isclose(result_2, result_2_py).all())

result_3_py = A.dot(states, dims_to_omit=[0], return_numpy=True)
print(np.isclose(result_3, result_3_py).all())

# now try by putting obs and states into Categoricals themselves
obs = Categorical(values=mat_contents["o"])
states = Categorical(values=mat_contents["s"][0])
Beispiel #15
0
 def test_normalize_two_dim(self):
     values = np.array([[1.0, 1.0], [1.0, 1.0]])
     c = Categorical(values=values)
     expected_values = np.array([[0.5, 0.5], [0.5, 0.5]])
     c.normalize()
     self.assertTrue(np.array_equal(c.values, expected_values))
Beispiel #16
0
 def test_log(self):
     values = np.random.rand(3, 2)
     log_values = np.log(values)
     c = Categorical(values=values)
     self.assertTrue(np.array_equal(c.log(return_numpy=True), log_values))
Beispiel #17
0
 def test_shape(self):
     values = np.random.rand(3, 2)
     c = Categorical(values=values)
     self.assertEqual(c.shape, (3, 2))
Beispiel #18
0
 def test_ndim(self):
     values = np.random.rand(3, 2)
     c = Categorical(values=values)
     self.assertEqual(c.ndim, c.values.ndim)
Beispiel #19
0
 def test_init_empty(self):
     c = Categorical()
     self.assertEqual(c.ndim, 2)
Beispiel #20
0
    def test_cross_function_e(self):
        """Test case e: outer-producting two sequences of vectors:
        Options:
        - First sequence is an AoA Categorical, second sequence is a numpy ndarray
        (dtype = object)
        - First sequence is NOT an AoA Categorical, second sequence is a numpy ndarray
        (dtype = object)
        - First sequence is an AoA Categorical, second sequence is an AoA Categorical
        - First sequence is NOT an AoA Categorical, second sequence is an AoA Categorical
        """
        array_path = os.path.join(os.getcwd(), "tests/data/cross_e.mat")
        mat_contents = loadmat(file_name=array_path)
        result_1 = mat_contents["result1"]

        s2 = mat_contents["s2"]
        s2_new = np.empty(s2.shape[1], dtype=object)
        for i in range(len(s2_new)):
            s2_new[i] = s2[0][i].squeeze()

        # first way (type 1), first sequence is a Categorical (self.AOA = True) and
        # second sequence is a numpy ndarray (dtype = object)
        s1 = Categorical(values=mat_contents["s1"][0])
        result_1aa_py = s1.cross(s2_new)
        self.assertTrue(np.isclose(result_1, result_1aa_py).all())

        # first way (type 2), first sequence is a Categorical (self.AOA = False) and
        # second sequence is a numpy ndarray (dtype = object)
        s1 = Categorical(values=mat_contents["s1"][0][0])
        result_1ab_py = s1.cross(s2_new)
        self.assertTrue(np.isclose(result_1, result_1ab_py).all())

        s2_new = Categorical(values=mat_contents["s2"][0])
        # second way (type 1), first sequence is a Categorical (self.AOA = True)
        # and second sequence is a Categorical
        s1 = Categorical(values=mat_contents["s1"][0])
        result_2aa_py = s1.cross(s2_new)
        self.assertTrue(np.isclose(result_1, result_2aa_py).all())

        # second way (type 2), first sequence is a Categorical (self.AOA = False)
        # and second sequence is a Categorical
        s1 = Categorical(values=mat_contents["s1"][0][0])
        result_2ab_py = s1.cross(s2_new)
        self.assertTrue(np.isclose(result_1, result_2ab_py).all())
Beispiel #21
0
 def test_multi_factor_init_dims(self):
     c = Categorical(dims=[[5, 4], [4, 3]])
     self.assertEqual(c.shape, (2, ))
     self.assertEqual(c[0].shape, (5, 4))
     self.assertEqual(c[1].shape, (4, 3))
Beispiel #22
0
 def test_init_overload(self):
     with self.assertRaises(ValueError):
         values = np.random.rand(3, 2)
         _ = Categorical(dims=2, values=values)
Beispiel #23
0
 def test_float_conversion(self):
     values = np.array([2, 3])
     self.assertEqual(values.dtype, np.int)
     c = Categorical(values=values)
     self.assertEqual(c.values.dtype, np.float64)
Beispiel #24
0
 def test_init_dims_int_expand(self):
     c = Categorical(dims=5)
     self.assertEqual(c.shape, (5, 1))
Beispiel #25
0
 def test_entropy(self):
     values = np.random.rand(3, 2)
     entropy = -np.sum(values * np.log(values), 0)
     c = Categorical(values=values)
     self.assertTrue(np.array_equal(c.entropy(return_numpy=True), entropy))