添加对python算法的支持
This commit is contained in:
@@ -0,0 +1,492 @@
|
||||
import numpy as np
|
||||
|
||||
|
||||
def linint3(x, y, xi):
|
||||
x = np.asarray(x, dtype=np.float64).ravel()
|
||||
y = np.asarray(y, dtype=np.float64).ravel()
|
||||
xi = np.asarray(xi, dtype=np.float64).ravel()
|
||||
|
||||
if len(x) != len(y):
|
||||
raise ValueError('length(x) must be == length(y)')
|
||||
if len(x) < 2:
|
||||
raise ValueError('length(x) must be >= 2')
|
||||
|
||||
sort_idx = np.argsort(xi)
|
||||
xi_sorted = xi[sort_idx]
|
||||
|
||||
yi_sorted = np.zeros_like(xi_sorted)
|
||||
N = len(x)
|
||||
k = 0
|
||||
newk = True
|
||||
|
||||
for i in range(len(xi_sorted)):
|
||||
foundxi = False
|
||||
while not foundxi:
|
||||
if k < N - 1 and xi_sorted[i] >= x[k] and xi_sorted[i] < x[k + 1]:
|
||||
if newk:
|
||||
m = (y[k + 1] - y[k]) / (x[k + 1] - x[k])
|
||||
newk = False
|
||||
yi_sorted[i] = m * (xi_sorted[i] - x[k]) + y[k]
|
||||
foundxi = True
|
||||
elif xi_sorted[i] <= x[0]:
|
||||
m = (y[1] - y[0]) / (x[1] - x[0])
|
||||
yi_sorted[i] = m * (xi_sorted[i] - x[0]) + y[0]
|
||||
foundxi = True
|
||||
elif xi_sorted[i] >= x[N - 1]:
|
||||
m = (y[N - 1] - y[N - 2]) / (x[N - 1] - x[N - 2])
|
||||
yi_sorted[i] = m * (xi_sorted[i] - x[N - 2]) + y[N - 2]
|
||||
foundxi = True
|
||||
else:
|
||||
if k < N - 2:
|
||||
k += 1
|
||||
newk = True
|
||||
else:
|
||||
raise ValueError('Linint3: No solution, because no pos. step xi')
|
||||
|
||||
yi = np.zeros_like(xi)
|
||||
yi[sort_idx] = yi_sorted
|
||||
return yi
|
||||
|
||||
|
||||
def lowlimit(x, bp):
|
||||
x = np.asarray(x, dtype=np.float64)
|
||||
y = x.copy()
|
||||
yind = np.where(x < bp)
|
||||
a2 = 1.0 / (2 * bp)
|
||||
a0 = 0.5 * bp
|
||||
y[yind] = a2 * x[yind] ** 2 + a0
|
||||
return y, a0
|
||||
|
||||
|
||||
def _layer_side_fit(results, side, layername, DefaultError=255.0):
|
||||
lay_idx = {'osc': 0, 'iso': 1, 'isc': 2}
|
||||
lay_names = ['osc', 'iso', 'isc']
|
||||
|
||||
wthvalid = (results[side]['wth'][2, :] == 0).astype(np.float64)
|
||||
|
||||
other_valid = np.ones(3, dtype=object)
|
||||
for li, ln in enumerate(lay_names):
|
||||
if ln == layername:
|
||||
this_valid = (results[side][ln][2, :] == 0).astype(np.float64)
|
||||
else:
|
||||
other_valid[li] = ((results[side][ln][2, :] == 0) |
|
||||
(results[side][ln][2, :] == 45)).astype(np.float64)
|
||||
|
||||
valid_new = wthvalid.copy()
|
||||
for li, ln in enumerate(lay_names):
|
||||
if ln == layername:
|
||||
valid_new *= this_valid
|
||||
else:
|
||||
valid_new *= other_valid[li]
|
||||
|
||||
other_sum = np.zeros_like(results[side]['wth'][1, :])
|
||||
for ln in lay_names:
|
||||
if ln != layername:
|
||||
other_sum += results[side][ln][1, :]
|
||||
|
||||
results[side][layername][1, :] = (
|
||||
(results[side]['wth'][1, :] - other_sum) * valid_new
|
||||
)
|
||||
|
||||
newfailed = this_valid & (1 - valid_new.astype(int))
|
||||
if np.any(newfailed):
|
||||
results[side][layername][2, :] = DefaultError * newfailed.astype(np.float64)
|
||||
|
||||
return results
|
||||
|
||||
|
||||
def oscfit(results):
|
||||
DefaultError = 255.0
|
||||
|
||||
for side in ['left', 'right']:
|
||||
wthvalid = (results[side]['wth'][2, :] == 0).astype(np.float64)
|
||||
oscvalid = (results[side]['osc'][2, :] == 0).astype(np.float64)
|
||||
isovalid = ((results[side]['iso'][2, :] == 0) |
|
||||
(results[side]['iso'][2, :] == 45)).astype(np.float64)
|
||||
iscvalid = ((results[side]['isc'][2, :] == 0) |
|
||||
(results[side]['isc'][2, :] == 45)).astype(np.float64)
|
||||
oscvalid_new = wthvalid * oscvalid * isovalid * iscvalid
|
||||
results[side]['osc'][1, :] = (
|
||||
(results[side]['wth'][1, :] -
|
||||
results[side]['iso'][1, :] -
|
||||
results[side]['isc'][1, :]) * oscvalid_new
|
||||
)
|
||||
newfailed = oscvalid * (1 - oscvalid_new.astype(int))
|
||||
if np.any(newfailed):
|
||||
results[side]['osc'][2, :] = DefaultError * newfailed.astype(np.float64)
|
||||
|
||||
return results
|
||||
|
||||
|
||||
def isofit(results):
|
||||
DefaultError = 255.0
|
||||
|
||||
for side in ['left', 'right']:
|
||||
wthvalid = (results[side]['wth'][2, :] == 0).astype(np.float64)
|
||||
oscvalid = ((results[side]['osc'][2, :] == 0) |
|
||||
(results[side]['osc'][2, :] == 45)).astype(np.float64)
|
||||
iscvalid = ((results[side]['isc'][2, :] == 0) |
|
||||
(results[side]['isc'][2, :] == 45)).astype(np.float64)
|
||||
isovalid = (results[side]['iso'][2, :] == 0).astype(np.float64)
|
||||
isovalid_new = wthvalid * oscvalid * isovalid * iscvalid
|
||||
results[side]['iso'][1, :] = (
|
||||
(results[side]['wth'][1, :] -
|
||||
results[side]['osc'][1, :] -
|
||||
results[side]['isc'][1, :]) * isovalid_new
|
||||
)
|
||||
newfailed = isovalid * (1 - isovalid_new.astype(int))
|
||||
if np.any(newfailed):
|
||||
results[side]['iso'][2, :] = DefaultError * newfailed.astype(np.float64)
|
||||
|
||||
return results
|
||||
|
||||
|
||||
def iscfit(results):
|
||||
DefaultError = 255.0
|
||||
|
||||
for side in ['left', 'right']:
|
||||
wthvalid = (results[side]['wth'][2, :] == 0).astype(np.float64)
|
||||
oscvalid = ((results[side]['osc'][2, :] == 0) |
|
||||
(results[side]['osc'][2, :] == 45)).astype(np.float64)
|
||||
isovalid = ((results[side]['iso'][2, :] == 0) |
|
||||
(results[side]['iso'][2, :] == 45)).astype(np.float64)
|
||||
iscvalid = (results[side]['isc'][2, :] == 0).astype(np.float64)
|
||||
iscvalid_new = wthvalid * oscvalid * isovalid * iscvalid
|
||||
results[side]['isc'][1, :] = (
|
||||
(results[side]['wth'][1, :] -
|
||||
results[side]['osc'][1, :] -
|
||||
results[side]['iso'][1, :]) * iscvalid_new
|
||||
)
|
||||
newfailed = iscvalid * (1 - iscvalid_new.astype(int))
|
||||
if np.any(newfailed):
|
||||
results[side]['isc'][2, :] = DefaultError * newfailed.astype(np.float64)
|
||||
|
||||
return results
|
||||
|
||||
|
||||
def corefit(results):
|
||||
DefaultError = 255.0
|
||||
|
||||
corevalid_new = (
|
||||
(results['dmOut'][2, :] == 0) &
|
||||
(results['left']['wth'][2, :] == 0) &
|
||||
(results['right']['wth'][2, :] == 0)
|
||||
).astype(np.float64)
|
||||
|
||||
results['coreDm'][1, :] = (
|
||||
(results['dmOut'][1, :] -
|
||||
results['left']['wth'][1, :] -
|
||||
results['right']['wth'][1, :]) * corevalid_new
|
||||
)
|
||||
|
||||
newfailed = (1 - results['coreDm'][2, :].astype(int)) & (1 - corevalid_new.astype(int))
|
||||
if np.any(newfailed):
|
||||
errvekx = np.array([results['dmOut'][2, 0],
|
||||
results['left']['wth'][2, 0],
|
||||
results['right']['wth'][2, 0]])
|
||||
errveky = np.array([results['dmOut'][2, 1],
|
||||
results['left']['wth'][2, 1],
|
||||
results['right']['wth'][2, 1]])
|
||||
errindx = np.where(errvekx)[0]
|
||||
errindy = np.where(errveky)[0]
|
||||
errxy = np.zeros(2)
|
||||
if len(errindx) > 0:
|
||||
errxy[0] = errvekx[errindx[0]]
|
||||
if len(errindy) > 0:
|
||||
errxy[1] = errveky[errindy[0]]
|
||||
results['coreDm'][2, :] = errxy * newfailed.astype(np.float64)
|
||||
|
||||
return results
|
||||
|
||||
|
||||
def _calib_side(results, results_out, csector, layername, calib_key):
|
||||
act_key = f'{calib_key}act'
|
||||
nom_key = f'{calib_key}nom'
|
||||
|
||||
if act_key in csector and len(np.atleast_1d(csector[act_key])) > 0:
|
||||
for side in ['left', 'right']:
|
||||
val_in = np.concatenate([
|
||||
results[side][layername][1, 0:2].ravel(),
|
||||
results['right' if side == 'left' else 'left'][layername][1, 0:2].ravel()
|
||||
]) if side == 'left' else None
|
||||
|
||||
if side == 'left':
|
||||
val_in_left = np.concatenate([
|
||||
results['left'][layername][1, 0:2].ravel(),
|
||||
results['right'][layername][1, 0:2].ravel()
|
||||
])
|
||||
try:
|
||||
val_out = linint3(csector[act_key], csector[nom_key], val_in_left)
|
||||
results_out['left'][layername][1, 0:2] = val_out[0:2]
|
||||
results_out['right'][layername][1, 0:2] = val_out[2:4]
|
||||
except Exception:
|
||||
pass
|
||||
return results_out
|
||||
|
||||
|
||||
def osc_calib(results, results_out, csector):
|
||||
if 'lay1act' in csector and len(np.atleast_1d(csector['lay1act'])) > 0:
|
||||
val_in = np.concatenate([
|
||||
results['left']['osc'][1, 0:2].ravel(),
|
||||
results['right']['osc'][1, 0:2].ravel()
|
||||
])
|
||||
try:
|
||||
val_out = linint3(csector['lay1act'], csector['lay1nom'], val_in)
|
||||
results_out['left']['osc'][1, 0:2] = val_out[0:2]
|
||||
results_out['right']['osc'][1, 0:2] = val_out[2:4]
|
||||
except Exception:
|
||||
pass
|
||||
return results_out
|
||||
|
||||
|
||||
def iso_calib(results, results_out, csector):
|
||||
if 'lay2act' in csector and len(np.atleast_1d(csector['lay2act'])) > 0:
|
||||
val_in = np.concatenate([
|
||||
results['left']['iso'][1, 0:2].ravel(),
|
||||
results['right']['iso'][1, 0:2].ravel()
|
||||
])
|
||||
try:
|
||||
val_out = linint3(csector['lay2act'], csector['lay2nom'], val_in)
|
||||
results_out['left']['iso'][1, 0:2] = val_out[0:2]
|
||||
results_out['right']['iso'][1, 0:2] = val_out[2:4]
|
||||
except Exception:
|
||||
pass
|
||||
return results_out
|
||||
|
||||
|
||||
def isc_calib(results, results_out, csector):
|
||||
if 'lay3act' in csector and len(np.atleast_1d(csector['lay3act'])) > 0:
|
||||
val_in = np.concatenate([
|
||||
results['left']['isc'][1, 0:2].ravel(),
|
||||
results['right']['isc'][1, 0:2].ravel()
|
||||
])
|
||||
try:
|
||||
val_out = linint3(csector['lay3act'], csector['lay3nom'], val_in)
|
||||
results_out['left']['isc'][1, 0:2] = val_out[0:2]
|
||||
results_out['right']['isc'][1, 0:2] = val_out[2:4]
|
||||
except Exception:
|
||||
pass
|
||||
return results_out
|
||||
|
||||
|
||||
def layer_correct(results, calib_struct, layeruse, cableType):
|
||||
import copy
|
||||
results_out = copy.deepcopy(results)
|
||||
|
||||
sectionindex = None
|
||||
calibrate = False
|
||||
|
||||
if (isinstance(calib_struct, dict) and
|
||||
calib_struct.get('calibrationoff') is not None and
|
||||
not calib_struct.get('calibrationoff', 1) and
|
||||
not calib_struct.get('err', 1) and
|
||||
len(np.atleast_1d(calib_struct.get('sectionType', []))) > 0):
|
||||
|
||||
ii = 0
|
||||
run = True
|
||||
NN = len(np.atleast_1d(calib_struct['sectionType']))
|
||||
sectionType = np.atleast_1d(calib_struct['sectionType']).ravel()
|
||||
mincableType = np.atleast_1d(calib_struct['mincableType']).ravel()
|
||||
maxcableType = np.atleast_1d(calib_struct['maxcableType']).ravel()
|
||||
|
||||
while run:
|
||||
if (mincableType[ii] <= cableType and
|
||||
maxcableType[ii] >= cableType):
|
||||
sectionindex = int(sectionType[ii])
|
||||
run = False
|
||||
elif ii == NN - 1:
|
||||
sectionindex = None
|
||||
run = False
|
||||
else:
|
||||
ii += 1
|
||||
sectionindex = None
|
||||
run = True
|
||||
else:
|
||||
sectionindex = None
|
||||
|
||||
if sectionindex is not None:
|
||||
sect = calib_struct.get('sect', {}).get(sectionindex, {})
|
||||
diam_err = calib_struct.get(f'sect.{sectionindex}.diam.err', 1)
|
||||
wall_err = calib_struct.get(f'sect.{sectionindex}.wall.err', 1)
|
||||
lay1_err = calib_struct.get(f'sect.{sectionindex}.lay1.err', 1)
|
||||
lay2_err = calib_struct.get(f'sect.{sectionindex}.lay2.err', 1)
|
||||
lay3_err = calib_struct.get(f'sect.{sectionindex}.lay3.err', 1)
|
||||
|
||||
if not diam_err and not wall_err and not lay1_err and not lay2_err and not lay3_err:
|
||||
calibrate = True
|
||||
|
||||
if calibrate:
|
||||
calidemand = np.zeros(3)
|
||||
calidemand[0] = len(np.atleast_1d(calib_struct.get(f'sect.{sectionindex}.lay1.nom', []))) > 0
|
||||
calidemand[1] = len(np.atleast_1d(calib_struct.get(f'sect.{sectionindex}.lay2.nom', []))) > 0
|
||||
calidemand[2] = len(np.atleast_1d(calib_struct.get(f'sect.{sectionindex}.lay3.nom', []))) > 0
|
||||
|
||||
layeruse = np.asarray(layeruse).ravel()
|
||||
|
||||
if np.all(layeruse == [0, 0, 0]):
|
||||
ccase = 0
|
||||
elif np.all(layeruse == [0, 0, 1]):
|
||||
ccase = 3
|
||||
elif np.all(layeruse == [0, 1, 0]):
|
||||
ccase = 2
|
||||
elif np.all(layeruse == [0, 1, 1]):
|
||||
if calidemand[1] == 0:
|
||||
ccase = 5
|
||||
else:
|
||||
ccase = 6
|
||||
elif np.all(layeruse == [1, 0, 0]):
|
||||
ccase = 1
|
||||
elif np.all(layeruse == [1, 1, 0]):
|
||||
if np.all(calidemand[0:2] == [0, 1]):
|
||||
ccase = 4
|
||||
else:
|
||||
ccase = 5
|
||||
elif np.all(layeruse == [1, 1, 1]):
|
||||
if np.all(calidemand == [0, 1, 1]):
|
||||
ccase = 4
|
||||
elif np.all(calidemand[1:3] == [1, 0]):
|
||||
ccase = 6
|
||||
else:
|
||||
ccase = 5
|
||||
else:
|
||||
ccase = 0
|
||||
|
||||
csector = {}
|
||||
for key in ['diam', 'wall', 'lay1', 'lay2', 'lay3']:
|
||||
for suffix in ['act', 'nom', 'err']:
|
||||
full_key = f'sect.{sectionindex}.{key}.{suffix}'
|
||||
if full_key in calib_struct:
|
||||
csector[f'{key}{suffix}'] = calib_struct[full_key]
|
||||
|
||||
diam_nom = calib_struct.get(f'sect.{sectionindex}.diam.nom', [])
|
||||
if len(np.atleast_1d(diam_nom)) > 0:
|
||||
val_in = results['dmOut'][1, 0:2].ravel()
|
||||
try:
|
||||
val_out = linint3(calib_struct[f'sect.{sectionindex}.diam.act'],
|
||||
calib_struct[f'sect.{sectionindex}.diam.nom'],
|
||||
val_in)
|
||||
results_out['dmOut'][1, 0:2] = val_out
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if ccase > 0:
|
||||
wall_nom = calib_struct.get(f'sect.{sectionindex}.wall.nom', [])
|
||||
if len(np.atleast_1d(wall_nom)) > 0:
|
||||
val_in = np.concatenate([
|
||||
results['left']['wth'][1, 0:2].ravel(),
|
||||
results['right']['wth'][1, 0:2].ravel()
|
||||
])
|
||||
try:
|
||||
val_out = linint3(calib_struct[f'sect.{sectionindex}.wall.act'],
|
||||
calib_struct[f'sect.{sectionindex}.wall.nom'],
|
||||
val_in)
|
||||
results_out['left']['wth'][1, 0:2] = val_out[0:2]
|
||||
results_out['right']['wth'][1, 0:2] = val_out[2:4]
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if ccase == 1:
|
||||
results_out['left']['osc'][1, :] = results_out['left']['wth'][1, :].copy()
|
||||
results_out['right']['osc'][1, :] = results_out['right']['wth'][1, :].copy()
|
||||
elif ccase == 2:
|
||||
results_out['left']['iso'][1, :] = results_out['left']['wth'][1, :].copy()
|
||||
results_out['right']['iso'][1, :] = results_out['right']['wth'][1, :].copy()
|
||||
elif ccase == 3:
|
||||
results_out['left']['isc'][1, :] = results_out['left']['wth'][1, :].copy()
|
||||
results_out['right']['isc'][1, :] = results_out['right']['wth'][1, :].copy()
|
||||
elif ccase == 4:
|
||||
results_out = iso_calib(results, results_out, csector)
|
||||
results_out = isc_calib(results, results_out, csector)
|
||||
results_out = oscfit(results_out)
|
||||
elif ccase == 5:
|
||||
results_out = osc_calib(results, results_out, csector)
|
||||
results_out = isc_calib(results, results_out, csector)
|
||||
results_out = isofit(results_out)
|
||||
elif ccase == 6:
|
||||
results_out = osc_calib(results, results_out, csector)
|
||||
results_out = iso_calib(results, results_out, csector)
|
||||
results_out = iscfit(results_out)
|
||||
|
||||
results_out = corefit(results_out)
|
||||
|
||||
return results_out
|
||||
|
||||
|
||||
def uponor_alucorrect2(results, cabletype):
|
||||
cabletype = round(float(cabletype))
|
||||
|
||||
if cabletype not in (101, 105):
|
||||
return results
|
||||
|
||||
if cabletype == 101:
|
||||
errvek = [
|
||||
results['dmOut'][1, 0], results['dmOut'][1, 0],
|
||||
results['right']['osc'][1, 0], results['right']['osc'][1, 1],
|
||||
results['left']['osc'][1, 1], results['right']['iso'][1, 0],
|
||||
results['right']['iso'][1, 1], results['left']['iso'][1, 1],
|
||||
]
|
||||
else:
|
||||
errvek = [
|
||||
results['dmOut'][1, 0], results['dmOut'][1, 0],
|
||||
results['left']['osc'][1, 0], results['left']['osc'][1, 1],
|
||||
results['right']['osc'][1, 0], results['right']['osc'][1, 1],
|
||||
results['left']['iso'][1, 0], results['left']['iso'][1, 1],
|
||||
results['right']['iso'][1, 0], results['right']['iso'][1, 1],
|
||||
]
|
||||
|
||||
if all(v == 0 for v in errvek):
|
||||
if cabletype == 101:
|
||||
isomitt = (1.0 / 3.0) * (
|
||||
results['right']['iso'][0, 0] +
|
||||
results['left']['iso'][0, 1] +
|
||||
results['right']['iso'][0, 1]
|
||||
)
|
||||
|
||||
results['right']['osc'][0, 0] += 0.5 * (results['right']['iso'][0, 0] - isomitt)
|
||||
results['left']['osc'][0, 1] += 0.5 * (results['left']['iso'][0, 1] - isomitt)
|
||||
results['right']['osc'][0, 1] += 0.5 * (results['right']['iso'][0, 1] - isomitt)
|
||||
|
||||
if not results['right']['isc'][1, 0]:
|
||||
results['right']['isc'][0, 0] += 0.5 * (results['right']['iso'][0, 0] - isomitt)
|
||||
if not results['left']['isc'][1, 1]:
|
||||
results['left']['isc'][0, 1] += 0.5 * (results['left']['iso'][0, 1] - isomitt)
|
||||
if not results['right']['isc'][1, 1]:
|
||||
results['right']['isc'][0, 1] += 0.5 * (results['right']['iso'][0, 1] - isomitt)
|
||||
|
||||
results['right']['iso'][0, 0] = isomitt
|
||||
results['left']['iso'][0, 1] = isomitt
|
||||
results['right']['iso'][0, 1] = isomitt
|
||||
else:
|
||||
isomitt = (1.0 / 4.0) * (
|
||||
results['left']['iso'][0, 0] + results['left']['iso'][0, 1] +
|
||||
results['right']['iso'][0, 0] + results['right']['iso'][0, 1]
|
||||
)
|
||||
|
||||
results['left']['osc'][0, 0] += 0.5 * (results['left']['iso'][0, 0] - isomitt)
|
||||
results['right']['osc'][0, 0] += 0.5 * (results['right']['iso'][0, 0] - isomitt)
|
||||
results['left']['osc'][0, 1] += 0.5 * (results['left']['iso'][0, 1] - isomitt)
|
||||
results['right']['osc'][0, 1] += 0.5 * (results['right']['iso'][0, 1] - isomitt)
|
||||
|
||||
if not results['left']['isc'][1, 0]:
|
||||
results['left']['isc'][0, 0] += 0.5 * (results['left']['iso'][0, 0] - isomitt)
|
||||
if not results['right']['isc'][1, 0]:
|
||||
results['right']['isc'][0, 0] += 0.5 * (results['right']['iso'][0, 0] - isomitt)
|
||||
if not results['left']['isc'][1, 1]:
|
||||
results['left']['isc'][0, 1] += 0.5 * (results['left']['iso'][0, 1] - isomitt)
|
||||
if not results['right']['isc'][1, 1]:
|
||||
results['right']['isc'][0, 1] += 0.5 * (results['right']['iso'][0, 1] - isomitt)
|
||||
|
||||
results['left']['iso'][0, 0] = isomitt
|
||||
results['right']['iso'][0, 0] = isomitt
|
||||
results['left']['iso'][0, 1] = isomitt
|
||||
results['right']['iso'][0, 1] = isomitt
|
||||
else:
|
||||
errind = [i for i, v in enumerate(errvek) if v > 0]
|
||||
errorvalue = np.array([0.0, errvek[errind[0]], 0.0])
|
||||
|
||||
for side in ['left', 'right']:
|
||||
for layer in ['osc', 'iso', 'isc']:
|
||||
for col in range(2):
|
||||
results[side][layer][:, col] = errorvalue.copy()
|
||||
|
||||
return results
|
||||
Reference in New Issue
Block a user