#诊断逻辑代码
importsysimportosimporttimeimportoperatorimportcx_Oracleimportnumpy as npimportpandas as pdimporttensorflow as tf
conn=cx_Oracle.connect('doctor/admin@localhost:1521/tszr')
cursor=conn.cursor()#one-hot编码
defonehot(labels):
n_sample=len(labels)
n_class= max(labels) + 1onehot_labels=np.zeros((n_sample, n_class))
onehot_labels[np.arange(n_sample), labels]= 1
returnonehot_labels#获取数据集
defgetdata(surgery ,surgeryChest):
sql= "select feature1,feature2,feature3,feature4,feature5,trainLable from menzhen where surgery='%s' and surgeryChest='%s'" %\
(surgery, surgeryChest)
cursor.execute(sql)
rows=cursor.fetchall()
dataset=[]
lables=[]for row inrows:
temp=[]
temp.append(row[0])
temp.append(row[1])
temp.append(row[2])
temp.append(row[3])
temp.append(row[4])
dataset.append(temp)if (row[5] == 3):
lables.append(0)elif (row[5] == 6):
lables.append(1)else:
lables.append(2)
dataset=np.array(dataset)
lables=np.array(lables)
dataset=dataset.astype(np.float32)
labless=onehot(lables)returndataset, lablessdefgetAnswer(a1, a2, a3, a4, a5):
answers=[]
answers.append(int(a1))
answers.append(int(a2))
answers.append(int(a3))
answers.append(int(a4))
answers.append(int(a5))
str_answers=[]if (int(a1) == 1):
str_answers.append("正常")elif (int(a1) == 2):
str_answers.append("轻度")elif (int(a1) == 3):
str_answers.append("偏重")else:
str_answers.append("严重")if (int(a2) == 1):
str_answers.append("正常")elif (int(a2) == 2):
str_answers.append("轻度")elif (int(a2) == 3):
str_answers.append("偏重")else:
str_answers.append("严重")if (int(a3) == 1):
str_answers.append("正常")elif (int(a3) == 2):
str_answers.append("轻度")elif (int(a3) == 3):
str_answers.append("偏重")else:
str_answers.append("严重")if (int(a4) == 1):
str_answers.append("正常")elif (int(a4) == 2):
str_answers.append("轻度")elif (int(a4) == 3):
str_answers.append("偏重")else:
str_answers.append("严重")if (int(a5) == 1):
str_answers.append("正常")elif (int(a5) == 2):
str_answers.append("轻度")elif (int(a5) == 3):
str_answers.append("偏重")else:
str_answers.append("严重")returnanswers, str_answersdefgetSugessiones(sujects, resultName):
sql= "select prescription_1,prescription_2,prescription_3,prescription_4,prescription_5,prescription_6,prescription_7,prescription_8,prescription_9,prescription_10,prescription_11,prescription_12,prescription_13,prescription_14,prescription_15 from prescription where SURGERYCHEST='%s' and illName='%s'" %(
sujects, resultName)
cursor.execute(sql)
rows=cursor.fetchall()
prescriptionData=[]for row inrows:
one=[]
one.append(row[0])
one.append(row[1])
one.append(row[2])
one.append(row[3])
one.append(row[4])
one.append(row[5])
one.append(row[6])
one.append(row[7])
one.append(row[8])
one.append(row[9])
one.append(row[10])
one.append(row[11])
one.append(row[12])
one.append(row[13])
one.append(row[14])
prescriptionData.append(one)
prescriptionDataFrame=pd.DataFrame(prescriptionData)
prescriptionDataSum= prescriptionDataFrame.sum(axis=0)
prescriptionDataSumSorted= sorted(enumerate(prescriptionDataSum), key=lambda x: x[1], reverse=True)
prescriptionDataSumSortedThirst= prescriptionDataSumSorted[:3]
prescriptionIndex=[]for i inrange(len(prescriptionDataSumSortedThirst)):
prescriptionIndex.append(prescriptionDataSumSortedThirst[i][0])
sql= "select prescriptionInfo,health from prescriptionInfo where FAMILY='%s' and ill_name='%s'" %(
sujects, resultName)
cursor.execute(sql)
rows=cursor.fetchall()
prescriptionInfoData=[]
healthData=[]for row inrows:
prescriptionInfoData.append(row[0])
healthData.append(row[1])
prescriptionInfoResult=[]for i inrange(len(prescriptionIndex)):
prescriptionInfoResult.append(prescriptionInfoData[prescriptionIndex[i]])
healthResult=[]for i inrange(len(prescriptionIndex)):
healthResult.append(healthData[prescriptionIndex[i]])returnprescriptionInfoResult, healthResultdefgetSeeProject(subject):
sql= "select CHACKPRO from chackProject where FAMILY='%s'" %(subject)
cursor.execute(sql)
rows=cursor.fetchall()
seeproject=[]for row inrows:
seeproject.append(rows[0])returnseeproject[0][0]defgetDoctors(sujects):
sql= "select addraction,name,summary from doctors where family='%s'" %(sujects)
cursor.execute(sql)
rows=cursor.fetchall()
doctorInfo=[]for row inrows:
doctorInfo.append(row[0])
doctorInfo.append(row[1])
doctorInfo.append(row[2])print("主治专家姓名:" + doctorInfo[1])print("主治专家简介:" + doctorInfo[2])print("主治专家所在医院:" +doctorInfo[0])returndoctorInfodefgetPAH(province, administrative):
sql= "select hostitalname from hostitalAdrrest where province='%s' and administrative='%s'" %(
province, administrative)
cursor.execute(sql)
rows=cursor.fetchall()
hostitalName=[]for row inrows:
hostitalName.append(row[0])
yiyuaan= ""
for i inrange(len(hostitalName)):print(hostitalName[i])
yiyuaan+=hostitalName[i]returnyiyuaandefaddUser_SeeIll(userid, username, sex, age, province, area, bumen, ke, result, chufang, jianyi, yiyuaan, yisheng,
jianchaxiang):
sql= "insert into zhenduanjilutable (userid,username,sex,age,province,area,bumen,ke,result,chufang,jianyi,yiyuaan,yisheng,jianchaxiang) values (%d,'%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')" %(
userid, username, sex, age, province, area, bumen, ke, result, chufang, jianyi, yiyuaan, yisheng, jianchaxiang)
cursor.execute(sql)
conn.commit()if (cursor.rowcount == 1):print("新看病记录添加成功")else:print("新看病记录添加失败")defzhenduan_n(a1, a2, a3, a4, a5, userid, username, surgery, surgeryChest, province, administ):
answers, str_answers=getAnswer(a1, a2, a3, a4, a5)
useranswer= [answers] * 3dataset, labless=getdata(surgery, surgeryChest)
x_data= tf.placeholder("float32", [None, 5])
y_data= tf.placeholder("float32", [None, 3])
weight= tf.Variable(tf.ones([5, 3]))
bias= tf.Variable(tf.ones([3]))#使用softmax激活函数
y_model = tf.nn.softmax(tf.matmul(x_data, weight) +bias)#y_model = tf.nn.relu(tf.matmul(x_data, weight) + bias)
#loss = tf.reduce_sum(tf.pow((y_model - y_data), 2))
#使用交叉熵作为损失函数
loss = -tf.reduce_sum(y_data *tf.log(y_model))#train_step = tf.train.GradientDescentOptimizer(1e-4).minimize(loss)
#使用AdamOptimizer优化器
#train_step = tf.train.AdamOptimizer(1e-4).minimize(loss)
train_step = tf.train.MomentumOptimizer(1e-4, 0.9).minimize(loss)#评估模型
correct_prediction = tf.equal(tf.argmax(y_model, 1), tf.argmax(y_data, 1))
accuracy= tf.reduce_mean(tf.cast(correct_prediction, "float"))
init=tf.initialize_all_variables()
sess=tf.Session()
sess.run(init)for _ in range(10):for i in range(int(len(dataset) / 100)):
sess.run(train_step, feed_dict={x_data: dataset[i:i + 100, :], y_data: labless[i:i + 100, :]})
xl_weight=sess.run(weight)
W=np.dot(xl_weight, useranswer)
result=0for i inrange(len(W[0])):for j inrange(len(W[0, :])):if (i ==j):
result+=W[i][j]
result= int(result / 5)if (result <= 3):
result= 3
elif (result <= 6):
result= 6
else:
result= 9sql= "select ILL_NAME from ill_result_tbZ where FAMILY='%s' and ILL_ID=%d" %(surgeryChest, result)
cursor.execute(sql)
rows=cursor.fetchall()
ILL_NAME=[]for row inrows:
ILL_NAME.append(row[0])
firstResult=ILL_NAME[0]if (firstResult[:2] == "疑似"):
firstResult= "疑似患病"
print(firstResult)
prescriptionInfoResult, healthResult=getSugessiones(surgeryChest, firstResult)
chufang= ''
for i inrange(len(prescriptionInfoResult)):
chufang= chufang + '诊断处方' + str(i + 1) + ':' + prescriptionInfoResult[i] + '。'
print('诊断处方' + str(i + 1) + ':' +prescriptionInfoResult[i])
jianyi= ''
for i inrange(len(healthResult)):
jianyi= jianyi + '养生建议' + str(i + 1) + ':' + healthResult[i] + '。'
print('养生建议' + str(i + 1) + ':' +healthResult[i])if (firstResult != '正常'):print("根据你的情况,系统推荐重点检查身体以下几项健康指标:")
seeproject=getSeeProject(surgeryChest)print("推荐检查:", seeproject)print("根据你的情况,系统向你推荐以下在这一治疗领域专家:")
doctorInfo=getDoctors(surgeryChest)
yisheng= ""
for i inrange(len(doctorInfo)):
yisheng+=doctorInfo[i]print("===============================================================================================")print("根据你所处的位置,系统找到以下与你距离最近的医院:")
yiyuaan=getPAH(province, administ)
sex= "男"age= 13addUser_SeeIll(userid, username, sex, age, province, administ, surgery, surgeryChest, firstResult, chufang,
jianyi, yiyuaan, yisheng, seeproject)#zhenduan_n("1","2","3","4","4",6,"小东东","外科","胸外科","广东省","广州市")#prescriptionInfoResult,healthResult = getSugessiones("胸外科","胸壁结核")#a = getDoctors("胸外科")#getPAH("广东省","广州市")
defaction(infon):
zhenduan_n(int(infon[0]),int(infon[1]),int(infon[2]),int(infon[3]),int(infon[4]),int(infon[5]),infon[6],infon[7],infon[8],infon[9],infon[10])if __name__ == '__main__':
infon=[]for i in range(1,len(sys.argv)):
infon.append(sys.argv[i])
action(infon)