emission_detect_model/models/lgb_predict.py

108 lines
4.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*-coding:utf-8 -*-
import lightgbm as lgb
import numpy as np
import pandas as pd
import json
import datetime as dt
def load_history_data(data_path:str):
data = pd.read_csv(data_path)
return data
def load_config(cfg_path:str):
with open(cfg_path, 'r', encoding='utf-8') as fr:
config = json.load(fr)
return config
def load_lgb_model(model_path:str):
return lgb.Booster(model_file=model_path)
def cal_CO2(coal_cost, ncv):
return coal_cost * ncv * 26.37e-3 * 0.94 * 44 / 12
def cal_coal_cost_emission(coal_cost, boiler, emission_factors):
factor = emission_factors.get(boiler)
if factor is not None:
return coal_cost * factor
else:
return emission_factors.get("default") * coal_cost
def cal_PM(c_smoke, boiler, emission_factors):
factor = emission_factors.get(boiler)
if factor is not None:
return c_smoke * factor / 1e6
else:
return emission_factors.get("default") * c_smoke / 1e6
def predict(his_data, input_data, model:lgb.Booster, object_cols, emission_factors):
feature_names = model.feature_name()
date = dt.datetime.strptime(input_data.get('time'), '%Y-%m-%d %H:%M:%S')
r_NOx = float(input_data.get('nox'))
r_SO2 = float(input_data.get('so2'))
r_smoke = float(input_data.get('smoke'))
flow = float(input_data.get('flow'))
c_NOx, c_SO2, c_smoke = flow * np.asarray([r_NOx, r_SO2, r_smoke])
caloric = float(input_data.get("caloric"))
if caloric > 1000:
caloric = caloric / 1000
inputs = {
"生产设备类型": input_data.get('boiler'),
"汽轮机类型": input_data.get('steam'),
"冷却方式": input_data.get('cold'),
"压力参数": input_data.get('pressure'),
"day_of_week": date.weekday(),
"month": date.month,
"hour": date.hour,
"0_r_NOx":np.log1p(float(r_NOx)),
"0_r_SO2":np.log1p(float(r_SO2)),
"0_r_smoke":np.log1p(float(r_smoke)),
"0_c_NOx": np.log1p(float(c_NOx)),
"0_c_SO2": np.log1p(float(c_SO2)),
"0_c_smoke": np.log1p(float(c_smoke)),
"0_flow": np.log1p(float(flow)),
"0_O2": np.log1p(float(input_data.get("o2"))),
"0_temp": np.log1p(float(input_data.get("temp"))),
"额定蒸发量_t/h": np.log1p(float(input_data.get("evaporation"))),
"低位发热量": np.log1p(caloric),
"单机容量MW": np.log1p(float(input_data.get("capacity"))),
"lon": np.log1p(float(input_data.get("lon"))),
"lat": np.log1p(float(input_data.get("lat"))),
}
new_df = pd.DataFrame.from_dict(inputs, orient='index').T
total_data = pd.concat([his_data, new_df])
new_inputs = pd.get_dummies(total_data, columns=object_cols)
new_inputs = new_inputs[feature_names].iloc[-1].values
coal_cost = np.expm1(model.predict([new_inputs])[0])
co = cal_coal_cost_emission(coal_cost, input_data.get('boiler'), emission_factors.get('co'))
vocs = cal_coal_cost_emission(coal_cost, input_data.get('boiler'), emission_factors.get('vocs'))
pm25 = cal_PM(c_smoke, input_data.get('boiler'), emission_factors.get('pm25'))
pm10 = cal_PM(c_smoke, input_data.get('boiler'), emission_factors.get('pm10'))
co2 = cal_CO2(coal_cost, caloric)
return {'coal': coal_cost, 'co':co, 'vocs':vocs, 'pm25':pm25, 'pm10':pm10, 'co2':co2}
if __name__ == '__main__':
history = load_history_data('../data/data_sample.csv')
object_cols = load_config('../config/object_cols.json')
model = load_lgb_model('../model_files/hour_best_model.txt')
emission_factors = load_config('../config/emission_factor.json')
inputs = {
"time": "2023-01-02 03:04:05",
"boiler": "循环流化床锅炉",
"steam": "凝气式",
"cold": "水冷-开式循环",
"pressure": "超超临界",
"nox": "12",
"so2":"0.15",
"smoke":"12",
"flow":"5000000",
"o2": "23",
"temp": "55",
"evaporation": "123",
"caloric": "23",
"capacity": "234",
"lon": "122",
"lat":"33",
}
print(predict(history, inputs, model, object_cols, emission_factors))