You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
204 lines
11 KiB
204 lines
11 KiB
2 weeks ago
|
import numpy as np
|
||
|
from numba import jit
|
||
|
import config
|
||
|
import json
|
||
|
import sys
|
||
|
import requests
|
||
|
import datetime
|
||
|
import jenkspy
|
||
|
import xlrd
|
||
|
import AANN_Fit
|
||
|
import traceback
|
||
|
|
||
|
@jit(nopython=True, cache=True)
|
||
|
def AANN_Fit(testdata, v1, v2, w1, w2):
|
||
|
"""
|
||
|
|
||
|
:param testdata:
|
||
|
:param model:
|
||
|
:return:
|
||
|
"""
|
||
|
y1 = 1/(1+np.exp(-np.dot(testdata, v1)))
|
||
|
out1 = np.dot(y1, w1)
|
||
|
y2 = 1/(1+np.exp(-np.dot(out1, v2)))
|
||
|
output = np.dot(y2, w2)
|
||
|
return output
|
||
|
|
||
|
def get_history_value(points,time,interval):
|
||
|
#url="http://192.168.1.201:8080/openPlant/getMultiplePointHistorys"
|
||
|
url=f"http://{config._EXA_IP}:9000/exawebapi/exatime/GetSamplingValueArrayFloat"
|
||
|
headers = {"Content-Type": "application/json;charset=utf-8"}#,"token":get_token()
|
||
|
point_array = points.split(",")
|
||
|
time_span = time.split(";")
|
||
|
value_array = []
|
||
|
for item in point_array:
|
||
|
value_group = []
|
||
|
for time_piece in time_span:
|
||
|
st = time_piece.split(",")[0]
|
||
|
et = time_piece.split(",")[1]
|
||
|
para = {"ItemName": item, "StartingTime": st, "TerminalTime": et, "SamplingPeriod": interval}
|
||
|
response = requests.get(url, headers=headers, params=para)
|
||
|
content = response.text.replace('"[','[').replace(']"',']')
|
||
|
value = json.loads(content)
|
||
|
if not isinstance(value, list):
|
||
|
print("aaa")
|
||
|
for row in value:
|
||
|
value_group.append(row[1])
|
||
|
value_array.append(value_group)
|
||
|
return np.transpose(np.array(value_array))
|
||
|
#return values
|
||
|
|
||
|
def isnumber(limits):
|
||
|
flag=True
|
||
|
for item in limits:
|
||
|
item=item.replace("-","")
|
||
|
if(item.isdigit()==False):
|
||
|
flag=False
|
||
|
break
|
||
|
return flag
|
||
|
|
||
|
def AANN_Test(model,Data):
|
||
|
v1 = np.array(model["v1"])
|
||
|
v2 = np.array(model["v2"])
|
||
|
w1 = np.array(model["w1"])
|
||
|
w2 = np.array(model["w2"])
|
||
|
maxdata = np.array(model["maxdata"])
|
||
|
mindata = np.array(model["mindata"])
|
||
|
mm, nn = Data.shape
|
||
|
# 预处理 数据归一化
|
||
|
mdata = (2 * Data - (maxdata + mindata + np.zeros((mm, nn)))) / (maxdata - mindata + np.zeros((mm, nn)))
|
||
|
reconData = AANN_Fit(mdata, v1, v2, w1, w2)
|
||
|
# 预处理 数据反归一化
|
||
|
a = maxdata - mindata + np.zeros((mm, nn))
|
||
|
b = maxdata + mindata + np.zeros((mm, nn))
|
||
|
reconData = (np.multiply(reconData, a) + b) / 2
|
||
|
|
||
|
res = {}
|
||
|
paraState = np.zeros([np.array(Data).shape[0], np.array(Data).shape[1]])
|
||
|
SPE_list = []
|
||
|
R = 0
|
||
|
errorData = Data - reconData # 偏差值
|
||
|
|
||
|
res["sampleData"]=np.transpose(np.array(Data)).tolist()
|
||
|
res["reconData"]=np.around(np.transpose(np.array(reconData)), decimals=3).tolist()
|
||
|
res["errorData"]=np.around(np.transpose(np.array(errorData)), decimals=3).tolist()
|
||
|
res["R"]=np.around(R, decimals=3).tolist()
|
||
|
res["SPE"]=np.around(np.transpose(np.array(SPE_list)), decimals=3).tolist()
|
||
|
res["paraState"]=np.transpose(np.array(paraState)).tolist()
|
||
|
|
||
|
return res
|
||
|
|
||
|
|
||
|
def clean_main(info):
|
||
|
try:
|
||
|
# datatype = info['type']
|
||
|
condition = info["condition"].replace("=", "==").replace(">=", ">").replace("<=", "<")
|
||
|
times = info["time"].split(';')
|
||
|
points = info["point"].split(',')
|
||
|
interval = 300000
|
||
|
dead = info["dead"].split(',')
|
||
|
limit = info["limit"].split(',')
|
||
|
uplower = info["uplow"].split(';')
|
||
|
res = json.loads(info["model"])
|
||
|
filename = res["Model_type"]
|
||
|
count = 0
|
||
|
ItemsInfo, SamplingTimePeriods = [], []
|
||
|
Constraint = ""
|
||
|
for i in range(len(points)):
|
||
|
iteminfo = {}
|
||
|
iteminfo["ItemName"] = points[i] # 加点
|
||
|
if (dead[i] == "1"): # 判断是否参与死区清洗
|
||
|
iteminfo["ClearDeadZone"] = "true"
|
||
|
else:
|
||
|
iteminfo["ClearDeadZone"] = "false"
|
||
|
if (limit[i] == "1"): # 参与上下限清洗
|
||
|
limits = uplower[i].split(',')
|
||
|
if (isnumber(limits) == True): # 输入上下限正确
|
||
|
count += 1
|
||
|
Constraint += "[" + points[i] + "]>" + limits[0] + " and " + "[" + points[i] + "]<" + limits[
|
||
|
1] + " and "
|
||
|
ItemsInfo.append(iteminfo)
|
||
|
if (count != 0):
|
||
|
Constraint = Constraint[:len(Constraint) - 4:]
|
||
|
else:
|
||
|
Constraint = "1==1" # 没有上下限清洗
|
||
|
Constraint += " and (" + condition + ")"
|
||
|
for i in range(len(times)):
|
||
|
Eachsampletime = {}
|
||
|
timess = times[i].split(',')
|
||
|
Eachsampletime["StartingTime"] = timess[0]
|
||
|
Eachsampletime["TerminalTime"] = timess[1]
|
||
|
SamplingTimePeriods.append(Eachsampletime)
|
||
|
Constraint = Constraint.replace("\n", " ")
|
||
|
url = f"http://{config._CLEAN_IP}/exawebapi/exatime/GetCleaningData?ItemsInfo=%s&SamplingTimePeriods=%s&Constraint=%s&SamplingPeriod=%s&DCount=6" % (
|
||
|
ItemsInfo, SamplingTimePeriods, Constraint, interval)
|
||
|
response = requests.get(url)
|
||
|
content = json.loads(response.text)
|
||
|
origndata = np.array([item for item in content["ClearData"]])
|
||
|
result = AANN_Test(res["Model_info"],origndata.T)
|
||
|
result["sampleData"] = origndata.tolist()
|
||
|
result["CleanOrNot"] = True
|
||
|
except Exception as e:
|
||
|
points = info['point']
|
||
|
time1 = info["time"]
|
||
|
interval = info['interval']
|
||
|
data_x, origndata = get_history_value(points, time1, interval)
|
||
|
res = json.loads(info["model"])
|
||
|
filename = res["Model_type"]
|
||
|
origndata = origndata.tolist()
|
||
|
result = AANN_Test(res["Model_info"],origndata.T)
|
||
|
result["sampleData"] = origndata
|
||
|
result["CleanOrNot"] = False
|
||
|
return result
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
# info = {"point":"DH4_40MAG20CT362,DH4_40MAG20AN002GT,DH4_40MAG20CE102,DH4_40MAG20CT312,DH4_40MAG20CT322,DH4_40MAG20CT332","dead":"1,1,1,1,1,1","condition":"[DH4_40MAG20CE102]>20","limit":"0,0,0,0,0,0","uplow":"null,null;null,null;null,null;null,null;null,null;null,null","time":"2021-06-02 17:37:17,2021-06-03 17:37:17","model":"{\"Model_info\":{\"Train_X_min\":[22.848,14.865,45.127,24.787,24.875,24.699],\"Train_X_max\":[49.215,54.841,185.917,80.635,81.084,80.097],\"Train_X_std\":[7.014,12.071,37.054,15.602,15.881,15.695],\"Train_X_mean\":[37.569,36.848,113.843,53.193,53.611,53.304],\"Train_X_bais_max\":[[0,0,0,0,0,0]],\"Train_X_bais_min\":[[0,0,0,0,0,0]],\"Train_X_bais_mean\":[[0,0,0,0,0,0]],\"QCUL_95_line\":[0,0,0,0,0,0],\"QCUL_99_line\":[0,0,0,0,0,0],\"count\":1957,\"maxdata\":[49.21523,54.841,185.917023,80.63535,81.08379,80.0973053],\"mindata\":[22.84761,14.864501,45.1272545,24.7868519,24.8750286,24.698679],\"sigma\":0.017395612757076084,\"v1\":[[0.05396457607128241,-1.1037970788364901,-0.9023458013688983,0.8166076266011788,-0.17679687427234053],[0.5200933810436045,0.007280858992126217,-0.5349408644184176,0.2845470690775108,-0.6855420074109138],[0.36869788692168753,0.022827023461346962,0.5064698648194299,-0.4703408850462729,-0.36541089496833906],[0.12892337610198143,0.8200230541433281,0.0447757887183383,0.1670333002651392,0.8993457054279524],[-0.2971657506583043,0.485522778322426,0.7944907999619857,-0.7713468714572788,-0.3803782724966992],[-0.24463129252472893,-0.8040155541706245,-0.4787131792272823,-0.6808617963702621,0.8823560072658708]],\"v2\":[[-0.624183704662755,-1.0451052181462246,-0.2814222311475065,-0.3354562640454462,2.000215994798745],[1.304013807858937,-1.2949290118426027,-1.274288771981608,1.3783276766624326,0.5918960339827718]],\"w1\":[[-1.804815936738639,-1.177820457498696],[-0.26858615380810363,0.942696914999828],[0.045393854822685514,1.0175755864672742],[0.9657639036369005,0.8851033322191303],[0.9882029320129792,-1.731780185362415]],\"w2\":[[-1.2783993770868123,0.12079935279430898,0.5404098532155389,-0.7614378857555226,-0.8806415399208807,-1.1425373462620356],[1.1519307867425934,1.7851341509876484,1.4232979025735768,1.9335508578138167,1.2388319720461638,1.2688802766586058],[1.0836842481718094,0.24511103285668004,0.19049263794782964,0.26731984115471025,1.0669397542018493,1.067090986764354],[-1.1907723532803773,0.19092261395624277,-0.17818889703150334,-1.2393336475691328,-0.9319272349811759,-0.7091295825768392],[0.2667338682884674,-2.3674951959521846,-2.19524658584935,-0.3717940912566747,-0.6570853336446724,-0.6287452290848574]]},\"Model_type\":\"AANN\",\"BeforeCleanSamNum\":2143,\"AfterCleanSamNum\":2067,\"CleanOrNot\":true}","interval":300000}
|
||
|
# result = clean_main(info)
|
||
|
# print('aaa')
|
||
|
model={
|
||
|
"Train_X_min": [ 20.821, 14.869, 43.163, 21.262, 21.35, 21.262 ],
|
||
|
"Train_X_max": [ 49.126, 49.925, 169.502, 77.588, 78.394, 77.856 ],
|
||
|
"Train_X_std": [ 7.125, 10.472, 31.807, 14.634, 14.847, 14.718 ],
|
||
|
"Train_X_mean": [ 36.205, 36.429, 111.547, 50.583, 50.955, 50.672 ],
|
||
|
"Train_X_bais_max": [ [ 0, 0, 0, 0, 0, 0 ] ],
|
||
|
"Train_X_bais_min": [ [ 0, 0, 0, 0, 0, 0 ] ],
|
||
|
"Train_X_bais_mean": [ [ 0, 0, 0, 0, 0, 0 ] ],
|
||
|
"QCUL_95_line": [ 0, 0, 0, 0, 0, 0 ],
|
||
|
"QCUL_99_line": [ 0, 0, 0, 0, 0, 0 ],
|
||
|
"r2": [ 0.9868677166895696, 0.9972168348807384, 0.994155641760367, 0.997452401678795, 0.9972480237895449, 0.9973549357761101 ],
|
||
|
"count": 1837,
|
||
|
"maxdata": [ 49.1263924, 49.92462, 169.501648, 77.58756, 78.39406, 77.85637 ],
|
||
|
"mindata": [ 20.8214855, 14.86908, 43.1634521, 21.261837, 21.3499146, 21.261837 ],
|
||
|
"sigma": 0.01806189580260563,
|
||
|
"v1": [
|
||
|
[ -0.23316133948572393, -0.9217914025920456, -0.07554897165584737, 0.5760860199806274, -1.0764354048741178 ],
|
||
|
[ 0.6269288587688561, 0.2873108121988404, -0.4522541335965181, 0.1976888146807624, -0.06952149372511203 ],
|
||
|
[ 0.1787240198568913, 0.19306067329316998, -0.6544931571469833, -0.7615224968711883, 0.34011752017824165 ],
|
||
|
[ 0.018938620699273264, 0.730637377526106, 0.4933076792373918, 0.6451509410317868, 0.053819869986792335 ],
|
||
|
[ -0.19609562960367105, 0.11172696177830604, -0.1584273023561399, 0.331300527227796, 0.5888365193680527 ],
|
||
|
[ 0.06189724560442225, -0.8971329436091366, 0.5277390828634656, -0.5983529844040149, 0.7979948968923328 ]
|
||
|
],
|
||
|
"v2": [
|
||
|
[ 1.2232098033189849, -1.6743334472355316, 0.801596559892827, 1.1379819603344574, 0.6490732883102263 ],
|
||
|
[ 1.0900875026570802, -0.9667042438318552, -0.6416000023658467, -0.506978913609041, 1.6559592831593986 ]
|
||
|
],
|
||
|
"w1": [
|
||
|
[ -1.330531486686979, 0.2236374553075324 ],
|
||
|
[ 0.7317719867861042, 2.0240829271774823 ],
|
||
|
[ 1.9002236694443064, -1.1102667989668475 ],
|
||
|
[ -0.18577976434243573, -0.5562627341260915 ],
|
||
|
[ -1.0437074026078084, -0.7558997859191849 ]
|
||
|
],
|
||
|
"w2": [
|
||
|
[ -1.3748396962059533, -0.4858035766258788, -0.8013045354908804, -0.6155000036590996, -0.48891412875006274, -1.0627377754183602 ],
|
||
|
[ 1.4595863114619005, 2.68853721134863, 2.238122625694176, 1.9127830289008108, 1.9551336244607311, 1.8698425061744566 ],
|
||
|
[ 0.5758092226574661, -0.4237580862942265, -1.4401060479517787, 0.28527357176477214, 0.1858972429240083, 0.054877840090252594 ],
|
||
|
[ 0.7197290283109676, -1.73044863841078, -0.4906062165572168, -0.19012584014090636, -0.18883049934775636, 0.22915594950041754 ],
|
||
|
[ -1.4023393826788018, 0.16500469061488093, 0.4718410186558856, -1.5843596374153093, -1.6622678974964407, -1.2898135647821567 ]
|
||
|
]
|
||
|
}
|
||
|
Data=np.array([[24.25785,22.9852276,71.4623947,26.1979523,26.2861671,26.2861671]])
|
||
|
result = AANN_Test(model, Data)
|
||
|
print('aaa')
|