Rathapoom commited on
Commit
449be20
·
verified ·
1 Parent(s): 59f682e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -16
app.py CHANGED
@@ -44,27 +44,42 @@ def create_bmd_and_tscore_prediction_table(df_bmd_data, drug_selected, bmd_patie
44
 
45
  return pd.DataFrame(predictions, columns=['Year', 'Predicted BMD', 'Predicted T-score'])
46
 
47
- # Step 5: Plot the BMD increase over time as a line graph and add T-score labels positioned below each point
48
- def plot_bmd_timeline_with_tscore(prediction_table, baseline_bmd):
49
- years = ['0'] + list(prediction_table['Year']) # Add "Year 0" as the starting point
50
  bmd_values = [baseline_bmd] + list(prediction_table['Predicted BMD'])
51
- tscore_values = [''] + list(prediction_table['Predicted T-score']) # No T-score for Year 0
52
 
53
- # Create a line plot for BMD values
54
  trace_bmd = go.Scatter(x=years, y=bmd_values, mode='lines+markers', name='BMD', line=dict(color='blue'))
55
- trace_tscore = go.Scatter(x=years, y=tscore_values, mode='lines+markers', name='T-score', line=dict(color='red'), yaxis='y2')
56
 
57
- # Create layout with dual y-axes
58
- layout = go.Layout(
59
- title="Predicted BMD and T-score over Time",
 
 
 
60
  xaxis=dict(title='Years'),
61
- yaxis=dict(title='BMD (g/cm²)', showgrid=False),
62
- yaxis2=dict(title='T-score', overlaying='y', side='right', showgrid=False),
63
- legend=dict(x=0.1, y=1.1, orientation='h'),
64
  )
65
 
66
- fig = go.Figure(data=[trace_bmd, trace_tscore], layout=layout)
67
- st.plotly_chart(fig)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
  # Step 6: Calculate T-score from adjusted BMD
70
  def calculate_tscore_from_bmd(bmd_patient, c_avg, c_sd):
@@ -87,8 +102,8 @@ def main_with_plot_tscore_labels(file_path, bmd_patient, tscore_patient, C_avg_l
87
  st.write("BMD and T-score Prediction Table")
88
  st.dataframe(prediction_table)
89
 
90
- # Step 5: Plot the BMD increase over time with T-score labels
91
- plot_bmd_timeline_with_tscore(prediction_table, bmd_patient)
92
 
93
  # Step 6: Check if goal is achieved and show the predicted BMD and T-score for each year
94
  for i, row in prediction_table.iterrows():
 
44
 
45
  return pd.DataFrame(predictions, columns=['Year', 'Predicted BMD', 'Predicted T-score'])
46
 
47
+ # Step 5: Plot BMD and T-score as separate graphs side by side
48
+ def plot_bmd_and_tscore_separate(prediction_table, baseline_bmd, baseline_tscore):
49
+ years = ['0'] + list(prediction_table['Year'])
50
  bmd_values = [baseline_bmd] + list(prediction_table['Predicted BMD'])
51
+ tscore_values = [baseline_tscore] + list(prediction_table['Predicted T-score'])
52
 
53
+ # Create BMD plot
54
  trace_bmd = go.Scatter(x=years, y=bmd_values, mode='lines+markers', name='BMD', line=dict(color='blue'))
 
55
 
56
+ # Create T-score plot
57
+ trace_tscore = go.Scatter(x=years, y=tscore_values, mode='lines+markers', name='T-score', line=dict(color='green'))
58
+
59
+ # Layout for BMD graph
60
+ layout_bmd = go.Layout(
61
+ title="Predicted BMD over Time",
62
  xaxis=dict(title='Years'),
63
+ yaxis=dict(title='BMD (g/cm²)', showgrid=False)
 
 
64
  )
65
 
66
+ # Layout for T-score graph
67
+ layout_tscore = go.Layout(
68
+ title="Predicted T-score over Time",
69
+ xaxis=dict(title='Years'),
70
+ yaxis=dict(title='T-score', showgrid=False)
71
+ )
72
+
73
+ # Create figures
74
+ fig_bmd = go.Figure(data=[trace_bmd], layout=layout_bmd)
75
+ fig_tscore = go.Figure(data=[trace_tscore], layout=layout_tscore)
76
+
77
+ # Use Streamlit columns to place two plots side by side
78
+ col1, col2 = st.columns(2)
79
+ with col1:
80
+ st.plotly_chart(fig_bmd)
81
+ with col2:
82
+ st.plotly_chart(fig_tscore)
83
 
84
  # Step 6: Calculate T-score from adjusted BMD
85
  def calculate_tscore_from_bmd(bmd_patient, c_avg, c_sd):
 
102
  st.write("BMD and T-score Prediction Table")
103
  st.dataframe(prediction_table)
104
 
105
+ # Step 5: Plot the BMD and T-score graphs side by side
106
+ plot_bmd_and_tscore_separate(prediction_table, bmd_patient, tscore_patient)
107
 
108
  # Step 6: Check if goal is achieved and show the predicted BMD and T-score for each year
109
  for i, row in prediction_table.iterrows():