def get_absolute_qs(self,line,verbosity=3): """ OLD! determine two points on y-axis with known q-distance (low-loss w-q reference with central spot and bragg spot) line ... 1D array with N-points containing two peaks """ x = np.arange(N,dtype='float'); ref=gaussfilt1D(line, 5); peaks=[]; for i in range(2): # fit 2 peaks imax = np.argmax(ref); # initial guess for peak p, pconv = \ opt.curve_fit(models.gauss,x,ref,p0=(imax, np.sum(ref[imax-5:imax+5]), 10)); peaks.append(p); # gauss fit imin = max(p[0]-5*p[2],0); imax = min(p[0]+5*p[2],N); ref[imin:imax]=0; # remove peak from line (5*fwhm around x0) if verbosity>2: plt.figure(); plt.title("DEBUG: Fit q-reference"); plt.plot(x,line,'k'); plt.plot(x,models.gauss(x,*peaks[0]),'r'); plt.plot(x,models.gauss(x,*peaks[1]),'g'); return peaks[0][0], peaks[1][0];
def get_absolute_qs(self, line, verbosity=3): """ OLD! determine two points on y-axis with known q-distance (low-loss w-q reference with central spot and bragg spot) line ... 1D array with N-points containing two peaks """ x = np.arange(N, dtype='float') ref = gaussfilt1D(line, 5) peaks = [] for i in range(2): # fit 2 peaks imax = np.argmax(ref) # initial guess for peak p, pconv = \ opt.curve_fit(models.gauss,x,ref,p0=(imax, np.sum(ref[imax-5:imax+5]), 10)) peaks.append(p) # gauss fit imin = max(p[0] - 5 * p[2], 0) imax = min(p[0] + 5 * p[2], N) ref[imin:imax] = 0 # remove peak from line (5*fwhm around x0) if verbosity > 2: plt.figure() plt.title("DEBUG: Fit q-reference") plt.plot(x, line, 'k') plt.plot(x, models.gauss(x, *peaks[0]), 'r') plt.plot(x, models.gauss(x, *peaks[1]), 'g') return peaks[0][0], peaks[1][0]
def fit_aperture_borders(self,order=2,log=False,**kwargs): """ Determine the left and right border of the aperture as polynom x(y) for the series of E-q maps. order ... (opt) order of the polynomial to fit log ... (opt) consider aperture images on log-scale for further options, see fit_border.get_border_points() RETURNS list of tuples(left,right) containing polynomials x = left(y) """ self.ap_order =order; self.ap_log =log; # illumination reference (smoothed version) if self.ref_name is not None: ref = np.abs(gaussfilt1D(self.ref_img,11)); # gauss-filter #ref = np.abs(lorentzfit1D(self.ref_img,offset=offset)); # fit lorentz ref[ref<np.mean(ref)] = np.mean(ref); # constant for low values # to avoid 0 devision if self.verbosity>9: self.__dbg_fig.append(self.plot_reference(ref)); # draw figure and save in list else: ref = 1; # no reference given # iterate over all aperture images points = []; fit = []; lines=[]; stack=[]; info=[]; for i,image in enumerate(self.ap_stack): # correct image by illu_ref filtimg = image/ref; if log: filtimg = np.log(np.abs(filtimg)+1); # get aperture border as point list in px (correct for binning!) c = self.crop; l,r = fit_border.get_border_points(filtimg, interp_out=True, xmin=int(c['xmin']/self.xbin), xmax=int(c['xmax']/self.xbin), ymin=int(c['ymin']/self.ybin), ymax=int(c['ymax']/self.ybin), verbosity=self.verbosity-10, **kwargs); l = ((l+0.5).T*(self.xbin,self.ybin)).T; # convert to px, points (x,y) r = ((r+0.5).T*(self.xbin,self.ybin)).T; points.append([l,r]); # fit polynom x=p(y) to left and right border polyl = np.poly1d(np.polyfit(l[1],l[0],order)); polyr = np.poly1d(np.polyfit(r[1],r[0],order)); fit.append((polyl,polyr)); # DEBUG: drawing fit points and polynoms if self.verbosity>2: y = np.arange(self.crop['ymin'],self.crop['ymax']); stack.append(filtimg); info.append({'desc': 'DEBUG: '+self.ap_names[i], 'xperchan':self.xbin, 'yperchan':self.ybin}); p1 = plt.Line2D(l[0],l[1],marker='x',ls='',c='b'); p2 = plt.Line2D(r[0],r[1],marker='x',ls='',c='b') p3 = plt.Line2D(polyl(y),y,color='r'); p4 = plt.Line2D(polyr(y),y,color='r'); lines.append([p1,p2,p3,p4]); if self.verbosity>2: self.__dbg_fig.append( self.plot_aperture_images(stack,info,lines) ); # store results self.ap_points=points; # contains Nap (l,r) tuples; l,r are a lists of (x,y) points self.ap_poly =fit; # contains Nap (pl,pr) tuples; pl,pr are polynoms y(x) # comments self.history.append("Fit aperture borders"); self.history.append("|- order=%d, log=%s"%(order,log)); params = ", ".join([key+": "+str(val) for key,val in kwargs.items()]); self.history.append("|- " + params); return fit;
def fit_aperture_borders(self, order=2, log=False, **kwargs): """ Determine the left and right border of the aperture as polynom x(y) for the series of E-q maps. order ... (opt) order of the polynomial to fit log ... (opt) consider aperture images on log-scale for further options, see fit_border.get_border_points() RETURNS list of tuples(left,right) containing polynomials x = left(y) """ self.ap_order = order self.ap_log = log # illumination reference (smoothed version) if self.ref_name is not None: ref = np.abs(gaussfilt1D(self.ref_img, 11)) # gauss-filter #ref = np.abs(lorentzfit1D(self.ref_img,offset=offset)); # fit lorentz ref[ref < np.mean(ref)] = np.mean(ref) # constant for low values # to avoid 0 devision if self.verbosity > 9: self.__dbg_fig.append(self.plot_reference(ref)) # draw figure and save in list else: ref = 1 # no reference given # iterate over all aperture images points = [] fit = [] lines = [] stack = [] info = [] for i, image in enumerate(self.ap_stack): # correct image by illu_ref filtimg = image / ref if log: filtimg = np.log(np.abs(filtimg) + 1) # get aperture border as point list in px (correct for binning!) c = self.crop l, r = fit_border.get_border_points( filtimg, interp_out=True, xmin=int(c['xmin'] / self.xbin), xmax=int(c['xmax'] / self.xbin), ymin=int(c['ymin'] / self.ybin), ymax=int(c['ymax'] / self.ybin), verbosity=self.verbosity - 10, **kwargs) l = ((l + 0.5).T * (self.xbin, self.ybin)).T # convert to px, points (x,y) r = ((r + 0.5).T * (self.xbin, self.ybin)).T points.append([l, r]) # fit polynom x=p(y) to left and right border polyl = np.poly1d(np.polyfit(l[1], l[0], order)) polyr = np.poly1d(np.polyfit(r[1], r[0], order)) fit.append((polyl, polyr)) # DEBUG: drawing fit points and polynoms if self.verbosity > 2: y = np.arange(self.crop['ymin'], self.crop['ymax']) stack.append(filtimg) info.append({ 'desc': 'DEBUG: ' + self.ap_names[i], 'xperchan': self.xbin, 'yperchan': self.ybin }) p1 = plt.Line2D(l[0], l[1], marker='x', ls='', c='b') p2 = plt.Line2D(r[0], r[1], marker='x', ls='', c='b') p3 = plt.Line2D(polyl(y), y, color='r') p4 = plt.Line2D(polyr(y), y, color='r') lines.append([p1, p2, p3, p4]) if self.verbosity > 2: self.__dbg_fig.append(self.plot_aperture_images( stack, info, lines)) # store results self.ap_points = points # contains Nap (l,r) tuples; l,r are a lists of (x,y) points self.ap_poly = fit # contains Nap (pl,pr) tuples; pl,pr are polynoms y(x) # comments self.history.append("Fit aperture borders") self.history.append("|- order=%d, log=%s" % (order, log)) params = ", ".join( [key + ": " + str(val) for key, val in kwargs.items()]) self.history.append("|- " + params) return fit