def test_compute_features_import(self, mock_compute_features_import): """ Unit test compute features import """ state = SmartState() state.compute_features_import(Mock()) mock_compute_features_import.assert_called()
def test_compute_grouped_contributions(self, mock_group_contributions): """ Unit test compute features groups contributions """ state = SmartState() state.compute_grouped_contributions(Mock(), {}) mock_group_contributions.assert_called()
def test_validate_contributions_1(self, choose_state): """ Unit test validate contributions 1 """ choose_state.return_value = MultiDecorator(SmartState()) predictor_1 = self.predictor_1 contributions = [ np.array([[2, 1], [8, 4]]), np.array([[5, 5], [0, 0]]) ] predictor_1.state = predictor_1.choose_state(contributions) predictor_1.data = {"x": None, "ypred": None, "contributions": None} predictor_1.data["x_preprocessed"] = pd.DataFrame( [[1, 2], [3, 4]], columns=['Col1', 'Col2'], index=['Id1', 'Id2']) expected_output = [ pd.DataFrame([[2, 1], [8, 4]], columns=['Col1', 'Col2'], index=['Id1', 'Id2']), pd.DataFrame([[5, 5], [0, 0]], columns=['Col1', 'Col2'], index=['Id1', 'Id2']) ] output = predictor_1.validate_contributions(contributions) assert len(expected_output) == len(output) test_list = [ pd.testing.assert_frame_equal(e, m) for e, m in zip(expected_output, output) ] assert all(x is None for x in test_list)
def test_assign_contributions_2(self): """ Unit test assign contributions 2 """ state = SmartState() ranked = [1, 2] with self.assertRaises(ValueError): state.assign_contributions(ranked)
def test_assign_contributions_1(self): """ Unit test assign contributions 1 """ state = SmartState() output = state.assign_contributions([1, 2, 3]) expected = {'contrib_sorted': 1, 'x_sorted': 2, 'var_dict': 3} self.assertDictEqual(output, expected)
def test_check_contributions_1(self): """ Unit test check contributions 1 """ state = SmartState() contributions = pd.DataFrame([[-0.2, 0.1], [0.8, -0.4], [0.5, -0.7]], ) x_pred = pd.DataFrame([[1, 2], [3, 4]], ) assert not state.check_contributions(contributions, x_pred)
def test_to_pandas_1(self): """ Unit test to pandas 1 """ xpl = SmartExplainer() xpl.state = SmartState() data = {} data['contrib_sorted'] = pd.DataFrame( [[0.32230754, 0.1550689, 0.10183475, 0.05471339], [-0.58547512, -0.37050409, -0.07249285, 0.00171975], [-0.48666675, 0.25507156, -0.16968889, 0.0757443]], columns=[ 'contribution_0', 'contribution_1', 'contribution_2', 'contribution_3' ], index=[0, 1, 2]) data['var_dict'] = pd.DataFrame( [[1, 0, 2, 3], [1, 0, 3, 2], [1, 0, 2, 3]], columns=['feature_0', 'feature_1', 'feature_2', 'feature_3'], index=[0, 1, 2]) data['x_sorted'] = pd.DataFrame( [[1., 3., 22., 1.], [2., 1., 2., 38.], [2., 3., 26., 1.]], columns=['feature_0', 'feature_1', 'feature_2', 'feature_3'], index=[0, 1, 2]) xpl.data = data xpl.columns_dict = {0: 'Pclass', 1: 'Sex', 2: 'Age', 3: 'Embarked'} xpl.features_dict = { 'Pclass': 'Pclass', 'Sex': 'Sex', 'Age': 'Age', 'Embarked': 'Embarked' } xpl.x = pd.DataFrame( [[3., 1., 22., 1.], [1., 2., 38., 2.], [3., 2., 26., 1.]], columns=['Pclass', 'Sex', 'Age', 'Embarked'], index=[0, 1, 2]) xpl.x_pred = xpl.x xpl.contributions = data['contrib_sorted'] xpl.y_pred = pd.DataFrame([1, 2, 3], columns=['pred'], index=[0, 1, 2]) model = lambda: None model.predict = types.MethodType(self.predict, model) xpl.model = model xpl._case, xpl._classes = xpl.check_model() xpl.state = xpl.choose_state(xpl.contributions) output = xpl.to_pandas(max_contrib=2) expected = pd.DataFrame( [[1, 'Sex', 1.0, 0.32230754, 'Pclass', 3.0, 0.1550689], [2, 'Sex', 2.0, -0.58547512, 'Pclass', 1.0, -0.37050409], [3, 'Sex', 2.0, -0.48666675, 'Pclass', 3.0, 0.25507156]], columns=[ 'pred', 'feature_1', 'value_1', 'contribution_1', 'feature_2', 'value_2', 'contribution_2' ], index=[0, 1, 2], dtype=object) expected['pred'] = expected['pred'].astype(int) assert not pd.testing.assert_frame_equal(expected, output)
def test_check_contributions_2(self): """ Unit test check contributions 2 """ state = SmartState() contributions = pd.DataFrame([[-0.2, 0.1], [0.8, -0.4]], index=['row_1', 'row_2']) x_pred = pd.DataFrame([[1, 2], [3, 4]]) assert not state.check_contributions(contributions, x_pred)
def test_inverse_transform_contributions(self, mock_inverse_transform_contributions): """ Unit test inverse transform contributions Parameters ---------- mock_inverse_transform_contributions : [type] [description] """ state = SmartState() state.inverse_transform_contributions(Mock(), Mock()) mock_inverse_transform_contributions.assert_called()
def test_compute_features_import(self, mock_compute_features_import): """ Unit test compute features import Parameters ---------- mock_compute_features_import : [type] [description] """ state = SmartState() state.compute_features_import(Mock()) mock_compute_features_import.assert_called()
def test_init_mask(self, mock_init_mask): """ Unit test init mask Parameters ---------- mock_init_mask : [type] [description] """ state = SmartState() state.init_mask(Mock()) mock_init_mask.assert_called()
def test_compute_masked_contributions(self, mock_compute_masked_contributions): """ Unit test compute masked contributions Parameters ---------- mock_compute_masked_contributions : [type] [description] """ state = SmartState() state.compute_masked_contributions(Mock(), Mock()) mock_compute_masked_contributions.assert_called()
def test_combine_masks(self, mock_combine_masks): """ Unit test combine masks Parameters ---------- mock_combine_masks : [type] [description] """ state = SmartState() state.combine_masks(Mock()) mock_combine_masks.assert_called()
def test_sign_contributions(self, mock_sign_contributions): """ Unit test sign contributions Parameters ---------- mock_sign_contributions : [type] [description] """ state = SmartState() state.sign_contributions(Mock(), Mock()) mock_sign_contributions.assert_called()
def test_check_contributions_3(self): """ Unit test check contributions 3 """ state = SmartState() contributions = pd.DataFrame( [[-0.2, 0.1], [0.8, -0.4]], columns=['col_1', 'col_2'], ) x_pred = pd.DataFrame([[1, 2], [3, 4]], ) assert not state.check_contributions(contributions, x_pred)
def test_filter_7(self): """ Unit test filter 7 """ xpl = SmartExplainer() contributions = [ pd.DataFrame( data=[[0.5, 0.4, 0.3], [0.9, 0.8, 0.7]], columns=['Col1', 'Col2', 'Col3'] ), pd.DataFrame( data=[[0.3, 0.2, 0.1], [0.6, 0.5, 0.4]], columns=['Col1', 'Col2', 'Col3'] ) ] xpl.data = {'var_dict': 1, 'contrib_sorted': contributions, 'x_sorted': 3} xpl.state = MultiDecorator(SmartState()) xpl.filter(threshold=0.5, max_contrib=2) expected_mask = [ pd.DataFrame( data=[[True, False, False], [True, True, False]], columns=['contrib_1', 'contrib_2', 'contrib_3'] ), pd.DataFrame( data=[[False, False, False], [True, True, False]], columns=['contrib_1', 'contrib_2', 'contrib_3'] ) ] assert len(expected_mask) == len(xpl.mask) test_list = [pd.testing.assert_frame_equal(e, m) for e, m in zip(expected_mask, xpl.mask)] assert all(x is None for x in test_list) expected_masked_contributions = [ pd.DataFrame( data=[[0.0, 0.7], [0.0, 0.7]], columns=['masked_neg', 'masked_pos'] ), pd.DataFrame( data=[[0.0, 0.6], [0.0, 0.4]], columns=['masked_neg', 'masked_pos'] ) ] assert len(expected_masked_contributions) == len(xpl.masked_contributions) test_list = [pd.testing.assert_frame_equal(e, m) for e, m in zip(expected_masked_contributions, xpl.masked_contributions)] assert all(x is None for x in test_list) expected_param_dict = { 'features_to_hide': None, 'threshold': 0.5, 'positive': None, 'max_contrib': 2 } self.assertDictEqual(expected_param_dict, xpl.mask_params)
def test_summarize_1(self): """ Unit test summarize 1 """ state = SmartState() contrib_sorted = pd.DataFrame( [[0.32230754, 0.1550689, 0.10183475, 0.05471339], [-0.58547512, -0.37050409, -0.07249285, 0.00171975], [-0.48666675, 0.25507156, -0.16968889, 0.0757443]], columns=[ 'contribution_0', 'contribution_1', 'contribution_2', 'contribution_3' ], index=[0, 1, 2]) var_dict = pd.DataFrame( [[1, 0, 2, 3], [1, 0, 3, 2], [1, 0, 2, 3]], columns=['feature_0', 'feature_1', 'feature_2', 'feature_3'], index=[0, 1, 2]) x_sorted = pd.DataFrame( [[1., 3., 22., 1.], [2., 1., 2., 38.], [2., 3., 26., 1.]], columns=['feature_0', 'feature_1', 'feature_2', 'feature_3'], index=[0, 1, 2]) mask = pd.DataFrame( [[True, True, False, False], [True, True, False, False], [True, True, False, False]], columns=[ 'contribution_0', 'contribution_1', 'contribution_2', 'contribution_3' ], index=[0, 1, 2]) columns_dict = {0: 'Pclass', 1: 'Sex', 2: 'Age', 3: 'Embarked'} features_dict = { 'Pclass': 'Pclass', 'Sex': 'Sex', 'Age': 'Age', 'Embarked': 'Embarked' } output = state.summarize(contrib_sorted, var_dict, x_sorted, mask, columns_dict, features_dict) expected = pd.DataFrame( [['Sex', 1.0, 0.32230754, 'Pclass', 3.0, 0.1550689], ['Sex', 2.0, -0.58547512, 'Pclass', 1.0, -0.37050409], ['Sex', 2.0, -0.48666675, 'Pclass', 3.0, 0.25507156]], columns=[ 'feature_1', 'value_1', 'contribution_1', 'feature_2', 'value_2', 'contribution_2' ], index=[0, 1, 2], dtype=object) assert not pd.testing.assert_frame_equal(expected, output)
def test_validate_contributions_2(self): """ Unit test validate contributions 2 """ state = SmartState() contributions = np.array([[2, 1], [8, 4]]) x_pred = pd.DataFrame([[1, 2], [3, 4]], columns=['Col1', 'Col2'], index=['Id1', 'Id2']) expected_output = pd.DataFrame([[2, 1], [8, 4]], columns=['Col1', 'Col2'], index=['Id1', 'Id2']) output = state.validate_contributions(contributions, x_pred) assert not pd.testing.assert_frame_equal(expected_output, output)
def test_validate_contributions_1(self): """ Unit test validate contributions Parameters ---------- unittest : [type] [description] """ state = SmartState() x_pred = Mock() contributions = pd.DataFrame([[2, 1], [8, 4]], columns=['Col1', 'Col2'], index=['Id1', 'Id2']) expected_output = contributions output = state.validate_contributions(contributions, x_pred) assert not pd.testing.assert_frame_equal(expected_output, output)
def test_check_contributions(self, choose_state): """ Unit test check_shape_contributions 1 """ choose_state.return_value = MultiDecorator(SmartState()) shap_values = self.clf_2.get_feature_importance(Pool( self.df_encoded_2), type="ShapValues") predictor_1 = self.predictor_2 predictor_1.data = { "x": None, "ypred": None, "contributions": None, "x_preprocessed": None } predictor_1.data["x"] = self.df_2[["x1", "x2"]] predictor_1.data["x_preprocessed"] = self.df_2[["x1", "x2"]] predictor_1.data["ypred"] = self.df_2["y"] adapt_contrib = [ np.array([[-0.04395604, 0.13186813], [-0.04395604, 0.13186813], [-0.0021978, 0.01318681], [-0.0021978, 0.01318681], [-0.04395604, 0.13186813]]), np.array([[0.04395604, -0.13186813], [0.04395604, -0.13186813], [0.0021978, -0.01318681], [0.0021978, -0.01318681], [0.04395604, -0.13186813]]) ] contributions = list() for element in adapt_contrib: contributions.append(pd.DataFrame(element, columns=["x1", "x2"])) predictor_1.state = predictor_1.choose_state(adapt_contrib) predictor_1.check_contributions(contributions) with self.assertRaises(ValueError): predictor_1.check_contributions(shap_values[:, :-1])