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,467 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from PySide6.QtCore import Qt
|
|
4
|
+
from PySide6.QtWidgets import (
|
|
5
|
+
QAbstractItemView,
|
|
6
|
+
QDialogButtonBox,
|
|
7
|
+
QHBoxLayout,
|
|
8
|
+
QLabel,
|
|
9
|
+
QListWidgetItem,
|
|
10
|
+
QMessageBox,
|
|
11
|
+
QPushButton,
|
|
12
|
+
QTableWidget,
|
|
13
|
+
QTableWidgetItem,
|
|
14
|
+
QWidget,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
from statstudio.stats import custom_doe
|
|
18
|
+
from statstudio.ui.dialogs.base import CheckList, BaseDialog, combo_value, text_value
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class CustomDesignDialog(BaseDialog):
|
|
22
|
+
def __init__(self, parent=None) -> None:
|
|
23
|
+
super().__init__(None, "Custom Design", parent)
|
|
24
|
+
self.setMinimumWidth(760)
|
|
25
|
+
self.preview_widget: QTableWidget | None = None
|
|
26
|
+
self.preview_design = None
|
|
27
|
+
self.design_spec: custom_doe.CustomDesignSpec | None = None
|
|
28
|
+
self._design_job = None
|
|
29
|
+
self._build_responses_page()
|
|
30
|
+
self._build_factors_page()
|
|
31
|
+
self._build_model_page()
|
|
32
|
+
self._build_constraints_page()
|
|
33
|
+
self._build_generation_page()
|
|
34
|
+
self._build_preview_page()
|
|
35
|
+
self._refresh_terms()
|
|
36
|
+
self._refresh_constraint_columns()
|
|
37
|
+
ok = self._buttons.button(QDialogButtonBox.StandardButton.Ok)
|
|
38
|
+
if ok is not None:
|
|
39
|
+
ok.setText("Make Table")
|
|
40
|
+
make_button = QPushButton("Make Design")
|
|
41
|
+
self._buttons.addButton(make_button, QDialogButtonBox.ButtonRole.ActionRole)
|
|
42
|
+
make_button.clicked.connect(self.make_design)
|
|
43
|
+
|
|
44
|
+
# ------------------------------------------------------------ responses
|
|
45
|
+
def _build_responses_page(self) -> None:
|
|
46
|
+
self._add_page("Responses")
|
|
47
|
+
self.add_label(
|
|
48
|
+
"Add one or more responses. Goals and limits are saved with the design so they can be used "
|
|
49
|
+
"by the profiler and multi-response optimization later."
|
|
50
|
+
)
|
|
51
|
+
self.response_table = QTableWidget(1, 7)
|
|
52
|
+
self.response_table.setHorizontalHeaderLabels(
|
|
53
|
+
["Response", "Goal", "Lower", "Upper", "Target", "Importance", "Units"]
|
|
54
|
+
)
|
|
55
|
+
self.response_table.horizontalHeader().setStretchLastSection(True)
|
|
56
|
+
self.response_table.setSelectionBehavior(QAbstractItemView.SelectionBehavior.SelectRows)
|
|
57
|
+
self._set_response_row(0, "Response", "None", "", "", "", "1", "")
|
|
58
|
+
self.add_row("Responses", self.response_table)
|
|
59
|
+
buttons = QHBoxLayout()
|
|
60
|
+
add_button = QPushButton("Add response")
|
|
61
|
+
add_button.clicked.connect(self._add_response)
|
|
62
|
+
remove_button = QPushButton("Remove selected")
|
|
63
|
+
remove_button.clicked.connect(self._remove_response)
|
|
64
|
+
buttons.addWidget(add_button)
|
|
65
|
+
buttons.addWidget(remove_button)
|
|
66
|
+
buttons.addStretch(1)
|
|
67
|
+
holder = QWidget()
|
|
68
|
+
holder.setLayout(buttons)
|
|
69
|
+
self.add_row("", holder)
|
|
70
|
+
|
|
71
|
+
def _set_response_row(
|
|
72
|
+
self,
|
|
73
|
+
row: int,
|
|
74
|
+
name: str,
|
|
75
|
+
goal: str,
|
|
76
|
+
lower: str,
|
|
77
|
+
upper: str,
|
|
78
|
+
target: str,
|
|
79
|
+
importance: str,
|
|
80
|
+
units: str,
|
|
81
|
+
) -> None:
|
|
82
|
+
for column, value in enumerate([name, goal, lower, upper, target, importance, units]):
|
|
83
|
+
self.response_table.setItem(row, column, QTableWidgetItem(str(value)))
|
|
84
|
+
|
|
85
|
+
def _add_response(self) -> None:
|
|
86
|
+
row = self.response_table.rowCount()
|
|
87
|
+
self.response_table.insertRow(row)
|
|
88
|
+
self._set_response_row(row, f"Response {row + 1}", "None", "", "", "", "1", "")
|
|
89
|
+
|
|
90
|
+
def _remove_response(self) -> None:
|
|
91
|
+
rows = sorted({index.row() for index in self.response_table.selectedIndexes()}, reverse=True)
|
|
92
|
+
for row in rows:
|
|
93
|
+
self.response_table.removeRow(row)
|
|
94
|
+
if self.response_table.rowCount() == 0:
|
|
95
|
+
self._set_response_row(0, "Response", "None", "", "", "", "1", "")
|
|
96
|
+
self.response_table.setRowCount(1)
|
|
97
|
+
|
|
98
|
+
# -------------------------------------------------------------- factors
|
|
99
|
+
def _build_factors_page(self) -> None:
|
|
100
|
+
self._add_page("Factors")
|
|
101
|
+
self.add_label(
|
|
102
|
+
"Continuous factors use low/high levels. Categorical and blocking factors use comma-separated "
|
|
103
|
+
"levels. A blocking factor is added automatically to the design model."
|
|
104
|
+
)
|
|
105
|
+
self.factor_table = QTableWidget(2, 6)
|
|
106
|
+
self.factor_table.setHorizontalHeaderLabels(["Factor", "Role", "Low", "High", "Levels (cat/block)", "Units"])
|
|
107
|
+
self.factor_table.horizontalHeader().setStretchLastSection(True)
|
|
108
|
+
self.factor_table.setSelectionBehavior(QAbstractItemView.SelectionBehavior.SelectRows)
|
|
109
|
+
self._set_factor_row(0, "A", "continuous", "0", "1", "", "")
|
|
110
|
+
self._set_factor_row(1, "B", "continuous", "0", "1", "", "")
|
|
111
|
+
self.add_row("Factors", self.factor_table)
|
|
112
|
+
buttons = QHBoxLayout()
|
|
113
|
+
for label, role in [
|
|
114
|
+
("Add continuous", "continuous"),
|
|
115
|
+
("Add categorical", "categorical"),
|
|
116
|
+
("Add blocking", "blocking"),
|
|
117
|
+
]:
|
|
118
|
+
button = QPushButton(label)
|
|
119
|
+
button.clicked.connect(lambda checked=False, value=role: self._add_factor(value))
|
|
120
|
+
buttons.addWidget(button)
|
|
121
|
+
remove_button = QPushButton("Remove selected")
|
|
122
|
+
remove_button.clicked.connect(self._remove_factor)
|
|
123
|
+
buttons.addWidget(remove_button)
|
|
124
|
+
buttons.addStretch(1)
|
|
125
|
+
holder = QWidget()
|
|
126
|
+
holder.setLayout(buttons)
|
|
127
|
+
self.add_row("", holder)
|
|
128
|
+
|
|
129
|
+
def _set_factor_row(self, row: int, name: str, role: str, low: str, high: str, levels: str, units: str) -> None:
|
|
130
|
+
for column, value in enumerate([name, role, low, high, levels, units]):
|
|
131
|
+
self.factor_table.setItem(row, column, QTableWidgetItem(str(value)))
|
|
132
|
+
|
|
133
|
+
def _add_factor(self, role: str = "continuous") -> None:
|
|
134
|
+
row = self.factor_table.rowCount()
|
|
135
|
+
self.factor_table.insertRow(row)
|
|
136
|
+
name = chr(ord("A") + row) if row < 26 else f"F{row + 1}"
|
|
137
|
+
if role == "continuous":
|
|
138
|
+
self._set_factor_row(row, name, role, "0", "1", "", "")
|
|
139
|
+
elif role == "categorical":
|
|
140
|
+
self._set_factor_row(row, name, role, "", "", "Level 1, Level 2", "")
|
|
141
|
+
else:
|
|
142
|
+
self._set_factor_row(row, name, role, "", "", "Block 1, Block 2", "")
|
|
143
|
+
self._refresh_terms()
|
|
144
|
+
self._refresh_constraint_columns()
|
|
145
|
+
|
|
146
|
+
def _remove_factor(self) -> None:
|
|
147
|
+
rows = sorted({index.row() for index in self.factor_table.selectedIndexes()}, reverse=True)
|
|
148
|
+
for row in rows:
|
|
149
|
+
self.factor_table.removeRow(row)
|
|
150
|
+
if self.factor_table.rowCount() == 0:
|
|
151
|
+
self._set_factor_row(0, "A", "continuous", "0", "1", "", "")
|
|
152
|
+
self.factor_table.setRowCount(1)
|
|
153
|
+
self._refresh_terms()
|
|
154
|
+
self._refresh_constraint_columns()
|
|
155
|
+
|
|
156
|
+
# ---------------------------------------------------------------- model
|
|
157
|
+
def _build_model_page(self) -> None:
|
|
158
|
+
self._add_page("Model")
|
|
159
|
+
self.add_label(
|
|
160
|
+
"Choose the terms the design must estimate. Quadratic terms are available for continuous "
|
|
161
|
+
"factors only. Blocking terms are added automatically."
|
|
162
|
+
)
|
|
163
|
+
self.term_list = CheckList([])
|
|
164
|
+
self.term_list.setMaximumHeight(220)
|
|
165
|
+
self.add_row("Terms", self.term_list)
|
|
166
|
+
buttons = QHBoxLayout()
|
|
167
|
+
for label, mode in [
|
|
168
|
+
("Refresh terms", "refresh"),
|
|
169
|
+
("Main effects", "main"),
|
|
170
|
+
("2-factor interactions", "interaction"),
|
|
171
|
+
("Quadratic terms", "quadratic"),
|
|
172
|
+
("Full quadratic", "all"),
|
|
173
|
+
]:
|
|
174
|
+
button = QPushButton(label)
|
|
175
|
+
button.clicked.connect(lambda checked=False, value=mode: self._select_terms(value))
|
|
176
|
+
buttons.addWidget(button)
|
|
177
|
+
holder = QWidget()
|
|
178
|
+
holder.setLayout(buttons)
|
|
179
|
+
self.add_row("", holder)
|
|
180
|
+
|
|
181
|
+
def _factor_specs(self, strict: bool = True) -> list[custom_doe.CustomFactor]:
|
|
182
|
+
factors: list[custom_doe.CustomFactor] = []
|
|
183
|
+
names: set[str] = set()
|
|
184
|
+
for row in range(self.factor_table.rowCount()):
|
|
185
|
+
name_item = self.factor_table.item(row, 0)
|
|
186
|
+
name = name_item.text().strip() if name_item else ""
|
|
187
|
+
if not name:
|
|
188
|
+
continue
|
|
189
|
+
if name in names:
|
|
190
|
+
raise ValueError(f"Duplicate factor name: {name}")
|
|
191
|
+
names.add(name)
|
|
192
|
+
role_item = self.factor_table.item(row, 1)
|
|
193
|
+
role = role_item.text().strip().lower() if role_item else "continuous"
|
|
194
|
+
levels_item = self.factor_table.item(row, 4)
|
|
195
|
+
levels = [item.strip() for item in (levels_item.text().split(",") if levels_item else []) if item.strip()]
|
|
196
|
+
units_item = self.factor_table.item(row, 5)
|
|
197
|
+
units = units_item.text().strip() if units_item else ""
|
|
198
|
+
if role == "continuous":
|
|
199
|
+
try:
|
|
200
|
+
low = float(self.factor_table.item(row, 2).text())
|
|
201
|
+
high = float(self.factor_table.item(row, 3).text())
|
|
202
|
+
except Exception as exc:
|
|
203
|
+
raise ValueError(f"Factor {name}: enter numeric low and high levels.") from exc
|
|
204
|
+
factors.append(custom_doe.CustomFactor(name, role="continuous", low=low, high=high, units=units))
|
|
205
|
+
else:
|
|
206
|
+
if strict and len(levels) < 2:
|
|
207
|
+
raise ValueError(f"Factor {name}: enter at least two levels.")
|
|
208
|
+
factors.append(custom_doe.CustomFactor(name, role=role, levels=levels, units=units))
|
|
209
|
+
if strict and not factors:
|
|
210
|
+
raise ValueError("Define at least one factor.")
|
|
211
|
+
return factors
|
|
212
|
+
|
|
213
|
+
def _refresh_terms(self) -> None:
|
|
214
|
+
try:
|
|
215
|
+
factors = self._factor_specs(False)
|
|
216
|
+
terms = custom_doe.default_terms(factors)
|
|
217
|
+
except Exception:
|
|
218
|
+
terms = []
|
|
219
|
+
self.term_list.clear()
|
|
220
|
+
for term in terms:
|
|
221
|
+
item = QListWidgetItem(term)
|
|
222
|
+
item.setFlags(item.flags() | Qt.ItemFlag.ItemIsUserCheckable)
|
|
223
|
+
item.setCheckState(Qt.CheckState.Checked)
|
|
224
|
+
self.term_list.addItem(item)
|
|
225
|
+
|
|
226
|
+
def _select_terms(self, mode: str) -> None:
|
|
227
|
+
if mode == "refresh":
|
|
228
|
+
self._refresh_terms()
|
|
229
|
+
return
|
|
230
|
+
for index in range(self.term_list.count()):
|
|
231
|
+
item = self.term_list.item(index)
|
|
232
|
+
text = item.text()
|
|
233
|
+
if mode == "main":
|
|
234
|
+
checked = "*" not in text and "^" not in text
|
|
235
|
+
elif mode == "interaction":
|
|
236
|
+
checked = "*" in text and "^" not in text
|
|
237
|
+
elif mode == "quadratic":
|
|
238
|
+
checked = "^2" in text
|
|
239
|
+
else:
|
|
240
|
+
checked = True
|
|
241
|
+
item.setCheckState(Qt.CheckState.Checked if checked else Qt.CheckState.Unchecked)
|
|
242
|
+
|
|
243
|
+
def _terms(self) -> list[str]:
|
|
244
|
+
return self.term_list.checked_items() if hasattr(self.term_list, "checked_items") else []
|
|
245
|
+
|
|
246
|
+
# ----------------------------------------------------------- constraints
|
|
247
|
+
def _build_constraints_page(self) -> None:
|
|
248
|
+
self._add_page("Constraints")
|
|
249
|
+
self.add_label(
|
|
250
|
+
"Optional linear constraints use actual factor values, for example A + B <= 12. "
|
|
251
|
+
"Only continuous factors can be used."
|
|
252
|
+
)
|
|
253
|
+
self.constraint_table = QTableWidget(0, 2)
|
|
254
|
+
self.add_row("Constraints", self.constraint_table)
|
|
255
|
+
buttons = QHBoxLayout()
|
|
256
|
+
add_button = QPushButton("Add constraint")
|
|
257
|
+
add_button.clicked.connect(self._add_constraint)
|
|
258
|
+
remove_button = QPushButton("Remove selected")
|
|
259
|
+
remove_button.clicked.connect(self._remove_constraint)
|
|
260
|
+
buttons.addWidget(add_button)
|
|
261
|
+
buttons.addWidget(remove_button)
|
|
262
|
+
buttons.addStretch(1)
|
|
263
|
+
holder = QWidget()
|
|
264
|
+
holder.setLayout(buttons)
|
|
265
|
+
self.add_row("", holder)
|
|
266
|
+
|
|
267
|
+
def _refresh_constraint_columns(self) -> None:
|
|
268
|
+
try:
|
|
269
|
+
continuous = [factor.name for factor in self._factor_specs(False) if factor.role == "continuous"]
|
|
270
|
+
except Exception:
|
|
271
|
+
continuous = []
|
|
272
|
+
self.constraint_table.clear()
|
|
273
|
+
self.constraint_table.setColumnCount(len(continuous) + 2)
|
|
274
|
+
self.constraint_table.setHorizontalHeaderLabels(continuous + ["Relation", "Bound"])
|
|
275
|
+
self.constraint_table.setRowCount(0)
|
|
276
|
+
|
|
277
|
+
def _add_constraint(self) -> None:
|
|
278
|
+
row = self.constraint_table.rowCount()
|
|
279
|
+
self.constraint_table.insertRow(row)
|
|
280
|
+
for column in range(self.constraint_table.columnCount() - 2):
|
|
281
|
+
self.constraint_table.setItem(row, column, QTableWidgetItem("0"))
|
|
282
|
+
self.constraint_table.setItem(row, self.constraint_table.columnCount() - 2, QTableWidgetItem("<="))
|
|
283
|
+
self.constraint_table.setItem(row, self.constraint_table.columnCount() - 1, QTableWidgetItem("0"))
|
|
284
|
+
|
|
285
|
+
def _remove_constraint(self) -> None:
|
|
286
|
+
rows = sorted({index.row() for index in self.constraint_table.selectedIndexes()}, reverse=True)
|
|
287
|
+
for row in rows:
|
|
288
|
+
self.constraint_table.removeRow(row)
|
|
289
|
+
|
|
290
|
+
def _constraints(self) -> list[custom_doe.LinearConstraint]:
|
|
291
|
+
constraints: list[custom_doe.LinearConstraint] = []
|
|
292
|
+
factor_columns = self.constraint_table.columnCount() - 2
|
|
293
|
+
if factor_columns <= 0:
|
|
294
|
+
return constraints
|
|
295
|
+
for row in range(self.constraint_table.rowCount()):
|
|
296
|
+
coefficients: dict[str, float] = {}
|
|
297
|
+
for column in range(factor_columns):
|
|
298
|
+
header = self.constraint_table.horizontalHeaderItem(column)
|
|
299
|
+
name = header.text() if header else ""
|
|
300
|
+
item = self.constraint_table.item(row, column)
|
|
301
|
+
text = item.text().strip() if item else "0"
|
|
302
|
+
try:
|
|
303
|
+
value = float(text or 0)
|
|
304
|
+
except Exception as exc:
|
|
305
|
+
raise ValueError(f"Constraint row {row + 1}: coefficient for {name} must be numeric.") from exc
|
|
306
|
+
if abs(value) > 1e-12:
|
|
307
|
+
coefficients[name] = value
|
|
308
|
+
if not coefficients:
|
|
309
|
+
continue
|
|
310
|
+
relation_item = self.constraint_table.item(row, factor_columns)
|
|
311
|
+
bound_item = self.constraint_table.item(row, factor_columns + 1)
|
|
312
|
+
relation = relation_item.text().strip() if relation_item else "<="
|
|
313
|
+
try:
|
|
314
|
+
bound = float(bound_item.text().strip() if bound_item else "0")
|
|
315
|
+
except Exception as exc:
|
|
316
|
+
raise ValueError(f"Constraint row {row + 1}: bound must be numeric.") from exc
|
|
317
|
+
constraints.append(custom_doe.LinearConstraint(coefficients, relation, bound))
|
|
318
|
+
return constraints
|
|
319
|
+
|
|
320
|
+
# ------------------------------------------------------------ generation
|
|
321
|
+
def _build_generation_page(self) -> None:
|
|
322
|
+
self._add_page("Design Generation")
|
|
323
|
+
self.add_label(
|
|
324
|
+
"The optimal runs are the treatment design. Center and replicate runs are added to that design. "
|
|
325
|
+
"A positive block size creates balanced random blocks; a blocking factor overrides it."
|
|
326
|
+
)
|
|
327
|
+
self.criterion_combo = self.add_combo("Optimality criterion", ["D-optimal", "A-optimal", "I-optimal"], default="D-optimal")
|
|
328
|
+
self.runs_spin = self.add_int("Optimal runs", 12, 1, 500)
|
|
329
|
+
self.centers_spin = self.add_int("Center points", 0, 0, 100)
|
|
330
|
+
self.replicates_spin = self.add_int("Replicate runs", 0, 0, 100)
|
|
331
|
+
self.block_size_spin = self.add_int("Random block size (0 = none)", 0, 0, 100)
|
|
332
|
+
self.starts_spin = self.add_int("Number of starts", 5, 1, 200)
|
|
333
|
+
self.randomize_check = self.add_bool("Randomize run order", True)
|
|
334
|
+
self.seed_spin = self.add_int("Random seed", 42, 0, 2_000_000_000)
|
|
335
|
+
self.name_edit = self.add_text("Table name", "Custom Design")
|
|
336
|
+
|
|
337
|
+
# --------------------------------------------------------------- preview
|
|
338
|
+
def _build_preview_page(self) -> None:
|
|
339
|
+
self._add_page("Preview")
|
|
340
|
+
self.preview_summary = QLabel("Click Make Design to preview the custom design.")
|
|
341
|
+
self.preview_summary.setWordWrap(True)
|
|
342
|
+
self.preview_summary.setStyleSheet("color:#3c4654;")
|
|
343
|
+
self.add_row("Summary", self.preview_summary)
|
|
344
|
+
self.preview_widget = QTableWidget(0, 0)
|
|
345
|
+
self.preview_widget.setEditTriggers(QAbstractItemView.EditTrigger.NoEditTriggers)
|
|
346
|
+
self.add_row("Design", self.preview_widget)
|
|
347
|
+
|
|
348
|
+
def _responses(self) -> list[custom_doe.ResponseSpec]:
|
|
349
|
+
responses: list[custom_doe.ResponseSpec] = []
|
|
350
|
+
names: set[str] = set()
|
|
351
|
+
for row in range(self.response_table.rowCount()):
|
|
352
|
+
name_item = self.response_table.item(row, 0)
|
|
353
|
+
name = name_item.text().strip() if name_item else ""
|
|
354
|
+
if not name:
|
|
355
|
+
continue
|
|
356
|
+
if name in names:
|
|
357
|
+
raise ValueError(f"Duplicate response name: {name}")
|
|
358
|
+
names.add(name)
|
|
359
|
+
values = [self.response_table.item(row, column) for column in range(7)]
|
|
360
|
+
texts = [item.text().strip() if item else "" for item in values]
|
|
361
|
+
goal = texts[1] or "None"
|
|
362
|
+
try:
|
|
363
|
+
lower = float(texts[2]) if texts[2] else None
|
|
364
|
+
upper = float(texts[3]) if texts[3] else None
|
|
365
|
+
target = float(texts[4]) if texts[4] else None
|
|
366
|
+
importance = float(texts[5] or 1)
|
|
367
|
+
except Exception as exc:
|
|
368
|
+
raise ValueError(f"Response {name}: limits and importance must be numeric.") from exc
|
|
369
|
+
responses.append(
|
|
370
|
+
custom_doe.ResponseSpec(
|
|
371
|
+
name=name,
|
|
372
|
+
goal=goal,
|
|
373
|
+
lower=lower,
|
|
374
|
+
upper=upper,
|
|
375
|
+
target=target,
|
|
376
|
+
importance=importance,
|
|
377
|
+
units=texts[6],
|
|
378
|
+
)
|
|
379
|
+
)
|
|
380
|
+
if not responses:
|
|
381
|
+
raise ValueError("Define at least one response.")
|
|
382
|
+
return responses
|
|
383
|
+
|
|
384
|
+
def _spec(self) -> custom_doe.CustomDesignSpec:
|
|
385
|
+
criteria = {"D-optimal": "D", "A-optimal": "A", "I-optimal": "I"}
|
|
386
|
+
factors = self._factor_specs()
|
|
387
|
+
terms = self._terms()
|
|
388
|
+
if not terms:
|
|
389
|
+
terms = custom_doe.default_terms(factors)
|
|
390
|
+
return custom_doe.CustomDesignSpec(
|
|
391
|
+
factors=factors,
|
|
392
|
+
responses=self._responses(),
|
|
393
|
+
terms=terms,
|
|
394
|
+
criterion=criteria.get(combo_value(self.criterion_combo) or "D-optimal", "D"),
|
|
395
|
+
runs=self.runs_spin.value(),
|
|
396
|
+
center_points=self.centers_spin.value(),
|
|
397
|
+
replicate_runs=self.replicates_spin.value(),
|
|
398
|
+
block_size=self.block_size_spin.value(),
|
|
399
|
+
randomize=self.randomize_check.isChecked(),
|
|
400
|
+
seed=self.seed_spin.value(),
|
|
401
|
+
starts=self.starts_spin.value(),
|
|
402
|
+
constraints=self._constraints(),
|
|
403
|
+
name=text_value(self.name_edit) or "Custom Design",
|
|
404
|
+
)
|
|
405
|
+
|
|
406
|
+
def _show_design(self, spec, table) -> None:
|
|
407
|
+
self._design_job = None
|
|
408
|
+
self.design_spec = spec
|
|
409
|
+
self.preview_design = table
|
|
410
|
+
evaluation = table.metadata["design"]["evaluation"]
|
|
411
|
+
blocks = table.metadata["design"].get("block_levels") or []
|
|
412
|
+
summary = (
|
|
413
|
+
f"{table.name}: {len(table)} runs, {evaluation['model_columns']} treatment columns, "
|
|
414
|
+
f"rank {evaluation['rank']}. D-efficiency {evaluation['d_efficiency']:.1f}%; "
|
|
415
|
+
f"A trace {evaluation['a_criterion']:.3f}; I average variance {evaluation['i_criterion']:.3f}; "
|
|
416
|
+
f"max |correlation| {evaluation['max_correlation']:.3f}."
|
|
417
|
+
)
|
|
418
|
+
if blocks:
|
|
419
|
+
summary += f" Blocks: {', '.join(blocks)}."
|
|
420
|
+
self.preview_summary.setText(summary)
|
|
421
|
+
display = table.data.head(100)
|
|
422
|
+
self.preview_widget.setRowCount(len(display))
|
|
423
|
+
self.preview_widget.setColumnCount(len(display.columns))
|
|
424
|
+
self.preview_widget.setHorizontalHeaderLabels([str(column) for column in display.columns])
|
|
425
|
+
for row in range(len(display)):
|
|
426
|
+
for column, name in enumerate(display.columns):
|
|
427
|
+
value = display.iat[row, column]
|
|
428
|
+
if value is None or (isinstance(value, float) and value != value):
|
|
429
|
+
text = ""
|
|
430
|
+
elif isinstance(value, float):
|
|
431
|
+
text = f"{value:.6g}"
|
|
432
|
+
else:
|
|
433
|
+
text = str(value)
|
|
434
|
+
self.preview_widget.setItem(row, column, QTableWidgetItem(text))
|
|
435
|
+
self.preview_widget.resizeColumnsToContents()
|
|
436
|
+
self._tabs.setCurrentIndex(self._tabs.count() - 1)
|
|
437
|
+
|
|
438
|
+
def make_design(self) -> None:
|
|
439
|
+
try:
|
|
440
|
+
spec = self._spec()
|
|
441
|
+
except Exception as exc:
|
|
442
|
+
QMessageBox.critical(self, "Custom Design", str(exc))
|
|
443
|
+
return
|
|
444
|
+
from statstudio.ui.jobs import start_job
|
|
445
|
+
|
|
446
|
+
def failed(message: str, details: str) -> None:
|
|
447
|
+
self._design_job = None
|
|
448
|
+
QMessageBox.critical(self, "Custom Design", f"{message}\n\n{details}".strip())
|
|
449
|
+
|
|
450
|
+
self._design_job = start_job(
|
|
451
|
+
self,
|
|
452
|
+
"Building Custom Design",
|
|
453
|
+
lambda token, progress: custom_doe.build_custom_design(spec),
|
|
454
|
+
on_result=lambda table: self._show_design(spec, table),
|
|
455
|
+
on_error=failed,
|
|
456
|
+
on_cancelled=lambda: setattr(self, "_design_job", None),
|
|
457
|
+
)
|
|
458
|
+
|
|
459
|
+
def make_job(self):
|
|
460
|
+
spec = self._spec()
|
|
461
|
+
return lambda token, progress: custom_doe.build_custom_design(spec)
|
|
462
|
+
|
|
463
|
+
def compute(self):
|
|
464
|
+
spec = self._spec()
|
|
465
|
+
table = custom_doe.build_custom_design(spec)
|
|
466
|
+
self.design_spec = spec
|
|
467
|
+
return table
|