|
@@ -10,6 +10,9 @@ from common.alert import send_message
|
|
|
from datetime import datetime, timedelta
|
|
|
import pytz
|
|
|
from pytz import timezone
|
|
|
+
|
|
|
+from common.processing_data_common import get_xxl_dq
|
|
|
+
|
|
|
app = Flask('model_prediction_lightgbm——service')
|
|
|
def str_to_list(arg):
|
|
|
if arg == '':
|
|
@@ -20,25 +23,25 @@ def str_to_list(arg):
|
|
|
|
|
|
def forecast_data_distribution(pre_data,args):
|
|
|
col_time = args['col_time']
|
|
|
-
|
|
|
- # tomorrow = (date.today() + timedelta(days=1)).strftime('%Y-%m-%d')
|
|
|
+ farm_id = args['farmId']
|
|
|
+ dt = datetime.now(pytz.utc).astimezone(timezone("Asia/Shanghai")).strftime('%Y%m%d')
|
|
|
tomorrow = (datetime.now(pytz.utc).astimezone(timezone("Asia/Shanghai")) + timedelta(days=1)).strftime('%Y-%m-%d')
|
|
|
- field_mapping = {'clearsky_ghi': 'clearskyGhi', 'dni_calcd': 'dniCalcd','surface_pressure':'surfacePressure'}
|
|
|
+ field_mapping = {'clearsky_ghi': 'clearskyGhi', 'dni_calcd': 'dniCalcd','surface_pressure':'surfacePressure',
|
|
|
+ 'wd140m': 'tj_wd140','ws140m': 'tj_ws140','wd170m': 'tj_wd170','cldt': 'tj_tcc','wd70m': 'tj_wd70',
|
|
|
+ 'ws100m': 'tj_ws100','DSWRFsfc': 'tj_radiation','wd10m': 'tj_wd10','TMP2m': 'tj_t2','wd30m': 'tj_wd30',
|
|
|
+ 'ws30m': 'tj_ws30','rh2m': 'tj_rh','PRATEsfc': 'tj_pratesfc','ws170m': 'tj_ws170','wd50m': 'tj_wd50',
|
|
|
+ 'ws50m': 'tj_ws50','wd100m': 'tj_wd100','ws70m': 'tj_ws70','ws10m': 'tj_ws10','psz': 'tj_pressure',
|
|
|
+ 'cldl': 'tj_lcc','pres': 'tj_pres','dateTime':'date_time'}
|
|
|
# 根据字段映射重命名列
|
|
|
pre_data = pre_data.rename(columns=field_mapping)
|
|
|
|
|
|
if len(pre_data)==0:
|
|
|
- send_message('lightgbm预测组件', args['farmId'], '请注意:获取NWP数据为空,预测文件无法生成!')
|
|
|
- result = pd.DataFrame({'farm_id':[], col_time:[], 'power_forecast':[]})
|
|
|
+ send_message('lightgbm预测组件', farm_id, '请注意:获取NWP数据为空,预测文件无法生成!')
|
|
|
+ result = get_xxl_dq(farm_id, dt)
|
|
|
|
|
|
elif len(pre_data[pre_data[col_time].str.contains(tomorrow)])<96:
|
|
|
- send_message('lightgbm预测组件', args['farmId'], "日前数据记录缺失,不足96条,用DQ代替并补值!")
|
|
|
- start_time = pre_data[col_time].min()
|
|
|
- end_time = pre_data[col_time].max()
|
|
|
- date_range = pd.date_range(start=start_time, end=end_time, freq='15T').strftime('%Y-%m-%d %H:%M:%S').tolist()
|
|
|
- df_date = pd.DataFrame({col_time:date_range})
|
|
|
- result = pd.merge(df_date,pre_data,how='left',on=col_time).sort_values(by=col_time).fillna(method='ffill').fillna(method='bfill')
|
|
|
- result = result[['farm_id', 'date_time', 'power_forecast']]
|
|
|
+ send_message('lightgbm预测组件', farm_id, "日前数据记录缺失,不足96条,用DQ代替并补值!")
|
|
|
+ result = get_xxl_dq(farm_id, dt)
|
|
|
else:
|
|
|
df = pre_data.sort_values(by=col_time).fillna(method='ffill').fillna(method='bfill')
|
|
|
mongodb_connection, mongodb_database, mongodb_model_table, model_name = "mongodb://root:sdhjfREWFWEF23e@192.168.1.43:30000/", \
|
|
@@ -53,19 +56,18 @@ def forecast_data_distribution(pre_data,args):
|
|
|
model = pickle.loads(model_binary)
|
|
|
diff = set(model.feature_name()) - set(pre_data.columns)
|
|
|
if len(diff) > 0:
|
|
|
- send_message('lightgbm预测组件', args['farmId'], f'NWP特征列缺失,使用DQ代替!features:{diff}')
|
|
|
- result = pre_data[['farm_id', 'date_time', 'power_forecast']]
|
|
|
+ send_message('lightgbm预测组件', farm_id, f'NWP特征列缺失,使用DQ代替!features:{diff}')
|
|
|
+ result = get_xxl_dq(farm_id, dt)
|
|
|
else:
|
|
|
df['power_forecast'] = model.predict(df[model.feature_name()])
|
|
|
df.loc[df['power_forecast'] < 0, 'power_forecast'] = 0
|
|
|
- # 添加小时列 把光夜间置为0
|
|
|
- df["hour"] = pd.to_datetime(df["date_time"]).dt.hour
|
|
|
- df.loc[(df["hour"] >= 20) | (df["hour"] < 6), 'power_forecast'] = 0
|
|
|
print("model predict result successfully!")
|
|
|
+ if 'farm_id' not in df.columns:
|
|
|
+ df['farm_id'] = farm_id
|
|
|
result = df[['farm_id', 'date_time', 'power_forecast']]
|
|
|
else:
|
|
|
- send_message('lightgbm预测组件', args['farmId'], "日前数据记录缺失,不足96条,用DQ代替并补值!")
|
|
|
- result = pre_data[['farm_id', 'date_time', 'power_forecast']]
|
|
|
+ send_message('lightgbm预测组件', farm_id, "模型文件缺失,用DQ代替并补值!")
|
|
|
+ result = get_xxl_dq(farm_id, dt)
|
|
|
result['power_forecast'] = round(result['power_forecast'],2)
|
|
|
return result
|
|
|
|