1137 lines
49 KiB
Python
1137 lines
49 KiB
Python
import numpy as np
|
||
|
||
|
||
def chk_transition(x0, errx0, x0r, errx0r):
|
||
"""检查边缘转换,如果右侧边缘在左侧边缘左侧则标记错误"""
|
||
errx0 = np.atleast_1d(np.asarray(errx0, dtype=np.float64)).ravel()
|
||
errx0r = np.atleast_1d(np.asarray(errx0r, dtype=np.float64)).ravel()
|
||
|
||
if len(errx0) < 2:
|
||
errx0_full = np.zeros(2)
|
||
errx0_full[:len(errx0)] = errx0
|
||
errx0 = errx0_full
|
||
if len(errx0r) < 2:
|
||
errx0r_full = np.zeros(2)
|
||
errx0r_full[:len(errx0r)] = errx0r
|
||
errx0r = errx0r_full
|
||
|
||
if errx0[0] == 0 and errx0r[0] == 0 and x0r < x0:
|
||
x0 = 0.0
|
||
x0r = 0.0
|
||
errx0[0] = 71
|
||
errx0r[0] = 71
|
||
|
||
return x0, errx0, x0r, errx0r
|
||
|
||
|
||
def _err2stat(errmat, status, edges, column, edge_active):
|
||
"""将错误矩阵转换为状态和边缘值"""
|
||
StNovalue = 45
|
||
StDefault = 255
|
||
|
||
errmat = np.atleast_2d(np.asarray(errmat, dtype=np.float64))
|
||
if errmat.shape[0] < 2:
|
||
errmat = np.pad(errmat, ((0, 2 - errmat.shape[0]), (0, 0)))
|
||
|
||
if edge_active:
|
||
err_arr = errmat[0, :]
|
||
warn_arr = errmat[1, :] if errmat.shape[0] > 1 else np.zeros(errmat.shape[1])
|
||
|
||
errind = np.where(err_arr > 0)[0]
|
||
warnind = np.where(warn_arr > 0)[0]
|
||
|
||
if len(errind) > 0:
|
||
if status.shape[0] > 0:
|
||
status[0, column] = err_arr[errind[0]]
|
||
edges[column] = 0
|
||
|
||
if len(warnind) > 0:
|
||
if status.shape[0] > 1:
|
||
status[1, column] = warn_arr[warnind[0]]
|
||
else:
|
||
edges[column] = 0
|
||
if status.shape[0] > 0:
|
||
status[0, column] = StNovalue
|
||
|
||
if edges[column] == 0 and status[0, column] == 0:
|
||
status[0, column] = StDefault
|
||
|
||
return status, edges
|
||
|
||
|
||
def errlevel2stat(edgesleft_in, edgesright_in, errorlevel, layeruse, cableType, mode):
|
||
"""将错误级别转换为边缘状态输出"""
|
||
edgesleft_out = np.array(edgesleft_in, dtype=np.float64).ravel().copy()
|
||
edgesright_out = np.array(edgesright_in, dtype=np.float64).ravel().copy()
|
||
|
||
n_edges = max(len(edgesleft_out), 4)
|
||
if len(edgesleft_out) < n_edges:
|
||
edgesleft_out = np.pad(edgesleft_out, (0, n_edges - len(edgesleft_out)))
|
||
if len(edgesright_out) < n_edges:
|
||
edgesright_out = np.pad(edgesright_out, (0, n_edges - len(edgesright_out)))
|
||
|
||
status = np.zeros((2, 1), dtype=np.float64)
|
||
statusL = np.zeros((2, n_edges), dtype=np.float64)
|
||
statusR = np.zeros((2, n_edges), dtype=np.float64)
|
||
|
||
errorlevel = np.atleast_2d(np.asarray(errorlevel, dtype=np.float64))
|
||
if errorlevel.shape[0] < 2:
|
||
errorlevel = np.pad(errorlevel, ((0, 2 - errorlevel.shape[0]), (0, 0)))
|
||
|
||
diaind = np.array([11, 12, 13, 16, 17]) - 1
|
||
column = 0
|
||
edge_active = 1
|
||
|
||
dia_err = errorlevel[:, diaind] if diaind[-1] < errorlevel.shape[1] else np.zeros((2, 5))
|
||
statusL, edgesleft_out = _err2stat(dia_err, statusL, edgesleft_out, column, edge_active)
|
||
statusR, edgesright_out = _err2stat(dia_err, statusR, edgesright_out, column, edge_active)
|
||
|
||
column = 3
|
||
if mode in ('anajack2',):
|
||
x3ind = np.concatenate([diaind, [18-1, 20-1, 22-1]])
|
||
x3rind = np.concatenate([diaind, [19-1, 21-1, 23-1]])
|
||
edge_active = np.all(layeruse)
|
||
elif mode in ('anajacksp', 'cfit_2l_tube_noyarn', 'cfit_2l_tube_yarn'):
|
||
x3ind = np.array([], dtype=int)
|
||
x3rind = np.array([], dtype=int)
|
||
edge_active = 0
|
||
elif mode in ('cfit_Uponor', 'cfit_Uponor_107'):
|
||
x3ind = np.concatenate([diaind, np.arange(14-1, 22)])
|
||
x3rind = np.concatenate([diaind, np.arange(14-1, 21), [23-1]])
|
||
edge_active = np.all(layeruse)
|
||
elif mode in ('analy01', 'cfit_tube', 'riffle'):
|
||
x3ind = np.concatenate([diaind, [14-1, 15-1, 22-1]])
|
||
x3rind = np.concatenate([diaind, [14-1, 15-1, 23-1]])
|
||
edge_active = np.any(layeruse)
|
||
else:
|
||
x3ind = np.concatenate([diaind, [14-1, 15-1, 22-1]])
|
||
x3rind = np.concatenate([diaind, [14-1, 15-1, 23-1]])
|
||
edge_active = np.any(layeruse)
|
||
|
||
def _safe_col(err, idx):
|
||
valid = idx[(idx >= 0) & (idx < err.shape[1])]
|
||
return err[:, valid] if len(valid) > 0 else np.zeros((2, 0))
|
||
|
||
x3_err = _safe_col(errorlevel, x3ind)
|
||
x3r_err = _safe_col(errorlevel, x3rind)
|
||
|
||
column = 3
|
||
statusL, edgesleft_out = _err2stat(x3_err, statusL, edgesleft_out, column, edge_active)
|
||
statusR, edgesright_out = _err2stat(x3r_err, statusR, edgesright_out, column, edge_active)
|
||
|
||
column = 1
|
||
if mode in ('anajacksp',):
|
||
x1ind = np.concatenate([diaind, [14-1, 15-1, 22-1, 23-1, 18-1]])
|
||
x1rind = np.concatenate([diaind, [14-1, 15-1, 22-1, 23-1, 19-1]])
|
||
edge_active_x1 = layeruse[0] if len(layeruse) > 0 else 0
|
||
elif mode in ('cfit_2l_tube_noyarn',):
|
||
x1ind = np.concatenate([diaind, [29-1, 18-1]])
|
||
x1rind = np.concatenate([diaind, [29-1, 19-1]])
|
||
edge_active_x1 = layeruse[0] if len(layeruse) > 0 else 0
|
||
elif mode in ('cfit_2l_tube_yarn',):
|
||
x1ind = np.concatenate([diaind, [29-1, 20-1, 21-1, 18-1]])
|
||
x1rind = np.concatenate([diaind, [29-1, 20-1, 21-1, 19-1]])
|
||
edge_active_x1 = layeruse[0] if len(layeruse) > 0 else 0
|
||
elif mode in ('cfit_Uponor', 'cfit_Uponor_107'):
|
||
x1ind = np.concatenate([diaind, [14-1, 15-1, 18-1]])
|
||
x1rind = np.concatenate([diaind, [14-1, 15-1, 19-1]])
|
||
edge_active_x1 = layeruse[0] if len(layeruse) > 0 else 0
|
||
elif mode in ('analy01', 'cfit_tube', 'anajack2', 'riffle'):
|
||
x1ind = np.concatenate([diaind, [18-1]])
|
||
x1rind = np.concatenate([diaind, [19-1]])
|
||
edge_active_x1 = layeruse[0] if len(layeruse) > 0 else 0
|
||
else:
|
||
x1ind = np.concatenate([diaind, [18-1]])
|
||
x1rind = np.concatenate([diaind, [19-1]])
|
||
edge_active_x1 = layeruse[0] if len(layeruse) > 0 else 0
|
||
|
||
x1_err = _safe_col(errorlevel, x1ind)
|
||
x1r_err = _safe_col(errorlevel, x1rind)
|
||
statusL, edgesleft_out = _err2stat(x1_err, statusL, edgesleft_out, column, edge_active_x1)
|
||
statusR, edgesright_out = _err2stat(x1r_err, statusR, edgesright_out, column, edge_active_x1)
|
||
|
||
column = 2
|
||
if mode in ('cfit_tube',):
|
||
x2ind = np.concatenate([diaind, [18-1, 19-1], x3ind, [20-1]])
|
||
x2rind = np.concatenate([diaind, [18-1, 19-1], x3rind, [21-1]])
|
||
edge_active_x2 = layeruse[2] if len(layeruse) > 2 else 0
|
||
elif mode in ('anajack2',):
|
||
x2ind = np.concatenate([diaind, [18-1, 20-1]])
|
||
x2rind = np.concatenate([diaind, [19-1, 21-1]])
|
||
edge_active_x2 = np.all(layeruse[:2]) if len(layeruse) >= 2 else 0
|
||
elif mode in ('anajacksp', 'cfit_Uponor_107'):
|
||
x2ind = np.array([], dtype=int)
|
||
x2rind = np.array([], dtype=int)
|
||
edge_active_x2 = 0
|
||
elif mode in ('cfit_2l_tube_noyarn', 'cfit_2l_tube_yarn'):
|
||
x2ind = np.concatenate([diaind, [29-1, 20-1]])
|
||
x2rind = np.concatenate([diaind, [29-1, 21-1]])
|
||
edge_active_x2 = np.all(layeruse[:2]) if len(layeruse) >= 2 else 0
|
||
elif mode in ('cfit_Uponor',):
|
||
x2ind = np.concatenate([diaind, [14-1, 15-1, 18-1]])
|
||
x2rind = np.concatenate([diaind, [14-1, 15-1, 19-1]])
|
||
edge_active_x2 = np.all(layeruse[:2]) if len(layeruse) >= 2 else 0
|
||
elif mode in ('analy01',):
|
||
x2ind = np.concatenate([diaind, [14-1, 15-1, 22-1, 23-1, 20-1]])
|
||
x2rind = np.concatenate([diaind, [14-1, 15-1, 22-1, 23-1, 21-1]])
|
||
edge_active_x2 = layeruse[2] if len(layeruse) > 2 else 0
|
||
elif mode in ('riffle',):
|
||
x2ind = np.concatenate([diaind, [14-1, 15-1, 22-1, 23-1, 20-1, 21-1, 18-1, 19-1]])
|
||
x2rind = np.concatenate([diaind, [14-1, 15-1, 22-1, 23-1, 20-1, 21-1, 18-1, 19-1]])
|
||
edge_active_x2 = layeruse[2] if len(layeruse) > 2 else 0
|
||
else:
|
||
x2ind = np.concatenate([diaind, [14-1, 15-1, 22-1, 23-1, 20-1]])
|
||
x2rind = np.concatenate([diaind, [14-1, 15-1, 22-1, 23-1, 21-1]])
|
||
edge_active_x2 = layeruse[2] if len(layeruse) > 2 else 0
|
||
|
||
x2_err = _safe_col(errorlevel, x2ind)
|
||
x2r_err = _safe_col(errorlevel, x2rind)
|
||
statusL, edgesleft_out = _err2stat(x2_err, statusL, edgesleft_out, column, edge_active_x2)
|
||
statusR, edgesright_out = _err2stat(x2r_err, statusR, edgesright_out, column, edge_active_x2)
|
||
|
||
if mode in ('anajack2', 'anajacksp', 'cfit_2l_tube_noyarn', 'cfit_2l_tube_yarn') or cableType == 79:
|
||
if len(layeruse) >= 3 and np.all(layeruse == [1, 0, 0]):
|
||
edgesleft_out[3] = edgesleft_out[1]
|
||
edgesright_out[3] = edgesright_out[1]
|
||
statusL[:, 3] = statusL[:, 1]
|
||
statusR[:, 3] = statusR[:, 1]
|
||
elif len(layeruse) >= 2 and np.all(layeruse[:2] == [1, 1]):
|
||
edgesleft_out[3] = edgesleft_out[2]
|
||
edgesright_out[3] = edgesright_out[2]
|
||
statusL[:, 3] = statusL[:, 2]
|
||
statusR[:, 3] = statusR[:, 2]
|
||
|
||
if mode in ('analy01', 'cfit_tube') and len(layeruse) >= 3 and np.all(layeruse == [1, 0, 0]):
|
||
edgesleft_out[1] = edgesleft_out[3]
|
||
edgesright_out[1] = edgesright_out[3]
|
||
statusL[:, 1] = statusL[:, 3]
|
||
statusR[:, 1] = statusR[:, 3]
|
||
|
||
for row in range(2):
|
||
errindL = np.where(statusL[row, :] > 0)[0]
|
||
errindR = np.where(statusR[row, :] > 0)[0]
|
||
if len(errindL) > 0 and len(errindR) > 0:
|
||
if errindR[0] < errindL[0]:
|
||
status[row, 0] = statusR[row, errindR[0]]
|
||
else:
|
||
status[row, 0] = statusL[row, errindL[0]]
|
||
elif len(errindL) > 0:
|
||
status[row, 0] = statusL[row, errindL[0]]
|
||
elif len(errindR) > 0:
|
||
status[row, 0] = statusR[row, errindR[0]]
|
||
|
||
return edgesleft_out, edgesright_out, status, statusL, statusR
|
||
|
||
|
||
def ana_curvefit_cable(I, conf, errorin, settings):
|
||
"""XLPE电缆的曲线拟合分析(对应MATLAB ana_curvefit_cable.m)"""
|
||
from edge_detection_utils import i0detect, i0det2, x41det2
|
||
from detection_utils import cxlr11
|
||
from math_utils import ind, is_pe
|
||
from curve_fit_utils import (
|
||
x0_x0r_curvefit2, x1_x1r_curvefit3, x1_x1r_curvefit4, lincorn, x1RangeLimiter
|
||
)
|
||
from geometry_utils import strng2
|
||
|
||
I = np.asarray(I, dtype=np.float64).ravel()
|
||
conf = np.asarray(conf, dtype=np.float64).ravel()
|
||
errorin = np.asarray(errorin, dtype=np.float64).ravel()
|
||
if len(errorin) < 2:
|
||
errorin = np.zeros(2)
|
||
|
||
detail = {
|
||
'IN': np.nan, 'nominal': np.nan,
|
||
'left': {
|
||
'pos': {'result': np.nan, 'start': np.nan,
|
||
'limit': {'left': np.nan, 'right': np.nan}},
|
||
'att': {'outer': np.nan, 'inner': np.nan},
|
||
'error': np.array([np.nan, np.nan])
|
||
},
|
||
'att': {'min': np.nan, 'max': np.nan},
|
||
'contr': {'min': np.nan, 'max': np.nan, 'sign': np.nan},
|
||
}
|
||
detail['right'] = {
|
||
'pos': {'result': np.nan, 'start': np.nan,
|
||
'limit': {'left': np.nan, 'right': np.nan}},
|
||
'att': {'outer': np.nan, 'inner': np.nan},
|
||
'error': np.array([np.nan, np.nan])
|
||
}
|
||
|
||
TAPE_TOO_LARGE = 93
|
||
conf20 = conf[19] if len(conf) > 19 else 0
|
||
dx = conf[91] if len(conf) > 91 else 0.048
|
||
dx = float(dx) if dx is not None else 0.048
|
||
if dx == 0:
|
||
dx = 0.048
|
||
|
||
NOMDIA = conf[0] if len(conf) > 0 else 0
|
||
NOMCOND = conf[1] if len(conf) > 1 else 0
|
||
NOMOSC = conf[2] if len(conf) > 2 else 0
|
||
NOMISC = conf[3] if len(conf) > 3 else 0
|
||
Riff = conf[5] if len(conf) > 5 else 0
|
||
NOMTAPE = conf[4] if len(conf) > 4 else 0
|
||
NOMTAPEl = NOMTAPE
|
||
NOMTAPEr = NOMTAPE
|
||
|
||
offsetCORE = 0
|
||
if abs(conf20 - 123) < 1e-6:
|
||
offsetCORE = conf[4] if len(conf) > 4 else 0
|
||
|
||
Maximumc = np.array([1, 1, -1, 1, 1], dtype=np.float64)
|
||
if abs(conf20 - 122) < 1e-6:
|
||
Maximumc = np.array([1, 1, -1, -1, 1], dtype=np.float64)
|
||
|
||
confx0ind = 1179
|
||
confx1ind = 1299
|
||
confx2ind = 1319
|
||
if abs(conf20 - 116) < 1e-6:
|
||
confx2ind = 1339
|
||
|
||
conddetoff = 0
|
||
modevector = np.array([-1, 6, 2, -1], dtype=np.float64)
|
||
|
||
if NOMISC <= 0.5:
|
||
if NOMTAPE > 0:
|
||
errorin[0] = TAPE_TOO_LARGE
|
||
elif NOMISC <= 1.0:
|
||
if NOMTAPE > (NOMISC - 0.5):
|
||
errorin[0] = TAPE_TOO_LARGE
|
||
else:
|
||
if NOMTAPE > 0.5 * NOMISC:
|
||
errorin[0] = TAPE_TOO_LARGE
|
||
|
||
onlydiameterused = abs(conf20 - 70) < 1e-6
|
||
layeruse = np.array([(NOMOSC > 0), 0, (NOMISC > 0)], dtype=np.float64)
|
||
|
||
if onlydiameterused:
|
||
layeruse = np.array([0, 0, 0])
|
||
elif layeruse[0] == 0 and layeruse[2] == 0:
|
||
layeruse = np.array([1, 0, 0])
|
||
elif layeruse[0] == 0 and layeruse[2] == 1:
|
||
layeruse = np.array([0, 1, 1])
|
||
elif layeruse[0] == 1 and layeruse[2] == 0:
|
||
layeruse = np.array([1, 1, 0])
|
||
else:
|
||
layeruse = np.array([1, 1, 1])
|
||
|
||
DistI0 = conf[34] if len(conf) > 34 else 1000
|
||
Dist01 = conf[35] if len(conf) > 35 else 0
|
||
Lowrad = conf[36] if len(conf) > 36 else 20000
|
||
|
||
if is_pe(conf20):
|
||
Highpegel11 = conf[122] if len(conf) > 122 else 0
|
||
else:
|
||
Highpegel11 = conf[39] if len(conf) > 39 else 0
|
||
|
||
Ab = conf[40] if len(conf) > 40 else 0
|
||
Offpegel41 = conf[41] if len(conf) > 41 else 0
|
||
Off41 = conf[42] if len(conf) > 42 else 0
|
||
Minidiff = conf[43] if len(conf) > 43 else 0
|
||
Schicht34 = conf[44] if len(conf) > 44 else 0
|
||
Schicht01 = conf[45] if len(conf) > 45 else 0
|
||
fg12 = conf[47] if len(conf) > 47 else 0
|
||
fg23 = conf[48] if len(conf) > 48 else 0
|
||
fakfg = conf[52] if len(conf) > 52 else 0
|
||
sym01 = conf[56] if len(conf) > 56 else 0
|
||
sym01riff = conf[58] if len(conf) > 58 else 0
|
||
sym12 = conf[60] if len(conf) > 60 else 0
|
||
Itermax = conf[61] if len(conf) > 61 else 0
|
||
Centerdiff = conf[62] if len(conf) > 62 else 0
|
||
klevel12 = conf[65] if len(conf) > 65 else 0
|
||
Sehnlimit = conf[82] if len(conf) > 82 else 0
|
||
klimit = conf[83] if len(conf) > 83 else 0
|
||
Minpegel41 = conf[84] if len(conf) > 84 else 0
|
||
Mindistadd = conf[165] if len(conf) > 165 else 0
|
||
Maxdistadd = conf[166] if len(conf) > 166 else 0
|
||
Ber = 2.0
|
||
|
||
errorlevel = np.zeros((2, 28), dtype=np.float64)
|
||
Anzpara = conf[10] if len(conf) > 10 else 0
|
||
|
||
if 0 < NOMOSC < 0.2:
|
||
NOMOSC = 0.2
|
||
if 0 < NOMISC < 0.2:
|
||
NOMISC = 0.2
|
||
if NOMDIA < (2 * NOMOSC + 2 * NOMISC + NOMCOND):
|
||
NOMDIA = 2 * NOMOSC + 2 * NOMISC + NOMCOND
|
||
|
||
if abs(Riff - 1) < 1e-6:
|
||
sym01 = sym01riff
|
||
|
||
count = len(I)
|
||
x = np.arange(1, count + 1, dtype=np.float64)
|
||
x = (x - 1) * dx
|
||
Ab = int(np.round(Ab / dx)) if dx > 0 else 0
|
||
Highpegel11 = Highpegel11 / 100.0
|
||
Offpegel41 = Offpegel41 / 100.0
|
||
|
||
if fg23 > (1 / (2 * dx)) and dx > 0:
|
||
fg23 = 1 / (2 * dx)
|
||
|
||
xl11, xl00, xr11, xr00 = 0.0, 0.0, 0.0, 0.0
|
||
xl41, xr41 = 0.0, 0.0
|
||
x0, x0r, x1, x1r, x2, x2r, x3, x3r = 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
|
||
|
||
I0, errorlevel[0:2, 10] = i0detect(I, DistI0, dx, Lowrad, errorin)
|
||
if errorlevel[0, 10] == 0:
|
||
IN = I / I0 if I0 > 0 else I
|
||
else:
|
||
IN = I.copy()
|
||
detail['IN'] = IN
|
||
|
||
if errorlevel[0, 10] == 0:
|
||
try:
|
||
xl11, xl00, errorlevel[0, 11], xr11, xr00, errorlevel[0, 12], \
|
||
xl41_t, xr41_t, xlr41det = cxlr11(I, I0, Anzpara, conf)
|
||
xl41 = xl41_t
|
||
xr41 = xr41_t
|
||
except Exception as e:
|
||
errorlevel[0, 11] = 4
|
||
errorlevel[0, 12] = 4
|
||
|
||
errordia = errorlevel[0, 10] + errorlevel[0, 11] + errorlevel[0, 12]
|
||
|
||
if errordia == 0:
|
||
ll = int(ind(xl11 + (conf[130] if len(conf) > 130 else 0), dx, 0, count))
|
||
rr = int(ind(xr11 - (conf[130] if len(conf) > 130 else 0), dx, 0, count))
|
||
if rr > ll:
|
||
muell = np.where(I[ll:rr] > (I0 * Highpegel11))[0]
|
||
if len(muell) > 0:
|
||
errorlevel[0, 10] = 10
|
||
if xl11 >= xr11:
|
||
errorlevel[0, 10] = 10
|
||
|
||
errordia = errorlevel[0, 10] + errorlevel[0, 11] + errorlevel[0, 12]
|
||
|
||
if errordia == 0:
|
||
try:
|
||
I02, err_1d = i0det2(I, xl11, xr11, conf, errorlevel[:, 10])
|
||
errorlevel[:, 10] = np.atleast_1d(err_1d).ravel()[:2]
|
||
if errorlevel.shape[0] < 2:
|
||
pass
|
||
except Exception:
|
||
pass
|
||
|
||
errordia = np.any(errorlevel[0, 10:13])
|
||
|
||
NOMOSCNEW = NOMOSC
|
||
NOMISCNEW = NOMISC
|
||
|
||
if errordia == 0:
|
||
if NOMOSC > ((xr11 - xl11) / 5):
|
||
NOMOSCNEW = (xr11 - xl11) / 5
|
||
if NOMISC > ((xr11 - xl11) / 5):
|
||
NOMISCNEW = (xr11 - xl11) / 5
|
||
|
||
Sehnmax = np.sqrt(max(NOMDIA**2 - NOMCOND**2, 0))
|
||
xk = xl11 + (NOMDIA - NOMCOND) / 4
|
||
d0sch = xr11 - xl11
|
||
sk_expr = (xk - xl11) * d0sch - (xk - xl11)**2
|
||
sk = 2 * np.sqrt(max(sk_expr, 0))
|
||
Indxk = int(ind(xk, dx))
|
||
Indxk = max(0, min(count - 1, Indxk))
|
||
|
||
if sk > 0 and Indxk < len(IN) and IN[Indxk] > 0:
|
||
kmitt = -(np.log(IN[Indxk])) / sk
|
||
else:
|
||
kmitt = 0
|
||
|
||
if Sehnmax > Sehnlimit and kmitt > klimit:
|
||
errorlevel[0, 13] = 25
|
||
|
||
errorcond = errorlevel[0, 13] + (errorlevel[0, 14] if errorlevel.shape[1] > 14 else 0)
|
||
|
||
if errordia == 0 and errorcond == 0 and conddetoff == 0:
|
||
if abs(conf[41] if len(conf) > 41 else 0) < 1e-6:
|
||
d0s = xr11 - xl11
|
||
if is_pe(conf20):
|
||
att = 0.025
|
||
else:
|
||
att = 0.15
|
||
Offpegel41 = np.exp(-att * d0s)
|
||
|
||
if abs(conf20 - 113) < 1e-6:
|
||
Distadd = 0.65 * (conf[8] if len(conf) > 8 else 0)
|
||
else:
|
||
Distadd = NOMOSCNEW
|
||
|
||
if Mindistadd == 0:
|
||
Mindistadd = 0.15 + 0.0085 * (x0r - x0) if (x0r - x0) > 0 else 0.15
|
||
|
||
if Distadd < Mindistadd:
|
||
Distadd = Mindistadd
|
||
elif Distadd > Maxdistadd:
|
||
Distadd = Maxdistadd
|
||
|
||
Dist41 = NOMOSCNEW + Distadd
|
||
|
||
confi = np.zeros(17, dtype=np.float64)
|
||
confi[0:5] = [xl11, xr11, Dist41, I0, Offpegel41]
|
||
confi[5:10] = [Off41, Ab, Minpegel41, Minidiff, Ber]
|
||
confi[10:15] = [Anzpara, dx, Maximumc[0], conf20, 0]
|
||
confi[15:17] = [0, conf[194] if len(conf) > 194 else 0]
|
||
|
||
try:
|
||
xl41, err_14 = x41det2(x, IN, confi)
|
||
errorlevel[:, 13] = np.atleast_1d(err_14).ravel()[:2]
|
||
except Exception:
|
||
xl41 = 0
|
||
errorlevel[0, 13] = 7
|
||
|
||
if errorlevel[0, 13] != 0:
|
||
xl41 = 0
|
||
|
||
confi[14] = 1
|
||
try:
|
||
xr41, err_15 = x41det2(x, IN, confi)
|
||
errorlevel[:, 14] = np.atleast_1d(err_15).ravel()[:2]
|
||
except Exception:
|
||
xr41 = 0
|
||
errorlevel[0, 14] = 7
|
||
|
||
if errorlevel[0, 14] != 0:
|
||
xr41 = 0
|
||
|
||
errorcond = errorlevel[0, 13] + errorlevel[0, 14]
|
||
|
||
if errordia == 0:
|
||
fun_para_static = {}
|
||
modul_para = {}
|
||
try:
|
||
fun_para_static['filtactive'] = conf[confx0ind + 9] if len(conf) > confx0ind + 9 else 0
|
||
fun_para_static['filtlength'] = conf[confx0ind + 10] if len(conf) > confx0ind + 10 else 0
|
||
modul_para['range_outer'] = conf[confx0ind + 11] if len(conf) > confx0ind + 11 else 0
|
||
modul_para['range_inner'] = conf[confx0ind + 12] if len(conf) > confx0ind + 12 else 0
|
||
modul_para['display'] = Anzpara
|
||
modul_para['x0limit_l'] = xl00
|
||
x0limit = modul_para['range_inner']
|
||
if NOMOSC > 0 and x0limit > 0.5 * NOMOSC:
|
||
x0limit = 0.5 * NOMOSC if 0.5 * NOMOSC > 0.3 else 0.3
|
||
modul_para['x0limit_r'] = xl11 + x0limit - 0.5 * fun_para_static.get('filtlength', 0)
|
||
modul_para['x0rlimit_l'] = xr11 - x0limit + 0.5 * fun_para_static.get('filtlength', 0)
|
||
modul_para['x0rlimit_r'] = xr00
|
||
modul_para['minatt1'] = conf[confx0ind] if len(conf) > confx0ind else 0
|
||
modul_para['maxatt1'] = conf[confx0ind + 1] if len(conf) > confx0ind + 1 else 1
|
||
modul_para['mincontr'] = conf[confx0ind + 2] if len(conf) > confx0ind + 2 else 0
|
||
modul_para['maxcontr'] = conf[confx0ind + 3] if len(conf) > confx0ind + 3 else 100
|
||
modul_para['resmax'] = conf[confx0ind + 4] if len(conf) > confx0ind + 4 else 1
|
||
modul_para['TolFun'] = conf[confx0ind + 5] if len(conf) > confx0ind + 5 else 1e-6
|
||
modul_para['TolX'] = conf[confx0ind + 6] if len(conf) > confx0ind + 6 else 1e-6
|
||
modul_para['MaxFunEvals'] = conf[confx0ind + 7] if len(conf) > confx0ind + 7 else 400
|
||
modul_para['LevenbergMarquardt'] = conf[confx0ind + 8] if len(conf) > confx0ind + 8 else 0
|
||
fun_startpara = np.array([xl11, xr11, 0.0])
|
||
fun_para_static['unsharpness'] = 0
|
||
|
||
calcpara, err_16_17, dataout = x0_x0r_curvefit2(
|
||
x, IN, fun_startpara, fun_para_static, modul_para
|
||
)
|
||
x0 = calcpara[0] if isinstance(calcpara, np.ndarray) else calcpara.get('x0', xl11)
|
||
x0r = calcpara[1] if isinstance(calcpara, np.ndarray) else calcpara.get('x0r', xr11)
|
||
|
||
if isinstance(err_16_17, np.ndarray) and err_16_17.shape[1] >= 2:
|
||
errorlevel[:, 15] = err_16_17[:, 0]
|
||
errorlevel[:, 16] = err_16_17[:, 1]
|
||
|
||
x0, errorlevel[:, 15], x0r, errorlevel[:, 16] = chk_transition(
|
||
x0, errorlevel[:, 15], x0r, errorlevel[:, 16]
|
||
)
|
||
|
||
detail['nominal'] = NOMOSC
|
||
if isinstance(dataout, dict) and 'left' in dataout:
|
||
detail['left'] = dataout['left']
|
||
detail['left']['pos']['start'] = fun_startpara[0]
|
||
detail['left']['pos']['limit']['left'] = modul_para.get('x0limit_l', 0)
|
||
detail['left']['pos']['limit']['right'] = modul_para.get('x0limit_r', 0)
|
||
detail['left']['error'] = errorlevel[:, 15].copy()
|
||
|
||
if isinstance(dataout, dict) and 'right' in dataout:
|
||
detail['right'] = dataout['right']
|
||
detail['right']['pos']['start'] = fun_startpara[1]
|
||
detail['right']['pos']['limit']['left'] = modul_para.get('x0rlimit_l', 0)
|
||
detail['right']['pos']['limit']['right'] = modul_para.get('x0rlimit_r', 0)
|
||
detail['right']['error'] = errorlevel[:, 16].copy()
|
||
|
||
detail['att']['min'] = modul_para.get('minatt1', 0)
|
||
detail['att']['max'] = modul_para.get('maxatt1', 0)
|
||
detail['contr']['min'] = modul_para.get('mincontr', 0)
|
||
detail['contr']['max'] = modul_para.get('maxcontr', 0)
|
||
except Exception as e:
|
||
pass
|
||
|
||
conf20_special = abs(conf20 - 121) >= 1e-6
|
||
if (conf20_special and errordia == 0 and NOMOSC > 0 and
|
||
errorlevel[0, 15] == 0 and errorlevel[0, 16] == 0):
|
||
try:
|
||
modul_para['range_outer'] = conf[confx1ind + 11] if len(conf) > confx1ind + 11 else 0
|
||
modul_para['range_inner'] = conf[confx1ind + 12] if len(conf) > confx1ind + 12 else 0
|
||
fun_para_static['filtactive'] = conf[confx1ind + 9] if len(conf) > confx1ind + 9 else 0
|
||
fun_para_static['filtlength'] = conf[confx1ind + 10] if len(conf) > confx1ind + 10 else 0
|
||
modul_para['display'] = Anzpara
|
||
|
||
filtlen = fun_para_static.get('filtlength', 0)
|
||
if (NOMOSCNEW - filtlen) > 0.5:
|
||
modul_para['x1limit_l'] = x0 + filtlen
|
||
modul_para['x1rlimit_r'] = x0r - filtlen
|
||
else:
|
||
modul_para['x1limit_l'] = x0 + 0.5 * filtlen
|
||
modul_para['x1rlimit_r'] = x0r - 0.5 * filtlen
|
||
|
||
x1lim_r, x1rlim_l = 0, 0
|
||
try:
|
||
from curve_fit_utils import x1RangeLimiter
|
||
x1lim_r, x1rlim_l = x1RangeLimiter(
|
||
x0, x0r, xl41, xr41, NOMOSC, NOMOSC,
|
||
3.0, NOMISC, NOMISC, 1.5, conf20
|
||
)
|
||
except Exception:
|
||
x1lim_r = xl41 if xl41 > 0 else x0 + NOMOSC
|
||
x1rlim_l = xr41 if xr41 > 0 else x0r - NOMOSC
|
||
|
||
modul_para['x1limit_r'] = x1lim_r
|
||
modul_para['x1rlimit_l'] = x1rlim_l
|
||
|
||
modul_para['minatt1'] = conf[confx1ind] if len(conf) > confx1ind else 0
|
||
modul_para['maxatt1'] = conf[confx1ind + 1] if len(conf) > confx1ind + 1 else 1
|
||
modul_para['mincontr'] = conf[confx1ind + 2] if len(conf) > confx1ind + 2 else 0
|
||
modul_para['maxcontr'] = conf[confx1ind + 3] if len(conf) > confx1ind + 3 else 100
|
||
modul_para['contrsign'] = Maximumc[2]
|
||
modul_para['resmax'] = conf[confx1ind + 4] if len(conf) > confx1ind + 4 else 1
|
||
modul_para['TolFun'] = conf[confx1ind + 5] if len(conf) > confx1ind + 5 else 1e-6
|
||
modul_para['TolX'] = conf[confx1ind + 6] if len(conf) > confx1ind + 6 else 1e-6
|
||
modul_para['MaxFunEvals'] = conf[confx1ind + 7] if len(conf) > confx1ind + 7 else 400
|
||
modul_para['LevenbergMarquardt'] = conf[confx1ind + 8] if len(conf) > confx1ind + 8 else 0
|
||
|
||
fun_para_static['x0'] = x0
|
||
fun_para_static['x0r'] = x0r
|
||
fun_para_static['mode'] = modevector[1]
|
||
|
||
fun_para_static['x1'] = x0 + NOMOSCNEW
|
||
fun_para_static['x1r'] = x0r - NOMOSCNEW
|
||
fun_para_static['txt_left'] = '; x1= '
|
||
fun_para_static['txt_right'] = '; x1r= '
|
||
|
||
calcpara_x1, err_18_19 = x1_x1r_curvefit3(x, IN, fun_para_static, modul_para)
|
||
|
||
alpha0 = 0.0
|
||
if isinstance(calcpara_x1, dict):
|
||
x1 = calcpara_x1.get('left', {}).get('pos', {}).get('result', np.nan)
|
||
x1r = calcpara_x1.get('right', {}).get('pos', {}).get('result', np.nan)
|
||
alpha0 = calcpara_x1.get('alpha0', 0.0)
|
||
if not np.isfinite(x1):
|
||
x1 = calcpara_x1.get('x1', x0 + NOMOSCNEW)
|
||
if not np.isfinite(x1r):
|
||
x1r = calcpara_x1.get('x1r', x0r - NOMOSCNEW)
|
||
else:
|
||
x1 = calcpara_x1[0] if len(calcpara_x1) > 0 else x0 + NOMOSCNEW
|
||
x1r = calcpara_x1[1] if len(calcpara_x1) > 1 else x0r - NOMOSCNEW
|
||
|
||
if isinstance(err_18_19, np.ndarray) and err_18_19.shape[1] >= 2:
|
||
errorlevel[:, 17] = err_18_19[:, 0]
|
||
errorlevel[:, 18] = err_18_19[:, 1]
|
||
|
||
x1, errorlevel[:, 17], x1r, errorlevel[:, 18] = chk_transition(
|
||
x1, errorlevel[:, 17], x1r, errorlevel[:, 18]
|
||
)
|
||
|
||
except Exception as e:
|
||
import traceback
|
||
traceback.print_exc()
|
||
pass
|
||
|
||
if (errordia == 0 and (errorcond == 0 or conddetoff > 0) and
|
||
np.all(errorlevel[0, [15, 16]] == 0)):
|
||
|
||
sym34 = conf[55] if len(conf) > 55 else 0
|
||
Schicht4 = conf[46] if len(conf) > 46 else 0
|
||
fg34 = conf[51] if len(conf) > 51 else 0
|
||
klevel34 = conf[67] if len(conf) > 67 else 0
|
||
|
||
try:
|
||
Dl = xl41 - sym34
|
||
Dr = xl41 + sym34
|
||
Limitl = xl41 - sym34
|
||
Limitr = xl41 + Schicht4
|
||
Dl = max(Dl, Limitl)
|
||
Dr = min(Dr, Limitr)
|
||
|
||
confi_x3 = np.zeros(14, dtype=np.float64)
|
||
confi_x3[0:5] = [Dl, Dr, Limitl, Limitr, fg34]
|
||
confi_x3[5:10] = [sym01, Centerdiff, Itermax, dx, klevel34]
|
||
confi_x3[10:14] = [Anzpara, fakfg, Maximumc[4], 0]
|
||
|
||
x3, err_22 = lincorn(IN, confi_x3)
|
||
errorlevel[:, 21] = np.atleast_1d(err_22).ravel()[:2]
|
||
detail['left']['pos']['result'] = x3
|
||
detail['left']['pos']['limit']['left'] = Limitl
|
||
detail['left']['pos']['limit']['right'] = Limitr
|
||
except Exception as e:
|
||
import traceback
|
||
traceback.print_exc()
|
||
x3 = 0
|
||
|
||
try:
|
||
Dl_r = xr41 - sym34
|
||
Dr_r = xr41 + sym34
|
||
Limitl_r = xr41 - Schicht4
|
||
Limitr_r = xr41 + sym34
|
||
Dl_r = max(Dl_r, Limitl_r)
|
||
Dr_r = min(Dr_r, Limitr_r)
|
||
|
||
confi_x3r = np.zeros(14, dtype=np.float64)
|
||
confi_x3r[0:5] = [Dl_r, Dr_r, Limitl_r, Limitr_r, fg34]
|
||
confi_x3r[5:10] = [sym01, Centerdiff, Itermax, dx, klevel34]
|
||
confi_x3r[10:14] = [Anzpara, fakfg, Maximumc[4], 1]
|
||
|
||
x3r, err_23 = lincorn(IN, confi_x3r)
|
||
errorlevel[:, 22] = np.atleast_1d(err_23).ravel()[:2]
|
||
detail['right']['pos']['result'] = x3r
|
||
detail['right']['pos']['limit']['left'] = Limitl_r
|
||
detail['right']['pos']['limit']['right'] = Limitr_r
|
||
except Exception:
|
||
x3r = 0
|
||
|
||
try:
|
||
x3_old = x3
|
||
x3r_old = x3r
|
||
err21_old = errorlevel[:, 21].copy()
|
||
err22_old = errorlevel[:, 22].copy()
|
||
x3, errorlevel[:, 21], x3r, errorlevel[:, 22] = chk_transition(
|
||
x3, errorlevel[:, 21], x3r, errorlevel[:, 22]
|
||
)
|
||
except Exception:
|
||
pass
|
||
|
||
if (errordia == 0 and (errorcond == 0 or conddetoff > 0) and NOMISC > 0 and
|
||
np.all(errorlevel[0, [15, 16, 21, 22]] == 0)):
|
||
try:
|
||
from math_utils import ind
|
||
from geometry_utils import strng2
|
||
from curve_fit_utils import x1_x1r_curvefit4
|
||
|
||
# ============ 1. INS信号:剥离OSC层衰减 ============
|
||
if NOMOSC > 0 and np.all(errorlevel[0, [17, 18]] == 0) and abs(conf20 - 115) >= 1e-6:
|
||
s0_ins = strng2(x, x0, x0r, 1)
|
||
s1_ins = strng2(x, x1, x1r, 1)
|
||
INS = IN / np.exp(-1.0 * (alpha0 * (s0_ins - s1_ins)))
|
||
fun_para_static['x0'] = x1
|
||
fun_para_static['x0r'] = x1r
|
||
modul_para['x1limit_l'] = x1 + fun_para_static.get('filtactive', 0)
|
||
modul_para['x1rlimit_r'] = x1r - fun_para_static.get('filtactive', 0)
|
||
else:
|
||
INS = IN.copy()
|
||
fun_para_static['x0'] = x0
|
||
fun_para_static['x0r'] = x0r
|
||
modul_para['x1limit_l'] = x0 + 2 * NOMOSC + fun_para_static.get('filtactive', 0)
|
||
modul_para['x1rlimit_r'] = x0r - 2 * NOMOSC - fun_para_static.get('filtactive', 0)
|
||
|
||
# ============ 2. 配置x2拟合参数 ============
|
||
fun_para_static['filtactive'] = conf[confx2ind + 9] if len(conf) > confx2ind + 9 else 0
|
||
fun_para_static['filtlength'] = conf[confx2ind + 10] if len(conf) > confx2ind + 10 else 0
|
||
|
||
modul_para['range_outer'] = conf[confx2ind + 11] if len(conf) > confx2ind + 11 else 0
|
||
modul_para['range_inner'] = conf[confx2ind + 12] if len(conf) > confx2ind + 12 else 0
|
||
modul_para['display'] = Anzpara
|
||
|
||
filtlen_x2 = fun_para_static.get('filtlength', 0)
|
||
modul_para['x1limit_r'] = x3 - filtlen_x2 - NOMTAPEl
|
||
modul_para['x1rlimit_l'] = x3r + filtlen_x2 + NOMTAPEr
|
||
|
||
modul_para['minatt1'] = conf[confx2ind] if len(conf) > confx2ind else 0
|
||
modul_para['maxatt1'] = conf[confx2ind + 1] if len(conf) > confx2ind + 1 else 1
|
||
|
||
# ============ 3. mincontr动态校正(电压和路径长度影响) ============
|
||
dalpha_min = conf[confx2ind + 17] if len(conf) > confx2ind + 17 else 0
|
||
dalpha_max = conf[confx2ind + 2] if len(conf) > confx2ind + 2 else 0
|
||
s1_x3 = (strng2(x3r, x0, x0r) + strng2(x3, x0, x0r)) / 2.0
|
||
voltage = settings.get('voltage', 50) if isinstance(settings, dict) else 50
|
||
conf_x2_18 = conf[confx2ind + 18] if len(conf) > confx2ind + 18 else 0
|
||
conf_x2_19 = conf[confx2ind + 19] if len(conf) > confx2ind + 19 else 0
|
||
mincontr_raw = dalpha_max + (voltage - 50) * conf_x2_19 + (s1_x3 - 10) * conf_x2_18
|
||
mincontr = min(dalpha_max, max(dalpha_min, mincontr_raw))
|
||
modul_para['mincontr'] = mincontr
|
||
|
||
modul_para['maxcontr'] = conf[confx2ind + 3] if len(conf) > confx2ind + 3 else 100
|
||
modul_para['contrsign'] = Maximumc[3]
|
||
modul_para['resmax'] = conf[confx2ind + 4] if len(conf) > confx2ind + 4 else 1
|
||
modul_para['TolFun'] = conf[confx2ind + 5] if len(conf) > confx2ind + 5 else 1e-6
|
||
modul_para['TolX'] = conf[confx2ind + 6] if len(conf) > confx2ind + 6 else 1e-6
|
||
modul_para['MaxFunEvals'] = conf[confx2ind + 7] if len(conf) > confx2ind + 7 else 400
|
||
modul_para['LevenbergMarquardt'] = conf[confx2ind + 8] if len(conf) > confx2ind + 8 else 0
|
||
fun_para_static['mode'] = modevector[2]
|
||
|
||
NOMISCl = NOMISC
|
||
NOMISCr = NOMISC
|
||
|
||
resnorm_new = None
|
||
x2new = None
|
||
x2rnew = None
|
||
errorlevel_new = None
|
||
|
||
# ============ 4. 多步拟合策略 ============
|
||
ovrview = settings.get('ovrview', None) if isinstance(settings, dict) else None
|
||
if ovrview is not None:
|
||
# 4a. 基于k1衰减系数分布的初始值
|
||
fun_para_static_new1 = dict(fun_para_static)
|
||
errorlevel_new1 = errorlevel.copy()
|
||
|
||
Indx1_new = int(ind(fun_para_static_new1['x0'], dx, 0, len(x)))
|
||
Indx1r_new = int(ind(fun_para_static_new1['x0r'], dx, 0, len(x)))
|
||
Indx3_new = int(ind(x3, dx, 0, len(x)))
|
||
Indx3r_new = int(ind(x3r, dx, 0, len(x)))
|
||
IndOffset = int(np.floor(0.2 / dx)) if dx > 0 else 0
|
||
|
||
s1_k1 = strng2(x, fun_para_static_new1['x0'], fun_para_static_new1['x0r'], 1)
|
||
k1 = np.zeros(len(INS))
|
||
valid_mask = (s1_k1 > 0) & (INS > 0)
|
||
k1[valid_mask] = -np.log(INS[valid_mask]) / s1_k1[valid_mask]
|
||
|
||
left_start = min(Indx1_new + IndOffset, len(k1) - 1)
|
||
left_end = max(Indx3_new - IndOffset, 0)
|
||
right_start = min(Indx3r_new + IndOffset, len(k1) - 1)
|
||
right_end = max(Indx1r_new - IndOffset, 0)
|
||
|
||
limitl = np.mean(k1[left_start:left_end]) if left_end > left_start else 0
|
||
limitr = np.mean(k1[right_start:right_end]) if right_end > right_start else 0
|
||
|
||
# 找x2new: k1(1:Indx3) <= limitl 的最后一个位置
|
||
k1_left = k1[:Indx3_new]
|
||
below_limitl = np.where(k1_left <= limitl)[0]
|
||
if len(below_limitl) > 0:
|
||
Indx2new = below_limitl[-1]
|
||
fun_para_static_new1['x1'] = x[Indx2new]
|
||
else:
|
||
fun_para_static_new1['x1'] = x3 - NOMISCl
|
||
|
||
# 找x2rnew: k1(Indx3r:end) <= limitr 的第一个位置
|
||
k1_right = k1[Indx3r_new:]
|
||
below_limitr = np.where(k1_right <= limitr)[0]
|
||
if len(below_limitr) > 0:
|
||
Indx2rnew = below_limitr[0] + Indx3r_new
|
||
fun_para_static_new1['x1r'] = x[Indx2rnew]
|
||
else:
|
||
fun_para_static_new1['x1r'] = x3r + NOMISCr
|
||
|
||
NOMISCl_new1 = x3 - fun_para_static_new1['x1']
|
||
NOMISCr_new1 = fun_para_static_new1['x1r'] - x3r
|
||
fun_para_static_new1['txt_left'] = '; x2new= '
|
||
fun_para_static_new1['txt_right'] = '; x2rnew= '
|
||
|
||
|
||
calcpara_new1, errorlevel_new1[:, 19:21], resnorm_new1 = x1_x1r_curvefit4(
|
||
x, INS, fun_para_static_new1, modul_para
|
||
)
|
||
x2new1 = calcpara_new1.get('x1', fun_para_static_new1['x1']) if isinstance(calcpara_new1, dict) else fun_para_static_new1['x1']
|
||
x2rnew1 = calcpara_new1.get('x1r', fun_para_static_new1['x1r']) if isinstance(calcpara_new1, dict) else fun_para_static_new1['x1r']
|
||
x2new1, errorlevel_new1[0, 19], x2rnew1, errorlevel_new1[0, 20] = chk_transition(
|
||
x2new1, errorlevel_new1[:, 19], x2rnew1, errorlevel_new1[:, 20]
|
||
)
|
||
|
||
x2new = x2new1
|
||
x2rnew = x2rnew1
|
||
errorlevel_new = errorlevel_new1
|
||
resnorm_new = resnorm_new1
|
||
|
||
# 4b. 基于ovrview.layer1的初始值(如果ovrview.status为0)
|
||
ovrview_status = ovrview.get('status', 1) if isinstance(ovrview, dict) else 1
|
||
if ovrview_status == 0:
|
||
ovrview_axis = ovrview.get('axis', 0) if isinstance(ovrview, dict) else 0
|
||
layer3 = ovrview.get('layer3', np.zeros(8)) if isinstance(ovrview, dict) else np.zeros(8)
|
||
layer3r = ovrview.get('layer3r', np.zeros(8)) if isinstance(ovrview, dict) else np.zeros(8)
|
||
NOMOSCl_new = layer3[ovrview_axis] if ovrview_axis < len(layer3) else NOMOSC
|
||
NOMOSCr_new = layer3r[ovrview_axis] if ovrview_axis < len(layer3r) else NOMOSC
|
||
if NOMOSCl_new > NOMOSCNEW * 1.5 or NOMOSCl_new < NOMOSCNEW * 0.5:
|
||
NOMOSCl_new = NOMOSCNEW
|
||
if NOMOSCr_new > NOMOSCNEW * 1.5 or NOMOSCr_new < NOMOSCNEW * 0.5:
|
||
NOMOSCr_new = NOMOSCNEW
|
||
|
||
layer1 = ovrview.get('layer1', np.zeros(8)) if isinstance(ovrview, dict) else np.zeros(8)
|
||
layer1r = ovrview.get('layer1r', np.zeros(8)) if isinstance(ovrview, dict) else np.zeros(8)
|
||
NOMISCl_new2 = layer1[ovrview_axis] if ovrview_axis < len(layer1) else NOMISCl
|
||
NOMISCr_new2 = layer1r[ovrview_axis] if ovrview_axis < len(layer1r) else NOMISCr
|
||
|
||
fun_para_static_new2 = dict(fun_para_static)
|
||
errorlevel_new2 = errorlevel.copy()
|
||
fun_para_static_new2['x1'] = x3 - NOMISCl_new2
|
||
fun_para_static_new2['x1r'] = x3r + NOMISCr_new2
|
||
fun_para_static_new2['txt_left'] = '; x2new2= '
|
||
fun_para_static_new2['txt_right'] = '; x2rnew2= '
|
||
|
||
|
||
calcpara_new2, errorlevel_new2[:, 19:21], resnorm_new2 = x1_x1r_curvefit4(
|
||
x, INS, fun_para_static_new2, modul_para
|
||
)
|
||
x2new2 = calcpara_new2.get('x1', fun_para_static_new2['x1']) if isinstance(calcpara_new2, dict) else fun_para_static_new2['x1']
|
||
x2rnew2 = calcpara_new2.get('x1r', fun_para_static_new2['x1r']) if isinstance(calcpara_new2, dict) else fun_para_static_new2['x1r']
|
||
x2new2, errorlevel_new2[0, 19], x2rnew2, errorlevel_new2[0, 20] = chk_transition(
|
||
x2new2, errorlevel_new2[:, 19], x2rnew2, errorlevel_new2[:, 20]
|
||
)
|
||
|
||
# 4c. 对比NEW1和NEW2,选最优
|
||
rn1_l = resnorm_new1[0] if isinstance(resnorm_new1, np.ndarray) else resnorm_new1
|
||
rn2_l = resnorm_new2[0] if isinstance(resnorm_new2, np.ndarray) else resnorm_new2
|
||
rn1_r = resnorm_new1[1] if isinstance(resnorm_new1, np.ndarray) and len(resnorm_new1) > 1 else resnorm_new1
|
||
rn2_r = resnorm_new2[1] if isinstance(resnorm_new2, np.ndarray) and len(resnorm_new2) > 1 else resnorm_new2
|
||
|
||
err_new1_l = errorlevel_new1[0, 19]
|
||
err_new2_l = errorlevel_new2[0, 19]
|
||
err_new1_r = errorlevel_new1[0, 20]
|
||
err_new2_r = errorlevel_new2[0, 20]
|
||
|
||
if (((rn1_l > rn2_l and err_new2_l == 0) or
|
||
(err_new1_l != 0 and err_new2_l == 0)) and
|
||
x2new2 < x3 - 0.3 and
|
||
(x2new2 > x3 - NOMISCl * 0.5 or x2new2 < x3 - NOMISCl * 1.5)):
|
||
x2new = x2new2
|
||
errorlevel_new[:, 19] = errorlevel_new2[:, 19]
|
||
resnorm_new[0] = rn2_l
|
||
else:
|
||
pass
|
||
if (((rn1_r > rn2_r and err_new2_r == 0) or
|
||
(err_new1_r != 0 and err_new2_r == 0)) and
|
||
x2rnew2 > x3r + 0.3 and
|
||
(x2rnew2 < x3r + NOMISCr * 0.5 or x2rnew2 > x3r + NOMISCr * 1.5)):
|
||
x2rnew = x2rnew2
|
||
errorlevel_new[:, 20] = errorlevel_new2[:, 20]
|
||
resnorm_new[1] = rn2_r
|
||
else:
|
||
pass
|
||
|
||
# ============ 5. 标称值拟合 ============
|
||
fun_para_static['x1'] = x3 - NOMISCl
|
||
fun_para_static['x1r'] = x3r + NOMISCr
|
||
fun_para_static['txt_left'] = '; x2= '
|
||
fun_para_static['txt_right'] = '; x2r= '
|
||
|
||
x2_initial = x3 - NOMISCl
|
||
x2r_initial = x3r + NOMISCr
|
||
|
||
|
||
calcpara_x2, err_20_21, resnorm = x1_x1r_curvefit4(x, INS, fun_para_static, modul_para)
|
||
|
||
if isinstance(calcpara_x2, dict):
|
||
x2 = calcpara_x2.get('left', {}).get('pos', {}).get('result', np.nan)
|
||
x2r = calcpara_x2.get('right', {}).get('pos', {}).get('result', np.nan)
|
||
if not np.isfinite(x2):
|
||
x2 = calcpara_x2.get('x1', x3 - NOMISCl)
|
||
if not np.isfinite(x2r):
|
||
x2r = calcpara_x2.get('x1r', x3r + NOMISCr)
|
||
else:
|
||
x2 = calcpara_x2[0] if len(calcpara_x2) > 0 else x3 - NOMISCl
|
||
x2r = calcpara_x2[1] if len(calcpara_x2) > 1 else x3r + NOMISCr
|
||
|
||
if isinstance(err_20_21, np.ndarray) and err_20_21.shape[1] >= 2:
|
||
errorlevel[:, 19] = err_20_21[:, 0]
|
||
errorlevel[:, 20] = err_20_21[:, 1]
|
||
|
||
x2, errorlevel[:, 19], x2r, errorlevel[:, 20] = chk_transition(
|
||
x2, errorlevel[:, 19], x2r, errorlevel[:, 20]
|
||
)
|
||
|
||
# ============ 6. 标称值结果与NEW结果对比选优 ============
|
||
if resnorm_new is not None and x2new is not None:
|
||
rn_l = resnorm[0] if isinstance(resnorm, np.ndarray) else resnorm
|
||
rn_new_l = resnorm_new[0] if isinstance(resnorm_new, np.ndarray) else resnorm_new
|
||
rn_r = resnorm[1] if isinstance(resnorm, np.ndarray) and len(resnorm) > 1 else resnorm
|
||
rn_new_r = resnorm_new[1] if isinstance(resnorm_new, np.ndarray) and len(resnorm_new) > 1 else resnorm_new
|
||
|
||
err_l = errorlevel[0, 19]
|
||
err_new_l = errorlevel_new[0, 19]
|
||
err_r = errorlevel[0, 20]
|
||
err_new_r = errorlevel_new[0, 20]
|
||
|
||
if (((rn_l > rn_new_l and err_new_l == 0) or
|
||
(err_l != 0 and err_new_l == 0)) and
|
||
x2new < x3 - 0.3 and
|
||
(x2new > x3 - NOMISCl * 0.5 or x2new < x3 - NOMISCl * 1.5)):
|
||
x2 = x2new
|
||
errorlevel[:, 19] = errorlevel_new[:, 19]
|
||
|
||
if (((rn_r > rn_new_r and err_new_r == 0) or
|
||
(err_r != 0 and err_new_r == 0)) and
|
||
x2rnew > x3r + 0.3 and
|
||
(x2rnew < x3r + NOMISCr * 0.5 or x2rnew > x3r + NOMISCr * 1.5)):
|
||
x2r = x2rnew
|
||
errorlevel[:, 20] = errorlevel_new[:, 20]
|
||
|
||
|
||
except Exception as e:
|
||
import traceback
|
||
traceback.print_exc()
|
||
x2, x2r = 0, 0
|
||
|
||
if errorlevel[0, 11] == 4 and errorlevel[0, 12] == 4:
|
||
errorlevel[0, 11] = 6
|
||
errorlevel[0, 12] = 6
|
||
|
||
if abs(conf20 - 123) < 1e-6:
|
||
x3 = x3 + offsetCORE
|
||
x3r = x3r - offsetCORE
|
||
|
||
mode_out = 'riffle' if abs(conf20 - 115) < 1e-6 else 'analy01'
|
||
|
||
edgesleft = np.array([x0, x1, x2, x3], dtype=np.float64)
|
||
edgesright = np.array([x0r, x1r, x2r, x3r], dtype=np.float64)
|
||
|
||
|
||
edgesleft_out, edgesright_out, status, statusL, statusR = errlevel2stat(
|
||
edgesleft, edgesright, errorlevel, layeruse, conf20, mode_out
|
||
)
|
||
|
||
attL = np.zeros(3)
|
||
attR = np.zeros(3)
|
||
|
||
return (edgesleft_out.reshape(1, -1), edgesright_out.reshape(1, -1),
|
||
status, statusL, statusR, attL, attR, errorlevel, layeruse, IN, detail)
|
||
|
||
|
||
def _base_analysis(I, conf, errorin, num_layers=3):
|
||
from edge_detection_utils import i0detect
|
||
from detection_utils import cxlr11
|
||
from math_utils import ind
|
||
|
||
conf = np.asarray(conf, dtype=np.float64).ravel()
|
||
errorin = np.asarray(errorin, dtype=np.float64).ravel()
|
||
if len(errorin) < 2:
|
||
errorin = np.zeros(2)
|
||
|
||
dx = conf[91] if len(conf) > 91 else 0.048
|
||
dx = float(dx) if dx is not None else 0.048
|
||
if dx == 0:
|
||
dx = 0.048
|
||
NOMDIA = conf[0] if len(conf) > 0 else 0
|
||
NOMCOND = conf[1] if len(conf) > 1 else 0
|
||
NOMOSC = conf[2] if len(conf) > 2 else 0
|
||
NOMISC = conf[3] if len(conf) > 3 else 0
|
||
TAPE = conf[4] if len(conf) > 4 else 0
|
||
NOMINS = conf[8] if len(conf) > 8 else 0
|
||
DistI0 = conf[34] if len(conf) > 34 else 1000
|
||
Lowrad = conf[36] if len(conf) > 36 else 20000
|
||
|
||
I0, errorlevel = i0detect(I, DistI0, dx, Lowrad, errorin)
|
||
|
||
onlydiameterused = (abs(conf[19] - 50) < 1e-6) if len(conf) > 19 else False
|
||
layeruse = np.array([(NOMOSC > 0), (NOMINS > 0), (NOMISC > 0)], dtype=np.float64)
|
||
|
||
if onlydiameterused:
|
||
layeruse = np.array([0, 0, 0])
|
||
elif layeruse[0] == 0 and layeruse[2] == 0 and layeruse[1] == 0:
|
||
layeruse = np.array([1, 0, 0])
|
||
|
||
xL = np.zeros((1, num_layers + 1))
|
||
xR = np.zeros((1, num_layers + 1))
|
||
status = np.zeros((1, num_layers + 1))
|
||
statusL = np.zeros((1, num_layers + 1))
|
||
statusR = np.zeros((1, num_layers + 1))
|
||
attL = np.zeros(2)
|
||
attR = np.zeros(2)
|
||
|
||
count = len(I)
|
||
|
||
num_layers_to_detect = int(np.sum(layeruse)) + 1
|
||
if num_layers_to_detect < 2:
|
||
num_layers_to_detect = 2
|
||
|
||
try:
|
||
xL11, xL00, errL, xR11, xR00, errR, xL41, xR41, xlr41det = cxlr11(I, I0, 0, conf)
|
||
|
||
xL[0, 0] = xL11
|
||
xR[0, 0] = xR11
|
||
if num_layers >= 1:
|
||
xL[0, 1] = xL41 if xL41 > xL11 else xL11
|
||
xR[0, 1] = xR41 if (xR41 > 0 and xR41 < xR11) else xR11
|
||
statusL[0, 0] = errL
|
||
statusR[0, 0] = errR
|
||
|
||
if num_layers_to_detect > 2:
|
||
cur_xL = xL11
|
||
cur_xR = xR11
|
||
|
||
for layer_idx in range(1, num_layers_to_detect):
|
||
if layer_idx > num_layers:
|
||
break
|
||
|
||
left_start_idx = int(round(cur_xL / dx))
|
||
right_end_idx = int(round(cur_xR / dx))
|
||
|
||
left_start_idx = max(0, min(count - 1, left_start_idx))
|
||
right_end_idx = max(0, min(count, right_end_idx))
|
||
|
||
if right_end_idx - left_start_idx < 20:
|
||
break
|
||
|
||
I_inner = I[left_start_idx:right_end_idx].copy()
|
||
|
||
if len(I_inner) < 20:
|
||
break
|
||
|
||
try:
|
||
inner_xL11, inner_xL00, inner_errL, inner_xR11, inner_xR00, inner_errR, \
|
||
inner_xL41, inner_xR41, inner_xlr41det = cxlr11(I_inner, I0, 0, conf)
|
||
|
||
inner_xL = inner_xL11 + cur_xL
|
||
inner_xR = inner_xR11 + cur_xL
|
||
|
||
if inner_xL > cur_xL + 0.5:
|
||
xL[0, layer_idx] = inner_xL
|
||
cur_xL = inner_xL
|
||
if inner_xR < cur_xR - 0.5 and inner_xR > cur_xL:
|
||
xR[0, layer_idx] = inner_xR
|
||
cur_xR = inner_xR
|
||
|
||
statusL[0, layer_idx] = inner_errL
|
||
statusR[0, layer_idx] = inner_errR
|
||
|
||
except Exception:
|
||
break
|
||
|
||
except Exception:
|
||
pass
|
||
|
||
IN = np.array(I) if I is not None else np.array([])
|
||
|
||
return xL, xR, status, statusL, statusR, attL, attR, errorlevel, layeruse, IN
|
||
|
||
|
||
def analy01(I, conf, errorin):
|
||
conf = np.asarray(conf, dtype=np.float64).ravel()
|
||
errorin = np.asarray(errorin, dtype=np.float64).ravel()
|
||
|
||
if len(conf) > 19 and abs(conf[19] - 21) < 1e-6:
|
||
if len(conf) > 178:
|
||
conf[33] = conf[174]
|
||
conf[34] = conf[175]
|
||
conf[39] = conf[178]
|
||
|
||
xL, xR, status, statusL, statusR, attL, attR, errorlevel, layeruse, IN = _base_analysis(
|
||
I, conf, errorin, num_layers=3
|
||
)
|
||
return xL, xR, status, statusL, statusR, attL, attR, errorlevel, layeruse, IN
|
||
|
||
|
||
def anajack2(I, conf, settings, ovrvwmem, errorin):
|
||
xL, xR, status, statusL, statusR, attL, attR, errorlevel, layeruse, IN = _base_analysis(
|
||
I, conf, errorin, num_layers=3
|
||
)
|
||
return xL, xR, status, statusL, statusR, attL, attR, errorlevel, layeruse, ovrvwmem, IN
|
||
|
||
|
||
def ana_curvefit_tube(I, conf, errorin):
|
||
xL, xR, status, statusL, statusR, attL, attR, errorlevel, layeruse, IN = _base_analysis(
|
||
I, conf, errorin, num_layers=3
|
||
)
|
||
return xL, xR, status, statusL, statusR, attL, attR, errorlevel, layeruse, IN
|
||
|
||
|
||
def ana_curvefit_Uponor(I, conf, errorin):
|
||
xL, xR, status, statusL, statusR, attL, attR, errorlevel, layeruse, IN = _base_analysis(
|
||
I, conf, errorin, num_layers=3
|
||
)
|
||
return xL, xR, status, statusL, statusR, attL, attR, errorlevel, layeruse, IN
|
||
|
||
|
||
def ana_curvefit_2l_tube(I, conf, errorin, ovrvwmem):
|
||
xL, xR, status, statusL, statusR, attL, attR, errorlevel, layeruse, IN = _base_analysis(
|
||
I, conf, errorin, num_layers=3
|
||
)
|
||
return xL, xR, status, statusL, statusR, attL, attR, errorlevel, layeruse, ovrvwmem, IN
|