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.
222 lines
9.9 KiB
222 lines
9.9 KiB
2 weeks ago
|
#!/usr/bin/python3.6.5
|
||
|
# -*- coding: utf-8 -*-
|
||
|
# Time : 2021/5/2423:03
|
||
|
# Author : LiFan,HeKang,RenShaojun
|
||
|
# Github : https://github.com/JustKeepSilence
|
||
|
|
||
|
import time
|
||
|
import numpy as np
|
||
|
import pandas as pd
|
||
|
from scipy.stats import norm
|
||
|
from scipy.stats.distributions import chi2
|
||
|
import json
|
||
|
from json import JSONDecodeError
|
||
|
import sys
|
||
|
import traceback
|
||
|
import pymssql
|
||
|
import requests
|
||
|
import datetime
|
||
|
from scipy.stats import norm
|
||
|
from scipy.stats import f
|
||
|
from scipy.stats import chi2
|
||
|
import jenkspy
|
||
|
# import pca_train_k
|
||
|
import pcamtcltest_k
|
||
|
# import model_performance
|
||
|
import xlrd
|
||
|
import random
|
||
|
from pylab import mpl
|
||
|
import matplotlib.pyplot as plt
|
||
|
from collections import Counter
|
||
|
from operator import itemgetter
|
||
|
import PCA_Test
|
||
|
import config
|
||
|
from recon import Lars, recon_fault_diagnosis_r, recon_fault_diagnosis_r_l, recon_fault_diagnosis_r_c
|
||
|
from AANN_SF_SBSRB import AANN_SF_SBSRB
|
||
|
import scipy.io as scio
|
||
|
|
||
|
|
||
|
def aann(model, LockVariable,Data_origin,number_sample, number_fault_variable,
|
||
|
low_f,high_f,Test_type,Limit_value, time_data, spe_recon,expand=True, sort_data=False):
|
||
|
|
||
|
low_f=np.array(low_f)
|
||
|
high_f=np.array(high_f)
|
||
|
yy = list(range(0, Data_origin.shape[0]))
|
||
|
time_sample = []
|
||
|
if expand:
|
||
|
if len(yy) < 1000:
|
||
|
random_row = np.array(list(range(len(yy))))
|
||
|
else:
|
||
|
random_row = np.array(random.sample(yy, number_sample))
|
||
|
time_original = time_data[random_row, :]
|
||
|
Data_origin = Data_origin[random_row, :] # 扩充选择1000个样�?
|
||
|
else:#这个是什么意思,问一下何康
|
||
|
random_row = np.array(random.sample(yy, number_sample))
|
||
|
time_original = time_data[random_row, :]
|
||
|
Data_origin = Data_origin[random_row, :] # 扩充选择random_row个样�?
|
||
|
|
||
|
v1 = np.array(model["v1"])
|
||
|
v2 = np.array(model["v2"])
|
||
|
w1 = np.array(model["w1"])
|
||
|
w2 = np.array(model["w2"])
|
||
|
sigma = model["sigma"]
|
||
|
mindata = model["mindata"]
|
||
|
maxdata = model["maxdata"]
|
||
|
mm, nn = Data_origin.shape
|
||
|
fault_magnitude = []
|
||
|
for i in range(mm):
|
||
|
magnitude=[]
|
||
|
for j in range(nn):
|
||
|
magnitude.append(np.random.uniform(low_f[j], high_f[j])) # 均匀分布
|
||
|
fault_magnitude.append(magnitude)
|
||
|
fault_magnitude=np.array(fault_magnitude)
|
||
|
fault_sample = Data_origin+ fault_magnitude
|
||
|
# scio.savemat('fault_sample.mat', {'A': fault_sample})
|
||
|
delt_ff,fdr,far,fdr_variable,far_variable,Reconstruction_precision,spe,ReconRes = AANN_SF_SBSRB(fault_sample, fault_magnitude, v1, v2, w1, w2, sigma, mindata, maxdata)
|
||
|
|
||
|
max_recon = ""
|
||
|
time_stamp = time_original.T.tolist()[0]
|
||
|
train_mean = model["Train_X_mean"]
|
||
|
train_std = model["Train_X_std"]
|
||
|
|
||
|
items = [
|
||
|
('fdr', np.around(fdr, decimals=3).tolist()),
|
||
|
('far', np.around(far, decimals=3).tolist()),
|
||
|
('fdr_variable', np.around(fdr_variable, decimals=3).tolist()),
|
||
|
('far_variable', np.around(far_variable, decimals=3).tolist()),
|
||
|
('Reconstruction_precision', np.around(Reconstruction_precision, decimals=3).tolist()),
|
||
|
("error_data", delt_ff),
|
||
|
("test_data", fault_sample),
|
||
|
("ReconRes", ReconRes),
|
||
|
("original_line", spe),
|
||
|
("time_stamp", time_stamp),
|
||
|
("max_recon", max_recon),
|
||
|
("mean", train_mean),
|
||
|
("std", train_std)
|
||
|
]
|
||
|
result = dict(items)
|
||
|
return result
|
||
|
|
||
|
def isnumber(limits):
|
||
|
flag = True
|
||
|
for item in limits:
|
||
|
item = item.replace("-","")
|
||
|
if not item.isdigit():
|
||
|
flag = False
|
||
|
break
|
||
|
return flag
|
||
|
|
||
|
def main(info):
|
||
|
try:
|
||
|
ItemsInfo, SamplingTimePeriods = [], []
|
||
|
model_id = info["Model_id"]
|
||
|
version = info["version"]
|
||
|
if version == "v-test":
|
||
|
res = PCA_Test.get_model_by_ID(model_id)
|
||
|
else:
|
||
|
res = PCA_Test.get_model_by_id_and_version(model_id, version)
|
||
|
lock = []
|
||
|
point_info = res["pointInfo"]
|
||
|
interval = info["Test_Data"]["interval"]
|
||
|
for i in range(len(point_info)):
|
||
|
try:
|
||
|
if point_info[i]["lock"]:
|
||
|
lock.append(i)
|
||
|
except:
|
||
|
continue
|
||
|
Test_Data = info["Test_Data"]
|
||
|
points = Test_Data["points"].split(",")
|
||
|
try:
|
||
|
times = Test_Data["time"].split(";")
|
||
|
for i in range(len(times)):
|
||
|
Eachsampletime = {}
|
||
|
timess = times[i].split(',')
|
||
|
Eachsampletime["StartingTime"] = timess[0]
|
||
|
Eachsampletime["TerminalTime"] = timess[1]
|
||
|
SamplingTimePeriods.append(Eachsampletime)
|
||
|
except KeyError:
|
||
|
SamplingTimePeriods = [{"StartingTime": item["st"],
|
||
|
"TerminalTime": item["et"]} for item in res["trainTime"]]
|
||
|
model = res["para"]["Model_info"]
|
||
|
condition = info["condition"].replace("=", "==").replace(">=", ">").replace("<=", "<")
|
||
|
dead = info["dead"].split(',')
|
||
|
limit = info["limit"].split(',')
|
||
|
uplower = info["uplow"].split(';')
|
||
|
count = 0
|
||
|
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): # 输入上下限正�?
|
||
|
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 + ")"
|
||
|
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)
|
||
|
data = np.array([item for item in content["ClearData"]]).T # test data
|
||
|
time_data = np.array([item for item in content["TimeData"]]).reshape(data.shape[0], -1)
|
||
|
test_type = info["Test_Type"]
|
||
|
limit_value = info["Limit_Value"]
|
||
|
number_sample = info["number_sample"]
|
||
|
try:
|
||
|
expand = info["expand"]
|
||
|
# sort_data = info["sort_data"]
|
||
|
except KeyError:
|
||
|
expand = True
|
||
|
# sort_data = True
|
||
|
spe_recon = False
|
||
|
far, fdr, amplitude = [], [], []
|
||
|
recon = []
|
||
|
result = {}
|
||
|
for _, ass in enumerate(info["assess"]):
|
||
|
number_fault_variable = ass["number_fault_variable"]
|
||
|
low_f = [float(item) for item in ass["low_f"].split(',')]
|
||
|
high_f = [float(item) for item in ass["high_f"].split(',')]
|
||
|
|
||
|
result_temp = aann(model, lock, data, number_sample, number_fault_variable, low_f, high_f, test_type,
|
||
|
limit_value, time_data, spe_recon, expand)
|
||
|
far.append(result_temp["far"])
|
||
|
fdr.append(result_temp["fdr"])
|
||
|
recon.append(result_temp["Reconstruction_precision"])
|
||
|
t_index = np.nonzero(high_f)[0][0]#索引high_f中第一个非0数位置的行数;在矩阵的第3行,t_index == 2
|
||
|
amplitude.append(high_f[t_index])
|
||
|
try:
|
||
|
index = Test_Data["time"].index(",")
|
||
|
result["time"] = Test_Data["time"][:index:]
|
||
|
except KeyError:
|
||
|
result["time"] = sorted(res["trainTime"], key=lambda item: item["st"])[0]["st"]
|
||
|
result["BeforeCleanSamNum"] = content["BeforeCleanSamNum"]
|
||
|
result["AfterCleanSamNum"] = content["AfterCleanSamNum"]
|
||
|
result["amplitude"] = amplitude
|
||
|
result["CleanOrNot"] = True
|
||
|
result["far_variable"] = far
|
||
|
result["fdr_variable"] = fdr
|
||
|
result["Reconstruction_precision"] = recon
|
||
|
return result
|
||
|
except Exception as e:
|
||
|
msg = traceback.format_exc()
|
||
|
result = [{"CleanOrNot": False, 'msg': msg}]
|
||
|
return result #
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
#info_str = {"expand":True,"Test_Data":{"time":"2021-03-17 18:01:14,2021-05-21 17:28:52","points":"DH4_DEA-LVL,DH4_40LAA10CL101,DH4_40LAA10CL102","interval":300000},"dead":"1,1,1","limit":"0,0,0","Model_id":"1161","number_sample":1000,"assess":[{"number_fault_variable":1,"low_f":"-31.16,0,0","high_f":"-31.16,0,0"},{"number_fault_variable":1,"low_f":"0,-31.11,0","high_f":"0,-31.11,0"},{"number_fault_variable":1,"low_f":"0,0,-33.22","high_f":"0,0,-33.22"}],"condition":"1=1","Test_Type":"FAI","Limit_Value":1.353,"uplow":"null,null;null,null;null,null","number":0,"version":"v-test"}
|
||
|
info_str = {"expand":True,"Test_Data":{"time":"2020-08-30 19:03:10,2020-09-04 12:02:16;2020-09-05 22:00:33,2020-09-08 15:35:07","points":"DH4_40MAG20CT362,DH4_40MAG20AN002GT,DH4_40MAG20CE102,DH4_40MAG20CT312,DH4_40MAG20CT322,DH4_40MAG20CT332","interval":300000},"dead":"1,1,1,1,1,1","limit":"0,0,0,0,0,0","Model_id":"1274","Model_alg":"AANN","number_sample":1000,"assess":[{"number_fault_variable":1,"low_f":"-3.96,0,0,0,0,0","high_f":"-3.96,0,0,0,0,0"},{"number_fault_variable":1,"low_f":"0,-6.00,0,0,0,0","high_f":"0,-6.00,0,0,0,0"},{"number_fault_variable":1,"low_f":"0,0,-21.12,0,0,0","high_f":"0,0,-21.12,0,0,0"},{"number_fault_variable":1,"low_f":"0,0,0,-8.38,0,0","high_f":"0,0,0,-8.38,0,0"},{"number_fault_variable":1,"low_f":"0,0,0,0,-8.43,0","high_f":"0,0,0,0,-8.43,0"},{"number_fault_variable":1,"low_f":"0,0,0,0,0,-8.31","high_f":"0,0,0,0,0,-8.31"}],"condition":"[DH4_40MAG20CE102]>20","Test_Type":"FAI","Limit_Value":0.0,"uplow":"null,null;null,null;null,null;null,null;null,null;null,null","number":0,"version":"v-test"}
|
||
|
res = main(info_str)
|
||
|
print("aaa")
|