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
staplot/stats/anova.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import itertools
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
from scipy import stats
|
|
5
|
+
from statsmodels.stats.multicomp import pairwise_tukeyhsd
|
|
6
|
+
from statsmodels.stats.multitest import multipletests
|
|
7
|
+
|
|
8
|
+
from ..models.project import DataTable, AnalysisResult
|
|
9
|
+
from .descriptive import describe, format_p, significance_stars
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def dunn_test(arrays) -> list[tuple[int, int, float]]:
|
|
13
|
+
"""Dunn's test (1964) after Kruskal-Wallis, Bonferroni-corrected.
|
|
14
|
+
|
|
15
|
+
Compares mean ranks of the POOLED sample (the ranks Kruskal-Wallis tested),
|
|
16
|
+
with the tie-corrected variance
|
|
17
|
+
z_ij = (Rbar_i - Rbar_j) / sqrt((N(N+1)/12 - T/(12(N-1))) * (1/n_i + 1/n_j)),
|
|
18
|
+
T = sum(t^3 - t) over tie groups; two-sided normal p-values x number of pairs.
|
|
19
|
+
Returns [(i, j, p_adj), ...] for every pair of group indices."""
|
|
20
|
+
arrays = [np.asarray(a, dtype=float) for a in arrays]
|
|
21
|
+
sizes = [len(a) for a in arrays]
|
|
22
|
+
pooled = np.concatenate(arrays)
|
|
23
|
+
n_tot = len(pooled)
|
|
24
|
+
ranks = stats.rankdata(pooled)
|
|
25
|
+
mean_ranks = []
|
|
26
|
+
start = 0
|
|
27
|
+
for n in sizes:
|
|
28
|
+
mean_ranks.append(float(ranks[start:start + n].mean()))
|
|
29
|
+
start += n
|
|
30
|
+
_, tie_counts = np.unique(pooled, return_counts=True)
|
|
31
|
+
ties = float(np.sum(tie_counts.astype(float) ** 3 - tie_counts))
|
|
32
|
+
base_var = n_tot * (n_tot + 1) / 12.0 - ties / (12.0 * (n_tot - 1))
|
|
33
|
+
|
|
34
|
+
pairs = list(itertools.combinations(range(len(arrays)), 2))
|
|
35
|
+
m = len(pairs)
|
|
36
|
+
out = []
|
|
37
|
+
for i, j in pairs:
|
|
38
|
+
se = np.sqrt(base_var * (1.0 / sizes[i] + 1.0 / sizes[j]))
|
|
39
|
+
if not np.isfinite(se) or se <= 0:
|
|
40
|
+
p = float("nan")
|
|
41
|
+
else:
|
|
42
|
+
z = abs(mean_ranks[i] - mean_ranks[j]) / se
|
|
43
|
+
p = min(1.0, 2.0 * float(stats.norm.sf(z)) * m)
|
|
44
|
+
out.append((i, j, p))
|
|
45
|
+
return out
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def run_one_way_anova(table: DataTable, posthoc: str = "tukey") -> AnalysisResult:
|
|
49
|
+
groups = table.group_arrays()
|
|
50
|
+
names = list(groups.keys())
|
|
51
|
+
arrays = [groups[n] for n in names]
|
|
52
|
+
|
|
53
|
+
if len(names) < 2:
|
|
54
|
+
raise ValueError("Need at least 2 groups for ANOVA.")
|
|
55
|
+
if any(len(a) < 2 for a in arrays):
|
|
56
|
+
raise ValueError("Each group needs at least 2 values.")
|
|
57
|
+
|
|
58
|
+
F, p = stats.f_oneway(*arrays)
|
|
59
|
+
|
|
60
|
+
desc_header = f"{'Group':20s} {'N':>6s} {'Mean':>12s} {'SD':>12s}"
|
|
61
|
+
desc_rows = []
|
|
62
|
+
for n, a in zip(names, arrays):
|
|
63
|
+
d = describe(a)
|
|
64
|
+
desc_rows.append(f"{n:20s} {d['n']:>6d} {d['mean']:>12.4f} {d['sd']:>12.4f}")
|
|
65
|
+
|
|
66
|
+
ph_lines: list[str] = []
|
|
67
|
+
if posthoc == "tukey" and len(names) > 2:
|
|
68
|
+
all_vals = np.concatenate(arrays)
|
|
69
|
+
all_labels = np.concatenate([[n] * len(a) for n, a in zip(names, arrays)])
|
|
70
|
+
tukey = pairwise_tukeyhsd(all_vals, all_labels)
|
|
71
|
+
ph_lines = ["", "Post-hoc (Tukey HSD):", str(tukey.summary())]
|
|
72
|
+
|
|
73
|
+
lines = [
|
|
74
|
+
"=== One-Way ANOVA ===",
|
|
75
|
+
"",
|
|
76
|
+
desc_header,
|
|
77
|
+
"-" * len(desc_header),
|
|
78
|
+
*desc_rows,
|
|
79
|
+
"",
|
|
80
|
+
f"F statistic: {F:.4f}",
|
|
81
|
+
f"{format_p(p)} {significance_stars(p)}",
|
|
82
|
+
*ph_lines,
|
|
83
|
+
]
|
|
84
|
+
return AnalysisResult(
|
|
85
|
+
name=f"One-Way ANOVA ({', '.join(names)})",
|
|
86
|
+
method="one-way-anova",
|
|
87
|
+
source_table=table.name,
|
|
88
|
+
summary_text="\n".join(lines),
|
|
89
|
+
details={"F": float(F), "p": float(p), "groups": names},
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def run_kruskal_wallis(table: DataTable) -> AnalysisResult:
|
|
94
|
+
groups = table.group_arrays()
|
|
95
|
+
names = list(groups.keys())
|
|
96
|
+
arrays = [groups[n] for n in names]
|
|
97
|
+
|
|
98
|
+
if len(names) < 2:
|
|
99
|
+
raise ValueError("Need at least 2 groups.")
|
|
100
|
+
|
|
101
|
+
H, p = stats.kruskal(*arrays)
|
|
102
|
+
|
|
103
|
+
ph_lines: list[str] = []
|
|
104
|
+
if len(names) > 2:
|
|
105
|
+
ph_lines = ["", "Post-hoc (Dunn's test / Bonferroni):"]
|
|
106
|
+
ph_lines.append(f"{'Comparison':35s} {'adj. p':>10s} {'sig':>6s}")
|
|
107
|
+
for i, j, ap in dunn_test(arrays):
|
|
108
|
+
label = f"{names[i]} vs {names[j]}"
|
|
109
|
+
ph_lines.append(f"{label:35s} {ap:>10.4f} {significance_stars(ap):>6s}")
|
|
110
|
+
|
|
111
|
+
lines = [
|
|
112
|
+
"=== Kruskal-Wallis Test ===",
|
|
113
|
+
"",
|
|
114
|
+
f"H statistic: {H:.4f}",
|
|
115
|
+
f"{format_p(p)} {significance_stars(p)}",
|
|
116
|
+
*ph_lines,
|
|
117
|
+
]
|
|
118
|
+
return AnalysisResult(
|
|
119
|
+
name=f"Kruskal-Wallis ({', '.join(names)})",
|
|
120
|
+
method="kruskal-wallis",
|
|
121
|
+
source_table=table.name,
|
|
122
|
+
summary_text="\n".join(lines),
|
|
123
|
+
details={"H": float(H), "p": float(p), "groups": names},
|
|
124
|
+
)
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"""Chi-square test of independence and Fisher's exact test for contingency tables."""
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
from scipy import stats
|
|
5
|
+
|
|
6
|
+
from ..models.project import ContingencyTable, AnalysisResult
|
|
7
|
+
from .descriptive import format_p, significance_stars
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def run_chi_square(table: ContingencyTable) -> AnalysisResult:
|
|
11
|
+
counts = table.get_matrix().astype(float)
|
|
12
|
+
if counts.shape[0] < 2 or counts.shape[1] < 2:
|
|
13
|
+
raise ValueError("Need at least a 2×2 contingency table.")
|
|
14
|
+
|
|
15
|
+
chi2, p, dof, expected = stats.chi2_contingency(counts, correction=False)
|
|
16
|
+
chi2_yates, p_yates, _, _ = stats.chi2_contingency(counts, correction=True)
|
|
17
|
+
|
|
18
|
+
# Cramér's V
|
|
19
|
+
n = counts.sum()
|
|
20
|
+
v = np.sqrt(chi2 / (n * (min(counts.shape) - 1))) if n > 0 else np.nan
|
|
21
|
+
|
|
22
|
+
# Row / column totals
|
|
23
|
+
row_totals = counts.sum(axis=1)
|
|
24
|
+
col_totals = counts.sum(axis=0)
|
|
25
|
+
|
|
26
|
+
obs_header = f"{'':20s}" + "".join(f"{c:>10s}" for c in table.col_labels) + f"{'Total':>10s}"
|
|
27
|
+
obs_rows = []
|
|
28
|
+
for i, rl in enumerate(table.row_labels):
|
|
29
|
+
row_str = f"{rl:20s}" + "".join(f"{counts[i,j]:>10.0f}" for j in range(len(table.col_labels)))
|
|
30
|
+
row_str += f"{row_totals[i]:>10.0f}"
|
|
31
|
+
obs_rows.append(row_str)
|
|
32
|
+
total_row = f"{'Total':20s}" + "".join(f"{c:>10.0f}" for c in col_totals) + f"{n:>10.0f}"
|
|
33
|
+
|
|
34
|
+
exp_header = f"\nExpected counts:"
|
|
35
|
+
exp_rows = []
|
|
36
|
+
for i, rl in enumerate(table.row_labels):
|
|
37
|
+
row_str = f"{rl:20s}" + "".join(f"{expected[i,j]:>10.2f}" for j in range(len(table.col_labels)))
|
|
38
|
+
exp_rows.append(row_str)
|
|
39
|
+
|
|
40
|
+
lines = [
|
|
41
|
+
"=== Chi-Square Test of Independence ===",
|
|
42
|
+
"",
|
|
43
|
+
"Observed counts:",
|
|
44
|
+
obs_header,
|
|
45
|
+
"-" * len(obs_header),
|
|
46
|
+
*obs_rows,
|
|
47
|
+
total_row,
|
|
48
|
+
exp_header,
|
|
49
|
+
*exp_rows,
|
|
50
|
+
"",
|
|
51
|
+
f"χ² = {chi2:.4f} (df={dof})",
|
|
52
|
+
f"χ² (Yates) = {chi2_yates:.4f}",
|
|
53
|
+
f"Cramér's V = {v:.4f}",
|
|
54
|
+
"",
|
|
55
|
+
f" Without correction: {format_p(p)} {significance_stars(p)}",
|
|
56
|
+
f" Yates' correction: {format_p(p_yates)} {significance_stars(p_yates)}",
|
|
57
|
+
]
|
|
58
|
+
return AnalysisResult(
|
|
59
|
+
name=f"Chi-square: {table.name}",
|
|
60
|
+
method="chi-square",
|
|
61
|
+
source_table=table.name,
|
|
62
|
+
summary_text="\n".join(lines),
|
|
63
|
+
details={"chi2": float(chi2), "p": float(p), "dof": int(dof), "cramers_v": float(v)},
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def run_fisher_exact(table: ContingencyTable) -> AnalysisResult:
|
|
68
|
+
counts = table.get_matrix()
|
|
69
|
+
if counts.shape != (2, 2):
|
|
70
|
+
raise ValueError("Fisher's exact test requires exactly a 2×2 table.")
|
|
71
|
+
|
|
72
|
+
odds_ratio, p = stats.fisher_exact(counts)
|
|
73
|
+
lines = [
|
|
74
|
+
"=== Fisher's Exact Test (2×2) ===",
|
|
75
|
+
"",
|
|
76
|
+
f"Odds Ratio = {odds_ratio:.4f}",
|
|
77
|
+
f"{format_p(float(p))} {significance_stars(float(p))}",
|
|
78
|
+
]
|
|
79
|
+
return AnalysisResult(
|
|
80
|
+
name=f"Fisher's exact: {table.name}",
|
|
81
|
+
method="fisher-exact",
|
|
82
|
+
source_table=table.name,
|
|
83
|
+
summary_text="\n".join(lines),
|
|
84
|
+
details={"odds_ratio": float(odds_ratio), "p": float(p)},
|
|
85
|
+
)
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"""Pearson and Spearman correlation."""
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
from scipy import stats
|
|
5
|
+
|
|
6
|
+
from ..models.project import DataTable, AnalysisResult
|
|
7
|
+
from .descriptive import format_p, significance_stars
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def run_correlation(
|
|
11
|
+
table: DataTable,
|
|
12
|
+
col_a: str,
|
|
13
|
+
col_b: str,
|
|
14
|
+
method: str = "pearson",
|
|
15
|
+
) -> AnalysisResult:
|
|
16
|
+
"""method: 'pearson' | 'spearman' | 'kendall'"""
|
|
17
|
+
# x/y pairs come from the same row: drop a row if either value is missing
|
|
18
|
+
a, b = table.paired_arrays(col_a, col_b)
|
|
19
|
+
|
|
20
|
+
if len(a) < 3:
|
|
21
|
+
raise ValueError("Need at least 3 paired values for correlation.")
|
|
22
|
+
|
|
23
|
+
method = method.lower()
|
|
24
|
+
if method == "pearson":
|
|
25
|
+
r, p = stats.pearsonr(a, b)
|
|
26
|
+
label = "Pearson r"
|
|
27
|
+
elif method == "spearman":
|
|
28
|
+
r, p = stats.spearmanr(a, b)
|
|
29
|
+
label = "Spearman ρ"
|
|
30
|
+
elif method == "kendall":
|
|
31
|
+
r, p = stats.kendalltau(a, b)
|
|
32
|
+
label = "Kendall τ"
|
|
33
|
+
else:
|
|
34
|
+
raise ValueError(f"Unknown method '{method}'.")
|
|
35
|
+
|
|
36
|
+
r2 = r ** 2
|
|
37
|
+
# 95% CI for Pearson r via Fisher Z
|
|
38
|
+
ci_lo = ci_hi = np.nan
|
|
39
|
+
if method == "pearson" and len(a) > 3:
|
|
40
|
+
z = np.arctanh(np.clip(r, -1 + 1e-15, 1 - 1e-15)) # |r| = 1 → CI [±1, ±1]
|
|
41
|
+
se = 1.0 / np.sqrt(len(a) - 3)
|
|
42
|
+
z_lo = z - 1.96 * se
|
|
43
|
+
z_hi = z + 1.96 * se
|
|
44
|
+
ci_lo, ci_hi = float(np.tanh(z_lo)), float(np.tanh(z_hi))
|
|
45
|
+
|
|
46
|
+
lines = [
|
|
47
|
+
f"=== {label.split()[0]} Correlation ===",
|
|
48
|
+
f"{col_a} vs {col_b}",
|
|
49
|
+
f"N = {len(a)}",
|
|
50
|
+
"",
|
|
51
|
+
f"{label:12s} = {r:>8.4f}",
|
|
52
|
+
f"R² = {r2:>8.4f}",
|
|
53
|
+
f"{format_p(float(p)):20s} {significance_stars(float(p))}",
|
|
54
|
+
]
|
|
55
|
+
if not np.isnan(ci_lo):
|
|
56
|
+
lines.append(f"95% CI [{ci_lo:.4f}, {ci_hi:.4f}]")
|
|
57
|
+
|
|
58
|
+
return AnalysisResult(
|
|
59
|
+
name=f"{label}: {col_a} vs {col_b}",
|
|
60
|
+
method=f"correlation-{method}",
|
|
61
|
+
source_table=table.name,
|
|
62
|
+
summary_text="\n".join(lines),
|
|
63
|
+
details={
|
|
64
|
+
"r": float(r),
|
|
65
|
+
"r2": float(r2),
|
|
66
|
+
"p": float(p),
|
|
67
|
+
"n": int(len(a)),
|
|
68
|
+
"col_a": col_a,
|
|
69
|
+
"col_b": col_b,
|
|
70
|
+
},
|
|
71
|
+
)
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
from scipy.optimize import curve_fit
|
|
3
|
+
|
|
4
|
+
from ..models.project import DataTable, AnalysisResult
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def _linear(x, slope, intercept):
|
|
8
|
+
return slope * x + intercept
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _exponential(x, a, b):
|
|
12
|
+
return a * np.exp(b * x)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _hill(x, bottom, top, ec50, hillslope):
|
|
16
|
+
"""4-parameter logistic (Hill) — dose-response / sigmoidal."""
|
|
17
|
+
return bottom + (top - bottom) / (1.0 + (ec50 / np.where(x == 0, 1e-12, x)) ** hillslope)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _michaelis_menten(x, vmax, km):
|
|
21
|
+
return vmax * x / (km + x)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
MODELS: dict[str, tuple] = {
|
|
25
|
+
"linear": (_linear, ["slope", "intercept"]),
|
|
26
|
+
"exponential": (_exponential, ["a", "b"]),
|
|
27
|
+
"hill": (_hill, ["Bottom", "Top", "EC50", "HillSlope"]),
|
|
28
|
+
"michaelis_menten": (_michaelis_menten, ["Vmax", "Km"]),
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def run_curve_fit(
|
|
33
|
+
table: DataTable, x_col: str, y_col: str, model: str = "linear"
|
|
34
|
+
) -> AnalysisResult:
|
|
35
|
+
if model not in MODELS:
|
|
36
|
+
raise ValueError(f"Unknown model '{model}'. Choose: {list(MODELS)}")
|
|
37
|
+
|
|
38
|
+
groups = table.group_arrays()
|
|
39
|
+
if x_col not in groups or y_col not in groups:
|
|
40
|
+
raise ValueError("Columns not found in table.")
|
|
41
|
+
|
|
42
|
+
# an (x, y) point is one row: drop rows where either is missing / excluded
|
|
43
|
+
x, y = table.paired_arrays(x_col, y_col)
|
|
44
|
+
|
|
45
|
+
if len(x) < 3:
|
|
46
|
+
raise ValueError("Need at least 3 data points for curve fitting.")
|
|
47
|
+
|
|
48
|
+
func, param_names = MODELS[model]
|
|
49
|
+
popt, pcov = curve_fit(func, x, y, maxfev=50_000)
|
|
50
|
+
|
|
51
|
+
y_pred = func(x, *popt)
|
|
52
|
+
ss_res = np.sum((y - y_pred) ** 2)
|
|
53
|
+
ss_tot = np.sum((y - np.mean(y)) ** 2)
|
|
54
|
+
r2 = 1 - ss_res / ss_tot if ss_tot > 0 else 0.0
|
|
55
|
+
perr = np.sqrt(np.diag(pcov))
|
|
56
|
+
|
|
57
|
+
param_header = f"{'Parameter':15s} {'Value':>15s} {'Std Error':>15s}"
|
|
58
|
+
param_rows = [
|
|
59
|
+
f"{name:15s} {val:>15.6g} {err:>15.6g}"
|
|
60
|
+
for name, val, err in zip(param_names, popt, perr)
|
|
61
|
+
]
|
|
62
|
+
|
|
63
|
+
lines = [
|
|
64
|
+
f"=== Curve Fitting ({model}) ===",
|
|
65
|
+
f"X: {x_col} | Y: {y_col}",
|
|
66
|
+
f"N = {len(x)}",
|
|
67
|
+
f"R² = {r2:.6f}",
|
|
68
|
+
"",
|
|
69
|
+
param_header,
|
|
70
|
+
"-" * len(param_header),
|
|
71
|
+
*param_rows,
|
|
72
|
+
]
|
|
73
|
+
return AnalysisResult(
|
|
74
|
+
name=f"Curve fit ({model}): {y_col} vs {x_col}",
|
|
75
|
+
method=f"curve-fit-{model}",
|
|
76
|
+
source_table=table.name,
|
|
77
|
+
summary_text="\n".join(lines),
|
|
78
|
+
details={
|
|
79
|
+
"model": model,
|
|
80
|
+
"params": {n: float(v) for n, v in zip(param_names, popt)},
|
|
81
|
+
"r2": float(r2),
|
|
82
|
+
"x_col": x_col,
|
|
83
|
+
"y_col": y_col,
|
|
84
|
+
},
|
|
85
|
+
)
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
from scipy import stats
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def describe(data: np.ndarray) -> dict:
|
|
6
|
+
clean = data[~np.isnan(data)]
|
|
7
|
+
n = len(clean)
|
|
8
|
+
if n == 0:
|
|
9
|
+
return {"n": 0, "mean": np.nan, "sd": np.nan, "sem": np.nan,
|
|
10
|
+
"ci95": np.nan, "median": np.nan, "q1": np.nan, "q3": np.nan,
|
|
11
|
+
"min": np.nan, "max": np.nan}
|
|
12
|
+
mean = float(np.mean(clean))
|
|
13
|
+
sd = float(np.std(clean, ddof=1)) if n > 1 else np.nan
|
|
14
|
+
sem = float(stats.sem(clean)) if n > 1 else np.nan
|
|
15
|
+
ci95 = float(stats.t.ppf(0.975, df=n - 1) * sem) if n > 1 else np.nan
|
|
16
|
+
q1, median, q3 = (float(x) for x in np.percentile(clean, [25, 50, 75]))
|
|
17
|
+
return {
|
|
18
|
+
"n": n,
|
|
19
|
+
"mean": mean,
|
|
20
|
+
"sd": sd,
|
|
21
|
+
"sem": sem,
|
|
22
|
+
"ci95": ci95,
|
|
23
|
+
"median": median,
|
|
24
|
+
"q1": q1,
|
|
25
|
+
"q3": q3,
|
|
26
|
+
"min": float(np.min(clean)),
|
|
27
|
+
"max": float(np.max(clean)),
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def format_p(p: float) -> str:
|
|
32
|
+
if np.isnan(p):
|
|
33
|
+
return "p = n/a"
|
|
34
|
+
if p < 0.0001:
|
|
35
|
+
return "p < 0.0001"
|
|
36
|
+
if p < 0.001:
|
|
37
|
+
return "p < 0.001"
|
|
38
|
+
return f"p = {p:.4f}"
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def significance_stars(p: float) -> str:
|
|
42
|
+
if np.isnan(p):
|
|
43
|
+
return "n/a"
|
|
44
|
+
if p < 0.0001:
|
|
45
|
+
return "****"
|
|
46
|
+
if p < 0.001:
|
|
47
|
+
return "***"
|
|
48
|
+
if p < 0.01:
|
|
49
|
+
return "**"
|
|
50
|
+
if p < 0.05:
|
|
51
|
+
return "*"
|
|
52
|
+
return "ns"
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
"""Distribution fitting, shape analysis, and test recommendation."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
from scipy import stats
|
|
7
|
+
|
|
8
|
+
from ..models.project import DataTable, AnalysisResult
|
|
9
|
+
from .descriptive import format_p
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
# ── Available distributions ──────────────────────────────────────────
|
|
13
|
+
|
|
14
|
+
_DISTS: dict[str, stats.rv_continuous] = {
|
|
15
|
+
"Normal": stats.norm,
|
|
16
|
+
"Log-normal": stats.lognorm,
|
|
17
|
+
"Exponential": stats.expon,
|
|
18
|
+
"Gamma": stats.gamma,
|
|
19
|
+
"Weibull": stats.weibull_min,
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
# distributions that require strictly positive data
|
|
23
|
+
_POSITIVE_ONLY = {"Log-normal", "Exponential", "Gamma", "Weibull"}
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
# ── Shape description ────────────────────────────────────────────────
|
|
27
|
+
|
|
28
|
+
def describe_shape(arr: np.ndarray) -> dict:
|
|
29
|
+
"""Return skewness, kurtosis (excess), and human-readable labels."""
|
|
30
|
+
skew = float(stats.skew(arr))
|
|
31
|
+
kurt = float(stats.kurtosis(arr)) # excess kurtosis (Normal = 0)
|
|
32
|
+
|
|
33
|
+
if abs(skew) < 0.5:
|
|
34
|
+
skew_label = "symmetric"
|
|
35
|
+
elif skew > 0.5:
|
|
36
|
+
skew_label = "right-skewed (+)"
|
|
37
|
+
else:
|
|
38
|
+
skew_label = "left-skewed (-)"
|
|
39
|
+
|
|
40
|
+
if abs(kurt) < 1.0:
|
|
41
|
+
kurt_label = "mesokurtic (normal-like tails)"
|
|
42
|
+
elif kurt > 1.0:
|
|
43
|
+
kurt_label = "leptokurtic (heavy tails)"
|
|
44
|
+
else:
|
|
45
|
+
kurt_label = "platykurtic (light tails)"
|
|
46
|
+
|
|
47
|
+
return {
|
|
48
|
+
"skewness": skew,
|
|
49
|
+
"kurtosis": kurt,
|
|
50
|
+
"skewness_label": skew_label,
|
|
51
|
+
"kurtosis_label": kurt_label,
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
# ── Distribution fitting ─────────────────────────────────────────────
|
|
56
|
+
|
|
57
|
+
def fit_distributions(arr: np.ndarray) -> list[dict]:
|
|
58
|
+
"""
|
|
59
|
+
Fit each distribution in _DISTS to *arr* using MLE.
|
|
60
|
+
Returns a list of result dicts sorted by AIC (best first).
|
|
61
|
+
"""
|
|
62
|
+
n = len(arr)
|
|
63
|
+
results: list[dict] = []
|
|
64
|
+
|
|
65
|
+
for name, dist in _DISTS.items():
|
|
66
|
+
if name in _POSITIVE_ONLY and np.any(arr <= 0):
|
|
67
|
+
continue
|
|
68
|
+
try:
|
|
69
|
+
params = dist.fit(arr)
|
|
70
|
+
log_like = float(np.sum(dist.logpdf(arr, *params)))
|
|
71
|
+
k = len(params)
|
|
72
|
+
aic = 2 * k - 2 * log_like
|
|
73
|
+
bic = k * np.log(n) - 2 * log_like
|
|
74
|
+
# KS goodness-of-fit against the fitted distribution
|
|
75
|
+
ks_stat, ks_p = stats.kstest(arr, dist.cdf, args=params)
|
|
76
|
+
results.append({
|
|
77
|
+
"distribution": name,
|
|
78
|
+
"params": params,
|
|
79
|
+
"log_likelihood": log_like,
|
|
80
|
+
"aic": aic,
|
|
81
|
+
"bic": bic,
|
|
82
|
+
"ks_stat": float(ks_stat),
|
|
83
|
+
"ks_p": float(ks_p),
|
|
84
|
+
})
|
|
85
|
+
except Exception:
|
|
86
|
+
pass
|
|
87
|
+
|
|
88
|
+
results.sort(key=lambda x: x["aic"])
|
|
89
|
+
return results
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
# ── Test recommendation ───────────────────────────────────────────────
|
|
93
|
+
|
|
94
|
+
def _recommend(is_normal: bool) -> dict:
|
|
95
|
+
if is_normal:
|
|
96
|
+
return {
|
|
97
|
+
"2_groups": "t-test (paired or unpaired)",
|
|
98
|
+
"3plus_groups": "One-Way ANOVA (+Tukey post-hoc)",
|
|
99
|
+
"correlation": "Pearson r",
|
|
100
|
+
"rm": "Repeated-Measures ANOVA",
|
|
101
|
+
"parametric": True,
|
|
102
|
+
}
|
|
103
|
+
else:
|
|
104
|
+
return {
|
|
105
|
+
"2_groups": "Mann-Whitney U / Wilcoxon signed-rank",
|
|
106
|
+
"3plus_groups": "Kruskal-Wallis (+Dunn post-hoc)",
|
|
107
|
+
"correlation": "Spearman rho / Kendall tau",
|
|
108
|
+
"rm": "Friedman test",
|
|
109
|
+
"parametric": False,
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
# ── Main entry point ─────────────────────────────────────────────────
|
|
114
|
+
|
|
115
|
+
def run_distribution_check(table: DataTable) -> AnalysisResult:
|
|
116
|
+
"""
|
|
117
|
+
Full per-column distribution analysis:
|
|
118
|
+
- Normality tests (reuses run_normality details)
|
|
119
|
+
- Shape stats (skewness, kurtosis)
|
|
120
|
+
- Distribution fitting (top 5 distributions by AIC)
|
|
121
|
+
- Test recommendation
|
|
122
|
+
"""
|
|
123
|
+
from .normality import run_normality
|
|
124
|
+
|
|
125
|
+
norm_result = run_normality(table)
|
|
126
|
+
|
|
127
|
+
lines = ["=== Distribution Check ===", ""]
|
|
128
|
+
all_details: dict = {}
|
|
129
|
+
|
|
130
|
+
for col in table.columns:
|
|
131
|
+
arr = col.valid_values()
|
|
132
|
+
n = len(arr)
|
|
133
|
+
lines.append(f"{'─'*52}")
|
|
134
|
+
lines.append(f" Column : {col.name} (n={n})")
|
|
135
|
+
lines.append(f"{'─'*52}")
|
|
136
|
+
|
|
137
|
+
if n < 3:
|
|
138
|
+
lines.append(" Insufficient data (need n >= 3).")
|
|
139
|
+
lines.append("")
|
|
140
|
+
all_details[col.name] = {"n": n, "error": "insufficient data"}
|
|
141
|
+
continue
|
|
142
|
+
|
|
143
|
+
# ── Shape stats ───────────────────────────────────────────
|
|
144
|
+
shape = describe_shape(arr)
|
|
145
|
+
skew = shape["skewness"]
|
|
146
|
+
kurt = shape["kurtosis"]
|
|
147
|
+
lines.append(f" Skewness : {skew:+.3f} ({shape['skewness_label']})")
|
|
148
|
+
lines.append(f" Excess kurtosis : {kurt:+.3f} ({shape['kurtosis_label']})")
|
|
149
|
+
lines.append("")
|
|
150
|
+
|
|
151
|
+
# ── Normality tests ───────────────────────────────────────
|
|
152
|
+
nd = norm_result.details.get(col.name, {})
|
|
153
|
+
sw_p = nd.get("shapiro_p", float("nan"))
|
|
154
|
+
dap_p = nd.get("dagostino_p", float("nan"))
|
|
155
|
+
ks_p = nd.get("ks_p", float("nan"))
|
|
156
|
+
|
|
157
|
+
lines.append(" Normality tests:")
|
|
158
|
+
if not np.isnan(sw_p):
|
|
159
|
+
lines.append(f" Shapiro-Wilk p = {format_p(sw_p)}")
|
|
160
|
+
if not np.isnan(dap_p):
|
|
161
|
+
lines.append(f" D'Agostino-Pearson p = {format_p(dap_p)}")
|
|
162
|
+
if not np.isnan(ks_p):
|
|
163
|
+
lines.append(f" KS (vs Normal) p = {format_p(ks_p)}")
|
|
164
|
+
|
|
165
|
+
# Consensus: PASS only if Shapiro-Wilk (most sensitive) says OK
|
|
166
|
+
is_normal = (not np.isnan(sw_p)) and (sw_p >= 0.05)
|
|
167
|
+
verdict_str = "PASS (normal)" if is_normal else "FAIL (non-normal)"
|
|
168
|
+
lines.append(f" Verdict : {verdict_str}")
|
|
169
|
+
lines.append("")
|
|
170
|
+
|
|
171
|
+
# ── Distribution fitting ──────────────────────────────────
|
|
172
|
+
fits = fit_distributions(arr)
|
|
173
|
+
if fits:
|
|
174
|
+
lines.append(" Distribution fitting (ranked by AIC):")
|
|
175
|
+
lines.append(f" {'Distribution':<15s} {'AIC':>8s} {'KS p':>8s} {'OK?'}")
|
|
176
|
+
for fit in fits[:5]:
|
|
177
|
+
ok = "yes" if fit["ks_p"] >= 0.05 else "no"
|
|
178
|
+
lines.append(
|
|
179
|
+
f" {fit['distribution']:<15s} {fit['aic']:>8.1f} {fit['ks_p']:>8.4f} {ok}"
|
|
180
|
+
)
|
|
181
|
+
best_name = fits[0]["distribution"]
|
|
182
|
+
lines.append(f" Best-fit dist : {best_name} (AIC={fits[0]['aic']:.1f})")
|
|
183
|
+
lines.append("")
|
|
184
|
+
else:
|
|
185
|
+
fits = []
|
|
186
|
+
best_name = "n/a"
|
|
187
|
+
|
|
188
|
+
# ── Test recommendation ───────────────────────────────────
|
|
189
|
+
rec = _recommend(is_normal)
|
|
190
|
+
lines.append(" Recommended tests:")
|
|
191
|
+
lines.append(f" 2 groups : {rec['2_groups']}")
|
|
192
|
+
lines.append(f" 3+ groups : {rec['3plus_groups']}")
|
|
193
|
+
lines.append(f" Correlation: {rec['correlation']}")
|
|
194
|
+
lines.append(f" Rep. measures: {rec['rm']}")
|
|
195
|
+
lines.append("")
|
|
196
|
+
|
|
197
|
+
all_details[col.name] = {
|
|
198
|
+
"n": n,
|
|
199
|
+
"skewness": skew,
|
|
200
|
+
"kurtosis": kurt,
|
|
201
|
+
"is_normal": is_normal,
|
|
202
|
+
"shapiro_p": sw_p,
|
|
203
|
+
"dagostino_p": dap_p,
|
|
204
|
+
"ks_p_norm": ks_p,
|
|
205
|
+
"fits": [
|
|
206
|
+
{k: (v.tolist() if isinstance(v, np.ndarray) else
|
|
207
|
+
float(v) if isinstance(v, (np.floating, np.integer)) else v)
|
|
208
|
+
for k, v in f.items() if k != "params"}
|
|
209
|
+
for f in fits[:5]
|
|
210
|
+
],
|
|
211
|
+
"best_fit": best_name,
|
|
212
|
+
"rec_2groups": rec["2_groups"],
|
|
213
|
+
"rec_3groups": rec["3plus_groups"],
|
|
214
|
+
"parametric": rec["parametric"],
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
return AnalysisResult(
|
|
218
|
+
name=f"Distribution check: {table.name}",
|
|
219
|
+
method="distribution-check",
|
|
220
|
+
source_table=table.name,
|
|
221
|
+
summary_text="\n".join(lines),
|
|
222
|
+
details=all_details,
|
|
223
|
+
)
|