validate.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # time: 2023/11/21 13:33
  4. # file: validate.py
  5. # author: David
  6. # company: shenyang JY
  7. import random
  8. from cache.limited_power_curve import LimitPower
  9. import pandas as pd
  10. import numpy as np
  11. np.random.seed(42)
  12. import pickle, os
  13. from cache.data_cleaning import key_field_row_cleaning, cleaning
  14. current_path = os.path.dirname(__file__)
  15. def readVar(path):
  16. if os.path.exists(path):
  17. with open(path, "rb") as file:
  18. var = pickle.load(file)
  19. return var
  20. else:
  21. return None
  22. class ValidationError(Exception):
  23. def __init__(self, message):
  24. self.msg = message
  25. class Validation(object):
  26. glob_rp = readVar(current_path + '/var/glob_rp.pickle')
  27. def __init__(self, log, args):
  28. self.logger = log
  29. self.args = args
  30. self.opt = args.parse_args_and_yaml()
  31. self.status = 1
  32. self.lp = LimitPower(log, args, None)
  33. @classmethod
  34. def set_lp(cls):
  35. cls.glob_rp = readVar(current_path + '/var/glob_rp.pickle')
  36. def validate_nwp(self, nwp):
  37. nwp_cs = [x for x in nwp.columns.to_list() if x in self.opt.nwp_columns]
  38. if 'C_TIME' in nwp_cs:
  39. nwp_cs.pop(nwp_cs.index('C_TIME'))
  40. nwp_filter = key_field_row_cleaning(nwp, nwp_cs, self.logger)
  41. if len(nwp_filter) < 8:
  42. self.status = 2
  43. raise ValidationError("NWP数据缺失")
  44. nwp_filter['C_TIME'] = pd.to_datetime(nwp_filter['C_TIME'])
  45. return nwp_filter
  46. def validate_his_data(self, history_rp, env, history_dq):
  47. if len(history_rp) <= 1 and len(env) <= 1:
  48. self.status = 3
  49. raise ValidationError("鉴权失败-历史数据时刻不满足USTA_HISTORY个数")
  50. if len(history_dq) < self.opt.Model["his_points"]:
  51. self.status = 2
  52. raise ValidationError("history_dq缺失")
  53. his = pd.merge(history_rp, history_dq, on='C_TIME')
  54. his = pd.merge(env, his, on='C_TIME')
  55. if self.opt.usable_power['clean_power_which'] in range(1, 3):
  56. his_clean = key_field_row_cleaning(his, [self.opt.usable_power['env']], self.logger)
  57. else:
  58. his_clean = his
  59. his_clean = his_clean.replace(-99, np.nan)
  60. points = self.opt.Model['his_points']
  61. if len(his_clean) < points * 0.5:
  62. self.status = 2
  63. raise ValidationError("历史数据缺失,实际长度:{}".format(len(his_clean)))
  64. elif len(his_clean) < self.opt.Model["his_points"] or his_clean.isnull().any().any():
  65. his_clean.drop(columns=['C_FP_VALUE'], inplace=True)
  66. his = pd.merge(history_dq, his_clean, on='C_TIME', how='left')
  67. his.set_index('C_TIME', inplace=True)
  68. his = his.interpolate(method='linear')
  69. his = his.fillna(method='ffill')
  70. his = his.fillna(method='bfill')
  71. his.reset_index(drop=False, inplace=True)
  72. his['LIMIT_STATUS'] = np.where((his['LIMIT_STATUS'] != 0) & (his['LIMIT_STATUS'] != 1), 1, his['LIMIT_STATUS'])
  73. return his
  74. else:
  75. return his_clean
  76. def validate_authentic(self, nwp, his):
  77. # 验证当前天气是否经过鉴权,第一个点一定要是当天的
  78. predict_dt = nwp.iloc[0, :]["C_TIME"]
  79. history_dt = his.iloc[-1, :]["C_TIME"]
  80. if predict_dt - history_dt > pd.Timedelta(hours=4):
  81. self.status = 3
  82. raise ValidationError("鉴权失败-环境数据结束时间早于预测开始时间4小时")
  83. def validate_power(self, his):
  84. self.lp.mapping_relation(self.glob_rp)
  85. history_rp_ws = his.copy()
  86. new_rp = []
  87. for index, row in history_rp_ws.iterrows():
  88. zfs = row[self.opt.usable_power["env"]]
  89. rp = row['C_REAL_VALUE']
  90. able_rp = row['C_ABLE_VALUE']
  91. status = row['LIMIT_STATUS'] # 限电是1,不限电是0
  92. # 以下三种情况用可用功率替代:
  93. # 1. api_able_power可用功率参数置为True
  94. # 2. 取消模型融合,解析接口数据填充
  95. # 3. 用样板机法或单独信号法清洗限电
  96. if self.opt.usable_power['api_able_power'] is True or self.opt.Model['fusion'] is False or self.opt.usable_power['clean_power_which'] not in range(1, 3):
  97. new_rp.append(able_rp)
  98. elif not self.lp.filter_unlimited_power(zfs, rp) or status: # 任意判断限电
  99. new_rp.append(able_rp)
  100. else:
  101. new_rp.append(rp) # 全部为不限电
  102. history_rp_ws["NEW_RP"] = new_rp
  103. return history_rp_ws[['C_TIME', 'NEW_RP']]