CognitiveScience commited on
Commit
c55481d
·
verified ·
1 Parent(s): 3e4c923

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +163 -0
app.py ADDED
@@ -0,0 +1,163 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # mcp_tools.py
2
+
3
+ import numpy as np
4
+ import pingouin as pg
5
+
6
+ import matplotlib.pyplot as plt
7
+ import io
8
+ import base64
9
+ import json
10
+ def plot_group_distribution(groups_str):
11
+ groups = [np.array([float(x) for x in g.split(",")]) for g in groups_str.split(";")]
12
+
13
+ fig, axes = plt.subplots(1, 3, figsize=(12, 4))
14
+
15
+ # Histogram
16
+ for g in groups:
17
+ axes[0].hist(g, alpha=0.5)
18
+ axes[0].set_title("Histogram")
19
+
20
+ # Boxplot
21
+ axes[1].boxplot(groups)
22
+ axes[1].set_title("Boxplot")
23
+
24
+ # QQ plot (first group)
25
+ pg.qqplot(groups[0], ax=axes[2])
26
+ axes[2].set_title("QQ Plot (Group 1)")
27
+
28
+ buf = io.BytesIO()
29
+ fig.savefig(buf, format="png")
30
+ buf.seek(0)
31
+ return base64.b64encode(buf.read()).decode()
32
+
33
+ def assumption_checks(groups_str):
34
+ import pandas as pd
35
+
36
+ groups = [np.array([float(x) for x in g.split(",")]) for g in groups_str.split(";")]
37
+
38
+
39
+ # Normality (Shapiro) with safety guard
40
+ normality = []
41
+
42
+ for i, g in enumerate(groups):
43
+ if len(g) < 4:
44
+ normality.append({
45
+ "group": f"G{i+1}",
46
+ "error": "Normality test requires at least 4 samples."
47
+ })
48
+ else:
49
+ result = pg.normality(g).to_dict(orient="records")[0]
50
+ normality.append({
51
+ "group": f"G{i+1}",
52
+ **result
53
+ })
54
+
55
+
56
+ # Homogeneity (Levene)
57
+ levene = pg.homoscedasticity(groups, method="levene").to_dict(orient="records")[0]
58
+
59
+ return {
60
+ "normality": normality,
61
+ "homogeneity": levene
62
+ }
63
+
64
+ def posthoc_tests(groups_str, method="games-howell"):
65
+ import pandas as pd
66
+
67
+ groups = [np.array([float(x) for x in g.split(",")]) for g in groups_str.split(";")]
68
+
69
+ data = []
70
+ for i, g in enumerate(groups):
71
+ for val in g:
72
+ data.append({"value": val, "group": f"G{i+1}"})
73
+
74
+ df = pd.DataFrame(data)
75
+
76
+ result = pg.pairwise_ttests(
77
+ dv="value",
78
+ between="group",
79
+ data=df,
80
+ padjust="bonf",
81
+ parametric=(method == "tukey"),
82
+ effsize="hedges"
83
+ )
84
+
85
+ return result.to_dict(orient="records")
86
+
87
+ # -----------------------------
88
+ # Helper: convert Pingouin DF → JSON
89
+ # -----------------------------
90
+ def pg_to_json(df):
91
+ """Convert Pingouin single-row DataFrame to JSON dict."""
92
+ return df.to_dict(orient="records")[0]
93
+
94
+
95
+ # -----------------------------
96
+ # Welch t-test (independent, unequal variances)
97
+ # -----------------------------
98
+ def welch_t_test(group1_str, group2_str, alternative="two-sided"):
99
+ g1 = np.array([float(x) for x in group1_str.split(",")])
100
+ g2 = np.array([float(x) for x in group2_str.split(",")])
101
+
102
+ result = pg.ttest(g1, g2, paired=False, correction=True, alternative=alternative)
103
+ return pg_to_json(result)
104
+
105
+
106
+ # -----------------------------
107
+ # Student t-test (independent, equal variances)
108
+ # -----------------------------
109
+ def student_t_test(group1_str, group2_str, alternative="two-sided"):
110
+ g1 = np.array([float(x) for x in group1_str.split(",")])
111
+ g2 = np.array([float(x) for x in group2_str.split(",")])
112
+
113
+ result = pg.ttest(g1, g2, paired=False, correction=False, alternative=alternative)
114
+ return pg_to_json(result)
115
+
116
+
117
+ # -----------------------------
118
+ # Paired t-test (same subjects measured twice)
119
+ # -----------------------------
120
+ def paired_t_test(group1_str, group2_str, alternative="two-sided"):
121
+ g1 = np.array([float(x) for x in group1_str.split(",")])
122
+ g2 = np.array([float(x) for x in group2_str.split(",")])
123
+
124
+ result = pg.ttest(g1, g2, paired=True, alternative=alternative)
125
+ return pg_to_json(result)
126
+
127
+
128
+ # -----------------------------
129
+ # One-sample t-test (sample vs population mean)
130
+ # -----------------------------
131
+ def one_sample_t_test(group_str, population_mean, alternative="two-sided"):
132
+ g = np.array([float(x) for x in group_str.split(",")])
133
+
134
+ # Adjust sample for one-sample t-test
135
+ adjusted = g - float(population_mean)
136
+
137
+ # Pingouin one-sample t-test: y=None, popmean implicitly = 0
138
+ result = pg.ttest(adjusted, y=None, paired=False, alternative=alternative)
139
+
140
+ return pg_to_json(result)
141
+
142
+
143
+ # -----------------------------
144
+ # One-way ANOVA (multiple independent groups)
145
+ # -----------------------------
146
+ def one_way_anova(groups_str):
147
+ """
148
+ groups_str format:
149
+ "1,2,3; 4,5,6; 7,8,9"
150
+ """
151
+ groups = [np.array([float(x) for x in g.split(",")]) for g in groups_str.split(";")]
152
+
153
+ # Build DataFrame for Pingouin
154
+ import pandas as pd
155
+ data = []
156
+ for i, g in enumerate(groups):
157
+ for val in g:
158
+ data.append({"value": val, "group": f"G{i+1}"})
159
+
160
+ df = pd.DataFrame(data)
161
+
162
+ result = pg.anova(dv="value", between="group", data=df)
163
+ return pg_to_json(result)