def test_assert_dict_equals_when_not_equal_nested_array_of_dicts(self): d1 = { 'a': { 'o1': [2, 5], 'o2': 5 }, 'b': { 'p1': 'hello', 'p2': np.array([3, 9, 1]) }, 'c': [{ 'a1': 4, 'a2': [2, 5], 'a3': 'ok' }] } d2 = { 'a': { 'o1': [2, 5], 'o2': 5 }, 'b': { 'p1': 'hello', 'p2': np.array([3, 9, 1]) }, 'c': [{ 'a1': 4, 'a2': [2, 7, 5], 'a3': 'ok' }] } with self.assertRaises(AssertionError): utils.assert_dict_equals(d1, d2)
def test_assert_dict_equals_when_equal_nested_array_of_dicts(self): d1 = { 'a': { 'o1': [2, 5], 'o2': 5 }, 'b': { 'p1': 'hello', 'p2': np.array([3, 9, 1]) }, 'c': [{ 'a1': 4, 'a2': [2, 5], 'a3': 'ok' }] } d2 = { 'a': { 'o1': [2, 5], 'o2': 5 }, 'b': { 'p1': 'hello', 'p2': np.array([3, 9, 1]) }, 'c': [{ 'a1': 4, 'a2': [2, 5], 'a3': 'ok' }] } utils.assert_dict_equals(d1, d2)
def test_assert_dict_equals_when_not_equal_types_in_values(self): d1 = { 'a': { 'o1': [2, 5], 'o2': '5' }, 'b': { 'p1': 'hello', 'p2': np.array([3, 9, 1]) }, 'c': [] } d2 = { 'a': { 'o1': [2, 5], 'o2': 5 }, 'b': { 'p1': 'hello', 'p2': np.array([3, 9, 1]) }, 'c': [] } with self.assertRaises(AssertionError): utils.assert_dict_equals(d1, d2)
def test_assert_dict_equals_when_not_equal_in_nested_keys(self): d1 = { 'a': { 'o1': [2, 5], 'o2': 5 }, 'b': { 'p1': 'hello', 'p2': np.array([3, 9, 1]), 'p3': np.zeros(2) }, 'c': [] } d2 = { 'a': { 'o1': [2, 5], 'o2': 5 }, 'b': { 'p1': 'hello', 'p2': np.array([3, 9, 1]) }, 'c': [] } with self.assertRaises(AssertionError): utils.assert_dict_equals(d1, d2)
def test_compute_all_teams_attachments_to_before_discussion_opinion(self): teams_data = { 55: { 'asbestos': { 'w12': [0.1, 0.0, 0.2], 'w21': [0.0, 0.0, 0.0], 'x1': [0.1, 0.2, 0.6, 0.4], 'x2': [0.9, 0.4, 0.7, 0.5] }, 'surgery': { 'w12': [0.35, 0.4, 0.5], 'w21': [0.25, 0.3, 0.3], 'x1': [0.6, 0.65, 0.7, 0.7], 'x2': [0.75, 0.5, 0.6, 0.7] } } } expected_attachments = { 55: { 'asbestos': { 'a11': [ 0.1 / 0.08, 0.5 / 0.1, (0.4 - 0.2) / (0.6 - 0.2 + 0.2 * (0.7 - 0.6)) ], 'a22': [np.nan, -0.2 / -0.5, (0.5 - 0.4) / (0.7 - 0.4)], # Nan was -0.5/0. 'a11_nan_details': [{}, {}, {}], 'a22_nan_details': [{ 'n/0': 1, 'xi[k]-xi[k-1]==0': 1, 'wij[k]==0': 1 }, {}, {}] }, 'surgery': { 'a11': [ 0.05 / (0.35 * 0.15), 0.1 / (0.05 + 0.4 * -0.15), np.nan ], # denominator is 0. It is (0.7-0.65)/(0.7-0.65+0.5*(0.6-0.7)). 'a22': [ -0.25 / (0.25 * -0.15), -0.15 / (-0.25 + 0.3 * 0.15), (0.7 - 0.5) / (0.6 - 0.5 + 0.3 * (0.7 - 0.6)) ], 'a11_nan_details': [{}, {}, { 'n/0': 1 }], 'a22_nan_details': [{}, {}, {}] } } } computed_attachments = gll.compute_all_teams_attachments( teams_data=teams_data, start_k=0, use_attachment_to_initial_opinion=False, eps=0) utils.assert_dict_equals(d1=expected_attachments, d2=computed_attachments, almost_number_of_decimals=6)
def test_compute_all_teams_attachments(self): # Attachment to the initial opinion. teams_data = { 55: { 'asbestos': { 'w12': [0.1, 0.0, 0.2], 'w21': [0.0, 0.0, 0.0], 'x1': [0.1, 0.2, 0.6, 0.4], 'x2': [0.9, 0.4, 0.7, 0.5] }, 'surgery': { 'w12': [0.35, 0.4, 0.5], 'w21': [0.25, 0.3, 0.3], 'x1': [0.6, 0.65, 0.7, 0.7], 'x2': [0.75, 0.5, 0.6, 0.7] } } } expected_attachments = { 55: { 'asbestos': { 'a11': [0.1 / 0.08, 0.5 / 0.1, 0.3 / 0.52], 'a22': [np.nan, -0.2 / -0.5, -0.4 / -0.2], # Nan was -0.5/0. 'a11_nan_details': [{}, {}, {}], 'a22_nan_details': [{ 'n/0': 1, 'xi[k]-xi[0]==0': 1, 'wij[k]==0': 1 }, {}, {}] }, 'surgery': { 'a11': [ 0.05 / (0.35 * 0.15), 0.1 / (0.05 + 0.4 * -0.15), 0.1 / (0.1 + 0.5 * -0.1) ], 'a22': [ -0.25 / (0.25 * -0.15), -0.15 / (-0.25 + 0.3 * 0.15), -0.05 / (-0.15 + 0.3 * 0.1) ], 'a11_nan_details': [{}, {}, {}], 'a22_nan_details': [{}, {}, {}] } } } computed_attachments = gll.compute_all_teams_attachments( teams_data=teams_data, start_k=0, use_attachment_to_initial_opinion=True, eps=0) utils.assert_dict_equals(d1=expected_attachments, d2=computed_attachments, almost_number_of_decimals=6)
def test_assert_dict_equals_when_same(self): d1 = { 'a': { 'o1': [2, 5], 'o2': 5 }, 'b': { 'p1': 'hello', 'p2': np.array([3, 9, 1]) }, 'c': [] } utils.assert_dict_equals(d1, d1)
def test_compute_attachment_to_initial_opinion(self): x1 = [0.1, 0.2, 0.6, 0.4] x2 = [0.9, 0.4, 0.7, 0.5] w12 = [0.1, 0.0, 0.2] expected_a11 = [0.1 / 0.08, 0.5 / 0.1, 0.3 / 0.52] expected_details = [{}, {}, {}] computed_a11, details = gll.compute_attachment( xi=x1, xj=x2, wij=w12, eps=0, to_opinion=gll.AttachmentType.TO_INITIAL) np_testing.assert_array_almost_equal(expected_a11, computed_a11) utils.assert_dict_equals({'1': expected_details}, {'1': details})
def test_compute_attachment_to_initial_op_when_denom_to_sum_almost_zero( self): x1 = [0.8, 0.85, 0.92, 0.92] x2 = [0.6, 0.6, 0.7, 0.7] w12 = [0.1, 0.2, 0.1] expected_a11 = [ (0.85 - 0.8) / (0.1 * (0.6 - 0.8)), np.nan, # (0.92 - 0.8) / (0.85 - 0.8 + 0.2 * (0.6 - 0.85)), (0.92 - 0.8) / (0.92 - 0.8 + 0.1 * (0.7 - 0.92)) ] expected_details = [{}, {'n/0': 1}, {}] computed_a11, details = gll.compute_attachment( xi=x1, xj=x2, wij=w12, eps=0, to_opinion=gll.AttachmentType.TO_INITIAL) np_testing.assert_array_almost_equal(expected_a11, computed_a11) utils.assert_dict_equals({'1': expected_details}, {'1': details})
def test_compute_all_teams_attachments_with_start_k_equals_1(self): teams_data = { 55: { 'asbestos': { 'w12': [0.1, 0.0, 0.2], 'w21': [0.0, 0.0, 0.0], 'x1': [0.1, 0.2, 0.6, 0.4], 'x2': [0.9, 0.4, 0.7, 0.5] }, 'surgery': { 'w12': [0.35, 0.4, 0.5], 'w21': [0.25, 0.3, 0.3], 'x1': [0.6, 0.65, 0.7, 0.7], 'x2': [0.75, 0.5, 0.6, 0.7] } } } expected_attachments = { 55: { 'asbestos': { 'a11': [0.5 / 0.1, 0.3 / 0.52], 'a22': [-0.2 / -0.5, -0.4 / -0.2], 'a11_nan_details': [{}, {}], 'a22_nan_details': [{}, {}] }, 'surgery': { 'a11': [0.1 / (0.05 + 0.4 * -0.15), 0.1 / (0.1 + 0.5 * -0.1)], 'a22': [ -0.15 / (-0.25 + 0.3 * 0.15), -0.05 / (-0.15 + 0.3 * 0.1) ], 'a11_nan_details': [{}, {}], 'a22_nan_details': [{}, {}] } } } computed_attachments = gll.compute_all_teams_attachments( teams_data=teams_data, start_k=1, eps=0) utils.assert_dict_equals(d1=expected_attachments, d2=computed_attachments, almost_number_of_decimals=6)
def test_assert_dict_equals_when_empty(self): d1 = {} d2 = {} utils.assert_dict_equals(d1, d2)