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 animatePatterns(self, output_path=None, logscale=False, offset=1e-1): """ Make an animated gif out of the given patterns. :param output_path: Where to save the animated git. :type output_path: str :raises IOError: File exists or parent directory not found. :param logscale: Whether to apply logarithmic scaling to the z axis (color). :type logscale: bool :param offset: Offset to apply if logarithmic scaling is on. :type offset: float """ # Handle default path for saving the animated gif. if output_path is None: output_path = os.path.join(os.getcwd(), "animated_patterns.gif") if not isinstance(output_path, str): raise TypeError( 'The parameter "output_path" must be a str, not %s.' % (type(output_path))) parent_dir = os.path.dirname(os.path.abspath(output_path)) if not os.path.isdir(parent_dir): raise IOError('%s does not exist.' % (parent_dir)) if os.path.isfile(output_path): raise IOError( '%s already exists, cowardly refusing to overwrite.' % (output_path)) self.__animation_output_path = os.path.abspath(output_path) # Make tempdir. tmp_out_dir = tempfile.mkdtemp() stack = numpy.array([p for p in self.patternGenerator()]) mn, mx = stack.min(), stack.max() x_range, y_range = stack.shape[1:] for i, img in enumerate(stack): plotImage(img, logscale=logscale, offset=offset) # Save image. if self.pattern_indices != "all": png_filename = "%07d.png" % (self.pattern_indices[i]) else: png_filename = "%07d.png" % (i) plt.savefig(os.path.join(tmp_out_dir, png_filename)) # Clear figure. plt.clf() # Render the animated gif. os.system("convert -delay 100 %s %s" % (os.path.join(tmp_out_dir, "*.png"), output_path))