def get_min_gradient(image, cen, mask=None, start_exc=25): """Return the radial coordinate with the largest negative gradient. Needed for center refinement with sector or ring methods. Arguments: `image`: Image to use. `cen`: Tuple with the center coordinates. `mask`: Mask giving the pixels to be used in determination. `start_exc`: Number of points to exclude from the start (center) of the image radial gradient array """ [I, n1] = c.radbin(image, cen, mask=mask) r = np.arange(len(I)) yerr = np.sqrt(I) nzerr = yerr.copy() zrepl = np.abs(np.median(yerr)) if zrepl == 0.0: zrepl = 1.0 # give up nzerr[nzerr <= 0.0] = zrepl sp = ip.UnivariateSpline(r, I, w=1/nzerr) # smoo = [ sp.derivatives(x)[0] for x in r ] grad = [ sp.derivatives(x)[1] for x in r ] gradmin = np.argmin(grad[start_exc:]) + start_exc # plt.plot(r, smoo, r, I, r, grad) # print("Min gradient: %d" % gradmin) return gradmin
def ofun_sectors(cen): """Calculate two chi2s from two opposing sectors and multiply""" sect_cens = [0, 0.5*np.pi, np.pi, 1.5*np.pi] seclims = [ np.array([a - hwidth, a + hwidth])+np.pi/4.0 for a in sect_cens ] rints = [ c.radbin(image, cen, radrange=np.linspace(rstart,rstop), phirange=x, mask=mask)[0] for x in seclims ] chi2_1 = chivectors(rints[0], rints[2]) chi2_2 = chivectors(rints[1], rints[3]) return chi2_1*chi2_2
def ofun_ringvariance(cen): """Calculate STD from counts in a (narrow) ring in the image""" [r, n] = c.radbin(image, cen, \ radrange=np.array([ringradius, ringradius+ringwidth]), \ phirange=np.linspace(0, 2*np.pi, \ np.floor(2*np.pi*ringradius)), mask=mask) r = r[r != 0] w = np.std(r) return w
def ofun_ring(cen): """Return width of a Debye ring with the given center""" [r, n] = c.radbin(image, cen, peakrange, mask=mask) w = fwhm(x, r) # 2nd moment is not very stable # w = secondmoment(x, r) # print cen, w # sys.stdout.flush() return w
def ofun_cylsymm(cen): """Calculate STD from counts in a (narrow) ring in the image""" R = 100 [r, n] = c.radbin(image, cen, radrange=np.array([R,R+2]), phirange=np.linspace(0, 2*np.pi, np.floor(2*np.pi*R)), mask=mask) r = r[r != 0] w = np.std(r) # plt.hold(0) # plt.plot(r) # plt.show() # plt.waitforbuttonpress() # print cen, w # sys.stdout.flush() return w
def coordinate_test(): m = np.zeros((9,9)) m[0,4] = 1 m[8,4]= 1 m[4,0] = 1 m[4,8]= 1 (I,n) = r.radbin(m, (4.5, 4.5), radrange=np.arange(0,10), norm=0) # plt.plot(I) # plt.show() # plt.waitforbuttonpress() # print(I[4]) assert(I[4] == 4.0) m = np.zeros((10,5)) m[0,3] = 1 (I,n) = r.radbin(m, (3.5, 5.5), radrange=np.arange(0,10), norm=0) # plt.plot(I) # plt.show() assert(I[5] == 1.0) I[5] = 0.0 assert(not I.all()) (I,n) = r.radbin(m, (3.5, 0.5), radrange=np.arange(0,10), norm=0) assert(I[0] == 1.0) I[0] = 0.0 assert(not I.all())
def ofun_sectors(cen): """Calculate two chi2s from two opposing sectors and multiply""" rstart = 120 rstop = 220 hwidth = 10 * (np.pi/180) # width in rads sect_cens = [0, 0.5*np.pi, np.pi, 1.5*np.pi] seclims = [ np.array([a - hwidth, a + hwidth])+np.pi/4.0 for a in sect_cens ] rints = [ c.radbin(image, cen, radrange=np.linspace(rstart,rstop), phirange=x, mask=mask)[0] for x in seclims ] chi2_1 = chivectors(rints[0], rints[2]) chi2_2 = chivectors(rints[1], rints[3]) # print(cen, chi2_1, chi2_2) # sys.stdout.flush() # plt.hold(0) # plt.plot(rints[0], 'b') # plt.hold(1) # plt.plot(rints[2], 'b') # plt.plot(rints[1], 'r') # plt.plot(rints[3], 'r') # plt.waitforbuttonpress() return chi2_1*chi2_2
def centerfit_peakwidth(image, startcen=None, peakrange=None, baseline=None, mask=None, plotit=False): """Return center from an image with a ring. Minimizes the width of the peak obtained from radial averaging in the given range as a function of the center position.""" if peakrange is None: raise ValueError("peakrange must be supplied") if startcen is None: startcen = get_im_max(image) [r1, n1] = c.radbin(image, startcen, peakrange, mask=mask) if baseline == None: baseline = r1.min() x = np.arange(peakrange.size - 1) if(plotit): plt.hold(0) plt.plot(x, r1) plt.show() # plt.waitforbuttonpress() def ofun_ring(cen): """Return width of a Debye ring with the given center""" [r, n] = c.radbin(image, cen, peakrange, mask=mask) w = fwhm(x, r) # 2nd moment is not very stable # w = secondmoment(x, r) # print cen, w # sys.stdout.flush() return w def ofun_cylsymm(cen): """Calculate STD from counts in a (narrow) ring in the image""" R = 100 [r, n] = c.radbin(image, cen, radrange=np.array([R,R+2]), phirange=np.linspace(0, 2*np.pi, np.floor(2*np.pi*R)), mask=mask) r = r[r != 0] w = np.std(r) # plt.hold(0) # plt.plot(r) # plt.show() # plt.waitforbuttonpress() # print cen, w # sys.stdout.flush() return w def ofun_sectors(cen): """Calculate two chi2s from two opposing sectors and multiply""" rstart = 120 rstop = 220 hwidth = 10 * (np.pi/180) # width in rads sect_cens = [0, 0.5*np.pi, np.pi, 1.5*np.pi] seclims = [ np.array([a - hwidth, a + hwidth])+np.pi/4.0 for a in sect_cens ] rints = [ c.radbin(image, cen, radrange=np.linspace(rstart,rstop), phirange=x, mask=mask)[0] for x in seclims ] chi2_1 = chivectors(rints[0], rints[2]) chi2_2 = chivectors(rints[1], rints[3]) # print(cen, chi2_1, chi2_2) # sys.stdout.flush() # plt.hold(0) # plt.plot(rints[0], 'b') # plt.hold(1) # plt.plot(rints[2], 'b') # plt.plot(rints[1], 'r') # plt.plot(rints[3], 'r') # plt.waitforbuttonpress() return chi2_1*chi2_2 optcen = optim.fmin(ofun_sectors, startcen, disp=True) [r2, n2] = c.radbin(image, optcen, peakrange, mask=mask) if(plotit): plt.plot(x, r1, x, r2) plt.show() return optcen