cdss_helper.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. import copy
  2. from hmac import new
  3. import os
  4. import sys
  5. import logging
  6. import json
  7. import time
  8. from service.kg_edge_service import KGEdgeService
  9. from db.session import get_db
  10. from service.kg_node_service import KGNodeService
  11. from service.kg_prop_service import KGPropService
  12. from cachetools import TTLCache
  13. from cachetools.keys import hashkey
  14. current_path = os.getcwd()
  15. sys.path.append(current_path)
  16. from community.graph_helper import GraphHelper
  17. from typing import List
  18. from agent.cdss.models.schemas import CDSSInput
  19. from config.site import SiteConfig
  20. import networkx as nx
  21. import pandas as pd
  22. logger = logging.getLogger(__name__)
  23. current_path = os.getcwd()
  24. sys.path.append(current_path)
  25. # 图谱数据缓存路径(由dump_graph_data.py生成)
  26. CACHED_DATA_PATH = os.path.join(current_path, 'community', 'web', 'cached_data')
  27. class CDSSHelper(GraphHelper):
  28. def node_search(self, node_id=None, node_type=None, filters=None, limit=1, min_degree=None):
  29. kg_node_service = KGNodeService(next(get_db()))
  30. es_result = kg_node_service.search_title_index("graph_entity_index", node_id, limit)
  31. results = []
  32. for item in es_result:
  33. n = self.graph.nodes.get(item["id"])
  34. score = item["score"]
  35. if n:
  36. results.append({
  37. 'id': item["title"],
  38. 'score': score,
  39. "name": item["title"],
  40. })
  41. return results
  42. def _load_entity_data(self):
  43. config = SiteConfig()
  44. # CACHED_DATA_PATH = config.get_config("CACHED_DATA_PATH")
  45. print("load entity data")
  46. # 这里设置了读取的属性
  47. data = {"id": [], "name": [], "type": [],"is_symptom": [], "sex": [], "age": [],"score": []}
  48. if not os.path.exists(os.path.join(CACHED_DATA_PATH, 'entities_med.json')):
  49. return []
  50. with open(os.path.join(CACHED_DATA_PATH, 'entities_med.json'), "r", encoding="utf-8") as f:
  51. entities = json.load(f)
  52. for item in entities:
  53. #如果id已经存在,则跳过
  54. # if item[0] in data["id"]:
  55. # print(f"skip {item[0]}")
  56. # continue
  57. data["id"].append(int(item[0]))
  58. data["name"].append(item[1]["name"])
  59. data["type"].append(item[1]["type"])
  60. self._append_entity_attribute(data, item, "sex")
  61. self._append_entity_attribute(data, item, "age")
  62. self._append_entity_attribute(data, item, "is_symptom")
  63. self._append_entity_attribute(data, item, "score")
  64. # item[1]["id"] = item[0]
  65. # item[1]["name"] = item[0]
  66. # attrs = item[1]
  67. # self.graph.add_node(item[0], **attrs)
  68. self.entity_data = pd.DataFrame(data)
  69. self.entity_data.set_index("id", inplace=True)
  70. print("load entity data finished")
  71. def _append_entity_attribute(self, data, item, attr_name):
  72. if attr_name in item[1]:
  73. value = item[1][attr_name].split(":")
  74. if len(value) < 2:
  75. data[attr_name].append(value[0])
  76. else:
  77. data[attr_name].append(value[1])
  78. else:
  79. data[attr_name].append("")
  80. def _load_relation_data(self):
  81. config = SiteConfig()
  82. # CACHED_DATA_PATH = config.get_config("CACHED_DATA_PATH")
  83. print("load relationship data")
  84. for i in range(47):
  85. if not os.path.exists(os.path.join(CACHED_DATA_PATH, f"relationship_med_{i}.json")):
  86. continue
  87. if os.path.exists(os.path.join(CACHED_DATA_PATH, f"relationship_med_{i}.json")):
  88. print(f"load entity data {CACHED_DATA_PATH}\\relationship_med_{i}.json")
  89. with open(os.path.join(CACHED_DATA_PATH, f"relationship_med_{i}.json"), "r", encoding="utf-8") as f:
  90. data = {"src": [], "dest": [], "type": [], "weight": []}
  91. relations = json.load(f)
  92. for item in relations:
  93. data["src"].append(int(item[0]))
  94. data["dest"].append(int(item[2]))
  95. data["type"].append(item[4]["type"])
  96. if "order" in item[4]:
  97. order = item[4]["order"].split(":")
  98. if len(order) < 2:
  99. data["weight"].append(order[0])
  100. else:
  101. data["weight"].append(order[1])
  102. else:
  103. data["weight"].append(1)
  104. self.relation_data = pd.concat([self.relation_data, pd.DataFrame(data)], ignore_index=True)
  105. def build_graph(self):
  106. self.entity_data = pd.DataFrame(
  107. {"id": [], "name": [], "type": [], "sex": [], "allowed_age_range": []})
  108. self.relation_data = pd.DataFrame({"src": [], "dest": [], "type": [], "weight": []})
  109. self._load_entity_data()
  110. self._load_relation_data()
  111. self._load_local_data()
  112. self.graph = nx.from_pandas_edgelist(self.relation_data, "src", "dest", edge_attr=True,
  113. create_using=nx.DiGraph())
  114. nx.set_node_attributes(self.graph, self.entity_data.to_dict(orient="index"))
  115. # print(self.graph.in_edges('1257357',data=True))
  116. def _load_local_data(self):
  117. # 这里加载update数据和权重数据
  118. config = SiteConfig()
  119. self.update_data_path = config.get_config('UPDATE_DATA_PATH')
  120. self.factor_data_path = config.get_config('FACTOR_DATA_PATH')
  121. print(f"load update data from {self.update_data_path}")
  122. for root, dirs, files in os.walk(self.update_data_path):
  123. for file in files:
  124. file_path = os.path.join(root, file)
  125. if file_path.endswith(".json") and file.startswith("ent"):
  126. self._load_update_entity_json(file_path)
  127. if file_path.endswith(".json") and file.startswith("rel"):
  128. self._load_update_relationship_json(file_path)
  129. def _load_update_entity_json(self, file):
  130. '''load json data from file'''
  131. print(f"load entity update data from {file}")
  132. # 这里加载update数据,update数据是一个json文件,格式同cached data如下:
  133. with open(file, "r", encoding="utf-8") as f:
  134. entities = json.load(f)
  135. for item in entities:
  136. original_data = self.entity_data[self.entity_data.index == item[0]]
  137. if original_data.empty:
  138. continue
  139. original_data = original_data.iloc[0]
  140. id = int(item[0])
  141. name = item[1]["name"] if "name" in item[1] else original_data['name']
  142. type = item[1]["type"] if "type" in item[1] else original_data['type']
  143. allowed_sex_list = item[1]["allowed_sex_list"] if "allowed_sex_list" in item[1] else original_data[
  144. 'allowed_sex_list']
  145. allowed_age_range = item[1]["allowed_age_range"] if "allowed_age_range" in item[1] else original_data[
  146. 'allowed_age_range']
  147. self.entity_data.loc[id, ["name", "type", "allowed_sex_list", "allowed_age_range"]] = [name, type,
  148. allowed_sex_list,
  149. allowed_age_range]
  150. def _load_update_relationship_json(self, file):
  151. '''load json data from file'''
  152. print(f"load relationship update data from {file}")
  153. with open(file, "r", encoding="utf-8") as f:
  154. relations = json.load(f)
  155. for item in relations:
  156. data = {}
  157. original_data = self.relation_data[(self.relation_data['src'] == data['src']) &
  158. (self.relation_data['dest'] == data['dest']) &
  159. (self.relation_data['type'] == data['type'])]
  160. if original_data.empty:
  161. continue
  162. original_data = original_data.iloc[0]
  163. data["src"] = int(item[0])
  164. data["dest"] = int(item[2])
  165. data["type"] = item[4]["type"]
  166. data["weight"] = item[4]["weight"] if "weight" in item[4] else original_data['weight']
  167. self.relation_data.loc[(self.relation_data['src'] == data['src']) &
  168. (self.relation_data['dest'] == data['dest']) &
  169. (self.relation_data['type'] == data['type']), 'weight'] = data["weight"]
  170. def check_sex_allowed(self, node, sex):
  171. # 性别过滤,假设疾病节点有一个属性叫做allowed_sex_type,值为“0,1,2”,分别代表未知,男,女
  172. sex_allowed = self.graph.nodes[node].get('sex', None)
  173. #sexProps = self.propService.get_props_by_ref_id(node, 'sex')
  174. #if len(sexProps) > 0 and sexProps[0]['prop_value'] is not None and sexProps[0][
  175. #'prop_value'] != input.pat_sex.value:
  176. #continue
  177. if sex_allowed:
  178. if len(sex_allowed) == 0:
  179. # 如果性别列表为空,那么默认允许所有性别
  180. return True
  181. sex_allowed_list = sex_allowed.split(',')
  182. if sex not in sex_allowed_list:
  183. # 如果性别不匹配,跳过
  184. return False
  185. return True
  186. def check_age_allowed(self, node, age):
  187. # 年龄过滤,假设疾病节点有一个属性叫做allowed_age_range,值为“6-88”,代表年龄在0-88月之间是允许的
  188. # 如果说年龄小于6岁,那么我们就认为是儿童,所以儿童的年龄范围是0-6月
  189. age_allowed = self.graph.nodes[node].get('age', None)
  190. if age_allowed:
  191. if len(age_allowed) == 0:
  192. # 如果年龄范围为空,那么默认允许所有年龄
  193. return True
  194. age_allowed_list = age_allowed.split('-')
  195. age_min = int(age_allowed_list[0])
  196. age_max = int(age_allowed_list[-1])
  197. if age_max ==0:
  198. return True
  199. if age >= age_min and age < age_max:
  200. # 如果年龄范围正常,那么返回True
  201. return True
  202. else:
  203. # 如果没有设置年龄范围,那么默认返回True
  204. return True
  205. return False
  206. def check_diease_allowed(self, node):
  207. is_symptom = self.graph.nodes[node].get('is_symptom', None)
  208. if is_symptom == "是":
  209. return False
  210. return True
  211. propService = KGPropService(next(get_db()))
  212. cache = TTLCache(maxsize=100000, ttl=60*60*24*30)
  213. def cdss_travel(self, input: CDSSInput, start_nodes: List, max_hops=3):
  214. """
  215. 基于输入的症状节点,在知识图谱中进行遍历,查找相关疾病、科室、检查和药品
  216. 参数:
  217. input: CDSSInput对象,包含患者的基本信息(年龄、性别等)
  218. start_nodes: 症状节点名称列表,作为遍历的起点
  219. max_hops: 最大遍历深度,默认为3
  220. 返回值:
  221. 返回一个包含以下信息的字典:
  222. - details: 按科室汇总的结果
  223. - diags: 按相关性排序的疾病列表
  224. - checks: 按出现频率排序的检查列表
  225. - drugs: 按出现频率排序的药品列表
  226. - total_diags: 疾病总数
  227. - total_checks: 检查总数
  228. - total_drugs: 药品总数
  229. 主要步骤:
  230. 1. 初始化允许的节点类型和关系类型
  231. 2. 将症状名称转换为节点ID
  232. 3. 遍历图谱查找相关疾病(STEP 1)
  233. 4. 查找疾病对应的科室、检查和药品(STEP 2)
  234. 5. 按科室汇总结果(STEP 3)
  235. 6. 对结果进行排序和统计(STEP 4-6)
  236. """
  237. # 定义允许的节点类型,包括科室、疾病、药品、检查和症状
  238. # 这些类型用于后续的节点过滤和路径查找
  239. DEPARTMENT = ['科室', 'Department']
  240. DIESEASE = ['疾病', 'Disease']
  241. DRUG = ['药品', 'Drug']
  242. CHECK = ['检查', 'Check']
  243. SYMPTOM = ['症状', 'Symptom']
  244. #allowed_types = DEPARTMENT + DIESEASE + DRUG + CHECK + SYMPTOM
  245. allowed_types = DEPARTMENT + DIESEASE + SYMPTOM
  246. # 定义允许的关系类型,包括has_symptom、need_check、recommend_drug、belongs_to
  247. # 这些关系类型用于后续的路径查找和过滤
  248. allowed_links = ['has_symptom','疾病相关症状','belongs_to','所属科室']
  249. # 将输入的症状名称转换为节点ID
  250. # 由于可能存在同名节点,转换后的节点ID数量可能大于输入的症状数量
  251. node_ids = []
  252. node_id_names = {}
  253. # start_nodes里面重复的症状,去重同样的症状
  254. start_nodes = list(set(start_nodes))
  255. for node in start_nodes:
  256. #print(f"searching for node {node}")
  257. result = self.entity_data[self.entity_data['name'] == node]
  258. #print(f"searching for node {result}")
  259. for index, data in result.iterrows():
  260. node_id_names[index] = data["name"]
  261. node_ids = node_ids + [index]
  262. #print(f"start travel from {node_id_names}")
  263. # 这里是一个队列,用于存储待遍历的症状:
  264. node_ids_filtered = []
  265. for node in node_ids:
  266. if self.graph.has_node(node):
  267. node_ids_filtered.append(node)
  268. else:
  269. logger.debug(f"node {node} not found")
  270. node_ids = node_ids_filtered
  271. results = self.step1(node_ids,node_id_names, input, allowed_types, allowed_links,max_hops,DIESEASE)
  272. #self.validDisease(results, start_nodes)
  273. results = self.validDisease(results, start_nodes)
  274. # 调用step2方法处理科室、检查和药品信息
  275. results = self.step2(results)
  276. # STEP 3: 对于结果按照科室维度进行汇总
  277. final_results = self.step3(results)
  278. sorted_final_results = self.step4(final_results)
  279. sorted_final_results = sorted_final_results[:10]
  280. departments = []
  281. for temp in sorted_final_results:
  282. departments.append({"name": temp[0], "count": temp[1]["count"]})
  283. # STEP 5: 对于final_results里面的diseases, checks和durgs统计全局出现的次数并且按照次数降序排序
  284. sorted_score_diags,total_diags = self.step5(final_results, input)
  285. # STEP 6: 整合数据并返回
  286. # if "department" in item.keys():
  287. # final_results["department"] = list(set(final_results["department"]+item["department"]))
  288. # if "diseases" in item.keys():
  289. # final_results["diseases"] = list(set(final_results["diseases"]+item["diseases"]))
  290. # if "checks" in item.keys():
  291. # final_results["checks"] = list(set(final_results["checks"]+item["checks"]))
  292. # if "drugs" in item.keys():
  293. # final_results["drugs"] = list(set(final_results["drugs"]+item["drugs"]))
  294. # if "symptoms" in item.keys():
  295. # final_results["symptoms"] = list(set(final_results["symptoms"]+item["symptoms"]))
  296. return {"details": sorted_final_results,
  297. "score_diags": sorted_score_diags,"total_diags": total_diags,
  298. "departments":departments
  299. # "checks":sorted_checks, "drugs":sorted_drugs,
  300. # "total_checks":total_check, "total_drugs":total_drug
  301. }
  302. def validDisease(self, results, start_nodes):
  303. """
  304. 输出有效的疾病信息为Markdown格式
  305. :param results: 疾病结果字典
  306. :param start_nodes: 起始症状节点列表
  307. :return: 格式化后的Markdown字符串
  308. """
  309. log_data = ["|疾病|症状|出现次数|是否相关"]
  310. log_data.append("|--|--|--|--|")
  311. filtered_results = {}
  312. for item in results:
  313. data = results[item]
  314. data['relevant'] = False
  315. if data["count"] / len(start_nodes) > 0.5:
  316. #cache_key = f'disease_name_ref_id_{data['name']}'
  317. data['relevant'] = True
  318. filtered_results[item] = data
  319. # 初始化疾病的父类疾病
  320. # disease_name = data["name"]
  321. # key = 'disease_name_parent_' +disease_name
  322. # cached_value = self.cache.get(key)
  323. # if cached_value is None:
  324. # out_edges = self.graph.out_edges(item, data=True)
  325. #
  326. # for edge in out_edges:
  327. # src, dest, edge_data = edge
  328. # if edge_data["type"] != '疾病相关父类':
  329. # continue
  330. # dest_data = self.entity_data[self.entity_data.index == dest]
  331. # if dest_data.empty:
  332. # continue
  333. # dest_name = self.entity_data[self.entity_data.index == dest]['name'].tolist()[0]
  334. # self.cache.set(key, dest_name)
  335. # break
  336. if data['relevant'] == False:
  337. continue
  338. log_data.append(f"|{data['name']}|{','.join(data['path'])}|{data['count']}|{data['relevant']}|")
  339. content = "疾病和症状相关性统计表格\n" + "\n".join(log_data)
  340. print(f"\n{content}")
  341. return filtered_results
  342. def step1(self, node_ids,node_id_names, input, allowed_types, allowed_links,max_hops,DIESEASE):
  343. """
  344. 根据症状节点查找相关疾病
  345. :param node_ids: 症状节点ID列表
  346. :param input: 患者信息输入
  347. :param allowed_types: 允许的节点类型
  348. :param allowed_links: 允许的关系类型
  349. :return: 过滤后的疾病结果
  350. """
  351. start_time = time.time()
  352. results = {}
  353. for node in node_ids:
  354. visited = set()
  355. temp_results = {}
  356. cache_key = f"symptom_ref_disease_{str(node)}"
  357. cache_data = self.cache[cache_key] if cache_key in self.cache else None
  358. if cache_data:
  359. temp_results = copy.deepcopy(cache_data)
  360. print(cache_key+":"+node_id_names[node] +':'+ str(len(temp_results)))
  361. if results=={}:
  362. results = temp_results
  363. else:
  364. for disease_id in temp_results:
  365. path = temp_results[disease_id]["path"][0]
  366. if disease_id in results.keys():
  367. results[disease_id]["count"] = results[disease_id]["count"] + 1
  368. results[disease_id]["path"].append(path)
  369. else:
  370. results[disease_id] = temp_results[disease_id]
  371. continue
  372. queue = [(node, 0, node_id_names[node],{'allowed_types': allowed_types, 'allowed_links': allowed_links})]
  373. # 整理input的数据,这里主要是要检查输入数据是否正确,也需要做转换
  374. if input.pat_age and input.pat_age.value is not None and input.pat_age.value > 0 and input.pat_age.type == 'year':
  375. # 这里将年龄从年转换为月,因为我们的图里面的年龄都是以月为单位的
  376. input.pat_age.value = input.pat_age.value * 12
  377. input.pat_age.type = 'month'
  378. # STEP 1: 假设start_nodes里面都是症状,第一步我们先找到这些症状对应的疾病
  379. # TODO 由于这部分是按照症状逐一去寻找疾病,所以实际应用中可以缓存这些结果
  380. while queue:
  381. temp_node, depth, path, data = queue.pop(0)
  382. temp_node = int(temp_node)
  383. # 这里是通过id去获取节点的name和type
  384. entity_data = self.entity_data[self.entity_data.index == temp_node]
  385. # 如果节点不存在,那么跳过
  386. if entity_data.empty:
  387. continue
  388. if self.graph.nodes.get(temp_node) is None:
  389. continue
  390. node_type = self.entity_data[self.entity_data.index == temp_node]['type'].tolist()[0]
  391. node_name = self.entity_data[self.entity_data.index == temp_node]['name'].tolist()[0]
  392. # print(f"node {node} type {node_type}")
  393. if node_type in DIESEASE:
  394. # print(f"node {node} type {node_type} is a disease")
  395. if self.check_diease_allowed(temp_node) == False:
  396. continue
  397. if temp_node in temp_results.keys():
  398. temp_results[temp_node]["count"] = temp_results[temp_node]["count"] + 1
  399. temp_results[temp_node]["path"].append(path)
  400. else:
  401. temp_results[temp_node] = {"type": node_type, "count": 1, "name": node_name, 'path': [path]}
  402. continue
  403. if temp_node in visited or depth > max_hops:
  404. # print(f"{node} already visited or reach max hops")
  405. continue
  406. visited.add(temp_node)
  407. # print(f"check edges from {node}")
  408. if temp_node not in self.graph:
  409. # print(f"node {node} not found in graph")
  410. continue
  411. # todo 目前是取入边,出边是不是也有用?
  412. for edge in self.graph.in_edges(temp_node, data=True):
  413. src, dest, edge_data = edge
  414. if src not in visited and depth + 1 < max_hops:
  415. # print(f"put into queue travel from {src} to {dest}")
  416. queue.append((src, depth + 1, path, data))
  417. # else:
  418. # print(f"skip travel from {src} to {dest}")
  419. print(cache_key+":"+node_id_names[node]+':'+ str(len(temp_results)))
  420. #对temp_results进行深拷贝,然后再进行处理
  421. self.cache[cache_key] = copy.deepcopy(temp_results)
  422. if results == {}:
  423. results = temp_results
  424. else:
  425. for disease_id in temp_results:
  426. path = temp_results[disease_id]["path"][0]
  427. if disease_id in results.keys():
  428. results[disease_id]["count"] = results[disease_id]["count"] + 1
  429. results[disease_id]["path"].append(path)
  430. else:
  431. results[disease_id] = temp_results[disease_id]
  432. end_time = time.time()
  433. # 这里我们需要对结果进行过滤,过滤掉不满足条件的疾病
  434. new_results = {}
  435. for item in results:
  436. if input.pat_sex and input.pat_sex.value is not None and self.check_sex_allowed(item, input.pat_sex.value) == False:
  437. continue
  438. if input.pat_age and input.pat_age.value is not None and self.check_age_allowed(item, input.pat_age.value) == False:
  439. continue
  440. new_results[item] = results[item]
  441. results = new_results
  442. print('STEP 1 '+str(len(results)))
  443. print(f"STEP 1 执行完成,耗时:{end_time - start_time:.2f}秒")
  444. print(f"STEP 1 遍历图谱查找相关疾病 finished")
  445. return results
  446. def step2(self, results):
  447. """
  448. 查找疾病对应的科室、检查和药品信息
  449. :param results: 包含疾病信息的字典
  450. :return: 更新后的results字典
  451. """
  452. start_time = time.time()
  453. print("STEP 2 查找疾病对应的科室、检查和药品 start")
  454. for disease in results.keys():
  455. # cache_key = f"disease_department_{disease}"
  456. # cached_data = self.cache.get(cache_key)
  457. # if cached_data:
  458. # results[disease]["department"] = cached_data
  459. # continue
  460. if results[disease]["relevant"] == False:
  461. continue
  462. department_data = []
  463. out_edges = self.graph.out_edges(disease, data=True)
  464. for edge in out_edges:
  465. src, dest, edge_data = edge
  466. if edge_data["type"] != 'belongs_to' and edge_data["type"] != 'belongs_to':
  467. continue
  468. dest_data = self.entity_data[self.entity_data.index == dest]
  469. if dest_data.empty:
  470. continue
  471. department_name = self.entity_data[self.entity_data.index == dest]['name'].tolist()[0]
  472. department_data.extend([department_name] * results[disease]["count"])
  473. if department_data:
  474. results[disease]["department"] = department_data
  475. #self.cache.set(cache_key, department_data)
  476. print(f"STEP 2 finished")
  477. end_time = time.time()
  478. print(f"STEP 2 执行完成,耗时:{end_time - start_time:.2f}秒")
  479. # 输出日志
  480. log_data = ["|disease|count|department|check|drug|"]
  481. log_data.append("|--|--|--|--|--|")
  482. for item in results.keys():
  483. department_data = results[item].get("department", [])
  484. count_data = results[item].get("count")
  485. check_data = results[item].get("check", [])
  486. drug_data = results[item].get("drug", [])
  487. log_data.append(
  488. f"|{results[item].get("name", item)}|{count_data}|{','.join(department_data)}|{','.join(check_data)}|{','.join(drug_data)}|")
  489. print("疾病科室检查药品相关统计\n" + "\n".join(log_data))
  490. return results
  491. def step3(self, results):
  492. print(f"STEP 3 对于结果按照科室维度进行汇总 start")
  493. final_results = {}
  494. total = 0
  495. for disease in results.keys():
  496. disease = int(disease)
  497. # 由于存在有些疾病没有科室的情况,所以这里需要做一下处理
  498. departments = ['DEFAULT']
  499. if 'department' in results[disease].keys():
  500. departments = results[disease]["department"]
  501. else:
  502. edges = KGEdgeService(next(get_db())).get_edges_by_nodes(src_id=disease, category='所属科室')
  503. #edges有可能为空,这里需要做一下处理
  504. if len(edges) == 0:
  505. continue
  506. departments = [edge['dest_node']['name'] for edge in edges]
  507. # 处理查询结果
  508. for department in departments:
  509. total += 1
  510. if not department in final_results.keys():
  511. final_results[department] = {
  512. "diseases": [str(disease)+":"+results[disease].get("name", disease)],
  513. "checks": results[disease].get("check", []),
  514. "drugs": results[disease].get("drug", []),
  515. "count": 1
  516. }
  517. else:
  518. final_results[department]["diseases"] = final_results[department]["diseases"] + [str(disease)+":"+
  519. results[disease].get("name", disease)]
  520. final_results[department]["checks"] = final_results[department]["checks"] + results[disease].get(
  521. "check", [])
  522. final_results[department]["drugs"] = final_results[department]["drugs"] + results[disease].get(
  523. "drug", [])
  524. final_results[department]["count"] += 1
  525. # 这里是统计科室出现的分布
  526. for department in final_results.keys():
  527. final_results[department]["score"] = final_results[department]["count"] / total
  528. print(f"STEP 3 finished")
  529. # 这里输出日志
  530. log_data = ["|department|disease|check|drug|count|score"]
  531. log_data.append("|--|--|--|--|--|--|")
  532. for department in final_results.keys():
  533. diesease_data = final_results[department].get("diseases", [])
  534. check_data = final_results[department].get("checks", [])
  535. drug_data = final_results[department].get("drugs", [])
  536. count_data = final_results[department].get("count", 0)
  537. score_data = final_results[department].get("score", 0)
  538. log_data.append(
  539. f"|{department}|{','.join(diesease_data)}|{','.join(check_data)}|{','.join(drug_data)}|{count_data}|{score_data}|")
  540. print("\n" + "\n".join(log_data))
  541. return final_results
  542. def step4(self, final_results):
  543. """
  544. 对final_results中的疾病、检查和药品进行统计和排序
  545. 参数:
  546. final_results: 包含科室、疾病、检查和药品的字典
  547. 返回值:
  548. 排序后的final_results
  549. """
  550. print(f"STEP 4 start")
  551. start_time = time.time()
  552. def sort_data(data, count=10):
  553. tmp = {}
  554. for item in data:
  555. if item in tmp.keys():
  556. tmp[item]["count"] += 1
  557. else:
  558. tmp[item] = {"count": 1}
  559. sorted_data = sorted(tmp.items(), key=lambda x: x[1]["count"], reverse=True)
  560. return sorted_data[:count]
  561. for department in final_results.keys():
  562. final_results[department]['name'] = department
  563. final_results[department]["diseases"] = sort_data(final_results[department]["diseases"])
  564. #final_results[department]["checks"] = sort_data(final_results[department]["checks"])
  565. #final_results[department]["drugs"] = sort_data(final_results[department]["drugs"])
  566. # 这里把科室做一个排序,按照出现的次数降序排序
  567. sorted_final_results = sorted(final_results.items(), key=lambda x: x[1]["count"], reverse=True)
  568. print(f"STEP 4 finished")
  569. end_time = time.time()
  570. print(f"STEP 4 执行完成,耗时:{end_time - start_time:.2f}秒")
  571. # 这里输出markdown日志
  572. log_data = ["|department|disease|check|drug|count|score"]
  573. log_data.append("|--|--|--|--|--|--|")
  574. for department in final_results.keys():
  575. diesease_data = final_results[department].get("diseases")
  576. check_data = final_results[department].get("checks")
  577. drug_data = final_results[department].get("drugs")
  578. count_data = final_results[department].get("count", 0)
  579. score_data = final_results[department].get("score", 0)
  580. log_data.append(f"|{department}|{diesease_data}|{check_data}|{drug_data}|{count_data}|{score_data}|")
  581. print("\n" + "\n".join(log_data))
  582. return sorted_final_results
  583. def step5(self, final_results, input):
  584. """
  585. 按科室汇总结果并排序
  586. 参数:
  587. final_results: 各科室的初步结果
  588. input: 患者输入信息
  589. 返回值:
  590. 返回排序后的诊断结果
  591. """
  592. print(f"STEP 5 start")
  593. start_time = time.time()
  594. diags = {}
  595. total_diags = 0
  596. for department in final_results.keys():
  597. department_factor = 0.1 if department == 'DEFAULT' else final_results[department]["score"]
  598. #当前科室权重增加0.1
  599. if input.department.value == department:
  600. #if '急诊医学科' == department:
  601. department_factor = department_factor * 1.001
  602. for disease, data in final_results[department]["diseases"]:
  603. total_diags += 1
  604. #key = 'disease_name_parent_' + disease
  605. # cached_data = self.cache.get(key)
  606. # if cached_data:
  607. # disease = cached_data
  608. if disease in diags.keys():
  609. diags[disease]["count"] += data["count"]
  610. diags[disease]["score"] += data["count"] * department_factor
  611. else:
  612. diags[disease] = {"count": data["count"], "score": data["count"] * department_factor}
  613. sorted_score_diags = sorted(diags.items(), key=lambda x: x[1]["score"], reverse=True)[:10]
  614. #循环sorted_score_diags,把disease现在是id:name替换name
  615. # diags = {}
  616. # for item in sorted_score_diags:
  617. # diseaseinfos = item[0].split(":")
  618. # diseaseId = diseaseinfos[0]
  619. # diseaseName = diseaseinfos[1]
  620. # diseaseScore = self.graph.nodes[int(diseaseId)].get('score', None)
  621. # if diseaseScore:
  622. # try:
  623. # # 创建新字典替换原元组
  624. # new_item = {"count": item[1]["count"], "score": float(diseaseScore)*0.1}
  625. # diags[diseaseName] = new_item
  626. # except (ValueError, TypeError):
  627. # # 如果转换失败,直接使用原元组
  628. # diags[diseaseName] = item[1]
  629. # else:
  630. # # 如果没有找到对应的score,直接使用原元组
  631. # diags[diseaseName] = item[1]
  632. # sorted_score_diags = sorted(diags.items(), key=lambda x: x[1]["score"], reverse=True)
  633. print(f"STEP 5 finished")
  634. end_time = time.time()
  635. print(f"STEP 5 执行完成,耗时:{end_time - start_time:.2f}秒")
  636. log_data = ["|department|disease|count|score"]
  637. log_data.append("|--|--|--|--|")
  638. for department in final_results.keys():
  639. diesease_data = final_results[department].get("diseases")
  640. count_data = final_results[department].get("count", 0)
  641. score_data = final_results[department].get("score", 0)
  642. log_data.append(f"|{department}|{diesease_data}|{count_data}|{score_data}|")
  643. print("这里是经过排序的数据\n" + "\n".join(log_data))
  644. return sorted_score_diags, total_diags