from sklearn.ensemble import RandomForestClassifier
import pandas as pd
from pyexplainer.pyexplainer_pyexplainer import PyExplainer
import pickle
import os
cwd = os.getcwd()
parent_dir = os.path.dirname(cwd)
path_train = parent_dir + "/tests/pyexplainer_test_data/activemq-5.0.0.zip"
training_data = pd.read_csv(path_train, index_col = 'File')
dep = training_data.columns[-4]
selected_features = ["ADEV", "AvgCyclomaticModified", "AvgEssential", "AvgLineBlank", "AvgLineComment",
"CountClassBase", "CountClassCoupled", "CountClassDerived", "CountDeclClass",
"CountDeclClassMethod", "CountDeclClassVariable", "CountDeclInstanceVariable",
"CountDeclMethodDefault", "CountDeclMethodPrivate", "CountDeclMethodProtected",
"CountDeclMethodPublic", "CountInput_Mean", "CountInput_Min", "CountOutput_Min", "MAJOR_LINE",
"MaxInheritanceTree", "MaxNesting_Min", "MINOR_COMMIT", "OWN_COMMIT", "OWN_LINE",
"PercentLackOfCohesion", "RatioCommentToCode"]
all_cols = training_data.columns
for col in all_cols:
if col not in selected_features:
all_cols = all_cols.drop(col)
indep = all_cols
X_train = training_data.loc[:, indep]
y_train = training_data.loc[:, dep]
blackbox_model = RandomForestClassifier(max_depth=3, random_state=0)
blackbox_model.fit(X_train, y_train)
class_label = ['Clean', 'Defect']
path_test = parent_dir + "/tests/pyexplainer_test_data/activemq-5.1.0.zip"
testing_data = pd.read_csv(path_test, index_col = 'File')
X_test = testing_data.loc[:, indep]
y_test = testing_data.loc[:, dep]
from sklearn.ensemble import RandomForestClassifier
import pandas as pd
from pyexplainer.pyexplainer_pyexplainer import PyExplainer
import pickle
import os
import unittest
class TestVisualisation(unittest.TestCase):
"""unittest the visualisation stuff in pyexplainer"""
def test_visualise_pyobject(self):
def load_object(filename):
with open(filename, 'rb') as file:
object_o = pickle.load(file)
return (object_o)
# load rule obj
if os.path.isfile('../tests/rule_objects/rule_object.pyobject'):
loaded_rule_obj = load_object('../tests/rule_objects/rule_object.pyobject')
py_explainer = PyExplainer(X_train,
y_train,
indep,
dep,
blackbox_model,
class_label=class_label)
py_explainer.visualise(loaded_rule_obj)
def test_visualise_manually_created(self):
for explain_index in range(14, 15):
X_explain = X_test.iloc[[explain_index]]
y_explain = y_test.iloc[[explain_index]]
py_explainer = PyExplainer(X_train,
y_train,
indep,
dep,
blackbox_model,
class_label=class_label)
rule_object = py_explainer.explain(X_explain, y_explain)
py_explainer.visualise(rule_object)
if __name__ == '__main__':
unittest.main(argv=['first-arg-is-ignored'], exit=False)
C:\Users\micha\anaconda3\lib\site-packages\sklearn\linear_model\_logistic.py:763: ConvergenceWarning: lbfgs failed to converge (status=1): STOP: TOTAL NO. of ITERATIONS REACHED LIMIT. Increase the number of iterations (max_iter) or scale the data as shown in: https://scikit-learn.org/stable/modules/preprocessing.html Please also refer to the documentation for alternative solver options: https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression n_iter_i = _check_optimize_result(
HBox(children=(Label(value='Risk Score: '), FloatProgress(value=0.0, bar_style='info', layout=Layout(width='40…
C:\Users\micha\anaconda3\lib\site-packages\ipywidgets\widgets\widget.py:412: DeprecationWarning: Passing unrecognized arguments to super(SliderStyle).__init__(widget_width='60%'). object.__init__() takes exactly one argument (the instance to initialize) This is deprecated in traitlets 4.2.This error will be raised in a future release of traitlets. super(Widget, self).__init__(**kwargs)
FloatSlider(value=4.0, continuous_update=False, description='#1 Decrease the values of AvgCyclomaticModified t…
Output(layout=Layout(border='3px solid black'))
.
HBox(children=(Label(value='Risk Score: '), FloatProgress(value=0.0, bar_style='info', layout=Layout(width='40…
C:\Users\micha\anaconda3\lib\site-packages\ipywidgets\widgets\widget.py:412: DeprecationWarning: Passing unrecognized arguments to super(SliderStyle).__init__(widget_width='60%'). object.__init__() takes exactly one argument (the instance to initialize) This is deprecated in traitlets 4.2.This error will be raised in a future release of traitlets. super(Widget, self).__init__(**kwargs)
FloatSlider(value=1.0, continuous_update=False, description='#1 Decrease the values of CountClassCoupled to le…
FloatSlider(value=4.0, continuous_update=False, description='#2 Decrease the values of CountDeclMethodProtecte…
Output(layout=Layout(border='3px solid black'))
. ---------------------------------------------------------------------- Ran 2 tests in 5.005s OK