validate.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 numpy as np
  8. import pandas as pd
  9. from cache.data_cleaning import key_field_row_cleaning
  10. from data_process import DataProcess
  11. from cache.limited_power import LimitPower
  12. class ValidationError(Exception):
  13. def __init__(self, message):
  14. self.msg = message
  15. class Validation(object):
  16. def __init__(self, log, args):
  17. self.logger = log
  18. self.args = args
  19. self.opt = args.parse_args_and_yaml()
  20. self.status = 1
  21. self.process = DataProcess(log, args)
  22. self.lp = LimitPower(log, args, None)
  23. def validate_nwp(self, nwp):
  24. # 0. 关键字段是否缺失
  25. nwp_cs = [x for x in nwp.columns.to_list() if x in self.opt.nwp_columns]
  26. if 'C_TIME' in nwp_cs:
  27. nwp_cs.pop(nwp_cs.index('C_TIME'))
  28. nwp_filter = key_field_row_cleaning(nwp, nwp_cs, self.logger)
  29. if len(nwp_filter) < 8:
  30. self.status = 2
  31. raise ValidationError("NWP数据缺失")
  32. nwp_filter['C_TIME'] = pd.to_datetime(nwp_filter['C_TIME'])
  33. return nwp_filter
  34. def validate_his_data(self, history_rp, env, history_dq):
  35. if len(history_rp) <= 1 and len(env) <= 1:
  36. self.status = 3
  37. raise ValidationError("鉴权失败-历史数据时刻不满足USTA_HISTORY个数")
  38. if len(history_dq) < self.opt.Model["his_points"]:
  39. self.status = 2
  40. raise ValidationError("history_dq缺失")
  41. his = pd.merge(history_rp, history_dq, on='C_TIME')
  42. his = pd.merge(env, his, on='C_TIME')
  43. if self.opt.usable_power['clean_power_which'] in range(1, 3):
  44. his_clean = key_field_row_cleaning(his, [self.opt.usable_power['env']], self.logger)
  45. else:
  46. his_clean = his
  47. his_clean = his_clean.replace(-99, np.nan)
  48. points = self.opt.Model['his_points']
  49. if len(his_clean) < points * 0.5:
  50. self.status = 2
  51. raise ValidationError("历史数据缺失,实际长度:{}".format(len(his_clean)))
  52. elif len(his_clean) < self.opt.Model["his_points"] or his_clean.isnull().any().any():
  53. his_clean.drop(columns=['C_FP_VALUE'], inplace=True)
  54. his = pd.merge(history_dq, his_clean, on='C_TIME', how='left')
  55. his.set_index('C_TIME', inplace=True)
  56. his = his.interpolate(method='linear')
  57. his = his.fillna(method='ffill')
  58. his = his.fillna(method='bfill')
  59. his.reset_index(drop=False, inplace=True)
  60. his['LIMIT_STATUS'] = np.where((his['LIMIT_STATUS'] != 0) & (his['LIMIT_STATUS'] != 1), 1, his['LIMIT_STATUS'])
  61. return his
  62. else:
  63. return his_clean
  64. def validate_authentic(self, nwp, his):
  65. # 验证当前天气是否经过鉴权,第一个点一定要是当天的
  66. predict_dt = nwp.iloc[0, :]["C_TIME"]
  67. history_dt = his.iloc[-1, :]["C_TIME"]
  68. if predict_dt - history_dt > pd.Timedelta(hours=4):
  69. self.status = 3
  70. raise ValidationError("鉴权失败-环境数据结束时间早于预测开始时间4小时")
  71. def validate_power(self, his):
  72. history_rp_env = his.copy()
  73. new_rp = []
  74. for index, row in history_rp_env.iterrows():
  75. zfs = row[self.opt.usable_power["env"]]
  76. rp = row['C_REAL_VALUE']
  77. able_rp = row['C_ABLE_VALUE']
  78. status = row['LIMIT_STATUS']
  79. # 以下三种情况用可用功率替代:
  80. # 1. api_able_power可用功率参数置为True
  81. # 2. 取消模型融合,解析接口数据填充
  82. # 3. 用样板机法或单独信号法清洗限电
  83. 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):
  84. new_rp.append(able_rp)
  85. # 否则用测光法和限电信号联合判断,任意一种限电就执行
  86. elif not self.lp.filter_unlimited_power(zfs, rp, self.opt.usable_power['k'], self.opt.usable_power['bias']) or status: # 任意判断限电
  87. new_rp.append(able_rp)
  88. else:
  89. new_rp.append(rp) # 全部为不限电
  90. history_rp_env["NEW_RP"] = new_rp
  91. return history_rp_env[['C_TIME', 'NEW_RP']]