CVD / app.py
Alinadi98's picture
Upload app.py
64a2ec5 verified
# -*- coding: utf-8 -*-
"""cvd
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1wfYcKQ_zXWiWclB3ioFMEoc4z8FzPQEd
"""
import streamlit as st
import pandas as pd
import math
# Define the regression coefficients (β) and mean values (M)
coefficients = {
'age': 0.05,
'sbp': 0.02,
'tc': 0.01,
'whr': 0.5,
'diabetes': 0.8,
'smoking': 0.6,
'family_history': 0.4
}
mean_values = {
'age': 50,
'sbp': 130,
'tc': 200,
'whr': 0.90,
'diabetes': 0,
'smoking': 0,
'family_history': 0
}
# Define the survival rate at mean risk factor values
S_t = 0.9
# Function to calculate f(x, M)
def calculate_f(individual_values, coefficients, mean_values):
f_x_M = 0
for key in coefficients:
if key in individual_values:
term = coefficients[key] * (individual_values[key] - mean_values[key])
f_x_M += term
return f_x_M
# Function to calculate the 10-year CVD risk
def calculate_cvd_risk(individual_values, coefficients, mean_values, S_t):
# Calculate f(x, M)
f_x_M = calculate_f(individual_values, coefficients, mean_values)
# Calculate exp[f(x, M)]
exp_f_x_M = math.exp(f_x_M)
# Calculate P
P = 1 - S_t ** exp_f_x_M
return P
# Streamlit app
st.title("CVD Risk Calculator")
# Input fields
name = st.text_input("Name")
last_name = st.text_input("Last Name")
worker_code = st.text_input("Worker Code")
age = st.number_input("Age", min_value=0, value=30)
sbp = st.number_input("Systolic Blood Pressure (mmHg)", min_value=0, value=120)
tc = st.number_input("Triglycerides (mg/dL)", min_value=0, value=150)
whr = st.number_input("Waist-to-Hip Ratio", min_value=0.0, value=0.85, step=0.01)
diabetes = st.selectbox("Diabetes (Yes/No)", [0, 1], format_func=lambda x: "Yes" if x == 1 else "No")
smoking = st.selectbox("Smoking (Yes/No)", [0, 1], format_func=lambda x: "Yes" if x == 1 else "No")
family_history = st.selectbox("Family History of CVD (Yes/No)", [0, 1], format_func=lambda x: "Yes" if x == 1 else "No")
# Calculate button
if st.button("Calculate CVD Risk"):
# Collect individual values
individual_values = {
'age': age,
'sbp': sbp,
'tc': tc,
'whr': whr,
'diabetes': diabetes,
'smoking': smoking,
'family_history': family_history
}
# Calculate CVD risk
risk = calculate_cvd_risk(individual_values, coefficients, mean_values, S_t)
# Display result
st.write(f"The estimated 10-year CVD risk is: {risk * 100:.2f}%")
# Save data to a DataFrame
data = {
'Name': name,
'Last Name': last_name,
'Worker Code': worker_code,
'Age': age,
'Systolic Blood Pressure': sbp,
'Triglycerides': tc,
'Waist-to-Hip Ratio': whr,
'Diabetes': "Yes" if diabetes == 1 else "No",
'Smoking': "Yes" if smoking == 1 else "No",
'Family History of CVD': "Yes" if family_history == 1 else "No",
'CVD Risk (%)': f"{risk * 100:.2f}"
}
# Append data to an Excel file or create a new one if it doesn't exist
try:
df = pd.read_excel('cvd_data.xlsx')
df = df.append(data, ignore_index=True)
except FileNotFoundError:
df = pd.DataFrame([data])
# Save updated DataFrame to Excel
df.to_excel('cvd_data.xlsx', index=False)
st.success("Data saved successfully!")