comparison dashboard.py @ 0:915447b14520 draft

planemo upload for repository https://github.com/goeckslab/Galaxy-Pycaret commit d79b0f722b7d09505a526d1a4332f87e548a3df1
author goeckslab
date Wed, 11 Dec 2024 05:00:00 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:915447b14520
1 import logging
2 from typing import Any, Dict, Optional
3
4 from pycaret.utils.generic import get_label_encoder
5
6 logging.basicConfig(level=logging.DEBUG)
7 LOG = logging.getLogger(__name__)
8
9
10 def generate_classifier_explainer_dashboard(
11 exp,
12 estimator,
13 display_format: str = "dash",
14 dashboard_kwargs: Optional[Dict[str, Any]] = None,
15 run_kwargs: Optional[Dict[str, Any]] = None,
16 **kwargs,):
17
18 """
19 This function is changed from pycaret.classification.oop.dashboard()
20
21 This function generates the interactive dashboard for a trained model.
22 The dashboard is implemented using
23 ExplainerDashboard (explainerdashboard.readthedocs.io)
24
25
26 estimator: scikit-learn compatible object
27 Trained model object
28
29
30 display_format: str, default = 'dash'
31 Render mode for the dashboard. The default is set to ``dash``
32 which will
33 render a dashboard in browser. There are four possible options:
34
35 - 'dash' - displays the dashboard in browser
36 - 'inline' - displays the dashboard in the jupyter notebook cell.
37 - 'jupyterlab' - displays the dashboard in jupyterlab pane.
38 - 'external' - displays the dashboard in a separate tab.
39 (use in Colab)
40
41
42 dashboard_kwargs: dict, default = {} (empty dict)
43 Dictionary of arguments passed to the ``ExplainerDashboard`` class.
44
45
46 run_kwargs: dict, default = {} (empty dict)
47 Dictionary of arguments passed to the ``run``
48 method of ``ExplainerDashboard``.
49
50
51 **kwargs:
52 Additional keyword arguments to pass to the ``ClassifierExplainer``
53 or ``RegressionExplainer`` class.
54
55
56 Returns:
57 ExplainerDashboard
58 """
59
60 dashboard_kwargs = dashboard_kwargs or {}
61 run_kwargs = run_kwargs or {}
62
63 from explainerdashboard import ClassifierExplainer, ExplainerDashboard
64
65 le = get_label_encoder(exp.pipeline)
66 if le:
67 labels_ = list(le.classes_)
68 else:
69 labels_ = None
70
71 # Replaceing chars which dash doesnt accept for column name `.` , `{`, `}`
72
73 X_test_df = exp.X_test_transformed.copy()
74 LOG.info(X_test_df)
75 X_test_df.columns = [
76 col.replace(".", "__").replace("{", "__").replace("}", "__")
77 for col in X_test_df.columns
78 ]
79
80 explainer = ClassifierExplainer(
81 estimator, X_test_df, exp.y_test_transformed, labels=labels_, **kwargs
82 )
83 return ExplainerDashboard(
84 explainer, mode=display_format,
85 contributions=False, whatif=False,
86 **dashboard_kwargs
87 )
88
89
90 def generate_regression_explainer_dashboard(
91 exp,
92 estimator,
93 display_format: str = "dash",
94 dashboard_kwargs: Optional[Dict[str, Any]] = None,
95 run_kwargs: Optional[Dict[str, Any]] = None,
96 **kwargs,):
97
98 """
99 This function is changed from pycaret.regression.oop.dashboard()
100
101 This function generates the interactive dashboard for a trained model.
102 The dashboard is implemented using ExplainerDashboard
103 (explainerdashboard.readthedocs.io)
104
105
106 estimator: scikit-learn compatible object
107 Trained model object
108
109
110 display_format: str, default = 'dash'
111 Render mode for the dashboard. The default is set to ``dash``
112 which will
113 render a dashboard in browser. There are four possible options:
114
115 - 'dash' - displays the dashboard in browser
116 - 'inline' - displays the dashboard in the jupyter notebook cell.
117 - 'jupyterlab' - displays the dashboard in jupyterlab pane.
118 - 'external' - displays the dashboard in a separate tab.
119 (use in Colab)
120
121
122 dashboard_kwargs: dict, default = {} (empty dict)
123 Dictionary of arguments passed to the ``ExplainerDashboard`` class.
124
125
126 run_kwargs: dict, default = {} (empty dict)
127 Dictionary of arguments passed to the ``run`` method
128 of ``ExplainerDashboard``.
129
130
131 **kwargs:
132 Additional keyword arguments to pass to the
133 ``ClassifierExplainer`` or
134 ``RegressionExplainer`` class.
135
136
137 Returns:
138 ExplainerDashboard
139 """
140
141 dashboard_kwargs = dashboard_kwargs or {}
142 run_kwargs = run_kwargs or {}
143
144 from explainerdashboard import ExplainerDashboard, RegressionExplainer
145
146 # Replaceing chars which dash doesnt accept for column name `.` , `{`, `}`
147 X_test_df = exp.X_test_transformed.copy()
148 X_test_df.columns = [
149 col.replace(".", "__").replace("{", "__").replace("}", "__")
150 for col in X_test_df.columns
151 ]
152 explainer = RegressionExplainer(
153 estimator, X_test_df, exp.y_test_transformed, **kwargs
154 )
155 return ExplainerDashboard(
156 explainer, mode=display_format, contributions=False,
157 whatif=False, shap_interaction=False, decision_trees=False,
158 **dashboard_kwargs
159 )