-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata2boxplot.py
More file actions
303 lines (251 loc) Β· 11.9 KB
/
data2boxplot.py
File metadata and controls
303 lines (251 loc) Β· 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import io
from scipy.stats import f_oneway
import statsmodels.api as sm
from statsmodels.formula.api import ols
from statsmodels.stats.multicomp import pairwise_tukeyhsd
import csv
import os
os.environ["STREAMLIT_SERVER_PORT"] = os.getenv("PORT", "8080")
os.environ["STREAMLIT_SERVER_ADDRESS"] = "0.0.0.0"
import streamlit as st
st.set_page_config(page_title="\U0001F4E6 Data to Boxplot + ANOVA", layout="wide")
st.markdown("""
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-T32QR2N5C2"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-T32QR2N5C2');
</script>
""", unsafe_allow_html=True)
# --- Custom UI Styling ---
st.markdown("""
<style>
.block-container {
padding-top: 1rem;
padding-bottom: 5rem;
}
.stMultiSelect, .stSelectbox, .stTextInput {
font-size: 15px;
margin-bottom: 0.3rem;
}
.stFileUploader > div {
height: 75px;
border: 2px dashed #aaa;
border-radius: 5px;
display: flex;
align-items: center;
justify-content: center;
}
.stRadio > div, .stCheckbox > div {
display: flex;
gap: 2rem;
flex-wrap: wrap;
}
.twocol {
display: flex;
justify-content: space-between;
gap: 2rem;
}
.twocol > div {
flex: 1;
}
</style>
""", unsafe_allow_html=True)
st.title("π¦ Data to Boxplot + ANOVA")
# --- File upload section ---
st.markdown("## \U0001F4C1 Upload Your Data")
st.caption("Supports .csv, .xlsx, .xls files up to 200MB.")
with st.container():
col1, col2 = st.columns([1, 1])
with col1:
uploaded_files = st.file_uploader(
label="Drag and drop files here",
type=["csv", "xlsx", "xls"],
accept_multiple_files=True,
label_visibility="visible"
)
with col2:
delimiter = None
autodetect = True
if uploaded_files:
st.markdown("### \U0001F50D Delimiter Settings (for CSVs only)")
autodetect = st.checkbox("Auto-detect delimiter for CSVs", value=True, key="autodetect_checkbox")
if not autodetect:
delimiter_options = {
"Comma ( , )": ",",
"Tab (\\t)": "\t",
"Semicolon ( ; )": ";",
"Pipe ( | )": "|",
"Other (custom)": "custom"
}
delimiter_choice = st.selectbox("Choose delimiter for CSV files", list(delimiter_options.keys()), key="delimiter_choice")
delimiter = delimiter_options[delimiter_choice]
if delimiter == "custom":
delimiter = st.text_input("Enter your custom delimiter", value=",", key="custom_delim")
def detect_delimiter(file):
try:
sample = file.read(1024).decode('utf-8', errors='ignore')
file.seek(0)
sniffer = csv.Sniffer()
dialect = sniffer.sniff(sample, delimiters=[",", ";", "\t", "|"])
return dialect.delimiter
except Exception:
return None
if uploaded_files:
st.markdown("---")
st.markdown("### ποΈ Column Selection and Labeling for Each File")
dataframes = []
for idx, file in enumerate(uploaded_files):
st.markdown(f"#### βοΈ File: `{file.name}`")
try:
if file.name.endswith(".csv"):
used_delimiter = delimiter
if autodetect:
used_delimiter = detect_delimiter(file)
if used_delimiter is None:
st.error(f"β Could not auto-detect delimiter for `{file.name}`. Please choose one manually.")
continue
df = pd.read_csv(file, delimiter=used_delimiter)
elif file.name.endswith((".xlsx", ".xls")):
df = pd.read_excel(file)
else:
st.error(f"β Unsupported file type: `{file.name}`")
continue
except Exception as e:
st.error(f"β Could not read `{file.name}`: {e}")
continue
if df.empty or df.shape[1] == 0:
st.warning(f"β οΈ `{file.name}` is empty or invalid.")
continue
numeric_cols = df.select_dtypes(include="number").columns.tolist()
selected_cols = st.multiselect(f"Select numeric columns from `{file.name}`", numeric_cols, key=f"cols_{idx}")
if not selected_cols:
st.warning("β οΈ Please select at least one column to proceed.")
for col in selected_cols:
with st.expander(f"βοΈ Settings for column: `{col}`", expanded=True):
label = st.text_input(
f"Label for `{col}`",
value=f"{file.name.split('.')[0]}_{col}",
key=f"label_{idx}_{col}"
)
temp_df = pd.DataFrame({"Value": df[col].dropna()})
temp_df["Group"] = label
dataframes.append(temp_df)
if dataframes:
df_all = pd.concat(dataframes, ignore_index=True)
st.success("β
Data combined successfully.")
st.download_button("π₯ Download Combined Data", data=df_all.to_csv(index=False).encode(), file_name="combined_data.csv", mime="text/csv")
if st.checkbox("π Show Combined Data Table"):
st.dataframe(df_all)
if df_all.isnull().values.any():
st.warning("β οΈ Missing values found.")
if st.checkbox("Remove rows with missing values?"):
df_all = df_all.dropna()
st.success("β
Removed rows with missing values.")
y_axis = "Value"
x_axis = "Group"
plot_title = st.text_input("π Plot Title", value="Group Comparison")
with st.container():
col1, col2 = st.columns([1, 1])
with col1:
custom_x_label = st.text_input("X-axis Label", value=x_axis)
show_sample_sizes = st.checkbox("Show sample sizes below groups?", value=True)
if st.checkbox("π« Remove outliers (IQR method)?"):
Q1 = df_all[y_axis].quantile(0.25)
Q3 = df_all[y_axis].quantile(0.75)
IQR = Q3 - Q1
df_all = df_all[(df_all[y_axis] >= Q1 - 1.5 * IQR) & (df_all[y_axis] <= Q3 + 1.5 * IQR)]
st.success("β
Outliers removed.")
show_points = st.checkbox("Show individual points", value=True)
show_means = st.checkbox("Show group means", value=False)
with col2:
custom_y_label = st.text_input("Y-axis Label", value=y_axis)
flip_y = st.checkbox("Flip Y-axis (most negative on top)?")
show_violin = st.checkbox("Overlay violin plot", value=False)
transform = st.selectbox("Transform Y-axis?", ["None", "log10", "sqrt"])
plot_width = st.slider("Plot width", 6, 20, 10)
plot_height = st.slider("Plot height", 4, 12, 6)
if transform == "log10":
df_all[y_axis] = df_all[y_axis].apply(lambda x: np.log10(x) if x > 0 else np.nan)
df_all = df_all.dropna()
st.warning("β οΈ Non-positive values removed for log10.")
elif transform == "sqrt":
df_all[y_axis] = df_all[y_axis].apply(lambda x: np.sqrt(x) if x >= 0 else np.nan)
df_all = df_all.dropna()
st.warning("β οΈ Negative values removed for sqrt.")
fig, ax = plt.subplots(figsize=(plot_width, plot_height))
palette = sns.color_palette("Set2")
if show_violin:
sns.violinplot(x=x_axis, y=y_axis, data=df_all, inner=None, palette=palette, ax=ax, linewidth=0.5, alpha=0.3)
sns.boxplot(x=x_axis, y=y_axis, data=df_all, palette=palette, ax=ax, width=0.3)
if show_points:
sns.stripplot(x=x_axis, y=y_axis, data=df_all, color="black", alpha=0.4, jitter=True, ax=ax)
if show_means:
means = df_all.groupby(x_axis)[y_axis].mean().reset_index()
sns.pointplot(data=means, x=x_axis, y=y_axis, color="red", markers="D", linestyles="--", ax=ax)
if show_sample_sizes:
for i, group in enumerate(df_all[x_axis].unique()):
count = df_all[df_all[x_axis] == group].shape[0]
y_min = df_all[y_axis].min()
ax.text(i, y_min - 0.05 * abs(y_min), f"n={count}", ha='center', va='top', fontsize=9)
ax.set_title(plot_title)
ax.set_xlabel(custom_x_label)
ax.set_ylabel(custom_y_label)
if flip_y:
ax.invert_yaxis()
left, center, right = st.columns([1, 6, 1]) # adjust width ratios as needed
with center:
st.pyplot(fig)
buf = io.BytesIO()
fig.savefig(buf, format="png")
st.download_button("π₯ Download Plot as PNG", data=buf.getvalue(), file_name="boxplot.png", mime="image/png")
st.markdown("### π§ͺ Run ANOVA + Tukey Post Hoc Test")
if st.checkbox("Run Statistical Analysis"):
try:
model = ols(f'{y_axis} ~ C({x_axis})', data=df_all).fit()
anova_table = sm.stats.anova_lm(model, typ=2)
st.markdown("#### ANOVA Table")
st.dataframe(anova_table)
st.download_button("π₯ Download ANOVA Table", data=anova_table.to_csv().encode(), file_name="anova_results.csv", mime="text/csv")
pval = anova_table["PR(>F)"].iloc[0]
if pval < 0.05:
st.success(f"β
ANOVA significant (p = {pval:.4f}) β Running Tukey HSD...")
tukey = pairwise_tukeyhsd(endog=df_all[y_axis], groups=df_all[x_axis], alpha=0.05)
tukey_data = tukey.summary().data[1:]
tukey_columns = tukey.summary().data[0]
tukey_df = pd.DataFrame(tukey_data, columns=tukey_columns)
#tukey_df["Reject Null?"] = tukey_df["p-adj"].apply(lambda p: "Yes" if float(p) < 0.05 else "No")
st.markdown("#### Tukey HSD Results")
st.dataframe(tukey_df)
st.download_button("π₯ Download Tukey HSD Results", data=tukey_df.to_csv(index=False).encode(), file_name="tukey_results.csv", mime="text/csv")
st.markdown("### β Interpretation of Significant Comparisons")
group_means = df_all.groupby(x_axis)[y_axis].mean().to_dict()
for _, row in tukey_df.iterrows():
g1, g2 = row["group1"], row["group2"]
p_adj = float(row["p-adj"])
if p_adj < 0.05:
m1, m2 = group_means.get(g1), group_means.get(g2)
if m1 is None or m2 is None or m2 == 0:
continue
direction = "higher" if m1 > m2 else "lower"
fold_change = abs(m1 / m2)
diff = abs(m1 - m2)
if fold_change >= 1.1:
st.markdown(f"- **{g1}** is **{fold_change:.2f}-fold {direction}** than **{g2}** (Ξ = {diff:.2f}), p = {p_adj:.4f}")
else:
pct_diff = abs((m1 - m2) / m2 * 100)
st.markdown(f"- **{g1}** is **{pct_diff:.1f}% {direction}** than **{g2}** (Ξ = {diff:.2f}), p = {p_adj:.4f}")
else:
st.info(f"ANOVA not significant (p = {pval:.4f}); Tukey not performed.")
except Exception as e:
st.error(f"β Statistical analysis failed: {e}")
else:
with col1:
st.info("Upload your CSV or Excel file(s) to get started.")