data_standardization.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import pandas as pd
  2. from data_initialize_standard.constant import *
  3. class DataStandardization(object):
  4. def __init__(self, data_dict):
  5. self.data_dict = data_dict
  6. def null_judge(self):
  7. Num = 0
  8. for k in self.data_dict:
  9. if self.data_dict[k] == ' ' or not self.data_dict[k]:
  10. Num += 1
  11. return Num
  12. def check_data(self):
  13. # 定义预期的数据结构
  14. missing_fields = []
  15. # 检查顶层字段缺失
  16. for top_key in ExpectedStructure:
  17. if top_key not in self.data_dict:
  18. missing_fields.append(top_key)
  19. # 检查子字段缺失
  20. if top_key in self.data_dict:
  21. current_data = self.data_dict[top_key]
  22. if isinstance(current_data, dict):
  23. expected_subkeys = ExpectedStructure[top_key]
  24. for sub_key in expected_subkeys:
  25. if sub_key not in current_data:
  26. missing_fields.append(f"{top_key}.{sub_key}")
  27. # 检查空值的递归函数
  28. def find_empty_fields(data, parent_path='', empty_fields=None):
  29. if empty_fields is None:
  30. empty_fields = set()
  31. if isinstance(data, dict):
  32. for key, value in data.items():
  33. new_path = f"{parent_path}.{key}" if parent_path else key
  34. # 检查当前值是否为空
  35. if value is None:
  36. empty_fields.add(new_path)
  37. elif isinstance(value, str) and value.strip() == '':
  38. empty_fields.add(new_path)
  39. elif isinstance(value, (list, dict)) and not value:
  40. empty_fields.add(new_path)
  41. # 递归检查子元素
  42. find_empty_fields(value, new_path, empty_fields)
  43. elif isinstance(data, list):
  44. if not data: # 空列表
  45. empty_fields.add(parent_path)
  46. else:
  47. # 检查字符串是否为空
  48. if isinstance(data, str) and data.strip() == '':
  49. empty_fields.add(parent_path)
  50. return empty_fields
  51. # 查找所有空值路径
  52. empty_paths = find_empty_fields(self.data_dict)
  53. return missing_fields, empty_paths
  54. def dict_standard(self):
  55. dict_chiller_cooling = self.data_dict['chillerConfigInfo']
  56. dict_chiller_outer = {}
  57. dict_code = self.data_dict['controlConfigInfo']
  58. all_chillers_info = self.data_dict['stationInfo']['allChillersInfo']
  59. all_chiller_water_temp_set = []
  60. all_water_temp_control_mode = []
  61. for i in range(len(all_chillers_info['chillerId'])):
  62. if all_chillers_info['runStatus'][i] == 1 and all_chillers_info['isInControl'][i] == 1:
  63. all_chiller_water_temp_set.append(all_chillers_info['chillerWaterTempSet'][i])
  64. if all_chillers_info['isInControl'][i] == 1:
  65. all_water_temp_control_mode.append(all_chillers_info['waterTempControlMode'][i])
  66. dict_chiller_cooling.update({
  67. 'triggerTime': self.data_dict['stationInfo']['triggerTime'],
  68. 'runMode': self.data_dict['stationInfo']['runMode'],
  69. 'runStatus': self.data_dict['chillerInfo']['runStatus'],
  70. 'chillerOffTimeLatest': self.data_dict['chillerInfo']['chillerOffTimeLatest'],
  71. 'offSetTempLatestCooling': self.data_dict['chillerInfo']['offSetTempLatestCooling'],
  72. # 'offSetTempInLatestCooling': 12,
  73. 'allChillerWaterTempSetOn': all_chiller_water_temp_set,
  74. # 'allChillerWaterTempInSetOn': [],
  75. 'allChillerControlSelect': all_water_temp_control_mode,
  76. # 'allChillerPowerRatio': [],
  77. 'chillerPowerRatio': self.data_dict['chillerInfo']['chillerPowerRatio'],
  78. 'chillerWaterTempOut': self.data_dict['chillerInfo']['chillerWaterTempOut'],
  79. 'chillerWaterTempIn': self.data_dict['chillerInfo']['chillerWaterTempIn'],
  80. 'chillerWaterTempSet': self.data_dict['chillerInfo']['chillerWaterTempSet'],
  81. 'chillerWaterTempSetInitial': self.data_dict['controlConfigInfo']['chillerWaterTempSetInitial'],
  82. 'chillerWaterTempSetUpper': self.data_dict['controlConfigInfo']['chillerWaterTempSetUpper'],
  83. # 'chillerWaterTempInSetUpper': self.data_dict,
  84. 'chillerWaterTempSetDown': self.data_dict['controlConfigInfo']['chillerWaterTempSetLower'],
  85. })
  86. dict_chiller_outer.update({
  87. 'triggerTime': self.data_dict['stationInfo']['triggerTime'],
  88. 'userName': self.data_dict['stationInfo']['userName'],
  89. 'coolingStationName': self.data_dict['stationInfo']['coolingStationName'],
  90. 'orgId': self.data_dict['stationInfo']['orgId'],
  91. 'chillerName': self.data_dict['chillerInfo']['chillerName'],
  92. 'chillerId': self.data_dict['chillerInfo']['chillerId'],
  93. 'stationId': self.data_dict['stationInfo']['stationId'],
  94. 'controllerId': self.data_dict['stationInfo']['controllerId'],
  95. 'allChillersInfo': self.data_dict['stationInfo']['allChillersInfo']})
  96. dict_code.update({'upControlTimeLatest': self.data_dict['chillerInfo']['upControlTimeLatest'],
  97. 'downControlTimeLatest': self.data_dict['chillerInfo']['downControlTimeLatest'],
  98. 'triggerTime': self.data_dict['stationInfo']['triggerTime'],
  99. 'runMode': self.data_dict['stationInfo']['runMode'],
  100. 'calMode': 0, # 监测点计算模式默认值为0
  101. 'allChillerControlSelect': all_water_temp_control_mode})
  102. return dict_chiller_cooling, dict_chiller_outer, dict_code
  103. def dict_safety(self):
  104. dict_upper_correct = {'refriType': self.data_dict['chillerInfo']['refriType'],
  105. 'chillerParameter': self.data_dict['chillerSafetyPara']}
  106. # dict_upper_correct.update({'refriType': self.data_dict['chillerInfo']['refriType'],
  107. # 'chillerParameter': self.data_dict['chillerSafetyPara']})
  108. return dict_upper_correct
  109. def data_multi_comp(self):
  110. dict_chiller_inner, dict_chiller_outer, dict_code = self.dict_standard()
  111. dict_upper_correct = self.dict_safety()
  112. data_temp_humi = pd.DataFrame(self.data_dict['terminalInfo'])
  113. return dict_chiller_inner, dict_chiller_outer, dict_code, dict_upper_correct, data_temp_humi