def test2Dfit(fitType = None, verbose = False): testDebug = 0 testOptimizer = 'levmar' roi = [1, 325, 1, 335] conIm = PrincetonSPEFile('testimage.spe')[0].astype(float32) conZ = array(meshgrid(arange(roi[0], roi[0] + roi[1]), arange(roi[2], roi[2] + roi[3]))) if fitType == 'twodgauss': guess = array([ conIm.max(), 162.5, 5, 167.5, 6]) func = [twodgauss] elif fitType == 'twodgausslin': guess = array([ 0.01*conIm.max(), 1e-8, 1e-8, conIm.max(), 162.5, 5, 167.5, 6]) func = [twodlin, twodgauss] times = [] for optimizer in ['leastsq', 'levmar', 'mpfit']: f = fit.fit(x = conZ, y = conIm, funcs = func , guess = guess, debug = testDebug, optimizer = optimizer) t1 = time.time() f.go() t2 = time.time() times.append('Optimizer %s took %0.3f s' % (optimizer, (t2-t1))) for t in times: print t
def test2Dfit(fitType=None, verbose=False): testDebug = 0 testOptimizer = 'levmar' roi = [1, 325, 1, 335] conIm = PrincetonSPEFile('testimage.spe')[0].astype(float32) conZ = array( meshgrid(arange(roi[0], roi[0] + roi[1]), arange(roi[2], roi[2] + roi[3]))) if fitType == 'twodgauss': guess = array([conIm.max(), 162.5, 5, 167.5, 6]) func = [twodgauss] elif fitType == 'twodgausslin': guess = array( [0.01 * conIm.max(), 1e-8, 1e-8, conIm.max(), 162.5, 5, 167.5, 6]) func = [twodlin, twodgauss] times = [] for optimizer in ['leastsq', 'levmar', 'mpfit']: f = fit.fit(x=conZ, y=conIm, funcs=func, guess=guess, debug=testDebug, optimizer=optimizer) t1 = time.time() f.go() t2 = time.time() times.append('Optimizer %s took %0.3f s' % (optimizer, (t2 - t1))) for t in times: print t
def fittest(): data = loadtxt('testdata.dat') times = [] for optimizer in ['leastsq', 'levmar', 'mpfit']: f = fit.fit(x = data[:,0], y = data[:,1], funcs = [fitfuncs.linear, fitfuncs.gauss], optimizer = optimizer) t1 = time.time() f.go() t2 = time.time() times.append('Optimizer %s took %0.3f s' % (optimizer, (t2-t1))) for t in times: print t
def fittest(): data = loadtxt('testdata.dat') times = [] for optimizer in ['leastsq', 'levmar', 'mpfit']: f = fit.fit(x=data[:, 0], y=data[:, 1], funcs=[fitfuncs.linear, fitfuncs.gauss], optimizer=optimizer) t1 = time.time() f.go() t2 = time.time() times.append('Optimizer %s took %0.3f s' % (optimizer, (t2 - t1))) for t in times: print t
def peak_analysis(img, x, y, dxy=40, interpolation_points=80, show_plots=False, center_on_max=True, calculate_width=False): x = int(x + 0.5) y = int(y + 0.5) sub_img = img[y - dxy:y + dxy, x - dxy:x + dxy].copy() params = peak_params() # calculate COM COM = scipy.ndimage.measurements.center_of_mass(sub_img) max_pos = scipy.ndimage.measurements.maximum_position(sub_img) #TODO: check if we want to use the max pixel or the COM for the width measurements if (center_on_max): used_peak_pos_small = np.array((max_pos[1], max_pos[0])) used_peak_pos_large = np.array( (x - dxy + max_pos[1], y - dxy + max_pos[0])) else: used_peak_pos_small = np.array((COM[1], COM[0])) used_peak_pos_large = np.array((x - dxy + COM[1], y - dxy + COM[0])) params.COM = np.array((x - dxy + COM[1], y - dxy + COM[0])) params.max_pos = np.array((x - dxy + max_pos[1], y - dxy + max_pos[0])) r_x_para = 1024 - used_peak_pos_large[0] r_y_para = 1024 - used_peak_pos_large[1] r_mag = np.sqrt(r_x_para**2 + r_y_para**2) r_x_para /= r_mag r_y_para /= r_mag if (r_x_para == 0 or r_y_para == 0): r_x_perp = r_y_para r_y_perp = r_x_para else: r_x_perp = 1. / r_x_para r_y_perp = -1. / r_y_para r_mag = np.sqrt(r_x_perp**2 + r_y_perp**2) r_x_perp /= r_mag r_y_perp /= r_mag # subtract background sub_img_bg = img[y - 2 * dxy:y + 2 * dxy, x - 2 * dxy:x + 2 * dxy].copy() xi = np.arange(4 * dxy) yi = np.arange(4 * dxy) func_bg = scipy.interpolate.RectBivariateSpline(xi, yi, sub_img_bg) d_vals = np.arange(4 * dxy) - 4 * dxy / 2. f_bg1 = np.zeros(len(d_vals)) f_bg2 = np.zeros(len(d_vals)) f_bg = np.zeros(len(d_vals)) p_1_x = np.sqrt(2) * dxy * r_x_perp + 2 * dxy p_1_y = np.sqrt(2) * dxy * r_y_perp + 2 * dxy p_2_x = -np.sqrt(2) * dxy * r_x_perp + 2 * dxy p_2_y = -np.sqrt(2) * dxy * r_y_perp + 2 * dxy line_p1_x = [] line_p1_y = [] line_p2_x = [] line_p2_y = [] for i in xrange(len(d_vals)): f_bg1[i] = func_bg(p_1_x + d_vals[i] * r_x_para, p_1_y + d_vals[i] * r_y_para) f_bg2[i] = func_bg(p_2_x + d_vals[i] * r_x_para, p_2_y + d_vals[i] * r_y_para) f_bg[i] = (f_bg1[i] + f_bg2[i]) / 2. #f_bg[i] /= 2. line_p1_x.append(p_1_x + d_vals[i] * r_x_para) line_p1_y.append(p_1_y + d_vals[i] * r_y_para) line_p2_x.append(p_2_x + d_vals[i] * r_x_para) line_p2_y.append(p_2_y + d_vals[i] * r_y_para) if (show_plots): plt.figure() plt.plot(f_bg1) plt.plot(f_bg2) fig = plt.figure() plt.title('Image with Background') ax = fig.add_subplot(111) im = ax.imshow(sub_img, interpolation='None') ax.plot([used_peak_pos_small[0]], [used_peak_pos_small[1]], 'wd') ax.format_coord = Imshow_Formatter(im) plt.show() dummy, bg_arr = np.meshgrid(f_bg, f_bg) bg_arr_rot = scipy.ndimage.interpolation.rotate( bg_arr, deg(np.arctan2(r_x_para, r_y_para)), reshape=False) bg_arr_rot_cut = bg_arr_rot[dxy:3 * dxy, dxy:3 * dxy] sub_img -= bg_arr_rot_cut params.max = scipy.ndimage.measurements.maximum(sub_img) params.integrated_intensity = np.sum(sub_img) if (show_plots): fig = plt.figure() plt.title('Background subtracted') ax = fig.add_subplot(111) im = ax.imshow(sub_img, interpolation='None') ax.plot([used_peak_pos_small[0]], [used_peak_pos_small[1]], 'wd') ax.format_coord = Imshow_Formatter(im) fig = plt.figure() ax = fig.add_subplot(111) im = ax.imshow(sub_img_bg, interpolation='None') ax.plot(line_p1_x, line_p1_y, 'w') ax.plot(line_p2_x, line_p2_y, 'w') ax.format_coord = Imshow_Formatter(im) # calculate peak width if (calculate_width): xi = np.arange(2 * dxy) yi = np.arange(2 * dxy) func = scipy.interpolate.RectBivariateSpline(xi, yi, sub_img) d_vals = np.arange(interpolation_points) - int( interpolation_points / 2.) d_x_para = np.zeros(len(d_vals)) d_y_para = np.zeros(len(d_vals)) d_ints_para = np.zeros(len(d_vals)) d_x_perp = np.zeros(len(d_vals)) d_y_perp = np.zeros(len(d_vals)) d_ints_perp = np.zeros(len(d_vals)) for i in xrange(len(d_vals)): d_x_para[i] = used_peak_pos_small[0] + d_vals[i] * r_x_para d_y_para[i] = used_peak_pos_small[1] + d_vals[i] * r_y_para d_ints_para[i] = func( used_peak_pos_small[0] + d_vals[i] * r_x_para, used_peak_pos_small[1] + d_vals[i] * r_y_para) d_x_perp[i] = used_peak_pos_small[0] + d_vals[i] * r_x_perp d_y_perp[i] = used_peak_pos_small[1] + d_vals[i] * r_y_perp d_ints_perp[i] = func( used_peak_pos_small[0] + d_vals[i] * r_x_perp, used_peak_pos_small[1] + d_vals[i] * r_y_perp) try: df_para = fit.fit( x=d_vals, y=d_ints_para, funcs=[pyspec.fitfuncs.gauss, pyspec.fitfuncs.linear]) df_para.run() df_perp = fit.fit( x=d_vals, y=d_ints_perp, funcs=[pyspec.fitfuncs.gauss, pyspec.fitfuncs.linear]) df_perp.run() params.FWHM_para = 2. * np.sqrt( 2. * np.log(2.)) * df_para.result[1] params.FWHM_perp = 2. * np.sqrt( 2. * np.log(2.)) * df_perp.result[1] if (show_plots): fig = plt.figure() ax = fig.add_subplot(111) im = ax.imshow(sub_img, interpolation='None') ax.plot([used_peak_pos_small[0]], [used_peak_pos_small[1]], 'wd') ax.plot(d_x_para, d_y_para, 'r-') ax.plot(d_x_perp, d_y_perp, 'b-') ax.format_coord = Imshow_Formatter(im) plt.figure() plt.plot(d_vals, d_ints_para, 'r-') plt.plot(d_vals, d_ints_perp, 'b-') plt.plot( d_vals, pyspec.fitfuncs.gauss(d_vals, df_para.result[0:3]) + pyspec.fitfuncs.linear(d_vals, df_para.result[3:]), 'r--') plt.plot( d_vals, pyspec.fitfuncs.gauss(d_vals, df_perp.result[0:3]) + pyspec.fitfuncs.linear(d_vals, df_perp.result[3:]), 'b--') except: return params return params
if (do_plot): plt.figure() plt.imshow(img) # Fit L cuts L_cut_half_width = 10 L_cut_img = img[:, COM[1] - L_cut_half_width:COM[1] + L_cut_half_width + 1] #img[: , 372:408] L_cut = np.sum(L_cut_img, axis=1) L_cut[255:261] = (L_cut[253] + L_cut[254] + L_cut[261] + L_cut[262]) / 4. # remove border between ccd panels L_cut[338] = (L_cut[337] + L_cut[339]) / 2. # remove dead pixel df = fit.fit(x=np.arange(516), y=L_cut, xlimits=[66, 515], funcs=[pyspec.fitfuncs.pvoight, pyspec.fitfuncs.constant]) df.run() _x = np.arange(800) _y = pyspec.fitfuncs.pvoight( _x, df.result[0:4]) + pyspec.fitfuncs.constant(_x, df.result[4:]) if (do_plot): plt.figure() plt.plot(L_cut) plt.plot(_x, _y) L_params = df.result print df.result