import numpy as np from scipy.signal import butter, filtfilt try: from .math_utils import equal, lininter from .edge_detection_utils import i0detect from .filtering_utils import hendfilt except ImportError: from math_utils import equal, lininter from edge_detection_utils import i0detect from filtering_utils import hendfilt SCAN_LENGTH_NE_ERROR = 20 VIB_DETECT_ERROR = 24 RECON_ERROR = 24 def _timtab1(INf, pegel, count, dt, minpegel, errorlevel): print("暂不支持: timtab1") return np.array([]), errorlevel def _speed2(Timetaba, Timetabb, Tab2, Sensab, Anzpara, Mustsperr, pegel_i, NTimetaba, NTimetabb, Tab2ind, errorlevel): print("暂不支持: speed2") return Tab2, Tab2ind, NTimetaba, NTimetabb, errorlevel def _materw2(Tab2, Sensab): print("暂不支持: materw2") return Tab2 def _vfunc(Tab2, Anzpara, dt, vscan, Sensab, reconstruct, t): print("暂不支持: vfunc") return np.zeros(5), np.zeros((2, 1)), 0 def _pause3(): pass def pegeldef(INfa, reconstruct, errorlevel): minpegel2 = np.min(INfa) + reconstruct['offminpeg'] if minpegel2 > reconstruct['minpegel']: m = (reconstruct['deltapegel1'] - reconstruct['deltapegel2']) / \ (reconstruct['pegelanf'] - reconstruct['minpegel']) reconstruct['deltapegel2'] = m * minpegel2 - m * reconstruct['minpegel'] + reconstruct['deltapegel2'] reconstruct['minpegel'] = minpegel2 startindex_arr = np.where(INfa < reconstruct['maxpegel'])[0] startindex = startindex_arr[0] if len(startindex_arr) > 0 else None stopindex = startindex_arr[-1] if len(startindex_arr) > 0 else None if startindex is not None and stopindex is not None and startindex < stopindex: seg = INfa[startindex:stopindex + 1] diff_seg = np.diff(seg) medpegel_l_index = np.argmin(diff_seg) medpegel_l_wert = seg[medpegel_l_index] medpegel_r_index = np.argmax(diff_seg) medpegel_r_wert = seg[medpegel_r_index] medpegel = (medpegel_l_wert + medpegel_r_wert) / 2.0 anz1 = int(reconstruct['anzpegel1']) log_start = np.log(reconstruct['pegelanf']) log_end = np.log(medpegel / reconstruct['pegelanf']) log_step = log_end / (anz1 - 1) tmp_pegel2 = np.exp(np.arange(log_start, log_start + log_step * anz1, log_step)) if len(tmp_pegel2) != anz1: tmp_pegel2 = np.concatenate([tmp_pegel2, [tmp_pegel2[-1] - np.diff(tmp_pegel2[-2:])[0]]]) pegel = np.zeros(anz1 + int(reconstruct['anzpegel2'])) pegel[:anz1] = tmp_pegel2[:anz1] anz2 = int(reconstruct['anzpegel2']) log_start2 = np.log(medpegel) log_end2 = np.log(reconstruct['minpegel'] / medpegel) log_step2 = log_end2 / anz2 tmp_pegel = np.exp(np.arange(log_start2, log_start2 + log_step2 * (anz2 + 1), log_step2)) if len(tmp_pegel) == anz2 + 1: tmp_pegel = tmp_pegel[1:] pegel[anz1:anz1 + anz2] = tmp_pegel[:anz2] else: pegel = np.array([]) errorlevel[0, 0] = 14 return pegel, errorlevel def recon(INa, INb, v, dt): counta = len(v) x = np.zeros(counta) x[0] = dt * v[0] for i in range(1, counta): x[i] = x[i - 1] + dt * v[i] Index = np.argsort(x) xr = x[Index] INar = np.zeros(counta) INbr = np.zeros(counta) for i in range(counta): INar[i] = INa[Index[i]] INbr[i] = INb[Index[i]] return INar, INbr, xr def unist(INar, INbr, xr, dx): counta = len(xr) k = 0 Ia1 = np.zeros(counta) Ib1 = np.zeros(counta) x1 = np.zeros(counta) Ia1[0] = INar[0] Ib1[0] = INbr[0] x1[0] = xr[0] for i in range(1, counta): if abs(xr[i] - x1[k]) < 1e-7: Ia1[k] = (Ia1[k] + INar[i]) / 2.0 Ib1[k] = (Ib1[k] + INbr[i]) / 2.0 else: k = k + 1 Ia1[k] = INar[i] Ib1[k] = INbr[i] x1[k] = xr[i] x = np.arange(x1[0] + dx, x1[k] - dx + dx, dx) INa = lininter(x1[:k + 1], Ia1[:k + 1], x) INb = lininter(x1[:k + 1], Ib1[:k + 1], x) return INa, INb, x def reconstr(x_data, y_data, settings, errorin): Ia = np.asarray(x_data, dtype=np.float64).ravel() Ib = np.asarray(y_data, dtype=np.float64).ravel() errorin = np.asarray(errorin, dtype=np.float64).reshape(-1, 1) if errorin.shape[0] < 2: errorin = np.pad(errorin, ((0, 2 - errorin.shape[0]), (0, 0))) confData = settings.get('confData', np.array([])) def _conf(idx): return confData[idx - 1] if len(confData) >= idx else 0.0 dt = _conf(31) vscan = _conf(32) dx = vscan * dt if equal(_conf(14), 1): if equal(_conf(7), 0): Sensab = _conf(33) else: Sensab = _conf(7) else: if equal(_conf(8), 0): Sensab = _conf(33) else: Sensab = _conf(8) DistI0 = _conf(35) Dist01 = _conf(36) Lowrad = _conf(37) reconstruct = {} reconstruct['pegelanf'] = 0.94 reconstruct['deltapegel1'] = 0.002 reconstruct['deltapegel2'] = 0.0005 reconstruct['minpegel'] = 0.01 reconstruct['maxpegel'] = 0.9 reconstruct['fgv'] = vscan / Sensab * 1.9 reconstruct['ord'] = 2 reconstruct['deltt'] = 0.005 reconstruct['maxt'] = 0.75 reconstruct['deltatstep'] = 0.1 reconstruct['deltat'] = 0.1 reconstruct['deltav'] = 0.1 reconstruct['verhtmitt'] = 4 reconstruct['deltv'] = 70 reconstruct['maxv'] = vscan + reconstruct['deltv'] reconstruct['minv'] = vscan - reconstruct['deltv'] reconstruct['Funkt'] = 0 reconstruct['verlt'] = 0.02 reconstruct['fgvscan'] = 70 reconstruct['ordscan'] = 2 reconstruct['Anzfilt'] = 1 reconstruct['Mustsperr'] = 0 reconstruct['pegdiff'] = 0.03 reconstruct['tdiff'] = 0.05 reconstruct['offminpeg'] = 0.05 reconstruct['anzpegel1'] = 450 reconstruct['anzpegel2'] = 50 reconstruct['r_squared_min'] = 0.85 reconstruct['r_squared_vib'] = 0.55 reconstruct['minproz'] = 0.04 reconstruct['defproz'] = 0.2 reconstruct['active'] = settings.get('Reconstruct', 0) reconstruct['minamp'] = 0.5 reconstruct['maxamp'] = 2.5 reconstruct['minfreq'] = 0.1 reconstruct['maxfreq'] = 12.0 reconstruct['vibwarning'] = settings.get('vibwarning', 0) vibration_detected = 0 Ic = Ia.copy() Ia = Ib.copy() Ib = Ic.copy() errorlevel = np.zeros((2, 1), dtype=np.float64) Anzpara = _conf(11) counta = len(Ia) countb = len(Ib) if counta != countb: errorlevel[0, 0] = SCAN_LENGTH_NE_ERROR INa = 0 INb = 0 I0a, errorlevel_1d = i0detect(Ia, DistI0, dx, Lowrad, errorin.ravel()) errorlevel[:, 0] = errorlevel_1d[:2] if errorlevel.shape[0] < 2: errorlevel = np.pad(errorlevel, ((0, 2 - errorlevel.shape[0]), (0, 0))) I0b, errorlevel_1d = i0detect(Ib, DistI0, dx, Lowrad, errorin.ravel()) errorlevel_tmp = errorlevel_1d[:2] for i in range(min(2, len(errorlevel_tmp))): if errorlevel_tmp[i] != 0 and errorlevel[i, 0] == 0: errorlevel[i, 0] = errorlevel_tmp[i] if (errorlevel[0, 0] == 0) and ((Ia[-1] / I0a < reconstruct['maxpegel']) or (Ib[-1] / I0b < reconstruct['maxpegel'])): errorlevel[1, 0] = 10 if (errorlevel[0, 0] == 0) and (errorlevel[1, 0] != 10): INa = Ia / I0a INb = Ib / I0b INaorig = INa.copy() INborig = INb.copy() t_arr = np.arange(1, counta + 1) t_arr = (t_arr - 1) * dt fgn = 2 * reconstruct['fgvscan'] * dt * (vscan / 26.0) BFilt, AFilt = butter(int(round(reconstruct['ordscan'])), fgn) INfa = filtfilt(BFilt, AFilt, INa) INfb = filtfilt(BFilt, AFilt, INb) pegel, errorlevel = pegeldef(INfa, reconstruct, errorlevel) if Anzpara > 0: pass if errorlevel[0, 0] == 0: Timetaba, errorlevel = _timtab1(INfa, pegel, counta, dt, reconstruct['minpegel'], errorlevel) if errorlevel[0, 0] == 0: Timetabb, errorlevel = _timtab1(INfb, pegel, countb, dt, reconstruct['minpegel'], errorlevel) if errorlevel[0, 0] == 0: Tab2ind = 0 Npegel = len(pegel) NTimetaba = len(Timetaba) NTimetabb = len(Timetabb) Tab2size = 5000 Tab2 = np.zeros((5, Tab2size)) for i in range(Npegel): if NTimetaba > 0 and NTimetabb > 0: Tab2, Tab2ind, NTimetaba, NTimetabb, errorlevel = _speed2( Timetaba, Timetabb, Tab2, Sensab, Anzpara, reconstruct['Mustsperr'], pegel[i], NTimetaba, NTimetabb, Tab2ind, errorlevel) if (Tab2ind) < Tab2size: Tab2 = Tab2[:, :Tab2ind] else: print(f'Arraysize Tab2 too low: modul speed; size: {Tab2.shape}') errorlevel[0, 0] = RECON_ERROR INa = Ia * I0a INb = Ib * I0b return y_data, x_data, errorlevel, vibration_detected if errorlevel[0, 0] == 0: if not equal(reconstruct['pegdiff'], 0): Tab2 = hendfilt(Tab2, reconstruct['pegdiff'], reconstruct['tdiff']) Tab2 = _materw2(Tab2, Sensab) index = np.array([0]) while len(index) > 0: diff_tab6 = np.diff(Tab2[5, :]) if Tab2.shape[0] > 5 else np.array([]) index = np.where(diff_tab6 == 0)[0] if len(index) > 0: idx = index[0] Tab2[:, idx] = (Tab2[:, idx] + Tab2[:, idx + 1]) / 2.0 Tab2 = np.delete(Tab2, idx + 1, axis=1) v, errorv, vibration_detected = _vfunc( Tab2, Anzpara, dt, vscan, Sensab, reconstruct, t_arr) if vibration_detected: try: vibdir = 'C:\\XRAYDATA\\vibdata\\' import os if not os.path.exists(vibdir): os.makedirs(vibdir) liste = os.listdir(vibdir) if len(liste) > 1000: try: full_paths = [os.path.join(vibdir, f) for f in liste] full_paths.sort(key=lambda p: os.path.getmtime(p)) if len(full_paths) > 0: os.remove(full_paths[0]) except Exception: pass except Exception: pass if (errorv[0, 0] == 0) and (reconstruct['active'] > 1) and vibration_detected: INa, INb, xr = recon(INa, INb, v, dt) INa, INb, xr = unist(INa, INb, xr, dx) if Anzpara > 0: pass errorlevel = errorv.copy() else: if vibration_detected and errorv[0, 0] and not errorin[0, 0]: errorlevel[0, 0] = errorv[0, 0] else: errorlevel[0, 0] = errorin[0, 0] errorlevel[1, 0] = errorv[1, 0] if len(errorv) > 1 else errorlevel[1, 0] INa = INa * I0a INb = INb * I0b else: INa = Ia.copy() INb = Ib.copy() return INb, INa, errorlevel, vibration_detected