def generate_q_bins(rmax, qmax, pixel_size, distance, wavelength): """ Generate the Q bins at the resolution of the detector Parameters ----------- rmax: float The maximum radial distance on the detector qmax: float The maximum Q on the detector pixel_size: float The size of the pixels, in the same units as rmax distance: float The sample to detector distance, in the same units as rmax wavelength: float The wavelength of the x-rays Returns ------- ndarray: The bin edges, suitable for np.histogram or scipy.stats.binned_statistic """ base_pixels = np.arange(0, rmax, pixel_size) pixel_bottom = base_pixels pixel_top = base_pixels + pixel_size tthb = np.arctan(pixel_bottom / distance) ttht = np.arctan(pixel_top / distance) dq = twotheta_to_q(ttht, wavelength) - twotheta_to_q(tthb, wavelength) fq = np.linspace(0, qmax, len(dq)) b = np.zeros(len(fq) + 1) b[1:] = dq + fq return b
def test_q_twotheta_conversion(): wavelength = 1 q = np.linspace(0, 4 * np.pi, 100) assert_array_almost_equal(q, core.twotheta_to_q( core.q_to_twotheta(q, wavelength), wavelength), decimal=12) two_theta = np.linspace(0, np.pi, 100) assert_array_almost_equal(two_theta, core.q_to_twotheta( core.twotheta_to_q(two_theta, wavelength), wavelength), decimal=12)
def angularIntegrationBackDetector(n_bins=100,center=(265,655),min_bin=40,max_bin=800,threshold=(0.001,10000)): ''' Does the circular integration and q-calibration on the back detector, with an additional threshold masking ''' # -- parameters Ldet = 5080. # detector to sample distance dpix = 0.055 # um (pixel size) energy = 8.4 #keV # -- constants h = 4.135667516*1e-18 #kev*sec c = 3*1e8 #m/s lambda_ = h*c/energy*1e10 # wavelength of the X-rays # -- calulate q-range width,spacing = float(max_bin-min_bin)/float(n_bins),0 edges = roi.ring_edges(min_bin, width, spacing, n_bins) two_theta = utils.radius_to_twotheta(Ldet, edges*dpix) q_val = utils.twotheta_to_q(two_theta, lambda_) q = np.mean(q_val, axis=1) # -- apply threshold mask data self.data[data<threshold[0]]=0 self.data[data>threshold[1]]=0 # -- angular average Iq= circularAverage(self.data,calibrated_center=center,nx=n_bins,min_x=min_bin,max_x=max_bin) print q.shape,Iq[1].shape return Iq[0],Iq[1]
def generate_q_bins(rmax, pixel_size, distance, wavelength, rmin=0): """ Generate the Q bins at the resolution of the detector Parameters ----------- rmax: float The maximum radial distance on the detector in distance units. Note that this should go to the bottom edge of the pixel. pixel_size: float The size of the pixels, in the same units as rmax distance: float The sample to detector distance, in the same units as rmax wavelength: float The wavelength of the x-rays rmin: float, optional The minimum radial distance on the detector in distance units. Defaults to zero. Note that this should be the bottom of the pixel Returns ------- ndarray: The bin edges, suitable for np.histogram or scipy.stats.binned_statistic """ pixel_bottom = np.arange(rmin, rmax, pixel_size) pixel_top = pixel_bottom + pixel_size bottom_tth = np.arctan(pixel_bottom[0] / distance) top_tth = np.arctan(pixel_top / distance) top_q = twotheta_to_q(top_tth, wavelength) bins = np.zeros(len(top_q) + 1) bins[0] = twotheta_to_q(bottom_tth, wavelength) bins[1:] = top_q return bins
def calculate_g2(data,mask=None,g2q=180,center = [265,655]): ''' Calculates the intensity-intensity temporal autocorrelation using scikit-beam packages on the back detector for a q-range (g2q) and width (width, num_rings,spacing) ''' # -- parameters inner_radius = g2q#180 # radius of the first ring width = 1 spacing = 0 num_rings = 10 #center = (273, 723) # center of the speckle pattern dpix = 0.055 # The physical size of the pixels energy = 8.4 #keV h = 4.135667516*1e-18#kev*sec c = 3*1e8 #m/s lambda_ = h*c/energy*1e10 # wavelength of the X-rays Ldet = 5080. # # detector to sample distance # -- average and mask data avg_data = np.average(data,axis=0)#*mask # -- ring array edges = roi.ring_edges(inner_radius, width, spacing, num_rings) # -- convert ring to q two_theta = utils.radius_to_twotheta(Ldet, edges*dpix) q_val = utils.twotheta_to_q(two_theta, lambda_) q_ring = np.mean(q_val, axis=1) # -- ring mask rings = roi.rings(edges, center, data[0].shape) ring_mask = rings*mask # -- calulate g2 num_levels = 1#7 num_bufs = 100#2 g2, lag_steps = corr.multi_tau_auto_corr(num_levels,num_bufs,ring_mask,mask*data) # -- average g2_avg = np.average(g2,axis=1) qt = np.average(q_ring) # -- standard error g2_err = np.std(g2,axis=1)/np.sqrt(len(g2[0,:])) return qt,lag_steps[1:],g2_avg[1:],g2_err[1:]
def angular_integration_back_detector(data, n_bins=100, mask=None, center=(265, 655), min_bin=40, max_bin=800, threshold=(0.001, 10000)): ''' Does the circular integration and q-calibration on the back detector, with an additional threshold masking ''' # -- parameters Ldet = 5080. # detector to sample distance dpix = 0.055 # um (pixel size) energy = 8.4 #keV # -- constants h = 4.135667516 * 1e-18 #kev*sec c = 3 * 1e8 #m/s lambda_ = h * c / energy * 1e10 # wavelength of the X-rays # -- calulate q-range width, spacing = float(max_bin - min_bin) / float(n_bins), 0 edges = roi.ring_edges(min_bin, width, spacing, n_bins) two_theta = utils.radius_to_twotheta(Ldet, edges * dpix) q_val = utils.twotheta_to_q(two_theta, lambda_) q = np.mean(q_val, axis=1) # -- apply threshold mask data data[data < threshold[0]] = 0 data[data > threshold[1]] = 0 # -- angular average Iq = circular_average(data, calibrated_center=center, mask=mask, nx=n_bins, min_x=min_bin, max_x=max_bin) print q.shape, Iq[1].shape return Iq[0], Iq[1]
def calculateG2(self, g2q=180, center=[265,655]): """ -------------------------------------------- Method: SpecklePattern.calculateG2 -------------------------------------------- Calculates the intensity-intensity temporal autocorrelation using scikit-beam packages on the back detector for a q-range (g2q) and width (width, num_rings,spacing) -------------------------------------------- Usage: mySpeckles.calculateG2() -------------------------------------------- Prerequisites: data - data stored as numpy 3D array -------------------------------------------- Arguments: (optional) g2q - at which q (pixels) you wanna calculate g2 center - beam position in the data array (y, x) -------------------------------------------- Returns tuple with: qt - q (in Ang-1) at which you calculated g2 lag_steps[1:] - time array g2_avg[1:] - g2 average array g2_err[1:] - g2 uncertainty array -------------------------------------------- """ # -- parameters inner_radius = g2q#180 # radius of the first ring width = 1 spacing = 0 num_rings = 10 #center = (273, 723) # center of the speckle pattern dpix = 0.055 # The physical size of the pixels energy = 8.4 #keV h = 4.135667516*1e-18#kev*sec c = 3*1e8 #m/s lambda_ = h*c/energy*1e10 # wavelength of the X-rays Ldet = 5080. # # detector to sample distance # -- average and mask data avg_data = np.average(self.data,axis=0)#*mask # -- ring array edges = roi.ring_edges(inner_radius, width, spacing, num_rings) # -- convert ring to q two_theta = utils.radius_to_twotheta(Ldet, edges*dpix) q_val = utils.twotheta_to_q(two_theta, lambda_) q_ring = np.mean(q_val, axis=1) # -- ring mask rings = roi.rings(edges, center, self.data[0].shape) ring_mask = rings*mask # -- calulate g2 num_levels = 1 #7 num_bufs = 100 #2 g2, lag_steps = corr.multi_tau_auto_corr(num_levels,num_bufs,ring_mask,self.mask*self.data) # -- average g2_avg = np.average(g2,axis=1) qt = np.average(q_ring) # -- standard error g2_err = np.std(g2,axis=1)/np.sqrt(len(g2[0,:])) return qt, lag_steps[1:], g2_avg[1:], g2_err[1:]
geo = Geometry( detector='Perkin', pixel1=.0002, pixel2=.0002, dist=.23, poni1=.209, poni2=.207, # rot1=.0128, rot2=-.015, rot3=-5.2e-8, wavelength=1.43e-11 ) r = geo.rArray((2048, 2048)) q = geo.qArray((2048, 2048)) pixels = np.arange(0, np.max(r), geo.pixel1) pixel_bottom = pixels pixel_top = pixels + geo.pixel1 tthb = np.arctan(pixel_bottom / geo.dist) ttht = np.arctan(pixel_top / geo.dist) dq = twotheta_to_q(ttht, .143) - twotheta_to_q(tthb, .143) fq = np.linspace(0, np.max(q), len(dq)) b = np.zeros(len(fq)+1) b[1:] = dq + fq int_q = np.zeros(q.shape, dtype=np.int) for i in range(len(b)-1): t_array = (b[i] <= q) & (q < b[i+1]) int_q[t_array] = i - 1 save_stem = '/mnt/bulk-data/Masters_Thesis/dp/figures/' for j in [100, 300, 500, 1000]: #make some sample data Z = 100*np.cos(50*r)**2 + 150
def calculateG2(self, g2q=180, center=[265, 655]): """ -------------------------------------------- Method: SpecklePattern.calculateG2 -------------------------------------------- Calculates the intensity-intensity temporal autocorrelation using scikit-beam packages on the back detector for a q-range (g2q) and width (width, num_rings,spacing) -------------------------------------------- Usage: mySpeckles.calculateG2() -------------------------------------------- Prerequisites: data - data stored as numpy 3D array -------------------------------------------- Arguments: (optional) g2q - at which q (pixels) you wanna calculate g2 center - beam position in the data array (y, x) -------------------------------------------- Returns tuple with: qt - q (in Ang-1) at which you calculated g2 lag_steps[1:] - time array g2_avg[1:] - g2 average array g2_err[1:] - g2 uncertainty array -------------------------------------------- """ # -- parameters inner_radius = g2q #180 # radius of the first ring width = 1 spacing = 0 num_rings = 10 #center = (273, 723) # center of the speckle pattern dpix = 0.055 # The physical size of the pixels energy = 8.4 #keV h = 4.135667516 * 1e-18 #kev*sec c = 3 * 1e8 #m/s lambda_ = h * c / energy * 1e10 # wavelength of the X-rays Ldet = 5080. # # detector to sample distance # -- average and mask data avg_data = np.average(self.data, axis=0) #*mask # -- ring array edges = roi.ring_edges(inner_radius, width, spacing, num_rings) # -- convert ring to q two_theta = utils.radius_to_twotheta(Ldet, edges * dpix) q_val = utils.twotheta_to_q(two_theta, lambda_) q_ring = np.mean(q_val, axis=1) # -- ring mask rings = roi.rings(edges, center, self.data[0].shape) ring_mask = rings * mask # -- calulate g2 num_levels = 1 #7 num_bufs = 100 #2 g2, lag_steps = corr.multi_tau_auto_corr(num_levels, num_bufs, ring_mask, self.mask * self.data) # -- average g2_avg = np.average(g2, axis=1) qt = np.average(q_ring) # -- standard error g2_err = np.std(g2, axis=1) / np.sqrt(len(g2[0, :])) return qt, lag_steps[1:], g2_avg[1:], g2_err[1:]