Spaces:
Sleeping
Sleeping
Upload streamlit_app.py
#1
by
DTanzillo
- opened
- streamlit_app.py +100 -0
streamlit_app.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import streamlit as st
|
| 4 |
+
|
| 5 |
+
# Import modules
|
| 6 |
+
from scripts.featureEngineering import add_flight_day
|
| 7 |
+
from scripts.stats import tidy_from_wide, analyze_r1_vs_L
|
| 8 |
+
from scripts.graphMaking import make_figure
|
| 9 |
+
|
| 10 |
+
# Load Data
|
| 11 |
+
def list_final_data(folder="final_data"):
|
| 12 |
+
"""Return list of CSV files in final_data folder."""
|
| 13 |
+
return [f for f in os.listdir(folder) if f.endswith(".csv")]
|
| 14 |
+
|
| 15 |
+
def load_final_data(fname, folder="final_data"):
|
| 16 |
+
"""Load selected CSV file as pandas DataFrame."""
|
| 17 |
+
path = os.path.join(folder, fname)
|
| 18 |
+
return pd.read_csv(path)
|
| 19 |
+
|
| 20 |
+
# Main App
|
| 21 |
+
def main():
|
| 22 |
+
st.title("Astronaut Biochemistry Dashboard")
|
| 23 |
+
|
| 24 |
+
# 1. Sidebar file selection
|
| 25 |
+
st.sidebar.header("Data Selection")
|
| 26 |
+
csv_files = list_final_data()
|
| 27 |
+
|
| 28 |
+
if not csv_files:
|
| 29 |
+
st.error("No CSV files found in final_data/")
|
| 30 |
+
return
|
| 31 |
+
|
| 32 |
+
selected_file = st.sidebar.selectbox("Choose dataset", csv_files)
|
| 33 |
+
df_raw = load_final_data(selected_file)
|
| 34 |
+
st.write(f"Loaded file: **{selected_file}**")
|
| 35 |
+
|
| 36 |
+
# 2. Clean with feature engineering
|
| 37 |
+
df_clean = add_flight_day(df_raw)
|
| 38 |
+
|
| 39 |
+
# 3. Transform to tidy format + run stats
|
| 40 |
+
tidy_df = tidy_from_wide(df_clean)
|
| 41 |
+
stats_df = analyze_r1_vs_L(tidy_df)
|
| 42 |
+
|
| 43 |
+
# 4. Sidebar user selections
|
| 44 |
+
st.sidebar.header("Plot Controls")
|
| 45 |
+
|
| 46 |
+
analyte = st.sidebar.selectbox(
|
| 47 |
+
"Select Analyte",
|
| 48 |
+
options=tidy_df["analyte"].unique().tolist(),
|
| 49 |
+
index=tidy_df["analyte"].unique().tolist().index("sodium")
|
| 50 |
+
if "sodium" in tidy_df["analyte"].unique() else 0
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
astronauts = st.sidebar.multiselect(
|
| 54 |
+
"Select Astronauts",
|
| 55 |
+
# Normalize IDs to uppercase for consistency
|
| 56 |
+
options=sorted([a.upper() for a in tidy_df["astronautID"].unique().tolist()]),
|
| 57 |
+
default=[]
|
| 58 |
+
)
|
| 59 |
+
astronauts = [a.upper() for a in astronauts]
|
| 60 |
+
|
| 61 |
+
sex_filter = st.sidebar.radio(
|
| 62 |
+
"Sex Filter",
|
| 63 |
+
["All", "Male", "Female"],
|
| 64 |
+
index=0
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
show_error = st.sidebar.radio(
|
| 68 |
+
"Error Band",
|
| 69 |
+
["None", "within", "group"],
|
| 70 |
+
index=0
|
| 71 |
+
)
|
| 72 |
+
show_error = None if show_error == "None" else show_error
|
| 73 |
+
|
| 74 |
+
# Unify filters: Astronauts take priority, else fall back to sex filter
|
| 75 |
+
if astronauts:
|
| 76 |
+
astronaut_filter = astronauts
|
| 77 |
+
elif sex_filter != "All":
|
| 78 |
+
astronaut_filter = sex_filter
|
| 79 |
+
else:
|
| 80 |
+
astronaut_filter = None
|
| 81 |
+
|
| 82 |
+
# 5. Generate figure
|
| 83 |
+
if analyte:
|
| 84 |
+
fig = make_figure(
|
| 85 |
+
tidy_df=tidy_df,
|
| 86 |
+
stats_df=stats_df,
|
| 87 |
+
analytes=[analyte],
|
| 88 |
+
astronaut_filter=astronaut_filter,
|
| 89 |
+
show_error=show_error
|
| 90 |
+
)
|
| 91 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 92 |
+
else:
|
| 93 |
+
st.warning("Please select at least one analyte to plot.")
|
| 94 |
+
|
| 95 |
+
# 6. Optional: preview data
|
| 96 |
+
with st.expander("Preview Data"):
|
| 97 |
+
st.dataframe(tidy_df.head(20))
|
| 98 |
+
|
| 99 |
+
if __name__ == "__main__":
|
| 100 |
+
main()
|