Spaces:
No application file
No application file
:sparkles: New App From Computer
Browse filesFiles changed (7)
- app.pyAdded+100
- final_data/Metabolic_Panel.csvAdded+29
- final_data/Serum_Cardiovascular.csvAdded+29
- requirements.txt+5-3
- scripts/featureEngineering.pyAdded+63
- scripts/graphMaking.pyAdded+256
- scripts/stats.pyAdded+205
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100import 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()1234567891011121314151617181920212223242526272829Sample Name,albumin_value_gram_per_deciliter,albumin_range_min_gram_per_deciliter,albumin_range_max_gram_per_deciliter,albumin_to_globulin_ratio_value,albumin_to_globulin_ratio_range_min,albumin_to_globulin_ratio_range_max,alkaline_phosphatase_value_units_per_liter,alkaline_phosphatase_range_min_units_per_liter,alkaline_phosphatase_range_max_units_per_liter,alt_value_units_per_liter,alt_range_min_units_per_liter,alt_max_units_per_liter,ast_value_units_per_liter,ast_range_min_units_per_liter,ast_max_units_per_liter,total_bilirubin_value_milligram_per_deciliter,total_bilirubin_range_min_milligram_per_deciliter,total_bilirubin_range_max_milligram_per_deciliter,bun_to_creatinine_ratio_value,bun_to_creatinine_ratio_range_min,bun_to_creatinine_ratio_range_max,calcium_value_milligram_per_deciliter,calcium_range_min_milligram_per_deciliter,calcium_range_max_milligram_per_deciliter,carbon_dioxide_value_millimol_per_liter,carbon_dioxide_range_min_millimol_per_liter,carbon_dioxide_range_max_millimol_per_liter,chloride_value_millimol_per_liter,chloride_range_min_millimol_per_liter,chloride_range_max_millimol_per_liter,creatinine_value_milligram_per_deciliter,creatinine_range_min_milligram_per_deciliter,creatinine_range_max_milligram_per_deciliter,egfr_african_american_value_milliliter_per_minute_per_1.73_meter_squared,egfr_african_american_range_min_milliliter_per_minute_per_1.73_meter_squared,egfr_african_american_range_max_milliliter_per_minute_per_1.73_meter_squared,egfr_non_african_american_value_milliliter_per_minute_per_1.73_meter_squared,egfr_non_african_american_range_min_milliliter_per_minute_per_1.73_meter_squared,egfr_non_african_american_range_max_milliliter_per_minute_per_1.73_meter_squared,globulin_value_gram_per_deciliter,globulin_range_min_gram_per_deciliter,globulin_range_max_gram_per_deciliter,glucose_value_milligram_per_deciliter,glucose_range_min_milligram_per_deciliter,glucose_range_max_milligram_per_deciliter,potassium_value_millimol_per_liter,potassium_range_min_millimol_per_liter,potassium_range_max_millimol_per_liter,total_protein_value_gram_per_deciliter,total_protein_range_min_gram_per_deciliter,total_protein_range_max_gram_per_deciliter,sodium_value_millimol_per_liter,sodium_range_min_millimol_per_liter,sodium_range_max_millimol_per_liter,urea_nitrogen_bun_value_milligram_per_deciliter,urea_nitrogen_bun_range_min_milligram_per_deciliter,urea_nitrogen_bun_range_max_milligram_per_deciliter,astronautID,timepoint
C001_serum_L-3,4.9,3.6,5.1,2.0,1,2.5,45,36,130,13,9,46,15,10,40,1.0,0.2,1.2,15.0,6.0,22.0,9.6,8.6,10.3,26,20,32,102,98,110,1.37,0.6,1.35,75,60,,65,60,,2.5,1.9,3.7,69,65,99,3.9,3.5,5.3,7.4,6.1,8.1,142,135,146,20,7,25,C001,L-3
C001_serum_L-44,5.0,3.6,5.1,1.9,1,2.5,46,36,130,16,9,46,18,10,40,0.9,0.2,1.2,,6.0,22.0,10.0,8.6,10.3,26,20,32,101,98,110,1.1,0.6,1.35,98,60,,85,60,,2.7,1.9,3.7,80,65,99,4.2,3.5,5.3,7.7,6.1,8.1,139,135,146,19,7,25,C001,L-44
C001_serum_L-92,5.0,3.6,5.1,2.2,1,2.5,51,36,130,15,9,46,15,10,40,0.7,0.2,1.2,,6.0,22.0,9.7,8.6,10.3,23,20,32,104,98,110,1.24,0.6,1.35,85,60,,73,60,,2.3,1.9,3.7,83,65,99,4.6,3.5,5.3,7.3,6.1,8.1,141,135,146,25,7,25,C001,L-92
C001_serum_R+1,4.9,3.6,5.1,1.9,1,2.5,52,36,130,9,9,46,16,10,40,1.2,0.2,1.2,,6.0,22.0,9.7,8.6,10.3,24,20,32,102,98,110,1.12,0.6,1.35,96,60,,83,60,,2.6,1.9,3.7,90,65,99,4.4,3.5,5.3,7.5,6.1,8.1,138,135,146,18,7,25,C001,R+1
C001_serum_R+194,4.6,3.6,5.1,1.9,1,2.5,50,36,130,15,9,46,12,10,40,0.8,0.2,1.2,22.0,6.0,22.0,9.4,8.6,10.3,23,20,32,102,98,110,1.29,0.6,1.35,80,60,,69,60,,2.4,1.9,3.7,87,65,99,4.3,3.5,5.3,7.0,6.1,8.1,137,135,146,28,7,25,C001,R+194
C001_serum_R+45,4.9,3.6,5.1,2.1,1,2.5,46,36,130,11,9,46,13,10,40,,0.2,1.2,,6.0,22.0,9.6,8.6,10.3,28,20,32,100,98,110,1.11,0.6,1.35,97,60,,84,60,,2.3,1.9,3.7,84,65,99,4.0,3.5,5.3,7.2,6.1,8.1,138,135,146,15,7,25,C001,R+45
C001_serum_R+82,5.1,3.6,5.1,1.9,1,2.5,49,36,130,15,9,46,16,10,40,1.2,0.2,1.2,,6.0,22.0,10.1,8.6,10.3,27,20,32,101,98,110,1.25,0.6,1.35,84,60,,73,60,,2.7,1.9,3.7,91,65,99,4.3,3.5,5.3,7.8,6.1,8.1,140,135,146,22,7,25,C001,R+82
C002_serum_L-3,4.3,3.6,5.1,1.6,1,2.5,68,37,153,19,6,29,40,10,35,0.3,0.2,1.2,,6.0,22.0,9.3,8.6,10.4,24,20,32,106,98,110,0.96,0.5,1.05,79,60,,68,60,,2.7,1.9,3.7,78,65,99,3.8,3.5,5.3,7.0,6.1,8.1,143,135,146,17,7,25,C002,L-3
C002_serum_L-44,4.6,3.6,5.1,1.5,1,2.5,78,37,153,24,6,29,31,10,35,0.4,0.2,1.2,,6.0,22.0,9.7,8.6,10.4,26,20,32,103,98,110,0.88,0.5,1.05,88,60,,76,60,,3.0,1.9,3.7,72,65,99,4.0,3.5,5.3,7.6,6.1,8.1,141,135,146,16,7,25,C002,L-44
C002_serum_L-92,4.9,3.6,5.1,1.6,1,2.5,73,37,153,16,6,29,19,10,35,0.5,0.2,1.2,,6.0,22.0,10.2,8.6,10.4,23,20,32,103,98,110,0.83,0.5,1.05,95,60,,82,60,,3.0,1.9,3.7,69,65,99,3.9,3.5,5.3,7.9,6.1,8.1,142,135,146,14,7,25,C002,L-92
C002_serum_R+1,4.4,3.6,5.1,1.5,1,2.5,71,37,153,16,6,29,23,10,35,0.4,0.2,1.2,,6.0,22.0,9.2,8.6,10.4,22,20,32,103,98,110,0.95,0.5,1.05,80,60,,69,60,,3.0,1.9,3.7,83,65,99,3.5,3.5,5.3,7.4,6.1,8.1,137,135,146,20,7,25,C002,R+1
C002_serum_R+194,4.3,3.6,5.1,1.4,1,2.5,66,37,153,17,6,29,24,10,35,0.3,0.2,1.2,,6.0,22.0,9.5,8.6,10.4,20,20,32,104,98,110,1.02,0.5,1.05,73,60,,63,60,,3.0,1.9,3.7,84,65,99,4.4,3.5,5.3,7.3,6.1,8.1,138,135,146,18,7,25,C002,R+194
C002_serum_R+45,4.2,3.6,5.1,1.6,1,2.5,65,37,153,14,6,29,17,10,35,0.5,0.2,1.2,,6.0,22.0,9.3,8.6,10.4,21,20,32,104,98,110,1.02,0.5,1.05,74,60,,64,60,,2.6,1.9,3.7,44,65,99,4.1,3.5,5.3,6.8,6.1,8.1,140,135,146,19,7,25,C002,R+45
C002_serum_R+82,4.5,3.6,5.1,1.6,1,2.5,65,37,153,17,6,29,21,10,35,0.5,0.2,1.2,,,,9.7,8.6,10.4,25,20,32,105,98,110,0.82,0.5,1.05,96,60,,83,60,,2.8,1.9,3.7,77,65,99,3.7,3.5,5.3,7.3,6.1,8.1,141,135,146,16,7,25,C002,R+82
C003_serum_L-3,4.2,3.6,5.1,1.6,1,2.5,31,36,130,13,9,46,13,10,40,0.2,0.2,1.2,24.0,6.0,22.0,9.5,8.6,10.3,23,20,32,101,98,110,1.08,0.6,1.35,80,60,,69,60,,2.6,1.9,3.7,75,65,99,3.3,3.5,5.3,6.8,6.1,8.1,138,135,146,26,7,25,C003,L-3
C003_serum_L-44,4.9,3.6,5.1,1.3,1,2.5,41,36,130,13,9,46,17,10,40,0.4,0.2,1.2,,6.0,22.0,10.3,8.6,10.3,21,20,32,99,98,110,1.03,0.6,1.35,85,60,,73,60,,3.9,1.9,3.7,62,65,99,3.5,3.5,5.3,8.8,6.1,8.1,139,135,146,19,7,25,C003,L-44
C003_serum_L-92,4.4,3.6,5.1,1.6,1,2.5,33,36,130,12,9,46,15,10,40,0.3,0.2,1.2,,6.0,22.0,9.3,8.6,10.3,20,20,32,102,98,110,1.0,0.6,1.35,88,60,,76,60,,2.7,1.9,3.7,77,65,99,3.7,3.5,5.3,7.1,6.1,8.1,139,135,146,17,7,25,C003,L-92
C003_serum_R+1,4.2,3.6,5.1,1.4,1,2.5,41,31,125,19,6,29,18,10,30,0.3,0.2,1.2,,6.0,22.0,9.4,8.6,10.2,20,20,32,103,98,110,0.89,0.5,1.1,102,60,,88,60,,2.9,1.9,3.7,103,65,99,3.0,3.5,5.3,7.1,6.1,8.1,137,135,146,21,7,25,C003,R+1
C003_serum_R+194,4.4,3.6,5.1,1.5,1,2.5,43,31,125,10,6,29,15,10,30,0.3,0.2,1.2,25.0,6.0,22.0,9.6,8.6,10.2,22,20,32,101,98,110,1.1,0.5,1.1,78,60,,67,60,,2.9,1.9,3.7,73,65,99,3.8,3.5,5.3,7.3,6.1,8.1,137,135,146,28,7,25,C003,R+194
C003_serum_R+45,4.5,3.6,5.1,1.6,1,2.5,51,31,125,17,6,29,19,10,30,0.7,0.2,1.2,,6.0,22.0,9.6,8.6,10.2,23,20,32,97,98,110,0.94,0.5,1.1,95,60,,82,60,,2.9,1.9,3.7,36,65,99,4.4,3.5,5.3,7.4,6.1,8.1,136,135,146,19,7,25,C003,R+45
C003_serum_R+82,4.8,3.6,5.1,1.8,1,2.5,50,31,125,12,6,29,15,10,30,0.5,0.2,1.2,,6.0,22.0,10.0,8.6,10.2,23,20,32,102,98,110,0.89,0.5,1.1,101,60,,87,60,,2.7,1.9,3.7,80,65,99,3.7,3.5,5.3,7.5,6.1,8.1,138,135,146,23,7,25,C003,R+82
C004_serum_L-3,4.6,3.6,5.1,1.7,1,2.5,55,36,130,37,9,46,39,10,40,0.5,0.2,1.2,,6.0,22.0,10.2,8.6,10.3,24,20,32,103,98,110,1.13,0.6,1.35,92,60,,80,60,,2.7,1.9,3.7,56,65,99,3.7,3.5,5.3,7.3,6.1,8.1,141,135,146,19,7,25,C004,L-3
C004_serum_L-44,4.4,3.6,5.1,1.5,1,2.5,56,36,130,35,9,46,44,10,40,0.7,0.2,1.2,,6.0,22.0,10.1,8.6,10.3,24,20,32,103,98,110,1.08,0.6,1.35,98,60,,85,60,,2.9,1.9,3.7,83,65,99,4.2,3.5,5.3,7.3,6.1,8.1,140,135,146,19,7,25,C004,L-44
C004_serum_L-92,4.6,3.6,5.1,1.6,1,2.5,58,36,130,37,9,46,36,10,40,0.7,0.2,1.2,,6.0,22.0,10.0,8.6,10.3,23,20,32,102,98,110,1.14,0.6,1.35,92,60,,79,60,,2.8,1.9,3.7,100,65,99,4.2,3.5,5.3,7.4,6.1,8.1,138,135,146,22,7,25,C004,L-92
C004_serum_R+1,4.5,3.6,5.1,1.6,1,2.5,62,36,130,40,9,46,48,10,40,0.4,0.2,1.2,23.0,6.0,22.0,9.8,8.6,10.3,23,20,32,105,98,110,1.15,0.6,1.35,90,60,,78,60,,2.9,1.9,3.7,97,65,99,4.0,3.5,5.3,7.4,6.1,8.1,140,135,146,26,7,25,C004,R+1
C004_serum_R+194,4.3,3.6,5.1,1.5,1,2.5,57,36,130,43,9,46,38,10,40,0.5,0.2,1.2,21.0,6.0,22.0,9.8,8.6,10.3,19,20,32,105,98,110,1.31,0.6,1.35,77,60,,67,60,,2.9,1.9,3.7,84,65,99,3.8,3.5,5.3,7.2,6.1,8.1,141,135,146,27,7,25,C004,R+194
C004_serum_R+45,4.5,3.6,5.1,1.6,1,2.5,60,36,130,32,9,46,33,10,40,0.7,0.2,1.2,,6.0,22.0,9.8,8.6,10.3,24,20,32,102,98,110,1.08,0.6,1.35,98,60,,84,60,,2.8,1.9,3.7,43,65,99,5.9,3.5,5.3,7.3,6.1,8.1,139,135,146,21,7,25,C004,R+45
C004_serum_R+82,4.6,3.6,5.1,1.6,1,2.5,54,36,130,23,9,46,25,10,40,1.0,0.2,1.2,,6.0,22.0,10.0,8.6,10.3,24,20,32,103,98,110,1.11,0.6,1.35,94,60,,81,60,,2.8,1.9,3.7,80,65,99,4.2,3.5,5.3,7.4,6.1,8.1,140,135,146,16,7,25,C004,R+821234567891011121314151617181920212223242526272829Sample Name,a2_macroglobulin_concentration_nanogram_per_milliliter,a2_macroglobulin_percent,agp_concentration_nanogram_per_milliliter,agp_percent,crp_concentration_picogram_per_milliliter,crp_percent,fetuin_a36_concentration_nanogram_per_milliliter,fetuin_a36_percent,fibrinogen_concentration_nanogram_per_milliliter,fibrinogen_percent,haptoglobin_concentration_nanogram_per_milliliter,haptoglobin_percent,l_selectin_concentration_picogram_per_milliliter,l_selectin_percent,pf4_concentration_nanogram_per_milliliter,pf4_percent,sap_concentration_picogram_per_milliliter,sap_percent,astronautID,timepoint
C001_serum_L-3,1002300.0,68.70291244,1214100.0,56.50502833,5131700,28.97219478,155302.07,63.4017261,2780.38,65.8613441,1305900.0,22.43495888,561452.93,57.94458721,8602.35,74.65951867,6057300,66.13248223,C001,L-3
C001_serum_L-44,657943.56,45.0989113,1745700.0,81.24604889,2191000,12.36979534,194978.74,79.59963875,5707.32,135.1943858,122441.48,2.10350683,675114.37,69.67498324,3903.78,33.88078092,5428000,59.2619011,C001,L-44
C001_serum_L-92,903742.69,61.94727616,1048700.0,48.80720139,18385000,103.7967537,144424.41,58.9609455,3208.77,76.00900061,1049100.0,18.02321415,515957.2,53.24921356,6973.32,60.52121975,4760000,51.96880052,C001,L-92
C001_serum_R+1,1048700.0,71.88341243,1034400.0,48.1416698,10637600,60.05702188,163848.05,66.8906035,2604.7,61.69985505,853104.4,14.65607024,596429.89,61.55437425,8143.69,70.6788233,6183500,67.51031051,C001,R+1
C001_serum_R+194,1651200.0,113.1819306,2595800.0,120.8102731,15415000,87.02893437,273640.25,111.713026,4587.13,108.6594449,55113000.0,946.824327,762532.23,78.6969182,11371.31,98.69123335,7739400,84.49733924,C001,R+194
C001_serum_R+45,1130800.0,77.51097814,1285200.0,59.81407002,6713900,37.90486944,159096.21,64.95067535,2732.11,64.7179295,1864800.0,32.03668835,568449.05,58.66661975,6374.28,55.32217088,6670900,72.83165366,C001,R+45
C001_serum_R+82,2993600.0,205.1970854,2455500.0,114.280617,25030000,141.3126323,300149.84,122.5355074,2513.02,59.52814901,13593000.0,233.5235439,1266600.0,130.7190866,23891.75,207.355729,13775000,150.3929049,C001,R+82
C002_serum_L-3,1892200.0,129.7013378,3653700.0,170.0456486,11967000,67.56245589,294376.04,120.1783663,6533.18,154.7572692,3825900.0,65.72778097,1336800.0,137.9640573,15879.14,137.8145448,11733000,128.0987262,C002,L-3
C002_serum_L-44,734243.34,50.32889943,1963300.0,91.37329884,1568500,8.855328158,190495.51,77.76937003,4027.43,95.40133114,80330.74,1.380057316,893494.17,92.21280733,3335.42,28.94800279,4509000,49.2284289,C002,L-44
C002_serum_L-92,2036900.0,139.6198367,2932000.0,136.4572466,8927100,50.4,276877.57,113.0346547,3657.0,86.62662492,3204700.0,55.05575673,1285900.0,132.7109375,13652.06,118.4857892,10983000,119.9103647,C002,L-92
C002_serum_R+1,913857.15,62.64057444,942754.34,43.87641932,9311500,52.57021877,125785.45,51.35163136,1999.88,47.37294357,568223.15,9.76189831,629653.79,64.98323724,5951.22,51.65044676,5592700,61.06006527,C002,R+1
C002_serum_R+194,2342900.0,160.5946858,2220700.0,103.3528675,21462000,121.1686662,241485.24,98.58581436,2852.5,67.56971495,6036000.0,103.6966167,1077400.0,111.1927553,21024.9,182.4744301,11337000,123.7752713,C002,R+194
C002_serum_R+45,1820800.0,124.8072064,2479200.0,115.3836309,3677200,20.76047989,270655.44,110.4944839,4074.55,96.51750466,3442000.0,59.13249748,1247300.0,128.7272357,14071.86,122.1292198,10765000,117.530281,C002,R+45
C002_serum_R+82,1403700.0,96.21697914,2968900.0,138.1745973,8633700,48.7435427,253558.23,103.5145858,3279.23,77.67804955,17907000.0,307.6367322,1114100.0,114.9803682,12168.67,105.6114951,10210000,111.4708936,C002,R+82
C003_serum_L-3,2948400.0,202.0988397,1616800.0,75.24695643,42838000,241.8517996,373933.42,152.6574905,4609.49,109.1891062,6263200.0,107.5998426,1128100.0,116.4252341,26633.16,231.1483381,18242000,199.1627855,C003,L-3
C003_serum_L-44,531433.88,36.4272726,1068100.0,49.71009041,9383100,52.97445307,188579.51,76.98716727,3723.84,88.20992369,203473.61,3.495613809,847821.61,87.49918398,2371.07,20.57844019,5319800,58.08059349,C003,L-44
C003_serum_L-92,3030400.0,207.7195509,1850900.0,86.14212745,75749000,427.6584333,357755.04,146.0527027,4153.93,98.39784961,17907000.0,307.6367322,1152700.0,118.96407,21999.42,190.9322578,16583000,181.0501301,C003,L-92
C003_serum_R+1,3051700.0,209.1795649,1559000.0,72.55690567,83617000,472.0790402,334021.3,136.3634559,4531.17,107.3338704,5442300.0,93.497034,1056400.0,109.0254564,28157.23,244.3756926,16745000,182.8188161,C003,R+1
C003_serum_R+194,2025600.0,138.8452753,2912600.0,135.5543576,9379400,52.95356387,295881.1,120.7928037,4334.63,102.6782519,2571500.0,44.1775762,1398200.0,144.3008265,18173.95,157.7311269,12038000,131.4286598,C003,R+194
C003_serum_R+45,2895000.0,198.4385229,3046500.0,141.7861534,39190000,221.256175,271898.71,111.0020461,4491.95,106.4048312,14476000.0,248.6932114,1317500.0,135.9722063,25381.08,220.2815761,13993000,152.7729886,C003,R+45
C003_serum_R+82,2127300.0,145.8163281,3249500.0,151.2339095,12680000,71.58786168,316530.95,129.2230593,4447.08,105.3419555,11995000.0,206.0703972,1058100.0,109.2009044,21375.52,185.5174498,13595000,148.4276981,C003,R+82
C004_serum_L-3,1780800.0,122.0653961,3606700.0,167.8582371,13742000,77.58362738,329448.71,134.4967062,5428.66,128.5935175,17907000.0,307.6367322,1364800.0,140.8537892,19678.31,170.7874189,12111000,132.2256603,C004,L-3
C004_serum_L-44,634017.94,43.45892349,2165100.0,100.7652062,1289600,7.280733945,180155.97,73.54827572,3602.38,85.3327922,73870.48,1.269072043,789635.95,81.49414978,2156.35,18.71489222,4048000,44.19531608,C004,L-44
C004_serum_L-92,1354300.0,92.83084338,2918800.0,135.8429097,21378000,120.6944248,253064.34,103.3129563,3226.41,76.42685504,17907000.0,307.6367322,1075600.0,111.0069868,13080.91,113.5287967,10138000,110.6848109,C004,L-92
C004_serum_R+1,1651200.0,113.1819306,3338700.0,155.3853374,17701000,99.9350741,280617.78,114.5615871,4187.89,99.20229046,17907000.0,307.6367322,1119400.0,115.5273532,14317.3,124.2593857,12329000,134.605744,C004,R+1
C004_serum_R+194,1568600.0,107.5200922,4564200.0,212.4209293,46006000,259.7374735,285112.88,116.3967018,4390.9,104.0111696,17907000.0,307.6367322,1250800.0,129.0884521,15624.9,135.608004,13414000,146.4515736,C004,R+194
C004_serum_R+45,1526000.0,104.6000642,3484600.0,162.1756212,22407000,126.5038814,287113.08,117.213279,2840.5,67.28546023,17907000.0,307.6367322,1256100.0,129.6354371,15095.42,131.0126641,11850000,129.3761105,C004,R+45
C004_serum_R+82,1785900.0,122.4149769,2651400.0,123.3979344,7902300,44.61425547,245913.55,100.3936621,3553.01,84.16332092,2919100.0,50.14923689,1193000.0,123.1232199,13342.85,115.8021655,9482000,103.5227241,C004,R+82123altairpandasstreamlit12345numpy==2.3.3pandas==2.3.2plotly==6.3.0scipy==1.16.2streamlit==1.50.0
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263import numpy as np
import pandas as pd
def parse_timepoint(timepoint: str) -> int:
"""
Convert timepoint strings like 'L-3', 'L0', 'R+0', 'R+1' into numeric flight days
on a stretched scale.
In particular, we are converting the 3 dats of flight into 30 days so there is a
difference, the final chart will have fake data in it.
Convention:
L-0 -> 0 (launch day = Flight Day 0)
L-3 -> -3 (3 days before launch)
R+0 -> 30 (last day in space, stretched to day 30)
R+1 -> 31 (first recovery day)
R+N -> N+30 (general rule for post-launch days)
"""
label = str(timepoint).strip().upper()
if label.startswith("L"): # Pre-launch
number = int(label.replace("L", "").replace("+", "").replace("-", "") or "0")
return -number
elif label.startswith("R"): # Return / post-flight
number = int(label.replace("R", "").replace("+", "").replace("-", "") or "0")
return number + 30
return np.nan
def add_flight_day(df: pd.DataFrame) -> pd.DataFrame:
"""
Add a 'flight_day' column to a dataframe that already has 'timepoint' and 'astronautID'.
Drops 'Sample Name' if present, since it's redundant.
"""
df = df.copy()
if "timepoint" not in df.columns:
raise ValueError("DataFrame must contain a 'timepoint' column")
# create numeric scale
df["flight_day"] = df["timepoint"].apply(parse_timepoint)
# drop redundant 'Sample Name' if it exists
if "Sample Name" in df.columns:
df = df.drop(columns=["Sample Name"])
return df
def add_derived_features(df: pd.DataFrame) -> pd.DataFrame:
"""
Add derived feature: Anion Gap.
Anion Gap = Sodium − Chloride − Carbon Dioxide
"""
df = df.copy()
if all(c in df.columns for c in ["sodium_value", "chloride_value", "carbon_dioxide_value"]):
df["anion_gap_value"] = (
df["sodium_value"].astype(float)
- df["chloride_value"].astype(float)
- df["carbon_dioxide_value"].astype(float)
)
# Placeholders; min/max defined manually in stats.ANALYTE_INFO
df["anion_gap_range_min"] = np.nan
df["anion_gap_range_max"] = np.nan
return df123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256import plotly.graph_objects as go
import plotly.express as px
import numpy as np
import pandas as pd
def make_figure(
tidy_df: pd.DataFrame,
stats_df: pd.DataFrame,
analytes: list,
astronaut_filter=None,
show_error: str = None
):
"""
Build interactive mission-day plots with stats overlays.
"""
fig = go.Figure()
# Highlight stretched space interval (0 to 30 days)
fig.add_vrect(x0=0, x1=30, fillcolor="LightGray", opacity=0.3,
layer="below", line_width=0)
for day in [10, 20]:
fig.add_vline(x=day, line=dict(color="white", width=2, dash="dot"),
layer="below")
df = tidy_df.copy()
# Apply participant filter
if astronaut_filter is None:
pass # show all
elif isinstance(astronaut_filter, str) and astronaut_filter in ["Male", "Female"]:
if "sex" in df.columns:
df = df[df["sex"] == astronaut_filter]
elif isinstance(astronaut_filter, (list, tuple, set)):
df = df[df["astronautID"].isin(astronaut_filter)]
# Loop analytes requested
for analyte in analytes:
subdf = df[df["analyte"] == analyte]
if subdf.empty:
print(f"[make_figure] Skipping {analyte} – no data")
continue
## Y-axis scaling
ref_min = subdf["min"].dropna().min()
ref_max = subdf["max"].dropna().max()
data_min = subdf["value"].min()
data_max = subdf["value"].max()
if "unit" in subdf.columns and not subdf["unit"].dropna().empty:
unit = subdf["unit"].dropna().iloc[0]
y_label = f"{analyte.title()} ({unit})"
else:
y_label = analyte.title()
## Add healthy range lines from min / max
if pd.notna(ref_min):
fig.add_hline(
y=ref_min,
line=dict(color="green", width=2, dash="dot"),
annotation_text="Min",
annotation_position="bottom right"
)
if pd.notna(ref_max):
fig.add_hline(
y=ref_max,
line=dict(color="green", width=2, dash="dot"),
annotation_text="Max",
annotation_position="top right"
)
## Decide axis limits: must include BOTH healthy range and all data
low_candidates = [v for v in [ref_min, data_min] if pd.notna(v)]
high_candidates = [v for v in [ref_max, data_max] if pd.notna(v)]
if low_candidates and high_candidates:
low = min(low_candidates)
high = max(high_candidates)
span = high - low if high > low else 1
padding = 0.1 * span
y_range = [low - padding, high + padding]
else:
y_range = None
## Apply axis update once
if y_range:
fig.update_yaxes(title=y_label, range=y_range)
else:
fig.update_yaxes(title=y_label)
## Plot each astronaut trace - first colors
palette = px.colors.qualitative.Set2
astronaut_colors = {astr: palette[i % len(palette)]
for i, astr in enumerate(subdf["astronautID"].unique())}
## Plot each astronaut trace
for astronaut, adf in subdf.groupby("astronautID"):
if adf.empty:
continue
adf = adf.sort_values("flight_day")
base_color = astronaut_colors[astronaut]
### Skip if astronaut not in filter
if isinstance(astronaut_filter, (list, tuple, set)) and astronaut not in astronaut_filter:
continue
# Main Scatter Plot
fig.add_trace(go.Scatter(
x=adf["flight_day"],
y=adf["value"],
mode="lines+markers",
name=f"{astronaut} ({analyte})",
hovertext=adf["timepoint"],
hovertemplate="Day %{hovertext}<br>Value %{y}<extra></extra>",
line=dict(color=base_color),
marker=dict(color=base_color)
))
### Within-astronaut error band
if show_error == "within" and not stats_df.empty:
stat_rows = stats_df[
(stats_df["analyte"] == analyte)
& (stats_df["test_type"] == "within")
]
for _, row in stat_rows.iterrows():
astronaut = row["astronautID"]
if astronaut not in subdf["astronautID"].unique():
continue # skip astronauts not in this analyte subset
mean_L = row.get("mean_L", np.nan)
se = row.get("se_L", np.nan)
R1 = row.get("R1", np.nan)
if pd.isna(mean_L) or pd.isna(se):
continue
base_color = astronaut_colors.get(astronaut, "gray")
if base_color.startswith("rgb"):
fill_color = base_color.replace("rgb", "rgba").replace(")", ",0.15)")
else:
fill_color = base_color
#### Horizontal band: L +/- SE
fig.add_hrect(
y0=mean_L - se, y1=mean_L + se,
fillcolor=fill_color,
opacity=0.2,
line_width=0,
layer="below"
)
#### Asterisk if R+1 outside band
if pd.notna(R1) and (R1 < mean_L - se or R1 > mean_L + se):
fig.add_annotation(
x=31,
y=R1,
text="*",
showarrow=False,
font=dict(size=20, color="red"),
yshift=15
)
## Group-level error band
if show_error == "group" and not stats_df.empty:
stat_rows = stats_df[
(stats_df["analyte"] == analyte)
& (stats_df["test_type"] == "group")
]
for _, row in stat_rows.iterrows():
mean_L = row.get("mean_L", np.nan)
n = row.get("n_L", 0)
error = np.nan
if pd.notna(row.get("effect_size")) and n > 1 and row["effect_size"] != 0:
error = abs(row.get("R1", np.nan) - mean_L) / abs(row["effect_size"])
if pd.isna(error):
error = 0
#### Filter bands only if stats_df has group info
should_plot = True
if "group" in row.index and astronaut_filter is not None:
group_id = row["group"]
if isinstance(astronaut_filter, str) and astronaut_filter in ["Male", "Female"]:
should_plot = (group_id == astronaut_filter)
elif isinstance(astronaut_filter, (list, tuple, set)):
# Only show if group_id matches one of the selected astronauts
should_plot = (group_id in astronaut_filter)
if should_plot and pd.notna(mean_L):
fig.add_hrect(
y0=mean_L - error, y1=mean_L + error,
fillcolor="gray", opacity=0.2,
layer="below", line_width=0,
annotation_text = "Group Error Band",
annotation_position="top left"
)
if row.get("p_value") is not None and row["p_value"] < 0.05:
fig.add_annotation(
x=31, # R+1 = 31
y=row.get("R1", mean_L),
text="*",
showarrow=False,
font=dict(size=20, color="red"),
yshift=15
)
## Only update range if ref_min/ref_max are valid
if pd.notna(ref_min) and pd.notna(ref_max):
fig.update_yaxes(title=y_label,
range=[ref_min * 0.9, ref_max * 1.1])
else:
fig.update_yaxes(title=y_label)
# Layout: Build Dynamic Title
if astronaut_filter is None:
group_label = "All Participants"
elif isinstance(astronaut_filter, str) and astronaut_filter in ["Male", "Female"]:
group_label = f"{astronaut_filter} Participants"
elif isinstance(astronaut_filter, (list, tuple, set)):
group_label = "Subset: " + ", ".join(astronaut_filter)
else:
group_label = "Participants"
# Build analyte label with units if available
ana_label = ", ".join(analytes)
unit_label = ""
subdf = df[df["analyte"] == analytes[0]]
if "unit" in subdf.columns and not subdf["unit"].dropna().empty:
unit_label = f" ({subdf['unit'].dropna().iloc[0]})"
fig.update_layout(
title=f"{ana_label.title()}{unit_label} Trends ({group_label})",
xaxis_title="Mission Day",
legend_title="Participant / Analyte",
hovermode="x unified",
template="plotly_white",
margin=dict(l=60, r=30, t=60, b=60)
)
# Custom ticks
ticks = [t for t in sorted(df["flight_day"].dropna().unique()) if pd.notna(t)]
ticktext = []
for t in ticks:
if t >= 30:
lbl = f"R+{int(t-30)}"
else:
lbl = f"L{int(t)}"
ticktext.append(lbl)
if ticks:
fig.update_xaxes(tickmode="array", tickvals=ticks, ticktext=ticktext)
return fig123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205import pandas as pd
import numpy as np
from scipy import stats
from .featureEngineering import parse_timepoint
# Map analyte base names to human labels + units + reference ranges
## To get sub and superscripts in Markdown I used ChatGPT: https://chatgpt.com/share/68d9c8f6-2674-8008-8ff7-0731bec9ad49
ANALYTE_INFO = {
#Blood Chemistry
"albumin": {"label": "Albumin", "unit": "g/dL"},
"alkaline_phosphatase": {"label": "Alkaline Phosphatase", "unit": "U/L"},
"alt": {"label": "ALT", "unit": "U/L"},
"ast": {"label": "AST", "unit": "U/L"},
"total_bilirubin": {"label": "Bilirubin", "unit": "mg/dL"},
"bun_to_creatinine_ratio": {"label": "BUN/Creatinine Ratio", "unit": ""},
"calcium": {"label": "Ca²⁺", "unit": "mg/dL"},
"carbon_dioxide": {"label": "CO₂", "unit": "mmol/L"},
"chloride": {"label": "Cl⁻", "unit": "mmol/L"},
"creatinine": {"label": "Creatinine", "unit": "mg/dL"},
"egfr_african_american": {"label": "eGFR (AA)", "unit": "mL/min/1.73m²"},
"egfr_non_african_american": {"label": "eGFR (non-AA)", "unit": "mL/min/1.73m²"},
"globulin": {"label": "Globulin", "unit": "g/dL"},
"glucose": {"label": "Glucose", "unit": "mg/dL"},
"potassium": {"label": "K⁺", "unit": "mmol/L"},
"total_protein": {"label": "Protein", "unit": "g/dL"},
"sodium": {"label": "Na⁺", "unit": "mmol/L"},
"urea_nitrogen_bun": {"label": "BUN", "unit": "mg/dL"},
# Derived feature
"anion_gap": {
"label": "Anion Gap",
"unit": "mmol/L",
"min": 8, # manual reference range
"max": 24
},
## cardiovascular
## Cardiovascular
"a2_macroglobulin": {"label": "α₂-Macroglobulin", "unit": "ng/mL"},
"agp": {"label": "AGP (α1-acid glycoprotein)", "unit": "ng/mL"},
"crp": {"label": "CRP (C-reactive protein)", "unit": "pg/mL"},
"fetuin_a36": {"label": "Fetuin A3/6", "unit": "ng/mL"},
"fibrinogen": {"label": "Fibrinogen", "unit": "ng/mL"},
"haptoglobin": {"label": "Haptoglobin", "unit": "ng/mL"},
"l_selectin": {"label": "L-Selectin", "unit": "pg/mL"},
"pf4": {"label": "Platelet Factor 4", "unit": "ng/mL"},
"sap": {"label": "SAP (Serum Amyloid P)", "unit": "pg/mL"},
}
# Helpers to find columns by prefix (robust to unit suffixes)
def _first_col_startswith(df: pd.DataFrame, prefixes) -> str | None:
"""
Return the first column whose lowercase name starts with any prefix in `prefixes`.
"""
if isinstance(prefixes, str):
prefixes = [prefixes]
prefixes = [p.lower() for p in prefixes]
for col in df.columns:
cl = col.lower()
for p in prefixes:
if cl.startswith(p):
return col
return None
def _value_min_max_cols(df: pd.DataFrame, analyte: str):
"""
For a given base analyte name, return (value_col, min_col, max_col).
Works with clinical chemistry (…_value) and cardiovascular (…_concentration / …_percent).
"""
v = _first_col_startswith(df, f"{analyte}_value")
if v is None:
v = _first_col_startswith(df, f"{analyte}_concentration")
mn = _first_col_startswith(df, [f"{analyte}_range_min", f"{analyte}_min"])
mx = _first_col_startswith(df, [f"{analyte}_range_max", f"{analyte}_max"])
return v, mn, mx
# Tidy Transformation
def tidy_from_wide(df: pd.DataFrame) -> pd.DataFrame:
"""
Transform astronaut CSV with value/min/max triplets into tidy format.
Adds derived analytes (like Anion Gap) using flexible column matching.
Returns: columns [astronautID, timepoint, flight_day, analyte, value, min, max, unit, label, sex]
"""
tidy_records = []
# normalize lookup for id/timepoint columns
colmap = {c.lower(): c for c in df.columns}
astronaut_col = colmap.get("astronautid")
timepoint_col = colmap.get("timepoint")
if astronaut_col is None or timepoint_col is None:
raise KeyError("Expected astronautID and timepoint columns in input CSV")
for analyte, meta in ANALYTE_INFO.items():
if analyte == "anion_gap":
continue
value_col, min_col, max_col = _value_min_max_cols(df, analyte)
if value_col is None:
continue
for _, row in df.iterrows():
rec = {
"astronautID": row[astronaut_col],
"timepoint": row[timepoint_col],
"flight_day": parse_timepoint(row[timepoint_col]),
"analyte": analyte,
"value": row[value_col],
"min": (row[min_col] if (min_col and pd.notna(row[min_col])) else meta.get("min")),
"max": (row[max_col] if (max_col and pd.notna(row[max_col])) else meta.get("max")),
"label": meta["label"],
"unit": meta["unit"],
"sex": "Male" if str(row[astronaut_col]) in ["C001", "C004"] else "Female",
}
tidy_records.append(rec)
return pd.DataFrame(tidy_records)
# Statistical Comparison: R+1 vs L-series
def analyze_r1_vs_L(tidy: pd.DataFrame) -> pd.DataFrame:
"""
Compare R+1 vs L-series for each analyte.
- Within-astronaut: one-sample t-test (H0: mean(L) == R+1)
Returns per-astronaut mean, std, SE, t-stat, p-value, and Cohen's d.
- Across-astronauts (group-level): paired t-test on per-astronaut mean(L) vs R+1
Returns group mean, std across astronauts, SEM, t-stat, p-value, and Cohen's d.
"""
results = []
for analyte, subdf in tidy.groupby("analyte"):
## Within-astronaut tests
for astronaut, adf in subdf.groupby("astronautID"):
L_mask = adf["timepoint"].astype(str).str.startswith("L")
R1_mask = adf["timepoint"].astype(str).isin(["R+1", "R1", "R+01"])
L_vals = adf.loc[L_mask, "value"].dropna().astype(float)
R1_vals = adf.loc[R1_mask, "value"].dropna().astype(float)
if len(L_vals) >= 2 and len(R1_vals) == 1:
R1 = float(R1_vals.iloc[0])
mean_L = float(L_vals.mean())
std_L = float(L_vals.std(ddof=1))
n_L = int(L_vals.shape[0])
if std_L > 0:
se = std_L / np.sqrt(n_L)
t_stat = (mean_L - R1) / se
p_val = 2 * (1 - stats.t.cdf(abs(t_stat), df=n_L - 1))
cohen_d = (R1 - mean_L) / std_L
else:
se = t_stat = p_val = cohen_d = np.nan
results.append({
"analyte": analyte,
"astronautID": astronaut,
"test_type": "within",
"n_L": n_L,
"mean_L": round(mean_L, 2),
"R1": round(R1, 2),
"std_L": round(std_L, 2),
"se_L": round(se, 2) if pd.notna(se) else np.nan,
"t_stat": round(t_stat, 3) if pd.notna(t_stat) else np.nan,
"p_value": round(p_val, 4) if pd.notna(p_val) else np.nan,
"effect_size": round(cohen_d, 3) if pd.notna(cohen_d) else np.nan,
})
## Across-astronauts (paired test)
astronaut_means, astronaut_R1 = [], []
for astronaut, adf in subdf.groupby("astronautID"):
L_mask = adf["timepoint"].astype(str).str.startswith("L")
R1_mask = adf["timepoint"].astype(str).isin(["R+1", "R1", "R+01"])
L_vals = adf.loc[L_mask, "value"].dropna().astype(float)
R1_vals = adf.loc[R1_mask, "value"].dropna().astype(float)
if len(L_vals) >= 2 and len(R1_vals) == 1:
astronaut_means.append(float(L_vals.mean()))
astronaut_R1.append(float(R1_vals.iloc[0]))
if len(astronaut_means) >= 2:
diffs = np.array(astronaut_R1) - np.array(astronaut_means)
t_stat, p_val = stats.ttest_rel(astronaut_R1, astronaut_means)
# Group-level variability
std_L = np.std(astronaut_means, ddof=1)
se_L = std_L / np.sqrt(len(astronaut_means))
cohen_d = diffs.mean() / diffs.std(ddof=1) if diffs.std(ddof=1) > 0 else np.nan
results.append({
"analyte": analyte,
"astronautID": "ALL",
"test_type": "group",
"n_L": len(astronaut_means),
"mean_L": round(float(np.mean(astronaut_means)), 2),
"R1": round(float(np.mean(astronaut_R1)), 2),
"t_stat": round(float(t_stat), 3),
"p_value": round(float(p_val), 4),
"effect_size": round(float(cohen_d), 3) if pd.notna(cohen_d) else np.nan,
})
return pd.DataFrame(results)app.pyAdded+100
final_data/Metabolic_Panel.csvAdded+29
final_data/Serum_Cardiovascular.csvAdded+29
requirements.txt+5-3
scripts/featureEngineering.pyAdded+63
scripts/graphMaking.pyAdded+256
scripts/stats.pyAdded+205