-
Notifications
You must be signed in to change notification settings - Fork 0
/
PatientStatis.py
326 lines (268 loc) · 11.3 KB
/
PatientStatis.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
# 使用UTF-8标准编码避免中文乱码
# -*- coding: UTF-8 -*-
import sys
import os
import datetime
import json
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QDialog, QApplication, QAbstractItemView, QTableView, QWidget, QHBoxLayout, QFrame, \
QVBoxLayout, QPushButton
from PyQt5.QtGui import QStandardItemModel, QStandardItem
from PyQt5.QtCore import Qt, pyqtSlot, pyqtSignal, QUrl
import pyecharts.options as opts
from pyecharts.charts import Boxplot
from pyecharts.charts import Pie
from pyecharts.charts import Bar
from pyecharts.faker import Faker
# 模块
from Util.Common import get_sql_connection, get_logger, show_error_message
class PatientStatis(QDialog):
def __init__(self, parent=None):
# 继承所有dialog的方法
super(PatientStatis, self).__init__(parent)
# 初始化ui
self.logger = None
self.connection = None
self.cursor = None
self.set_connection_cursor() # 建立连接
self.set_logger()
self.initUI()
# 为查询进行配置
def initUI(self):
self.setWindowTitle("检验结果分析统计")
self.setMinimumSize(1200, 800)
self.myHtml = QWebEngineView(self) # 浏览器引擎控件
self.myHtml.move(10, 50)
self.myHtml.resize(1000, 600)
self.myHtml.setVisible(False)
btn1 = QPushButton("性别")
btn2 = QPushButton("年龄")
btn3 = QPushButton("是否有诊断结果")
btn4 = QPushButton("是否吸烟")
btn5 = QPushButton("是否饮酒")
btn6 = QPushButton("是否有输血史")
btn7 = QPushButton("是否有手术史")
btn8 = QPushButton("是否有传染病史")
btn9 = QPushButton("是否有过敏史")
hlayout = QHBoxLayout()
hlayout.addWidget(btn1)
hlayout.addWidget(btn2)
hlayout.addWidget(btn3)
hlayout.addWidget(btn4)
hlayout.addWidget(btn5)
hlayout.addWidget(btn6)
hlayout.addWidget(btn7)
hlayout.addWidget(btn8)
hwidget = QWidget(self)
hwidget.setLayout(hlayout)
btn1.clicked.connect(self.sta_gender)
btn2.clicked.connect(self.sta_age)
btn3.clicked.connect(self.sta_result)
btn4.clicked.connect(lambda:self.sta_history("smoke"))
btn5.clicked.connect(lambda: self.sta_history("drink"))
btn6.clicked.connect(lambda:self.sta_history("transfusion"))
btn7.clicked.connect(lambda:self.sta_history("operation"))
btn8.clicked.connect(lambda:self.sta_history("infectious"))
btn9.clicked.connect(lambda:self.sta_history("allergy"))
def load_url(self,file_name):
file_path = os.path.abspath(
os.path.join(
os.path.dirname(__file__),
file_name))
local_url = QUrl.fromLocalFile(file_path)
self.myHtml.load(local_url)
def sta_gender(self):
self.myHtml.setVisible(True)
#有多少女
sql_f = """SELECT COUNT(*) FROM Patient_table WHERE gender='女'"""
self.cursor.execute(sql_f)
num_f = self.cursor.fetchone()[0]
#有多少男
sql_m = """SELECT COUNT(*) FROM Patient_table WHERE gender='男'"""
self.cursor.execute(sql_m)
num_m = self.cursor.fetchone()[0]
x_data = ["女", "男"]
y_data = [num_f,num_m]
(
Pie(init_opts=opts.InitOpts(width="800px", height="500px"))
.add(
series_name="性别分布",
data_pair=[list(z) for z in zip(x_data, y_data)],
radius=["50%", "70%"],
label_opts=opts.LabelOpts(is_show=False, position="center"),
)
.set_global_opts(legend_opts=opts.LegendOpts(pos_left="legft", orient="vertical"))
.set_series_opts(
tooltip_opts=opts.TooltipOpts(
trigger="item", formatter="{a} <br/>{b}: {c} ({d}%)"
),
# label_opts=opts.LabelOpts(formatter="{b}: {c}")
)
.render("gender_sta.html")
)
self.load_url("gender_sta.html")
def sta_age(self):
self.myHtml.setVisible(True)
#0-10
sql1 = """select count(*) from Patient_table where (age>0 and age<=10)"""
self.cursor.execute(sql1)
num1 = self.cursor.fetchone()[0]
sql2 = """select count(*) from Patient_table where (age>10 and age<=20)"""
self.cursor.execute(sql2)
num2 = self.cursor.fetchone()[0]
sql3 = """select count(*) from Patient_table where (age>20 and age<=30)"""
self.cursor.execute(sql3)
num3 = self.cursor.fetchone()[0]
sql4 = """select count(*) from Patient_table where (age>30 and age<=40)"""
self.cursor.execute(sql4)
num4 = self.cursor.fetchone()[0]
sql5 = """select count(*) from Patient_table where (age>40 and age<=50)"""
self.cursor.execute(sql5)
num5 = self.cursor.fetchone()[0]
sql6 = """select count(*) from Patient_table where (age>50 and age<=60)"""
self.cursor.execute(sql6)
num6 = self.cursor.fetchone()[0]
sql7 = """select count(*) from Patient_table where (age>60 and age<=70)"""
self.cursor.execute(sql7)
num7 = self.cursor.fetchone()[0]
sql8 = """select count(*) from Patient_table where (age>70 and age<=80)"""
self.cursor.execute(sql8)
num8 = self.cursor.fetchone()[0]
sql9 = """select count(*) from Patient_table where (age>80 and age<=90)"""
self.cursor.execute(sql9)
num9 = self.cursor.fetchone()[0]
sql10 = """select count(*) from Patient_table where (age>90)"""
self.cursor.execute(sql10)
num10 = self.cursor.fetchone()[0]
data_x = ['0','10','20','30','40','50','60','70','80','90以上']
data_y = [num1,num2,num3,num4,num5,num6,num7,num8,num9,num10]
c = (
Bar()
.add_xaxis(data_x)
.add_yaxis("年龄分布", data_y, color=Faker.rand_color())
.set_global_opts(
title_opts=opts.TitleOpts(title="年龄分布"),
datazoom_opts=opts.DataZoomOpts(orient="vertical"),
)
.render("patient_age.html")
)
self.load_url("patient_age.html")
def sta_result(self):
self.myHtml.setVisible(True)
# 有多少女
sql_yes = """SELECT COUNT(*) FROM Patient_table WHERE result='有'"""
self.cursor.execute(sql_yes)
num_yes = self.cursor.fetchone()[0]
sql_no = """SELECT COUNT(*) FROM Patient_table WHERE result='无'"""
self.cursor.execute(sql_no)
num_no = self.cursor.fetchone()[0]
x_data = ["有", "无"]
y_data = [num_yes, num_no]
c = (
Pie()
.add(
"比例",
[list(z) for z in zip(x_data, y_data)],
radius=["40%", "55%"],
label_opts=opts.LabelOpts(
position="outside",
formatter="{a|{a}}{abg|}\n{hr|}\n {b|{b}: }{c} {per|{d}%} ",
background_color="#eee",
border_color="#aaa",
border_width=1,
border_radius=4,
rich={
"a": {"color": "#999", "lineHeight": 22, "align": "center"},
"abg": {
"backgroundColor": "#e3e3e3",
"width": "100%",
"align": "right",
"height": 22,
"borderRadius": [4, 4, 0, 0],
},
"hr": {
"borderColor": "#aaa",
"width": "100%",
"borderWidth": 0.5,
"height": 0,
},
"b": {"fontSize": 16, "lineHeight": 33},
"per": {
"color": "#eee",
"backgroundColor": "#334455",
"padding": [2, 4],
"borderRadius": 2,
},
},
),
)
.set_global_opts(title_opts=opts.TitleOpts(title="是否有诊断结果"))
.render("result_sta_pie.html")
)
self.load_url("result_sta_pie.html")
def sta_history(self,project):
sql_yes = """SELECT COUNT(*) FROM Patient_table WHERE %s='有'""" % (project)
print("sql")
print(sql_yes)
self.cursor.execute(sql_yes)
num_yes = self.cursor.fetchone()[0]
sql_no = """SELECT COUNT(*) FROM Patient_table WHERE %s='否'""" % (project)
self.cursor.execute(sql_no)
num_no = self.cursor.fetchone()[0]
sql_null = """SELECT COUNT(*) FROM Patient_table WHERE %s='未知'""" %(project)
self.cursor.execute(sql_null)
num_null = self.cursor.fetchone()[0]
x_data = ["是", "否","未知"]
y_data = [num_yes, num_no,num_null]
c = (
Pie()
.add(
"",
[list(z) for z in zip(x_data, y_data)],
radius=["40%", "55%"],
label_opts=opts.LabelOpts(
position="outside",
formatter="{a|{a}}{abg|}\n{hr|}\n {b|{b}: }{c} {per|{d}%} ",
background_color="#eee",
border_color="#aaa",
border_width=1,
border_radius=4,
rich={
"a": {"color": "#999", "lineHeight": 22, "align": "center"},
"abg": {
"backgroundColor": "#e3e3e3",
"width": "100%",
"align": "right",
"height": 22,
"borderRadius": [4, 4, 0, 0],
},
"hr": {
"borderColor": "#aaa",
"width": "100%",
"borderWidth": 0.5,
"height": 0,
},
"b": {"fontSize": 16, "lineHeight": 33},
"per": {
"color": "#eee",
"backgroundColor": "#334455",
"padding": [2, 4],
"borderRadius": 2,
},
},
),
)
.set_global_opts(title_opts=opts.TitleOpts(title="是否有病史"))
.render("result_sta_pie.html")
)
self.load_url("result_sta_pie.html")
## ============================== 功能函数区 ==============================#
# 设置cursor和connection
def set_connection_cursor(self) -> None:
self.connection = get_sql_connection()
self.cursor = self.connection.cursor()
def set_logger(self) -> None:
self.logger = get_logger("my_logger")
# 记录Debug信息
def record_debug(self, debug_message: str) -> None:
self.logger.debug("语句错误,错误原因为{}".format(debug_message))