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.
Files changed (110) hide show
  1. statstudio/__init__.py +9 -0
  2. statstudio/__main__.py +3 -0
  3. statstudio/app.py +83 -0
  4. statstudio/cli.py +116 -0
  5. statstudio/core/__init__.py +3 -0
  6. statstudio/core/archive.py +27 -0
  7. statstudio/core/commands.py +67 -0
  8. statstudio/core/formula_catalog.py +74 -0
  9. statstudio/core/formulas.py +605 -0
  10. statstudio/core/joins.py +123 -0
  11. statstudio/core/project.py +358 -0
  12. statstudio/core/runtime.py +321 -0
  13. statstudio/core/scripting.py +594 -0
  14. statstudio/core/table.py +796 -0
  15. statstudio/core/transform.py +450 -0
  16. statstudio/feature_help.py +595 -0
  17. statstudio/io/__init__.py +3 -0
  18. statstudio/io/docx.py +201 -0
  19. statstudio/io/export.py +99 -0
  20. statstudio/io/file_import.py +199 -0
  21. statstudio/io/odbc.py +334 -0
  22. statstudio/io/sample_data.py +164 -0
  23. statstudio/io/table_io.py +118 -0
  24. statstudio/plots/__init__.py +19 -0
  25. statstudio/plots/advanced.py +651 -0
  26. statstudio/plots/basic.py +957 -0
  27. statstudio/plots/bayesian_plots.py +94 -0
  28. statstudio/plots/common.py +334 -0
  29. statstudio/plots/diagnostics.py +214 -0
  30. statstudio/plots/doe_plots.py +235 -0
  31. statstudio/plots/plotly_builders.py +719 -0
  32. statstudio/plots/predictive_plots.py +72 -0
  33. statstudio/plots/registry.py +251 -0
  34. statstudio/plugins.py +111 -0
  35. statstudio/resources/help_topics.json +13195 -0
  36. statstudio/resources/logo.ico +0 -0
  37. statstudio/resources/logo.png +0 -0
  38. statstudio/resources/statstudio.svg +8 -0
  39. statstudio/stats/__init__.py +42 -0
  40. statstudio/stats/bayesian.py +1094 -0
  41. statstudio/stats/bayesian_optimization.py +590 -0
  42. statstudio/stats/common.py +140 -0
  43. statstudio/stats/correlation.py +135 -0
  44. statstudio/stats/custom_doe.py +1714 -0
  45. statstudio/stats/descriptive.py +200 -0
  46. statstudio/stats/desirability.py +132 -0
  47. statstudio/stats/distributions.py +331 -0
  48. statstudio/stats/doe.py +1070 -0
  49. statstudio/stats/hypothesis.py +1305 -0
  50. statstudio/stats/multivariate.py +307 -0
  51. statstudio/stats/optimal_doe.py +264 -0
  52. statstudio/stats/power.py +194 -0
  53. statstudio/stats/predictive.py +605 -0
  54. statstudio/stats/profiler.py +379 -0
  55. statstudio/stats/quality.py +1218 -0
  56. statstudio/stats/regression.py +1600 -0
  57. statstudio/stats/results.py +235 -0
  58. statstudio/stats/simulator.py +95 -0
  59. statstudio/stats/survival.py +637 -0
  60. statstudio/stats/timeseries.py +643 -0
  61. statstudio/stats/tolerance.py +154 -0
  62. statstudio/ui/__init__.py +0 -0
  63. statstudio/ui/analysis_hub.py +271 -0
  64. statstudio/ui/command_catalog.py +460 -0
  65. statstudio/ui/command_palette.py +55 -0
  66. statstudio/ui/console.py +239 -0
  67. statstudio/ui/dialogs/__init__.py +0 -0
  68. statstudio/ui/dialogs/advanced_dialogs.py +392 -0
  69. statstudio/ui/dialogs/base.py +537 -0
  70. statstudio/ui/dialogs/bayesian_dialogs.py +488 -0
  71. statstudio/ui/dialogs/bayesian_optimization_dialog.py +299 -0
  72. statstudio/ui/dialogs/custom_doe_dialog.py +467 -0
  73. statstudio/ui/dialogs/doe_dialogs.py +220 -0
  74. statstudio/ui/dialogs/extended_dialogs.py +665 -0
  75. statstudio/ui/dialogs/graph_builder.py +139 -0
  76. statstudio/ui/dialogs/help_dialog.py +207 -0
  77. statstudio/ui/dialogs/help_topics.py +280 -0
  78. statstudio/ui/dialogs/lazy.py +184 -0
  79. statstudio/ui/dialogs/odbc_dialog.py +311 -0
  80. statstudio/ui/dialogs/plot_dialog.py +1385 -0
  81. statstudio/ui/dialogs/report_composer.py +218 -0
  82. statstudio/ui/dialogs/script_dialogs.py +267 -0
  83. statstudio/ui/dialogs/stats_dialogs.py +1688 -0
  84. statstudio/ui/dialogs/table_dialogs.py +997 -0
  85. statstudio/ui/dialogs/window.py +58 -0
  86. statstudio/ui/docks/__init__.py +0 -0
  87. statstudio/ui/formula_editor.py +121 -0
  88. statstudio/ui/icons.py +257 -0
  89. statstudio/ui/jobs.py +179 -0
  90. statstudio/ui/main_window.py +3934 -0
  91. statstudio/ui/plot_interaction.py +178 -0
  92. statstudio/ui/profiler_widget.py +437 -0
  93. statstudio/ui/recent.py +82 -0
  94. statstudio/ui/reports/__init__.py +0 -0
  95. statstudio/ui/reports/report_view.py +1632 -0
  96. statstudio/ui/script_jobs.py +35 -0
  97. statstudio/ui/scripts_panel.py +271 -0
  98. statstudio/ui/table_document.py +233 -0
  99. statstudio/ui/table_model.py +285 -0
  100. statstudio/ui/table_view.py +285 -0
  101. statstudio/ui/theme.py +645 -0
  102. statstudio/ui/welcome.py +453 -0
  103. statstudio/ui/window_controls.py +53 -0
  104. statstudio/ui/workers.py +53 -0
  105. statstudio-0.1.0.dist-info/METADATA +261 -0
  106. statstudio-0.1.0.dist-info/RECORD +110 -0
  107. statstudio-0.1.0.dist-info/WHEEL +5 -0
  108. statstudio-0.1.0.dist-info/entry_points.txt +5 -0
  109. statstudio-0.1.0.dist-info/licenses/LICENSE +674 -0
  110. statstudio-0.1.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,379 @@
1
+ from __future__ import annotations
2
+
3
+ import math
4
+ from dataclasses import dataclass, field
5
+ from itertools import product
6
+ from typing import Any, Callable
7
+
8
+ import numpy as np
9
+ import pandas as pd
10
+ from scipy.optimize import differential_evolution
11
+
12
+
13
+ def _optional_float(value: Any) -> float | None:
14
+ if value is None:
15
+ return None
16
+ try:
17
+ number = float(value)
18
+ except (TypeError, ValueError):
19
+ return None
20
+ return number if math.isfinite(number) else None
21
+
22
+
23
+ def _normalize_goal(value: Any) -> str:
24
+ goal = str(value or "maximize").strip().lower()
25
+ if goal in {"max", "maximum", "maximize"}:
26
+ return "maximize"
27
+ if goal in {"min", "minimum", "minimize"}:
28
+ return "minimize"
29
+ if goal in {"target", "nominal", "match target"}:
30
+ return "target"
31
+ if goal in {"within", "range", "within range"}:
32
+ return "within"
33
+ return "maximize"
34
+
35
+
36
+ @dataclass
37
+ class ProfilerGoal:
38
+ goal: str = "maximize"
39
+ low: float | None = None
40
+ high: float | None = None
41
+ target: float | None = None
42
+
43
+ def __post_init__(self) -> None:
44
+ self.goal = _normalize_goal(self.goal)
45
+ self.low = _optional_float(self.low)
46
+ self.high = _optional_float(self.high)
47
+ self.target = _optional_float(self.target)
48
+
49
+
50
+ @dataclass
51
+ class ProfilerOptimization:
52
+ prediction: float
53
+ settings: dict[str, Any]
54
+
55
+
56
+ @dataclass
57
+ class ProfilerFactor:
58
+ name: str
59
+ kind: str = "continuous"
60
+ low: float = 0.0
61
+ high: float = 1.0
62
+ levels: list[str] = field(default_factory=list)
63
+ current: Any = None
64
+ initial: Any = None
65
+ locked: bool = False
66
+ units: str = ""
67
+
68
+ def __post_init__(self) -> None:
69
+ self.name = str(self.name)
70
+ self.kind = "categorical" if str(self.kind).lower().startswith("cat") else "continuous"
71
+ self.units = str(self.units or "")
72
+ if self.kind == "continuous":
73
+ self.low = float(self.low)
74
+ self.high = float(self.high)
75
+ if self.high < self.low:
76
+ self.low, self.high = self.high, self.low
77
+ if self.current is None:
78
+ self.current = self.low + (self.high - self.low) / 2.0
79
+ if self.initial is None:
80
+ self.initial = self.current
81
+ self.current = self.clamp(self.current)
82
+ self.initial = self.clamp(self.initial)
83
+ else:
84
+ self.levels = [str(level) for level in self.levels]
85
+ if self.initial is None:
86
+ self.initial = self.levels[0] if self.levels else ""
87
+ self.initial = str(self.initial)
88
+ if self.levels and self.initial not in self.levels:
89
+ self.initial = self.levels[0]
90
+ if self.current is None:
91
+ self.current = self.initial
92
+ self.current = str(self.current)
93
+ if self.levels and self.current not in self.levels:
94
+ self.current = self.initial
95
+
96
+ def clamp(self, value: Any) -> float:
97
+ try:
98
+ number = float(value)
99
+ except (TypeError, ValueError):
100
+ number = self.low
101
+ return float(min(self.high, max(self.low, number)))
102
+
103
+ def coerce(self, value: Any) -> Any:
104
+ if self.kind == "continuous":
105
+ return self.clamp(value)
106
+ text = str(value)
107
+ if self.levels and text not in self.levels:
108
+ return self.initial
109
+ return text
110
+
111
+ def level_index(self) -> int:
112
+ try:
113
+ return self.levels.index(str(self.current))
114
+ except ValueError:
115
+ return 0
116
+
117
+ def display_value(self) -> str:
118
+ if self.kind == "continuous":
119
+ return f"{float(self.current):.6g}"
120
+ return str(self.current)
121
+
122
+
123
+ @dataclass
124
+ class ProfilerSession:
125
+ response: str
126
+ factors: list[ProfilerFactor]
127
+ predict: Callable[[dict[str, Any]], float]
128
+ response_label: str = ""
129
+ points: int = 9
130
+ response_goal: ProfilerGoal | None = None
131
+
132
+ def __post_init__(self) -> None:
133
+ self.response = str(self.response)
134
+ self.response_label = str(self.response_label or self.response)
135
+ self.points = max(3, int(self.points))
136
+
137
+ def factor(self, name: str) -> ProfilerFactor:
138
+ for factor in self.factors:
139
+ if factor.name == name:
140
+ return factor
141
+ raise KeyError(name)
142
+
143
+ def settings(self) -> dict[str, Any]:
144
+ return {factor.name: factor.current for factor in self.factors}
145
+
146
+ def set_value(self, name: str, value: Any) -> bool:
147
+ factor = self.factor(name)
148
+ if factor.locked:
149
+ return False
150
+ coerced = factor.coerce(value)
151
+ if coerced == factor.current:
152
+ return False
153
+ factor.current = coerced
154
+ return True
155
+
156
+ def reset(self) -> None:
157
+ for factor in self.factors:
158
+ factor.current = factor.initial
159
+
160
+ def current_prediction(self) -> float:
161
+ try:
162
+ return float(self.predict(self.settings()))
163
+ except Exception:
164
+ return float("nan")
165
+
166
+ def prediction_bounds(self, points: int | None = None) -> tuple[float, float]:
167
+ values: list[float] = []
168
+ for factor in self.factors:
169
+ try:
170
+ profile = self.profile(factor.name, points)
171
+ except Exception:
172
+ continue
173
+ values.extend(float(value) for value in profile["y"] if np.isfinite(value))
174
+ current = self.current_prediction()
175
+ if np.isfinite(current):
176
+ values.append(float(current))
177
+ if not values:
178
+ return 0.0, 1.0
179
+ low = min(values)
180
+ high = max(values)
181
+ if high <= low:
182
+ padding = max(abs(low) * 0.05, 1e-9)
183
+ low -= padding
184
+ high += padding
185
+ return float(low), float(high)
186
+
187
+ def resolved_goal(self, goal: ProfilerGoal | None = None) -> ProfilerGoal:
188
+ selected = goal or self.response_goal or ProfilerGoal()
189
+ if selected.goal == "within":
190
+ low, high = self.prediction_bounds()
191
+ if selected.low is not None:
192
+ low = selected.low
193
+ if selected.high is not None:
194
+ high = selected.high
195
+ if high <= low:
196
+ padding = max(abs(low) * 0.05, 1e-9)
197
+ low -= padding
198
+ high += padding
199
+ return ProfilerGoal("within", low, high, None)
200
+ if selected.goal == "target":
201
+ return ProfilerGoal("target", None, None, selected.target)
202
+ return ProfilerGoal(selected.goal)
203
+
204
+ def _response_objective(self, prediction: float, goal: ProfilerGoal) -> float:
205
+ if not np.isfinite(prediction):
206
+ return float("nan")
207
+ if goal.goal == "maximize":
208
+ return -float(prediction)
209
+ if goal.goal == "minimize":
210
+ return float(prediction)
211
+ if goal.goal == "target" and goal.target is not None:
212
+ return abs(float(prediction) - goal.target)
213
+ if goal.goal == "within" and goal.low is not None and goal.high is not None:
214
+ if goal.low <= prediction <= goal.high:
215
+ return 0.0
216
+ return min(abs(prediction - goal.low), abs(prediction - goal.high))
217
+ return float("nan")
218
+
219
+ def optimize_response(
220
+ self,
221
+ goal: ProfilerGoal | None = None,
222
+ maxiter: int = 60,
223
+ popsize: int = 8,
224
+ seed: int = 42,
225
+ max_categorical_combinations: int = 5000,
226
+ ) -> ProfilerOptimization:
227
+ selected = self.resolved_goal(goal)
228
+ if selected.goal == "target" and selected.target is None:
229
+ raise ValueError("A target value is required for a target goal.")
230
+ active = [factor for factor in self.factors if not factor.locked]
231
+ continuous = [
232
+ factor
233
+ for factor in active
234
+ if factor.kind == "continuous" and factor.high > factor.low
235
+ ]
236
+ categorical = [
237
+ factor
238
+ for factor in active
239
+ if factor.kind == "categorical" and factor.levels
240
+ ]
241
+ base = {factor.name: factor.current for factor in self.factors}
242
+ combination_count = math.prod(len(factor.levels) for factor in categorical) if categorical else 1
243
+ if combination_count > max(1, int(max_categorical_combinations)):
244
+ raise ValueError(
245
+ f"Too many categorical factor combinations ({combination_count}). "
246
+ "Reduce categorical factors or increase the combination limit."
247
+ )
248
+ combinations = (
249
+ list(product(*(tuple(factor.levels) for factor in categorical)))
250
+ if categorical
251
+ else [()]
252
+ )
253
+ best: tuple[float, float, dict[str, Any]] | None = None
254
+
255
+ def consider(settings: dict[str, Any]) -> None:
256
+ nonlocal best
257
+ try:
258
+ prediction = float(self.predict(settings))
259
+ except Exception:
260
+ return
261
+ objective_value = self._response_objective(prediction, selected)
262
+ if not np.isfinite(objective_value):
263
+ return
264
+ if best is None or objective_value < best[0]:
265
+ best = (float(objective_value), prediction, dict(settings))
266
+
267
+ for index, combination in enumerate(combinations):
268
+ fixed_values = {
269
+ factor.name: str(value)
270
+ for factor, value in zip(categorical, combination)
271
+ }
272
+ baseline = dict(base)
273
+ baseline.update(fixed_values)
274
+ consider(baseline)
275
+
276
+ def objective(values: np.ndarray) -> float:
277
+ settings = dict(base)
278
+ settings.update(fixed_values)
279
+ for factor, value in zip(continuous, values):
280
+ settings[factor.name] = float(value)
281
+ try:
282
+ prediction = float(self.predict(settings))
283
+ except Exception:
284
+ return 1e12
285
+ value = self._response_objective(prediction, selected)
286
+ return 1e12 if not np.isfinite(value) else float(value)
287
+
288
+ if not continuous:
289
+ continue
290
+ try:
291
+ outcome = differential_evolution(
292
+ objective,
293
+ [(factor.low, factor.high) for factor in continuous],
294
+ seed=int(seed) + index,
295
+ maxiter=max(1, int(maxiter)),
296
+ popsize=max(1, int(popsize)),
297
+ tol=1e-6,
298
+ polish=True,
299
+ workers=1,
300
+ )
301
+ except Exception:
302
+ continue
303
+ if not np.isfinite(outcome.fun):
304
+ continue
305
+ settings = dict(base)
306
+ settings.update(fixed_values)
307
+ for factor, value in zip(continuous, outcome.x):
308
+ settings[factor.name] = float(value)
309
+ consider(settings)
310
+
311
+ if best is None:
312
+ raise ValueError("Global response optimization could not find valid settings.")
313
+ _, prediction, settings = best
314
+ for factor in self.factors:
315
+ if factor.name in settings:
316
+ factor.current = factor.coerce(settings[factor.name])
317
+ self.response_goal = selected
318
+ return ProfilerOptimization(prediction=float(prediction), settings=self.settings())
319
+
320
+ def profile(self, name: str, points: int | None = None) -> dict[str, Any]:
321
+ factor = self.factor(name)
322
+ base = self.settings()
323
+ count = max(3, int(points or self.points))
324
+ if factor.kind == "continuous":
325
+ grid = np.linspace(factor.low, factor.high, count) if factor.high != factor.low else np.array([factor.low])
326
+ predicted = []
327
+ for value in grid:
328
+ settings = dict(base)
329
+ settings[factor.name] = float(value)
330
+ predicted.append(float(self.predict(settings)))
331
+ return {
332
+ "x": [float(value) for value in grid],
333
+ "y": predicted,
334
+ "labels": None,
335
+ "current": float(factor.current),
336
+ }
337
+ levels = list(factor.levels)
338
+ predicted = []
339
+ for level in levels:
340
+ settings = dict(base)
341
+ settings[factor.name] = level
342
+ predicted.append(float(self.predict(settings)))
343
+ return {
344
+ "x": list(range(len(levels))),
345
+ "y": predicted,
346
+ "labels": levels,
347
+ "current": float(factor.level_index()),
348
+ }
349
+
350
+ def snapshot_frame(self, points: int | None = None) -> pd.DataFrame:
351
+ rows: list[dict[str, Any]] = []
352
+ for factor in self.factors:
353
+ profile = self.profile(factor.name, points)
354
+ labels = profile["labels"]
355
+ for index, (level, predicted) in enumerate(zip(profile["x"], profile["y"])):
356
+ if factor.kind == "categorical":
357
+ rows.append(
358
+ {
359
+ "Factor": factor.name,
360
+ "Level": float(index),
361
+ "Level Label": labels[index] if labels else "",
362
+ "Categorical": True,
363
+ "Predicted": predicted,
364
+ }
365
+ )
366
+ else:
367
+ rows.append(
368
+ {
369
+ "Factor": factor.name,
370
+ "Level": float(level),
371
+ "Level Label": f"{float(level):.6g}",
372
+ "Categorical": False,
373
+ "Predicted": predicted,
374
+ }
375
+ )
376
+ return pd.DataFrame(
377
+ rows,
378
+ columns=["Factor", "Level", "Level Label", "Categorical", "Predicted"],
379
+ )