File size: 3,429 Bytes
64a2ec5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# -*- 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!")