def calc_adj(self): for corr in self.corr_axes: if corr not in self.coeffs: self.coeffs[corr] = self.calc_coeffs(corr) ax_slice, az_slice = ifunc.get_ax_az_slice(self.clip) for axis in self.displ_axes: logging.info("Computing adj[{}][{}][full,clip]".format(axis, corr)) adj = ifunc.calc_adj_displ(self.ifuncs[axis], self.coeffs[corr], adj_clip=self.adj_clip) self.adj[axis][corr]["full"] = adj self.adj[axis][corr]["clip"] = adj[ax_slice, az_slice]
def calc_adj(self): for corr in self.corr_axes: if corr not in self.coeffs: self.coeffs[corr] = self.calc_coeffs(corr) ax_slice, az_slice = ifunc.get_ax_az_slice(self.clip) for axis in self.displ_axes: logging.info("Computing adj[{}][{}][full,clip]".format( axis, corr)) adj = ifunc.calc_adj_displ(self.ifuncs[axis], self.coeffs[corr], adj_clip=self.adj_clip) self.adj[axis][corr]['full'] = adj self.adj[axis][corr]['clip'] = adj[ax_slice, az_slice]
def calc_adj(self): for corr in self.corr_axes: logging.info('Computing corr coeffs using axis {}...' .format(corr)) coeffs = ifunc.calc_coeffs(self.ifuncs[corr], self.displ[corr]['img']['full'], n_ss=self.n_ss, clip=self.clip) self.coeffs[corr] = coeffs clip = self.clip for axis in self.displ_axes: logging.info("Computing adj[{}][{}][full,clip]". format(axis, corr)) adj = ifunc.calc_adj_displ(self.ifuncs[axis], coeffs) self.adj[axis][corr]['full'] = adj self.adj[axis][corr]['clip'] = adj[clip:-clip, clip:-clip]
pid = os.getpid() hostname = socket.gethostname() outfile = os.path.join('drive_noise-{}-{}.dat'.format(hostname, pid)) for noise in noises: print 'Drive noise = {} (fraction of {} drive voltage)'.format( noise, args.drive_ref) drive_noise = noise * args.drive_ref coeffs_noisy = coeffs.copy() if noise > 0: ok = coeffs > 0 # Only add noise to actuators that are being driven coeffs_noisy[ok] += np.random.normal(0.0, scale=drive_noise, size=len(coeffs))[ok] coeffs_noisy = np.abs(coeffs_noisy) adj_displ = ifunc.calc_adj_displ(calc_mtf.ifuncs, coeffs_noisy) resid = bias.vals + error.vals - adj_displ resid_stats = ifunc.calc_scatter(resid, ax_clip=out['ax_clip'], az_clip=out['az_clip'], n_proc=args.n_proc, n_strips=args.n_strips) print 'Corr HPD={:.2f} RMSD={:.2f}'.format(resid_stats['hpd'], resid_stats['rmsd']) hpd = resid_stats['hpd'] rmsd = resid_stats['rmsd'] with open(outfile, 'a') as f: print >>f, ' '.join(str(x) for x in (noise, hpd, rmsd))
outfile = os.path.join('drive_noise-{}-{}.dat'.format(hostname, pid)) for noise in noises: print 'Drive noise = {} (fraction of {} drive voltage)'.format( noise, args.drive_ref) drive_noise = noise * args.drive_ref coeffs_noisy = coeffs.copy() if noise > 0: ok = coeffs > 0 # Only add noise to actuators that are being driven coeffs_noisy[ok] += np.random.normal(0.0, scale=drive_noise, size=len(coeffs))[ok] coeffs_noisy = np.abs(coeffs_noisy) adj_displ = ifunc.calc_adj_displ(calc_mtf.ifuncs, coeffs_noisy) resid = bias.vals + error.vals - adj_displ resid_stats = ifunc.calc_scatter(resid, ax_clip=out['ax_clip'], az_clip=out['az_clip'], n_proc=args.n_proc, n_strips=args.n_strips) print 'Corr HPD={:.2f} RMSD={:.2f}'.format(resid_stats['hpd'], resid_stats['rmsd']) hpd = resid_stats['hpd'] rmsd = resid_stats['rmsd'] with open(outfile, 'a') as f: print >> f, ' '.join(str(x) for x in (noise, hpd, rmsd))
def calc_plot_adj(row_clip=2, col_clip=2, ax_clip=None, az_clip=None, bias=None, error=None, plot_file=None, max_iter=0, nnls=False): """ Calculate and plot the displacement, residuals, and coeffs for an input displacement function. Example:: >>> error = displ_sin_ax(n_cycle=1.0, ampl=0.5, phase=0.0) >>> bias = displ_flat(1.0) >>> out = calc_plot_adj(row_clip=2, col_clip=2, ax_clip=75, az_clip=150, bias=bias, error=error, plot_file=None) :param row_clip: Number of actuator rows near edge to ignore :param col_clip: Number of actuator columns near edge to ignore :param ax_clip: Number of pixels near edge to clip in axial direction :param az_clip: Number of pixels near edge to clip in azimuthal direction :param bias: N_AX x N_AZ image of input bias :param error: N_AX x N_AZ image of input figure error :param max_iter: Maximum iterations for removing negative coefficients (default=0) :param plot_file: plot file name (default=None for no saved file) :param nnls: Use SciPy non-negative least squares solver instead of SVD :returns: dict of results """ row_slice = slice(row_clip, -row_clip) if row_clip else slice(None, None) col_slice = slice(col_clip, -col_clip) if col_clip else slice(None, None) ax_slice = slice(ax_clip, -ax_clip) az_slice = slice(az_clip, -az_clip) # Get stddev of input displacement within clipped region displ = bias.vals + error.vals input_stddev = np.std(error.vals[ax_slice, az_slice]) # Get ifuncs and displ that are clipped and sub-sampled (at default of n_ss=5) ifuncs_clip_4d, displ_clip, M_2d = ifunc.clip_ifuncs_displ( ifuncs, displ, row_slice=row_slice, col_slice=col_slice, ax_clip=ax_clip, az_clip=az_clip) ifuncs_clip = ifuncs_clip_4d.reshape(-1, *(ifuncs_clip_4d.shape[-2:])) coeffs_clip = np.zeros(len(ifuncs_clip)) coeffs = ifunc.calc_coeffs(ifuncs_clip, displ_clip, n_ss=None, clip=0, nnls=nnls) pos_vals = np.ones(len(coeffs), dtype=bool) for ii in range(max_iter): pos = coeffs >= 0 pos_idxs = np.flatnonzero(pos) if len(pos_idxs) == len(coeffs): break ifuncs_clip = ifuncs_clip.take(pos_idxs, axis=0) coeffs = ifunc.calc_coeffs(ifuncs_clip, displ_clip, n_ss=None, clip=0) pos_val_idxs = np.array([ii for ii, val in enumerate(pos_vals) if val]) new_neg_idxs = pos_val_idxs[~pos] logger.info('Negative indexes: {}'.format(new_neg_idxs)) pos_vals[pos_val_idxs[~pos]] = False coeffs_clip[np.where(pos_vals)] = coeffs adj = ifunc.calc_adj_displ(ifuncs[row_slice, col_slice, :, :], coeffs_clip) ny, nx = ifuncs.shape[2:4] clipbox_x = [az_clip, nx - az_clip, nx - az_clip, az_clip, az_clip] clipbox_y = [ax_clip, ax_clip, ny - ax_clip, ny - ax_clip, ax_clip] resid = displ - adj resid_clip = resid[ax_slice, az_slice] resid_min, resid_max = np.percentile(resid[ax_slice, az_slice], [0.5, 99.5]) dv = 0.02 resid_stddev = np.std(resid_clip) plt.figure(1, figsize=(14, 7)) plt.clf() plt.subplot(2, 2, 1) ax = plt.gca() ax.axison = False vmin, vmax = np.percentile(bias.vals, [0.5, 99.5]) plt.imshow(bias.vals, vmin=vmin - dv, vmax=vmax + dv) plt.title(bias.title) plt.colorbar(fraction=0.07) plt.subplot(2, 2, 2) ax = plt.gca() ax.axison = False vmin, vmax = np.percentile(error.vals, [0.5, 99.5]) plt.imshow(error.vals, vmin=vmin - dv, vmax=vmax + dv) plt.title(error.title) plt.colorbar(fraction=0.07) plt.subplot(2, 2, 3) plt.imshow(resid, vmin=resid_min, vmax=resid_max) ax = plt.gca() ax.axison = False ax.autoscale(enable=False) plt.title('Resids (min={:.4f} max={:.4f} stddev={:.4f})'.format( np.min(resid_clip), np.max(resid_clip), resid_stddev)) plt.plot(clipbox_x, clipbox_y, '-m') plt.colorbar(fraction=0.07) plt.subplot(2, 2, 4) coeffs_all = np.zeros(ifuncs.shape[:2]) coeffs_2d = coeffs_clip.reshape(ifuncs_clip_4d.shape[:2]) coeffs_all[row_slice, col_slice] = coeffs_2d cimg = np.dstack([coeffs_all, coeffs_all]).reshape(coeffs_all.shape[0], coeffs_all.shape[1] * 2) ax = plt.gca() ax.axison = False plt.imshow(cimg, interpolation='nearest') ax.autoscale(enable=False) coeffs_min, coeffs_max = np.min(coeffs_2d), np.max(coeffs_2d) plt.title('Actuator coeffs (min={:.4f} max={:.4f})'.format( coeffs_min, coeffs_max)) plt.colorbar(fraction=0.07) r, c = np.mgrid[0:coeffs_all.shape[0], 0:coeffs_all.shape[1]] ok = coeffs_all < 0 plt.plot(c[ok] * 2 + 0.5, r[ok], '.r', ms=10) if plot_file: plt.savefig(plot_file) # Make resulting statistics available names = ( 'input_stddev resid_stddev resid_min resid_max coeffs_min coeffs_max ' 'ax_clip az_clip'.split()) _locals = locals() results = {name: _locals[name] for name in names} results['resid_img'] = resid.copy() results['bias_img'] = bias.vals.copy() results['error_img'] = error.vals.copy() results['coeffs_img'] = coeffs_all.copy() for name in names: logger.info('{:12s} : {:.3f}'.format(name, results[name])) return results
def calc_plot_adj(row_clip=2, col_clip=2, ax_clip=None, az_clip=None, bias=None, error=None, plot_file=None, max_iter=0, nnls=False): """ Calculate and plot the displacement, residuals, and coeffs for an input displacement function. Example:: >>> error = displ_sin_ax(n_cycle=1.0, ampl=0.5, phase=0.0) >>> bias = displ_flat(1.0) >>> out = calc_plot_adj(row_clip=2, col_clip=2, ax_clip=75, az_clip=150, bias=bias, error=error, plot_file=None) :param row_clip: Number of actuator rows near edge to ignore :param col_clip: Number of actuator columns near edge to ignore :param ax_clip: Number of pixels near edge to clip in axial direction :param az_clip: Number of pixels near edge to clip in azimuthal direction :param bias: N_AX x N_AZ image of input bias :param error: N_AX x N_AZ image of input figure error :param max_iter: Maximum iterations for removing negative coefficients (default=0) :param plot_file: plot file name (default=None for no saved file) :param nnls: Use SciPy non-negative least squares solver instead of SVD :returns: dict of results """ row_slice = slice(row_clip, -row_clip) if row_clip else slice(None, None) col_slice = slice(col_clip, -col_clip) if col_clip else slice(None, None) ax_slice = slice(ax_clip, -ax_clip) az_slice = slice(az_clip, -az_clip) # Get stddev of input displacement within clipped region displ = bias.vals + error.vals input_stddev = np.std(error.vals[ax_slice, az_slice]) # Get ifuncs and displ that are clipped and sub-sampled (at default of n_ss=5) ifuncs_clip_4d, displ_clip, M_2d = ifunc.clip_ifuncs_displ( ifuncs, displ, row_slice=row_slice, col_slice=col_slice, ax_clip=ax_clip, az_clip=az_clip) ifuncs_clip = ifuncs_clip_4d.reshape(-1, *(ifuncs_clip_4d.shape[-2:])) coeffs_clip = np.zeros(len(ifuncs_clip)) coeffs = ifunc.calc_coeffs(ifuncs_clip, displ_clip, n_ss=None, clip=0, nnls=nnls) pos_vals = np.ones(len(coeffs), dtype=bool) for ii in range(max_iter): pos = coeffs >= 0 pos_idxs = np.flatnonzero(pos) if len(pos_idxs) == len(coeffs): break ifuncs_clip = ifuncs_clip.take(pos_idxs, axis=0) coeffs = ifunc.calc_coeffs(ifuncs_clip, displ_clip, n_ss=None, clip=0) pos_val_idxs = np.array([ii for ii, val in enumerate(pos_vals) if val]) new_neg_idxs = pos_val_idxs[~pos] logger.info('Negative indexes: {}'.format(new_neg_idxs)) pos_vals[pos_val_idxs[~pos]] = False coeffs_clip[np.where(pos_vals)] = coeffs adj = ifunc.calc_adj_displ(ifuncs[row_slice, col_slice, :, :], coeffs_clip) ny, nx = ifuncs.shape[2:4] clipbox_x = [az_clip, nx - az_clip, nx - az_clip, az_clip, az_clip] clipbox_y = [ax_clip, ax_clip, ny - ax_clip, ny - ax_clip, ax_clip] resid = displ - adj resid_clip = resid[ax_slice, az_slice] resid_min, resid_max = np.percentile(resid[ax_slice, az_slice], [0.5, 99.5]) dv = 0.02 resid_stddev = np.std(resid_clip) plt.figure(1, figsize=(14, 7)) plt.clf() plt.subplot(2, 2, 1) ax = plt.gca() ax.axison = False vmin, vmax = np.percentile(bias.vals, [0.5, 99.5]) plt.imshow(bias.vals, vmin=vmin - dv, vmax=vmax + dv) plt.title(bias.title) plt.colorbar(fraction=0.07) plt.subplot(2, 2, 2) ax = plt.gca() ax.axison = False vmin, vmax = np.percentile(error.vals, [0.5, 99.5]) plt.imshow(error.vals, vmin=vmin - dv, vmax=vmax + dv) plt.title(error.title) plt.colorbar(fraction=0.07) plt.subplot(2, 2, 3) plt.imshow(resid, vmin=resid_min, vmax=resid_max) ax = plt.gca() ax.axison = False ax.autoscale(enable=False) plt.title('Resids (min={:.4f} max={:.4f} stddev={:.4f})' .format(np.min(resid_clip), np.max(resid_clip), resid_stddev)) plt.plot(clipbox_x, clipbox_y, '-m') plt.colorbar(fraction=0.07) plt.subplot(2, 2, 4) coeffs_all = np.zeros(ifuncs.shape[:2]) coeffs_2d = coeffs_clip.reshape(ifuncs_clip_4d.shape[:2]) coeffs_all[row_slice, col_slice] = coeffs_2d cimg = np.dstack([coeffs_all, coeffs_all]).reshape(coeffs_all.shape[0], coeffs_all.shape[1] * 2) ax = plt.gca() ax.axison = False plt.imshow(cimg, interpolation='nearest') ax.autoscale(enable=False) coeffs_min, coeffs_max = np.min(coeffs_2d), np.max(coeffs_2d) plt.title('Actuator coeffs (min={:.4f} max={:.4f})' .format(coeffs_min, coeffs_max)) plt.colorbar(fraction=0.07) r, c = np.mgrid[0:coeffs_all.shape[0], 0:coeffs_all.shape[1]] ok = coeffs_all < 0 plt.plot(c[ok] * 2 + 0.5, r[ok], '.r', ms=10) if plot_file: plt.savefig(plot_file) # Make resulting statistics available names = ('input_stddev resid_stddev resid_min resid_max coeffs_min coeffs_max ' 'ax_clip az_clip'.split()) _locals = locals() results = {name: _locals[name] for name in names} results['resid_img'] = resid.copy() results['bias_img'] = bias.vals.copy() results['error_img'] = error.vals.copy() results['coeffs_img'] = coeffs_all.copy() for name in names: logger.info('{:12s} : {:.3f}'.format(name, results[name])) return results