806 lines
28 KiB
Python
806 lines
28 KiB
Python
import numpy as np
|
|
from scipy.optimize import least_squares
|
|
|
|
try:
|
|
from .math_utils import ind, polyfit2
|
|
from .geometry_utils import strng2
|
|
from .signal_utils import filter2k3
|
|
from .curve_fit_utils import (
|
|
attcalcx0, attcalcx1, x1fun3, x1fun3setup, x1fun3out,
|
|
_simple_lm
|
|
)
|
|
from .edge_detection_utils import i0detect
|
|
except ImportError:
|
|
from math_utils import ind, polyfit2
|
|
from geometry_utils import strng2
|
|
from signal_utils import filter2k3
|
|
from curve_fit_utils import (
|
|
attcalcx0, attcalcx1, x1fun3, x1fun3setup, x1fun3out,
|
|
_simple_lm
|
|
)
|
|
from edge_detection_utils import i0detect
|
|
|
|
|
|
def gauss(s, k):
|
|
s = float(s)
|
|
k = int(k)
|
|
n = np.arange(-(k - 1) / 2, (k - 1) / 2 + 1)
|
|
g = np.exp(-n ** 2 / (2 * s ** 2))
|
|
g = g / np.sum(g)
|
|
return g
|
|
|
|
|
|
def _resfail(resnorm, lengthxt, avgval):
|
|
if avgval > 0 and lengthxt > 0:
|
|
return resnorm / (lengthxt * avgval)
|
|
return 0.0
|
|
|
|
|
|
def edge_error2(calcval, exitflag, resnorm, avgval, startalpha0,
|
|
minxt, maxxt, minalpha0, maxalpha0,
|
|
mincontr, maxcontr, contrsign,
|
|
resmax, diffxt1, diffxt2, lengthxt):
|
|
errorl = np.zeros(2, dtype=np.float64)
|
|
|
|
if exitflag < 0:
|
|
errorl[0] = 51
|
|
else:
|
|
if calcval.get('x', 0) < (minxt + diffxt1) or calcval.get('x', 0) > (maxxt - diffxt1):
|
|
errorl[1] = 47
|
|
if calcval.get('x', 0) < (minxt + diffxt2) or calcval.get('x', 0) > (maxxt - diffxt2):
|
|
errorl[0] = 47
|
|
if calcval.get('alpha0', 0) < minalpha0:
|
|
errorl[0] = 52
|
|
if calcval.get('alpha0', 0) > maxalpha0:
|
|
errorl[0] = 53
|
|
if abs(calcval.get('alpha1', 0) - calcval.get('alpha0', 0)) < mincontr:
|
|
errorl[0] = 54
|
|
kontrast = calcval.get('alpha1', 0) - calcval.get('alpha0', 0)
|
|
if ((kontrast < 0 and contrsign == 1) or
|
|
(kontrast > 0 and contrsign == -1)):
|
|
errorl[0] = 56
|
|
relfail = _resfail(resnorm, lengthxt, avgval)
|
|
if relfail > resmax:
|
|
errorl[0] = 57
|
|
if calcval.get('x', 0) < minxt or calcval.get('x', 0) > maxxt:
|
|
errorl[0] = 46
|
|
|
|
return errorl
|
|
|
|
|
|
def mcoldef(errorl):
|
|
errorl = np.asarray(errorl, dtype=np.float64).ravel()
|
|
if errorl[0]:
|
|
return 'r-'
|
|
elif len(errorl) > 1 and errorl[1]:
|
|
return 'm-'
|
|
else:
|
|
return 'g-'
|
|
|
|
|
|
def attfacthres(k1, dx, ll, leftdetect, Anzpara=0):
|
|
k1 = np.asarray(k1, dtype=np.float64).ravel()
|
|
|
|
leftindlimit = np.array([], dtype=np.int64)
|
|
|
|
hdiffwidth = 0.1
|
|
difflimit = 0.02
|
|
diffoff = 0.4
|
|
|
|
klow2thresh1 = 1.6
|
|
klow2thresh2 = 1.4
|
|
klow2thresh3 = 1.3
|
|
|
|
hdiffwind = int(np.round(hdiffwidth / dx))
|
|
if hdiffwind < 1:
|
|
hdiffwind = 1
|
|
|
|
filtpara = np.concatenate([
|
|
-np.ones(hdiffwind), [0.0], np.ones(hdiffwind)
|
|
]) / hdiffwind
|
|
filtpara = filtpara / ((hdiffwind + 1) * dx)
|
|
|
|
k1diff, _ = filter2k3(filtpara, k1, 2)
|
|
|
|
diffoffind = int(np.round(diffoff / dx))
|
|
if diffoffind > len(k1diff):
|
|
diffoffind = len(k1diff)
|
|
|
|
diffoffvek = np.ones(len(k1diff), dtype=np.float64)
|
|
if diffoffind > 0:
|
|
diffoffvek[:diffoffind] = 0.0
|
|
|
|
k1trueind = np.where(
|
|
(k1diff < difflimit) & (k1diff > -difflimit) & (diffoffvek > 0)
|
|
)[0]
|
|
|
|
if len(k1trueind) > 0:
|
|
klow1 = np.cumsum(k1[k1trueind])
|
|
klow1 = klow1 / np.arange(1, len(k1trueind) + 1)
|
|
|
|
klow2 = np.full(len(k1), np.nan)
|
|
klow2[k1trueind] = klow1
|
|
|
|
oldvalue = np.nan
|
|
for ii in range(len(klow2)):
|
|
if np.isnan(klow2[ii]):
|
|
klow2[ii] = oldvalue
|
|
else:
|
|
oldvalue = klow2[ii]
|
|
|
|
valid_mask = ~np.isnan(klow2)
|
|
if np.any(valid_mask):
|
|
idx1 = np.where(k1[valid_mask] >= klow2[valid_mask] * klow2thresh1)[0]
|
|
if len(idx1) > 0:
|
|
leftindlimit1 = np.where(valid_mask)[0][idx1[0]]
|
|
else:
|
|
return leftindlimit
|
|
|
|
idx2 = np.where(k1[:leftindlimit1 + 1] < klow2[:leftindlimit1 + 1] * klow2thresh2)[0]
|
|
if len(idx2) > 0:
|
|
leftindlimit2 = idx2[-1]
|
|
else:
|
|
return leftindlimit
|
|
|
|
leftindlimit3vek = np.where(k1[:leftindlimit2 + 1] >= klow2[:leftindlimit2 + 1] * klow2thresh2)[0]
|
|
|
|
leftindlimit = np.array([leftindlimit2])
|
|
|
|
runwhile = True
|
|
searchind = 0
|
|
while runwhile:
|
|
if searchind < len(leftindlimit3vek):
|
|
seg_start = leftindlimit3vek[searchind]
|
|
seg_end = leftindlimit2
|
|
seg_mean = np.mean(k1[seg_start:seg_end + 1])
|
|
thresh_mean = np.mean(klow2[seg_start:seg_end + 1] * klow2thresh3)
|
|
if seg_mean >= thresh_mean:
|
|
leftindlimit = np.array([leftindlimit3vek[searchind]])
|
|
runwhile = False
|
|
else:
|
|
runwhile = False
|
|
searchind += 1
|
|
|
|
return leftindlimit
|
|
|
|
|
|
def x1_x1r_predetect(x, IN, x0, x0r, dx, Anzpara=0):
|
|
IN = np.asarray(IN, dtype=np.float64).ravel()
|
|
x = np.asarray(x, dtype=np.float64).ravel()
|
|
errorl = np.zeros((2, 2), dtype=np.float64)
|
|
prex1 = 0.0
|
|
prex1r = 0.0
|
|
|
|
NN = len(IN)
|
|
|
|
ll = int(ind(x0 + dx, dx, 0, NN))
|
|
rr = int(ind(0.5 * (x0 + x0r), dx, 0, NN))
|
|
if ll < rr and ll >= 0 and rr <= NN:
|
|
x_seg = x[ll:rr]
|
|
s1 = strng2(x_seg, x0, x0r, 1)
|
|
IN_seg = IN[ll:rr]
|
|
valid = (IN_seg > 0) & (s1 > 0)
|
|
if np.any(valid):
|
|
k1 = -(np.log(IN_seg[valid])) / s1[valid]
|
|
leftdetect = 1
|
|
leftindlimit = attfacthres(k1, dx, ll, leftdetect, Anzpara)
|
|
if len(leftindlimit) > 0:
|
|
kvalid_idx = np.where(valid)[0]
|
|
if leftindlimit[0] < len(kvalid_idx):
|
|
prex1 = x_seg[kvalid_idx[leftindlimit[0]]]
|
|
else:
|
|
prex1 = 0.0
|
|
errorl[0, 0] = 80
|
|
else:
|
|
prex1 = 0.0
|
|
errorl[0, 0] = 80
|
|
else:
|
|
errorl[0, 0] = 80
|
|
else:
|
|
errorl[0, 0] = 80
|
|
|
|
ll_r = int(ind(0.5 * (x0 + x0r), dx, 0, NN))
|
|
rr_r = int(ind(x0r - dx, dx, 0, NN))
|
|
if ll_r < rr_r and ll_r >= 0 and rr_r <= NN:
|
|
x_seg_r = x[ll_r:rr_r]
|
|
s1_r = strng2(x_seg_r, x0, x0r, 1)
|
|
IN_seg_r = IN[ll_r:rr_r]
|
|
valid_r = (IN_seg_r > 0) & (s1_r > 0)
|
|
if np.any(valid_r):
|
|
k1_r = -(np.log(IN_seg_r[valid_r])) / s1_r[valid_r]
|
|
k1_r_rev = k1_r[::-1]
|
|
leftdetect = 0
|
|
rightindlimit = attfacthres(k1_r_rev, dx, ll_r, leftdetect, Anzpara)
|
|
if len(rightindlimit) > 0:
|
|
rightindlimit_orig = len(k1_r) - rightindlimit[0]
|
|
kvalid_idx_r = np.where(valid_r)[0]
|
|
if rightindlimit_orig >= 0 and rightindlimit_orig < len(kvalid_idx_r):
|
|
prex1r = x_seg_r[kvalid_idx_r[rightindlimit_orig]]
|
|
else:
|
|
prex1r = 0.0
|
|
errorl[1, 1] = 80
|
|
else:
|
|
prex1r = 0.0
|
|
errorl[1, 1] = 80
|
|
else:
|
|
errorl[1, 1] = 80
|
|
else:
|
|
errorl[1, 1] = 80
|
|
|
|
return prex1, prex1r, errorl
|
|
|
|
|
|
def Autocxlr11(IN, I0, Anzpara, conf):
|
|
IN = np.asarray(IN, dtype=np.float64).ravel()
|
|
conf = np.asarray(conf, dtype=np.float64).ravel()
|
|
|
|
TubePegel = 0.03
|
|
CablePegel = 0.02
|
|
TubeTol = 30
|
|
CableTol = 15
|
|
DistIndMin = 100
|
|
IntDiff = 0.15
|
|
IntRohr = 0.05
|
|
|
|
dx = conf[91] if len(conf) > 91 else 0.1
|
|
dx = float(dx) if dx is not None else 0.1
|
|
if dx == 0:
|
|
dx = 0.1
|
|
xl01 = 0.0
|
|
xr01 = 0.0
|
|
xl11 = 0.0
|
|
xr11 = 0.0
|
|
xl41 = 0.0
|
|
xr41 = 0.0
|
|
xlr41det = 0
|
|
errorl = 0
|
|
errorr = 0
|
|
count = len(IN)
|
|
Tube = 0
|
|
|
|
filtlength = 11
|
|
rsigma = 0.7
|
|
filtvek = gauss(rsigma * 0.5 * (filtlength - 1), filtlength)
|
|
INF, N2 = filter2k3(filtvek, IN)
|
|
|
|
Imin, IndImin = np.min(INF), np.argmin(INF)
|
|
ObjInd = IndImin
|
|
ObjIndL = ObjInd
|
|
ObjIndR = ObjInd
|
|
|
|
Imaxl = np.max(INF[:ObjIndL + 1]) if ObjIndL >= 0 else 0
|
|
Imaxr = np.max(INF[ObjIndR:]) if ObjIndR < len(INF) else 0
|
|
|
|
Bandbr = 0.98
|
|
DistI0 = conf[34] if len(conf) > 34 else 0
|
|
dxZeile = conf[89] if len(conf) > 89 else dx
|
|
midcount = int(np.round(DistI0 / dxZeile)) if dxZeile > 0 else 0
|
|
|
|
Iindl = np.where(INF[:ObjIndL + 1] > Bandbr * Imaxl)[0]
|
|
Iindr = np.where(INF[ObjIndR:] > Bandbr * Imaxr)[0] + ObjIndR
|
|
|
|
if len(Iindl) > midcount and len(Iindr) > midcount:
|
|
Il = np.mean(IN[Iindl])
|
|
Ir = np.mean(IN[Iindr])
|
|
elif len(Iindl) > midcount and len(Iindr) <= midcount:
|
|
Il = np.mean(IN[Iindl])
|
|
Ir = Il
|
|
elif len(Iindl) <= midcount and len(Iindr) > midcount:
|
|
Ir = np.mean(IN[Iindr])
|
|
Il = Ir
|
|
else:
|
|
errorl = 1
|
|
errorr = 1
|
|
return xl11, errorl, xr11, errorr, xl41, xr41, Tube
|
|
|
|
def find_last_ge(arr, thresh, start, end):
|
|
seg = arr[start:end + 1]
|
|
idx = np.where(seg >= thresh)[0]
|
|
if len(idx) > 0:
|
|
return idx[-1] + start
|
|
return None
|
|
|
|
def find_first_ge(arr, thresh, start, end):
|
|
seg = arr[start:end + 1]
|
|
idx = np.where(seg >= thresh)[0]
|
|
if len(idx) > 0:
|
|
return idx[0] + start
|
|
return None
|
|
|
|
xl01_98 = find_last_ge(INF, 0.98 * Il, 0, ObjIndL)
|
|
xl01_98 = xl01_98 * dxZeile if xl01_98 is not None else 0
|
|
|
|
xr01_98 = find_first_ge(INF, 0.98 * Ir, ObjIndR, len(INF) - 1)
|
|
xr01_98 = xr01_98 * dxZeile if xr01_98 is not None else 0
|
|
|
|
xl01_96 = find_last_ge(INF, 0.96 * Il, 0, ObjIndL)
|
|
xl01_96 = xl01_96 * dxZeile if xl01_96 is not None else 0
|
|
|
|
xr01_96 = find_first_ge(INF, 0.96 * Ir, ObjIndR, len(INF) - 1)
|
|
xr01_96 = xr01_96 * dxZeile if xr01_96 is not None else 0
|
|
|
|
xl01_94 = find_last_ge(INF, 0.94 * Il, 0, ObjIndL)
|
|
xl01_94 = xl01_94 * dxZeile if xl01_94 is not None else 0
|
|
|
|
xr01_94 = find_first_ge(INF, 0.94 * Ir, ObjIndR, len(INF) - 1)
|
|
xr01_94 = xr01_94 * dxZeile if xr01_94 is not None else 0
|
|
|
|
xl01_90 = find_last_ge(INF, 0.90 * Il, 0, ObjIndL)
|
|
xl01_90 = xl01_90 * dxZeile if xl01_90 is not None else 0
|
|
|
|
xr01_90 = find_first_ge(INF, 0.90 * Ir, ObjIndR, len(INF) - 1)
|
|
xr01_90 = xr01_90 * dxZeile if xr01_90 is not None else 0
|
|
|
|
xl01_70 = find_last_ge(INF, 0.70 * Il, 0, ObjIndL)
|
|
xl01_70 = xl01_70 * dxZeile if xl01_70 is not None else 0
|
|
|
|
xr01_70 = find_first_ge(INF, 0.70 * Ir, ObjIndR, len(INF) - 1)
|
|
xr01_70 = xr01_70 * dxZeile if xr01_70 is not None else 0
|
|
|
|
xl01 = xl01_96
|
|
xr01 = xr01_96
|
|
|
|
IntDiffl = xl01_70 - xl01_96
|
|
IntDiffr = xr01_96 - xr01_70
|
|
|
|
if IntDiffl < IntDiff or IntDiffr < IntDiff:
|
|
if (Imin < TubePegel and abs(IntDiffl - IntDiffr) < IntRohr and
|
|
xl01_96 > TubeTol and xr01_96 > TubeTol):
|
|
Tube = 1
|
|
|
|
xl41 = xl01_98
|
|
xr41 = xr01_98
|
|
|
|
xl11 = xl01
|
|
xr11 = xr01
|
|
|
|
return xl11, errorl, xr11, errorr, xl41, xr41, Tube
|
|
|
|
|
|
def AutoDetect(I, conf, errorin, CableType):
|
|
I = np.asarray(I, dtype=np.float64).ravel()
|
|
conf = np.asarray(conf, dtype=np.float64).ravel()
|
|
|
|
AnzPict = 10
|
|
xlr11Offset = 30
|
|
|
|
FoamedPE = 0.014
|
|
PE = 0.08
|
|
PVC = 0.25
|
|
|
|
errorlevel = np.zeros((2, 1), dtype=np.float64)
|
|
errorl = 0
|
|
errorr = 0
|
|
DistI0 = conf[34] if len(conf) > 34 else 0
|
|
Lowrad = conf[36] if len(conf) > 36 else 0
|
|
dx = conf[91] if len(conf) > 91 else 0.1
|
|
dx = float(dx) if dx is not None else 0.1
|
|
if dx == 0:
|
|
dx = 0.1
|
|
AnzPara = conf[10] if len(conf) > 10 else 0
|
|
|
|
I0, errorlevel_1d = i0detect(I, DistI0, dx, Lowrad, errorin)
|
|
errorlevel = np.atleast_2d(errorlevel_1d).reshape(-1, 1)
|
|
if errorlevel.shape[0] < 2:
|
|
errorlevel = np.pad(errorlevel, ((0, 2 - errorlevel.shape[0]), (0, 0)))
|
|
IN = I.copy()
|
|
|
|
IN = I / I0 if I0 > 0 else I
|
|
xl11, errorl, xr11, errorr, xl41, xr41, Tube = Autocxlr11(IN, I0, AnzPara, conf)
|
|
if errorl != 0:
|
|
errorlevel[0, 0] = errorl
|
|
elif errorr != 0:
|
|
errorlevel[0, 0] = errorr
|
|
|
|
if CableType.get('Counter', 0) == AnzPict:
|
|
CableType_Number = CableType.get('Number', np.zeros(4))
|
|
temp, ID = np.max(CableType_Number), np.argmax(CableType_Number)
|
|
if ID == 0:
|
|
if temp == 0:
|
|
if Tube == 0:
|
|
CableType['Type'] = 50
|
|
elif Tube >= 1:
|
|
CableType['Type'] = 139
|
|
elif temp != 0:
|
|
CableType['Type'] = 7
|
|
CableType['NOMOSC'] = 0
|
|
CableType['NOMINS'] = 0
|
|
CableType['NOMISC'] = 0
|
|
elif ID == 1:
|
|
if temp == 0:
|
|
CableType['Type'] = 121
|
|
CableType['NOMOSC'] = 0.4
|
|
CableType['NOMINS'] = 0
|
|
CableType['NOMISC'] = 0
|
|
else:
|
|
CableType['Type'] = 7
|
|
CableType['NOMOSC'] = 0
|
|
CableType['NOMINS'] = 0
|
|
CableType['NOMISC'] = 0
|
|
elif ID == 2:
|
|
if temp == 0:
|
|
CableType['Type'] = 119
|
|
CableType['NOMOSC'] = 0.5
|
|
CableType['NOMINS'] = 0
|
|
CableType['NOMISC'] = 0
|
|
else:
|
|
CableType['Type'] = 7
|
|
CableType['NOMOSC'] = 0
|
|
CableType['NOMINS'] = 0
|
|
CableType['NOMISC'] = 0
|
|
elif ID == 3:
|
|
CableType['Type'] = 118
|
|
CableType['NOMOSC'] = 0.6
|
|
CableType['NOMINS'] = 0
|
|
CableType['NOMISC'] = 0
|
|
|
|
CableType['Counter'] = 0
|
|
|
|
if CableType.get('Counter', 0) < AnzPict:
|
|
if errorlevel[0, 0] == 0:
|
|
xlr11 = abs(xr11 - xl11)
|
|
if xlr11 > 0:
|
|
xlr41 = abs(xr41 - xl41)
|
|
xlrdiff = xlr11 - xlr41
|
|
if xlrdiff > CableTol:
|
|
if 'Number' not in CableType:
|
|
CableType['Number'] = np.zeros(4)
|
|
CableType['Number'][0] += 1
|
|
elif xlr11Offset < xlr11 < 2 * xlr11Offset:
|
|
CableType.setdefault('Number', np.zeros(4))
|
|
CableType['Number'][3] += 1
|
|
else:
|
|
att = -np.log(max(Imin, 1e-6)) / max(xlr11, 1e-6) if xlr11 > 0 else 0
|
|
Imin = np.min(IN) if errorlevel[0, 0] == 0 else 0
|
|
if att < FoamedPE:
|
|
CableType.setdefault('Number', np.zeros(4))
|
|
CableType['Number'][1] += 1
|
|
elif att < PE:
|
|
CableType.setdefault('Number', np.zeros(4))
|
|
CableType['Number'][0] += 1
|
|
elif att < PVC:
|
|
CableType.setdefault('Number', np.zeros(4))
|
|
CableType['Number'][2] += 1
|
|
else:
|
|
CableType.setdefault('Number', np.zeros(4))
|
|
CableType['Number'][0] += 1
|
|
CableType['Counter'] = CableType.get('Counter', 0) + 1
|
|
|
|
return CableType, errorlevel
|
|
|
|
|
|
def x1_x1r_curvefit4(x, y, fun_para_static, modul_para):
|
|
x = np.asarray(x, dtype=np.float64).ravel()
|
|
y = np.asarray(y, dtype=np.float64).ravel()
|
|
|
|
errorlevel = np.zeros((2, 2), dtype=np.float64)
|
|
calcpara = np.zeros(4, dtype=np.float64)
|
|
|
|
dataout = {}
|
|
dataout['alpha0'] = 0.0
|
|
dataout['x1'] = 0.0
|
|
dataout['x1r'] = 0.0
|
|
dataout['alpha1'] = 0.0
|
|
resnorm = np.ones(2, dtype=np.float64)
|
|
|
|
dataout['left'] = {'pos': {'result': np.nan},
|
|
'att': {'outer': np.nan, 'inner': np.nan}}
|
|
dataout['right'] = {'pos': {'result': np.nan},
|
|
'att': {'outer': np.nan, 'inner': np.nan}}
|
|
|
|
if len(x) < 2:
|
|
return dataout, errorlevel, resnorm
|
|
|
|
dx = x[1] - x[0]
|
|
N = len(x)
|
|
|
|
tol_fun = modul_para.get('TolFun', 1e-6)
|
|
tol_x = modul_para.get('TolX', 1e-6)
|
|
max_iter = modul_para.get('MaxFunEvals', 400)
|
|
display = modul_para.get('display', 0)
|
|
|
|
x1_val = fun_para_static.get('x1', 0)
|
|
x0_val = fun_para_static.get('x0', 0)
|
|
x0r_val = fun_para_static.get('x0r', 0)
|
|
filtlength = fun_para_static.get('filtlength', 0)
|
|
range_outer = modul_para.get('range_outer', 0)
|
|
range_inner = modul_para.get('range_inner', 0)
|
|
x1limit_l = modul_para.get('x1limit_l', 0)
|
|
x1limit_r = modul_para.get('x1limit_r', 0)
|
|
|
|
leftcut = x1_val - range_outer - filtlength
|
|
if leftcut < (x1limit_l - filtlength):
|
|
leftcut = x1limit_l - filtlength
|
|
rightcut = x1_val + range_inner + filtlength
|
|
if rightcut > (x1limit_r + filtlength):
|
|
rightcut = x1limit_r + filtlength
|
|
|
|
l_idx = int(ind(leftcut, dx, 0, N))
|
|
r_idx = int(ind(rightcut, dx, 0, N))
|
|
|
|
if l_idx < r_idx and l_idx >= 0 and r_idx <= N:
|
|
xt = x[l_idx:r_idx]
|
|
yt = y[l_idx:r_idx]
|
|
|
|
if len(xt) > 10:
|
|
att0, att1 = attcalcx1(xt, yt, x0_val, x0r_val,
|
|
x1_val, fun_para_static.get('x1r', 0), 0)
|
|
|
|
fun_startpara = x1fun3setup(fun_para_static, att0, att1)
|
|
|
|
try:
|
|
result = least_squares(
|
|
lambda p: x1fun3(p, fun_para_static, xt, yt)[0],
|
|
fun_startpara, method='lm',
|
|
ftol=tol_fun, xtol=tol_x, max_nfev=max_iter
|
|
)
|
|
calcpara = result.x
|
|
resnorm[0] = np.sum(result.fun ** 2)
|
|
exitflag = 1 if result.success else -1
|
|
except Exception:
|
|
calcpara, resnorm[0], exitflag = _simple_lm(
|
|
lambda p: x1fun3(p, fun_para_static, xt, yt)[0],
|
|
fun_startpara, tol_fun, tol_x, max_iter
|
|
)
|
|
|
|
_, fun_para_static['reg_sub'], _ = x1fun3(calcpara, fun_para_static, xt, yt)
|
|
dataoutleft = x1fun3out(calcpara, fun_para_static)
|
|
|
|
calcval = {
|
|
'alpha0': dataoutleft.get('alpha0', 0),
|
|
'alpha1': dataoutleft.get('alpha1', 0),
|
|
'x': dataoutleft.get('x1', 0)
|
|
}
|
|
|
|
dataout['left']['pos']['result'] = calcval['x']
|
|
dataout['left']['att']['inner'] = calcval['alpha1']
|
|
dataout['left']['att']['outer'] = calcval['alpha0']
|
|
|
|
minxt = np.min(xt)
|
|
maxxt = np.max(xt)
|
|
errorlevel[:, 0] = edge_error2(
|
|
calcval, exitflag, resnorm[0], np.mean(yt), att0,
|
|
minxt, maxxt,
|
|
modul_para.get('minatt1', 0), modul_para.get('maxatt1', 1),
|
|
modul_para.get('mincontr', 0), modul_para.get('maxcontr', 100),
|
|
modul_para.get('contrsign', 0),
|
|
modul_para.get('resmax', 1),
|
|
0.1 * (maxxt - minxt), 0.05 * (maxxt - minxt),
|
|
len(yt)
|
|
)
|
|
else:
|
|
errorlevel[0, 0] = 50
|
|
else:
|
|
errorlevel[0, 0] = 50
|
|
|
|
x1r_val = fun_para_static.get('x1r', 0)
|
|
x1rlimit_l = modul_para.get('x1rlimit_l', 0)
|
|
x1rlimit_r = modul_para.get('x1rlimit_r', 0)
|
|
|
|
leftcut_r = x1r_val - range_inner - filtlength
|
|
if leftcut_r < (x1rlimit_l - filtlength):
|
|
leftcut_r = x1rlimit_l - filtlength
|
|
rightcut_r = x1r_val + range_outer + filtlength
|
|
if rightcut_r > (x1rlimit_r + filtlength):
|
|
rightcut_r = x1rlimit_r + filtlength
|
|
|
|
l_idx_r = int(ind(leftcut_r, dx, 0, N))
|
|
r_idx_r = int(ind(rightcut_r, dx, 0, N))
|
|
|
|
if l_idx_r < r_idx_r and l_idx_r >= 0 and r_idx_r <= N:
|
|
xt_r = x[l_idx_r:r_idx_r]
|
|
yt_r = y[l_idx_r:r_idx_r]
|
|
|
|
if len(xt_r) > 10:
|
|
att0_r, att1_r = attcalcx1(xt_r, yt_r, x0_val, x0r_val,
|
|
x1_val, x1r_val, 1)
|
|
|
|
fun_startpara_r = x1fun3setup(fun_para_static, att0_r, att1_r)
|
|
|
|
try:
|
|
result_r = least_squares(
|
|
lambda p: x1fun3(p, fun_para_static, xt_r, yt_r)[0],
|
|
fun_startpara_r, method='lm',
|
|
ftol=tol_fun, xtol=tol_x, max_nfev=max_iter
|
|
)
|
|
calcpara_r = result_r.x
|
|
resnorm[1] = np.sum(result_r.fun ** 2)
|
|
exitflag_r = 1 if result_r.success else -1
|
|
except Exception:
|
|
calcpara_r, resnorm[1], exitflag_r = _simple_lm(
|
|
lambda p: x1fun3(p, fun_para_static, xt_r, yt_r)[0],
|
|
fun_startpara_r, tol_fun, tol_x, max_iter
|
|
)
|
|
|
|
_, fun_para_static['reg_sub'], _ = x1fun3(calcpara_r, fun_para_static, xt_r, yt_r)
|
|
dataoutright = x1fun3out(calcpara_r, fun_para_static)
|
|
|
|
calcval_r = {
|
|
'alpha0': dataoutright.get('alpha0', 0),
|
|
'alpha1': dataoutright.get('alpha1', 0),
|
|
'x': dataoutright.get('x1r', 0)
|
|
}
|
|
|
|
dataout['right']['pos']['result'] = calcval_r['x']
|
|
dataout['right']['att']['inner'] = calcval_r['alpha1']
|
|
dataout['right']['att']['outer'] = calcval_r['alpha0']
|
|
|
|
minxt_r = np.min(xt_r)
|
|
maxxt_r = np.max(xt_r)
|
|
errorlevel[:, 1] = edge_error2(
|
|
calcval_r, exitflag_r, resnorm[1], np.mean(yt_r), att0_r,
|
|
minxt_r, maxxt_r,
|
|
modul_para.get('minatt1', 0), modul_para.get('maxatt1', 1),
|
|
modul_para.get('mincontr', 0), modul_para.get('maxcontr', 100),
|
|
modul_para.get('contrsign', 0),
|
|
modul_para.get('resmax', 1),
|
|
0.1 * (maxxt_r - minxt_r), 0.05 * (maxxt_r - minxt_r),
|
|
len(yt_r)
|
|
)
|
|
else:
|
|
errorlevel[0, 1] = 50
|
|
else:
|
|
errorlevel[0, 1] = 50
|
|
|
|
dataoutleft = dataout['left']
|
|
dataoutright = dataout['right']
|
|
|
|
if errorlevel[0, 0] == 0 and errorlevel[0, 1] == 0:
|
|
dataout['alpha0'] = 0.5 * (dataoutleft['att']['outer'] + dataoutright['att']['outer'])
|
|
dataout['x1'] = dataoutleft['pos']['result']
|
|
dataout['x1r'] = dataoutright['pos']['result']
|
|
dataout['alpha1'] = 0.5 * (dataoutleft['att']['inner'] + dataoutright['att']['inner'])
|
|
elif errorlevel[0, 0] == 0:
|
|
dataout['alpha0'] = dataoutleft['att']['outer']
|
|
dataout['x1'] = dataoutleft['pos']['result']
|
|
dataout['x1r'] = 0
|
|
dataout['alpha1'] = dataoutleft['att']['inner']
|
|
elif errorlevel[0, 1] == 0:
|
|
dataout['alpha0'] = dataoutright['att']['outer']
|
|
dataout['x1'] = 0
|
|
dataout['x1r'] = dataoutright['pos']['result']
|
|
dataout['alpha1'] = dataoutright['att']['inner']
|
|
|
|
return dataout, errorlevel, resnorm
|
|
|
|
|
|
def cxlr11(I, I0, Anzpara, conf):
|
|
from math_utils import is_pe
|
|
conf = np.asarray(conf, dtype=np.float64).ravel()
|
|
I = np.asarray(I, dtype=np.float64).ravel()
|
|
|
|
conf20 = conf[19] if len(conf) > 19 else 0
|
|
|
|
if is_pe(conf20):
|
|
Pegel00 = conf[120] / 100.0 if len(conf) > 120 else 0.5
|
|
Lowpegel11 = conf[121] / 100.0 if len(conf) > 121 else 0.2
|
|
Highpegel11 = conf[122] / 100.0 if len(conf) > 122 else 0.8
|
|
else:
|
|
Pegel00 = conf[37] / 100.0 if len(conf) > 37 else 0.5
|
|
Lowpegel11 = conf[38] / 100.0 if len(conf) > 38 else 0.2
|
|
Highpegel11 = conf[39] / 100.0 if len(conf) > 39 else 0.8
|
|
|
|
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
|
|
|
|
xl00, xr00 = 0.0, 0.0
|
|
xl01, xr01 = 0.0, 0.0
|
|
xl11, xr11 = 0.0, 0.0
|
|
xl41, xr41 = 0.0, 0.0
|
|
xlr41det = 0
|
|
errorl, errorr = 0, 0
|
|
count = len(I)
|
|
NOMINS = conf[8] if len(conf) > 8 else 0
|
|
|
|
I_slice = I[1:count-1]
|
|
Iind = np.where(I_slice >= Pegel00 * I0)[0]
|
|
|
|
if len(Iind) > 0:
|
|
startind1 = Iind[0] + 2
|
|
stopind1 = Iind[-1] + 2
|
|
|
|
xl00 = (startind1 - 1) * dx
|
|
xr00 = (stopind1 - 1) * dx
|
|
|
|
I_mid = I[startind1-1:stopind1]
|
|
Iind2 = np.where(I_mid <= Lowpegel11 * I0)[0]
|
|
|
|
if (len(Iind2) == 0 and errorl == 0 and errorr == 0 and NOMINS == 0 and
|
|
131 <= conf20 <= 150):
|
|
filtlength = 11
|
|
rsigma = 0.7
|
|
from math_utils import gauss
|
|
from signal_utils import filter2k3
|
|
filtvek = gauss(rsigma * 0.5 * (filtlength - 1), filtlength)
|
|
try:
|
|
INF, N2 = filter2k3(filtvek, I)
|
|
Imin = np.min(INF[:len(INF)])
|
|
IminPegel = Imin / I0
|
|
if IminPegel < (Pegel00 - 0.01):
|
|
Iind2 = np.where(I_mid <= (IminPegel + 0.01) * I0)[0]
|
|
except Exception:
|
|
pass
|
|
|
|
if len(Iind2) > 0:
|
|
startind2 = Iind2[0] + startind1
|
|
stopind2 = Iind2[-1] + startind1
|
|
|
|
xl01 = (startind2 - 1) * dx
|
|
xr01 = (stopind2 - 1) * dx
|
|
|
|
I_left = I[startind1-1:startind2]
|
|
Iind3 = np.where(I_left >= Highpegel11 * I0)[0]
|
|
if len(Iind3) > 0:
|
|
stopind3 = Iind3[-1] + startind1
|
|
xl11 = (stopind3 - 1) * dx
|
|
else:
|
|
errorl = 4
|
|
|
|
I_right = I[stopind2-1:stopind1]
|
|
Iind4 = np.where(I_right >= Highpegel11 * I0)[0]
|
|
if len(Iind4) > 0:
|
|
startind3 = Iind4[0] + stopind2
|
|
xr11 = (startind3 - 1) * dx
|
|
else:
|
|
errorr = 4
|
|
else:
|
|
errorl = 4
|
|
errorr = 4
|
|
else:
|
|
errorl = 4
|
|
errorr = 4
|
|
|
|
if errorl == 0 and errorr == 0:
|
|
if ((131 <= conf20 <= 150 and NOMINS == 0) or (105 <= conf20 <= 110)):
|
|
DistIndMin = 100 if conf20 == 144 else 25
|
|
DistMin = 10 if (105 <= conf20 <= 110) else 1.0
|
|
|
|
filtlength = 11
|
|
rsigma = 0.7
|
|
from math_utils import gauss
|
|
from signal_utils import filter2k3
|
|
filtvek = gauss(rsigma * 0.5 * (filtlength - 1), filtlength)
|
|
try:
|
|
INF, N2 = filter2k3(filtvek, I)
|
|
Imin = np.min(INF[:len(INF)])
|
|
ObjInd_arr = np.where(INF[:len(INF)] <= Imin)[0]
|
|
if len(ObjInd_arr) > 0:
|
|
ObjInd = ObjInd_arr[0]
|
|
left_end = max(0, ObjInd - DistIndMin)
|
|
right_start = min(len(INF), ObjInd + DistIndMin)
|
|
|
|
Imin1 = np.min(INF[:left_end]) if left_end > 0 else Imin
|
|
Imin2 = np.min(INF[right_start:]) if right_start < len(INF) else Imin
|
|
|
|
if Imin2 < Imin1:
|
|
right_arr = np.where(INF[right_start:] <= Imin2)[0]
|
|
ObjIndR = right_start + (right_arr[-1] if len(right_arr) > 0 else 0)
|
|
ObjIndL = ObjInd
|
|
else:
|
|
left_arr = np.where(INF[:left_end] <= Imin1)[0]
|
|
ObjIndL = left_arr[0] if len(left_arr) > 0 else 0
|
|
ObjIndR = ObjInd
|
|
|
|
mid_point = round(ObjIndL + (ObjIndR - ObjIndL) / 2)
|
|
IminL, IndMinL = np.min(INF[ObjIndL:mid_point]), np.argmin(INF[ObjIndL:mid_point])
|
|
IminR, IndMinR = np.min(INF[mid_point:ObjIndR]), np.argmin(INF[mid_point:ObjIndR])
|
|
|
|
DistL = abs(((ObjIndL + IndMinL - 1) * dx) - xl11)
|
|
DistR = abs(((mid_point + IndMinR - 1) * dx) - xr11)
|
|
|
|
if DistL < DistMin or DistR < DistMin:
|
|
xl41 = xl11 + DistL
|
|
xr41 = xr11 - DistR
|
|
xlr41det = 1
|
|
|
|
if conf20 == 144:
|
|
xl41 = ObjIndL * dx
|
|
xr41 = ObjIndR * dx
|
|
xlr41det = 2
|
|
except Exception:
|
|
pass
|
|
|
|
return xl11, xl00, errorl, xr11, xr00, errorr, xl41, xr41, xlr41det
|