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.
144 lines
6.8 KiB
144 lines
6.8 KiB
2 weeks ago
|
import numpy as np
|
||
|
import time
|
||
|
import copy
|
||
|
from AANN_Derivative import AANN_Derivative
|
||
|
from AANN_Fit import AANN_Fit
|
||
|
|
||
|
|
||
|
|
||
|
def AANN_SF_SBSRB(fault_sample, fault_magnitude, v1, v2, w1, w2, sigma, mindata, maxdata):
|
||
|
"""
|
||
|
前向后向序列
|
||
|
:param fault_sample:
|
||
|
:param fault_magnitude:
|
||
|
:param model:
|
||
|
:return:
|
||
|
"""
|
||
|
# print(type(fault_sample))
|
||
|
a = 0
|
||
|
[m, n] = fault_sample.shape
|
||
|
isolated = np.zeros((m, n))
|
||
|
delt_ff=np.zeros((m, n))
|
||
|
detected = 0 # 检测出故障样本数量
|
||
|
rod_n = 0
|
||
|
mdata = (2*fault_sample-np.tile(maxdata, (m, 1))-np.tile(mindata, (m, 1)))/(np.tile(maxdata, (m, 1))-np.tile(mindata, (m, 1)))
|
||
|
time1 = int(round(time.time()*1000))
|
||
|
confidence = 2*sigma
|
||
|
index = []
|
||
|
nodenum_record = np.zeros(m)
|
||
|
spe = np.zeros(m)
|
||
|
for sample_id in range(m):
|
||
|
# output = model(mdata[sample_id,:])
|
||
|
if sample_id==225:
|
||
|
print(1)
|
||
|
output = AANN_Fit(mdata[sample_id, :], v1, v2, w1, w2)
|
||
|
index.append(np.sum((mdata[sample_id, :]-output)*(mdata[sample_id, :]-output)))
|
||
|
spe[sample_id] = index[sample_id]
|
||
|
if index[sample_id] > sigma:
|
||
|
vnum = 1
|
||
|
detected += 1
|
||
|
node_num = 0
|
||
|
# 前向序列
|
||
|
variables = [i for i in range(n)]
|
||
|
selected = np.zeros((n, 1))
|
||
|
selected_v_num = -1
|
||
|
while selected_v_num <= n:
|
||
|
selected_variables = [variables[i] for i in (np.where(selected == 1)[0])]
|
||
|
unselected_variables = [variables[i] for i in (np.where(selected == 0)[0])]
|
||
|
cur_rbindex = np.zeros(len(unselected_variables))
|
||
|
for v_id in range(len(unselected_variables)):
|
||
|
cur_fault_variables = copy.deepcopy(selected_variables)
|
||
|
cur_fault_variables.append(unselected_variables[v_id])
|
||
|
cur_fault_variables = np.array(cur_fault_variables).astype(np.float64)
|
||
|
cur_rbindex[v_id], B, C, SPE = AANN_Derivative(v1.astype(np.float64), v2.astype(np.float64), w1.astype(np.float64), w2.astype(np.float64), mdata[sample_id, :], cur_fault_variables)
|
||
|
node_num += node_num
|
||
|
min_rbindex, op_vc_id = min(cur_rbindex), np.argmin(cur_rbindex)
|
||
|
selected[unselected_variables[op_vc_id]] = 1
|
||
|
if min_rbindex < confidence:
|
||
|
isolated_variable = [variables[i] for i in (np.where(selected == 1)[0])]#有问题
|
||
|
isolated[sample_id, isolated_variable] = 1
|
||
|
break
|
||
|
else:
|
||
|
selected_v_num += 1
|
||
|
# 后向序列
|
||
|
remained_v_num = len(np.where(selected == 1)[0])
|
||
|
while remained_v_num > 1:
|
||
|
remained_variables = [variables[i] for i in (np.where(selected == 1)[0])]
|
||
|
cur_rbindex = np.zeros(len(remained_variables))
|
||
|
for v_id in range(len(remained_variables)):
|
||
|
cur_fault_variables = copy.deepcopy(remained_variables)
|
||
|
cur_fault_variables = np.delete(cur_fault_variables, v_id)
|
||
|
cur_fault_variables = np.array(cur_fault_variables).astype(np.float64)
|
||
|
cur_rbindex[v_id], B, C, SPE = AANN_Derivative(v1, v2, w1, w2, mdata[sample_id, :], cur_fault_variables)
|
||
|
node_num += node_num
|
||
|
min_rbindex, op_vc_id = min(cur_rbindex), np.argmin(cur_rbindex)
|
||
|
if min_rbindex > confidence:
|
||
|
isolated_variable = [variables[i] for i in (np.where(selected == 1)[0])] # 有问题
|
||
|
isolated[sample_id, :] = 0
|
||
|
isolated[sample_id, isolated_variable] = 1
|
||
|
isolated_variable = np.array(isolated_variable).astype(np.float64)
|
||
|
A, delt_ff[sample_id,:], C, D = AANN_Derivative(v1, v2, w1, w2, mdata[sample_id, :],
|
||
|
isolated_variable)
|
||
|
# spe[sample_id] = min_rbindex
|
||
|
break
|
||
|
else:
|
||
|
selected[remained_variables[op_vc_id], :] = 0
|
||
|
remained_v_num -= 1
|
||
|
# 后向序列结束
|
||
|
nodenum_record[sample_id] = node_num
|
||
|
false_alarm = np.where((((fault_magnitude[sample_id, :]>0).astype('int')-isolated[sample_id, :]) == -1) == True)
|
||
|
print('sample_id-->{};false alarm:{}'.format(sample_id, false_alarm))
|
||
|
maxvalue1, maxindex1 = max(isolated[sample_id, :]), np.argmax(isolated[sample_id, :])
|
||
|
maxvalue2, maxindex2 = max(abs(fault_magnitude[sample_id, :])), np.argmax(abs(fault_magnitude[sample_id, :]))
|
||
|
if maxindex1 == maxindex2:
|
||
|
rod_n +=1
|
||
|
time2 = int(round(time.time()*1000))
|
||
|
tc = time2-time1
|
||
|
real_fault = (abs(fault_magnitude) > 0).astype('int')
|
||
|
fdr = np.sum(((real_fault-(isolated == 0).astype('int')) == 1).astype('int'))/np.sum(real_fault)
|
||
|
far = np.sum(((real_fault-isolated) == -1).astype('int'))/np.sum((real_fault == 0).astype('int'))
|
||
|
|
||
|
#fdr_variable, far_variable
|
||
|
# fdr_variable = []
|
||
|
# far_variable = []
|
||
|
# for i in range(real_fault.shape[1]):
|
||
|
# if np.sum(real_fault[:,i]) != 0:
|
||
|
# fdr_variable.append(np.sum(((real_fault[:,i]-(isolated[:,i] == 0).astype('int')) == 1).astype('int'))/np.sum(real_fault[:,i]))*100
|
||
|
# else:
|
||
|
# fdr_variable.append(0)
|
||
|
# if np.sum((real_fault[:,i] == 0).astype('int')) != 0:
|
||
|
# far_variable.append(np.sum(((real_fault[:,i]-isolated[:,i]) == -1).astype('int'))/np.sum((real_fault[:,i] == 0).astype('int')))*100
|
||
|
# else:
|
||
|
# far_variable.append(0)
|
||
|
fdr_variable = fdr
|
||
|
far_variable = far
|
||
|
|
||
|
delt_ff=delt_ff/2*(np.tile(maxdata, (m, 1))-np.tile(mindata, (m, 1))) #对偏差值进行反归一化
|
||
|
Reconstruction_precision=np.sum(np.average(abs(fault_magnitude-delt_ff), axis=0))
|
||
|
|
||
|
#为AANN_test_offline准备,2021-6-16 rsj&lf
|
||
|
Data_origin=fault_sample
|
||
|
errorData = delt_ff #测量值-重构值
|
||
|
reconData = Data_origin-errorData
|
||
|
SPE_list = np.sum(errorData ** 2)
|
||
|
R = 0
|
||
|
for index in range(0, reconData.shape[1]):
|
||
|
vector1 = Data_origin[:, index]
|
||
|
vector2 = np.array(reconData)[:, index]
|
||
|
R += np.dot(vector1, vector2.T) / (np.sqrt(np.sum(vector1 ** 2)) * np.sqrt(np.sum(vector2 ** 2)))
|
||
|
R /= reconData.shape[1]
|
||
|
items = [('reconData', reconData.tolist())
|
||
|
, ('errorData', errorData.tolist()), ('R', R.tolist()), ('SPE', SPE_list), ('FAI', SPE_list),
|
||
|
('paraState', paraState.tolist())]
|
||
|
ReconRes=json.dumps(dict(items)) # json.dumps(result)
|
||
|
# 为AANN_test_offline准备
|
||
|
|
||
|
|
||
|
dr = detected/np.sum((np.sum(real_fault, axis=1) > 0).astype('int'))*100
|
||
|
rod = rod_n/detected*100
|
||
|
nodes = np.mean(nodenum_record)
|
||
|
|
||
|
|
||
|
|
||
|
return delt_ff,fdr,far,fdr_variable,far_variable,Reconstruction_precision,spe,ReconRes
|