commit e3f06639540dd7aafab53dfe1d581eee2a91b130 Author: WIN-T9V3LPE8NK2\Administrator Date: Wed Jun 18 14:47:20 2025 +0800 最新版给药算法 diff --git a/geiyao.py b/geiyao.py new file mode 100644 index 0000000..8f0514f --- /dev/null +++ b/geiyao.py @@ -0,0 +1,995 @@ +import time +import math +import numpy as np +import pandas as pd +import pymysql +import datetime +from torch import device, cuda, zeros, from_numpy, load +from torch.nn import LSTM,Linear,Module +device = device("cuda" if cuda.is_available() else "cpu") + + +df = pd.read_csv('./information.txt',sep=',',header=None) +name = df.iloc[0,0] +dbname = str(name) + + +#####计算BMI,肥胖患者需要调整 +def weight(sex, high, weight): + BMI = weight / (high / 100 * high / 100) + if (BMI > 30): + if (sex == "M"): + jiao_weight = high - 100 + 0.4 * (weight - (high - 100)) + lixiang_weight = high - 100 + else: + jiao_weight = high - 105 + 0.4 * (weight - (high - 105)) + lixiang_weight = high - 105 + else: + jiao_weight = weight + lixiang_weight = weight + return jiao_weight, lixiang_weight + +def ku_patient(): + while(1): + try: + conn3 = pymysql.connect(host='127.0.0.1', user='root', password='Xg137839', charset='utf8mb4', db=dbname,port=3306,connect_timeout= 60) + cur3 = conn3.cursor() + sql = ' SELECT * FROM patienttable ORDER BY id DESC LIMIT 1' + cur3.execute(sql) + results = cur3.fetchall() + if len(results) == 0: + print('PatientTable is None!') + time.sleep(2) + else: + break + except Exception as e: + current_time = datetime.datetime.now() + current_time = current_time.strftime("%Y%m%d%H%M%S") + with open("geiyao_error_log.txt", "a") as file: + file.write(f"{current_time} - 错误信息: {str(e)}\n") + global a1 + a1 = np.array(results) + a1 = a1.reshape(16) + conn3.commit() + cur3.close() + conn3.close() + return a1 + +patient = ku_patient() +high = patient[5] +weight1 = patient[4] +sex = patient[2] +weight1, weight2 = weight(sex, high, weight1) + +######################################获取featuretable数据############################################################# +def ku_data(): + while(1): + try: + conn2 = pymysql.connect(host='127.0.0.1', user='root', password='Xg137839', charset='utf8mb4', db=dbname,port=3306,connect_timeout= 60) + cur2 = conn2.cursor() + sql = ' SELECT * FROM featuretable ORDER BY id DESC LIMIT 1' + cur2.execute(sql) + results = cur2.fetchall() + id = results[0][0] + ID[1] = id + if ID[0] == ID[1] or len(results) == 0: + pass + elif ID[0] != ID[1]: + ID[0] = ID[1] + ID[1] = 0 + break + except Exception as e: + current_time = datetime.datetime.now() + current_time = current_time.strftime("%Y%m%d%H%M%S") + with open("geiyao_error_log.txt", "a") as file: + file.write(f"{current_time} - 错误信息: {str(e)}\n") + global a + a = np.array(results) + a = a.reshape(15) + conn2.commit() + cur2.close() + conn2.close() + return a + +##############################40 60: + CHO_NOW = CHO + 10 + elif 0 < BIS < 40: + CHO_NOW = CHO - 10 + return CHO_NOW + +##############################血压############### +def BP_data(SBP, DBP, MAP): + xy = -1 ##### 0代表正常、1代表高血压、2代表低血压、-1代表血压不在判断范围内 + # MAP = (SBP + 2*DBP)/3 + if (SBP > 130 and DBP >=60) or (80 <= SBP <= 130 and DBP>90): + xy = 1 + elif SBP >=80 and 0 < DBP < 60 : + if MAP > 110 : + xy = 1 + elif 0 < MAP < 60: + xy = 2 + elif 60 <= MAP <= 110: + xy = 0 + elif 80 <= SBP <= 130 and 60 <= DBP <= 90: + xy = 0 + elif 0 < SBP < 80 and 0 < DBP <= 90: + xy = 2 + return xy + +#############################读取连续1min的HR(去掉零值),判读其趋势 +def HR_data(b): + b_flag = 0 + while(1): + try: + conn4 = pymysql.connect(host='127.0.0.1', user='root', password='Xg137839', charset='utf8mb4',db=dbname, port=3306, connect_timeout=60) + cur4 = conn4.cursor() + sql = ' SELECT * FROM featuretable ORDER BY id DESC LIMIT %d' % (60) + cur4.execute(sql) + results = cur4.fetchall() + global a + a = np.array(results) + conn4.commit() + cur4.close() + conn4.close() + break + except Exception as e: + current_time = datetime.datetime.now() + current_time = current_time.strftime("%Y%m%d%H%M%S") + with open("geiyao_error_log.txt", "a") as file: + file.write(f"{current_time} - 错误信息: {str(e)}\n") + filtered_data = [x for x in a[:, 3] if x != 0] + if len(filtered_data) == 0: + b_flag = 2 + if b==1: + for i in range(len(filtered_data)): + if filtered_data[i] > 89: + b_flag=1 + else: + b_flag=0 + break + else: + for i in range(len(filtered_data)): + if (0 < filtered_data[i] < 46): + b_flag=1 + else: + b_flag=0 + break + return b_flag + +#############################读取连续10条BIS值,用于LSTM的BIS预测算法;BIS值不足10条时将对已有数据进行复制 +def Feature_data(): + while(1): + try: + conn9 = pymysql.connect(host='127.0.0.1', user='root', password='Xg137839', charset='utf8mb4',db=dbname, port=3306, connect_timeout=60) + cur9 = conn9.cursor() + sql = ' SELECT BIS FROM featuretable ORDER BY id DESC LIMIT %d ' % (10) + cur9.execute(sql) + results = cur9.fetchall() + global a + a = np.array(results) + if len(a) < 10: + repeat_count = math.ceil((10 - len(a)) / len(a)) + 1 + final_results = a.repeat(repeat_count) + final_results = np.array(final_results[:10]).reshape(10, 1) + a = final_results[::-1, :] + a = (a - 40) / 20 + a = a.reshape(a.shape[0], a.shape[1], 1) + conn9.commit() + cur9.close() + conn9.close() + break + else: + a = a[::-1, :] + a = (a-40)/20 + a = a.reshape(a.shape[0],a.shape[1],1) + conn9.commit() + cur9.close() + conn9.close() + break + except Exception as e: + current_time = datetime.datetime.now() + current_time = current_time.strftime("%Y%m%d%H%M%S") + with open("geiyao_error_log.txt", "a") as file: + file.write(f"{current_time} - 错误信息: {str(e)}\n") + return a + +# 丙泊酚最大速度与最小速度 +CHO_max = 120 # ug/kg/min +CHO_min = 60 + +# 瑞芬太尼最大速度与最小速度 +REM_max = 0.3 # ug/kg/min +REM_min = 0.1 + +# 顺阿曲库铵最大速度与最小速度 +ROC_max = 2 # ug/kg/min +ROC_min = 1 # ug/kg/min + +# 罗库溴铵最大速度与最小速度 +RB_max = 12 # ug/kg/min +RB_min = 9 # ug/kg/min + +# 丙泊酚、瑞芬太尼以及顺阿曲库铵的正常浓度 +re_CHO = 90 # ug/kg/min +re_REM = 0.2 # ug/kg/min +re_ROC = 1.5 # ug/kg/min +re_RB = 10 # ug/kg/min + +# 7种药的浓度(又称规格) +CHO_con=10 #mg/ml +REM_con=0.1 #mg/ml +ROC_con=1 #mg/ml +EPH_con=3 #mg/ml 麻黄素 +NIC_con=0.5 #mg/ml 尼卡地平 +ESM_con=10 #mg/ml 艾司洛尔 +ATR_con=0.2 #mg/ml 阿托品 +RB_con = 2.5 #mg/ml 罗库溴铵 + +# 目前速度或剂量,初始化为正常速度 +now_CHO = re_CHO +now_REM = re_REM +now_ROC = re_ROC +now_RB = re_RB +now_NIC = 0 +now_EPH = 0 +now_ESM = 0 +now_ATR = 0 + +# 关于药物时间间隔(生效时间)的计数或给药次数bi +bis_count = 0 +pre_bis_count = 0 +NIC_count = 0 +EPH_count = 0 +TOF_count = 0 + +HRS_count = 0 #慢心率计数 +HRD_count = 0 #快心率计数 +ESM_count = 0 +ATR_count = 0 + +REMSS_count = 0 #瑞芬太尼加药时间间隔的计数 +REMDD_count = 0 #瑞芬太尼减药时间间隔的计数 +REMS_count = 0 #瑞芬太尼加药给药次数 +REMD_count = 0 #瑞芬太尼减药给药次数 + +flag = 0 #判别症状标志 0为正常状态 1为BIS异常;当丙泊酚输注速率达到最大速率或最小速率,视为BIS异常 +bis_flag = 0 #丙泊酚初次判断标志 0为正常状态 1为异常,则进入BIS药物间隔时间计数 +flag1 = 0 #高血压或低血压两次给药无效则转瑞芬太尼 1为转瑞芬太尼 +flag2 = 0 #快心率或慢心率两次给药无效则转瑞芬太尼 1为转瑞芬太尼 +flag4 = 0 #TOF 连续两次加药无效,转人工 1代表转人工 +xueya_flag = 0 #血压标志: 0为正常 1为高血压 2为低血压 +xinlv_flag = 0 #心率标志:0为正常 1为快心率 2为慢心率 +REM_flag = 0 #瑞芬太尼初次判断标志 0为正常状态 1为异常,则进入瑞芬太尼药物间隔时间计数 +TOF_flag = 0 #TOF初次判断标志 0为正常状态 1为异常 + + +gaoxue_count = 0 #高血压时间间隔计数(暂关闭) +dixue_count = 0 #低血压时间间隔计数(暂关闭) +gaoxue_cishu = 0 #测得高血压的次数 +dixue_cishu = 0 #测得低血压的次数 +rem_gaoxue_cishu = 0 #转瑞芬太尼后,测得高血压的次数 +rem_dixue_cishu = 0 #转瑞芬太尼后,测得低血压的次数 + +CHO_unit = 2.5 #40 60) and bis_flag == 0:# 判断此时BIS是否正常,如果异常,则观察2min进行丙泊酚药量调整,调整如果到达最大值,转到相应的瑞芬模块 + if now_CHO == 0: + now_CHO = re_CHO + now_REM = re_REM + now_CHO = CHO_BIS(now_CHO, BIS) + bis_flag = 1 + elif flag == 0 and (40 <= BIS <= 60 or BIS == 0) and bis_flag == 0: + if now_CHO == 0 : + now_CHO = re_CHO + now_REM = re_REM + bis_data = Feature_data() + bis_data = bis_data.copy() + bis_data = from_numpy(bis_data).float() + bis_data = bis_data.to(device) + bis_model = load("./bis_model.pt") + bis_model = bis_model.to(device) + bis_pred = bis_model(bis_data) + bis_pred = bis_pred[-1].item() + bis_pred = bis_pred*20+40 + pre_bis_count = pre_bis_count + 1 + print("pre_bis_count",pre_bis_count) + if pre_bis_count == 61 and 42 < bis_pred <= 45: + print(bis_pred) + now_CHO = now_CHO + (-2) * CHO_unit + pre_bis_count = 1 + elif pre_bis_count == 61 and 45 < bis_pred <= 48: + print(bis_pred) + now_CHO = now_CHO + (-1) * CHO_unit + pre_bis_count = 1 + elif pre_bis_count == 61 and 55 < bis_pred <= 58: + print(bis_pred) + now_CHO = now_CHO + 2 * CHO_unit + pre_bis_count = 1 + elif pre_bis_count == 61 and 52 < bis_pred <= 55: + print(bis_pred) + now_CHO = now_CHO + CHO_unit + pre_bis_count = 1 + elif pre_bis_count == 62: + print(bis_pred) + pre_bis_count = 1 + + if bis_flag == 1: + bis_count = bis_count + 1 + print("bis_count",bis_count) + if 0 < BIS <= 35 : + bis_flag = 0 + bis_count = 0 + pre_bis_count = 0 + if bis_count == 121 and (35 < BIS < 40 or BIS > 60): + now_CHO = CHO_BIS(now_CHO, BIS) + bis_count = 1 + pre_bis_count = 0 + elif bis_count == 121 and (40 <= BIS <= 60): + bis_flag = 0 + bis_count = 0 + pre_bis_count = 0 + elif bis_count == 121 and BIS == 0: + bis_count = bis_count - 1 + + if now_CHO > CHO_max: + now_CHO = CHO_max + flag = 1 + elif 0 < now_CHO < CHO_min: + now_CHO = CHO_min + flag = 1 + else: + flag = 0 + ##########################BIS值######################################################## + + ##########################无创血压######################################################## + print("xueya_flag",xueya_flag) + if BP_flag == 2: + if xueya_flag == 0 and BP_data(SBP,DBP,MAP) == 1: + xueya_flag = 1 + elif xueya_flag == 0 and BP_data(SBP,DBP,MAP) == 2: + xueya_flag = 2 + elif xueya_flag == 0 and BP_data(SBP,DBP,MAP) == 0 : + NIC_count = 0 + EPH_count = 0 + if xueya_flag == 1: + if BP_data(SBP,DBP,MAP) == 1: + now_EPH = 0 + EPH_count = 0 + gaoxue_cishu = gaoxue_cishu + 1 + elif BP_data(SBP,DBP,MAP) == 0 or BP_data(SBP,DBP,MAP) == 2: + now_NIC = 0 + now_EPH = 0 + NIC_count = 0 + EPH_count = 0 + xueya_flag = 0 + elif xueya_flag == 2: + if BP_data(SBP,DBP,MAP) == 2: + now_NIC = 0 + NIC_count = 0 + dixue_cishu= dixue_cishu+1 + elif BP_data(SBP,DBP,MAP) == 0 or BP_data(SBP,DBP,MAP) == 1: + now_NIC = 0 + now_EPH = 0 + NIC_count = 0 + EPH_count = 0 + xueya_flag = 0 + #############################两次异常################################################## + if BP_data(SBP,DBP,MAP) == 1 and (gaoxue_cishu == 2): + now_NIC = 1 + now_EPH = 0 + gaoxue_count = 1 + NIC_count = NIC_count + 1 + EPH_count = 0 + gaoxue_cishu = 0 + elif BP_data(SBP,DBP,MAP) == 2 and (dixue_cishu == 2): + now_NIC = 0 + now_EPH = 1 + NIC_count = 0 + EPH_count = EPH_count + 1 + dixue_cishu = 0 + + ##########################有创血压######################################################## + elif BP_flag == 1: + if xueya_flag == 0 and BP_data(SBP,DBP,MAP) == 1: # 判断此时血压是否正常,正常则不进行调整,异常则给对应的异常标志 + xueya_flag = 1 + gaoxue_count = 0 + gaoxue_cishu = 1 + elif xueya_flag == 0 and BP_data(SBP,DBP,MAP) == 2: + xueya_flag = 2 + dixue_count = 0 + dixue_cishu = 1 + elif xueya_flag == 0 and BP_data(SBP,DBP,MAP) == 0: + gaoxue_count = 0 + dixue_count = 0 + NIC_count = 0 + EPH_count = 0 + + if xueya_flag == 1: + gaoxue_count = gaoxue_count + 1 + if gaoxue_count == 61 and BP_data(SBP,DBP,MAP) == 1: + now_EPH = 0 + EPH_count = 0 + gaoxue_count = 1 + gaoxue_cishu = gaoxue_cishu + 1 + elif gaoxue_count == 61 and (BP_data(SBP,DBP,MAP) == 0 or BP_data(SBP,DBP,MAP) == 2): + now_NIC = 0 + now_EPH = 0 + NIC_count = 0 + EPH_count = 0 + xueya_flag = 0 + gaoxue_count = 0 + elif gaoxue_count == 61 and (SBP == 0 or DBP == 0): + gaoxue_count = gaoxue_count - 1 + + elif xueya_flag == 2: + dixue_count = dixue_count + 1 + if dixue_count == 61 and BP_data(SBP,DBP,MAP) == 2: + now_NIC = 0 + NIC_count = 0 + dixue_count = 1 + dixue_cishu = dixue_cishu + 1 + elif dixue_count == 61 and (BP_data(SBP,DBP,MAP) == 0 or BP_data(SBP,DBP,MAP) == 1): + now_NIC = 0 + now_EPH = 0 + NIC_count = 0 + EPH_count = 0 + xueya_flag = 0 + dixue_count = 0 + elif dixue_count == 61 and (SBP == 0 or DBP == 0): + dixue_count = dixue_count - 1 + + if BP_data(SBP,DBP,MAP) == 1 and (gaoxue_cishu == 2): + now_NIC = 1 + now_EPH = 0 + gaoxue_count = 1 + NIC_count = NIC_count + 1 + EPH_count = 0 + gaoxue_cishu = 1 + elif BP_data(SBP,DBP,MAP) == 2 and (dixue_cishu == 2): + now_NIC = 0 + now_EPH = 1 + NIC_count = 0 + EPH_count = EPH_count + 1 + dixue_cishu = 1 + dixue_count = 0 + ######################血压################################## + if NIC_count > 2 or EPH_count > 2: # 如果两次给药后,还是异常则需要转的瑞芬模块 + flag1 = 1 + else: + flag1 = 0 + ##########################心率######################################################## + # 心率调节 + if HR > 89 and xinlv_flag == 0: # 检测心率,如果心率异常则进行给药,记录给药次数,将心率状态标志赋予对应异常状态 + HRD_count = HRD_count + 1 + if HRD_count == 61: + xinlv_flag = 1 + now_ESM = 1 + now_ATR = 0 + ESM_count = ESM_count + 1 + ATR_count = 0 + HRD_count = 0 + HRS_count = 0 + elif (0 < HR < 46) and xinlv_flag == 0: + HRS_count = HRS_count + 1 + if HRS_count == 61: + xinlv_flag = 2 + now_ESM = 0 + now_ATR = 1 + ESM_count = 0 + ATR_count = ATR_count + 1 + HRD_count = 0 + HRS_count = 0 + elif (46 <= HR <= 89) and xinlv_flag == 0: + now_ESM = 0 + now_ATR = 0 + ESM_count = 0 + ATR_count = 0 + HRD_count = 0 + HRS_count = 0 + + if xinlv_flag == 1: # 检测异常状态,说明此时已经给药,需要间隔2min查看状态;如果状态异常则再给药一次,记录给药次数;如果异常状态消失则说明,此时正常,将相应标志置零 + HRD_count = HRD_count + 1 + if HRD_count == 121 and HR_data(1) == 1: + now_ESM = 1 + now_ATR = 0 + ESM_count = ESM_count + 1 + ATR_count = 0 + HRD_count = 1 + elif HRD_count == 121 and HR_data(1) == 0: + now_ESM = 0 + now_ATR = 0 + ESM_count = 0 + ATR_count = 0 + HRD_count = 0 + xinlv_flag = 0 + elif HRD_count == 121 and HR_data(1) == 2: + HRD_count = HRD_count - 1 + elif xinlv_flag == 2: + HRS_count = HRS_count + 1 + if HRS_count == 121 and HR_data(2) == 1: + now_ESM = 0 + now_ATR = 1 + ESM_count = 0 + ATR_count = ATR_count + 1 + HRS_count = 1 + elif HRS_count == 121 and HR_data(2) == 0: + now_ESM = 0 + now_ATR = 0 + ESM_count = 0 + ATR_count = 0 + HRS_count = 0 + xinlv_flag = 0 + elif HRS_count == 121 and HR_data(2) == 2: + HRS_count = HRS_count - 1 + + if ESM_count > 2 or ATR_count > 2: + flag2 = 1 + else: + flag2 = 0 + ##########################心率######################################################## + + ##############################肌松_罗库溴铵################################################## + if jisong == 1: + if TOF >= 10 and TOF_flag == 0: + now_RB = now_RB + 1 + TOF_flag = 1 + elif TOF < 10 and TOF_flag == 0: + now_RB = re_RB + TOF_count = 0 + flag4 = 0 + + if TOF_flag == 1: + TOF_count = TOF_count + 1 + if TOF_count == 121 and TOF >= 10: + now_RB = now_RB + 1 + TOF_count = 1 + elif TOF_count == 121 and 0 < TOF < 10 : + now_RB = re_RB + TOF_count = 0 + TOF_flag = 0 + flag4 = 0 + elif TOF_count == 121 and TOF == 0: + TOF_count = TOF_count - 1 + + if now_RB > RB_max: + now_RB = RB_max + flag4 = 1 + TOF_flag = 0 + TOF_count = 0 + + ##############################肌松_顺阿曲库铵################################################## + elif jisong == 2: + if TOF >= 10 and TOF_flag == 0: + now_ROC = now_ROC + 0.25 + TOF_flag = 1 + elif TOF < 10 and TOF_flag == 0: + now_ROC = re_ROC + TOF_count = 0 + flag4 = 0 + + if TOF_flag == 1: + TOF_count = TOF_count + 1 + if TOF_count == 121 and TOF >= 10: + now_ROC = now_ROC + 0.25 + TOF_count = 1 + elif TOF_count == 121 and 0 < TOF < 10 : + now_ROC = re_ROC + TOF_count = 0 + TOF_flag = 0 + flag4 = 0 + elif TOF_count == 121 and TOF == 0: + TOF_count = TOF_count - 1 + + if now_ROC > ROC_max: + now_ROC = ROC_max + flag4 = 1 + TOF_flag = 0 + TOF_count = 0 + ##################################肌松######################################################## + ##################################瑞芬太尼调控################################################## + if REM_flag == 0 and (flag1 == 1 or flag2 == 1): # 启动瑞芬模块,REM_flag=0 第一次给药之后需要间隔2min 在重新判断 + if NIC_count > 2 or ESM_count > 2: + now_REM = now_REM + 0.05 + REMS_count = REMS_count + 1 + rem_gaoxue_cishu = -1 + REMD_count = 0 + REM_flag = 1 + else: + now_REM = now_REM - 0.05 + REMS_count = 0 + REMD_count = REMD_count + 1 + rem_dixue_cishu = -1 + REM_flag = 2 + now_NIC = 0 + now_EPH = 0 + now_ESM = 0 + now_ATR = 0 + xueya_flag = 0 + xinlv_flag = 0 + + ###########################################yo#######瑞芬太尼_无创血压############ + if REM_flag == 1 and (BP_flag == 2 or BP_flag == 0) : + now_NIC = 0 + now_EPH = 0 + now_ESM = 0 + now_ATR = 0 + if now_REM == 0: + flag = 3 + if flag1 == 1 and now_REM != 0: + if BP_data(SBP,DBP,MAP) == 1: + rem_gaoxue_cishu = rem_gaoxue_cishu +1 + elif BP_data(SBP,DBP,MAP) == 0 and BP_data(SBP,DBP,MAP) == 2: + now_REM = re_REM + REMS_count = 0 + REMD_count = 0 + REM_flag = 0 + flag1 = 0 + rem_gaoxue_cishu = 0 + if BP_data(SBP,DBP,MAP) == 1 and rem_gaoxue_cishu == 2: + now_REM = now_REM + 0.05 + REMS_count = REMS_count + 1 + REMD_count = 0 + rem_gaoxue_cishu = 0 + REMSS_count = REMSS_count + 1 + if REMSS_count == 121: + REMD_count = 0 + if flag2 == 1 and now_REM != 0: + if HR_data(1) == 1: + now_REM = now_REM + 0.05 + REMS_count = REMS_count + 1 + REMD_count = 0 + REMSS_count = 1 + elif HR_data(1) == 2: + REMSS_count = REMSS_count - 1 + elif HR_data(1) == 0: + now_REM = re_REM + REMS_count = 0 + REMD_count = 0 + REM_flag = 0 + flag2 = 0 + REMSS_count = 1 + elif REM_flag == 2 and (BP_flag == 2 or BP_flag == 0): + + now_NIC = 0 + now_EPH = 0 + now_ESM = 0 + now_ATR = 0 + if now_REM == 0: + flag = 3 + if flag1 == 1 and now_REM != 0: + if BP_data(SBP,DBP,MAP) == 2: + rem_dixue_cishu = rem_dixue_cishu + 1 + elif BP_data(SBP,DBP,MAP) == 0 or BP_data(SBP,DBP,MAP) == 1: + now_REM = re_REM + REMS_count = 0 + REMD_count = 0 + REM_flag = 0 + flag1 = 0 + if BP_data(SBP,DBP,MAP) == 2 and rem_dixue_cishu == 2: + now_REM = now_REM - 0.05 + REMS_count = 0 + REMD_count = REMD_count + 1 + rem_dixue_cishu = 0 + REMDD_count = REMDD_count + 1 + if REMDD_count == 121: + REMS_count = 0 + if flag2 == 1 and now_REM != 0: + if HR_data(2) == 1: + now_REM = now_REM - 0.05 + REMS_count = 0 + REMD_count = REMD_count + 1 + REMDD_count = 1 + elif HR_data(2) == 2: + REMDD_count = REMDD_count - 1 + elif HR_data(2) == 0: + now_REM = re_REM + REMS_count = 0 + REMD_count = 0 + REM_flag = 0 + flag2 = 0 + REMDD_count = 1 + + ##################################################瑞芬太尼_有创血压############ + if REM_flag == 1 and BP_flag == 1: + now_NIC = 0 + now_EPH = 0 + now_ESM = 0 + now_ATR = 0 + if now_REM == 0: + flag = 3 + REMSS_count = REMSS_count + 1 + if REMSS_count == 121: + REMD_count = 0 + if flag1 == 1 and now_REM != 0: + if BP_data(SBP,DBP,MAP) == 1: + now_REM = now_REM + 0.05 + REMS_count = REMS_count + 1 + REMD_count = 0 + REMSS_count = 1 + elif SBP == 0 or DBP == 0: + REMSS_count = REMSS_count - 1 + elif BP_data(SBP,DBP,MAP) == 0 or BP_data(SBP,DBP,MAP) == 2: + now_REM = re_REM + REMS_count = 0 + REMD_count = 0 + REM_flag = 0 + flag1 = 0 + REMSS_count = 1 + if flag2 == 1 and now_REM != 0: + if HR_data(1) == 1: + now_REM = now_REM + 0.05 + REMS_count = REMS_count + 1 + REMD_count = 0 + REMSS_count = 1 + elif HR_data(1) == 2: + REMSS_count = REMSS_count - 1 + elif HR_data(1) == 0: + now_REM = re_REM + REMS_count = 0 + REMD_count = 0 + REM_flag = 0 + flag2 = 0 + REMSS_count = 1 + elif REM_flag == 2 and BP_flag == 1: + now_NIC = 0 + now_EPH = 0 + now_ESM = 0 + now_ATR = 0 + REMDD_count = REMDD_count + 1 + if REMDD_count == 121: + REMS_count = 0 + if flag1 == 1 and now_REM != 0: + if BP_data(SBP,DBP,MAP) == 2: + now_REM = now_REM - 0.05 + REMS_count = 0 + REMD_count = REMD_count + 1 + REMDD_count = 1 + elif SBP == 0 or DBP == 0: + REMDD_count = REMDD_count - 1 + elif BP_data(SBP,DBP,MAP) == 0 or BP_data(SBP,DBP,MAP) == 1: + now_REM = re_REM + REMS_count = 0 + REMD_count = 0 + REM_flag = 0 + flag1 = 0 + REMDD_count = 1 + elif flag2 == 1 and now_REM != 0: + if HR_data(2) == 1: + now_REM = now_REM - 0.05 + REMS_count = 0 + REMD_count = REMD_count + 1 + REMDD_count = 1 + elif HR_data(2) == 2: + REMDD_count = REMDD_count - 1 + elif HR_data(2) == 0: + now_REM = re_REM + REMS_count = 0 + REMD_count = 0 + REM_flag = 0 + flag2 = 0 + REMDD_count = 1 + + ###############是否转人工################################# + + if REMS_count > 2 or REMD_count > 2 or flag4 == 1 or flag == 1 or flag == 3: + print("ai_flag",ai_flag) + while (ai_flag): + try: + conn8 = pymysql.connect(host='127.0.0.1', user='root', password='Xg137839', charset='utf8mb4',db=dbname, port=3306, connect_timeout=60) + cur8 = conn8.cursor() + sql = 'INSERT INTO aiflagtable (Flag,ConvertFlag) VALUES (%2f,%2f)' % (0,1) + cur8.execute(sql) + conn8.commit() + cur8.close() + conn8.close() + break + except Exception as e: + current_time = datetime.datetime.now() + current_time = current_time.strftime("%Y%m%d%H%M%S") + with open("geiyao_error_log.txt", "a") as file: + file.write(f"{current_time} - 错误信息: {str(e)}\n") + #########瑞芬太尼速率 + if now_REM > REM_max: + now_REM = REM_max + elif 0 < now_REM < REM_min: + now_REM = REM_min + + while (ai_flag): + try: + print('维持期给药数据:丙泊酚 %.2f,舒芬太尼 %.2f,瑞芬太尼 %.2f,顺阿曲库铵 %.2f,%2f,%2f,%2f,%2f' % (now_CHO, 0, now_REM, now_ROC, now_EPH, now_ESM, now_ATR, now_NIC)) + conn7 = pymysql.connect(host='127.0.0.1', user='root', password='Xg137839', charset='utf8mb4',db=dbname, port=3306, connect_timeout=60) + cur7 = conn7.cursor() + sql = 'INSERT INTO aimedicinetable (Phase,丙泊酚 ,舒芬太尼 ,瑞芬太尼 ,顺阿曲库铵 ,麻黄碱 ,艾司洛尔 ,阿托品 ,尼卡地平 )VALUES (2,%2f,%2f,%2f,%2f,%2f,%2f,%2f,%2f)' % ( + (now_CHO * weight2 * 60) / (1000 * CHO_con), 0, (now_REM * weight2 * 60) / (1000 * REM_con), + (now_ROC * weight2 * 60) / (1000 * ROC_con), now_EPH, now_ESM,now_ATR, now_NIC) + cur7.execute(sql) + conn7.commit() + now_NIC = 0 + now_EPH = 0 + now_ESM = 0 + now_ATR = 0 + cur7.close() + conn7.close() + break + except Exception as e: + current_time = datetime.datetime.now() + current_time = current_time.strftime("%Y%m%d%H%M%S") + with open("geiyao_error_log.txt", "a") as file: + file.write(f"{current_time} - 错误信息: {str(e)}\n") + + except Exception as e: + current_time = datetime.datetime.now() + current_time = current_time.strftime("%Y%m%d%H%M%S") + with open("geiyao_error_log.txt", "a") as file: + file.write(f"{current_time} - 错误信息: {str(e)}\n") + +