def test_creation(self): """Test creating Factors.""" with self.assertRaises(Exception): Factor(0) fA = Factor([0.6, 0.4], {'A': ['a1', 'a0']}) self.assertEqual( repr(fA), 'factor(A)\nA \na1 0.6\na0 0.4\ndtype: float64')
def test_from_data(self): """Test creating an (empirical) distribution from data.""" filename = thomas.core.get_pkg_filename('dataset_17_2.csv') df = pd.read_csv(filename, sep=';') scope = ['H', 'S', 'E'] factor = Factor.from_data(df, cols=scope) self.assertEqual(set(factor.scope), set(scope)) self.assertEqual(factor.sum(), 16)
def test_state_order(self): """Test that a Factor keeps its states in order and/or its index correct. Regression test for GitHub issue #1. """ # P(A) fA = Factor([0.6, 0.4], {'A': ['a1', 'a0']}) self.assertEquals(fA['a1'], 0.6) self.assertEquals(fA['a0'], 0.4) # P(B|A) fB_A = Factor([0.2, 0.8, 0.75, 0.25], { 'A': ['a1', 'a0'], 'B': ['b1', 'b0'] }) self.assertEquals(fB_A['a1', 'b1'], 0.20) self.assertEquals(fB_A['a1', 'b0'], 0.80) self.assertEquals(fB_A['a0', 'b1'], 0.75) self.assertEquals(fB_A['a0', 'b0'], 0.25)
def test_serialization_complex(self): """Test the JSON serialization.""" [fA, fB_A, fC_A, fD_BC, fE_C] = examples.get_sprinkler_factors() dict_repr = fB_A.as_dict() # Make sure the expected keys exist for key in ['type', 'scope', 'states', 'data']: self.assertTrue(key in dict_repr) self.assertTrue(dict_repr['type'] == 'Factor') fB_A2 = Factor.from_dict(dict_repr) self.assertEquals(fB_A.scope, fB_A2.scope) self.assertEquals(fB_A.states, fB_A2.states)
def test_multiplication_state_order(self): """Test factor multiplication with states ordered differently.""" # fE has states ordered ['T', 'F']; T=1.0, F=0.0 fE_out_of_order = Factor.from_dict({ 'type': 'Factor', 'scope': ['E'], 'states': { 'E': ['T', 'F'] }, 'data': [1.0, 0.0] }) fE = Factor.from_dict({ 'type': 'Factor', 'scope': ['E'], 'states': { 'E': ['F', 'T'] }, 'data': [0.0, 1.0] }) multiplied = fE * fE_out_of_order self.assertAlmostEquals(multiplied['T'], 1.0, places=2) self.assertAlmostEquals(multiplied['F'], 0.0, places=2)
def test_values(self): """Test cast to np.array.""" fA = Factor([0.6, 0.4], {'A': ['a1', 'a0']}) self.assertTrue(isinstance(fA.values, np.ndarray))
def test_align_index(self): """Test index aligning.""" fA1 = Factor.from_dict({ 'type': 'Factor', 'scope': ['A'], 'states': { 'A': ['a1', 'a2', 'a3'] }, 'data': [1.0, 2.0, 3.0] }) fA2 = Factor.from_dict({ 'type': 'Factor', 'scope': ['A'], 'states': { 'A': ['a3', 'a1', 'a2'] }, 'data': [3.0, 1.0, 0.0] }) fB = Factor.from_dict({ 'type': 'Factor', 'scope': ['B'], 'states': { 'B': ['b3', 'b1', 'b2'] }, 'data': [3.0, 1.0, 0.0] }) aligned = fA1.align_index(fA2) self.assertTrue(isinstance(aligned, Factor)) self.assertEquals(aligned.scope, fA1.scope) self.assertEquals(aligned.scope, fA2.scope) self.assertEquals(aligned['a1'], 1.0) self.assertEquals(aligned['a2'], 2.0) self.assertEquals(aligned['a3'], 3.0) with self.assertRaises(error.IncompatibleScopeError): fA1.align_index(fB) # Same factors, but one variable has a different order. fB_A1 = Factor([0.2, 0.8, 0.75, 0.25], { 'A': ['a1', 'a0'], 'B': ['b1', 'b0'] }) fB_A2 = Factor([0.8, 0.2, 0.25, 0.75], { 'A': ['a1', 'a0'], 'B': ['b0', 'b1'] }) aligned = fB_A1.align_index(fB_A2) self.assertEquals(aligned['a1', 'b1'], 0.2) self.assertEquals(aligned['a1', 'b0'], 0.8) self.assertEquals(aligned['a0', 'b1'], 0.75) self.assertEquals(aligned['a0', 'b0'], 0.25) aligned = fB_A2.align_index(fB_A1) self.assertEquals(aligned['a1', 'b1'], 0.2) self.assertEquals(aligned['a1', 'b0'], 0.8) self.assertEquals(aligned['a0', 'b1'], 0.75) self.assertEquals(aligned['a0', 'b0'], 0.25) # This redefines fA1 and fA0! fA1 = Factor([1.0, 0.0], {'A': ['a1', 'a0']}) fA0 = Factor([0.0, 1.0], {'A': ['a0', 'a1']}) aligned = fA1.align_index(fB_A1) self.assertEquals(aligned['a1'], 1.0) self.assertEquals(aligned['a0'], 0.0) aligned = fA0.align_index(fB_A1) self.assertEquals(aligned['a1'], 1.0) self.assertEquals(aligned['a0'], 0.0)