def reconstruct(c1, L, K, n=8): from zernike import RZern cart = RZern(n) ddx = np.linspace(-1.0, 1.0, K) ddy = np.linspace(-1.0, 1.0, L) xv, yv = np.meshgrid(ddx, ddy) cart.make_cart_grid(xv, yv) return cart.eval_grid(c1[0:cart.nk], matrix=True)
def test_normalisations_real(self): log = logging.getLogger('TestZern.test_normalisations_real') n_alpha = 6 L, K = 400, 357 # polar grid pol = RZern(n_alpha) fitAlpha = FitZern(pol, L, K) t1 = time() pol.make_pol_grid(fitAlpha.rho_j, fitAlpha.theta_i) t2 = time() log.debug('make pol grid {:.6f}'.format(t2 - t1)) # cartesian grid cart = RZern(n_alpha) dd = np.linspace(-1.0, 1.0, max(L, K)) xx, yy = np.meshgrid(dd, dd) t1 = time() cart.make_cart_grid(xx, yy) t2 = time() log.debug('make cart grid {:.6f}'.format(t2 - t1)) smap = np.isfinite(cart.eval_grid(np.zeros(cart.nk))) scale = (1.0/np.sum(smap)) log.debug('') log.debug('{} modes, {} x {} grid'.format(n_alpha, L, K)) for i in range(pol.nk): a = np.zeros(pol.nk) a[i] = 1.0 Phi_a = cart.eval_grid(a) for j in range(pol.nk): b = np.zeros(pol.nk) b[j] = 1.0 Phi_b = cart.eval_grid(b) ip = scale*np.sum(Phi_a[smap]*Phi_b[smap]) if i == j: eip = 1.0 else: eip = 0.0 iperr = abs(ip - eip) log.debug('<{:02},{:02}> = {:+e} {:+e}'.format( i + 1, j + 1, ip, iperr)) self.assertTrue(iperr < self.max_ip_err)
def phase_Zernike(self, Plot=True, Save=False): SLMRes = np.min([self.SLMResX, self.SLMResY]) apertureD = int(round(self.aperture_radius / self.pixelpitch * 2)) Zmatrix_size = np.min([SLMRes, apertureD]) r0 = self.pixelpitch * Zmatrix_size / 2 r0mm = r0 * 1e3 cart = RZern(6) ddx = np.linspace(-1.0, 1.0, Zmatrix_size) ddy = np.linspace(-1.0, 1.0, Zmatrix_size) xv, yv = np.meshgrid(ddx, ddy) cart.make_cart_grid(xv, yv) c = np.zeros(cart.nk) c[[self.ind_Zernike]] = 1 Phi = cart.eval_grid(c, matrix=True) # change all nan values in Phi matrix to be zero where_are_NaNs = np.isnan(Phi) Phi[where_are_NaNs] = 0 Phi_norm = Phi n = cart.ntab[self.ind_Zernike] m = cart.mtab[self.ind_Zernike] zernike_str = f"Radial: {n}, Angular: {m}" print(zernike_str) # Launch the Zernike phase pattern to SLM screen SLM_screen_aberr = np.zeros((int(self.SLMResY), int(self.SLMResX))) col_Phi_norm = np.size(Phi_norm, axis=1) row_Phi_norm = np.size(Phi_norm, axis=0) startRow_screen = self.SLMResY / 2 - round(row_Phi_norm / 2) endRow_screen = self.SLMResY / 2 + round(row_Phi_norm / 2) startCol_screen = self.SLMResX / 2 - round(col_Phi_norm / 2) endCol_screen = self.SLMResX / 2 + round(col_Phi_norm / 2) SLM_screen_aberr[int(startRow_screen):int(endRow_screen), :][:, int(startCol_screen):int(endCol_screen)] = \ Phi_norm*self.percent if Plot: im = plt.imshow(Phi_norm, origin='lower', extent=(-r0mm, r0mm, -r0mm, r0mm)) plt.title(zernike_str) plt.colorbar(im) plt.show() if Save: np.savetxt(f"SLM_Zernike_{n}{m}_{percent}.csv", SLM_screen_aberr, delimiter=",") return SLM_screen_aberr, m, n
inds = np.where(cart.ntab == n)[0] ms = cart.mtab[inds] inds = inds[ms.argsort()] left = (1 - inds.size * width1 - (inds.size - 1) * width_span) / 2 bott = (1 - nrows * height1 - (nrows - 1) * height_span) / 2 bt = (bott + (nrows - i - 1) * (height1 + height_span)) for j in range(inds.size): lf = left + j * (width1 + width_span) + leftoff ax = fig.add_axes([lf, bt, width1, height1]) c *= 0.0 c[inds[j]] = 1.0 Phi = cart.eval_grid(c, matrix=True) ax.imshow(Phi, origin='lower', extent=[0, 1, 0, 1]) ax.axis('off') ze = inds[j] + 1 zn = cart.ntab[inds[j]] zm = cart.mtab[inds[j]] ax.text(1, 1, '$\\#' + str(ze) + '$', transform=ax.transAxes, fontsize=fs1) plt.text(0, bt + height1 / 2,
def main_Calib(filename, output, mode, alg, basis, order, figure, verbose, offset, qt, pre, split): ''' # main program # input: radius: %+.3f, 'str' (in makefile, str is default) # path: file storage path, 'str' # fout: file output name as .h5, 'str' (.h5 not included') # cut_max: cut off of Legendre # output: the gathered result EventID, ChannelID, x, y, z ''' if pre != 'r': print('begin reading file', flush=True) EventID, ChannelID, Q, PETime, photonTime, PulseTime, dETime, x, y, z = pub.ReadFile(filename) VertexTruth = (np.vstack((x, y, z))/1e3).T if(offset): off = pub.LoadBase(offset) else: off = np.zeros_like(PMTPos[:,0]) print('total event: %d' % np.size(np.unique(EventID)), flush=True) print('begin processing legendre coeff', flush=True) # this part for the same vertex tmp = time.time() EventNo = np.size(np.unique(EventID)) PMTNo = np.size(PMTPos[:,0]) if mode == 'PE': PMTPosRep = np.tile(PMTPos, (EventNo,1)) vertex = np.repeat(VertexTruth, PMTNo, axis=0) elif mode == 'time': counts = np.bincount(EventID) counts = counts[counts!=0] PMTPosRep = PMTPos[ChannelID] vertex = np.repeat(VertexTruth, counts, axis=0) elif mode == 'combined': PMTPosRep = np.tile(PMTPos, (EventNo,1)) vertex = np.repeat(VertexTruth, PMTNo, axis=0) if basis == 'Legendre': X, cos_theta = pub.LegendreCoeff(PMTPosRep, vertex, order, Legendre=True) elif basis == 'Zernike': from zernike import RZern cos_theta = pub.LegendreCoeff(PMTPosRep, vertex, order, Legendre=False) cart = RZern(order) nk = cart.nk m = cart.mtab n = cart.ntab rho = np.linalg.norm(vertex, axis=1)/0.65 theta = np.arccos(cos_theta) X = np.zeros((rho.shape[0], nk)) for i in np.arange(nk): if not i % 5: print(f'process {i}-th event') X[:,i] = cart.Zk(i, rho, theta) X = X[:,m>=0] print(f'rank: {np.linalg.matrix_rank(X)}') print(f'use {time.time() - tmp} s') # which info should be used if mode == 'PE': y = Q elif mode == 'time': y = PulseTime elif mode == 'combined': # PulseTime = PulseTime - np.min(PulseTime) # PulseTime = (PulseTime - np.max(PulseTime)/2)/np.max(PulseTime)*2 # print(np.min(PulseTime), np.max(PulseTime)) PulseTime = (PulseTime - np.max(PulseTime)/2)/np.max(PulseTime)*2 bins = np.arange(-1, 0.05, 0.1) N = 10 # Legendre coeff x = pub.legval(bins, np.eye(N).reshape(N, N, 1)) # 1st basis Y = np.tile(x, len(np.unique(EventID))*len(np.unique(ChannelID))).T # 2nd basis X = np.repeat(X, bins.shape[0], axis=0) # output y = np.zeros((len(np.unique(EventID)), len(np.unique(ChannelID)), len(bins))) ''' basis = np.zeros((X.shape[0], X.shape[1]*Y.shape[1])) for i_index, i in enumerate(np.arange(X.shape[1])): for j_index, j in enumerate(np.arange(Y.shape[1])): total_index = i_index*Y.shape[1] + j_index if not total_index % 10: print(total_index) basis[:, total_index] = X[:,i_index]*Y[:,j_index] X = basis ''' split_index = np.unique(EventID).shape[0] for k_index, k in enumerate(np.unique(EventID)): # event begin with 1 if k_index > split_index * split: break if not k % 100: print(k) index = EventID == k CID = ChannelID[index] Pulse_t = PulseTime[index] for i in np.unique(CID): # PMT begin with 0 y[k_index, i, 1:], _ = np.histogram(Pulse_t[CID==i], bins=bins) y = np.reshape(y,(-1)) if verbose: print(f'the basis shape is {X.shape}, and the dependent variable shape is {y.shape}') if pre =='w': if split != 1: split_index = np.int(split*y.shape[0]) X = X[:split_index] Y = Y[:split_index] y = y[:split_index] import pandas as pd import pyarrow as pa import pyarrow.parquet as pq y = np.atleast_2d(y).T #data = np.hstack((X, y, np.ones_like(y))) df_X = pd.DataFrame(X) X_names = [] for i in df_X.columns: X_names.append('X' + str(i)) df_X.columns = X_names df_Y = pd.DataFrame(Y) Y_names = [] for i in df_Y.columns: Y_names.append('Y' + str(i)) df_Y.columns = Y_names df_y = pd.DataFrame(y) df_y.columns = ['output'] df = pd.concat([df_X, df_Y, df_y], axis=1) table = pa.Table.from_pandas(df) pq.write_table(table, 'test1.parquet') return if not pre: # Regression methods: if alg == 'sms': import statsmodels.api as sm if mode == 'PE': model = sm.GLM(y, X, family=sm.families.Poisson(), fit_intercept=False) result = model.fit() if verbose: print(result.summary()) AIC = result.aic coef_ = result.params std = result.bse elif mode == 'time': import pandas as pd data = pd.DataFrame(data = np.hstack((X, np.atleast_2d(y).T))) strs = 'y ~ ' start = data.keys().start stop = data.keys().stop step = data.keys().step cname = [] cname.append('X0') for i in np.arange(start+1, stop, step): if i == start + 1: strs += 'X%d ' % i elif i == stop - step: pass else: strs += ' + X%d ' % i if i == stop - step: cname.append('y') else: cname.append('X%d' % i) data.columns = cname mod = sm.formula.quantreg(strs, data[cname]) result = mod.fit(q=qt,) coef_ = result.params AIC = np.zeros_like(coef_) std = np.zeros_like(coef_) print('Waring! No AIC and std value') elif mode == 'combined': # data = pd.DataFrame(data = np.hstack((basis, np.atleast_2d(y).T))) with h5py.File(output,'w') as out: out.create_dataset('X', data = X) out.create_dataset('Y', data = y) print('begin...') model = sm.GLM(y, X, family=sm.families.Poisson()) result = model.fit() if verbose: print(result.summary()) coef_ = result.params std = result.bse AIC = result.aic if verbose: print(result.summary()) elif (alg == 'custom'): from scipy.optimize import minimize x0 = np.zeros_like(X[0]) # initial value (be careful of Zernike order) if mode == 'PE': x0[0] = 0.8 + np.log(2) # intercept is much more important result = minimize(pub.CalibPE, x0=x0, method='SLSQP', args = (y, PMTPos, X)) elif mode == 'time': x0[0] = np.mean(y) qt = 0.1 ts = 2.6 result = minimize(pub.CalibTime, x0=x0, method='SLSQP', args = (np.hstack((EventID, EventID)), y, X, qt, ts)) elif mode == 'combined': x0 = np.zeros_like(X[0]) x0[0] = 0.8 + np.log(2) # intercept is much more important result = minimize(pub.CalibPE, x0=x0, method='SLSQP', args = (y, PMTPos, X)) coef_ = np.array(result.x, dtype=float) if verbose: print(result.message) AIC = np.zeros_like(coef_) std = np.zeros_like(coef_) H = pub.MyHessian(result.x, pub.CalibPE, *(y, PMTPos, X)) # H = pub.MyHessian(result.x, *(Q, PMTPos, X, pub.CalibTime)) # std = 1/np.sqrt(-np.diag(np.linalg.pinv(H1))) print(coef_) # print(std) print('Waring! No AIC and std value, std is testing') elif alg == 'sk': from sklearn.linear_model import TweedieRegressor alpha = 0.001 reg = TweedieRegressor(power=1, alpha=alpha, link='log', max_iter=1000, tol=1e-6, fit_intercept=False) reg.fit(X, y) # just for point data # pred = reg.predict(X[0:30,0:cut+1]) print('coeff:\n', reg.coef_,'\n') coef_ = reg.coef_ AIC = np.zeros_like(coef_) std = np.zeros_like(coef_) print('Waring! No AIC and std value') elif alg == 'h2o': import h2o from h2o.estimators.gbm import H2OGradientBoostingEstimator from h2o.estimators.glm import H2OGeneralizedLinearEstimator if mode != 'combined': y = np.atleast_2d(y).T data = np.hstack((X, y, np.ones_like(y))) h2o.init() hf = h2o.H2OFrame(data) predictors = hf.columns[0:-2] response_col = hf.columns[-2] if mode == 'PE': #offset_col = hf.columns[-1] glm_model = H2OGeneralizedLinearEstimator(family= "poisson", #offset_column = offset_col, lambda_ = 0, compute_p_values = True) glm_model.train(predictors, response_col, training_frame=hf) coef_table = glm_model._model_json['output']['coefficients_table'] coef_ = glm_model.coef() elif mode == 'time': gbm = H2OGradientBoostingEstimator(distribution="quantile", seed = 1234, stopping_metric = "mse", stopping_tolerance = 1e-4) gbm.train(x = predictors, y = response_col, training_frame = hf) breakpoint() print(gbm) exit() elif mode == 'combined': y = np.atleast_2d(y).T data = np.hstack((X, Y, y, np.ones_like(y))) h2o.init() hf = h2o.H2OFrame(data) predictors = hf.columns[0:-2] response_col = hf.columns[-2] if verbose: print(coef_) if basis == 'Zernike': print(f'Regession coef shape is f{np.array(coef_).shape}, Zernike shape is {nk}') coef_ = coef_table['coefficients'] std = coef_table['std_error'] AIC = glm_model.aic() h2o.cluster().shutdown() elif pre == 'r': import h2o from h2o.estimators.gbm import H2OGradientBoostingEstimator from h2o.estimators.glm import H2OGeneralizedLinearEstimator h2o.init() hf = h2o.import_file("electron-1.parquet") pairs = [] for i in hf.columns: for j in hf.columns: if (i.startswith('Z') and j.startswith('L')): if ((i!='X0') and (j != 'Y0')): pairs.append((i,j)) predictors = hf.columns[2:] response_col = hf.columns[0] print(predictors) print(response_col) print(pairs) if mode == 'PE': #offset_col = hf.columns[-1] glm_model = H2OGeneralizedLinearEstimator(family= "poisson", #offset_column = offset_col, lambda_ = 0, compute_p_values = True) glm_model.train(predictors, response_col, training_frame=hf) elif mode == 'combined': #offset_col = hf.columns[-1] glm_model = H2OGeneralizedLinearEstimator(family= "poisson", #offset_column = offset_col, interaction_pairs=pairs, lambda_ = 0, #remove_collinear_columns = True, compute_p_values = True) glm_model.train(predictors, response_col, training_frame=hf) breakpoint() coef_table = glm_model._model_json['output']['coefficients_table'] coef_ = coef_table['coefficients'] std = coef_table['std_error'] AIC = glm_model.aic() print(f'Regession coef is f{np.array(coef_)}') if (figure=='ON'): import matplotlib.pyplot as plt L, K = 500, 500 ddx = np.linspace(-1.0, 1.0, K) ddy = np.linspace(-1.0, 1.0, L) xv, yv = np.meshgrid(ddx, ddy) cart.make_cart_grid(xv, yv) # normal scale # im = plt.imshow(np.exp(cart.eval_grid(np.array(coef_), matrix=True)), origin='lower', extent=(-1, 1, -1, 1)) # log scale im = plt.imshow(cart.eval_grid(np.array(coef_), matrix=True), origin='lower', extent=(-1, 1, -1, 1)) plt.colorbar() plt.savefig('test.png') else: print('error regression algorithm') with h5py.File(output,'w') as out: out.create_dataset('coeff' + str(order), data = coef_) out.create_dataset('std' + str(order), data = std) out.create_dataset('AIC' + str(order), data = AIC)
class ZernikePanel(QWidget): def_pars = {'zernike_labels': {}, 'shown_modes': 21} def __init__(self, wavelength, n_radial, z0=None, callback=None, pars={}, parent=None): super().__init__(parent=parent) self.log = logging.getLogger(self.__class__.__name__) self.pars = {**deepcopy(self.def_pars), **deepcopy(pars)} self.units = 'rad' self.status = None self.mul = 1.0 self.figphi = None self.ax = None self.im = None self.cb = None self.shape = (128, 128) self.P = 1 self.rzern = RZern(n_radial) dd = np.linspace(-1, 1, self.shape[0]) xv, yv = np.meshgrid(dd, dd) self.rzern.make_cart_grid(xv, yv) self.rad_to_nm = wavelength / (2 * np.pi) self.callback = callback self.zernike_rows = [] if z0 is None: self.z = np.zeros(self.rzern.nk) else: self.z = z0.copy() assert (self.rzern.nk == self.z.size) group_phase = QGroupBox('phase') lay_phase = QGridLayout() group_phase.setLayout(lay_phase) self.figphi = FigureCanvas(Figure(figsize=(2, 2))) self.ax = self.figphi.figure.add_subplot(1, 1, 1) phi = self.rzern.matrix(self.rzern.eval_grid(np.dot(self.P, self.z))) self.im = self.ax.imshow(phi, origin='lower') self.cb = self.figphi.figure.colorbar(self.im) self.cb.locator = ticker.MaxNLocator(nbins=5) self.cb.update_ticks() self.ax.axis('off') self.status = QLabel('') lay_phase.addWidget(self.figphi, 0, 0) lay_phase.addWidget(self.status, 1, 0) def nmodes(): return min(self.pars['shown_modes'], self.rzern.nk) bot = QGroupBox('Zernike') lay_zern = QGridLayout() bot.setLayout(lay_zern) labzm = QLabel('shown modes') lezm = QLineEdit(str(nmodes())) lezm.setMaximumWidth(50) lezmval = MyQIntValidator(1, self.rzern.nk) lezmval.setFixup(nmodes()) lezm.setValidator(lezmval) brad = QCheckBox('rad') brad.setChecked(True) breset = QPushButton('reset') lay_zern.addWidget(labzm, 0, 0) lay_zern.addWidget(lezm, 0, 1) lay_zern.addWidget(brad, 0, 2) lay_zern.addWidget(breset, 0, 3) scroll = QScrollArea() lay_zern.addWidget(scroll, 1, 0, 1, 5) scroll.setWidget(QWidget()) scrollLayout = QGridLayout(scroll.widget()) scroll.setWidgetResizable(True) def make_hand_slider(ind): def f(r): self.z[ind] = r self.update_phi_plot() return f def make_hand_lab(le, i): def f(): self.pars['zernike_labels'][str(i)] = le.text() return f def default_zernike_name(i, n, m): if i == 1: return 'piston' elif i == 2: return 'tip' elif i == 3: return 'tilt' elif i == 4: return 'defocus' elif m == 0: return 'spherical' elif abs(m) == 1: return 'coma' elif abs(m) == 2: return 'astigmatism' elif abs(m) == 3: return 'trefoil' elif abs(m) == 4: return 'quadrafoil' elif abs(m) == 5: return 'pentafoil' else: return '' def make_update_zernike_rows(): def f(mynk=None): if mynk is None: mynk = len(self.zernike_rows) ntab = self.rzern.ntab mtab = self.rzern.mtab if len(self.zernike_rows) < mynk: for i in range(len(self.zernike_rows), mynk): lab = QLabel( f'Z<sub>{i + 1}</sub> ' + f'Z<sub>{ntab[i]}</sub><sup>{mtab[i]}</sup>') slider = RelSlider(self.z[i], make_hand_slider(i)) if str(i) in self.pars['zernike_labels'].keys(): zname = self.pars['zernike_labels'][str(i)] else: zname = default_zernike_name( i + 1, ntab[i], mtab[i]) self.pars['zernike_labels'][str(i)] = zname lbn = QLineEdit(zname) lbn.setMaximumWidth(120) hand_lab = make_hand_lab(lbn, i) lbn.editingFinished.connect(hand_lab) scrollLayout.addWidget(lab, i, 0) scrollLayout.addWidget(lbn, i, 1) slider.add_to_layout(scrollLayout, i, 2) self.zernike_rows.append((lab, slider, lbn, hand_lab)) assert (len(self.zernike_rows) == mynk) elif len(self.zernike_rows) > mynk: for i in range(len(self.zernike_rows) - 1, mynk - 1, -1): lab, slider, lbn, hand_lab = self.zernike_rows.pop() scrollLayout.removeWidget(lab) slider.remove_from_layout(scrollLayout) scrollLayout.removeWidget(lbn) lbn.editingFinished.disconnect(hand_lab) lab.setParent(None) lbn.setParent(None) assert (len(self.zernike_rows) == mynk) return f self.update_zernike_rows = make_update_zernike_rows() def reset_fun(): self.z *= 0. self.update_gui_controls() self.update_phi_plot() def change_nmodes(): try: ival = int(lezm.text()) assert (ival > 0) assert (ival <= self.rzern.nk) except Exception: lezm.setText(str(len(self.zernike_rows))) return if ival != len(self.zernike_rows): self.update_zernike_rows(ival) self.update_phi_plot() lezm.setText(str(len(self.zernike_rows))) def f2(): def f(b): if b: self.units = 'rad' self.mul = 1.0 else: self.units = 'nm' self.mul = self.rad_to_nm self.update_phi_plot() return f self.update_zernike_rows(nmodes()) brad.stateChanged.connect(f2()) breset.clicked.connect(reset_fun) lezm.editingFinished.connect(change_nmodes) splitv = QSplitter(Qt.Vertical) top = QSplitter(Qt.Horizontal) top.addWidget(group_phase) splitv.addWidget(top) splitv.addWidget(bot) self.top = top self.bot = bot l1 = QGridLayout() l1.addWidget(splitv) self.setLayout(l1) self.lezm = lezm def save_parameters(self, merge={}): d = {**merge, **self.pars} d['shown_modes'] = len(self.zernike_rows) return d def load_parameters(self, d): self.pars = {**deepcopy(self.def_pars), **deepcopy(d)} nmodes = min(self.pars['shown_modes'], self.rzern.nk) self.pars['shown_modes'] = nmodes self.lezm.blockSignals(True) self.lezm.setText(str(nmodes)) self.lezm.blockSignals(False) self.update_zernike_rows(0) self.update_zernike_rows(nmodes) def update_gui_controls(self): for i, t in enumerate(self.zernike_rows): slider = t[1] slider.block() slider.set_value(self.z[i]) slider.unblock() def update_phi_plot(self, run_callback=True): phi = self.mul * self.rzern.matrix( self.rzern.eval_grid(np.dot(self.P, self.z))) inner = phi[np.isfinite(phi)] min1 = inner.min() max1 = inner.max() rms = self.mul * norm(self.z) self.status.setText( '{} [{: 03.2f} {: 03.2f}] {: 03.2f} PV {: 03.2f} RMS'.format( self.units, min1, max1, max1 - min1, rms)) self.im.set_data(phi) self.im.set_clim(inner.min(), inner.max()) self.figphi.figure.canvas.draw() if self.callback and run_callback: self.callback(self.z)