import numpy as np try: from .validation_utils import limit except ImportError: from validation_utils import limit def _lfilter(b, a, x, zi=None): b = np.asarray(b, dtype=np.float64) a = np.asarray(a, dtype=np.float64) x = np.asarray(x, dtype=np.float64) if a[0] != 1.0: b = b / a[0] a = a / a[0] nb = len(b) na = len(a) n = max(nb, na) if nb < n: b = np.pad(b, (0, n - nb)) if na < n: a = np.pad(a, (0, n - na)) y = np.zeros_like(x) if zi is None: zi = np.zeros(n - 1) else: zi = np.asarray(zi, dtype=np.float64).copy() for i in range(len(x)): xi = x[i] yi = b[0] * xi + zi[0] for j in range(n - 2): zi[j] = b[j + 1] * xi + zi[j + 1] - a[j + 1] * yi zi[n - 2] = b[n - 1] * xi - a[n - 1] * yi y[i] = yi if zi is not None and len(zi) > 0: return y, zi return y def filter2k3(filtpara, x, mode=0): filtpara = np.asarray(filtpara, dtype=np.float64) x = np.asarray(x, dtype=np.float64) N2 = int(np.round(0.5 * (len(filtpara) - 1))) Nx = len(x) if len(filtpara) > len(x): return x.copy(), 0 if mode > 0: filtpara = filtpara[::-1] y = np.convolve(x, filtpara, mode='full') y = np.concatenate([y[N2:Nx], y[:N2]]) if mode == 2: y[:N2] = y[N2] y[-N2:] = y[-N2 - 1] else: y[:N2] = x[:N2] y[Nx - N2:Nx] = x[Nx - N2:Nx] return y, N2 def fifi(b, a, x): b = np.asarray(b, dtype=np.float64).ravel() a = np.asarray(a, dtype=np.float64).ravel() x = np.asarray(x, dtype=np.float64) if len(b) == 0 or len(a) == 0 or len(x) == 0: return np.array([]) orig_shape = x.shape if x.ndim > 1 and min(x.shape) > 1: raise ValueError('Only works for vector input.') x_flat = x.ravel() len_x = len(x_flat) nb = len(b) na = len(a) nfilt = max(nb, na) if nb < nfilt: b = np.pad(b, (0, nfilt - nb)) if na < nfilt: a = np.pad(a, (0, nfilt - na)) nfact = 3 * (nfilt - 1) if len_x <= nfact: raise ValueError('Data must have length more than 3 times filter order.') zi = np.linalg.solve( np.eye(nfilt - 1) - np.column_stack([ -a[1:nfilt], np.row_stack([np.eye(nfilt - 2), np.zeros(nfilt - 2)]) ]), b[1:nfilt] - a[1:nfilt] * b[0] ) y_padded = np.concatenate([ 2 * x_flat[0] - x_flat[nfact:0:-1], x_flat, 2 * x_flat[-1] - x_flat[-2:-nfact - 2:-1] ]) y_padded, _ = _lfilter(b, a, y_padded, zi=zi * y_padded[0]) y_padded = y_padded[::-1] y_padded, _ = _lfilter(b, a, y_padded, zi=zi * y_padded[0]) y_padded = y_padded[::-1] y_out = y_padded[nfact:len_x + nfact] if orig_shape[0] == 1 and x.ndim > 1: y_out = y_out.reshape(1, -1) return y_out def dilate1(x, N): x = np.asarray(x, dtype=np.float64) y = x.copy() N = int(np.round(N)) if N >= 3: filtvek = np.array([1, 0, 0, 0, 1], dtype=np.float64) y2, _ = filter2k3(filtvek, y.astype(float)) y2 = (y2 >= 2) y = np.logical_or(y.astype(bool), y2).astype(float) N = 2 if N == 2: filtvek = np.array([1, 0, 0, 1, 0], dtype=np.float64) y2, _ = filter2k3(filtvek, y.astype(float)) y2 = (y2 >= 2) y = np.logical_or(y.astype(bool), y2).astype(float) filtvek = np.array([0, 1, 0, 0, 1], dtype=np.float64) y2, _ = filter2k3(filtvek, y.astype(float)) y2 = (y2 >= 2) y = np.logical_or(y.astype(bool), y2).astype(float) N = 1 if N == 1: filtvek = np.array([1, 0, 1], dtype=np.float64) y2, _ = filter2k3(filtvek, y.astype(float)) y2 = (y2 >= 2) y = np.logical_or(y.astype(bool), y2).astype(float) return y def videochk(video, errnorm, rawxycount, maxrawxycount, memerror, minlevel1, maxlevel1, minlevel2, maxlevel2, errmssmin_start, errmssmax_start, errmssmin_end, errmssmax_end): errfac = 0.80 errnorm = np.atleast_2d(np.asarray(errnorm, dtype=np.float64)) if errnorm.shape[0] < 2: errnorm = np.vstack([errnorm, np.zeros((1, errnorm.shape[1]))]) video = np.atleast_2d(np.asarray(video, dtype=np.float64)) if not errnorm[0, 0] and rawxycount < (maxrawxycount - 2): sorted_vid = np.sort(video, axis=1)[:, ::-1] if sorted_vid.shape[1] >= 25: meanmax = np.mean(sorted_vid[:, 5:25]) elif sorted_vid.shape[1] > 5: meanmax = np.mean(sorted_vid[:, 5:]) else: meanmax = np.mean(sorted_vid) if minlevel1 is not None and len(np.atleast_1d(minlevel1)) > 0 and meanmax < minlevel1: errnorm[0, 0] = errmssmin_start if maxlevel1 is not None and len(np.atleast_1d(maxlevel1)) > 0 and meanmax > maxlevel1: errnorm[0, 0] = errmssmax_start elif rawxycount == maxrawxycount - 2 or rawxycount == maxrawxycount - 1 or memerror: sorted_vid = np.sort(video, axis=1)[:, ::-1] if sorted_vid.shape[1] >= 25: meanmax = np.mean(sorted_vid[:, 5:25]) elif sorted_vid.shape[1] > 5: meanmax = np.mean(sorted_vid[:, 5:]) else: meanmax = np.mean(sorted_vid) if minlevel2 is not None and len(np.atleast_1d(minlevel2)) > 0: if meanmax < minlevel2 * errfac: if not errnorm[0, 0]: errnorm[0, 0] = errmssmin_end memerror = errmssmin_end elif meanmax < minlevel2: if not errnorm[1, 0]: errnorm[1, 0] = errmssmin_end memerror = errmssmin_end if maxlevel2 is not None and len(np.atleast_1d(maxlevel2)) > 0 and meanmax > maxlevel2: if not errnorm[0, 0]: errnorm[0, 0] = errmssmax_end memerror = errmssmax_end return errnorm, memerror def scan_norm(in_sig, in0bright, in0darkStatic, in0darkDynamic, dxZeile, gap, videoOffs, errsettings, mem, init, video_req_time): mmPixel = 0 maxrawxycount = 210 inbright_minlevel1 = 5000 inbright_maxlevel1 = 12000 inbright_minlevel2 = 4000 inbright_maxlevel2 = 12000 inbright_too_low = 196 inbright_too_high = 197 raw_minlevel1 = [] raw_maxlevel1 = 12000 raw_minlevel2 = [] raw_maxlevel2 = 12000 raw_too_low = 194 raw_too_high = 195 raw_Contr = 193 dark_maxwarnlevel = 5000 dark_maxerrlevel = 6000 dark_minerrlevel = 1 darkdyn_too_low = 198 darkdyn_too_high = 199 darkdyntimeout = 600 maxdarkdyncount = 2 * darkdyntimeout / video_req_time rad_minlevel1 = 5000 rad_maxlevel1 = [] rad_minlevel2 = 4000 rad_maxlevel2 = [] rad_too_low = 200 rad_too_high = 195 in_sig = np.asarray(in_sig, dtype=np.float64).ravel() in0bright = np.asarray(in0bright, dtype=np.float64).ravel() in0darkStatic = np.asarray(in0darkStatic, dtype=np.float64).ravel() in0darkDynamic = np.asarray(in0darkDynamic, dtype=np.float64).ravel() start = int(videoOffs) stop = len(in_sig) - int(videoOffs) - 1 if mem is None or not mem or init: mem = {} mem['rawxycount'] = 0 mem['darkdyncount'] = 0 mem['ref_error'] = 0 mem['raw_error'] = 0 mem['rad_error'] = 0 if mem['rawxycount'] < maxrawxycount: mem['rawxycount'] += 1 if mem['darkdyncount'] < maxdarkdyncount: mem['darkdyncount'] += 1 errnorm = np.atleast_2d(np.asarray(errsettings, dtype=np.float64)).copy() if errnorm.shape[0] < 2: errnorm = np.vstack([errnorm, np.zeros((1, max(1, errnorm.shape[1])))]) if not errnorm[0, 0]: if (len(in_sig) != len(in0darkDynamic) or len(in_sig) != len(in0bright) or len(in_sig) != len(in0darkStatic)): errnorm[0, 0] = 186 elif len(in_sig) < 200: errnorm[0, 0] = 186 bright_seg = in0bright[start:stop + 1] errref, mem['ref_error'] = videochk( bright_seg, np.array([[0], [0]]), mem['rawxycount'], maxrawxycount, mem['ref_error'], inbright_minlevel1, inbright_maxlevel1, inbright_minlevel2, inbright_maxlevel2, inbright_too_low, inbright_too_high, inbright_too_low, inbright_too_high ) if not errnorm[1, 0]: if errref[0, 0]: errnorm[1, 0] = errref[0, 0] elif errref[1, 0]: errnorm[1, 0] = errref[1, 0] raw_seg = in_sig[start:stop + 1] errnorm, mem['raw_error'] = videochk( raw_seg, errnorm, mem['rawxycount'], maxrawxycount, mem['raw_error'], raw_minlevel1, raw_maxlevel1, raw_minlevel2, raw_maxlevel2, raw_Contr, raw_Contr, raw_too_low, raw_too_high ) if mem['darkdyncount'] >= maxdarkdyncount: dark_dyn_seg = in0darkDynamic[start:stop + 1] if not errnorm[0, 0]: if np.mean(dark_dyn_seg) > dark_maxerrlevel: errnorm[0, 0] = darkdyn_too_high elif np.mean(dark_dyn_seg) < dark_minerrlevel: errnorm[0, 0] = darkdyn_too_low if np.all(errnorm[:, 0] == 0): if np.mean(dark_dyn_seg) > dark_maxwarnlevel: errnorm[1, 0] = darkdyn_too_high rad_seg = in_sig[start:stop + 1] - in0darkDynamic[start:stop + 1] errnorm, mem['rad_error'] = videochk( rad_seg, errnorm, mem['rawxycount'], maxrawxycount, mem['rad_error'], rad_minlevel1, rad_maxlevel1, rad_minlevel2, rad_maxlevel2, raw_Contr, raw_Contr, rad_too_low, rad_too_high ) normvideo = np.zeros(stop - start + 1) if not errnorm[0, 0]: intsOffset = 100.0 intsFactor = 40000 in_norm = intsFactor * ((in_sig[start:stop + 1] - in0darkDynamic[start:stop + 1]) / (in0bright[start:stop + 1] - in0darkStatic[start:stop + 1])) in_norm, _, _ = limit(in_norm, np.array([]), 1) in_norm = in_norm + intsOffset if gap is not None and len(np.atleast_1d(gap)) > 0: gap_arr = np.asarray(gap, dtype=np.float64) if gap_arr.ndim == 1: gap_arr = gap_arr.reshape(-1, 1) sort_idx = np.argsort(gap_arr[0, :]) gap_arr[0, :] = gap_arr[0, sort_idx] gap_arr[0, :] = np.round(gap_arr[0, :]) gap_arr[1, :] = gap_arr[1, sort_idx] if gap_arr.shape[0] >= 3: gap_arr[2, :] = np.round(gap_arr[2, sort_idx]) if gap_arr.shape[0] >= 4: gap_arr[3, :] = np.round(gap_arr[3, sort_idx]) gap_arr[0, :] = np.round(gap_arr[0, :] - videoOffs) valid_idx = np.where((gap_arr[0, :] > 1) & (gap_arr[0, :] < len(in_norm)))[0] gap_arr = gap_arr[:, valid_idx] else: gap_arr = np.array([]) if gap_arr.size == 0: normvideo = in_norm else: normvideo_list = [] startind = 0 mmstart1 = 0.0 mmstart2 = 0.0 for ii in range(gap_arr.shape[1]): stopind = int(gap_arr[0, ii] - gap_arr[2, ii]) - 1 x1 = mmstart1 + np.arange(0, stopind - startind + 1) * dxZeile x2 = np.arange(mmstart2, x1[-1] + dxZeile, dxZeile) if stopind > startind: if startind == 0: secvideo = in_norm[startind:stopind + 1] else: secvideo = np.interp(x2, x1, in_norm[startind:stopind + 1]) normvideo_list.append(secvideo) mmstart1 = x1[-1] if len(x2) > 0: mmstart2 = x2[-1] + dxZeile numdisablepix = int(gap_arr[2, ii] + gap_arr[3, ii]) startind = stopind + 1 stopind = startind + numdisablepix if stopind < len(in_norm): x1 = mmstart1 + np.array([0, gap_arr[1, ii] + numdisablepix * dxZeile]) x2 = np.arange(mmstart2, x1[-1] + dxZeile, dxZeile) secvideo = np.interp(x2, x1, in_norm[[startind, min(stopind, len(in_norm) - 1)]]) normvideo_list.append(secvideo) mmstart1 = x1[-1] if len(x2) > 0: mmstart2 = x2[-1] + dxZeile startind = stopind + 1 stopind = len(in_norm) - 1 if startind <= stopind: x1 = mmstart1 + np.arange(0, stopind - startind + 1) * dxZeile x2 = np.arange(mmstart2, x1[-1] + dxZeile, dxZeile) secvideo = np.interp(x2, x1, in_norm[startind:stopind + 1]) normvideo_list.append(secvideo) if normvideo_list: normvideo = np.concatenate(normvideo_list) else: normvideo = in_norm mmPixel = -0.5 * dxZeile * (len(normvideo) - 1) return normvideo, mmPixel, errnorm, mem