def fitted_flat_field(q, FoV, ff_samples, it_no, data_dir, verbose=False): ''' Saves out data for the fitted flat-field at sample points across the focal plane. Parameters ---------- q : numpy array The fitted flat-field parameters FoV : float array The imagers field of view in degrees (dalpha, dbeta) it_no : int The self-calibration iteration number data_dir: string The directory name in which the output data should be stored verbose : boolean True to run function in verbose mode ''' filename = '{0}/FF/{1}_ff.p'.format(data_dir, it_no) if verbose: print("Saving out fitted flat-field data to {0}...".format(filename)) x = np.linspace(-FoV[0] / 2, FoV[0] / 2, ff_samples[0]) y = np.linspace(-FoV[1] / 2, FoV[1] / 2, ff_samples[1]) X, Y = np.meshgrid(x, y) z = self_calibration.evaluate_flat_field(X.flatten(), Y.flatten(), q) fitted_ff = np.reshape(z, (len(X[:, 0]), len(X[0, :]))) dic = {'x': X, 'y': Y, 'fitted_ff': fitted_ff, 'iteration_number': it_no} pickle.dump(dic, open(filename, "wb")) if verbose: print("...done!")
def best_in_basis(q, FoV, ff_samples, best_fit_params, verbose=False): ''' Calculates the 'best-in-basis badness' of the instrument response fit. This is defined as the root-mean-square difference between the fitted instrument response and the *best fit possible* with the model basis used for the fit (on a regular grid of sample points across the focal plane). Parameters ---------- q : numpy array the fitted instrument response parameters FoV : Float Array The simulate imager's field-of-view in degrees [alpha, beta] ff_samples : integer array The number of sample points on the focal plane for the best-in-basis fitting and all of the badness measures verbose : Boolean Set to true to run simulations in verbose mode Returns ------- out : float the calculated best-in-basis badness ''' if verbose: print("Calculating best-in-basis badness of fitted flat-field...") x = np.linspace(- FoV[0] / 2, FoV[0] / 2, ff_samples[0]) y = np.linspace(- FoV[1] / 2, FoV[1] / 2, ff_samples[1]) X, Y = np.meshgrid(x, y) our_ff = self_cal.evaluate_flat_field(x.flatten(), y.flatten(), q) bf_ff = self_cal.evaluate_flat_field(x.flatten(), y.flatten(), best_fit_params) best_in_basis_badness = \ 100. * np.sqrt(np.mean(((our_ff - bf_ff) / bf_ff) ** 2)) if verbose: print("...done!") return best_in_basis_badness
def badness(q, FoV, ff_samples, verbose=False): ''' Calculates the 'badness' of the instrument response fit. The 'badness' is defined as the root-mean-square difference between the true instrument response and the fitted instrument response measured on a regular grid of sample points across the focal plane. Parameters ---------- q : numpy array The fitted instrument response parameters FoV : float array The imagers field-of-view in degrees (dalpha, dbeta) ff_samples : float array The number of focal plane sample points (alpha-axis, beta-axis) Returns ------- out : float the calculated badness ''' if verbose: print("Calculating best-in-basis badness of fitted flat-field...") x = np.linspace(-FoV[0] / 2, FoV[0] / 2, ff_samples[0]) y = np.linspace(-FoV[1] / 2, FoV[1] / 2, ff_samples[1]) X, Y = np.meshgrid(x, y) our_ff = self_cal.evaluate_flat_field(x.flatten(), y.flatten(), q) true_ff = true_functions.flat_field(x.flatten(), y.flatten(), FoV) badness = 100. * np.sqrt(np.mean(((our_ff - true_ff) / true_ff) ** 2)) if verbose: print("...done!") return badness
def flat_fields(filename, FoV, ff_samples, best_fit_params, output_path=False, fig_width=8.3, suffix='.png', verbose=False): ''' This function plots the fitted flat-field against the *true* (top) and the best-in-basis (bottom) flat-fields. Left: contour plots to compare flat-fields, Right: residuals between two flat-fields. Input ----- filename : string The path to the survey files FoV : float array The size of the imagers field-of-view in degrees (dalpha, dbeta) ff_samples : float array The number of points at which the instrument responses are sampled in the (x-direction, y-direction) The imager's field-of-view in degrees (dalpha, dbeta) best_fit_params : numpy array Array of the best-in-basis parameters output_path : string The path to save the figure to. If no value is given, figure is saved in same directory as source pickle figure_width : float The width of the figure in inches, default it 8.3 suffix : string The format of the saved figure, either '.pdf', '.eps' or '.png'. Default is '.png' verbose : Boolean Set to true to run function in verbose mode ''' if verbose: print("Plotting flat-field from {0}...".format(filename)) scale = 0.88 dic = pickle.load(open(filename)) fig = plt.figure(figsize=(fig_width, scale * fig_width)) fig.clf() ax_cb = fig.add_axes([0.85, 0.075 / scale, 0.05, 0.75 / scale]) ax1 = fig.add_axes([0.075, 0.075 / scale, 0.375, 0.375 / scale]) ax2 = fig.add_axes([0.075, 0.45 / scale, 0.375, 0.375 / scale]) ax3 = fig.add_axes([0.45, 0.075 / scale, 0.375, 0.375 / scale]) ax4 = fig.add_axes([0.45, 0.45 / scale, 0.375, 0.375 / scale]) ax1.text(0.95, 0.96, '(c)', va='center', ha='center', bbox=dict(facecolor='w', edgecolor='w', alpha=0.7), zorder=2000, transform=ax1.transAxes) ax2.text(0.95, 0.04, '(a)', va='center', ha='center', bbox=dict(facecolor='w', edgecolor='w', alpha=0.7), zorder=2000, transform=ax2.transAxes) ax3.text(0.05, 0.96, '(d)', va='center', ha='center', zorder=2000, transform=ax3.transAxes) ax4.text(0.05, 0.04, '(b)', va='center', ha='center', zorder=2000, transform=ax4.transAxes) ax2.set_xticklabels([]) ax4.set_xticklabels([]) ax4.set_yticklabels([]) ax3.set_yticklabels([]) ax_cb.set_xticks([]) fig.text(0.45, 0.025 / scale, r'Focal Plane Position $x$ (deg)', va='center', ha='center') fig.text(0.015, 0.45 / scale, r'Focal Plane Position $y$ (deg)', va='center', ha='center', rotation='vertical') true_ff = true_functions.flat_field(dic['x'], dic['y'], FoV) best_ff = self_calibration.evaluate_flat_field( dic['x'].flatten(), dic['y'].flatten(), best_fit_params).reshape(ff_samples) levels = np.linspace(np.min(true_ff), np.max(true_ff), 25) labels = levels[::2] cs = ax2.contour(dic['x'], dic['y'], true_ff, colors='0.75', levels=levels) cs = ax1.contour(dic['x'], dic['y'], best_ff, colors='0.75', levels=levels) cs = ax1.contour(dic['x'], dic['y'], dic['fitted_ff'], colors='k', levels=levels) cs.clabel(labels, fontsize=8) cs = ax2.contour(dic['x'], dic['y'], dic['fitted_ff'], colors='k', levels=levels) cs.clabel(labels, fontsize=8) true_residual = 100 * (dic['fitted_ff'] - true_ff) / true_ff best_residual = 100 * (dic['fitted_ff'] - best_ff) / best_ff fp = (-FoV[0] / 2, FoV[0] / 2, -FoV[1] / 2, FoV[1] / 2) a = ax4.imshow(true_residual, extent=fp, vmin=-2., vmax=2., cmap='gray', aspect='auto') ax3.imshow(best_residual, extent=fp, vmin=-2., vmax=2., cmap='gray', aspect='auto') cbar = fig.colorbar(a, ax_cb, orientation='vertical') cbar.set_label(r'Residuals (percent)') if output_path: filename = output_path + suffix else: filename = string.replace(filename, '.p', suffix) fig.savefig(filename) fig.clf() if verbose: print('...done!')
def flat_fields(filename, FoV, ff_samples, best_fit_params, output_path=False, fig_width=8.3, suffix='.png', verbose=False): ''' This function plots the fitted flat-field against the *true* (top) and the best-in-basis (bottom) flat-fields. Left: contour plots to compare flat-fields, Right: residuals between two flat-fields. Input ----- filename : string The path to the survey files FoV : float array The size of the imagers field-of-view in degrees (dalpha, dbeta) ff_samples : float array The number of points at which the instrument responses are sampled in the (x-direction, y-direction) The imager's field-of-view in degrees (dalpha, dbeta) best_fit_params : numpy array Array of the best-in-basis parameters output_path : string The path to save the figure to. If no value is given, figure is saved in same directory as source pickle figure_width : float The width of the figure in inches, default it 8.3 suffix : string The format of the saved figure, either '.pdf', '.eps' or '.png'. Default is '.png' verbose : Boolean Set to true to run function in verbose mode ''' if verbose: print("Plotting flat-field from {0}...".format(filename)) scale = 0.88 dic = pickle.load(open(filename)) fig = plt.figure(figsize=(fig_width, scale * fig_width)) fig.clf() ax_cb = fig.add_axes([0.85, 0.075 / scale, 0.05, 0.75 / scale]) ax1 = fig.add_axes([0.075, 0.075 / scale, 0.375, 0.375 / scale]) ax2 = fig.add_axes([0.075, 0.45 / scale, 0.375, 0.375 / scale]) ax3 = fig.add_axes([0.45, 0.075 / scale, 0.375, 0.375 / scale]) ax4 = fig.add_axes([0.45, 0.45 / scale, 0.375, 0.375 / scale]) ax1.text(0.95, 0.96, '(c)', va='center', ha='center', bbox=dict(facecolor='w', edgecolor='w', alpha=0.7), zorder=2000, transform=ax1.transAxes) ax2.text(0.95, 0.04, '(a)', va='center', ha='center', bbox=dict(facecolor='w', edgecolor='w', alpha=0.7), zorder=2000, transform=ax2.transAxes) ax3.text(0.05, 0.96, '(d)', va='center', ha='center', zorder=2000, transform=ax3.transAxes) ax4.text(0.05, 0.04, '(b)', va='center', ha='center', zorder=2000, transform=ax4.transAxes) ax2.set_xticklabels([]) ax4.set_xticklabels([]) ax4.set_yticklabels([]) ax3.set_yticklabels([]) ax_cb.set_xticks([]) fig.text(0.45, 0.025 / scale, r'Focal Plane Position $x$ (deg)', va='center', ha='center') fig.text(0.015, 0.45 / scale, r'Focal Plane Position $y$ (deg)', va='center', ha='center', rotation='vertical') true_ff = true_functions.flat_field(dic['x'], dic['y'], FoV) best_ff = self_calibration.evaluate_flat_field(dic['x'].flatten(), dic['y'].flatten(), best_fit_params).reshape(ff_samples) levels = np.linspace(np.min(true_ff), np.max(true_ff), 25) labels = levels[::2] cs = ax2.contour(dic['x'], dic['y'], true_ff, colors='0.75', levels=levels) cs = ax1.contour(dic['x'], dic['y'], best_ff, colors='0.75', levels=levels) cs = ax1.contour(dic['x'], dic['y'], dic['fitted_ff'], colors='k', levels=levels) cs.clabel(labels, fontsize=8) cs = ax2.contour(dic['x'], dic['y'], dic['fitted_ff'], colors='k', levels=levels) cs.clabel(labels, fontsize=8) true_residual = 100 * (dic['fitted_ff'] - true_ff) / true_ff best_residual = 100 * (dic['fitted_ff'] - best_ff) / best_ff fp = (-FoV[0] / 2, FoV[0] / 2, -FoV[1] / 2, FoV[1] / 2) a = ax4.imshow(true_residual, extent=fp, vmin=-2., vmax=2., cmap='gray', aspect='auto') ax3.imshow(best_residual, extent=fp, vmin=-2., vmax=2., cmap='gray', aspect='auto') cbar = fig.colorbar(a, ax_cb, orientation='vertical') cbar.set_label(r'Residuals (percent)') if output_path: filename = output_path + suffix else: filename = string.replace(filename, '.p', suffix) fig.savefig(filename) fig.clf() if verbose: print('...done!')