DTanzillo commited on
Commit
2e7692f
·
verified ·
1 Parent(s): 73c7123

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +97 -37
src/streamlit_app.py CHANGED
@@ -1,40 +1,100 @@
1
- import altair as alt
2
- import numpy as np
3
  import pandas as pd
4
  import streamlit as st
5
 
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()