annotate plot_ml_performance.py @ 3:1c5dcef5ce0f draft

planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 271a4454eea5902e29da4b8dfa7b9124fefac6bc
author bgruening
date Tue, 07 May 2024 14:11:16 +0000
parents 62e3a4e8c54c
children f234e2e59d76
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
1 import argparse
3
1c5dcef5ce0f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 271a4454eea5902e29da4b8dfa7b9124fefac6bc
bgruening
parents: 2
diff changeset
2
0
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
3 import pandas as pd
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
4 import plotly
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
5 import plotly.graph_objs as go
3
1c5dcef5ce0f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 271a4454eea5902e29da4b8dfa7b9124fefac6bc
bgruening
parents: 2
diff changeset
6 from galaxy_ml.model_persist import load_model_from_h5
1c5dcef5ce0f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 271a4454eea5902e29da4b8dfa7b9124fefac6bc
bgruening
parents: 2
diff changeset
7 from galaxy_ml.utils import clean_params
1c5dcef5ce0f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 271a4454eea5902e29da4b8dfa7b9124fefac6bc
bgruening
parents: 2
diff changeset
8 from sklearn.metrics import (auc, confusion_matrix,
1c5dcef5ce0f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 271a4454eea5902e29da4b8dfa7b9124fefac6bc
bgruening
parents: 2
diff changeset
9 precision_recall_fscore_support, roc_curve)
0
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
10 from sklearn.preprocessing import label_binarize
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
11
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
12
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
13 def main(infile_input, infile_output, infile_trained_model):
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
14 """
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
15 Produce an interactive confusion matrix (heatmap), precision, recall, fscore and auc plots
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
16 Args:
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
17 infile_input: str, input tabular file with true labels
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
18 infile_output: str, input tabular file with predicted labels
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
19 infile_trained_model: str, input trained model file (zip)
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
20 """
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
21
3
1c5dcef5ce0f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 271a4454eea5902e29da4b8dfa7b9124fefac6bc
bgruening
parents: 2
diff changeset
22 df_input = pd.read_csv(infile_input, sep="\t", parse_dates=True)
1c5dcef5ce0f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 271a4454eea5902e29da4b8dfa7b9124fefac6bc
bgruening
parents: 2
diff changeset
23 df_output = pd.read_csv(infile_output, sep="\t", parse_dates=True)
0
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
24 true_labels = df_input.iloc[:, -1].copy()
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
25 predicted_labels = df_output.iloc[:, -1].copy()
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
26 axis_labels = list(set(true_labels))
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
27 c_matrix = confusion_matrix(true_labels, predicted_labels)
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
28 data = [
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
29 go.Heatmap(
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
30 z=c_matrix,
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
31 x=axis_labels,
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
32 y=axis_labels,
3
1c5dcef5ce0f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 271a4454eea5902e29da4b8dfa7b9124fefac6bc
bgruening
parents: 2
diff changeset
33 colorscale="Portland",
0
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
34 )
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
35 ]
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
36
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
37 layout = go.Layout(
3
1c5dcef5ce0f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 271a4454eea5902e29da4b8dfa7b9124fefac6bc
bgruening
parents: 2
diff changeset
38 title="Confusion Matrix between true and predicted class labels",
1c5dcef5ce0f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 271a4454eea5902e29da4b8dfa7b9124fefac6bc
bgruening
parents: 2
diff changeset
39 xaxis=dict(title="Predicted class labels"),
1c5dcef5ce0f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 271a4454eea5902e29da4b8dfa7b9124fefac6bc
bgruening
parents: 2
diff changeset
40 yaxis=dict(title="True class labels"),
0
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
41 )
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
42
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
43 fig = go.Figure(data=data, layout=layout)
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
44 plotly.offline.plot(fig, filename="output_confusion.html", auto_open=False)
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
45
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
46 # plot precision, recall and f_score for each class label
3
1c5dcef5ce0f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 271a4454eea5902e29da4b8dfa7b9124fefac6bc
bgruening
parents: 2
diff changeset
47 precision, recall, f_score, _ = precision_recall_fscore_support(
1c5dcef5ce0f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 271a4454eea5902e29da4b8dfa7b9124fefac6bc
bgruening
parents: 2
diff changeset
48 true_labels, predicted_labels
1c5dcef5ce0f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 271a4454eea5902e29da4b8dfa7b9124fefac6bc
bgruening
parents: 2
diff changeset
49 )
0
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
50
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
51 trace_precision = go.Scatter(
3
1c5dcef5ce0f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 271a4454eea5902e29da4b8dfa7b9124fefac6bc
bgruening
parents: 2
diff changeset
52 x=axis_labels, y=precision, mode="lines+markers", name="Precision"
0
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
53 )
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
54
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
55 trace_recall = go.Scatter(
3
1c5dcef5ce0f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 271a4454eea5902e29da4b8dfa7b9124fefac6bc
bgruening
parents: 2
diff changeset
56 x=axis_labels, y=recall, mode="lines+markers", name="Recall"
0
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
57 )
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
58
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
59 trace_fscore = go.Scatter(
3
1c5dcef5ce0f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 271a4454eea5902e29da4b8dfa7b9124fefac6bc
bgruening
parents: 2
diff changeset
60 x=axis_labels, y=f_score, mode="lines+markers", name="F-score"
0
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
61 )
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
62
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
63 layout_prf = go.Layout(
3
1c5dcef5ce0f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 271a4454eea5902e29da4b8dfa7b9124fefac6bc
bgruening
parents: 2
diff changeset
64 title="Precision, recall and f-score of true and predicted class labels",
1c5dcef5ce0f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 271a4454eea5902e29da4b8dfa7b9124fefac6bc
bgruening
parents: 2
diff changeset
65 xaxis=dict(title="Class labels"),
1c5dcef5ce0f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 271a4454eea5902e29da4b8dfa7b9124fefac6bc
bgruening
parents: 2
diff changeset
66 yaxis=dict(title="Precision, recall and f-score"),
0
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
67 )
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
68
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
69 data_prf = [trace_precision, trace_recall, trace_fscore]
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
70 fig_prf = go.Figure(data=data_prf, layout=layout_prf)
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
71 plotly.offline.plot(fig_prf, filename="output_prf.html", auto_open=False)
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
72
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
73 # plot roc and auc curves for different classes
3
1c5dcef5ce0f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 271a4454eea5902e29da4b8dfa7b9124fefac6bc
bgruening
parents: 2
diff changeset
74 classifier_object = load_model_from_h5(infile_trained_model)
1c5dcef5ce0f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 271a4454eea5902e29da4b8dfa7b9124fefac6bc
bgruening
parents: 2
diff changeset
75 model = clean_params(classifier_object)
0
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
76
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
77 # remove the last column (label column)
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
78 test_data = df_input.iloc[:, :-1]
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
79 model_items = dir(model)
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
80
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
81 try:
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
82 # find the probability estimating method
3
1c5dcef5ce0f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 271a4454eea5902e29da4b8dfa7b9124fefac6bc
bgruening
parents: 2
diff changeset
83 if "predict_proba" in model_items:
0
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
84 y_score = model.predict_proba(test_data)
3
1c5dcef5ce0f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 271a4454eea5902e29da4b8dfa7b9124fefac6bc
bgruening
parents: 2
diff changeset
85 elif "decision_function" in model_items:
0
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
86 y_score = model.decision_function(test_data)
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
87
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
88 true_labels_list = true_labels.tolist()
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
89 one_hot_labels = label_binarize(true_labels_list, classes=axis_labels)
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
90 data_roc = list()
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
91
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
92 if len(axis_labels) > 2:
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
93 fpr = dict()
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
94 tpr = dict()
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
95 roc_auc = dict()
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
96 for i in axis_labels:
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
97 fpr[i], tpr[i], _ = roc_curve(one_hot_labels[:, i], y_score[:, i])
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
98 roc_auc[i] = auc(fpr[i], tpr[i])
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
99 for i in range(len(axis_labels)):
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
100 trace = go.Scatter(
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
101 x=fpr[i],
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
102 y=tpr[i],
3
1c5dcef5ce0f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 271a4454eea5902e29da4b8dfa7b9124fefac6bc
bgruening
parents: 2
diff changeset
103 mode="lines+markers",
1c5dcef5ce0f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 271a4454eea5902e29da4b8dfa7b9124fefac6bc
bgruening
parents: 2
diff changeset
104 name="ROC curve of class {0} (AUC = {1:0.2f})".format(
1c5dcef5ce0f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 271a4454eea5902e29da4b8dfa7b9124fefac6bc
bgruening
parents: 2
diff changeset
105 i, roc_auc[i]
1c5dcef5ce0f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 271a4454eea5902e29da4b8dfa7b9124fefac6bc
bgruening
parents: 2
diff changeset
106 ),
0
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
107 )
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
108 data_roc.append(trace)
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
109 else:
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
110 try:
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
111 y_score_binary = y_score[:, 1]
3
1c5dcef5ce0f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 271a4454eea5902e29da4b8dfa7b9124fefac6bc
bgruening
parents: 2
diff changeset
112 except Exception:
0
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
113 y_score_binary = y_score
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
114 fpr, tpr, _ = roc_curve(one_hot_labels, y_score_binary, pos_label=1)
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
115 roc_auc = auc(fpr, tpr)
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
116 trace = go.Scatter(
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
117 x=fpr,
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
118 y=tpr,
3
1c5dcef5ce0f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 271a4454eea5902e29da4b8dfa7b9124fefac6bc
bgruening
parents: 2
diff changeset
119 mode="lines+markers",
1c5dcef5ce0f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 271a4454eea5902e29da4b8dfa7b9124fefac6bc
bgruening
parents: 2
diff changeset
120 name="ROC curve (AUC = {0:0.2f})".format(roc_auc),
0
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
121 )
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
122 data_roc.append(trace)
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
123
3
1c5dcef5ce0f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 271a4454eea5902e29da4b8dfa7b9124fefac6bc
bgruening
parents: 2
diff changeset
124 trace_diag = go.Scatter(x=[0, 1], y=[0, 1], mode="lines", name="Chance")
0
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
125 data_roc.append(trace_diag)
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
126 layout_roc = go.Layout(
3
1c5dcef5ce0f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 271a4454eea5902e29da4b8dfa7b9124fefac6bc
bgruening
parents: 2
diff changeset
127 title="Receiver operating characteristics (ROC) and area under curve (AUC)",
1c5dcef5ce0f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 271a4454eea5902e29da4b8dfa7b9124fefac6bc
bgruening
parents: 2
diff changeset
128 xaxis=dict(title="False positive rate"),
1c5dcef5ce0f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 271a4454eea5902e29da4b8dfa7b9124fefac6bc
bgruening
parents: 2
diff changeset
129 yaxis=dict(title="True positive rate"),
0
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
130 )
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
131
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
132 fig_roc = go.Figure(data=data_roc, layout=layout_roc)
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
133 plotly.offline.plot(fig_roc, filename="output_roc.html", auto_open=False)
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
134
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
135 except Exception as exp:
3
1c5dcef5ce0f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 271a4454eea5902e29da4b8dfa7b9124fefac6bc
bgruening
parents: 2
diff changeset
136 print(
1c5dcef5ce0f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 271a4454eea5902e29da4b8dfa7b9124fefac6bc
bgruening
parents: 2
diff changeset
137 "Plotting the ROC-AUC graph failed. This exception was raised: {}".format(
1c5dcef5ce0f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 271a4454eea5902e29da4b8dfa7b9124fefac6bc
bgruening
parents: 2
diff changeset
138 exp
1c5dcef5ce0f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 271a4454eea5902e29da4b8dfa7b9124fefac6bc
bgruening
parents: 2
diff changeset
139 )
1c5dcef5ce0f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 271a4454eea5902e29da4b8dfa7b9124fefac6bc
bgruening
parents: 2
diff changeset
140 )
0
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
141
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
142
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
143 if __name__ == "__main__":
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
144 aparser = argparse.ArgumentParser()
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
145 aparser.add_argument("-i", "--input", dest="infile_input", required=True)
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
146 aparser.add_argument("-j", "--output", dest="infile_output", required=True)
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
147 aparser.add_argument("-k", "--model", dest="infile_trained_model", required=True)
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
148 args = aparser.parse_args()
4fac53da862f planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/plotly_ml_performance_plots commit 8496ba724e35ba551172ea975b0fed091d4bbe88
bgruening
parents:
diff changeset
149 main(args.infile_input, args.infile_output, args.infile_trained_model)