birgermoell commited on
Commit
87110a5
1 Parent(s): 0c079e6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +246 -0
app.py ADDED
@@ -0,0 +1,246 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import numpy as np
3
+ import scipy.stats as stats
4
+ from fpdf import FPDF
5
+ import base64
6
+ import os
7
+ from plots import test_profile
8
+ import matplotlib.pyplot as plt
9
+ from PIL import Image
10
+
11
+ # Function to calculate z-score
12
+ def calculate_z_score(test_score, mean, std_dev):
13
+ return (test_score - mean) / std_dev
14
+
15
+ def z_score_calculator(value, norm_mean, norm_sd):
16
+ z_value = (value - norm_mean) / norm_sd
17
+ stanine_value = round(1.25 * z_value + 5.5)
18
+ z_score = round(z_value, 2)
19
+ return z_score, stanine_value
20
+
21
+ def bnt_calculator(age, education, bnt):
22
+ if age <= 60 and education <= 12:
23
+ norm_mean = 54.5
24
+ norm_sd = 3.2
25
+ z_score, stanine_value = z_score_calculator(bnt, norm_mean, norm_sd)
26
+ return norm_mean, norm_sd, z_score, stanine_value
27
+ elif age <= 60 and education > 12:
28
+ norm_mean = 54.0
29
+ norm_sd = 4.4
30
+ z_score, stanine_value = z_score_calculator(bnt, norm_mean, norm_sd)
31
+ return norm_mean, norm_sd, z_score, stanine_value
32
+ elif age > 60 and education <= 12:
33
+ norm_mean = 54.8
34
+ norm_sd = 3.3
35
+ z_score, stanine_value = z_score_calculator(bnt, norm_mean, norm_sd)
36
+ return norm_mean, norm_sd, z_score, stanine_value
37
+ elif age > 60 and education > 12:
38
+ norm_mean = 56.2
39
+ norm_sd = 3.4
40
+ z_score, stanine_value = z_score_calculator(bnt, norm_mean, norm_sd)
41
+ return norm_mean, norm_sd, z_score, stanine_value
42
+ else:
43
+ print("missing value/ wrong format")
44
+
45
+ def fas_calculator(age, education, fas):
46
+ if age <= 60 and education <= 12:
47
+ norm_mean = 42.7
48
+ norm_sd = 13.7
49
+ z_score, stanine_value = z_score_calculator(fas, norm_mean, norm_sd)
50
+ return norm_mean, norm_sd, z_score, stanine_value
51
+ elif age <= 60 and education > 12:
52
+ norm_mean = 46.7
53
+ norm_sd = 13.7
54
+ z_score, stanine_value = z_score_calculator(fas, norm_mean, norm_sd)
55
+ return norm_mean, norm_sd, z_score, stanine_value
56
+ elif age > 60 and education <= 12:
57
+ norm_mean = 46.9
58
+ norm_sd = 10.4
59
+ z_score, stanine_value = z_score_calculator(fas, norm_mean, norm_sd)
60
+ return norm_mean, norm_sd, z_score, stanine_value
61
+ elif age > 60 and education > 12:
62
+ norm_mean = 51.6
63
+ norm_sd = 12.6
64
+ z_score, stanine_value = z_score_calculator(fas, norm_mean, norm_sd)
65
+ return norm_mean, norm_sd, z_score, stanine_value
66
+ else:
67
+ print("missing value/ wrong format")
68
+
69
+ def generate_graph(BNT_stanine, FAS_stanine):
70
+ # Create a plot
71
+ fig, ax = plt.subplots()
72
+
73
+ # Set axis labels and title
74
+ ax.set_xlabel('Stanine values')
75
+ ax.set_ylabel('Test')
76
+
77
+ # Set the y-axis to display the tests
78
+ ax.set_yticks([1, 2])
79
+ ax.set_yticklabels(['BNT', 'FAS'])
80
+
81
+ # Set the range of the x-axis
82
+ ax.set_xlim([0, 10])
83
+
84
+ # Add dots for BNT and FAS scores
85
+ ax.scatter(BNT_stanine, 1, s=100, label='BNT')
86
+ ax.scatter(FAS_stanine, 2, s=100, label='FAS')
87
+
88
+ # Add legend
89
+ ax.legend()
90
+
91
+ # Show the plot
92
+ # plt.show()
93
+
94
+ # Save the graph as a png file
95
+ fig.savefig('test_profile.png')
96
+ return 'test_profile.png'
97
+
98
+
99
+
100
+ def create_pdf(z_score, mean, std_dev, logo_path, plot_path):
101
+ pdf = FPDF()
102
+ pdf.add_page()
103
+ pdf.set_xy(0, 0)
104
+ pdf.set_font("Arial", size=12)
105
+
106
+ # Add logos
107
+ x_positions = [25, 85, 145]
108
+ for i, logo_path in enumerate(logo_paths):
109
+ pdf.image(logo_path, x=x_positions[i], y=8, w=40)
110
+ pdf.set_xy(10, 50)
111
+
112
+
113
+ # Add title and center it
114
+ title = "Z-Score Report"
115
+ pdf.set_font("Arial", style="B", size=16)
116
+ title_width = pdf.get_string_width(title) + 6
117
+ pdf.cell((210 - title_width) / 2)
118
+ pdf.cell(title_width, 10, title, 0, 1, "C")
119
+
120
+ # Add z-score and center it
121
+ pdf.set_font("Arial", size=12)
122
+ z_score_text = "Your z-score is: {:.2f}".format(z_score)
123
+ z_score_width = pdf.get_string_width(z_score_text) + 6
124
+ pdf.cell((210 - z_score_width) / 2)
125
+ pdf.cell(z_score_width, 10, z_score_text, 0, 1, "C")
126
+
127
+ # Add mean and standard deviation and center it
128
+ mean_std_text = "Mean: {}, Standard Deviation: {}".format(mean, std_dev)
129
+ mean_std_width = pdf.get_string_width(mean_std_text) + 6
130
+ pdf.cell((210 - mean_std_width) / 2)
131
+ pdf.cell(mean_std_width, 10, mean_std_text, 0, 1, "C")
132
+
133
+ # Add logo
134
+ pdf.image(plot_path, x=10, y=80, w=200)
135
+ # pdf.set_xy(10, 40)
136
+
137
+ # Add tool description and center it
138
+ pdf.set_xy(10, 230)
139
+ pdf.set_font("Arial", size=10)
140
+ description = "This PDF report was generated using the Z-Score Calculator Streamlit App."
141
+ pdf.multi_cell(0, 10, description, 0, "C")
142
+
143
+ # Add explanatory text about the collaboration between KI and KTH
144
+ pdf.set_xy(10, 255)
145
+ pdf.set_font("Arial", size=8)
146
+ collaboration_text = (
147
+ "Den här PDF:en är en del av ett samarbetsprojekt mellan Karolinska Institutet (KI) och "
148
+ "Kungliga Tekniska Högskolan (KTH) med målsättningen att använda artificiell intelligens (AI) och "
149
+ "teknik för att minska administration i sjukhusarbete. Projektet fokuserar på att utveckla och "
150
+ "implementera AI-baserade lösningar för att förbättra arbetsflöden, öka effektiviteten och "
151
+ "minska den administrativa bördan för sjukvårdspersonal. För frågor om formuläret kontakta Fredrik Sand fredrik.sand-aronsson@regionstockholm.se, för frågor om teknik kontakta Birger Moëll bmoell@kth.se."
152
+ )
153
+ line_width = 190
154
+ line_height = pdf.font_size_pt * 0.6
155
+ lines = collaboration_text.split(' ')
156
+ current_line = ''
157
+ for word in lines:
158
+ if pdf.get_string_width(current_line + word) < line_width:
159
+ current_line += word + ' '
160
+ else:
161
+ pdf.cell(line_width, line_height, current_line, 0, 1)
162
+ current_line = word + ' '
163
+ pdf.cell(line_width, line_height, current_line, 0, 1)
164
+
165
+ return pdf
166
+
167
+ def pdf_to_base64(pdf):
168
+ with open(pdf, "rb") as file:
169
+ return base64.b64encode(file.read()).decode('utf-8')
170
+
171
+
172
+ # Title and description
173
+ st.title("Z-Score Calculator")
174
+ st.write("Enter your test score, age, and education level to calculate the z-score.")
175
+
176
+ # Input fields
177
+ #test_score = st.number_input("Test Score", min_value=0, value=0, step=1)
178
+ age = st.number_input("Age", min_value=0, value=18, step=1)
179
+ education_level = st.number_input("Education Level in years", min_value=0, value=18, step=1)
180
+ isw = st.number_input("ISW", min_value=0, value=0, step=1)
181
+ bnt = st.number_input("BNT", min_value=0, value=0, step=1)
182
+ fas = st.number_input("FAS", min_value=0, value=0, step=1)
183
+ animal = st.number_input("Animal", min_value=0, value=0, step=1)
184
+ verb = st.number_input("Verb", min_value=0, value=0, step=1)
185
+ repetition = st.number_input("Repetition", min_value=0, value=0, step=1)
186
+ logicogrammatic = st.number_input("Logicogrammatic", min_value=0, value=0, step=1)
187
+ inference = st.number_input("Inference", min_value=0, value=0, step=1)
188
+ reading_speed = st.number_input("Reading Speed", min_value=0, value=0, step=1)
189
+ decoding_words = st.number_input("Decoding Words", min_value=0, value=0, step=1)
190
+ decoding_non_words = st.number_input("Decoding Non-Words", min_value=0, value=0, step=1)
191
+ months_backward = st.number_input("Months Backward", min_value=0, value=0, step=1)
192
+ pataka = st.number_input("Pataka", min_value=0, value=0, step=1)
193
+
194
+
195
+ # add all the tests
196
+
197
+
198
+
199
+ # Calculate mean and standard deviation based on age and education level
200
+ # For simplicity, we will use made-up values for mean and std_dev
201
+ mean = np.random.randint(50, 100)
202
+ std_dev = np.random.randint(10, 30)
203
+
204
+ # Calculate z-score and display result
205
+ if st.button("Calculate Z-Score"):
206
+ profile = test_profile(age, education_level, isw, bnt, fas)
207
+
208
+ # for each value in the profile, calculate the z-score
209
+ bnt_mean, bnt_std, z_bnt, stanine_bnt = bnt_calculator(age, education_level, bnt)
210
+ fas_mean, fas_std, z_fas, stanine_fas = fas_calculator(age, education_level, fas)
211
+
212
+ # z_score = calculate_z_score(test_score, mean, std_dev)
213
+ st.write(f"Your bnt z-score is: {z_bnt:.2f}")
214
+ st.write(f"Mean: {bnt_mean}, Standard Deviation: {bnt_std}")
215
+
216
+ st.write(f"Your fas z-score is: {z_fas:.2f}")
217
+ st.write(f"Mean: {fas_mean}, Standard Deviation: {fas_std}")
218
+ # Create PDF
219
+ # logo_path="logo.jpg"
220
+
221
+ logo_paths = ["logo.jpg", "logo2.jpg", "logo3.jpg"]
222
+
223
+ # create the plot from the dataframe
224
+
225
+ # check if education level is more than 12 years, if more than 12, set value to one, otherwise zero
226
+ education_level = 1 if education_level > 12 else 0
227
+
228
+ plot_path = generate_graph(stanine_bnt, stanine_fas)
229
+
230
+ # create an image from the plot and add to streamlit display
231
+ image = Image.open(plot_path)
232
+ st.image(image, caption='Stanine plot', use_column_width=True)
233
+
234
+ pdf_filename = "z_score_report.pdf"
235
+ pdf = create_pdf(z_bnt, bnt_mean, bnt_std, logo_paths, plot_path)
236
+ pdf.output(name=pdf_filename)
237
+
238
+ # Download PDF
239
+ with open(pdf_filename, "rb") as file:
240
+ base64_pdf = base64.b64encode(file.read()).decode('utf-8')
241
+ pdf_display = f'<a href="data:application/octet-stream;base64,{base64_pdf}" download="{pdf_filename}">Download PDF</a>'
242
+ st.markdown(pdf_display, unsafe_allow_html=True)
243
+
244
+ # Remove PDF file after download
245
+ if os.path.exists(pdf_filename):
246
+ os.remove(pdf_filename)