def animate(self, qspace=False, logscale=False): """ Generate an animated gif from the wavefront data. """ intensity = self.intensity # Get limits. xmin, xmax, ymax, ymin = self.wavefront.get_limits() mx = intensity.max() mn = intensity.min() if logscale and mn <= 0.0: mn = intensity[numpy.where(intensity > 0.0)].min(), inp_filename = os.path.split(self.input_path)[-1] os.mkdir("tmp") number_of_slices = intensity.shape[-1] # Setup a figure. for i in range(0,number_of_slices): print("Processing slice #%d." % (i)) # Plot profile as 2D colorcoded map. if logscale: plt.imshow(intensity[:,:,i], norm=mpl.colors.LogNorm(vmin=mn, vmax=mx), extent=[xmin*1.e6, xmax*1.e6, ymax*1.e6, ymin*1.e6], cmap="viridis") else: plt.imshow(intensity[:,:,i], norm=mpl.colors.Normalize(vmin=mn, vmax=mx), extent=[xmin*1.e6, xmax*1.e6, ymax*1.e6, ymin*1.e6], cmap="viridis") plt.savefig("tmp/%s_%07d.png" % (inp_filename, i) ) plt.clf() os.system("convert -delay 10 tmp/*.png %s.gif" % (inp_filename) ) shutil.rmtree("tmp")
def plotImage(pattern, logscale=False, offset=1e-1): """ Workhorse function to plot an image :param logscale: Whether to show the data on logarithmic scale (z axis) (default False). :type logscale: bool :param offset: Offset to apply if logarithmic scaling is on. :type offset: float """ plt.figure() # Get limits. mn, mx = pattern.min(), pattern.max() x_range, y_range = pattern.shape if logscale: if mn <= 0.0: mn += pattern.min() + offset pattern = pattern.astype(float) + mn plt.imshow(pattern, norm=mpl.colors.LogNorm(vmin=mn, vmax=mx), cmap="viridis") else: plt.imshow(pattern, norm=Normalize(vmin=mn, vmax=mx), cmap='viridis') plt.xlabel(r'$x$ (pixel)') plt.ylabel(r'$y$ (pixel)') plt.xlim([0, x_range - 1]) plt.ylim([0, y_range - 1]) plt.tight_layout() plt.colorbar()
def plotImage(pattern, logscale=False, offset=1e-1, symlog=False, *argv, **kwargs): """ Workhorse function to plot an image :param logscale: Whether to show the data on logarithmic scale (z axis) (default False). :type logscale: bool :param offset: Offset to apply if logarithmic scaling is on. :type offset: float :param symlog: If logscale is True, to show the data on symlogarithmic scale (z axis) (default False). :type symlog: bool :return: the handles of figure and axis :rtype: figure,axis """ fig, ax = plt.subplots() # Get limits. mn, mx = pattern.min(), pattern.max() x_range, y_range = pattern.shape if logscale: if mn <= 0.0: mn = pattern.min() + offset mx = pattern.max() + offset pattern = pattern.astype(float) + offset # default plot setup kwargs['cmap'] = kwargs.pop('cmap', "viridis") if symlog: kwargs['norm'] = kwargs.pop( 'norm', mpl.colors.SymLogNorm(0.015, vmin=mn, vmax=mx)) else: kwargs['norm'] = kwargs.pop('norm', mpl.colors.LogNorm(vmin=mn, vmax=mx)) axes = kwargs.pop('axes', None) plt.imshow(pattern, *argv, **kwargs) else: kwargs['norm'] = kwargs.pop('norm', Normalize(vmin=mn, vmax=mx)) kwargs['cmap'] = kwargs.pop('cmap', "viridis") plt.imshow(pattern, *argv, **kwargs) plt.xlabel(r'$x$ (pixel)') plt.ylabel(r'$y$ (pixel)') plt.xlim([0, x_range - 1]) plt.ylim([0, y_range - 1]) plt.tight_layout() plt.colorbar() return fig, ax
def plotImage(pattern, logscale=False, offset=None, symlog=False, *argv, **kwargs): """ Workhorse function to plot an image :param logscale: Whether to show the data on logarithmic scale (z axis) (default False). :type logscale: bool :param offset: Offset to apply to the pattern. :type offset: float :param symlog: To show the data on symlogarithmic scale (z axis) (default False). :type symlog: bool """ fig, ax = plt.subplots() # Get limits. mn, mx = pattern.min(), pattern.max() x_range, y_range = pattern.shape if offset: mn = pattern.min() + offset mx = pattern.max() + offset pattern = pattern.astype(float) + offset if (logscale and symlog): print('logscale and symlog are both true.\noverrided by logscale') # default plot setup if (logscale or symlog): kwargs['cmap'] = kwargs.pop('cmap', "viridis") if logscale: kwargs['norm'] = kwargs.pop('norm', mpl.colors.LogNorm()) elif symlog: kwargs['norm'] = kwargs.pop('norm', mpl.colors.SymLogNorm(0.015)) axes = kwargs.pop('axes', None) plt.imshow(pattern, *argv, **kwargs) else: kwargs['norm'] = kwargs.pop('norm', Normalize(vmin=mn, vmax=mx)) kwargs['cmap'] = kwargs.pop('cmap', "viridis") plt.imshow(pattern, *argv, **kwargs) plt.xlabel(r'$x$ (pixel)') plt.ylabel(r'$y$ (pixel)') plt.xlim([0, x_range - 1]) plt.ylim([0, y_range - 1]) plt.tight_layout() plt.colorbar()
def shannonPixelPhoton(self, resolution): """ Get the average number of photons per shannon pixel :param resolution: The full periodic resolution (A) for shannon pixels :type resolution: float """ # Extract parameters. beam = self.parameters['beam'] geom = self.parameters['geom'] # Photon energy and wavelength E0 = beam['photonEnergy'] lmd = 1239.8 / E0 # Pixel dimension apix = geom['pixelWidth'] # Sample-detector distance Ddet = geom['detectorDist'] # Number of pixels in each dimension Npix = geom['mask'].shape[0] # Find center. center = 0.5 * (Npix - 1) # Max. scattering angle. theta_max = math.atan(center * apix / Ddet) # Min resolution. d_min = 0.5 * lmd / math.sin(theta_max / 2.0) # Next integer resolution. d0 = 0.1 * math.ceil(d_min * 10.0) # 10 powers to get Angstrom ds = resolution / 10 # nm # Pixel numbers corresponding to resolution rings. N = Ddet / apix * numpy.tan(numpy.arcsin(lmd / 2. / ds) * 2) pi = self.patterns_iterator stack = numpy.array([p for p in pi]) y, x = numpy.indices(stack[0].shape) r = numpy.sqrt((x - center)**2 + (y - center)**2) mask = (abs(r - N) <= 0.5) for i in range(len(stack)): stack[i] *= mask plt.figure() plt.imshow(stack[0]) plt.title('Frame 0') a = mask[mask == True] nShannonPixel = len(a) # Mean number of expected photons per Shannon pixel photons = numpy.sum(stack, axis=(1, 2)) / nShannonPixel avg_photons = numpy.mean(photons) rms_photons = numpy.std(photons) print("*************************") print("nShannonPixel = %i" % (nShannonPixel)) print("avg = %6.5e" % (avg_photons)) print("std = %6.5e" % (rms_photons)) print("*************************")