read_csv.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import json
  2. from pathlib import Path
  3. def read_config(file_path):
  4. with open(file_path, 'r', encoding='utf-8') as file:
  5. config = json.load(file)
  6. return config
  7. def calculate_hydraulic_fault_weights(faults, config):
  8. # 初始化总权值
  9. total_weight = 0
  10. if '液压故障' in faults:
  11. total_weight += config['液压故障']['weight']
  12. else:
  13. # 检查左液压子系统
  14. left_hydraulic_subsystem = config['左液压子系统']
  15. if any(fault in faults for fault in left_hydraulic_subsystem['faults']):
  16. total_weight += left_hydraulic_subsystem['weight']
  17. else:
  18. subsystem_total_weight = 0
  19. for subsystem_name, subsystem_data in left_hydraulic_subsystem['subsystems'].items():
  20. relevant_fault_indices = [i for i, fault in enumerate(subsystem_data['faults']) if fault in faults]
  21. if relevant_fault_indices:
  22. if subsystem_data.get('max_weight', False):
  23. max_weight = max(subsystem_data['weights'][i] for i in relevant_fault_indices)
  24. subsystem_total_weight = max(subsystem_total_weight, max_weight)
  25. else:
  26. # 特殊处理液压泵子系统的权重计算
  27. if '左液压泵超压' in faults:
  28. subsystem_total_weight += 0.2
  29. else:
  30. pump1_faults = ['液压泵1低压', '液压泵1传感器故障']
  31. pump3_faults = ['液压泵3低压', '液压泵3传感器故障']
  32. pump1_occurred = any(fault in faults for fault in pump1_faults)
  33. pump3_occurred = any(fault in faults for fault in pump3_faults)
  34. if pump1_occurred:
  35. subsystem_total_weight += 0.1
  36. if pump3_occurred:
  37. subsystem_total_weight += 0.1
  38. total_weight += subsystem_total_weight
  39. # 检查右液压子系统
  40. right_hydraulic_subsystem = config['右液压子系统']
  41. if any(fault in faults for fault in right_hydraulic_subsystem['faults']):
  42. total_weight += right_hydraulic_subsystem['weight']
  43. else:
  44. subsystem_total_weight = 0
  45. for subsystem_name, subsystem_data in right_hydraulic_subsystem['subsystems'].items():
  46. relevant_fault_indices = [i for i, fault in enumerate(subsystem_data['faults']) if fault in faults]
  47. if relevant_fault_indices:
  48. if subsystem_data.get('max_weight', False):
  49. max_weight = max(subsystem_data['weights'][i] for i in relevant_fault_indices)
  50. subsystem_total_weight = max(subsystem_total_weight, max_weight)
  51. else:
  52. subsystem_total_weight += sum(subsystem_data['weights'][i] for i in relevant_fault_indices)
  53. total_weight += subsystem_total_weight
  54. return total_weight
  55. # 示例用法
  56. if __name__ == "__main__":
  57. current_dir = Path(__file__).parent
  58. json_path = current_dir.parent / "config" / "score_config.json"
  59. config = read_config(json_path)
  60. faults_occurred = ["液压泵1低压", "液压泵1传感器故障"]
  61. total_weight = calculate_hydraulic_fault_weights(faults_occurred, config)
  62. print(f"Total Weight: {total_weight}")