添加对python算法的支持

This commit is contained in:
2026-07-30 18:46:32 +08:00
parent 7eb3b89714
commit 86af7f30be
25 changed files with 13525 additions and 2 deletions
+331
View File
@@ -0,0 +1,331 @@
import numpy as np
try:
from .math_utils import polyfit2
except ImportError:
from math_utils import polyfit2
def limit(x, uplimit, lowlimit, limitflag=None):
x = np.asarray(x, dtype=np.float64)
if limitflag is None:
limitflag = np.ones_like(x, dtype=bool)
else:
limitflag = np.asarray(limitflag, dtype=bool)
y = x.copy()
errorl = 0
lflag = 0
uplimiterused = 0
lowlimiterused = 0
if np.isscalar(uplimit):
uplimit = uplimit * np.ones_like(x)
if np.isscalar(lowlimit):
lowlimit = lowlimit * np.ones_like(x)
if (x.shape == uplimit.shape) and (x.shape == limitflag.shape):
uplimiterused = 1
ind = np.where((x > uplimit) & limitflag)[0]
y[ind] = uplimit[ind]
if len(ind) > 0:
lflag = 1
if (x.shape == lowlimit.shape) and (x.shape == limitflag.shape):
lowlimiterused = 1
ind = np.where((x < lowlimit) & limitflag)[0]
y[ind] = lowlimit[ind]
if len(ind) > 0:
lflag = 1
if (not uplimiterused) and (not lowlimiterused):
errorl = 1
return y, errorl, lflag
def lowlimit(x, bp):
x = np.asarray(x, dtype=np.float64)
y = x.copy()
yind = np.where(x < bp)[0]
a2 = 1.0 / (2 * bp)
a0 = 0.5 * bp
y[yind] = a2 * x[yind] ** 2 + a0
return y, a0
def symlimit(symj, symj2, NOMJACK1, NOMJACK2):
if (NOMJACK1 > 0) and (NOMJACK2 > 0):
if NOMJACK1 < NOMJACK2:
NOMJACK = NOMJACK1
else:
NOMJACK = NOMJACK2
elif NOMJACK1 > 0:
NOMJACK = NOMJACK1
elif NOMJACK2 > 0:
NOMJACK = NOMJACK2
else:
NOMJACK = 0
if NOMJACK > 0:
if symj > NOMJACK:
symj = NOMJACK
if symj2 > NOMJACK:
symj2 = NOMJACK
return symj, symj2
def _minmaxp(y, smax):
y = np.asarray(y, dtype=np.float64)
if smax:
ys = np.max(y)
xs = np.argmax(y) + 1
else:
ys = np.min(y)
xs = np.argmin(y) + 1
if (xs > 1) and (xs < len(y)):
x_arr = np.array([xs - 1, xs, xs + 1], dtype=np.float64)
p = polyfit2(x_arr, y[x_arr.astype(int) - 1], 2)
xs = -p[1] / (2 * p[0])
ys = p[2] - (p[1] ** 2) / (4 * p[0])
return ys, xs
def minmax(y, smax):
y = np.asarray(y, dtype=np.float64)
ys = 0.0
xs = 0.0
N = len(y)
if N == 1:
ys = y[0]
xs = 1.0
elif N == 2:
if (y[1] > y[0]) and smax:
ys = y[1]
xs = 2.0
elif (y[1] > y[0]) and not smax:
ys = y[0]
xs = 1.0
elif (y[1] < y[0]) and smax:
ys = y[0]
xs = 1.0
else:
ys = y[1]
xs = 2.0
elif N > 2:
run = 1
i = 0
Il = N
if smax:
while run and (i < N - 1):
if y[i + 1] > y[i]:
run = 0
Il = i
else:
i = i + 1
else:
while run and (i < N - 1):
if y[i + 1] < y[i]:
run = 0
Il = i
else:
i = i + 1
run = 1
i = N - 1
Ir = 0
if smax:
while run and (i > 0):
if y[i - 1] > y[i]:
run = 0
Ir = i
else:
i = i - 1
else:
while run and (i > 0):
if y[i - 1] < y[i]:
run = 0
Ir = i
else:
i = i - 1
if Il < Ir:
ys, xs = _minmaxp(y[Il:Ir + 1], smax)
xs = xs + Il - 1
else:
ys, xs = _minmaxp(y, smax)
return ys, xs
def _minmaxp2(y, smax, xr41det, level):
y = np.asarray(y, dtype=np.float64)
if smax:
if not xr41det:
found = np.where(y > level)[0]
if len(found) > 0:
xs = found[0]
while (xs + 1 < len(y)) and (y[xs + 1] > y[xs]):
xs = xs + 1
else:
xs = None
else:
found = np.where(y > level)[0]
if len(found) > 0:
xs = found[-1]
while (xs > 0) and (y[xs - 1] > y[xs]):
xs = xs - 1
else:
xs = None
else:
ys = np.min(y)
xs = np.argmin(y)
if xs is not None:
if (xs > 0) and (xs < len(y) - 1):
x_arr = np.array([xs, xs + 1, xs + 2], dtype=np.float64)
p = polyfit2(x_arr, y[[xs - 1, xs, xs + 1]], 2)
xs = -p[1] / (2 * p[0])
ys = p[2] - (p[1] ** 2) / (4 * p[0])
else:
xs = 0.0
ys = 0.0
return ys, float(xs) if isinstance(xs, (int, np.integer)) else xs
def minmax2(y, smax, xr41det, level):
y = np.asarray(y, dtype=np.float64)
ys = 0.0
xs = 0.0
N = len(y)
if N == 1:
ys = y[0]
xs = 1.0
elif N == 2:
if (y[1] > y[0]) and smax:
ys = y[1]
xs = 2.0
elif (y[1] > y[0]) and not smax:
ys = y[0]
xs = 1.0
elif (y[1] < y[0]) and smax:
ys = y[0]
xs = 1.0
else:
ys = y[1]
xs = 2.0
elif N > 2:
run = 1
i = 0
Il = N
if smax:
while run and (i < N - 1):
if y[i + 1] > y[i]:
run = 0
Il = i
else:
i = i + 1
else:
while run and (i < N - 1):
if y[i + 1] < y[i]:
run = 0
Il = i
else:
i = i + 1
run = 1
i = N - 1
Ir = 0
if smax:
while run and (i > 0):
if y[i - 1] > y[i]:
run = 0
Ir = i
else:
i = i - 1
else:
while run and (i > 0):
if y[i - 1] < y[i]:
run = 0
Ir = i
else:
i = i - 1
if Il < Ir:
ys, xs = _minmaxp2(y[Il:Ir + 1], smax, xr41det, level)
xs = xs + Il - 1
else:
ys, xs = _minmaxp2(y, smax, xr41det, level)
return ys, xs
def minmaxall(x):
x = np.asarray(x, dtype=np.float64)
Val_max = np.array([])
Ind_max = np.array([])
Val_min = np.array([])
Ind_min = np.array([])
inan = np.where(np.isnan(x))[0]
if len(inan) > 0:
x = np.delete(x, inan)
Nt = len(x)
if Nt < 3:
return Val_max, Ind_max, Val_min, Ind_min
dx = np.diff(x)
b = (dx > 0).astype(int)
db = np.diff(b)
Ind_max = np.where(db == -1)[0] + 1
Ind_min = np.where(db == 1)[0] + 1
Val_max = x[Ind_max]
Val_min = x[Ind_min]
sort_idx = np.argsort(-Ind_max)
Ind_max = Ind_max[sort_idx]
Val_max = Val_max[sort_idx]
sort_idx = np.argsort(-Ind_min)
Ind_min = Ind_min[sort_idx]
Val_min = Val_min[sort_idx]
return Val_max, Ind_max, Val_min, Ind_min
def findmaxima(I, levelfactor, left):
I = np.asarray(I, dtype=np.float64)
maximum = np.max(I)
maxindex = np.argmax(I)
meanI = np.mean(I)
minlimit1 = meanI + 4.0 * np.std(I)
minlimit2 = levelfactor * (maximum - meanI) + meanI
I2 = I.copy()
if minlimit1 >= 0.99 * maximum:
return float(maxindex)
if minlimit1 > minlimit2:
minlimit2 = minlimit1
ind = np.where(I < minlimit2)[0]
I2[ind] = minlimit2
I2_padded = np.pad(I2, (1, 1), mode='edge')
max_filt = np.maximum(np.maximum(I2_padded[:-2], I2_padded[1:-1]), I2_padded[2:])
I3 = (I2 == max_filt)
maxindexvek = np.where(I3)[0]
if left:
maxindex = float(maxindexvek[0])
else:
maxindex = float(maxindexvek[-1])
return maxindex
def qcheck(Indiff, dx, Ref, klevel, Anzpara=0):
Indiff = np.asarray(Indiff, dtype=np.float64)
errorlevel = np.zeros(2)
N = len(Indiff)
x = np.arange(N) * dx
A = polyfit2(x, Indiff, 2)
Scheitelx = -A[1] / (2 * A[0])
if Scheitelx > N * dx:
errorlevel[1] = 11
if Scheitelx < 0:
errorlevel[1] = 11
if abs(A[0]) > 0:
Scheitely = A[2] - (A[1] ** 2) / (4 * A[0])
diff1 = abs(np.polyval([A[0], A[1], A[2]], 0) - Scheitely)
diff2 = abs(np.polyval([A[0], A[1], A[2]], N * dx) - Scheitely)
diff = (diff1 + diff2) / 2
Kontrast = diff * 1 / (N * dx)
if Kontrast < klevel:
errorlevel[0] = 11
else:
errorlevel[0] = 11
Maximum = (A[0] < 0)
return errorlevel, Maximum
def errfunc(errorl, errorlevel):
if (errorlevel[0] == 0) and (errorl[0] > 0):
errorlevel[0] = errorl[0]
if (errorlevel[1] == 0) and (errorl[1] > 0):
errorlevel[1] = errorl[1]
return errorlevel