rtichoke 0.1.13__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
rtichoke/__init__.py ADDED
@@ -0,0 +1,57 @@
1
+ """rtichoke is a package for interactive vizualization of performance metrics"""
2
+
3
+ from importlib.metadata import version
4
+
5
+ __version__ = version("rtichoke")
6
+
7
+ from rtichoke.discrimination.roc import create_roc_curve as create_roc_curve
8
+ from rtichoke.discrimination.roc import plot_roc_curve as plot_roc_curve
9
+
10
+ from rtichoke.discrimination.lift import create_lift_curve as create_lift_curve
11
+ from rtichoke.discrimination.lift import plot_lift_curve as plot_lift_curve
12
+
13
+ from rtichoke.discrimination.precision_recall import (
14
+ create_precision_recall_curve as create_precision_recall_curve,
15
+ )
16
+ from rtichoke.discrimination.precision_recall import (
17
+ plot_precision_recall_curve as plot_precision_recall_curve,
18
+ )
19
+
20
+ from rtichoke.discrimination.gains import create_gains_curve as create_gains_curve
21
+ from rtichoke.discrimination.gains import plot_gains_curve as plot_gains_curve
22
+
23
+ from rtichoke.calibration.calibration import (
24
+ create_calibration_curve as create_calibration_curve,
25
+ )
26
+
27
+ from rtichoke.utility.decision import create_decision_curve as create_decision_curve
28
+ from rtichoke.utility.decision import plot_decision_curve as plot_decision_curve
29
+
30
+ from rtichoke.performance_data.performance_data import (
31
+ prepare_performance_data as prepare_performance_data,
32
+ )
33
+
34
+ from rtichoke.performance_data.performance_data_times import (
35
+ prepare_performance_data_times as prepare_performance_data_times,
36
+ )
37
+
38
+ from rtichoke.summary_report.summary_report import (
39
+ create_summary_report as create_summary_report,
40
+ )
41
+
42
+ __all__ = [
43
+ "create_roc_curve",
44
+ "plot_roc_curve",
45
+ "create_lift_curve",
46
+ "plot_lift_curve",
47
+ "create_precision_recall_curve",
48
+ "plot_precision_recall_curve",
49
+ "create_gains_curve",
50
+ "plot_gains_curve",
51
+ "create_calibration_curve",
52
+ "create_decision_curve",
53
+ "plot_decision_curve",
54
+ "prepare_performance_data",
55
+ "prepare_performance_data_times",
56
+ "create_summary_report",
57
+ ]
@@ -0,0 +1,3 @@
1
+ """
2
+ Subpackage for Calibration
3
+ """
@@ -0,0 +1,284 @@
1
+ """
2
+ A module for Calibration Curves
3
+ """
4
+
5
+ from typing import Any, Dict, List, Optional
6
+ import pandas as pd
7
+ import plotly.graph_objects as go
8
+ from plotly.subplots import make_subplots
9
+ from plotly.graph_objs._figure import Figure
10
+ from rtichoke.helpers.send_post_request_to_r_rtichoke import send_requests_to_rtichoke_r
11
+
12
+
13
+ def create_calibration_curve(
14
+ probs: Dict[str, List[float]],
15
+ reals: Dict[str, List[int]],
16
+ calibration_type: str = "discrete",
17
+ size: Optional[int] = None,
18
+ color_values: Optional[List[str]] = [
19
+ "#1b9e77",
20
+ "#d95f02",
21
+ "#7570b3",
22
+ "#e7298a",
23
+ "#07004D",
24
+ "#E6AB02",
25
+ "#FE5F55",
26
+ "#54494B",
27
+ "#006E90",
28
+ "#BC96E6",
29
+ "#52050A",
30
+ "#1F271B",
31
+ "#BE7C4D",
32
+ "#63768D",
33
+ "#08A045",
34
+ "#320A28",
35
+ "#82FF9E",
36
+ "#2176FF",
37
+ "#D1603D",
38
+ "#585123",
39
+ ],
40
+ url_api: str = "http://localhost:4242/",
41
+ ) -> Figure:
42
+ """Creates Calibration Curve
43
+
44
+ Args:
45
+ probs (Dict[str, List[float]]): _description_
46
+ reals (Dict[str, List[int]]): _description_
47
+ calibration_type (str, optional): _description_. Defaults to "discrete".
48
+ size (Optional[int], optional): _description_. Defaults to None.
49
+ color_values (List[str], optional): _description_. Defaults to None.
50
+ url_api (_type_, optional): _description_. Defaults to "http://localhost:4242/".
51
+
52
+ Returns:
53
+ Figure: _description_
54
+ """
55
+
56
+ rtichoke_response = send_requests_to_rtichoke_r(
57
+ dictionary_to_send={
58
+ "probs": probs,
59
+ "reals": reals,
60
+ "size": size,
61
+ "color_values ": color_values,
62
+ },
63
+ url_api=url_api,
64
+ endpoint="create_calibration_curve_list",
65
+ )
66
+
67
+ calibration_curve_list = rtichoke_response.json()
68
+
69
+ calibration_curve_list["deciles_dat"] = pd.DataFrame.from_dict(
70
+ calibration_curve_list["deciles_dat"]
71
+ )
72
+ calibration_curve_list["smooth_dat"] = pd.DataFrame.from_dict(
73
+ calibration_curve_list["smooth_dat"]
74
+ )
75
+ calibration_curve_list["reference_data"] = pd.DataFrame.from_dict(
76
+ calibration_curve_list["reference_data"]
77
+ )
78
+ calibration_curve_list["histogram_for_calibration"] = pd.DataFrame.from_dict(
79
+ calibration_curve_list["histogram_for_calibration"]
80
+ )
81
+
82
+ calibration_curve = create_plotly_curve_from_calibration_curve_list(
83
+ calibration_curve_list=calibration_curve_list, calibration_type=calibration_type
84
+ )
85
+
86
+ return calibration_curve
87
+
88
+
89
+ def create_plotly_curve_from_calibration_curve_list(
90
+ calibration_curve_list: Dict[str, Any], calibration_type: str = "discrete"
91
+ ) -> Figure:
92
+ """Create plotly curve from calibration curve list
93
+
94
+ Args:
95
+ calibration_curve_list (Dict[str, Any]): _description_
96
+ calibration_type (str, optional): _description_. Defaults to "discrete".
97
+
98
+ Returns:
99
+ Figure: _description_
100
+ """
101
+ calibration_curve = make_subplots(
102
+ rows=2, cols=1, shared_xaxes=True, x_title="Predicted", row_heights=[0.8, 0.2]
103
+ )
104
+
105
+ calibration_curve.update_layout(
106
+ {
107
+ "xaxis": {"showgrid": False},
108
+ "yaxis": {"showgrid": False},
109
+ "barmode": "overlay",
110
+ "plot_bgcolor": "rgba(0, 0, 0, 0)",
111
+ "legend": {
112
+ "orientation": "h",
113
+ "xanchor": "center",
114
+ "yanchor": "top",
115
+ "x": 0.5,
116
+ "y": 1.3,
117
+ "bgcolor": "rgba(0, 0, 0, 0)",
118
+ },
119
+ "showlegend": calibration_curve_list["performance_type"][0] != "one model",
120
+ }
121
+ )
122
+
123
+ calibration_curve.add_trace(
124
+ go.Scatter(
125
+ x=calibration_curve_list["reference_data"]["x"].values.tolist(),
126
+ y=calibration_curve_list["reference_data"]["y"].values.tolist(),
127
+ hovertext=calibration_curve_list["reference_data"]["text"].values.tolist(),
128
+ name="Perfectly Calibrated",
129
+ legendgroup="Perfectly Calibrated",
130
+ hoverinfo="text",
131
+ line={
132
+ "width": 2,
133
+ "dash": "dot",
134
+ "color": calibration_curve_list["group_colors_vec"]["reference_line"][
135
+ 0
136
+ ],
137
+ },
138
+ showlegend=False,
139
+ ),
140
+ row=1,
141
+ col=1,
142
+ )
143
+
144
+ if calibration_type == "discrete":
145
+ for reference_group in list(calibration_curve_list["group_colors_vec"].keys()):
146
+ if any(
147
+ calibration_curve_list["deciles_dat"]["reference_group"]
148
+ == reference_group
149
+ ):
150
+ calibration_curve.add_trace(
151
+ go.Scatter(
152
+ x=calibration_curve_list["deciles_dat"]["x"][
153
+ calibration_curve_list["deciles_dat"]["reference_group"]
154
+ == reference_group
155
+ ].values.tolist(),
156
+ y=calibration_curve_list["deciles_dat"]["y"][
157
+ calibration_curve_list["deciles_dat"]["reference_group"]
158
+ == reference_group
159
+ ].values.tolist(),
160
+ hovertext=calibration_curve_list["deciles_dat"]["text"][
161
+ calibration_curve_list["deciles_dat"]["reference_group"]
162
+ == reference_group
163
+ ].values.tolist(),
164
+ name=reference_group,
165
+ legendgroup=reference_group,
166
+ hoverinfo="text",
167
+ mode="lines+markers",
168
+ marker={
169
+ "size": 10,
170
+ "color": calibration_curve_list["group_colors_vec"][
171
+ reference_group
172
+ ][0],
173
+ },
174
+ ),
175
+ row=1,
176
+ col=1,
177
+ )
178
+
179
+ if calibration_type == "smooth":
180
+ for reference_group in list(calibration_curve_list["group_colors_vec"].keys()):
181
+ if any(
182
+ calibration_curve_list["smooth_dat"]["reference_group"]
183
+ == reference_group
184
+ ):
185
+ calibration_curve.add_trace(
186
+ go.Scatter(
187
+ x=calibration_curve_list["smooth_dat"]["x"][
188
+ calibration_curve_list["smooth_dat"]["reference_group"]
189
+ == reference_group
190
+ ].values.tolist(),
191
+ y=calibration_curve_list["smooth_dat"]["y"][
192
+ calibration_curve_list["smooth_dat"]["reference_group"]
193
+ == reference_group
194
+ ].values.tolist(),
195
+ hovertext=calibration_curve_list["smooth_dat"]["text"][
196
+ calibration_curve_list["smooth_dat"]["reference_group"]
197
+ == reference_group
198
+ ].values.tolist(),
199
+ name=reference_group,
200
+ legendgroup=reference_group,
201
+ hoverinfo="text",
202
+ mode="lines",
203
+ marker={
204
+ "size": 10,
205
+ "color": calibration_curve_list["group_colors_vec"][
206
+ reference_group
207
+ ][0],
208
+ },
209
+ ),
210
+ row=1,
211
+ col=1,
212
+ )
213
+
214
+ for reference_group in list(calibration_curve_list["group_colors_vec"].keys()):
215
+ if any(
216
+ calibration_curve_list["histogram_for_calibration"]["reference_group"]
217
+ == reference_group
218
+ ):
219
+ calibration_curve.add_trace(
220
+ go.Bar(
221
+ x=calibration_curve_list["histogram_for_calibration"]["mids"][
222
+ calibration_curve_list["histogram_for_calibration"][
223
+ "reference_group"
224
+ ]
225
+ == reference_group
226
+ ].values.tolist(),
227
+ y=calibration_curve_list["histogram_for_calibration"]["counts"][
228
+ calibration_curve_list["histogram_for_calibration"][
229
+ "reference_group"
230
+ ]
231
+ == reference_group
232
+ ].values.tolist(),
233
+ hovertext=calibration_curve_list["histogram_for_calibration"][
234
+ "text"
235
+ ][
236
+ calibration_curve_list["histogram_for_calibration"][
237
+ "reference_group"
238
+ ]
239
+ == reference_group
240
+ ].values.tolist(),
241
+ name=reference_group,
242
+ width=0.01,
243
+ legendgroup=reference_group,
244
+ hoverinfo="text",
245
+ marker_color=calibration_curve_list["group_colors_vec"][
246
+ reference_group
247
+ ][0],
248
+ showlegend=False,
249
+ opacity=calibration_curve_list["histogram_opacity"][0],
250
+ ),
251
+ row=2,
252
+ col=1,
253
+ )
254
+
255
+ print(calibration_curve_list["axes_ranges"]["xaxis"])
256
+
257
+ calibration_curve.update_xaxes(
258
+ zeroline=True,
259
+ range=calibration_curve_list["axes_ranges"]["xaxis"],
260
+ zerolinewidth=1,
261
+ zerolinecolor="black",
262
+ fixedrange=False,
263
+ )
264
+ calibration_curve.update_yaxes(
265
+ zeroline=True,
266
+ range=calibration_curve_list["axes_ranges"]["yaxis"],
267
+ zerolinewidth=1,
268
+ zerolinecolor="black",
269
+ fixedrange=False,
270
+ row=1,
271
+ col=1,
272
+ )
273
+ calibration_curve.update_yaxes(title="Observed", row=1, col=1)
274
+
275
+ print("size")
276
+ print(calibration_curve_list["size"])
277
+ print(calibration_curve_list["size"][0])
278
+
279
+ calibration_curve.update_layout(
280
+ width=calibration_curve_list["size"][0][0],
281
+ height=calibration_curve_list["size"][0][0],
282
+ )
283
+
284
+ return calibration_curve
@@ -0,0 +1,3 @@
1
+ """
2
+ Subpackage for Discrimination
3
+ """
@@ -0,0 +1,114 @@
1
+ """
2
+ A module for Gains Curves
3
+ """
4
+
5
+ from typing import Dict, List, Optional
6
+ from pandas import DataFrame
7
+ from plotly.graph_objs._figure import Figure
8
+ from rtichoke.helpers.send_post_request_to_r_rtichoke import create_rtichoke_curve
9
+ from rtichoke.helpers.send_post_request_to_r_rtichoke import plot_rtichoke_curve
10
+
11
+
12
+ def create_gains_curve(
13
+ probs: Dict[str, List[float]],
14
+ reals: Dict[str, List[int]],
15
+ by: float = 0.01,
16
+ stratified_by: str = "probability_threshold",
17
+ size: Optional[int] = None,
18
+ color_values: List[str] = [
19
+ "#1b9e77",
20
+ "#d95f02",
21
+ "#7570b3",
22
+ "#e7298a",
23
+ "#07004D",
24
+ "#E6AB02",
25
+ "#FE5F55",
26
+ "#54494B",
27
+ "#006E90",
28
+ "#BC96E6",
29
+ "#52050A",
30
+ "#1F271B",
31
+ "#BE7C4D",
32
+ "#63768D",
33
+ "#08A045",
34
+ "#320A28",
35
+ "#82FF9E",
36
+ "#2176FF",
37
+ "#D1603D",
38
+ "#585123",
39
+ ],
40
+ url_api: str = "http://localhost:4242/",
41
+ ) -> Figure:
42
+ """Create Gains Curve
43
+
44
+ Args:
45
+ probs (Dict[str, List[float]]): _description_
46
+ reals (Dict[str, List[int]]): _description_
47
+ by (float, optional): _description_. Defaults to 0.01.
48
+ stratified_by (str, optional): _description_. Defaults to "probability_threshold".
49
+ size (Optional[int], optional): _description_. Defaults to None.
50
+ color_values (List[str], optional): _description_. Defaults to None.
51
+ url_api (_type_, optional): _description_. Defaults to "http://localhost:4242/".
52
+
53
+ Returns:
54
+ Figure: _description_
55
+ """
56
+ fig = create_rtichoke_curve(
57
+ probs,
58
+ reals,
59
+ by=by,
60
+ stratified_by=stratified_by,
61
+ size=size,
62
+ color_values=color_values,
63
+ url_api=url_api,
64
+ curve="gains",
65
+ )
66
+ return fig
67
+
68
+
69
+ def plot_gains_curve(
70
+ performance_data: DataFrame,
71
+ size: Optional[int] = None,
72
+ color_values: List[str] = [
73
+ "#1b9e77",
74
+ "#d95f02",
75
+ "#7570b3",
76
+ "#e7298a",
77
+ "#07004D",
78
+ "#E6AB02",
79
+ "#FE5F55",
80
+ "#54494B",
81
+ "#006E90",
82
+ "#BC96E6",
83
+ "#52050A",
84
+ "#1F271B",
85
+ "#BE7C4D",
86
+ "#63768D",
87
+ "#08A045",
88
+ "#320A28",
89
+ "#82FF9E",
90
+ "#2176FF",
91
+ "#D1603D",
92
+ "#585123",
93
+ ],
94
+ url_api: str = "http://localhost:4242/",
95
+ ) -> Figure:
96
+ """Plot Gains Curve
97
+
98
+ Args:
99
+ performance_data (DataFrame): _description_
100
+ size (Optional[int], optional): _description_. Defaults to None.
101
+ color_values (List[str], optional): _description_. Defaults to None.
102
+ url_api (_type_, optional): _description_. Defaults to "http://localhost:4242/".
103
+
104
+ Returns:
105
+ Figure: _description_
106
+ """
107
+ fig = plot_rtichoke_curve(
108
+ performance_data,
109
+ size=size,
110
+ color_values=color_values,
111
+ url_api=url_api,
112
+ curve="gains",
113
+ )
114
+ return fig
@@ -0,0 +1,114 @@
1
+ """
2
+ A module for Lift Curves
3
+ """
4
+
5
+ from typing import Dict, List, Optional
6
+ from plotly.graph_objs._figure import Figure
7
+ from pandas import DataFrame
8
+ from rtichoke.helpers.send_post_request_to_r_rtichoke import create_rtichoke_curve
9
+ from rtichoke.helpers.send_post_request_to_r_rtichoke import plot_rtichoke_curve
10
+
11
+
12
+ def create_lift_curve(
13
+ probs: Dict[str, List[float]],
14
+ reals: Dict[str, List[int]],
15
+ by: float = 0.01,
16
+ stratified_by: str = "probability_threshold",
17
+ size: Optional[int] = None,
18
+ color_values: List[str] = [
19
+ "#1b9e77",
20
+ "#d95f02",
21
+ "#7570b3",
22
+ "#e7298a",
23
+ "#07004D",
24
+ "#E6AB02",
25
+ "#FE5F55",
26
+ "#54494B",
27
+ "#006E90",
28
+ "#BC96E6",
29
+ "#52050A",
30
+ "#1F271B",
31
+ "#BE7C4D",
32
+ "#63768D",
33
+ "#08A045",
34
+ "#320A28",
35
+ "#82FF9E",
36
+ "#2176FF",
37
+ "#D1603D",
38
+ "#585123",
39
+ ],
40
+ url_api: str = "http://localhost:4242/",
41
+ ) -> Figure:
42
+ """Create Lift Curve
43
+
44
+ Args:
45
+ probs (Dict[str, List[float]]): _description_
46
+ reals (Dict[str, List[int]]): _description_
47
+ by (float, optional): _description_. Defaults to 0.01.
48
+ stratified_by (str, optional): _description_. Defaults to "probability_threshold".
49
+ size (Optional[int], optional): _description_. Defaults to None.
50
+ color_values (List[str], optional): _description_. Defaults to None.
51
+ url_api (_type_, optional): _description_. Defaults to "http://localhost:4242/".
52
+
53
+ Returns:
54
+ Figure: _description_
55
+ """
56
+ fig = create_rtichoke_curve(
57
+ probs,
58
+ reals,
59
+ by=by,
60
+ stratified_by=stratified_by,
61
+ size=size,
62
+ color_values=color_values,
63
+ url_api=url_api,
64
+ curve="lift",
65
+ )
66
+ return fig
67
+
68
+
69
+ def plot_lift_curve(
70
+ performance_data: DataFrame,
71
+ size: Optional[int] = None,
72
+ color_values: List[str] = [
73
+ "#1b9e77",
74
+ "#d95f02",
75
+ "#7570b3",
76
+ "#e7298a",
77
+ "#07004D",
78
+ "#E6AB02",
79
+ "#FE5F55",
80
+ "#54494B",
81
+ "#006E90",
82
+ "#BC96E6",
83
+ "#52050A",
84
+ "#1F271B",
85
+ "#BE7C4D",
86
+ "#63768D",
87
+ "#08A045",
88
+ "#320A28",
89
+ "#82FF9E",
90
+ "#2176FF",
91
+ "#D1603D",
92
+ "#585123",
93
+ ],
94
+ url_api: str = "http://localhost:4242/",
95
+ ) -> Figure:
96
+ """Plot Lift Curve
97
+
98
+ Args:
99
+ performance_data (DataFrame): _description_
100
+ size (Optional[int], optional): _description_. Defaults to None.
101
+ color_values (List[str], optional): _description_. Defaults to None.
102
+ url_api (_type_, optional): _description_. Defaults to "http://localhost:4242/".
103
+
104
+ Returns:
105
+ Figure: _description_
106
+ """
107
+ fig = plot_rtichoke_curve(
108
+ performance_data,
109
+ size=size,
110
+ color_values=color_values,
111
+ url_api=url_api,
112
+ curve="lift",
113
+ )
114
+ return fig
@@ -0,0 +1,114 @@
1
+ """
2
+ A module for Precision Recall Curves
3
+ """
4
+
5
+ from typing import Dict, List, Optional
6
+ from plotly.graph_objs._figure import Figure
7
+ from pandas import DataFrame
8
+ from rtichoke.helpers.send_post_request_to_r_rtichoke import create_rtichoke_curve
9
+ from rtichoke.helpers.send_post_request_to_r_rtichoke import plot_rtichoke_curve
10
+
11
+
12
+ def create_precision_recall_curve(
13
+ probs: Dict[str, List[float]],
14
+ reals: Dict[str, List[int]],
15
+ by: float = 0.01,
16
+ stratified_by: str = "probability_threshold",
17
+ size: Optional[int] = None,
18
+ color_values: List[str] = [
19
+ "#1b9e77",
20
+ "#d95f02",
21
+ "#7570b3",
22
+ "#e7298a",
23
+ "#07004D",
24
+ "#E6AB02",
25
+ "#FE5F55",
26
+ "#54494B",
27
+ "#006E90",
28
+ "#BC96E6",
29
+ "#52050A",
30
+ "#1F271B",
31
+ "#BE7C4D",
32
+ "#63768D",
33
+ "#08A045",
34
+ "#320A28",
35
+ "#82FF9E",
36
+ "#2176FF",
37
+ "#D1603D",
38
+ "#585123",
39
+ ],
40
+ url_api: str = "http://localhost:4242/",
41
+ ) -> Figure:
42
+ """Create Precision Recall Curve
43
+
44
+ Args:
45
+ probs (Dict[str, List[float]]): _description_
46
+ reals (Dict[str, List[int]]): _description_
47
+ by (float, optional): _description_. Defaults to 0.01.
48
+ stratified_by (str, optional): _description_. Defaults to "probability_threshold".
49
+ size (Optional[int], optional): _description_. Defaults to None.
50
+ color_values (List[str], optional): _description_. Defaults to None.
51
+ url_api (_type_, optional): _description_. Defaults to "http://localhost:4242/".
52
+
53
+ Returns:
54
+ Figure: _description_
55
+ """
56
+ fig = create_rtichoke_curve(
57
+ probs,
58
+ reals,
59
+ by=by,
60
+ stratified_by=stratified_by,
61
+ size=size,
62
+ color_values=color_values,
63
+ url_api=url_api,
64
+ curve="precision recall",
65
+ )
66
+ return fig
67
+
68
+
69
+ def plot_precision_recall_curve(
70
+ performance_data: DataFrame,
71
+ size: Optional[int] = None,
72
+ color_values: List[str] = [
73
+ "#1b9e77",
74
+ "#d95f02",
75
+ "#7570b3",
76
+ "#e7298a",
77
+ "#07004D",
78
+ "#E6AB02",
79
+ "#FE5F55",
80
+ "#54494B",
81
+ "#006E90",
82
+ "#BC96E6",
83
+ "#52050A",
84
+ "#1F271B",
85
+ "#BE7C4D",
86
+ "#63768D",
87
+ "#08A045",
88
+ "#320A28",
89
+ "#82FF9E",
90
+ "#2176FF",
91
+ "#D1603D",
92
+ "#585123",
93
+ ],
94
+ url_api: str = "http://localhost:4242/",
95
+ ) -> Figure:
96
+ """Plot Precision Recall Curve
97
+
98
+ Args:
99
+ performance_data (DataFrame): _description_
100
+ size (Optional[int], optional): _description_. Defaults to None.
101
+ color_values (List[str], optional): _description_. Defaults to None.
102
+ url_api (_type_, optional): _description_. Defaults to "http://localhost:4242/".
103
+
104
+ Returns:
105
+ Figure: _description_
106
+ """
107
+ fig = plot_rtichoke_curve(
108
+ performance_data,
109
+ size=size,
110
+ color_values=color_values,
111
+ url_api=url_api,
112
+ curve="precision recall",
113
+ )
114
+ return fig