File size: 7,367 Bytes
9cf86b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db7295e
e9fb974
446d568
db7295e
e9fb974
 
9cf86b7
db7295e
 
 
 
 
e9fb974
db7295e
e9fb974
db7295e
 
 
 
 
 
 
 
970f4ce
0cd1b6c
970f4ce
 
e9fb974
970f4ce
9cf86b7
 
 
 
 
 
 
 
 
 
 
 
 
 
970f4ce
9cf86b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9fb974
 
 
580ce8d
22f0baf
9cf86b7
e9fb974
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9cf86b7
 
 
 
 
e9fb974
 
9cf86b7
 
 
 
 
 
 
 
e9fb974
 
 
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# import streamlit as st
# import pandas as pd
# import torch
# import torch.nn as nn
# import torch.optim as optim
# from sklearn.metrics import r2_score

# class RegressionModel2(nn.Module):
#     def __init__(self, input_dim2, hidden_dim2, output_dim2):
#         super(RegressionModel2, self).__init__()
#         self.fc1 = nn.Linear(input_dim2, hidden_dim2)
#         self.relu1 = nn.ReLU()
#         self.fc2 = nn.Linear(hidden_dim2, output_dim2)
#         self.batch_norm1 = nn.BatchNorm1d(hidden_dim2)

#     def forward(self, x2):
#         out = self.fc1(x2)
#         out = self.relu1(out)
#         out = self.batch_norm1(out)
#         out = self.fc2(out)
#         return out

# # Load the saved model state dictionary
# model = RegressionModel2(3, 32, 1)
# model.load_state_dict(torch.load('model.pt'))
# model.eval()  # Set the model to evaluation mode

# # Define a function to make predictions
# def predict_astigmatism(age, axis, aca):
#   """
#   This function takes three arguments (age, axis, aca) as input,
#   converts them to a tensor, makes a prediction using the loaded model,
#   and returns the predicted value.
#   """
#   # Prepare the input data
#   data = torch.tensor([[age, axis, aca]], dtype=torch.float32)

#   # Make prediction
#   with torch.no_grad():
#     prediction = model(data)

#   # Return the predicted value
#   return prediction.item()

# def main():
#     st.set_page_config(page_title='Astigmatism Prediction', page_icon=':eyeglasses:', layout='wide')
#     st.write('<style>.st-emotion-cache-1dp5vir.ezrtsby1 { display: none; }</style>', unsafe_allow_html=True)
#     st.write("""<style>.st-emotion-cache-czk5ss.e16jpq800 {display: none;}</style>""", unsafe_allow_html=True)
#     st.markdown(
#         """
#         <style>
#         .navbar {
#             display: flex;
#             justify-content: space-between;
#             align-items: center;
#             background-color: #f2f2f2;
#             padding: 10px;
#         }
#         .logo img {
#             height: 50px;
#         }
#         .menu {
#             list-style-type: none;
#             display: flex;
#         }
#         .menu li {
#             margin-left: 20px;
#         }
#         .text-content {
#             margin-top: 50px;
#             text-align: center;
#         }
#         .button {
#             margin-top: 20px;
#             padding: 10px 20px;
#             font-size: 16px;
#         }
#         </style>
#         """,
#         unsafe_allow_html=True
#     )

#     # st.markdown(
#     #     """
#     #     <body>
#     #     <header>
#     #     <nav class="navbar">
#     #     <div class="logo"><img src="iol.png" alt="Image description"></div>
#     #     <ul class="menu">
#     #     <li><a href="#">Home</a></li>
#     #     <li><a href="#">About</a></li>
#     #     <li><a href="#">Contact</a></li>
#     #     </ul>
#     #     </nav>
#     #     <div class="text-content">
#     #     <h2>Enter Variables</h2>
#     #     <br>
#     #     </div>
#     #     </header>
#     #     </body>
#     #     """,
#     #     unsafe_allow_html=True
#     # )

#     age = st.number_input('Enter Patient Age:', step=0.1)
#     aca_magnitude = st.number_input('Enter ACA Magnitude:', step=0.1)
#     aca_axis = st.number_input('Enter ACA Axis:', step=0.1)

#     if st.button('Predict!'):
#         astigmatism = predict_astigmatism(age, aca_axis, aca_magnitude)
#         st.success(f'Predicted Total Corneal Astigmatism: {astigmatism:.4f}')

# if __name__ == '__main__':
#     main()



import streamlit as st
import pandas as pd
import torch
import torch.nn as nn
import torch.optim as optim
from sklearn.metrics import r2_score
import math

class RegressionModel2(nn.Module):
    def __init__(self, input_dim2, hidden_dim2, output_dim2):
        super(RegressionModel2, self).__init__()
        self.fc1 = nn.Linear(input_dim2, hidden_dim2)
        self.relu1 = nn.ReLU()
        self.fc2 = nn.Linear(hidden_dim2, output_dim2)
        self.batch_norm1 = nn.BatchNorm1d(hidden_dim2)

    def forward(self, x2):
        out = self.fc1(x2)
        out = self.relu1(out)
        out = self.batch_norm1(out)
        out = self.fc2(out)
        return out

# Load the saved model state dictionary
model = RegressionModel2(3, 32, 1)
model.load_state_dict(torch.load('model.pt'))
model.eval()  # Set the model to evaluation mode

def predict_astigmatism(age, axis, aca):
    """
    This function takes three arguments (age, axis, aca) as input,
    converts them to a tensor, makes a prediction using the loaded model,
    and returns the predicted value.
    """
    # Prepare the input data
    data = torch.tensor([[age, axis, aca]], dtype=torch.float32)
    
    # Make prediction
    with torch.no_grad():
        prediction = model(data)
    
    # Return the predicted value
    return prediction.item()

def predict_axis(aca_magnitude, aca_axis):
    # Convert axis to radians
    aca_axis_rad = math.radians(aca_axis)
    
    # Calculate X and Y components
    X = aca_magnitude * math.cos(2 * aca_axis_rad)
    Y = aca_magnitude * math.sin(2 * aca_axis_rad)
    
    # Calculate intermediate axis prediction
    Z = math.degrees(0.5 * math.atan2(Y, X))
    
    # Determine final predicted axis
    if X > 0:
        if Y > 0:
            predicted_axis = Z
        else:
            predicted_axis = Z + 180
    else:
        predicted_axis = Z + 90
    
    # Ensure the axis is between 0 and 180 degrees
    predicted_axis = predicted_axis % 180
    
    return predicted_axis

def main():
    st.set_page_config(page_title='Astigmatism Prediction', page_icon=':eyeglasses:', layout='wide')
    st.write('<style>.st-emotion-cache-1dp5vir.ezrtsby1 { display: none; }</style>', unsafe_allow_html=True)
    st.write("""<style>.st-emotion-cache-czk5ss.e16jpq800 {display: none;}</style>""", unsafe_allow_html=True)

    st.markdown(
        """
        <style>
        .navbar {
            display: flex;
            justify-content: space-between;
            align-items: center;
            background-color: #f2f2f2;
            padding: 10px;
        }
        .logo img {
            height: 50px;
        }
        .menu {
            list-style-type: none;
            display: flex;
        }
        .menu li {
            margin-left: 20px;
        }
        .text-content {
            margin-top: 50px;
            text-align: center;
        }
        .button {
            margin-top: 20px;
            padding: 10px 20px;
            font-size: 16px;
        }
        </style>
        """,
        unsafe_allow_html=True
    )

    st.title('Total Corneal Astigmatism Prediction')

    age = st.number_input('Enter Patient Age:', min_value=0.0, step=0.1)
    aca_magnitude = st.number_input('Enter ACA Magnitude:', min_value=0.0, step=0.1)
    aca_axis = st.number_input('Enter ACA Axis:', min_value=0.0, max_value=180.0, step=0.1)

    if st.button('Predict!'):
        # Predict magnitude
        tca_magnitude = predict_astigmatism(age, aca_axis, aca_magnitude)
        
        # Predict axis
        tca_axis = predict_axis(aca_magnitude, aca_axis)
        
        st.success(f'Predicted Total Corneal Astigmatism Magnitude: {tca_magnitude:.4f} D')
        st.success(f'Predicted Total Corneal Astigmatism Axis: {tca_axis:.2f}°')

if __name__ == '__main__':
    main()