Rathapoom commited on
Commit
b27e6a4
·
verified ·
1 Parent(s): 5aaa2b1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -37
app.py CHANGED
@@ -14,7 +14,7 @@ REGRESSION_CONSTANTS = {
14
  # Load medication data
15
  @st.cache_data
16
  def load_medication_data():
17
- file_path = "cleaned_bmd_medication_data.xlsx"
18
  return pd.read_excel(file_path)
19
 
20
  # Calculate predicted BMD after medication
@@ -28,27 +28,27 @@ def calculate_tscore(bmd, mu, sigma):
28
  # Generate prediction table for all drugs
29
  def generate_predictions(medication_data, site, bmd, mu, sigma):
30
  site_data = medication_data[medication_data['Site'] == site]
31
- years = ['1st Year', '2nd Year', '3rd Year', '4th Year', '5th Year', '6th Year', '8th Year', '10th Year']
32
 
33
- prediction_results = []
34
  for _, row in site_data.iterrows():
35
  drug = row['Medication']
36
- bmd_predictions = [bmd]
37
- tscore_predictions = [calculate_tscore(bmd, mu, sigma)]
38
 
39
- for year in years:
 
40
  if not pd.isna(row[year]):
41
- bmd_new = calculate_bmd(bmd_predictions[-1], row[year])
42
- tscore_new = calculate_tscore(bmd_new, mu, sigma)
43
- bmd_predictions.append(bmd_new)
44
- tscore_predictions.append(tscore_new)
 
 
 
 
 
45
 
46
- prediction_results.append({
47
- 'Drug': drug,
48
- 'BMD Predictions': bmd_predictions,
49
- 'T-score Predictions': tscore_predictions,
50
- })
51
- return prediction_results
52
 
53
  # Display results as table and plots
54
  def display_results(predictions, site):
@@ -56,33 +56,32 @@ def display_results(predictions, site):
56
 
57
  for result in predictions:
58
  drug = result['Drug']
59
- bmd_predictions = result['BMD Predictions']
60
- tscore_predictions = result['T-score Predictions']
61
- years = ['0', '1st', '2nd', '3rd', '4th', '5th', '6th', '8th', '10th']
62
 
63
- # Format values
64
- bmd_predictions = [f"{bmd:.3f}" for bmd in bmd_predictions]
65
- tscore_predictions = [f"{tscore:.1f}" for tscore in tscore_predictions]
66
-
67
- # Create table
68
- data = {
69
- 'Year': years[:len(bmd_predictions)],
70
- 'Predicted BMD': bmd_predictions,
71
- 'Predicted T-score': tscore_predictions,
72
- }
73
  st.write(f"### {drug}")
74
- st.dataframe(pd.DataFrame(data))
75
 
76
- # Create plots
77
- fig_bmd = go.Scatter(x=data['Year'], y=[float(bmd) for bmd in bmd_predictions], mode='lines+markers', name='BMD')
78
- fig_tscore = go.Scatter(x=data['Year'], y=[float(tscore) for tscore in tscore_predictions], mode='lines+markers', name='T-score')
79
-
80
- # Combine both graphs in a single row
 
 
 
 
 
 
81
  col1, col2 = st.columns(2)
82
  with col1:
83
- st.plotly_chart(go.Figure(data=[fig_bmd], layout=go.Layout(title=f"{drug} - Predicted BMD", xaxis_title="Year", yaxis_title="BMD (g/cm²)")))
 
 
84
  with col2:
85
- st.plotly_chart(go.Figure(data=[fig_tscore], layout=go.Layout(title=f"{drug} - Predicted T-score", xaxis_title="Year", yaxis_title="T-score")))
 
 
86
 
87
  # Streamlit UI
88
  def main():
 
14
  # Load medication data
15
  @st.cache_data
16
  def load_medication_data():
17
+ file_path = "/mnt/data/cleaned_bmd_medication_data.xlsx"
18
  return pd.read_excel(file_path)
19
 
20
  # Calculate predicted BMD after medication
 
28
  # Generate prediction table for all drugs
29
  def generate_predictions(medication_data, site, bmd, mu, sigma):
30
  site_data = medication_data[medication_data['Site'] == site]
31
+ all_results = []
32
 
 
33
  for _, row in site_data.iterrows():
34
  drug = row['Medication']
35
+ predictions = {'Year': [], 'Predicted BMD': [], 'Predicted T-score': []}
 
36
 
37
+ baseline_bmd = bmd
38
+ for year in row.index[1:-1]: # Skip 'Medication' and 'Site' columns
39
  if not pd.isna(row[year]):
40
+ percentage_increase = row[year]
41
+ predicted_bmd = calculate_bmd(baseline_bmd, percentage_increase)
42
+ predicted_tscore = calculate_tscore(predicted_bmd, mu, sigma)
43
+
44
+ predictions['Year'].append(year)
45
+ predictions['Predicted BMD'].append(round(predicted_bmd, 3))
46
+ predictions['Predicted T-score'].append(round(predicted_tscore, 1))
47
+
48
+ baseline_bmd = predicted_bmd
49
 
50
+ all_results.append({'Drug': drug, 'Predictions': predictions})
51
+ return all_results
 
 
 
 
52
 
53
  # Display results as table and plots
54
  def display_results(predictions, site):
 
56
 
57
  for result in predictions:
58
  drug = result['Drug']
59
+ predictions = result['Predictions']
 
 
60
 
61
+ # Display table
 
 
 
 
 
 
 
 
 
62
  st.write(f"### {drug}")
63
+ st.dataframe(pd.DataFrame(predictions))
64
 
65
+ # Plot BMD and T-score
66
+ bmd_plot = go.Scatter(
67
+ x=predictions['Year'], y=predictions['Predicted BMD'], mode='lines+markers',
68
+ name='Predicted BMD', line=dict(color='blue')
69
+ )
70
+ tscore_plot = go.Scatter(
71
+ x=predictions['Year'], y=predictions['Predicted T-score'], mode='lines+markers',
72
+ name='Predicted T-score', line=dict(color='green')
73
+ )
74
+
75
+ # Combine plots in a single row
76
  col1, col2 = st.columns(2)
77
  with col1:
78
+ st.plotly_chart(go.Figure(data=[bmd_plot], layout=go.Layout(
79
+ title=f"{drug} - Predicted BMD", xaxis_title="Year", yaxis_title="BMD (g/cm²)"
80
+ )))
81
  with col2:
82
+ st.plotly_chart(go.Figure(data=[tscore_plot], layout=go.Layout(
83
+ title=f"{drug} - Predicted T-score", xaxis_title="Year", yaxis_title="T-score"
84
+ )))
85
 
86
  # Streamlit UI
87
  def main():