statstudio 0.1.0__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.
- statstudio/__init__.py +9 -0
- statstudio/__main__.py +3 -0
- statstudio/app.py +83 -0
- statstudio/cli.py +116 -0
- statstudio/core/__init__.py +3 -0
- statstudio/core/archive.py +27 -0
- statstudio/core/commands.py +67 -0
- statstudio/core/formula_catalog.py +74 -0
- statstudio/core/formulas.py +605 -0
- statstudio/core/joins.py +123 -0
- statstudio/core/project.py +358 -0
- statstudio/core/runtime.py +321 -0
- statstudio/core/scripting.py +594 -0
- statstudio/core/table.py +796 -0
- statstudio/core/transform.py +450 -0
- statstudio/feature_help.py +595 -0
- statstudio/io/__init__.py +3 -0
- statstudio/io/docx.py +201 -0
- statstudio/io/export.py +99 -0
- statstudio/io/file_import.py +199 -0
- statstudio/io/odbc.py +334 -0
- statstudio/io/sample_data.py +164 -0
- statstudio/io/table_io.py +118 -0
- statstudio/plots/__init__.py +19 -0
- statstudio/plots/advanced.py +651 -0
- statstudio/plots/basic.py +957 -0
- statstudio/plots/bayesian_plots.py +94 -0
- statstudio/plots/common.py +334 -0
- statstudio/plots/diagnostics.py +214 -0
- statstudio/plots/doe_plots.py +235 -0
- statstudio/plots/plotly_builders.py +719 -0
- statstudio/plots/predictive_plots.py +72 -0
- statstudio/plots/registry.py +251 -0
- statstudio/plugins.py +111 -0
- statstudio/resources/help_topics.json +13195 -0
- statstudio/resources/logo.ico +0 -0
- statstudio/resources/logo.png +0 -0
- statstudio/resources/statstudio.svg +8 -0
- statstudio/stats/__init__.py +42 -0
- statstudio/stats/bayesian.py +1094 -0
- statstudio/stats/bayesian_optimization.py +590 -0
- statstudio/stats/common.py +140 -0
- statstudio/stats/correlation.py +135 -0
- statstudio/stats/custom_doe.py +1714 -0
- statstudio/stats/descriptive.py +200 -0
- statstudio/stats/desirability.py +132 -0
- statstudio/stats/distributions.py +331 -0
- statstudio/stats/doe.py +1070 -0
- statstudio/stats/hypothesis.py +1305 -0
- statstudio/stats/multivariate.py +307 -0
- statstudio/stats/optimal_doe.py +264 -0
- statstudio/stats/power.py +194 -0
- statstudio/stats/predictive.py +605 -0
- statstudio/stats/profiler.py +379 -0
- statstudio/stats/quality.py +1218 -0
- statstudio/stats/regression.py +1600 -0
- statstudio/stats/results.py +235 -0
- statstudio/stats/simulator.py +95 -0
- statstudio/stats/survival.py +637 -0
- statstudio/stats/timeseries.py +643 -0
- statstudio/stats/tolerance.py +154 -0
- statstudio/ui/__init__.py +0 -0
- statstudio/ui/analysis_hub.py +271 -0
- statstudio/ui/command_catalog.py +460 -0
- statstudio/ui/command_palette.py +55 -0
- statstudio/ui/console.py +239 -0
- statstudio/ui/dialogs/__init__.py +0 -0
- statstudio/ui/dialogs/advanced_dialogs.py +392 -0
- statstudio/ui/dialogs/base.py +537 -0
- statstudio/ui/dialogs/bayesian_dialogs.py +488 -0
- statstudio/ui/dialogs/bayesian_optimization_dialog.py +299 -0
- statstudio/ui/dialogs/custom_doe_dialog.py +467 -0
- statstudio/ui/dialogs/doe_dialogs.py +220 -0
- statstudio/ui/dialogs/extended_dialogs.py +665 -0
- statstudio/ui/dialogs/graph_builder.py +139 -0
- statstudio/ui/dialogs/help_dialog.py +207 -0
- statstudio/ui/dialogs/help_topics.py +280 -0
- statstudio/ui/dialogs/lazy.py +184 -0
- statstudio/ui/dialogs/odbc_dialog.py +311 -0
- statstudio/ui/dialogs/plot_dialog.py +1385 -0
- statstudio/ui/dialogs/report_composer.py +218 -0
- statstudio/ui/dialogs/script_dialogs.py +267 -0
- statstudio/ui/dialogs/stats_dialogs.py +1688 -0
- statstudio/ui/dialogs/table_dialogs.py +997 -0
- statstudio/ui/dialogs/window.py +58 -0
- statstudio/ui/docks/__init__.py +0 -0
- statstudio/ui/formula_editor.py +121 -0
- statstudio/ui/icons.py +257 -0
- statstudio/ui/jobs.py +179 -0
- statstudio/ui/main_window.py +3934 -0
- statstudio/ui/plot_interaction.py +178 -0
- statstudio/ui/profiler_widget.py +437 -0
- statstudio/ui/recent.py +82 -0
- statstudio/ui/reports/__init__.py +0 -0
- statstudio/ui/reports/report_view.py +1632 -0
- statstudio/ui/script_jobs.py +35 -0
- statstudio/ui/scripts_panel.py +271 -0
- statstudio/ui/table_document.py +233 -0
- statstudio/ui/table_model.py +285 -0
- statstudio/ui/table_view.py +285 -0
- statstudio/ui/theme.py +645 -0
- statstudio/ui/welcome.py +453 -0
- statstudio/ui/window_controls.py +53 -0
- statstudio/ui/workers.py +53 -0
- statstudio-0.1.0.dist-info/METADATA +261 -0
- statstudio-0.1.0.dist-info/RECORD +110 -0
- statstudio-0.1.0.dist-info/WHEEL +5 -0
- statstudio-0.1.0.dist-info/entry_points.txt +5 -0
- statstudio-0.1.0.dist-info/licenses/LICENSE +674 -0
- statstudio-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,719 @@
|
|
|
1
|
+
"""Native Plotly traces for the core Graph Builder types.
|
|
2
|
+
|
|
3
|
+
The Matplotlib builders remain the in-app renderer. These builders power the
|
|
4
|
+
interactive HTML export, where the previous Matplotlib-to-Plotly conversion was
|
|
5
|
+
lossy (blank heatmaps, failing histograms, unsupported 3-D axes).
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from typing import Callable
|
|
11
|
+
|
|
12
|
+
import numpy as np
|
|
13
|
+
import pandas as pd
|
|
14
|
+
import plotly.graph_objects as go
|
|
15
|
+
|
|
16
|
+
from statstudio.plots.common import COLORS, error_column_name, numeric
|
|
17
|
+
from statstudio.stats.results import PlotSpec
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _positional_ids(spec: PlotSpec, index) -> list:
|
|
21
|
+
if spec.row_ids is not None and len(spec.row_ids) == len(index):
|
|
22
|
+
return list(spec.row_ids)
|
|
23
|
+
return list(index)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _ids_for(spec: PlotSpec, data: pd.DataFrame, index) -> list:
|
|
27
|
+
ids = _positional_ids(spec, data.index)
|
|
28
|
+
return pd.Series(ids, index=data.index).loc[index].tolist()
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _hover_data(spec: PlotSpec, row_ids: list) -> tuple[list, str]:
|
|
32
|
+
styles = spec.row_styles or {}
|
|
33
|
+
custom = []
|
|
34
|
+
for row_id in row_ids:
|
|
35
|
+
label = (styles.get(row_id) or {}).get("label") or f"Row {row_id}"
|
|
36
|
+
custom.append([str(row_id), label])
|
|
37
|
+
return custom, "%{customdata[1]}<extra></extra>"
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _figure(spec: PlotSpec) -> go.Figure:
|
|
41
|
+
params = spec.params or {}
|
|
42
|
+
title = spec.title or params.get("title") or ""
|
|
43
|
+
figure = go.Figure()
|
|
44
|
+
figure.update_layout(
|
|
45
|
+
template="plotly_white",
|
|
46
|
+
margin=dict(l=60, r=20, t=50 if title else 20, b=60),
|
|
47
|
+
title=title or None,
|
|
48
|
+
legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1),
|
|
49
|
+
)
|
|
50
|
+
if params.get("legend_position") == "Outside right":
|
|
51
|
+
figure.update_layout(legend=dict(orientation="v", x=1.02, y=1))
|
|
52
|
+
return figure
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _style_axes(figure: go.Figure, spec: PlotSpec) -> None:
|
|
56
|
+
params = spec.params or {}
|
|
57
|
+
x_label = params.get("x_label") or params.get("x") or params.get("column") or ""
|
|
58
|
+
y_label = params.get("y_label") or params.get("y") or ""
|
|
59
|
+
if x_label:
|
|
60
|
+
figure.update_xaxes(title_text=str(x_label))
|
|
61
|
+
if y_label:
|
|
62
|
+
figure.update_yaxes(title_text=str(y_label))
|
|
63
|
+
try:
|
|
64
|
+
x_size = float(params.get("x_label_size"))
|
|
65
|
+
except (TypeError, ValueError):
|
|
66
|
+
x_size = None
|
|
67
|
+
try:
|
|
68
|
+
y_size = float(params.get("y_label_size"))
|
|
69
|
+
except (TypeError, ValueError):
|
|
70
|
+
y_size = None
|
|
71
|
+
if x_size and x_size > 0:
|
|
72
|
+
figure.update_xaxes(title_font=dict(size=x_size))
|
|
73
|
+
if y_size and y_size > 0:
|
|
74
|
+
figure.update_yaxes(title_font=dict(size=y_size))
|
|
75
|
+
x_scale = params.get("x_scale") or ("log" if params.get("log_x") else None)
|
|
76
|
+
y_scale = params.get("y_scale") or ("log" if params.get("log_y") else None)
|
|
77
|
+
if x_scale in {"log", "linear"}:
|
|
78
|
+
figure.update_xaxes(type=x_scale)
|
|
79
|
+
if y_scale in {"log", "linear"}:
|
|
80
|
+
figure.update_yaxes(type=y_scale)
|
|
81
|
+
ref_lines = params.get("ref_lines") or []
|
|
82
|
+
for value in ref_lines:
|
|
83
|
+
try:
|
|
84
|
+
figure.add_hline(y=float(value), line_dash="dash", line_color="#666666")
|
|
85
|
+
except (TypeError, ValueError):
|
|
86
|
+
continue
|
|
87
|
+
ref_lines_x = params.get("ref_lines_x") or []
|
|
88
|
+
for value in ref_lines_x:
|
|
89
|
+
try:
|
|
90
|
+
figure.add_vline(x=float(value), line_dash="dash", line_color="#666666")
|
|
91
|
+
except (TypeError, ValueError):
|
|
92
|
+
continue
|
|
93
|
+
for annotation in params.get("annotations") or []:
|
|
94
|
+
if not isinstance(annotation, dict):
|
|
95
|
+
continue
|
|
96
|
+
try:
|
|
97
|
+
figure.add_annotation(
|
|
98
|
+
x=float(annotation.get("x")),
|
|
99
|
+
y=float(annotation.get("y")),
|
|
100
|
+
text=str(annotation.get("text", "")),
|
|
101
|
+
showarrow=True,
|
|
102
|
+
arrowhead=2,
|
|
103
|
+
font=dict(size=10),
|
|
104
|
+
)
|
|
105
|
+
except (TypeError, ValueError):
|
|
106
|
+
continue
|
|
107
|
+
_apply_ranges(figure, spec)
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def _style_scene_axes(figure: go.Figure, spec: PlotSpec) -> None:
|
|
111
|
+
params = spec.params or {}
|
|
112
|
+
x_label = params.get("x_label") or params.get("x") or ""
|
|
113
|
+
y_label = params.get("y_label") or params.get("y") or ""
|
|
114
|
+
z_label = params.get("z_label") or params.get("z") or ""
|
|
115
|
+
scene = {
|
|
116
|
+
"xaxis_title": str(x_label),
|
|
117
|
+
"yaxis_title": str(y_label),
|
|
118
|
+
"zaxis_title": str(z_label),
|
|
119
|
+
}
|
|
120
|
+
for key, axis in (("x_min", "xaxis"), ("x_max", "xaxis"), ("y_min", "yaxis"), ("y_max", "yaxis")):
|
|
121
|
+
value = params.get(key)
|
|
122
|
+
if value is None:
|
|
123
|
+
continue
|
|
124
|
+
try:
|
|
125
|
+
scene.setdefault(axis, {}).setdefault("range", [None, None])
|
|
126
|
+
except Exception:
|
|
127
|
+
continue
|
|
128
|
+
scene[axis]["range"][0 if key.endswith("min") else 1] = float(value)
|
|
129
|
+
figure.update_layout(scene=scene)
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def _trace_values(traces, axis: str) -> list:
|
|
133
|
+
values = []
|
|
134
|
+
for trace in traces:
|
|
135
|
+
try:
|
|
136
|
+
raw = getattr(trace, axis, None)
|
|
137
|
+
except Exception:
|
|
138
|
+
raw = None
|
|
139
|
+
if raw is None:
|
|
140
|
+
continue
|
|
141
|
+
try:
|
|
142
|
+
array = np.asarray(raw, dtype=float).ravel()
|
|
143
|
+
except (TypeError, ValueError):
|
|
144
|
+
continue
|
|
145
|
+
array = array[np.isfinite(array)]
|
|
146
|
+
if array.size:
|
|
147
|
+
values.append(array)
|
|
148
|
+
return values
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def _range_pair(values) -> list | None:
|
|
152
|
+
if not values:
|
|
153
|
+
return None
|
|
154
|
+
combined = np.concatenate(values)
|
|
155
|
+
if combined.size == 0:
|
|
156
|
+
return None
|
|
157
|
+
low = float(combined.min())
|
|
158
|
+
high = float(combined.max())
|
|
159
|
+
if high <= low:
|
|
160
|
+
pad = abs(low) * 0.05 or 1.0
|
|
161
|
+
else:
|
|
162
|
+
pad = (high - low) * 0.05
|
|
163
|
+
return [low - pad, high + pad]
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def _apply_ranges(figure: go.Figure, spec: PlotSpec) -> None:
|
|
167
|
+
params = spec.params or {}
|
|
168
|
+
traces = list(figure.data)
|
|
169
|
+
if not traces:
|
|
170
|
+
return
|
|
171
|
+
if any(getattr(trace, "type", "") == "scatter3d" for trace in traces):
|
|
172
|
+
return
|
|
173
|
+
if any(getattr(trace, "type", "") == "histogram" for trace in traces):
|
|
174
|
+
x_values = _trace_values(traces, "x")
|
|
175
|
+
x_range = _range_pair(x_values)
|
|
176
|
+
if x_range is not None:
|
|
177
|
+
figure.update_xaxes(range=x_range)
|
|
178
|
+
return
|
|
179
|
+
x_range = _range_pair(_trace_values(traces, "x"))
|
|
180
|
+
x_min, x_max = params.get("x_min"), params.get("x_max")
|
|
181
|
+
y_min, y_max = params.get("y_min"), params.get("y_max")
|
|
182
|
+
if x_range is not None:
|
|
183
|
+
if x_min is not None:
|
|
184
|
+
x_range[0] = float(x_min)
|
|
185
|
+
if x_max is not None:
|
|
186
|
+
x_range[1] = float(x_max)
|
|
187
|
+
figure.update_xaxes(range=x_range)
|
|
188
|
+
y_range_data = _range_pair(_trace_values(traces, "y"))
|
|
189
|
+
if y_range_data is not None:
|
|
190
|
+
if y_min is not None:
|
|
191
|
+
y_range_data[0] = float(y_min)
|
|
192
|
+
if y_max is not None:
|
|
193
|
+
y_range_data[1] = float(y_max)
|
|
194
|
+
figure.update_yaxes(range=y_range_data)
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
def _marker_size(params: dict) -> float:
|
|
198
|
+
try:
|
|
199
|
+
return max(4.0, float(params.get("marker_size", 28)) ** 0.5)
|
|
200
|
+
except Exception:
|
|
201
|
+
return 6.0
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def _float_param(params: dict, key: str, default: float) -> float:
|
|
205
|
+
try:
|
|
206
|
+
return float(params.get(key, default))
|
|
207
|
+
except (TypeError, ValueError):
|
|
208
|
+
return float(default)
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
def _series_mode(show_line: bool, show_points: bool) -> str | None:
|
|
212
|
+
if show_line and show_points:
|
|
213
|
+
return "lines+markers"
|
|
214
|
+
if show_line:
|
|
215
|
+
return "lines"
|
|
216
|
+
if show_points:
|
|
217
|
+
return "markers"
|
|
218
|
+
return None
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def _y1_mode(params: dict, kind: str) -> str | None:
|
|
222
|
+
default_line = kind == "line"
|
|
223
|
+
default_points = kind == "scatter"
|
|
224
|
+
return _series_mode(
|
|
225
|
+
bool(params.get("y1_show_line", default_line)),
|
|
226
|
+
bool(params.get("y1_show_points", default_points)),
|
|
227
|
+
)
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def _grouped(data: pd.DataFrame, group_name: str | None):
|
|
231
|
+
if group_name and group_name in data.columns:
|
|
232
|
+
return list(data.groupby(group_name, dropna=False, sort=True))
|
|
233
|
+
return [(None, data)]
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
def scatter(spec: PlotSpec) -> go.Figure:
|
|
237
|
+
data = spec.data
|
|
238
|
+
params = spec.params
|
|
239
|
+
x_name, y_name = params.get("x"), params.get("y")
|
|
240
|
+
figure = _figure(spec)
|
|
241
|
+
mode = _y1_mode(params, "scatter")
|
|
242
|
+
x_values = data[x_name]
|
|
243
|
+
if params.get("jitter") and pd.api.types.is_numeric_dtype(x_values):
|
|
244
|
+
values = pd.to_numeric(x_values, errors="coerce")
|
|
245
|
+
finite = values.dropna()
|
|
246
|
+
span = float(finite.max() - finite.min()) if len(finite) else 0.0
|
|
247
|
+
width = span * 0.02 if span > 0 else 0.02
|
|
248
|
+
rng = np.random.default_rng(0)
|
|
249
|
+
x_values = values + pd.Series(rng.uniform(-width, width, len(values)), index=values.index)
|
|
250
|
+
if mode:
|
|
251
|
+
for position, (label, chunk) in enumerate(_grouped(data, params.get("group"))):
|
|
252
|
+
ordered = chunk.sort_values(x_name) if "lines" in mode else chunk
|
|
253
|
+
row_ids = _ids_for(spec, data, ordered.index)
|
|
254
|
+
custom, template = _hover_data(spec, row_ids)
|
|
255
|
+
series_color = params.get("y1_color") or COLORS[position % len(COLORS)]
|
|
256
|
+
figure.add_trace(
|
|
257
|
+
go.Scatter(
|
|
258
|
+
x=x_values.loc[ordered.index],
|
|
259
|
+
y=numeric(ordered, y_name),
|
|
260
|
+
mode=mode,
|
|
261
|
+
name=str(label) if label is not None else y_name,
|
|
262
|
+
line=dict(color=series_color, width=_float_param(params, "y1_line_width", 1.8)),
|
|
263
|
+
marker=dict(size=_marker_size(params), color=series_color, opacity=0.85),
|
|
264
|
+
customdata=custom,
|
|
265
|
+
hovertemplate=template,
|
|
266
|
+
)
|
|
267
|
+
)
|
|
268
|
+
_add_secondary(figure, spec)
|
|
269
|
+
_style_axes(figure, spec)
|
|
270
|
+
return figure
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
def line(spec: PlotSpec) -> go.Figure:
|
|
274
|
+
data = spec.data
|
|
275
|
+
params = spec.params
|
|
276
|
+
x_name, y_name = params.get("x"), params.get("y")
|
|
277
|
+
figure = _figure(spec)
|
|
278
|
+
mode = _y1_mode(params, "line")
|
|
279
|
+
if mode:
|
|
280
|
+
ordered = data.sort_values(x_name)
|
|
281
|
+
ids = _ids_for(spec, data, ordered.index)
|
|
282
|
+
custom, template = _hover_data(spec, ids)
|
|
283
|
+
series_color = params.get("y1_color") or COLORS[0]
|
|
284
|
+
figure.add_trace(
|
|
285
|
+
go.Scatter(
|
|
286
|
+
x=ordered[x_name],
|
|
287
|
+
y=numeric(ordered, y_name),
|
|
288
|
+
mode=mode,
|
|
289
|
+
name=y_name,
|
|
290
|
+
line=dict(color=series_color, width=_float_param(params, "y1_line_width", 1.8)),
|
|
291
|
+
marker=dict(size=_marker_size(params), color=series_color),
|
|
292
|
+
customdata=custom,
|
|
293
|
+
hovertemplate=template,
|
|
294
|
+
)
|
|
295
|
+
)
|
|
296
|
+
_add_secondary(figure, spec)
|
|
297
|
+
_style_axes(figure, spec)
|
|
298
|
+
return figure
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
def _y2_mode(params: dict) -> str | None:
|
|
302
|
+
return _series_mode(
|
|
303
|
+
bool(params.get("y2_show_line", True)),
|
|
304
|
+
bool(params.get("y2_show_points", True)),
|
|
305
|
+
)
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
def bar(spec: PlotSpec) -> go.Figure:
|
|
309
|
+
data = spec.data
|
|
310
|
+
params = spec.params
|
|
311
|
+
x_name, y_name, group_name = params.get("x"), params.get("y"), params.get("group")
|
|
312
|
+
figure = _figure(spec)
|
|
313
|
+
if y_name:
|
|
314
|
+
work = pd.DataFrame(
|
|
315
|
+
{
|
|
316
|
+
"__x": data[x_name].fillna("(missing)").astype(str),
|
|
317
|
+
"__y": numeric(data, y_name),
|
|
318
|
+
}
|
|
319
|
+
)
|
|
320
|
+
error_name = error_column_name(params, data)
|
|
321
|
+
if error_name:
|
|
322
|
+
work["__e"] = numeric(data, error_name)
|
|
323
|
+
if group_name and group_name in data.columns:
|
|
324
|
+
work["__g"] = data[group_name].fillna("(missing)").astype(str)
|
|
325
|
+
aggregated = work.groupby(["__x", "__g"], sort=False, dropna=False)["__y"].sum().unstack("__g")
|
|
326
|
+
error_agg = None
|
|
327
|
+
if error_name:
|
|
328
|
+
error_agg = work.groupby(["__x", "__g"], sort=False, dropna=False)["__e"].sum().unstack("__g")
|
|
329
|
+
categories = [str(value) for value in aggregated.index]
|
|
330
|
+
for position, group_label in enumerate(aggregated.columns):
|
|
331
|
+
error_y = None
|
|
332
|
+
if error_agg is not None and group_label in error_agg.columns:
|
|
333
|
+
error_y = dict(
|
|
334
|
+
type="data",
|
|
335
|
+
array=error_agg[group_label].reindex(aggregated.index).to_numpy(dtype=float),
|
|
336
|
+
)
|
|
337
|
+
figure.add_trace(
|
|
338
|
+
go.Bar(
|
|
339
|
+
x=categories,
|
|
340
|
+
y=aggregated[group_label].to_numpy(dtype=float),
|
|
341
|
+
name=str(group_label),
|
|
342
|
+
marker_color=COLORS[position % len(COLORS)],
|
|
343
|
+
error_y=error_y,
|
|
344
|
+
)
|
|
345
|
+
)
|
|
346
|
+
figure.update_layout(barmode="group")
|
|
347
|
+
else:
|
|
348
|
+
aggregated = work.groupby("__x", sort=False, dropna=False)["__y"].sum()
|
|
349
|
+
categories = [str(value) for value in aggregated.index]
|
|
350
|
+
error_y = None
|
|
351
|
+
if error_name:
|
|
352
|
+
error_values = work.groupby("__x", sort=False)["__e"].sum().reindex(aggregated.index)
|
|
353
|
+
error_y = dict(type="data", array=error_values.to_numpy(dtype=float))
|
|
354
|
+
figure.add_trace(
|
|
355
|
+
go.Bar(
|
|
356
|
+
x=categories,
|
|
357
|
+
y=aggregated.to_numpy(dtype=float),
|
|
358
|
+
name=y_name,
|
|
359
|
+
marker_color=COLORS[0],
|
|
360
|
+
error_y=error_y,
|
|
361
|
+
)
|
|
362
|
+
)
|
|
363
|
+
y2_name = params.get("y2")
|
|
364
|
+
mode = _y2_mode(params)
|
|
365
|
+
if y2_name and y2_name in data.columns and mode:
|
|
366
|
+
secondary = numeric(data, y2_name).groupby(work["__x"]).sum().reindex(aggregated.index)
|
|
367
|
+
y2_color = params.get("y2_color") or COLORS[1]
|
|
368
|
+
figure.add_trace(
|
|
369
|
+
go.Scatter(
|
|
370
|
+
x=categories,
|
|
371
|
+
y=secondary.to_numpy(dtype=float),
|
|
372
|
+
mode=mode,
|
|
373
|
+
name=y2_name,
|
|
374
|
+
yaxis="y2",
|
|
375
|
+
line=dict(color=y2_color, width=_float_param(params, "y2_line_width", 1.8)),
|
|
376
|
+
marker=dict(size=_float_param(params, "y2_marker_size", 5), color=y2_color),
|
|
377
|
+
)
|
|
378
|
+
)
|
|
379
|
+
figure.update_layout(yaxis2=dict(title=str(y2_name), overlaying="y", side="right"))
|
|
380
|
+
else:
|
|
381
|
+
counts = data[x_name].value_counts(dropna=False)
|
|
382
|
+
figure.add_trace(go.Bar(x=list(counts.index.astype(str)), y=list(counts.to_numpy()), name="Count"))
|
|
383
|
+
_style_axes(figure, spec)
|
|
384
|
+
return figure
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
def histogram(spec: PlotSpec) -> go.Figure:
|
|
388
|
+
data = spec.data
|
|
389
|
+
params = spec.params
|
|
390
|
+
column = params.get("column") or params.get("x")
|
|
391
|
+
bins = params.get("bins")
|
|
392
|
+
nbinsx = int(bins) if isinstance(bins, (int, float)) and int(bins) > 0 else None
|
|
393
|
+
figure = _figure(spec)
|
|
394
|
+
for position, (label, chunk) in enumerate(_grouped(data, params.get("group"))):
|
|
395
|
+
figure.add_trace(
|
|
396
|
+
go.Histogram(
|
|
397
|
+
x=numeric(chunk, column),
|
|
398
|
+
name=str(label) if label is not None else column,
|
|
399
|
+
nbinsx=nbinsx,
|
|
400
|
+
opacity=0.65,
|
|
401
|
+
marker_color=COLORS[position % len(COLORS)],
|
|
402
|
+
)
|
|
403
|
+
)
|
|
404
|
+
if params.get("group"):
|
|
405
|
+
figure.update_layout(barmode="overlay")
|
|
406
|
+
_style_axes(figure, spec)
|
|
407
|
+
return figure
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
def _distribution_figure(spec: PlotSpec, kind: str) -> go.Figure:
|
|
411
|
+
data = spec.data
|
|
412
|
+
params = spec.params
|
|
413
|
+
column = params.get("column") or params.get("y") or params.get("x")
|
|
414
|
+
group_name = params.get("group") or params.get("x")
|
|
415
|
+
show_points = bool(params.get("show_points", True))
|
|
416
|
+
figure = _figure(spec)
|
|
417
|
+
if group_name and group_name in data.columns:
|
|
418
|
+
for position, (label, chunk) in enumerate(data.groupby(group_name, dropna=False, sort=True)):
|
|
419
|
+
values = numeric(chunk, column).dropna()
|
|
420
|
+
custom, hover = _hover_data(spec, _ids_for(spec, data, values.index))
|
|
421
|
+
if kind == "box":
|
|
422
|
+
figure.add_trace(
|
|
423
|
+
go.Box(
|
|
424
|
+
y=values,
|
|
425
|
+
name=str(label),
|
|
426
|
+
marker_color=COLORS[position % len(COLORS)],
|
|
427
|
+
boxpoints="all" if show_points else "outliers",
|
|
428
|
+
jitter=0.35,
|
|
429
|
+
pointpos=0,
|
|
430
|
+
customdata=custom,
|
|
431
|
+
hovertemplate=hover,
|
|
432
|
+
)
|
|
433
|
+
)
|
|
434
|
+
else:
|
|
435
|
+
figure.add_trace(go.Violin(y=values, name=str(label), box_visible=True, meanline_visible=True))
|
|
436
|
+
else:
|
|
437
|
+
values = numeric(data, column).dropna()
|
|
438
|
+
custom, hover = _hover_data(spec, _ids_for(spec, data, values.index))
|
|
439
|
+
if kind == "box":
|
|
440
|
+
figure.add_trace(
|
|
441
|
+
go.Box(
|
|
442
|
+
y=values,
|
|
443
|
+
name=column,
|
|
444
|
+
marker_color=COLORS[0],
|
|
445
|
+
boxpoints="all" if show_points else "outliers",
|
|
446
|
+
jitter=0.35,
|
|
447
|
+
pointpos=0,
|
|
448
|
+
customdata=custom,
|
|
449
|
+
hovertemplate=hover,
|
|
450
|
+
)
|
|
451
|
+
)
|
|
452
|
+
else:
|
|
453
|
+
figure.add_trace(go.Violin(y=values, name=column, box_visible=True, meanline_visible=True))
|
|
454
|
+
_style_axes(figure, spec)
|
|
455
|
+
if not params.get("y_label") and not params.get("y") and column:
|
|
456
|
+
figure.update_yaxes(title_text=str(column))
|
|
457
|
+
return figure
|
|
458
|
+
|
|
459
|
+
|
|
460
|
+
def box(spec: PlotSpec) -> go.Figure:
|
|
461
|
+
return _distribution_figure(spec, "box")
|
|
462
|
+
|
|
463
|
+
|
|
464
|
+
def violin(spec: PlotSpec) -> go.Figure:
|
|
465
|
+
return _distribution_figure(spec, "violin")
|
|
466
|
+
|
|
467
|
+
|
|
468
|
+
def strip(spec: PlotSpec) -> go.Figure:
|
|
469
|
+
data = spec.data
|
|
470
|
+
params = spec.params
|
|
471
|
+
column = params.get("column") or params.get("y")
|
|
472
|
+
group_name = params.get("group")
|
|
473
|
+
figure = _figure(spec)
|
|
474
|
+
rng = np.random.default_rng(0)
|
|
475
|
+
if group_name and group_name in data.columns:
|
|
476
|
+
for position, (label, chunk) in enumerate(data.groupby(group_name, dropna=False, sort=True)):
|
|
477
|
+
values = numeric(chunk, column)
|
|
478
|
+
jitter = rng.uniform(-0.2, 0.2, len(values))
|
|
479
|
+
figure.add_trace(
|
|
480
|
+
go.Scatter(
|
|
481
|
+
x=[position + offset for offset in jitter],
|
|
482
|
+
y=values,
|
|
483
|
+
mode="markers",
|
|
484
|
+
name=str(label),
|
|
485
|
+
marker=dict(color=COLORS[position % len(COLORS)], size=7),
|
|
486
|
+
)
|
|
487
|
+
)
|
|
488
|
+
figure.update_xaxes(tickvals=list(range(len(data[group_name].unique()))))
|
|
489
|
+
else:
|
|
490
|
+
values = numeric(data, column)
|
|
491
|
+
figure.add_trace(go.Scatter(x=rng.uniform(-0.2, 0.2, len(values)), y=values, mode="markers", name=column))
|
|
492
|
+
_style_axes(figure, spec)
|
|
493
|
+
return figure
|
|
494
|
+
|
|
495
|
+
|
|
496
|
+
def bubble(spec: PlotSpec) -> go.Figure:
|
|
497
|
+
data = spec.data
|
|
498
|
+
params = spec.params
|
|
499
|
+
x_name, y_name = params.get("x"), params.get("y")
|
|
500
|
+
size_name = params.get("size")
|
|
501
|
+
color_name = params.get("color") or params.get("group")
|
|
502
|
+
try:
|
|
503
|
+
scale = float(params.get("bubble_scale", 1.0))
|
|
504
|
+
except (TypeError, ValueError):
|
|
505
|
+
scale = 1.0
|
|
506
|
+
scale = min(5.0, max(0.05, scale))
|
|
507
|
+
figure = _figure(spec)
|
|
508
|
+
if size_name and size_name in data.columns:
|
|
509
|
+
sizes = numeric(data, size_name).abs()
|
|
510
|
+
finite = sizes.dropna()
|
|
511
|
+
if len(finite) and float(finite.max()) != float(finite.min()):
|
|
512
|
+
scaled = 8 + 34 * (sizes - float(finite.min())) / (float(finite.max()) - float(finite.min()))
|
|
513
|
+
else:
|
|
514
|
+
scaled = pd.Series([16.0] * len(data), index=data.index)
|
|
515
|
+
else:
|
|
516
|
+
scaled = pd.Series([16.0] * len(data), index=data.index)
|
|
517
|
+
scaled = scaled * scale
|
|
518
|
+
|
|
519
|
+
color_values = numeric(data, color_name) if color_name and color_name in data.columns else None
|
|
520
|
+
continuous = bool(
|
|
521
|
+
color_values is not None
|
|
522
|
+
and color_values.notna().any()
|
|
523
|
+
and color_values.nunique(dropna=True) > 12
|
|
524
|
+
)
|
|
525
|
+
if continuous:
|
|
526
|
+
row_ids = _ids_for(spec, data, data.index)
|
|
527
|
+
custom, template = _hover_data(spec, row_ids)
|
|
528
|
+
figure.add_trace(
|
|
529
|
+
go.Scatter(
|
|
530
|
+
x=data[x_name],
|
|
531
|
+
y=numeric(data, y_name),
|
|
532
|
+
mode="markers",
|
|
533
|
+
name=str(color_name),
|
|
534
|
+
marker=dict(
|
|
535
|
+
size=scaled,
|
|
536
|
+
color=color_values,
|
|
537
|
+
colorscale="Viridis",
|
|
538
|
+
showscale=True,
|
|
539
|
+
colorbar=dict(title=str(color_name)),
|
|
540
|
+
opacity=0.75,
|
|
541
|
+
line=dict(width=0.5, color="white"),
|
|
542
|
+
),
|
|
543
|
+
customdata=custom,
|
|
544
|
+
hovertemplate=template,
|
|
545
|
+
)
|
|
546
|
+
)
|
|
547
|
+
else:
|
|
548
|
+
for position, (label, chunk) in enumerate(_grouped(data, color_name)):
|
|
549
|
+
row_ids = _ids_for(spec, data, chunk.index)
|
|
550
|
+
custom, template = _hover_data(spec, row_ids)
|
|
551
|
+
figure.add_trace(
|
|
552
|
+
go.Scatter(
|
|
553
|
+
x=chunk[x_name],
|
|
554
|
+
y=numeric(chunk, y_name),
|
|
555
|
+
mode="markers",
|
|
556
|
+
name=str(label) if label is not None else y_name,
|
|
557
|
+
marker=dict(
|
|
558
|
+
size=scaled.loc[chunk.index],
|
|
559
|
+
color=COLORS[position % len(COLORS)],
|
|
560
|
+
opacity=0.75,
|
|
561
|
+
line=dict(width=0.5, color="white"),
|
|
562
|
+
),
|
|
563
|
+
customdata=custom,
|
|
564
|
+
hovertemplate=template,
|
|
565
|
+
)
|
|
566
|
+
)
|
|
567
|
+
_style_axes(figure, spec)
|
|
568
|
+
return figure
|
|
569
|
+
|
|
570
|
+
|
|
571
|
+
def area(spec: PlotSpec) -> go.Figure:
|
|
572
|
+
data = spec.data
|
|
573
|
+
params = spec.params
|
|
574
|
+
x_name, y_name = params.get("x"), params.get("y")
|
|
575
|
+
figure = _figure(spec)
|
|
576
|
+
ordered = data.sort_values(x_name)
|
|
577
|
+
ids = _ids_for(spec, data, ordered.index)
|
|
578
|
+
custom, template = _hover_data(spec, ids)
|
|
579
|
+
figure.add_trace(
|
|
580
|
+
go.Scatter(
|
|
581
|
+
x=ordered[x_name],
|
|
582
|
+
y=numeric(ordered, y_name),
|
|
583
|
+
mode="lines",
|
|
584
|
+
fill="tozeroy",
|
|
585
|
+
name=y_name,
|
|
586
|
+
line=dict(color=COLORS[0]),
|
|
587
|
+
customdata=custom,
|
|
588
|
+
hovertemplate=template,
|
|
589
|
+
)
|
|
590
|
+
)
|
|
591
|
+
_style_axes(figure, spec)
|
|
592
|
+
return figure
|
|
593
|
+
|
|
594
|
+
|
|
595
|
+
def heatmap(spec: PlotSpec) -> go.Figure:
|
|
596
|
+
data = spec.data
|
|
597
|
+
params = spec.params
|
|
598
|
+
x_name, y_name, z_name = params.get("x"), params.get("y"), params.get("z")
|
|
599
|
+
figure = _figure(spec)
|
|
600
|
+
pivot = data.pivot_table(index=y_name, columns=x_name, values=z_name, aggfunc="mean")
|
|
601
|
+
figure.add_trace(
|
|
602
|
+
go.Heatmap(
|
|
603
|
+
x=[str(value) for value in pivot.columns],
|
|
604
|
+
y=[str(value) for value in pivot.index],
|
|
605
|
+
z=pivot.to_numpy(),
|
|
606
|
+
colorscale="Viridis",
|
|
607
|
+
)
|
|
608
|
+
)
|
|
609
|
+
_style_axes(figure, spec)
|
|
610
|
+
return figure
|
|
611
|
+
|
|
612
|
+
|
|
613
|
+
def correlation_heatmap(spec: PlotSpec) -> go.Figure:
|
|
614
|
+
data = spec.data
|
|
615
|
+
params = spec.params
|
|
616
|
+
columns = [column for column in (params.get("columns") or []) if column in data.columns]
|
|
617
|
+
method = params.get("method") or "pearson"
|
|
618
|
+
figure = _figure(spec)
|
|
619
|
+
correlations = data[columns].apply(pd.to_numeric, errors="coerce").corr(method=method)
|
|
620
|
+
figure.add_trace(
|
|
621
|
+
go.Heatmap(
|
|
622
|
+
x=list(correlations.columns),
|
|
623
|
+
y=list(correlations.index),
|
|
624
|
+
z=correlations.to_numpy(),
|
|
625
|
+
zmin=-1,
|
|
626
|
+
zmax=1,
|
|
627
|
+
colorscale="RdBu",
|
|
628
|
+
reversescale=True,
|
|
629
|
+
text=np.round(correlations.to_numpy(), 2),
|
|
630
|
+
texttemplate="%{text}",
|
|
631
|
+
)
|
|
632
|
+
)
|
|
633
|
+
_style_axes(figure, spec)
|
|
634
|
+
return figure
|
|
635
|
+
|
|
636
|
+
|
|
637
|
+
def scatter3d(spec: PlotSpec) -> go.Figure:
|
|
638
|
+
data = spec.data
|
|
639
|
+
params = spec.params
|
|
640
|
+
x_name, y_name, z_name = params.get("x"), params.get("y"), params.get("z")
|
|
641
|
+
size_name = params.get("size")
|
|
642
|
+
figure = _figure(spec)
|
|
643
|
+
if size_name and size_name in data.columns:
|
|
644
|
+
sizes = numeric(data, size_name).abs()
|
|
645
|
+
finite = sizes.dropna()
|
|
646
|
+
if len(finite) and float(finite.max()) != float(finite.min()):
|
|
647
|
+
scaled = 4 + 14 * (sizes - float(finite.min())) / (float(finite.max()) - float(finite.min()))
|
|
648
|
+
else:
|
|
649
|
+
scaled = pd.Series([8.0] * len(data), index=data.index)
|
|
650
|
+
else:
|
|
651
|
+
scaled = pd.Series([8.0] * len(data), index=data.index)
|
|
652
|
+
for position, (label, chunk) in enumerate(_grouped(data, params.get("group"))):
|
|
653
|
+
row_ids = _ids_for(spec, data, chunk.index)
|
|
654
|
+
custom, template = _hover_data(spec, row_ids)
|
|
655
|
+
figure.add_trace(
|
|
656
|
+
go.Scatter3d(
|
|
657
|
+
x=chunk[x_name],
|
|
658
|
+
y=numeric(chunk, y_name),
|
|
659
|
+
z=numeric(chunk, z_name),
|
|
660
|
+
mode="markers",
|
|
661
|
+
name=str(label) if label is not None else z_name,
|
|
662
|
+
marker=dict(size=scaled.loc[chunk.index], color=COLORS[position % len(COLORS)], opacity=0.8),
|
|
663
|
+
customdata=custom,
|
|
664
|
+
hovertemplate=template,
|
|
665
|
+
)
|
|
666
|
+
)
|
|
667
|
+
_style_scene_axes(figure, spec)
|
|
668
|
+
return figure
|
|
669
|
+
|
|
670
|
+
|
|
671
|
+
def _add_secondary(figure: go.Figure, spec: PlotSpec) -> None:
|
|
672
|
+
params = spec.params or {}
|
|
673
|
+
y2_name = params.get("y2")
|
|
674
|
+
data = spec.data
|
|
675
|
+
mode = _y2_mode(params)
|
|
676
|
+
if not y2_name or data is None or y2_name not in data.columns or not mode:
|
|
677
|
+
return
|
|
678
|
+
x_name = params.get("x")
|
|
679
|
+
ordered = data.sort_values(x_name)
|
|
680
|
+
ids = _ids_for(spec, data, ordered.index)
|
|
681
|
+
custom, template = _hover_data(spec, ids)
|
|
682
|
+
y2_color = params.get("y2_color") or COLORS[1]
|
|
683
|
+
figure.add_trace(
|
|
684
|
+
go.Scatter(
|
|
685
|
+
x=ordered[x_name],
|
|
686
|
+
y=numeric(ordered, y2_name),
|
|
687
|
+
mode=mode,
|
|
688
|
+
name=y2_name,
|
|
689
|
+
yaxis="y2",
|
|
690
|
+
line=dict(color=y2_color, width=_float_param(params, "y2_line_width", 1.8)),
|
|
691
|
+
marker=dict(size=_float_param(params, "y2_marker_size", 5), color=y2_color),
|
|
692
|
+
customdata=custom,
|
|
693
|
+
hovertemplate=template,
|
|
694
|
+
)
|
|
695
|
+
)
|
|
696
|
+
figure.update_layout(yaxis2=dict(title=str(y2_name), overlaying="y", side="right"))
|
|
697
|
+
|
|
698
|
+
|
|
699
|
+
PLOTLY_BUILDERS: dict[str, Callable[[PlotSpec], go.Figure]] = {
|
|
700
|
+
"scatter": scatter,
|
|
701
|
+
"line": line,
|
|
702
|
+
"bar": bar,
|
|
703
|
+
"histogram": histogram,
|
|
704
|
+
"box": box,
|
|
705
|
+
"violin": violin,
|
|
706
|
+
"strip": strip,
|
|
707
|
+
"bubble": bubble,
|
|
708
|
+
"area": area,
|
|
709
|
+
"heatmap": heatmap,
|
|
710
|
+
"correlation_heatmap": correlation_heatmap,
|
|
711
|
+
"scatter3d": scatter3d,
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
|
|
715
|
+
def build_plotly_figure(spec: PlotSpec) -> go.Figure | None:
|
|
716
|
+
builder = PLOTLY_BUILDERS.get(spec.kind)
|
|
717
|
+
if builder is None or spec.data is None:
|
|
718
|
+
return None
|
|
719
|
+
return builder(spec)
|