Skip to content

Commit cf631a1

Browse files
Kun-Lung WuKun-Lung Wu
authored andcommitted
esnure predict generates the same result from sklearn and codeflare pipelines
1 parent b11067f commit cf631a1

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import pytest
2+
import ray
3+
4+
# Taking an example from sklearn pipeline to assert that
5+
# the classification report from a rediction from sklearn pipeline is
6+
# the same as that from the converted codeflare pipeline
7+
8+
from sklearn import set_config
9+
set_config(display='diagram')
10+
from sklearn.datasets import make_classification
11+
from sklearn.model_selection import train_test_split
12+
from sklearn.feature_selection import SelectKBest, f_classif
13+
from sklearn.pipeline import make_pipeline
14+
from sklearn.svm import LinearSVC
15+
from sklearn.metrics import classification_report
16+
17+
import codeflare.pipelines.Datamodel as dm
18+
import codeflare.pipelines.Runtime as rt
19+
from codeflare.pipelines.Datamodel import Xy
20+
from codeflare.pipelines.Datamodel import XYRef
21+
from codeflare.pipelines.Runtime import ExecutionType
22+
23+
#
24+
# prediction from an sklearn pipeline
25+
#
26+
27+
def test_pipeline_predict():
28+
29+
ray.shutdown()
30+
ray.init()
31+
32+
#
33+
# prediction from an sklearn pipeline
34+
#
35+
X, y = make_classification(
36+
n_features=20, n_informative=3, n_redundant=0, n_classes=2,
37+
n_clusters_per_class=2, random_state=42)
38+
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
39+
40+
anova_filter = SelectKBest(f_classif, k=3)
41+
clf = LinearSVC()
42+
43+
anova_svm = make_pipeline(anova_filter, clf)
44+
anova_svm.fit(X_train, y_train)
45+
46+
y_pred = anova_svm.predict(X_test)
47+
48+
report_sklearn = classification_report(y_test, y_pred)
49+
print(report_sklearn)
50+
51+
#
52+
# constructing a codeflare pipeline
53+
#
54+
pipeline = dm.Pipeline()
55+
node_anova_filter = dm.EstimatorNode('anova_filter', anova_filter)
56+
node_clf = dm.EstimatorNode('clf', clf)
57+
pipeline.add_edge(node_anova_filter, node_clf)
58+
59+
pipeline_input = dm.PipelineInput()
60+
xy = dm.Xy(X_train, y_train)
61+
62+
pipeline_input.add_xy_arg(node_anova_filter, xy)
63+
64+
pipeline_output = rt.execute_pipeline(pipeline, ExecutionType.FIT, pipeline_input)
65+
66+
node_clf_output = pipeline_output.get_xyrefs(node_clf)
67+
68+
Xout = ray.get(node_clf_output[0].get_Xref())
69+
yout = ray.get(node_clf_output[0].get_yref())
70+
71+
selected_pipeline = rt.select_pipeline(pipeline_output, node_clf_output[0])
72+
73+
pipeline_input = dm.PipelineInput()
74+
pipeline_input.add_xy_arg(node_anova_filter, dm.Xy(X_test, y_test))
75+
76+
predict_output = rt.execute_pipeline(selected_pipeline, ExecutionType.PREDICT, pipeline_input)
77+
78+
predict_clf_output = predict_output.get_xyrefs(node_clf)
79+
80+
#y_pred = ray.get(predict_clf_output[0].get_yref())
81+
y_pred = ray.get(predict_clf_output[0].get_Xref())
82+
83+
84+
report_codeflare = classification_report(y_test, y_pred)
85+
86+
print(report_codeflare)
87+
88+
assert(report_sklearn == report_codeflare)
89+
90+
ray.shutdown()
91+
92+
93+
if __name__ == "__main__":
94+
sys.exit(pytest.main(["-v", __file__]))
95+

0 commit comments

Comments
 (0)