-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiagExam.py
212 lines (153 loc) · 7.22 KB
/
DiagExam.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
# 使用UTF-8标准编码避免中文乱码
# -*- coding: UTF-8 -*-
import sys
import os
import datetime
import json
import numpy as np
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QDialog, QApplication, QAbstractItemView, QTableView, QWidget, QComboBox,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.options import ComponentTitleOpts
from pyecharts.charts import Boxplot
from pyecharts.components import Table
from pyecharts.charts import Bar,Pie
from pyecharts.charts import Tab
from pyecharts.faker import Faker
from pyecharts.commons.utils import JsCode
# 模块
from Util.Common import get_sql_connection, get_logger, show_error_message
class DiagExam(QDialog):
def __init__(self, parent=None):
# 继承所有dialog的方法
super(DiagExam, self).__init__(parent)
# 为查询进行配置
self.db_name = ["APTT", "Ag", "Act", "RIPA", "FV3C", "CB", "pp", "BS"]
self.dict = {"APTT":0,"Ag":1,"全血凝固时间":2,"血浆蛋白":3,"凝血因子活性":4,"结合胆红素":5,"PP":6,"血糖":7}
self.age_list = []
self.gender_list = []
self.logger = None
self.connection = None
self.cursor = None
self.set_connection_cursor() # 建立连接
self.set_logger()
# 初始化ui
self.initUI()
def initUI(self):
self.setWindowTitle("按病人分析诊断结果")
self.setMinimumSize(1200, 800)
self.cb = QComboBox()
self.cb.addItems(["按照VWD亚型分析","按照血友病分析", "按照出血/血栓分析"])
self.btn = QPushButton("确定", self) # 确定按钮
self.btn.clicked.connect(self.cao) # 绑定槽函数,确定后,显示后续所有ui
hlayout = QHBoxLayout()
hlayout.addWidget(self.cb)
hlayout.addWidget(self.btn)
hwidget = QWidget(self)
hwidget.setLayout(hlayout)
self.myHtml = QWebEngineView(self) # 浏览器引擎控件
self.myHtml.setVisible(False)
self.myHtml.move(10, 50)
self.myHtml.resize(1000, 600)
def cao(self):
self.myHtml.setVisible(True)
if self.cb.currentText() == '按照VWD亚型分析':
x_data = ['1','2A','2B','2M','2N','3']
column = 'vwd_type'
elif self.cb.currentText() == '按照血友病分析':
x_data = ['A','B','VWD']
column = 'haemophilia_type'
elif self.cb.currentText() == '按照出血/血栓分析':
x_data = ['出血病','血栓病']
column = 'binary_type'
self.type_exam_chart(x_data,column)
def type_exam_chart(self,x_data,column):
tab = Tab()
tab.add(self.type_blood_bar(x_data, column), "血型")
tab.add(self.type_exam_boxplot(x_data, column, "APTT", "APTT分析"), "APTT")
tab.add(self.type_exam_boxplot(x_data, column, "Ag", "Ag分析"), "Ag")
tab.add(self.type_exam_boxplot(x_data, column, "Act", "Act分析"), "Act")
tab.add(self.type_exam_boxplot(x_data, column, "RIPA", "RIPA分析"), "RIPA")
tab.add(self.type_exam_boxplot(x_data, column, "FV3C", "FV3C分析"), "FV3C")
tab.render("vwd_exam_tab.html")
self.load_url("vwd_exam_tab.html")
def type_blood_bar(self,x_data,column):
a = []
b = []
o = []
ab = []
for x in x_data:
sql_a = """SELECT COUNT(*) FROM Diagnosis_table INNER JOIN Exam_table ON Diagnosis_table.patient_ID=Exam_table.patient_ID
WHERE Exam_table.Blood_type='A' AND Diagnosis_table.%s='%s'""" % (column,x)
sql_b = """SELECT COUNT(*) FROM Diagnosis_table INNER JOIN Exam_table ON Diagnosis_table.patient_ID=Exam_table.patient_ID
WHERE Exam_table.Blood_type='B' AND Diagnosis_table.%s='%s'""" % (column,x)
sql_o = """SELECT COUNT(*) FROM Diagnosis_table INNER JOIN Exam_table ON Diagnosis_table.patient_ID=Exam_table.patient_ID
WHERE Exam_table.Blood_type='O' AND Diagnosis_table.%s='%s'""" % (column,x)
sql_ab = """SELECT COUNT(*) FROM Diagnosis_table INNER JOIN Exam_table ON Diagnosis_table.patient_ID=Exam_table.patient_ID
WHERE Exam_table.Blood_type='AB' AND Diagnosis_table.%s='%s'""" % (column,x)
print(sql_a)
self.cursor.execute(sql_a)
a_num = self.cursor.fetchone()[0]
self.cursor.execute(sql_b)
b_num = self.cursor.fetchone()[0]
self.cursor.execute(sql_o)
o_num = self.cursor.fetchone()[0]
self.cursor.execute(sql_ab)
ab_num = self.cursor.fetchone()[0]
if(a_num + b_num+o_num+ab_num == 0):
a_ration = b_ration = o_ration = ab_ration = 0
else:
a_ration = np.round(a_num / (a_num + b_num+o_num+ab_num),4)
b_ration = np.round(b_num / (a_num + b_num+o_num+ab_num),4)
o_ration = np.round(o_num / (a_num + b_num+o_num+ab_num),4)
ab_ration = np.round(ab_num / (a_num + b_num+o_num+ab_num),4)
a.append(a_ration)
b.append(b_ration)
o.append(o_ration)
ab.append(ab_ration)
c = (
Bar()
.add_xaxis(x_data)
.add_yaxis("A", a, stack="stack1")
.add_yaxis("B", b, stack="stack1")
.add_yaxis("O", o, stack="stack1")
.add_yaxis("AB", ab, stack="stack1")
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(title_opts=opts.TitleOpts(title="血型"))
)
return c
def type_exam_boxplot(self,x_data,column,project,title):
exam_list = []
for x in x_data:
sql = """SELECT Exam_table.%s FROM Diagnosis_table INNER JOIN Exam_table ON Exam_table.patient_ID = Diagnosis_table.patient_ID
WHERE Diagnosis_table.%s='%s' AND Exam_table.%s is not null""" %(project,column,x,project)
self.cursor.execute(sql)
exam = [i[0] for i in self.cursor.fetchall()]
exam_list.append((exam))
c = Boxplot()
c.add_xaxis([title])
for i in range(len(x_data)):
c.add_yaxis(x_data[i], c.prepare_data([exam_list[i]]))
c.set_global_opts(title_opts=opts.TitleOpts(title=title))
return c
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)
## ============================== 功能函数区 ==============================#
# 设置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))