File size: 3,719 Bytes
0f4785f
 
 
 
4dd63ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0f4785f
4dd63ab
 
 
 
 
0f4785f
4dd63ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0f4785f
4dd63ab
 
 
 
 
 
 
 
0f4785f
4dd63ab
 
 
 
 
 
 
 
0f4785f
 
 
 
4dd63ab
53461be
4dd63ab
 
 
 
 
 
 
 
 
 
 
 
0f4785f
 
331fa49
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 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():
    # Use the combined medication data we cleaned earlier
    file_path = "/mnt/data/BMD medication prediction.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]
    years = ['1st Year', '2nd Year', '3rd Year', '4th Year', '5th Year', '6th Year', '8th Year', '10th Year']
    
    prediction_results = []
    for _, row in site_data.iterrows():
        drug = row['Medication']
        bmd_predictions = [bmd]
        tscore_predictions = [calculate_tscore(bmd, mu, sigma)]
        
        for year in years:
            if not pd.isna(row[year]):
                bmd_new = calculate_bmd(bmd_predictions[-1], row[year])
                tscore_new = calculate_tscore(bmd_new, mu, sigma)
                bmd_predictions.append(bmd_new)
                tscore_predictions.append(tscore_new)
        
        prediction_results.append({
            'Drug': drug,
            'BMD Predictions': bmd_predictions,
            'T-score Predictions': tscore_predictions,
        })
    return prediction_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']
        bmd_predictions = result['BMD Predictions']
        tscore_predictions = result['T-score Predictions']
        years = ['0', '1st', '2nd', '3rd', '4th', '5th', '6th', '8th', '10th']
        
        # Create table
        data = {
            'Year': years[:len(bmd_predictions)],
            'Predicted BMD': bmd_predictions,
            'Predicted T-score': tscore_predictions,
        }
        st.write(f"### {drug}")
        st.dataframe(pd.DataFrame(data))
        
        # Create plots
        fig_bmd = go.Figure(data=[go.Scatter(x=data['Year'], y=data['Predicted BMD'], mode='lines+markers', name='BMD')])
        fig_bmd.update_layout(title=f"{drug} - Predicted BMD over Time", xaxis_title="Year", yaxis_title="BMD (g/cm²)")
        st.plotly_chart(fig_bmd)
        
        fig_tscore = go.Figure(data=[go.Scatter(x=data['Year'], y=data['Predicted T-score'], mode='lines+markers', name='T-score')])
        fig_tscore.update_layout(title=f"{drug} - Predicted T-score over Time", xaxis_title="Year", yaxis_title="T-score")
        st.plotly_chart(fig_tscore)

# 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()