Rathapoom commited on
Commit
fe0e27c
·
verified ·
1 Parent(s): 0e2051f

Create appbackup.py

Browse files
Files changed (1) hide show
  1. appbackup.py +106 -0
appbackup.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import plotly.graph_objs as go
4
+
5
+ # Constants from linear regression
6
+ REGRESSION_CONSTANTS = {
7
+ 'FN': {'mu': 0.916852, 'sigma': 0.120754},
8
+ 'TH': {'mu': 0.955439, 'sigma': 0.125406},
9
+ 'LS': {'mu': 1.131649, 'sigma': 0.139618},
10
+ }
11
+
12
+ # Load medication data
13
+ @st.cache_data
14
+ def load_medication_data():
15
+ file_path = "cleaned_bmd_medication_data.xlsx"
16
+ return pd.read_excel(file_path)
17
+
18
+ # Calculate predicted BMD after medication
19
+ def calculate_bmd(bmd, percentage_increase):
20
+ return bmd * (1 + percentage_increase)
21
+
22
+ # Convert BMD to T-score
23
+ def calculate_tscore(bmd, mu, sigma):
24
+ return (bmd - mu) / sigma
25
+
26
+ # Generate prediction table for all drugs
27
+ def generate_predictions(medication_data, site, bmd, mu, sigma):
28
+ site_data = medication_data[medication_data['Site'] == site]
29
+ all_results = []
30
+
31
+ for _, row in site_data.iterrows():
32
+ drug = row['Medication']
33
+ predictions = {'Year': ['0'], 'Predicted BMD': [round(bmd, 3)], 'Predicted T-score': [round(calculate_tscore(bmd, mu, sigma), 1)]}
34
+
35
+ for year in row.index[1:-1]: # Skip 'Medication' and 'Site' columns
36
+ if not pd.isna(row[year]):
37
+ percentage_increase = row[year]
38
+ # BMD calculated using initial BMD (from user input)
39
+ predicted_bmd = bmd * (1 + percentage_increase)
40
+ predicted_tscore = calculate_tscore(predicted_bmd, mu, sigma)
41
+
42
+ predictions['Year'].append(year.replace(" Year", "")) # Simplify year label
43
+ predictions['Predicted BMD'].append(round(predicted_bmd, 3))
44
+ predictions['Predicted T-score'].append(round(predicted_tscore, 1))
45
+
46
+ all_results.append({'Drug': drug, 'Predictions': predictions})
47
+ return all_results
48
+
49
+ # Display results as table and plots
50
+ def display_results(predictions, site):
51
+ st.subheader(f"Predictions for {site}")
52
+
53
+ for result in predictions:
54
+ drug = result['Drug']
55
+ predictions = result['Predictions']
56
+
57
+ # Display table
58
+ st.write(f"### {drug}")
59
+ st.dataframe(pd.DataFrame(predictions))
60
+
61
+ # Plot BMD and T-score
62
+ bmd_plot = go.Scatter(
63
+ x=predictions['Year'], y=predictions['Predicted BMD'], mode='lines+markers',
64
+ name='Predicted BMD', line=dict(color='blue')
65
+ )
66
+ tscore_plot = go.Scatter(
67
+ x=predictions['Year'], y=predictions['Predicted T-score'], mode='lines+markers',
68
+ name='Predicted T-score', line=dict(color='green')
69
+ )
70
+
71
+ # Combine plots in a single row
72
+ col1, col2 = st.columns(2)
73
+ with col1:
74
+ st.plotly_chart(go.Figure(data=[bmd_plot], layout=go.Layout(
75
+ title=f"{drug} - Predicted BMD", xaxis_title="Year", yaxis_title="BMD (g/cm²)"
76
+ )))
77
+ with col2:
78
+ st.plotly_chart(go.Figure(data=[tscore_plot], layout=go.Layout(
79
+ title=f"{drug} - Predicted T-score", xaxis_title="Year", yaxis_title="T-score"
80
+ )))
81
+
82
+ # Streamlit UI
83
+ def main():
84
+ st.title("BMD and T-score Prediction Tool")
85
+
86
+ # Input patient data
87
+ bmd_patient = st.number_input(
88
+ "Initial BMD",
89
+ min_value=0.000, max_value=2.000,
90
+ value=0.800, step=0.001,
91
+ format="%.3f"
92
+ )
93
+ site_options = ['FN', 'TH', 'LS']
94
+ site = st.selectbox("Select Region (Site)", site_options)
95
+
96
+ # Load constants and medication data
97
+ constants = REGRESSION_CONSTANTS[site]
98
+ medication_data = load_medication_data()
99
+
100
+ # Generate and display predictions
101
+ if st.button("Predict"):
102
+ predictions = generate_predictions(medication_data, site, bmd_patient, constants['mu'], constants['sigma'])
103
+ display_results(predictions, site)
104
+
105
+ if __name__ == "__main__":
106
+ main()