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,321 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import contextlib
|
|
4
|
+
import io
|
|
5
|
+
import traceback
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
import numpy as np
|
|
9
|
+
import pandas as pd
|
|
10
|
+
|
|
11
|
+
from statstudio import stats
|
|
12
|
+
from statstudio.core import transform
|
|
13
|
+
from statstudio.core.joins import append_tables, merge_tables
|
|
14
|
+
from statstudio.core.project import JournalEntry, Project, ReportRecord
|
|
15
|
+
from statstudio.core.table import DataTable, add_rows, restore_table
|
|
16
|
+
from statstudio.core import scripting
|
|
17
|
+
from statstudio.io.file_import import CSVOptions, parse_clipboard_frame, read_file
|
|
18
|
+
from statstudio.io.sample_data import load_sample
|
|
19
|
+
from statstudio.io.odbc import OdbcProfile, import_query
|
|
20
|
+
from statstudio.io.table_io import load_table, save_table
|
|
21
|
+
from statstudio.stats.results import AnalysisResult, PlotSpec
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class HeadlessSDK:
|
|
25
|
+
"""Qt-free scripting facade used by the CLI and headless runner."""
|
|
26
|
+
|
|
27
|
+
def __init__(self, project: Project, history: list[str] | None = None) -> None:
|
|
28
|
+
self.project = project
|
|
29
|
+
self.history = history if history is not None else []
|
|
30
|
+
|
|
31
|
+
def datasets(self) -> list[str]:
|
|
32
|
+
return [table.name for table in self.project.tables]
|
|
33
|
+
|
|
34
|
+
def dataset(self, name: str) -> DataTable:
|
|
35
|
+
return self.project.get_table(name)
|
|
36
|
+
|
|
37
|
+
def analyses(self) -> list[str]:
|
|
38
|
+
return [report.title for report in self.project.reports]
|
|
39
|
+
|
|
40
|
+
def reports(self) -> list[str]:
|
|
41
|
+
return self.analyses()
|
|
42
|
+
|
|
43
|
+
def journal(self) -> list[str]:
|
|
44
|
+
return [entry.title for entry in self.project.journal]
|
|
45
|
+
|
|
46
|
+
def run(self, code: str) -> str:
|
|
47
|
+
namespace = build_namespace(self.project, history=self.history, sdk=self)
|
|
48
|
+
output, ok = execute_code(code, namespace, capture_errors=True)
|
|
49
|
+
if ok:
|
|
50
|
+
sync_tables(self.project, namespace)
|
|
51
|
+
return output
|
|
52
|
+
|
|
53
|
+
def new_dataset(self, name: str = "Dataset") -> DataTable:
|
|
54
|
+
table = DataTable(pd.DataFrame(), name)
|
|
55
|
+
return self.project.add_table(table)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def build_namespace(
|
|
59
|
+
project: Project,
|
|
60
|
+
*,
|
|
61
|
+
table: DataTable | None = None,
|
|
62
|
+
history: list[str] | None = None,
|
|
63
|
+
sdk: HeadlessSDK | None = None,
|
|
64
|
+
) -> dict[str, object]:
|
|
65
|
+
sdk = sdk or HeadlessSDK(project, history=history)
|
|
66
|
+
namespace: dict[str, object] = {
|
|
67
|
+
"project": project,
|
|
68
|
+
"tables": lambda: project.tables,
|
|
69
|
+
"stats": stats,
|
|
70
|
+
"plots": __import__("statstudio.plots", fromlist=["build_figure", "save_figure"]),
|
|
71
|
+
"pd": pd,
|
|
72
|
+
"np": np,
|
|
73
|
+
"sdk": sdk,
|
|
74
|
+
"transform": transform,
|
|
75
|
+
"merge_tables": merge_tables,
|
|
76
|
+
"append_tables": append_tables,
|
|
77
|
+
"read_file": read_file,
|
|
78
|
+
"parse_clipboard_frame": parse_clipboard_frame,
|
|
79
|
+
"OdbcProfile": OdbcProfile,
|
|
80
|
+
"import_query": import_query,
|
|
81
|
+
"CSVOptions": CSVOptions,
|
|
82
|
+
"load_sample": load_sample,
|
|
83
|
+
"load_table": load_table,
|
|
84
|
+
"save_table": save_table,
|
|
85
|
+
"DataTable": DataTable,
|
|
86
|
+
"PlotSpec": PlotSpec,
|
|
87
|
+
"add_rows": add_rows,
|
|
88
|
+
"_tables": {item.name: item.copy() for item in project.tables},
|
|
89
|
+
}
|
|
90
|
+
if table is not None:
|
|
91
|
+
namespace["table"] = table
|
|
92
|
+
namespace["_tables"][table.name] = table
|
|
93
|
+
return namespace
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def execute_code(
|
|
97
|
+
code: str,
|
|
98
|
+
namespace: dict[str, object],
|
|
99
|
+
*,
|
|
100
|
+
filename: str = "<script>",
|
|
101
|
+
capture_errors: bool = False,
|
|
102
|
+
) -> tuple[str, bool]:
|
|
103
|
+
code = code.strip()
|
|
104
|
+
if not code:
|
|
105
|
+
return "", True
|
|
106
|
+
buffer = io.StringIO()
|
|
107
|
+
try:
|
|
108
|
+
with contextlib.redirect_stdout(buffer), contextlib.redirect_stderr(buffer):
|
|
109
|
+
try:
|
|
110
|
+
result = eval(compile(code, filename, "eval"), namespace)
|
|
111
|
+
except SyntaxError:
|
|
112
|
+
exec(compile(code, filename, "exec"), namespace)
|
|
113
|
+
result = None
|
|
114
|
+
if result is not None:
|
|
115
|
+
print(repr(result))
|
|
116
|
+
except Exception:
|
|
117
|
+
if not capture_errors:
|
|
118
|
+
raise
|
|
119
|
+
buffer.write(traceback.format_exc())
|
|
120
|
+
return buffer.getvalue().rstrip(), False
|
|
121
|
+
return buffer.getvalue().rstrip(), True
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def sync_tables(project: Project, namespace: dict[str, object], original_table: DataTable | None = None) -> None:
|
|
125
|
+
"""Reconcile script-mutated tables with the live project."""
|
|
126
|
+
tables = namespace.get("_tables")
|
|
127
|
+
if isinstance(tables, dict):
|
|
128
|
+
desired: set[str] = set()
|
|
129
|
+
for name, candidate in tables.items():
|
|
130
|
+
if not isinstance(candidate, DataTable):
|
|
131
|
+
continue
|
|
132
|
+
desired.add(str(name))
|
|
133
|
+
existing = project.find_table(name)
|
|
134
|
+
if existing is None:
|
|
135
|
+
candidate.name = str(name)
|
|
136
|
+
project.add_table(candidate)
|
|
137
|
+
elif existing is not candidate:
|
|
138
|
+
restore_table(existing, candidate)
|
|
139
|
+
for existing in list(project.tables):
|
|
140
|
+
if existing.name not in desired:
|
|
141
|
+
project.remove_table(existing)
|
|
142
|
+
if original_table is not None:
|
|
143
|
+
outcome = namespace.get("table")
|
|
144
|
+
if isinstance(outcome, DataTable) and outcome is not original_table:
|
|
145
|
+
restore_table(original_table, outcome)
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def run_script_file(
|
|
149
|
+
project: Project,
|
|
150
|
+
path: str | Path,
|
|
151
|
+
*,
|
|
152
|
+
table: DataTable | None = None,
|
|
153
|
+
) -> tuple[str, bool]:
|
|
154
|
+
path = Path(path)
|
|
155
|
+
namespace = build_namespace(project, table=table)
|
|
156
|
+
namespace["__file__"] = str(path)
|
|
157
|
+
output, ok = execute_code(path.read_text(encoding="utf-8"), namespace, filename=str(path), capture_errors=True)
|
|
158
|
+
if ok:
|
|
159
|
+
sync_tables(project, namespace, table)
|
|
160
|
+
return output, ok
|
|
161
|
+
|
|
162
|
+
class TableHandle:
|
|
163
|
+
"""Small automation handle for one project table."""
|
|
164
|
+
|
|
165
|
+
def __init__(self, session: "AutomationSession", name: str) -> None:
|
|
166
|
+
self.session = session
|
|
167
|
+
self.name = name
|
|
168
|
+
|
|
169
|
+
@property
|
|
170
|
+
def table(self) -> DataTable:
|
|
171
|
+
return self.session.project.get_table(self.name)
|
|
172
|
+
|
|
173
|
+
@property
|
|
174
|
+
def data(self):
|
|
175
|
+
return self.table.data
|
|
176
|
+
|
|
177
|
+
def filter(self, expression: str) -> "TableHandle":
|
|
178
|
+
return self.session.apply("filter", {"expression": expression}, [self.name])
|
|
179
|
+
|
|
180
|
+
def clear_filter(self) -> "TableHandle":
|
|
181
|
+
return self.session.apply("clear_filter", {}, [self.name])
|
|
182
|
+
|
|
183
|
+
def add_formula(self, name: str, formula: str) -> "TableHandle":
|
|
184
|
+
return self.session.apply("formula", {"name": name, "formula": formula}, [self.name])
|
|
185
|
+
|
|
186
|
+
def add_column(self, name: str, data_type: str = "numeric", default=0) -> "TableHandle":
|
|
187
|
+
return self.session.apply("add_column", {"name": name, "data_type": data_type, "default": default}, [self.name])
|
|
188
|
+
|
|
189
|
+
def sort(self, spec: list[tuple[str, bool]]) -> "TableHandle":
|
|
190
|
+
return self.session.apply("sort", {"spec": spec}, [self.name])
|
|
191
|
+
|
|
192
|
+
def subset_rows(self, rows: list, name: str | None = None) -> "TableHandle":
|
|
193
|
+
target = name or f"{self.name} Subset"
|
|
194
|
+
return self.session.apply("subset_rows", {"rows": list(rows)}, [self.name], target)
|
|
195
|
+
|
|
196
|
+
def rename_column(self, old: str, new: str) -> "TableHandle":
|
|
197
|
+
return self.session.apply("rename_column", {"old": old, "new": new}, [self.name])
|
|
198
|
+
|
|
199
|
+
def save(self, path) -> Path:
|
|
200
|
+
return self.session.table_io_save(self.name, path)
|
|
201
|
+
|
|
202
|
+
def analyze(self, analysis_type: str, params: dict, title: str = "") -> AnalysisResult:
|
|
203
|
+
return self.session.analyze(self.name, analysis_type, params, title)
|
|
204
|
+
|
|
205
|
+
def plot(self, kind: str, params: dict, title: str = ""):
|
|
206
|
+
return self.session.plot(self.name, kind, params, title)
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
class AutomationSession:
|
|
210
|
+
"""Qt-free automation and macro-recording surface."""
|
|
211
|
+
|
|
212
|
+
def __init__(self, project: Project | None = None) -> None:
|
|
213
|
+
self.project = project or Project()
|
|
214
|
+
self.recording = True
|
|
215
|
+
self._history: list[str] = []
|
|
216
|
+
|
|
217
|
+
@classmethod
|
|
218
|
+
def open(cls, path) -> "AutomationSession":
|
|
219
|
+
return cls(Project.load(path))
|
|
220
|
+
|
|
221
|
+
def save(self, path=None) -> Path:
|
|
222
|
+
return self.project.save(path or self.project.path)
|
|
223
|
+
|
|
224
|
+
def table(self, name: str) -> TableHandle:
|
|
225
|
+
self.project.get_table(name)
|
|
226
|
+
return TableHandle(self, name)
|
|
227
|
+
|
|
228
|
+
def table_io_save(self, name: str, path) -> Path:
|
|
229
|
+
from statstudio.io.table_io import save_table
|
|
230
|
+
|
|
231
|
+
return save_table(self.project.get_table(name), path)
|
|
232
|
+
|
|
233
|
+
def new_table(self, name: str = "Dataset") -> TableHandle:
|
|
234
|
+
table = self.project.add_table(DataTable(pd.DataFrame(), name))
|
|
235
|
+
return TableHandle(self, table.name)
|
|
236
|
+
|
|
237
|
+
def apply(
|
|
238
|
+
self,
|
|
239
|
+
operation: str,
|
|
240
|
+
params: dict,
|
|
241
|
+
sources: list[str] | None = None,
|
|
242
|
+
result_table: str = "",
|
|
243
|
+
title: str = "",
|
|
244
|
+
) -> TableHandle:
|
|
245
|
+
sources = list(sources or [])
|
|
246
|
+
target_name = result_table or (sources[0] if sources else "")
|
|
247
|
+
if not target_name:
|
|
248
|
+
raise ValueError("An operation target table is required.")
|
|
249
|
+
if operation in scripting.CREATION_OPERATIONS and self.project.find_table(target_name) is not None:
|
|
250
|
+
raise ValueError(f"A table named {target_name!r} already exists.")
|
|
251
|
+
namespace = build_namespace(self.project)
|
|
252
|
+
code = scripting.operation_code(operation, params, sources, target_name)
|
|
253
|
+
output, ok = execute_code(code, namespace, filename=f"<automation:{operation}>", capture_errors=True)
|
|
254
|
+
if not ok:
|
|
255
|
+
raise RuntimeError(output or f"Operation failed: {operation}")
|
|
256
|
+
sync_tables(self.project, namespace)
|
|
257
|
+
if self.recording:
|
|
258
|
+
self.project.add_journal(
|
|
259
|
+
JournalEntry(
|
|
260
|
+
kind="data_prep",
|
|
261
|
+
operation=operation,
|
|
262
|
+
title=title or f"{operation} {target_name}",
|
|
263
|
+
source_tables=sources,
|
|
264
|
+
result_table=target_name,
|
|
265
|
+
params=dict(params or {}),
|
|
266
|
+
code=code,
|
|
267
|
+
)
|
|
268
|
+
)
|
|
269
|
+
return TableHandle(self, target_name)
|
|
270
|
+
|
|
271
|
+
def analyze(self, table_name: str, analysis_type: str, params: dict, title: str = "") -> AnalysisResult:
|
|
272
|
+
result = AnalysisResult(title=title or analysis_type, analysis_type=analysis_type, source_table=table_name, params=dict(params or {}))
|
|
273
|
+
code = scripting.analysis_code(result, f"_tables[{scripting.literal(table_name)}]")
|
|
274
|
+
namespace = build_namespace(self.project)
|
|
275
|
+
output, ok = execute_code(code, namespace, filename=f"<automation:{analysis_type}>", capture_errors=True)
|
|
276
|
+
if not ok or not isinstance(namespace.get("result"), AnalysisResult):
|
|
277
|
+
raise RuntimeError(output or f"Analysis failed: {analysis_type}")
|
|
278
|
+
analysis = namespace["result"]
|
|
279
|
+
if self.recording:
|
|
280
|
+
self.project.add_report(
|
|
281
|
+
ReportRecord(
|
|
282
|
+
title=analysis.title,
|
|
283
|
+
html=analysis.to_html(include_plots=True),
|
|
284
|
+
kind=analysis.analysis_type,
|
|
285
|
+
source_table=table_name,
|
|
286
|
+
params=dict(analysis.params),
|
|
287
|
+
code=scripting.analysis_code(analysis, "table"),
|
|
288
|
+
)
|
|
289
|
+
)
|
|
290
|
+
self.project.add_journal(
|
|
291
|
+
JournalEntry(
|
|
292
|
+
kind="analysis",
|
|
293
|
+
operation=analysis.analysis_type,
|
|
294
|
+
title=analysis.title,
|
|
295
|
+
source_tables=[table_name],
|
|
296
|
+
params=dict(analysis.params),
|
|
297
|
+
code=code,
|
|
298
|
+
)
|
|
299
|
+
)
|
|
300
|
+
return analysis
|
|
301
|
+
|
|
302
|
+
def plot(self, table_name: str, kind: str, params: dict, title: str = ""):
|
|
303
|
+
table = self.project.get_table(table_name)
|
|
304
|
+
frame = table.plot_df()
|
|
305
|
+
spec = PlotSpec(kind, dict(params or {}), title=title, data=frame, row_ids=list(frame.index))
|
|
306
|
+
from statstudio.plots.registry import build_figure
|
|
307
|
+
|
|
308
|
+
return build_figure(spec)
|
|
309
|
+
|
|
310
|
+
def replay_journal(self, *, trust: bool = False) -> None:
|
|
311
|
+
if not trust:
|
|
312
|
+
raise PermissionError(
|
|
313
|
+
"Journal replay can execute arbitrary Python. Pass trust=True only for projects you trust."
|
|
314
|
+
)
|
|
315
|
+
code = scripting.cumulative_script(self.project, str(self.project.path or ""))
|
|
316
|
+
namespace = build_namespace(self.project)
|
|
317
|
+
output, ok = execute_code(code, namespace, filename="<journal-replay>", capture_errors=True)
|
|
318
|
+
if not ok:
|
|
319
|
+
raise RuntimeError(output or "Journal replay failed.")
|
|
320
|
+
sync_tables(self.project, namespace)
|
|
321
|
+
|