staplot 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.
- staplot/__init__.py +1 -0
- staplot/api.py +441 -0
- staplot/main.py +21 -0
- staplot/models/__init__.py +1 -0
- staplot/models/project.py +461 -0
- staplot/plots/__init__.py +14 -0
- staplot/plots/bar.py +104 -0
- staplot/plots/before_after.py +110 -0
- staplot/plots/bland_altman.py +73 -0
- staplot/plots/box.py +95 -0
- staplot/plots/distribution_plot.py +188 -0
- staplot/plots/grouped_bar.py +112 -0
- staplot/plots/heatmap.py +73 -0
- staplot/plots/histogram.py +76 -0
- staplot/plots/qq.py +61 -0
- staplot/plots/residuals.py +72 -0
- staplot/plots/roc_plot.py +40 -0
- staplot/plots/scatter.py +61 -0
- staplot/plots/significance.py +94 -0
- staplot/plots/styles.py +154 -0
- staplot/plots/survival.py +78 -0
- staplot/plots/volcano.py +110 -0
- staplot/stats/__init__.py +15 -0
- staplot/stats/anova.py +124 -0
- staplot/stats/chi_square.py +85 -0
- staplot/stats/correlation.py +71 -0
- staplot/stats/curve_fit.py +85 -0
- staplot/stats/descriptive.py +52 -0
- staplot/stats/distribution.py +223 -0
- staplot/stats/multiple_comparisons.py +104 -0
- staplot/stats/normality.py +67 -0
- staplot/stats/outliers.py +62 -0
- staplot/stats/power.py +130 -0
- staplot/stats/regression.py +95 -0
- staplot/stats/repeated_measures.py +114 -0
- staplot/stats/roc.py +196 -0
- staplot/stats/tests.py +142 -0
- staplot/stats/two_way_anova.py +88 -0
- staplot/ui/__init__.py +0 -0
- staplot/ui/analysis_dialog.py +406 -0
- staplot/ui/axis_dialog.py +104 -0
- staplot/ui/computed_column_dialog.py +261 -0
- staplot/ui/contingency_table_widget.py +158 -0
- staplot/ui/data_table_widget.py +277 -0
- staplot/ui/distribution_inspector.py +431 -0
- staplot/ui/figure_layout_widget.py +236 -0
- staplot/ui/graph_gallery.py +117 -0
- staplot/ui/graph_options_dialog.py +325 -0
- staplot/ui/grouped_table_widget.py +190 -0
- staplot/ui/layout_dialog.py +218 -0
- staplot/ui/main_window.py +1126 -0
- staplot/ui/multiple_comparisons_dialog.py +168 -0
- staplot/ui/navigator.py +88 -0
- staplot/ui/plot_canvas.py +251 -0
- staplot/ui/power_dialog.py +108 -0
- staplot/ui/report_dialog.py +255 -0
- staplot/ui/reshape_dialog.py +286 -0
- staplot/ui/results_widget.py +19 -0
- staplot/ui/standard_curve_dialog.py +150 -0
- staplot/ui/test_wizard.py +252 -0
- staplot/ui/transform_dialog.py +137 -0
- staplot/ui/undo_commands.py +86 -0
- staplot-0.1.0.dist-info/METADATA +85 -0
- staplot-0.1.0.dist-info/RECORD +68 -0
- staplot-0.1.0.dist-info/WHEEL +5 -0
- staplot-0.1.0.dist-info/entry_points.txt +2 -0
- staplot-0.1.0.dist-info/licenses/LICENSE +24 -0
- staplot-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
"""Multiple-comparisons correction dialog."""
|
|
2
|
+
|
|
3
|
+
from qtpy.QtWidgets import (
|
|
4
|
+
QDialog, QVBoxLayout, QHBoxLayout, QFormLayout,
|
|
5
|
+
QComboBox, QDoubleSpinBox, QPushButton, QTextEdit,
|
|
6
|
+
QTableWidget, QTableWidgetItem, QDialogButtonBox,
|
|
7
|
+
QLabel, QHeaderView, QAbstractItemView, QMessageBox,
|
|
8
|
+
)
|
|
9
|
+
from qtpy.QtCore import Qt
|
|
10
|
+
from qtpy.QtGui import QFont
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
_METHODS = [
|
|
14
|
+
("fdr_bh", "Benjamini-Hochberg FDR (recommended)"),
|
|
15
|
+
("fdr_by", "Benjamini-Yekutieli FDR"),
|
|
16
|
+
("bonferroni","Bonferroni"),
|
|
17
|
+
("holm", "Holm (Holm-Bonferroni)"),
|
|
18
|
+
("holm-sidak", "Holm-Sidak"),
|
|
19
|
+
("sidak", "Sidak"),
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class MultipleComparisonsDialog(QDialog):
|
|
24
|
+
"""
|
|
25
|
+
Enter (or auto-import) a list of p-values, choose a correction method,
|
|
26
|
+
and see adjusted p-values.
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
def __init__(self, analyses: list, parent=None):
|
|
30
|
+
super().__init__(parent)
|
|
31
|
+
self.setWindowTitle("Multiple Comparisons Correction")
|
|
32
|
+
self.setMinimumSize(580, 540)
|
|
33
|
+
self._analyses = analyses
|
|
34
|
+
self._result = None
|
|
35
|
+
|
|
36
|
+
layout = QVBoxLayout(self)
|
|
37
|
+
|
|
38
|
+
# ── Method / alpha ────────────────────────────────────────────
|
|
39
|
+
form = QFormLayout()
|
|
40
|
+
self._method_cb = QComboBox()
|
|
41
|
+
for key, label in _METHODS:
|
|
42
|
+
self._method_cb.addItem(label, key)
|
|
43
|
+
form.addRow("Correction method:", self._method_cb)
|
|
44
|
+
|
|
45
|
+
self._alpha_sb = QDoubleSpinBox()
|
|
46
|
+
self._alpha_sb.setRange(0.001, 0.2)
|
|
47
|
+
self._alpha_sb.setSingleStep(0.005)
|
|
48
|
+
self._alpha_sb.setValue(0.05)
|
|
49
|
+
self._alpha_sb.setDecimals(3)
|
|
50
|
+
form.addRow("Family-wise alpha:", self._alpha_sb)
|
|
51
|
+
layout.addLayout(form)
|
|
52
|
+
|
|
53
|
+
# ── Table of comparisons ──────────────────────────────────────
|
|
54
|
+
layout.addWidget(QLabel(
|
|
55
|
+
"Enter comparison names and raw p-values, or click "
|
|
56
|
+
"<b>Import from analyses</b> to load pairwise results automatically."
|
|
57
|
+
))
|
|
58
|
+
|
|
59
|
+
self._tbl = QTableWidget(0, 2)
|
|
60
|
+
self._tbl.setHorizontalHeaderLabels(["Comparison name", "Raw p-value"])
|
|
61
|
+
self._tbl.horizontalHeader().setSectionResizeMode(
|
|
62
|
+
0, QHeaderView.ResizeMode.Stretch)
|
|
63
|
+
self._tbl.horizontalHeader().setSectionResizeMode(
|
|
64
|
+
1, QHeaderView.ResizeMode.ResizeToContents)
|
|
65
|
+
self._tbl.setSelectionBehavior(QAbstractItemView.SelectionBehavior.SelectRows)
|
|
66
|
+
layout.addWidget(self._tbl)
|
|
67
|
+
|
|
68
|
+
# Row control buttons
|
|
69
|
+
row_btns = QHBoxLayout()
|
|
70
|
+
add_btn = QPushButton("+ Row")
|
|
71
|
+
add_btn.clicked.connect(self._add_row)
|
|
72
|
+
del_btn = QPushButton("− Row")
|
|
73
|
+
del_btn.clicked.connect(self._del_row)
|
|
74
|
+
imp_btn = QPushButton("Import from analyses")
|
|
75
|
+
imp_btn.clicked.connect(self._import_from_analyses)
|
|
76
|
+
clr_btn = QPushButton("Clear all")
|
|
77
|
+
clr_btn.clicked.connect(lambda: self._tbl.setRowCount(0))
|
|
78
|
+
for b in (add_btn, del_btn, imp_btn, clr_btn):
|
|
79
|
+
row_btns.addWidget(b)
|
|
80
|
+
row_btns.addStretch()
|
|
81
|
+
layout.addLayout(row_btns)
|
|
82
|
+
|
|
83
|
+
# ── Run button ────────────────────────────────────────────────
|
|
84
|
+
run_btn = QPushButton("Apply correction")
|
|
85
|
+
run_btn.setDefault(True)
|
|
86
|
+
run_btn.clicked.connect(self._run)
|
|
87
|
+
layout.addWidget(run_btn, alignment=Qt.AlignmentFlag.AlignRight)
|
|
88
|
+
|
|
89
|
+
# ── Result box ────────────────────────────────────────────────
|
|
90
|
+
self._result_box = QTextEdit()
|
|
91
|
+
self._result_box.setReadOnly(True)
|
|
92
|
+
mono = QFont("Courier New", 8)
|
|
93
|
+
mono.setStyleHint(QFont.StyleHint.Monospace)
|
|
94
|
+
self._result_box.setFont(mono)
|
|
95
|
+
self._result_box.setMinimumHeight(140)
|
|
96
|
+
layout.addWidget(self._result_box)
|
|
97
|
+
|
|
98
|
+
btns = QDialogButtonBox(
|
|
99
|
+
QDialogButtonBox.StandardButton.Ok
|
|
100
|
+
| QDialogButtonBox.StandardButton.Cancel
|
|
101
|
+
)
|
|
102
|
+
btns.accepted.connect(self.accept)
|
|
103
|
+
btns.rejected.connect(self.reject)
|
|
104
|
+
layout.addWidget(btns)
|
|
105
|
+
|
|
106
|
+
# Pre-populate from analyses
|
|
107
|
+
self._import_from_analyses(silent=True)
|
|
108
|
+
|
|
109
|
+
# ------------------------------------------------------------------
|
|
110
|
+
def _add_row(self):
|
|
111
|
+
r = self._tbl.rowCount()
|
|
112
|
+
self._tbl.insertRow(r)
|
|
113
|
+
self._tbl.setItem(r, 0, QTableWidgetItem(f"Comparison {r + 1}"))
|
|
114
|
+
self._tbl.setItem(r, 1, QTableWidgetItem("1.000"))
|
|
115
|
+
|
|
116
|
+
def _del_row(self):
|
|
117
|
+
rows = sorted({i.row() for i in self._tbl.selectedItems()}, reverse=True)
|
|
118
|
+
for r in rows:
|
|
119
|
+
self._tbl.removeRow(r)
|
|
120
|
+
|
|
121
|
+
def _import_from_analyses(self, silent: bool = False):
|
|
122
|
+
from ..stats.multiple_comparisons import extract_pairwise_p
|
|
123
|
+
names, pvals = extract_pairwise_p(self._analyses)
|
|
124
|
+
if not names:
|
|
125
|
+
if not silent:
|
|
126
|
+
QMessageBox.information(
|
|
127
|
+
self, "Nothing to import",
|
|
128
|
+
"No pairwise comparisons found in existing analyses.\n"
|
|
129
|
+
"Run t-tests or post-hoc ANOVA comparisons first."
|
|
130
|
+
)
|
|
131
|
+
return
|
|
132
|
+
self._tbl.setRowCount(0)
|
|
133
|
+
for name, pv in zip(names, pvals):
|
|
134
|
+
r = self._tbl.rowCount()
|
|
135
|
+
self._tbl.insertRow(r)
|
|
136
|
+
self._tbl.setItem(r, 0, QTableWidgetItem(name))
|
|
137
|
+
self._tbl.setItem(r, 1, QTableWidgetItem(f"{pv:.6g}"))
|
|
138
|
+
|
|
139
|
+
def _collect(self) -> tuple[list[str], list[float]]:
|
|
140
|
+
names, pvals = [], []
|
|
141
|
+
for r in range(self._tbl.rowCount()):
|
|
142
|
+
n_item = self._tbl.item(r, 0)
|
|
143
|
+
p_item = self._tbl.item(r, 1)
|
|
144
|
+
name = n_item.text().strip() if n_item else f"Row {r + 1}"
|
|
145
|
+
try:
|
|
146
|
+
pv = float(p_item.text().strip()) if p_item else 1.0
|
|
147
|
+
except ValueError:
|
|
148
|
+
pv = 1.0
|
|
149
|
+
names.append(name)
|
|
150
|
+
pvals.append(pv)
|
|
151
|
+
return names, pvals
|
|
152
|
+
|
|
153
|
+
def _run(self):
|
|
154
|
+
names, pvals = self._collect()
|
|
155
|
+
if not pvals:
|
|
156
|
+
self._result_box.setPlainText("Add at least one p-value.")
|
|
157
|
+
return
|
|
158
|
+
method = self._method_cb.currentData()
|
|
159
|
+
alpha = self._alpha_sb.value()
|
|
160
|
+
try:
|
|
161
|
+
from ..stats.multiple_comparisons import run_multiple_comparisons
|
|
162
|
+
self._result = run_multiple_comparisons(pvals, names, method, alpha)
|
|
163
|
+
self._result_box.setPlainText(self._result.summary_text)
|
|
164
|
+
except Exception as exc:
|
|
165
|
+
self._result_box.setPlainText(f"Error: {exc}")
|
|
166
|
+
|
|
167
|
+
def get_result(self):
|
|
168
|
+
return self._result
|
staplot/ui/navigator.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
from qtpy.QtWidgets import QTreeWidget, QTreeWidgetItem
|
|
2
|
+
from qtpy.QtCore import Qt, Signal
|
|
3
|
+
from qtpy.QtGui import QFont
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class NavigatorPanel(QTreeWidget):
|
|
7
|
+
"""Left-side tree navigator mirroring Prism's project navigator."""
|
|
8
|
+
|
|
9
|
+
item_activated = Signal(str, str, str) # (section, name, kind)
|
|
10
|
+
|
|
11
|
+
def __init__(self, parent=None):
|
|
12
|
+
super().__init__(parent)
|
|
13
|
+
self.setHeaderHidden(True)
|
|
14
|
+
self.setMinimumWidth(170)
|
|
15
|
+
self.setMaximumWidth(280)
|
|
16
|
+
self.setAnimated(True)
|
|
17
|
+
self.setIndentation(14)
|
|
18
|
+
|
|
19
|
+
bold = QFont()
|
|
20
|
+
bold.setBold(True)
|
|
21
|
+
|
|
22
|
+
self._data_root = QTreeWidgetItem(self, ["Data Tables"])
|
|
23
|
+
self._data_root.setFont(0, bold)
|
|
24
|
+
self._data_root.setExpanded(True)
|
|
25
|
+
|
|
26
|
+
self._analyses_root = QTreeWidgetItem(self, ["Analyses"])
|
|
27
|
+
self._analyses_root.setFont(0, bold)
|
|
28
|
+
self._analyses_root.setExpanded(True)
|
|
29
|
+
|
|
30
|
+
self._graphs_root = QTreeWidgetItem(self, ["Graphs"])
|
|
31
|
+
self._graphs_root.setFont(0, bold)
|
|
32
|
+
self._graphs_root.setExpanded(True)
|
|
33
|
+
|
|
34
|
+
self.itemActivated.connect(self._on_activated)
|
|
35
|
+
self.itemClicked.connect(self._on_activated)
|
|
36
|
+
|
|
37
|
+
self.setStyleSheet("""
|
|
38
|
+
QTreeWidget { border: none; background: #f5f5f5; }
|
|
39
|
+
QTreeWidget::item { padding: 3px 2px; }
|
|
40
|
+
QTreeWidget::item:selected { background: #2d7dd2; color: white; border-radius: 3px; }
|
|
41
|
+
""")
|
|
42
|
+
|
|
43
|
+
# ------------------------------------------------------------------
|
|
44
|
+
def _on_activated(self, item: QTreeWidgetItem, _col: int = 0):
|
|
45
|
+
kind = item.data(0, Qt.ItemDataRole.UserRole)
|
|
46
|
+
if kind:
|
|
47
|
+
section, name = kind
|
|
48
|
+
self.item_activated.emit(section, name, kind[0])
|
|
49
|
+
|
|
50
|
+
def _add_child(self, root: QTreeWidgetItem, section: str, name: str) -> QTreeWidgetItem:
|
|
51
|
+
child = QTreeWidgetItem(root, [name])
|
|
52
|
+
child.setData(0, Qt.ItemDataRole.UserRole, (section, name))
|
|
53
|
+
return child
|
|
54
|
+
|
|
55
|
+
# ------------------------------------------------------------------
|
|
56
|
+
def add_table(self, name: str):
|
|
57
|
+
self._add_child(self._data_root, "table", name)
|
|
58
|
+
self._data_root.setExpanded(True)
|
|
59
|
+
|
|
60
|
+
def add_analysis(self, name: str):
|
|
61
|
+
self._add_child(self._analyses_root, "analysis", name)
|
|
62
|
+
self._analyses_root.setExpanded(True)
|
|
63
|
+
|
|
64
|
+
def add_graph(self, name: str):
|
|
65
|
+
self._add_child(self._graphs_root, "graph", name)
|
|
66
|
+
self._graphs_root.setExpanded(True)
|
|
67
|
+
|
|
68
|
+
def rename_last_table(self, old: str, new: str):
|
|
69
|
+
for i in range(self._data_root.childCount()):
|
|
70
|
+
child = self._data_root.child(i)
|
|
71
|
+
if child.text(0) == old:
|
|
72
|
+
child.setText(0, new)
|
|
73
|
+
child.setData(0, Qt.ItemDataRole.UserRole, ("table", new))
|
|
74
|
+
break
|
|
75
|
+
|
|
76
|
+
def clear_all(self):
|
|
77
|
+
self._data_root.takeChildren()
|
|
78
|
+
self._analyses_root.takeChildren()
|
|
79
|
+
self._graphs_root.takeChildren()
|
|
80
|
+
|
|
81
|
+
def populate(self, project):
|
|
82
|
+
self.clear_all()
|
|
83
|
+
for t in project.tables:
|
|
84
|
+
self.add_table(t.name)
|
|
85
|
+
for a in project.analyses:
|
|
86
|
+
self.add_analysis(a.name)
|
|
87
|
+
for g in project.graphs:
|
|
88
|
+
self.add_graph(g.name)
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import io
|
|
2
|
+
|
|
3
|
+
import matplotlib
|
|
4
|
+
matplotlib.use("QtAgg")
|
|
5
|
+
|
|
6
|
+
from matplotlib.backends.backend_qtagg import FigureCanvasQTAgg, NavigationToolbar2QT
|
|
7
|
+
from matplotlib.figure import Figure
|
|
8
|
+
|
|
9
|
+
from qtpy.QtWidgets import (
|
|
10
|
+
QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QFileDialog,
|
|
11
|
+
QInputDialog, QApplication,
|
|
12
|
+
)
|
|
13
|
+
from qtpy.QtCore import Qt, Signal
|
|
14
|
+
from qtpy.QtGui import QImage
|
|
15
|
+
|
|
16
|
+
from ..plots import styles
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class StyledCanvas(FigureCanvasQTAgg):
|
|
20
|
+
"""Qt canvas that (re)draws inside the staplot style: some settings (e.g.
|
|
21
|
+
the tick label size the number of ticks is chosen from) are read when the
|
|
22
|
+
figure is drawn, not when it is built."""
|
|
23
|
+
|
|
24
|
+
def draw(self):
|
|
25
|
+
with styles.context():
|
|
26
|
+
super().draw()
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def apply_axis_settings(fig: Figure, opts: dict):
|
|
30
|
+
"""Apply axis options dict to every Axes in a Figure."""
|
|
31
|
+
if not opts:
|
|
32
|
+
return
|
|
33
|
+
for ax in fig.get_axes():
|
|
34
|
+
x_scale = opts.get("x_scale", "linear")
|
|
35
|
+
y_scale = opts.get("y_scale", "linear")
|
|
36
|
+
if x_scale == "log":
|
|
37
|
+
ax.set_xscale("log")
|
|
38
|
+
if y_scale == "log":
|
|
39
|
+
ax.set_yscale("log")
|
|
40
|
+
|
|
41
|
+
x_min, x_max = opts.get("x_min"), opts.get("x_max")
|
|
42
|
+
if x_min is not None or x_max is not None:
|
|
43
|
+
lo, hi = ax.get_xlim()
|
|
44
|
+
ax.set_xlim(
|
|
45
|
+
x_min if x_min is not None else lo,
|
|
46
|
+
x_max if x_max is not None else hi,
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
y_min, y_max = opts.get("y_min"), opts.get("y_max")
|
|
50
|
+
y_zero = opts.get("y_zero", False)
|
|
51
|
+
lo_y, hi_y = ax.get_ylim()
|
|
52
|
+
new_lo = 0 if y_zero else (y_min if y_min is not None else lo_y)
|
|
53
|
+
new_hi = y_max if y_max is not None else hi_y
|
|
54
|
+
ax.set_ylim(new_lo, new_hi)
|
|
55
|
+
|
|
56
|
+
if opts.get("x_label"):
|
|
57
|
+
ax.set_xlabel(opts["x_label"])
|
|
58
|
+
if opts.get("y_label"):
|
|
59
|
+
ax.set_ylabel(opts["y_label"])
|
|
60
|
+
|
|
61
|
+
fig.tight_layout()
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
class PlotCanvas(QWidget):
|
|
65
|
+
"""Embeds a matplotlib figure with toolbar, export, clipboard, annotation, and point exclusion."""
|
|
66
|
+
|
|
67
|
+
axes_changed = Signal(dict) # emitted when user saves new axis options
|
|
68
|
+
point_excluded = Signal(str, int, bool) # (column_name, row_index, exclude_all)
|
|
69
|
+
|
|
70
|
+
def __init__(self, parent=None):
|
|
71
|
+
super().__init__(parent)
|
|
72
|
+
layout = QVBoxLayout(self)
|
|
73
|
+
layout.setContentsMargins(4, 4, 4, 4)
|
|
74
|
+
|
|
75
|
+
self._fig = Figure(figsize=(6, 4))
|
|
76
|
+
self._canvas = StyledCanvas(self._fig)
|
|
77
|
+
self._toolbar = NavigationToolbar2QT(self._canvas, self)
|
|
78
|
+
|
|
79
|
+
# ── Button bar ────────────────────────────────────────────────
|
|
80
|
+
btn_bar = QHBoxLayout()
|
|
81
|
+
btn_bar.addWidget(self._toolbar)
|
|
82
|
+
btn_bar.addStretch()
|
|
83
|
+
|
|
84
|
+
axes_btn = QPushButton("Axes…")
|
|
85
|
+
axes_btn.setFixedWidth(64)
|
|
86
|
+
axes_btn.setToolTip("Edit axis scales and ranges")
|
|
87
|
+
axes_btn.clicked.connect(self._edit_axes)
|
|
88
|
+
btn_bar.addWidget(axes_btn)
|
|
89
|
+
|
|
90
|
+
self._annotate_btn = QPushButton("Annotate")
|
|
91
|
+
self._annotate_btn.setFixedWidth(76)
|
|
92
|
+
self._annotate_btn.setCheckable(True)
|
|
93
|
+
self._annotate_btn.setToolTip(
|
|
94
|
+
"Toggle annotation mode: click on the plot to add a text label"
|
|
95
|
+
)
|
|
96
|
+
self._annotate_btn.toggled.connect(self._toggle_annotate)
|
|
97
|
+
btn_bar.addWidget(self._annotate_btn)
|
|
98
|
+
|
|
99
|
+
self._exclude_btn = QPushButton("Exclude")
|
|
100
|
+
self._exclude_btn.setFixedWidth(70)
|
|
101
|
+
self._exclude_btn.setCheckable(True)
|
|
102
|
+
self._exclude_btn.setToolTip(
|
|
103
|
+
"Toggle point-exclusion mode: click a data point to exclude/include it"
|
|
104
|
+
)
|
|
105
|
+
self._exclude_btn.toggled.connect(self._toggle_exclude_mode)
|
|
106
|
+
btn_bar.addWidget(self._exclude_btn)
|
|
107
|
+
|
|
108
|
+
copy_btn = QPushButton("Copy")
|
|
109
|
+
copy_btn.setFixedWidth(56)
|
|
110
|
+
copy_btn.setToolTip("Copy graph to clipboard as PNG")
|
|
111
|
+
copy_btn.clicked.connect(self.copy_to_clipboard)
|
|
112
|
+
btn_bar.addWidget(copy_btn)
|
|
113
|
+
|
|
114
|
+
export_btn = QPushButton("Export…")
|
|
115
|
+
export_btn.setFixedWidth(72)
|
|
116
|
+
export_btn.clicked.connect(self.export_dialog)
|
|
117
|
+
btn_bar.addWidget(export_btn)
|
|
118
|
+
|
|
119
|
+
layout.addLayout(btn_bar)
|
|
120
|
+
layout.addWidget(self._canvas)
|
|
121
|
+
|
|
122
|
+
self._current_axis_opts: dict = {}
|
|
123
|
+
self._annotate_mode = False
|
|
124
|
+
self._exclude_mode = False
|
|
125
|
+
self._cid_click = self._canvas.mpl_connect("button_press_event", self._on_mpl_click)
|
|
126
|
+
self._cid_pick = self._canvas.mpl_connect("pick_event", self._on_pick_event)
|
|
127
|
+
self._show_placeholder()
|
|
128
|
+
|
|
129
|
+
# ------------------------------------------------------------------
|
|
130
|
+
def _show_placeholder(self):
|
|
131
|
+
self._fig.clear()
|
|
132
|
+
ax = self._fig.add_subplot(111)
|
|
133
|
+
ax.text(0.5, 0.5, "No graph selected", ha="center", va="center",
|
|
134
|
+
transform=ax.transAxes, color="#aaaaaa", fontsize=12)
|
|
135
|
+
ax.axis("off")
|
|
136
|
+
self._canvas.draw()
|
|
137
|
+
|
|
138
|
+
@styles.styled
|
|
139
|
+
def show_figure(self, fig: Figure, axis_opts: dict | None = None):
|
|
140
|
+
# Disconnect old handlers from previous figure, reconnect to new
|
|
141
|
+
try:
|
|
142
|
+
self._canvas.mpl_disconnect(self._cid_click)
|
|
143
|
+
self._canvas.mpl_disconnect(self._cid_pick)
|
|
144
|
+
except Exception:
|
|
145
|
+
pass
|
|
146
|
+
self._fig = fig
|
|
147
|
+
if axis_opts:
|
|
148
|
+
self._current_axis_opts = axis_opts
|
|
149
|
+
apply_axis_settings(fig, axis_opts)
|
|
150
|
+
self._canvas.figure = fig
|
|
151
|
+
fig.set_canvas(self._canvas)
|
|
152
|
+
self._cid_click = self._canvas.mpl_connect("button_press_event", self._on_mpl_click)
|
|
153
|
+
self._cid_pick = self._canvas.mpl_connect("pick_event", self._on_pick_event)
|
|
154
|
+
self._canvas.draw()
|
|
155
|
+
|
|
156
|
+
# ------------------------------------------------------------------
|
|
157
|
+
@styles.styled
|
|
158
|
+
def _edit_axes(self):
|
|
159
|
+
from .axis_dialog import AxisDialog
|
|
160
|
+
dlg = AxisDialog(self._current_axis_opts, parent=self)
|
|
161
|
+
if dlg.exec():
|
|
162
|
+
self._current_axis_opts = dlg.get_options()
|
|
163
|
+
apply_axis_settings(self._fig, self._current_axis_opts)
|
|
164
|
+
self._canvas.draw()
|
|
165
|
+
self.axes_changed.emit(self._current_axis_opts)
|
|
166
|
+
|
|
167
|
+
# ------------------------------------------------------------------
|
|
168
|
+
# Annotation mode
|
|
169
|
+
# ------------------------------------------------------------------
|
|
170
|
+
def _toggle_annotate(self, checked: bool):
|
|
171
|
+
self._annotate_mode = checked
|
|
172
|
+
if checked:
|
|
173
|
+
self._exclude_btn.setChecked(False)
|
|
174
|
+
cursor = Qt.CursorShape.CrossCursor if checked else Qt.CursorShape.ArrowCursor
|
|
175
|
+
self._canvas.setCursor(cursor)
|
|
176
|
+
|
|
177
|
+
# ------------------------------------------------------------------
|
|
178
|
+
# Point exclusion mode
|
|
179
|
+
# ------------------------------------------------------------------
|
|
180
|
+
def _toggle_exclude_mode(self, checked: bool):
|
|
181
|
+
self._exclude_mode = checked
|
|
182
|
+
if checked:
|
|
183
|
+
self._annotate_btn.setChecked(False)
|
|
184
|
+
self._exclude_btn.setStyleSheet("background-color: #ff9999;")
|
|
185
|
+
else:
|
|
186
|
+
self._exclude_btn.setStyleSheet("")
|
|
187
|
+
cursor = Qt.CursorShape.PointingHandCursor if checked else Qt.CursorShape.ArrowCursor
|
|
188
|
+
self._canvas.setCursor(cursor)
|
|
189
|
+
|
|
190
|
+
def _on_pick_event(self, event):
|
|
191
|
+
"""Handle matplotlib pick_event: emit point_excluded for the clicked artist."""
|
|
192
|
+
if not self._exclude_mode:
|
|
193
|
+
return
|
|
194
|
+
artist = event.artist
|
|
195
|
+
meta = getattr(artist, "_staplot_meta", None)
|
|
196
|
+
if meta is None:
|
|
197
|
+
return
|
|
198
|
+
col_name = meta["column"]
|
|
199
|
+
row_list = meta["rows"]
|
|
200
|
+
excl_all = meta.get("exclude_all", False)
|
|
201
|
+
for ind in event.ind:
|
|
202
|
+
if ind < len(row_list):
|
|
203
|
+
self.point_excluded.emit(col_name, int(row_list[ind]), excl_all)
|
|
204
|
+
|
|
205
|
+
@styles.styled
|
|
206
|
+
def _on_mpl_click(self, event):
|
|
207
|
+
if not self._annotate_mode:
|
|
208
|
+
return
|
|
209
|
+
if event.inaxes is None or event.xdata is None:
|
|
210
|
+
return
|
|
211
|
+
# Only left-clicks
|
|
212
|
+
if event.button != 1:
|
|
213
|
+
return
|
|
214
|
+
text, ok = QInputDialog.getText(self, "Add Annotation", "Annotation text:")
|
|
215
|
+
if not ok or not text.strip():
|
|
216
|
+
return
|
|
217
|
+
ax = event.inaxes
|
|
218
|
+
ax.text(
|
|
219
|
+
event.xdata, event.ydata, text.strip(),
|
|
220
|
+
fontsize=9, ha="center", va="bottom", color="#222222",
|
|
221
|
+
bbox=dict(boxstyle="round,pad=0.3",
|
|
222
|
+
facecolor="#ffffcc", edgecolor="#888888", alpha=0.85),
|
|
223
|
+
zorder=10,
|
|
224
|
+
)
|
|
225
|
+
self._fig.tight_layout()
|
|
226
|
+
self._canvas.draw()
|
|
227
|
+
|
|
228
|
+
# ------------------------------------------------------------------
|
|
229
|
+
# Clipboard / export
|
|
230
|
+
# ------------------------------------------------------------------
|
|
231
|
+
@styles.styled
|
|
232
|
+
def copy_to_clipboard(self):
|
|
233
|
+
"""Render current figure to PNG and place on system clipboard."""
|
|
234
|
+
buf = io.BytesIO()
|
|
235
|
+
try:
|
|
236
|
+
self._fig.savefig(buf, format="png", dpi=150, bbox_inches="tight")
|
|
237
|
+
except Exception:
|
|
238
|
+
return
|
|
239
|
+
buf.seek(0)
|
|
240
|
+
image = QImage()
|
|
241
|
+
image.loadFromData(buf.read())
|
|
242
|
+
QApplication.clipboard().setImage(image)
|
|
243
|
+
|
|
244
|
+
@styles.styled
|
|
245
|
+
def export_dialog(self):
|
|
246
|
+
path, _ = QFileDialog.getSaveFileName(
|
|
247
|
+
self, "Export Graph", "",
|
|
248
|
+
"PNG (*.png);;SVG (*.svg);;PDF (*.pdf);;TIFF (*.tiff)",
|
|
249
|
+
)
|
|
250
|
+
if path:
|
|
251
|
+
self._fig.savefig(path, dpi=300, bbox_inches="tight")
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"""Power analysis / sample-size calculator dialog."""
|
|
2
|
+
|
|
3
|
+
from qtpy.QtWidgets import (
|
|
4
|
+
QDialog, QVBoxLayout, QFormLayout, QComboBox, QDoubleSpinBox,
|
|
5
|
+
QSpinBox, QDialogButtonBox, QPushButton, QTextEdit, QLabel,
|
|
6
|
+
)
|
|
7
|
+
from qtpy.QtGui import QFont
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class PowerDialog(QDialog):
|
|
11
|
+
"""Standalone power analysis dialog (no table needed)."""
|
|
12
|
+
|
|
13
|
+
def __init__(self, parent=None):
|
|
14
|
+
super().__init__(parent)
|
|
15
|
+
self.setWindowTitle("Power Analysis")
|
|
16
|
+
self.setMinimumWidth(400)
|
|
17
|
+
|
|
18
|
+
layout = QVBoxLayout(self)
|
|
19
|
+
|
|
20
|
+
form = QFormLayout()
|
|
21
|
+
|
|
22
|
+
self._test_cb = QComboBox()
|
|
23
|
+
self._test_cb.addItems([
|
|
24
|
+
"Two-sample t-test (unpaired)",
|
|
25
|
+
"Paired t-test",
|
|
26
|
+
"One-Way ANOVA",
|
|
27
|
+
])
|
|
28
|
+
form.addRow("Test type:", self._test_cb)
|
|
29
|
+
|
|
30
|
+
self._effect_sb = QDoubleSpinBox()
|
|
31
|
+
self._effect_sb.setRange(0.01, 5.0)
|
|
32
|
+
self._effect_sb.setSingleStep(0.05)
|
|
33
|
+
self._effect_sb.setValue(0.5)
|
|
34
|
+
self._effect_sb.setDecimals(3)
|
|
35
|
+
form.addRow("Effect size (Cohen's d / f):", self._effect_sb)
|
|
36
|
+
|
|
37
|
+
self._alpha_sb = QDoubleSpinBox()
|
|
38
|
+
self._alpha_sb.setRange(0.001, 0.2)
|
|
39
|
+
self._alpha_sb.setSingleStep(0.005)
|
|
40
|
+
self._alpha_sb.setValue(0.05)
|
|
41
|
+
self._alpha_sb.setDecimals(3)
|
|
42
|
+
form.addRow("α (significance level):", self._alpha_sb)
|
|
43
|
+
|
|
44
|
+
self._power_sb = QDoubleSpinBox()
|
|
45
|
+
self._power_sb.setRange(0.5, 0.999)
|
|
46
|
+
self._power_sb.setSingleStep(0.05)
|
|
47
|
+
self._power_sb.setValue(0.80)
|
|
48
|
+
self._power_sb.setDecimals(3)
|
|
49
|
+
form.addRow("Target power (1−β):", self._power_sb)
|
|
50
|
+
|
|
51
|
+
self._n_sb = QSpinBox()
|
|
52
|
+
self._n_sb.setRange(0, 100_000)
|
|
53
|
+
self._n_sb.setValue(0)
|
|
54
|
+
self._n_sb.setSpecialValueText("Auto (solve for n)")
|
|
55
|
+
form.addRow("N per group (0 = solve):", self._n_sb)
|
|
56
|
+
|
|
57
|
+
self._k_sb = QSpinBox()
|
|
58
|
+
self._k_sb.setRange(2, 50)
|
|
59
|
+
self._k_sb.setValue(3)
|
|
60
|
+
form.addRow("Groups k (ANOVA only):", self._k_sb)
|
|
61
|
+
|
|
62
|
+
layout.addLayout(form)
|
|
63
|
+
|
|
64
|
+
hint = QLabel(
|
|
65
|
+
"Cohen's d: small = 0.20 | medium = 0.50 | large = 0.80\n"
|
|
66
|
+
"Cohen's f: small = 0.10 | medium = 0.25 | large = 0.40"
|
|
67
|
+
)
|
|
68
|
+
hint.setStyleSheet("color: #666; font-size: 10px;")
|
|
69
|
+
layout.addWidget(hint)
|
|
70
|
+
|
|
71
|
+
calc_btn = QPushButton("Calculate")
|
|
72
|
+
calc_btn.setDefault(True)
|
|
73
|
+
calc_btn.clicked.connect(self._calculate)
|
|
74
|
+
layout.addWidget(calc_btn)
|
|
75
|
+
|
|
76
|
+
self._result_box = QTextEdit()
|
|
77
|
+
self._result_box.setReadOnly(True)
|
|
78
|
+
mono = QFont("Courier New", 9)
|
|
79
|
+
mono.setStyleHint(QFont.StyleHint.Monospace)
|
|
80
|
+
self._result_box.setFont(mono)
|
|
81
|
+
self._result_box.setMinimumHeight(130)
|
|
82
|
+
layout.addWidget(self._result_box)
|
|
83
|
+
|
|
84
|
+
btns = QDialogButtonBox(QDialogButtonBox.StandardButton.Close)
|
|
85
|
+
btns.rejected.connect(self.reject)
|
|
86
|
+
layout.addWidget(btns)
|
|
87
|
+
|
|
88
|
+
# ------------------------------------------------------------------
|
|
89
|
+
def _calculate(self):
|
|
90
|
+
test = self._test_cb.currentText()
|
|
91
|
+
effect = self._effect_sb.value()
|
|
92
|
+
alpha = self._alpha_sb.value()
|
|
93
|
+
power = self._power_sb.value()
|
|
94
|
+
n_val = self._n_sb.value()
|
|
95
|
+
k = self._k_sb.value()
|
|
96
|
+
n = None if n_val == 0 else n_val
|
|
97
|
+
|
|
98
|
+
try:
|
|
99
|
+
if "ANOVA" in test:
|
|
100
|
+
from ..stats.power import run_power_anova
|
|
101
|
+
result = run_power_anova(effect, alpha, power, k, n)
|
|
102
|
+
else:
|
|
103
|
+
from ..stats.power import run_power_ttest
|
|
104
|
+
paired = "Paired" in test
|
|
105
|
+
result = run_power_ttest(effect, alpha, power, n, paired)
|
|
106
|
+
self._result_box.setPlainText(result.summary_text)
|
|
107
|
+
except Exception as exc:
|
|
108
|
+
self._result_box.setPlainText(f"Error: {exc}")
|