initial code

This commit is contained in:
赵敬皓 2023-06-26 13:49:10 +08:00
commit 770ce7fff9
10 changed files with 44877 additions and 0 deletions

10
Dockerfile Normal file
View File

@ -0,0 +1,10 @@
FROM python:3.7
WORKDIR /app
ADD ./ /app/
RUN pip install --upgrade pip -i https://pypi.douban.com/simple/ --no-cache-dir
RUN pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple --no-cache-dir
CMD ["python3", "run.py"]

0
README.md Normal file
View File

View File

@ -0,0 +1,30 @@
{
"CO":{
"煤粉": 2,
"循环流化床": 2,
"自动炉排层燃炉": 15,
"手动炉排层燃炉 ": 124.3,
"default": 2
},
"VOCs":{
"煤粉": 2.16,
"循环流化床": 2.16,
"自动炉排层燃炉": 2.16,
"手动炉排层燃炉 ": 4.53,
"default": 2.16
},
"PM25":{
"煤粉": 0.06,
"循环流化床": 0.07,
"自动炉排层燃炉": 0.1,
"手动炉排层燃炉 ": 0.07,
"default": 0.06
},
"PM10":{
"煤粉": 0.23,
"循环流化床": 0.29,
"自动炉排层燃炉": 0.3,
"手动炉排层燃炉 ": 0.2,
"default": 0.23
}
}

1
config/object_cols.json Normal file
View File

@ -0,0 +1 @@
["\u751f\u4ea7\u8bbe\u5907\u7c7b\u578b", "\u6c7d\u8f6e\u673a\u7c7b\u578b", "\u51b7\u5374\u65b9\u5f0f", "\u538b\u529b\u53c2\u6570", "day_of_week", "month", "hour"]

6481
data/data_sample.csv Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

Binary file not shown.

108
models/lgb_predict.py Normal file
View File

@ -0,0 +1,108 @@
# -*-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(r_smoke, boiler, emission_factors):
factor = emission_factors.get(boiler)
if factor is not None:
return r_smoke * factor
else:
return emission_factors.get("default") * r_smoke
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(r_smoke, input_data.get('boiler'), emission_factors.get('PM25'))
pm10 = cal_PM(r_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))

5
requirements.txt Normal file
View File

@ -0,0 +1,5 @@
lightgbm==3.2.1
numpy==1.19.5
pandas==1.3.5
logzero==1.7.0
Flask==2.1.0

43
run.py Normal file
View File

@ -0,0 +1,43 @@
# -*-coding:utf-8-*-
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
import json
from flask import Flask, request, make_response
from logzero import logger
current_path = os.path.dirname(os.path.abspath(__file__)) # for local
# current_path = "/app" # for docker
logger.info(f"{current_path}")
from models.lgb_predict import load_config, load_history_data, load_lgb_model, predict
lgb_model = load_lgb_model(model_path=f"{current_path}/model_files/hour_best_model.txt")
object_cols = load_config(f"{current_path}/config/object_cols.json")
history_data = load_history_data(data_path=f"{current_path}/data/data_sample.csv")
emission_factors = load_config(f"{current_path}/config/emission_factor.json")
app = Flask(__name__)
@app.route('/emission/', methods=["POST"])
def run_case_check():
resp_info = dict()
if request.method == "POST":
data = request.json.get('data')
logger.info(data)
if data is not None and len(data) != 0:
rst = predict(history_data, data, lgb_model, object_cols, emission_factors)
resp_info["code"] = 200
resp_info["data"] = rst
else:
resp_info["msg"] = "Input is None, please check !"
resp_info["code"] = 406
resp = make_response(json.dumps(resp_info))
resp.status_code = 200
return resp
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8788, debug=False)