def test_DecisionMatrix_no_provide_objective(data_values): mtx, _, weights, alternatives, criteria = data_values(seed=42) with pytest.raises(TypeError): data.mkdm( mtxt=mtx, weights=weights, criteria=criteria, alternatives=alternatives, )
def test_DecisionMatrix_weight_no_float(data_values): mtx, objectives, _, alternatives, criteria = data_values(seed=42) weights = ["hola"] with pytest.raises(ValueError): data.mkdm( matrix=mtx, objectives=objectives, weights=weights, alternatives=alternatives, criteria=criteria, )
def test_DecisionMatrix_missmatch_cnames(data_values): mtx, objectives, weights, alternatives, criteria = data_values(seed=42) criteria = criteria[1:] with pytest.raises(ValueError): data.mkdm( matrix=mtx, objectives=objectives, weights=weights, alternatives=alternatives, criteria=criteria, )
def test_DecisionMatrix_mtx_ndim1(data_values): mtx, objectives, weights, alternatives, criteria = data_values(seed=42) mtx = mtx.flatten() with pytest.raises(ValueError): data.mkdm( matrix=mtx, objectives=objectives, weights=weights, alternatives=alternatives, criteria=criteria, )
def test_DecisionMatrix_mtx_ndim3(data_values): _, objectives, weights, alternatives, criteria = data_values(seed=42) mtx = [[[1, 2, 3], [4, 5, 6], [7, 8, 9]]] with pytest.raises(ValueError): data.mkdm( matrix=mtx, objectives=objectives, weights=weights, alternatives=alternatives, criteria=criteria, )
def test_DecisionMatrix_invalid_objective(data_values): mtx, _, weights, alternatives, criteria = data_values(seed=42) objectives = [1, 2, 3, 4] with pytest.raises(ValueError): data.mkdm( matrix=mtx, objectives=objectives, weights=weights, alternatives=alternatives, criteria=criteria, )
def test_DecisionMatrix_to_dict(data_values): mtx, objectives, weights, alternatives, criteria = data_values(seed=42) dm = data.mkdm( matrix=mtx, objectives=objectives, weights=weights, alternatives=alternatives, criteria=criteria, ) expected = { "matrix": mtx, "objectives": construct_iobjectives(objectives), "weights": weights, "alternatives": np.asarray(alternatives), "criteria": np.asarray(criteria), "dtypes": np.full(len(weights), float), } result = dm.to_dict() cmp = {k: (np.all(result[k] == expected[k])) for k in result.keys()} assert np.all(cmp.values())
def test_DecisionMatrix_no_provide_cnames_and_anames(data_values): mtx, objectives, weights, _, _ = data_values(seed=42) dm = data.mkdm( matrix=mtx, objectives=objectives, weights=weights, ) alternatives = [f"A{idx}" for idx in range(mtx.shape[0])] criteria = [f"C{idx}" for idx in range(mtx.shape[1])] np.testing.assert_array_equal(dm.matrix, mtx) np.testing.assert_array_equal(dm.iobjectives, construct_iobjectives(objectives)) np.testing.assert_array_equal(dm.objectives, construct_objectives(objectives)) np.testing.assert_array_equal(dm.weights, weights) np.testing.assert_array_equal(dm.alternatives, alternatives) np.testing.assert_array_equal(dm.criteria, criteria) np.testing.assert_array_equal(dm.minwhere, dm.objectives == data.Objective.MIN) np.testing.assert_array_equal(dm.maxwhere, dm.objectives == data.Objective.MAX)
def test_DecisionMatrix_simple_creation(data_values): mtx, objectives, weights, alternatives, criteria = data_values(seed=42) dm = data.mkdm( matrix=mtx, objectives=objectives, weights=weights, alternatives=alternatives, criteria=criteria, ) np.testing.assert_array_equal(dm.matrix, mtx) np.testing.assert_array_equal(dm.iobjectives, construct_iobjectives(objectives)) np.testing.assert_array_equal(dm.objectives, construct_objectives(objectives)) np.testing.assert_array_equal(dm.weights, weights) np.testing.assert_array_equal(dm.alternatives, alternatives) np.testing.assert_array_equal(dm.criteria, criteria) np.testing.assert_array_equal(dm.dtypes, [np.float64] * len(criteria)) np.testing.assert_array_equal(dm.minwhere, dm.objectives == data.Objective.MIN) np.testing.assert_array_equal(dm.maxwhere, dm.objectives == data.Objective.MAX)
def test_DecisionMatrixDominanceAccessor_dominance_strict(): dm = data.mkdm( matrix=[ [10, 80], [20, 70], ], objectives=[max, min], alternatives=["A0", "A1"], criteria=["C0", "C1"], ) dom = dominance.DecisionMatrixDominanceAccessor(dm) expected = pd.DataFrame( [ [False, False], [True, False], ], index=["A0", "A1"], columns=["A0", "A1"], ) pd.testing.assert_frame_equal(dom.dominance(strict=True), expected) pd.testing.assert_frame_equal(dm.dominance.dominance(strict=True), expected)
def test_DecisionMatrixDominanceAccessor_dominated_strict_false(): dm = data.mkdm( matrix=[ [10, 80], [10, 70], ], objectives=[max, min], alternatives=["A0", "A1"], criteria=["C0", "C1"], ) dom = dominance.DecisionMatrixDominanceAccessor(dm) strict_expected = pd.Series([False, False], index=["A0", "A1"]) pd.testing.assert_series_equal(dom.dominated(strict=True), strict_expected) pd.testing.assert_series_equal(dm.dominance.dominated(strict=True), strict_expected) not_strict_expected = pd.Series([True, False], index=["A0", "A1"]) pd.testing.assert_series_equal(dom.dominated(strict=False), not_strict_expected) pd.testing.assert_series_equal(dm.dominance.dominated(strict=False), not_strict_expected)
def test_DecisionMatrix_no_provide_weights(data_values): mtx, objectives, _, alternatives, criteria = data_values(seed=42) dm = data.mkdm( matrix=mtx, objectives=objectives, alternatives=alternatives, criteria=criteria, ) weights = np.ones(len(dm.objectives)) np.testing.assert_array_equal(dm.matrix, mtx) np.testing.assert_array_equal(dm.iobjectives, construct_iobjectives(objectives)) np.testing.assert_array_equal(dm.objectives, construct_objectives(objectives)) np.testing.assert_array_equal(dm.weights, weights) np.testing.assert_array_equal(dm.alternatives, alternatives) np.testing.assert_array_equal(dm.criteria, criteria) np.testing.assert_array_equal(dm.minwhere, dm.objectives == data.Objective.MIN) np.testing.assert_array_equal(dm.maxwhere, dm.objectives == data.Objective.MAX)
def test_DecisionMatrixDominanceAccessor_compare(): dm = data.mkdm( matrix=[ [10, 70], [20, 70], ], objectives=[max, min], alternatives=["A0", "A1"], criteria=["C0", "C1"], ) dom = dominance.DecisionMatrixDominanceAccessor(dm) expected = pd.DataFrame.from_dict({ ("Criteria", "C0"): { ("Alternatives", "A0"): False, ("Alternatives", "A1"): True, ("Equals", ""): False, }, ("Criteria", "C1"): { ("Alternatives", "A0"): False, ("Alternatives", "A1"): False, ("Equals", ""): True, }, ("Performance", ""): { ("Alternatives", "A0"): 0, ("Alternatives", "A1"): 1, ("Equals", ""): 1, }, }) result = dom.compare("A0", "A1") pd.testing.assert_frame_equal(result, expected)
def test_DecisionMatrix_self_ne(data_values): mtx, objectives, weights, alternatives, criteria = data_values(seed=42) dm = data.mkdm( matrix=mtx, objectives=objectives, weights=weights, alternatives=alternatives, criteria=criteria, ) omtx, oobjectives, oweights, oanames, ocnames = data_values(seed=43) other = data.mkdm( matrix=omtx, objectives=oobjectives, weights=oweights, alternatives=oanames, criteria=ocnames, ) assert not dm.equals(other)
def test_DecisionMatrix_copy(data_values): mtx, objectives, weights, alternatives, criteria = data_values(seed=42) dm = data.mkdm( matrix=mtx, objectives=objectives, weights=weights, alternatives=alternatives, criteria=criteria, ) copy = dm.copy() assert dm is not copy assert dm.equals(copy)
def test_DecisionMatrix_self_eq(data_values): mtx, objectives, weights, alternatives, criteria = data_values(seed=42) dm = data.mkdm( matrix=mtx, objectives=objectives, weights=weights, alternatives=alternatives, criteria=criteria, ) same = dm assert dm is same assert dm.equals(same)
def test_DecisionMatrixDominanceAccessor_has_loops_false(): dm = data.mkdm( matrix=[ [10, 80], [10, 70], ], objectives=[max, min], alternatives=["A0", "A1"], criteria=["C0", "C1"], ) dom = dominance.DecisionMatrixDominanceAccessor(dm) assert not dom.has_loops(strict=True) assert not dom.has_loops(strict=False)
def test_DecisionMatrixDominanceAccessor_dominators_of_strict_false(): dm = data.mkdm( matrix=[ [10, 80], [10, 70], ], objectives=[max, min], alternatives=["A0", "A1"], criteria=["C0", "C1"], ) dom = dominance.DecisionMatrixDominanceAccessor(dm) assert np.all(dom.dominators_of("A0", strict=False) == ["A1"]) assert not len(dom.dominators_of("A0", strict=True))
def test_mksm_simple_repr(): dm = data.mkdm( matrix=[[1, 2, 3], [4, 5, 6], [7, 8, 9]], objectives=[min, max, min], weights=[0.1, 0.2, 0.3], ) expected = (" C0[\u25bc 0.1] C1[\u25b2 0.2] C2[\u25bc 0.3]\n" "A0 1 2 3\n" "A1 4 5 6\n" "A2 7 8 9\n" "[3 Alternatives x 3 Criteria]") result = repr(dm) assert result == expected
def test_DecisionMatrix_describe(data_values): mtx, objectives, weights, alternatives, criteria = data_values(seed=42) dm = data.mkdm( matrix=mtx, objectives=objectives, weights=weights, alternatives=alternatives, criteria=criteria, ) expected = pd.DataFrame(mtx, columns=criteria, index=alternatives).describe() with pytest.deprecated_call(): result = dm.describe() assert result.equals(expected)
def test_DecisionMatrix_to_dataframe(data_values): mtx, objectives, weights, alternatives, criteria = data_values(seed=42) dm = data.mkdm( matrix=mtx, objectives=objectives, weights=weights, alternatives=alternatives, criteria=criteria, ) df = dm.to_dataframe() rows = np.vstack((construct_objectives(objectives), weights, mtx)) expected = pd.DataFrame(rows, index=["objectives", "weights"] + alternatives, columns=criteria) pd.testing.assert_frame_equal(df, expected)
def test_DecisionMatrixDominanceAccessor_bt(): dm = data.mkdm( matrix=[ [10, 40], [20, 70], ], objectives=[max, min], alternatives=["A0", "A1"], criteria=["C0", "C1"], ) dom = dominance.DecisionMatrixDominanceAccessor(dm) expected = pd.DataFrame( [ [0, 1], [1, 0], ], index=["A0", "A1"], columns=["A0", "A1"], ) pd.testing.assert_frame_equal(dom.bt(), expected) pd.testing.assert_frame_equal(dm.dominance.bt(), expected)
def test_DecisionMatrixDominanceAccessor_has_loops_true(): """This test is complex so we relay on a hack""" dm = data.mkdm( matrix=[ [10, 80], [10, 70], ], objectives=[max, min], alternatives=["A0", "A1"], criteria=["C0", "C1"], ) fake_dominance = pd.DataFrame( [[False, True], [True, False]], index=["A0", "A1"], columns=["A0", "A1"], ) with mock.patch.object(dm.dominance, "dominance", return_value=fake_dominance): assert dm.dominance.has_loops()
def test_simple_html(): dm = data.mkdm( matrix=[[1, 2, 3], [4, 5, 6], [7, 8, 9]], objectives=[min, max, min], weights=[0.1, 0.2, 0.3], ) expected = PyQuery(""" <div class="decisionmatrix"> <div> <style scoped=""> .dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; } </style> <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th/> <th>C0[▼ 0.1]</th> <th>C1[▲ 0.2]</th> <th>C2[▼ 0.3]</th> </tr> </thead> <tbody> <tr> <th>A0</th> <td>1</td> <td>2</td> <td>3</td> </tr> <tr> <th>A1</th> <td>4</td> <td>5</td> <td>6</td> </tr> <tr> <th>A2</th> <td>7</td> <td>8</td> <td>9</td> </tr> </tbody> </table> </div> <em class="decisionmatrix-dim">3 Alternatives x 3 Criteria </em> </div> """) result = PyQuery(dm._repr_html_()) assert result.text() == expected.text()