-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
394 lines (302 loc) · 15.4 KB
/
main.py
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import sys
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
from tkinter import font
from collections import OrderedDict
import re
import math
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import norm
class Model:
def __init__(self):
self.parameters = OrderedDict({
"reliability": None,
"sd": None,
"mean": None,
"normvalue": None,
"confidence_level": None,
"question": None,
"hypothesis": None
})
self.statistics = OrderedDict({
"z_value": None,
"se": None,
"set": None,
"normvalue_regression": None
})
self.intervals = OrderedDict({
"ci_lower": None,
"ci_upper": None,
"ci_lower_regression": None,
"ci_upper_regression": None,
"ci": None,
"ci_regression": None,
})
def set_parameter_values(self, value_list):
keys = self.parameters.keys()
self.parameters.update(zip(keys, value_list))
# FIXME: Any way to make this code smaller and more readable?
# Idea: Maybe use sql-database?
def set_z_value(self):
if self.parameters["question"] == "einseitig" and self.parameters["confidence_level"] == "80%":
self.statistics["z_value"] = 0.8416
elif self.parameters["question"] == "einseitig" and self.parameters["confidence_level"] == "90%":
self.statistics["z_value"] = 1.282
elif self.parameters["question"] == "einseitig" and self.parameters["confidence_level"] == "95%":
self.statistics["z_value"] = 1.645
elif self.parameters["question"] == "einseitig" and self.parameters["confidence_level"] == "99%":
self.statistics["z_value"] = 2.326
elif self.parameters["question"] == "zweiseitig" and self.parameters["confidence_level"] == "80%":
self.statistics["z_value"] = 1.282
elif self.parameters["question"] == "zweiseitig" and self.parameters["confidence_level"] == "90%":
self.statistics["z_value"] = 1.645
elif self.parameters["question"] == "zweiseitig" and self.parameters["confidence_level"] == "95%":
self.statistics["z_value"] = 1.96
elif self.parameters["question"] == "zweiseitig" and self.parameters["confidence_level"] == "99%":
self.statistics["z_value"] = 2.576
# FIXME: z theoretically also belongs to category 'statistics' so from a logical point of view it would make more sense to integrate setting of z in this function
# I decided to put the setting of z-value in a separate function called set_z_value for reasons of readability
# In case set_z_value function can be compressed, maybe merge the two functions again to one function to follow logical reasoning
def calculate_statistics(self):
self.statistics["se"] = self.parameters["sd"] * math.sqrt((1-self.parameters["reliability"]))
self.statistics["set"] = self.parameters["sd"] * math.sqrt((self.parameters["reliability"] * (1-self.parameters["reliability"])))
self.statistics["normvalue_regression"] = self.parameters["reliability"] * self.parameters["normvalue"] + self.parameters["mean"] * (1-self.parameters["reliability"])
def calculate_confidence_intervals(self):
self.intervals["ci_lower"] = self.parameters["normvalue"] - self.statistics["z_value"] * self.statistics["se"]
self.intervals["ci_upper"] = self.parameters["normvalue"] + self.statistics["z_value"] * self.statistics["se"]
self.intervals["ci_lower_regression"] = self.statistics["normvalue_regression"] - self.statistics["z_value"] * self.statistics["set"]
self.intervals["ci_upper_regression"] = self.statistics["normvalue_regression"] + self.statistics["z_value"] * self.statistics["set"]
self.intervals["ci"] = self.intervals["ci_upper"] - self.intervals["ci_lower"]
self.intervals["ci_regression"] = self.intervals["ci_upper_regression"] - self.intervals["ci_lower_regression"]
def run(self):
self.set_z_value()
self.calculate_statistics()
self.calculate_confidence_intervals()
def get_plot_data(self):
plotdata = self.parameters
if self.parameters["hypothesis"] == "Äquivalenzhypothese":
plotdata["plot_errorbar_normvalue"] = self.parameters["normvalue"]
plotdata["plot_ci_lower"] = self.intervals["ci_lower"]
plotdata["plot_ci_upper"] = self.intervals["ci_upper"]
plotdata["plot_ci"] = self.intervals["ci"] / 2
elif self.parameters["hypothesis"] == "Regression zur Mitte":
plotdata["plot_errorbar_normvalue"] = self.statistics["normvalue_regression"]
plotdata["plot_ci_lower"] = self.intervals["ci_lower_regression"]
plotdata["plot_ci_upper"] = self.intervals["ci_upper_regression"]
plotdata["plot_ci"] = self.intervals["ci_regression"] / 2
return plotdata
class ParameterFormular(tk.Frame):
def __init__(self,master):
tk.Frame.__init__(self,master)
self.parameterlist = [
"Reliabilitätskoeffizient",
"Standardabweichung des Normwertes",
"Normmittelwert",
"Individueller Normwert",
"Sicherheitswahrscheinlichkeit",
"Seitigkeit",
"Hypothese"
]
self.input_vars = list()
self.create(self.parameterlist)
def create(self,parameterlist):
space = ttk.Frame(self)
space.grid(row=0,column=1,padx=10)
for idx,text in enumerate(parameterlist):
label = ttk.Label(self,text=text,background='#ffffff')
label.grid(row=idx,column=0,sticky="W")
var = tk.StringVar(self.master)
if text == "Reliabilitätskoeffizient":
var.trace("w",self.validate_unit_interval_input)
element = ttk.Entry(self,justify="left",textvariable=var)
elif text == "Sicherheitswahrscheinlichkeit":
var.set("95%")
element = ttk.Combobox(self,textvariable=var,state="readonly",values=["99%","95%","90%","80%"])
elif text == "Seitigkeit":
var.set("einseitig")
element = ParameterFormular.create_radiobuttons(self,var,buttonlist=["einseitig","zweiseitig"])
elif text == "Hypothese":
var.set("Äquivalenzhypothese")
element = ParameterFormular.create_radiobuttons(self,var,buttonlist=["Äquivalenzhypothese","Regression zur Mitte"])
else:
var.trace("w",self.validate_float_input)
element = ttk.Entry(self,justify="left",textvariable=var)
self.input_vars.append(var)
element.grid(row=idx,column=2,sticky="EW")
@staticmethod
def create_radiobuttons(parent,textvariable,buttonlist):
frame = tk.Frame(parent)
# background keyword argument seems not to exist for ttk.Radiobutton
s = ttk.Style()
s.configure('White.TRadiobutton',background='#ffffff')
for text in buttonlist:
radiobutton = ttk.Radiobutton(
frame,
text=text,
value=text,
variable=textvariable,
style='White.TRadiobutton'
)
radiobutton.pack(anchor="w")
return frame
def validate_unit_interval_input(self,name,index,mode):
# allow a number between 0 and 1 or an empty string
regex = re.compile(r'^0(\.[0-9]*)?$|^1(\.0?)?$|^$')
for var in self.input_vars:
if name == str(var):
if not regex.match(var.get()):
var.set(var.get()[:-1])
self.master.bell()
def validate_float_input(self,name,index,mode):
# allow only floating point numbers or an empty string
regex = re.compile(r'^[+-]?((\d+\.?\d*)|(\.\d+))$|^$')
for var in self.input_vars:
if name == str(var):
if not regex.match(var.get()):
var.set(var.get()[:-1])
self.master.bell()
def check_for_input(self):
input_check = True
for var in self.input_vars:
if not var.get():
input_check = False
return input_check
def get_input_values(self):
output_list = [
float(self.input_vars[0].get()),
float(self.input_vars[1].get()),
float(self.input_vars[2].get()),
float(self.input_vars[3].get()),
self.input_vars[4].get(),
self.input_vars[5].get(),
self.input_vars[6].get()
]
return output_list
class Navbar(tk.Frame):
def __init__(self,master):
tk.Frame.__init__(self,master)
self.plot_button = ttk.Button(self,text="Plotten")
self.plot_button.pack()
class Application:
def __init__(self, master):
self.master = master
self.master.title("Konfidenzintervall-Rechner")
self.master.resizable(0,0)
self.config_system_icon()
self.model = Model()
self.build_view()
def build_view(self):
self.parameter_formular = ParameterFormular(self.master)
self.parameter_formular.pack(side="top",fill="x",padx=5,pady=5)
self.navbar = Navbar(self.master)
self.navbar.plot_button.configure(command=self.perform_button_action)
self.navbar.pack(side="left",fill="x",padx=5,pady=5)
def update_model(self):
self.model.set_parameter_values(self.parameter_formular.get_input_values())
self.model.run()
# FIXME: following an object-oriented approach I originally wanted the plot window to also be a class but I didn't know how to do that
def call_plot_window(self,plotdata):
# turn on interactive mode
plt.ion()
# if there is already a plot window clear old plot
if plt:
plt.clf()
# create x values for normal distribution
x_normdist = np.concatenate((
np.linspace(plotdata["mean"] - 3 * plotdata["sd"], plotdata["mean"] - 2 * plotdata["sd"],endpoint=False),
np.linspace(plotdata["mean"] - 2 * plotdata["sd"], plotdata["mean"] - 1 * plotdata["sd"],endpoint=False),
np.linspace(plotdata["mean"] - 1 * plotdata["sd"], plotdata["mean"] + 1 * plotdata["sd"],endpoint=False),
np.linspace(plotdata["mean"] + 1 * plotdata["sd"], plotdata["mean"] + 2 * plotdata["sd"],endpoint=False),
np.linspace(plotdata["mean"] + 2 * plotdata["sd"], plotdata["mean"] + 3 * plotdata["sd"])
))
# plot normal distribution curve
y_normdist = norm.pdf(x_normdist,plotdata["mean"],plotdata["sd"])
plt.plot(x_normdist,y_normdist)
# create logical lists which are used for 'where'-argument in fill-between method
average = (x_normdist >= (plotdata["mean"] - 1 * plotdata["sd"])) & (x_normdist <= (plotdata["mean"] + 1 * plotdata["sd"]))
above_and_below_average = (x_normdist >= (plotdata["mean"] - 2 * plotdata["sd"])) & (x_normdist <= (plotdata["mean"] - 1 * plotdata["sd"])) | (x_normdist >= (plotdata["mean"] + 1 * plotdata["sd"])) & (x_normdist <= (plotdata["mean"] + 2 * plotdata["sd"]))
far_above_and_below_average = (x_normdist >= (plotdata["mean"] - 3 * plotdata["sd"])) & (x_normdist <= (plotdata["mean"] - 2 * plotdata["sd"])) | (x_normdist >= (plotdata["mean"] + 2 * plotdata["sd"])) & (x_normdist <= (plotdata["mean"] + 3 * plotdata["sd"]))
regions = [average,
above_and_below_average,
far_above_and_below_average
]
alpha_values = [0.75,0.5,0.25]
region_labels = [
"durchschnittlich",
"unter-/überdurchschnittlich",
"weit unter-/überdurchschnittlich"
]
# shade regions under curve, use different alpha channel values and labels
for idx,region in enumerate(regions):
plt.fill_between(x_normdist, y_normdist,color="C0",alpha=alpha_values[idx],label=region_labels[idx],where=regions[idx])
# create a vertical line labeled with 'mean'
plt.vlines(plotdata["mean"],ymin=0,ymax=norm.pdf(plotdata["mean"],plotdata["mean"],plotdata["sd"]),color="C0")
plt.text(plotdata["mean"] + 0.01 * plotdata["sd"],norm.pdf(plotdata["mean"],plotdata["mean"],plotdata["sd"]) / 2,'Mittelwert',rotation=90,alpha=0.5)
# plot confidence interval
plt.errorbar(x=plotdata["plot_errorbar_normvalue"],y=0,xerr=plotdata["plot_ci"],fmt=".k",capsize=10)
# set x-axis title
plt.xlabel(xlabel="Skala der Normwerte")
# set x-axis ticks (force x-axis to always be in units of SD)
x_ticks = np.array([
plotdata["mean"] - 3 * plotdata["sd"],
plotdata["mean"] - 2 * plotdata["sd"],
plotdata["mean"] - 1 * plotdata["sd"],
plotdata["mean"],
plotdata["mean"] + 1 * plotdata["sd"],
plotdata["mean"] + 2 * plotdata["sd"],
plotdata["mean"] + 3 * plotdata["sd"]
])
plt.xticks(x_ticks)
# don't show y-axis ticks and title
plt.gca().axes.get_yaxis().set_visible(False)
# create textbox and place it in upper left corner
textbox_strings = [
"Individueller Normwert: {}".format(plotdata["normvalue"]),
"Untere KI-Grenze: {}".format(round(plotdata["plot_ci_lower"],2)),
"Obere KI-Grenze: {}".format(round(plotdata["plot_ci_upper"],2)),
"Reliabilität: {}".format(plotdata["reliability"]),
"Normmittelwert: {}".format(plotdata["mean"]),
"Standardabweichung: {}".format(plotdata["sd"]),
"Sicherheitswahrscheinlichkeit: {}".format(plotdata["confidence_level"]),
"Seitigkeit: {}".format(plotdata["question"]),
"Hypothese: {}".format(plotdata["hypothesis"])
]
sep = '\n'
textbox_content = sep.join(textbox_strings)
textbox_props = dict(boxstyle='round',facecolor='white',alpha=0.5)
plt.text(0.01, 0.98,textbox_content,fontsize=8,transform = plt.gca().transAxes,verticalalignment='top',bbox=textbox_props)
# create legend
plt.legend(loc='upper right', prop={'size': 8})
# change window icon and title
# TODO: Make sure these methods also work under linux and MAC OS
window_manager = plt.get_current_fig_manager()
window_manager.window.wm_iconbitmap("app_icon.ico")
window_manager.canvas.set_window_title('Konfidenzintervall-Rechner')
def perform_button_action(self):
input_check = self.parameter_formular.check_for_input()
if input_check:
self.update_model()
self.call_plot_window(self.model.get_plot_data())
else:
messagebox.showerror("Konfidenzintervall-Rechner","Bitte fülle alle Eingabefelder aus")
def config_system_icon(self):
# application icon (iconbitmap-method doesn't work on linux)
if sys.platform == "win32":
tk.Tk.iconbitmap(self.master, "app_icon.ico")
elif sys.platform == "linux":
pass
@staticmethod
def run():
root = tk.Tk()
# change default color
root.tk_setPalette(background='#ffffff')
# change default font settings
default_font = font.nametofont("TkDefaultFont")
default_font.configure(size=10)
root.option_add("*Font", default_font)
my_gui = Application(root)
root.mainloop()
if __name__ == "__main__":
Application.run()