#file_path = "cleaned_bmd_medication_data.xlsx" import streamlit as st import pandas as pd import plotly.graph_objs as go # Constants from linear regression REGRESSION_CONSTANTS = { 'FN': {'mu': 0.916852, 'sigma': 0.120754}, 'TH': {'mu': 0.955439, 'sigma': 0.125406}, 'LS': {'mu': 1.131649, 'sigma': 0.139618}, } # Load medication data @st.cache_data def load_medication_data(): file_path = "/mnt/data/cleaned_bmd_medication_data.xlsx" return pd.read_excel(file_path) # Calculate predicted BMD after medication def calculate_bmd(bmd, percentage_increase): return bmd * (1 + percentage_increase) # Convert BMD to T-score def calculate_tscore(bmd, mu, sigma): return (bmd - mu) / sigma # Generate prediction table for all drugs def generate_predictions(medication_data, site, bmd, mu, sigma): site_data = medication_data[medication_data['Site'] == site] all_results = [] for _, row in site_data.iterrows(): drug = row['Medication'] predictions = {'Year': [], 'Predicted BMD': [], 'Predicted T-score': []} baseline_bmd = bmd for year in row.index[1:-1]: # Skip 'Medication' and 'Site' columns if not pd.isna(row[year]): percentage_increase = row[year] predicted_bmd = calculate_bmd(baseline_bmd, percentage_increase) predicted_tscore = calculate_tscore(predicted_bmd, mu, sigma) predictions['Year'].append(year) predictions['Predicted BMD'].append(round(predicted_bmd, 3)) predictions['Predicted T-score'].append(round(predicted_tscore, 1)) baseline_bmd = predicted_bmd all_results.append({'Drug': drug, 'Predictions': predictions}) return all_results # Display results as table and plots def display_results(predictions, site): st.subheader(f"Predictions for {site}") for result in predictions: drug = result['Drug'] predictions = result['Predictions'] # Display table st.write(f"### {drug}") st.dataframe(pd.DataFrame(predictions)) # Plot BMD and T-score bmd_plot = go.Scatter( x=predictions['Year'], y=predictions['Predicted BMD'], mode='lines+markers', name='Predicted BMD', line=dict(color='blue') ) tscore_plot = go.Scatter( x=predictions['Year'], y=predictions['Predicted T-score'], mode='lines+markers', name='Predicted T-score', line=dict(color='green') ) # Combine plots in a single row col1, col2 = st.columns(2) with col1: st.plotly_chart(go.Figure(data=[bmd_plot], layout=go.Layout( title=f"{drug} - Predicted BMD", xaxis_title="Year", yaxis_title="BMD (g/cm²)" ))) with col2: st.plotly_chart(go.Figure(data=[tscore_plot], layout=go.Layout( title=f"{drug} - Predicted T-score", xaxis_title="Year", yaxis_title="T-score" ))) # Streamlit UI def main(): st.title("BMD and T-score Prediction Tool") # Input patient data bmd_patient = st.number_input("Initial BMD", min_value=0.0, max_value=2.0, value=0.8, step=0.01) site_options = ['FN', 'TH', 'LS'] site = st.selectbox("Select Region (Site)", site_options) # Load constants and medication data constants = REGRESSION_CONSTANTS[site] medication_data = load_medication_data() # Generate and display predictions if st.button("Predict"): predictions = generate_predictions(medication_data, site, bmd_patient, constants['mu'], constants['sigma']) display_results(predictions, site) if __name__ == "__main__": main()