Spaces:
No application file
No application file
File size: 2,992 Bytes
73c7123 |
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 |
import os
import pandas as pd
import streamlit as st
# Import modules
from scripts.featureEngineering import add_flight_day
from scripts.stats import tidy_from_wide, analyze_r1_vs_L
from scripts.graphMaking import make_figure
# Load Data
def list_final_data(folder="final_data"):
"""Return list of CSV files in final_data folder."""
return [f for f in os.listdir(folder) if f.endswith(".csv")]
def load_final_data(fname, folder="final_data"):
"""Load selected CSV file as pandas DataFrame."""
path = os.path.join(folder, fname)
return pd.read_csv(path)
# Main App
def main():
st.title("Astronaut Biochemistry Dashboard")
# 1. Sidebar file selection
st.sidebar.header("Data Selection")
csv_files = list_final_data()
if not csv_files:
st.error("No CSV files found in final_data/")
return
selected_file = st.sidebar.selectbox("Choose dataset", csv_files)
df_raw = load_final_data(selected_file)
st.write(f"Loaded file: **{selected_file}**")
# 2. Clean with feature engineering
df_clean = add_flight_day(df_raw)
# 3. Transform to tidy format + run stats
tidy_df = tidy_from_wide(df_clean)
stats_df = analyze_r1_vs_L(tidy_df)
# 4. Sidebar user selections
st.sidebar.header("Plot Controls")
analyte = st.sidebar.selectbox(
"Select Analyte",
options=tidy_df["analyte"].unique().tolist(),
index=tidy_df["analyte"].unique().tolist().index("sodium")
if "sodium" in tidy_df["analyte"].unique() else 0
)
astronauts = st.sidebar.multiselect(
"Select Astronauts",
# Normalize IDs to uppercase for consistency
options=sorted([a.upper() for a in tidy_df["astronautID"].unique().tolist()]),
default=[]
)
astronauts = [a.upper() for a in astronauts]
sex_filter = st.sidebar.radio(
"Sex Filter",
["All", "Male", "Female"],
index=0
)
show_error = st.sidebar.radio(
"Error Band",
["None", "within", "group"],
index=0
)
show_error = None if show_error == "None" else show_error
# Unify filters: Astronauts take priority, else fall back to sex filter
if astronauts:
astronaut_filter = astronauts
elif sex_filter != "All":
astronaut_filter = sex_filter
else:
astronaut_filter = None
# 5. Generate figure
if analyte:
fig = make_figure(
tidy_df=tidy_df,
stats_df=stats_df,
analytes=[analyte],
astronaut_filter=astronaut_filter,
show_error=show_error
)
st.plotly_chart(fig, use_container_width=True)
else:
st.warning("Please select at least one analyte to plot.")
# 6. Optional: preview data
with st.expander("Preview Data"):
st.dataframe(tidy_df.head(20))
if __name__ == "__main__":
main() |