def convertImage(self, thumbpath, plotspath, thumbscale):
        os.chdir(thumbpath)  # cd into thumbnails dir
        if (thumbpath == plotspath):
            print(
                "Image/thumbnail paths match: NOT removing current images...")
        else:
            # print("Image/thumbnail paths DO NOT match: removing previous images...")
            thmfiles = glob.glob(thumbpath + "*")
            for f in thmfiles:
                os.remove(f)  # rm tmp thumbnails from Thumbnails dir

        # read from output plots dir
        imgfiles = glob.glob(plotspath + "*")
        #print("thumbscale = " + str(thumbscale))
        cnt = 0

        for filespec in imgfiles:
            # get the original file name
            tmp = re.split('/', filespec)
            orig_filename = tmp[len(tmp) - 1].strip()

            # the filename format is station_name.png - parse
            # the station_name
            tmp = re.split('\.', orig_filename)
            station_name = tmp[0].strip()  # pull station name

            # make the new spec by appending _24hr.png to
            # the station_name
            new_name = station_name + "_24hr.png"

            # create the thumbnail version of the original image
            img.thumbnail(filespec, new_name, scale=thumbscale)
            cnt = cnt + 1

        print("thumbnails generated: " + str(cnt) + "\n")
Esempio n. 2
0
 def make_population_map_file(self):
     if not os.path.exists(workspace_path(scenario_filename())):
         os.makedirs(workspace_path(scenario_filename()))
     print("Calculating a new Population Map")
     fig = population_results_map()
     FigureCanvas(fig).print_png(self.path)
     thumbnail(self.path, self.thumb_path, scale=0.1923)
     print("Finished Population Map")
Esempio n. 3
0
def gen_gallery(app, doctree):
    if app.builder.name != "html":
        return
    outdir = app.builder.outdir
    rootdir = "plot_directive/mpl_examples"
    skips = set(["mathtext_examples", "matshow_02", "matshow_03", "matplotlib_icon"])
    print
    print "generating gallery: ",
    data = []
    for subdir in ("api", "pylab_examples", "mplot3d", "widgets", "axes_grid"):
        origdir = os.path.join("build", rootdir, subdir)
        thumbdir = os.path.join(outdir, rootdir, subdir, "thumbnails")
        if not os.path.exists(thumbdir):
            os.makedirs(thumbdir)
        print subdir,
        for filename in sorted(glob.glob(os.path.join(origdir, "*.png"))):
            if filename.endswith("hires.png"):
                continue
            path, filename = os.path.split(filename)
            basename, ext = os.path.splitext(filename)
            if basename in skips:
                sys.stdout.write("[skipping %s]" % basename)
                sys.stdout.flush()
                continue
            orig_path = str(os.path.join(origdir, filename))
            thumb_path = str(os.path.join(thumbdir, filename))
            if out_of_date(orig_path, thumb_path):
                image.thumbnail(orig_path, thumb_path, scale=0.3)
            m = multiimage.match(basename)
            if m is None:
                pyfile = "%s.py" % basename
            else:
                basename = m.group(1)
                pyfile = "%s.py" % basename
            data.append((subdir, basename, os.path.join(rootdir, subdir, "thumbnails", filename)))
            sys.stdout.write(".")
            sys.stdout.flush()
        print
    link_template = """\
    <a href="%s"><img src="%s" border="0" alt="%s"/></a>
    """
    if len(data) == 0:
        warnings.warn("No thumbnails were found")
    rows = []
    for (subdir, basename, thumbfile) in data:
        if thumbfile is not None:
            link = "examples/%s/%s.html" % (subdir, basename)
            rows.append(link_template % (link, thumbfile, basename))
    fh = file(os.path.join(app.builder.srcdir, "_templates", "gallery.html"), "w")
    fh.write(template % "\n".join(rows))
    fh.close()
Esempio n. 4
0
def population_thumbnail_png(request, second_try=False):
    path = workspace_path(scenario_filename() + '/population_map.png')
    thumb_path = workspace_path(scenario_filename() + '/population_thumbnail.png')
    try:
        with open(thumb_path, "rb") as f:
            return HttpResponse(f.read(), content_type="image/png")
    except IOError:
        if os.path.exists(path):
            if second_try:
               sleep(1) 
            thumbnail(path, thumb_path, scale=0.1923)  # create the thumbnail
            return population_thumbnail_png(request, second_try=True)
        else:
            sleep(5)
            return population_thumbnail_png(request, second_try=False)
Esempio n. 5
0
def generate_thumbnails(thumbnails_dir, images_dir, scale=0.3):
    """
    Generate thumbnails into `thumbnails_dir` corresponding to images in
    `images_dir`.
    """
    ensure_path(thumbnails_dir + os.path.sep)

    output('generating thumbnails...')
    filenames = glob.glob(os.path.join(images_dir, '*.png'))
    for fig_filename in filenames:
        ebase = fig_filename.replace(sfepy.data_dir, '').lstrip(os.path.sep)
        output('"%s"' % ebase)

        base = os.path.basename(fig_filename)
        thumb_filename = os.path.join(thumbnails_dir, base)

        image.thumbnail(fig_filename, thumb_filename, scale=scale)

    output('...done')
Esempio n. 6
0
def generate_thumbnails(thumbnails_dir, images_dir, scale=0.3):
    """
    Generate thumbnails into `thumbnails_dir` corresponding to images in
    `images_dir`.
    """
    ensure_path(thumbnails_dir + os.path.sep)

    output('generating thumbnails...')
    filenames = glob.glob(os.path.join(images_dir, '*.png'))
    for fig_filename in filenames:
        ebase = fig_filename.replace(sfepy.data_dir, '').lstrip(os.path.sep)
        output('"%s"' % ebase)

        base = os.path.basename(fig_filename)
        thumb_filename = os.path.join(thumbnails_dir, base)

        image.thumbnail(fig_filename, thumb_filename, scale=scale)

    output('...done')
 def _gen_thumbnail(preview_fqn, thumb_fqn):
     logging.debug(f'Generating thumbnail for file {preview_fqn}.')
     count = 0
     if os.path.exists(preview_fqn):
         thumb = image.thumbnail(preview_fqn, thumb_fqn, scale=0.25)
         if thumb is not None:
             count = 1
     else:
         logging.warning(f'Could not find {preview_fqn} for thumbnail '
                         f'generation.')
     return count
Esempio n. 8
0
    def decorated(*args, **kwargs):
        ba = args[0]
        if "add_title" not in kwargs:
            kwargs["add_title"] = ""
        add_title = kwargs["add_title"]
        if ("fig_folder" in kwargs) and (kwargs["fig_folder"] is not None):
            kwargs["fig_folder"] = kwargs["fig_folder"] / add_title
        else:
            kwargs["fig_folder"] = None
        if len(add_title) > 0:
            add_title = f": {add_title}"

        if not (("fax" in kwargs) and (kwargs["fax"] is not None)):
            fax = plt.subplots(1, 1, figsize=(PAGE_WIDTH, ROW_HEIGHT))
            kwargs["fax"] = fax

        f, ax = func(*args, **kwargs)

        # Post process plot
        ncol = 1
        if len(ax.lines) / 2 > 10:
            ncol = 2
        ax.legend(loc=6, ncol=ncol)
        ax.xaxis.set_major_formatter(mdates.DateFormatter("%b-%y"))
        ax.set_title(ba + add_title)
        ax.set_ylabel(f"{kwargs['unit']}")
        add_watermark(ax)
        f.tight_layout()

        if kwargs["fig_folder"] is not None:
            fig_folder = kwargs["fig_folder"]
            fig_folder.mkdir(exist_ok=True, parents=True)
            f.savefig(fig_folder / f"{ba}.png")
            f.savefig(fig_folder / f"{ba}.pdf")
            image.thumbnail(fig_folder / f"{ba}.png",
                            fig_folder / f"{ba}-thumbnail.png",
                            scale=0.1)
            plt.close(f)
 def _gen_thumbnail(self):
     self._logger.debug(f'Generating thumbnail for file '
                        f'{self._science_fqn}.')
     count = 0
     if os.path.exists(self._preview_fqn):
         thumb = image.thumbnail(self._preview_fqn,
                                 self._thumb_fqn,
                                 scale=0.25)
         if thumb is not None:
             count = 1
     else:
         self._logger.warning(f'Could not find {self._preview_fqn} for '
                              f'thumbnail generation.')
     return count
	def convertImage(self, thumbpath, plotspath, thumbscale):
		os.chdir(thumbpath)	# cd into thumbnails dir
		if (thumbpath == plotspath):
			print "Image/thumbnail paths match: NOT removing current images..."
		else:
			print "Image/thumbnail paths DO NOT match: removing previous images..."
			thmfiles = glob.glob(thumbpath+"*")
			for f in thmfiles:
				os.remove(f)	# rm tmp thumbnails from Thumbnails dir

		# read from output plots dir
		imgfiles = glob.glob(plotspath+"*")
		print "thumbscale = " + str(thumbscale) 
		cnt = 0	
		for f in imgfiles:
			tmp = re.split('/', f)
			tmplen = len(tmp)
			fname = tmp[tmplen-1].strip()	# pull image name
			tmp = re.split('\.', fname)
			fout = tmp[1].strip()	# pull station name
			fout = fout + "_24hr.png"	# append png
			img.thumbnail(f, fout, scale=thumbscale)
			cnt = cnt + 1
		print "num thumbnails = " + str(cnt) + "\n"
Esempio n. 11
0
 def _gen_thumbnail(self):
     self._logger.debug(f'Generating thumbnail for file '
                        f'{self._science_fqn}.')
     count = 0
     if os.path.exists(self._preview_fqn):
         thumb = image.thumbnail(self._preview_fqn, self._thumb_fqn,
                                 scale=0.25)
         if thumb is not None:
             self.add_preview(self._storage_name.thumb_uri,
                              self._storage_name.thumb,
                              ProductType.THUMBNAIL)
             self.add_to_delete(self._thumb_fqn)
             count = 1
     else:
         self._logger.warning(f'Could not find {self._preview_fqn} for '
                              f'thumbnail generation.')
     return count
Esempio n. 12
0
def write_example(src_name, src_dir, rst_dir, cfg):
    """Write rst file from a given python example.

    Parameters
    ----------
    src_name : str
        Name of example file.
    src_dir : 'str'
        Source directory for python examples.
    rst_dir : 'str'
        Destination directory for rst files generated from python examples.
    cfg : config object
        Sphinx config object created by Sphinx.
    """
    last_dir = src_dir.psplit()[-1]
    # to avoid leading . in file names, and wrong names in links
    if last_dir == '.' or last_dir == 'examples':
        last_dir = Path('')
    else:
        last_dir += '_'

    src_path = src_dir.pjoin(src_name)
    example_file = rst_dir.pjoin(src_name)
    shutil.copyfile(src_path, example_file)

    image_dir = rst_dir.pjoin('images')
    thumb_dir = image_dir.pjoin('thumb')
    image_dir.makedirs()
    thumb_dir.makedirs()

    base_image_name = os.path.splitext(src_name)[0]
    image_path = image_dir.pjoin(base_image_name + '_{0}.png')

    basename, py_ext = os.path.splitext(src_name)
    rst_path = rst_dir.pjoin(basename + cfg.source_suffix)

    if _plots_are_current(src_path, image_path) and rst_path.exists:
        return

    flags = cfg.plot2rst_flags.copy()
    blocks, new_flags = split_code_and_text_blocks(example_file)
    flags.update(new_flags)

    while True:
        head = blocks[0][2]
        if head.startswith('#!') or head.startswith(FLAG_PREFIX):
            blocks.pop(0) # don't add shebangs or flags to rst file.
        else:
            break

    # Note that `process_blocks` executes the source, so plots are now 'active'
    figure_list, rst = process_blocks(blocks, src_path, image_path, cfg)

    rst_link = '.. _example_%s:\n\n' % (last_dir + src_name)
    example_rst = ''.join([rst_link, rst])

    has_inline_plots = any(cfg.plot2rst_plot_tag in b[2] for b in blocks)
    if not has_inline_plots and flags['auto_plots']:
        # Show all plots at the end of the example
        if len(plt.get_fignums()) > 0:
            figure_list = save_all_figures(image_path)
            img_blocks = [IMAGE_TEMPLATE % f.lstrip('/') for f in figure_list]
            example_rst += ''.join(img_blocks)
    plt.close('all')

    example_rst += CODE_LINK.format(src_name)

    f = open(rst_path,'w')
    f.write(example_rst)
    f.flush()

    thumb_path = thumb_dir.pjoin(src_name[:-3] + '.png')
    if figure_list:
        first_image_file = image_dir.pjoin(figure_list[0].lstrip('/'))
        if first_image_file.exists:
            thumb_scale = cfg.plot2rst_thumb_scale
            image.thumbnail(first_image_file, thumb_path, thumb_scale)

    if not thumb_path.exists:
        if cfg.plot2rst_default_thumb is None:
            print "WARNING: No plots found and default thumbnail not defined."
            print "Specify 'plot2rst_default_thumb' in Sphinx config file."
        else:
            shutil.copy(cfg.plot2rst_default_thumb, thumb_path)
Esempio n. 13
0
===============
Image Thumbnail
===============

You can use Matplotlib to generate thumbnails from existing images.
Matplotlib relies on Pillow_ for reading images, and thus supports all formats
supported by Pillow.

.. _Pillow: https://python-pillow.org/
"""

from argparse import ArgumentParser
from pathlib import Path
import sys
import matplotlib.image as image

parser = ArgumentParser(
    description="Build thumbnails of all images in a directory.")
parser.add_argument("imagedir", type=Path)
args = parser.parse_args()
if not args.imagedir.isdir():
    sys.exit(f"Could not find input directory {args.imagedir}")

outdir = Path("thumbs")
outdir.mkdir(parents=True, exist_ok=True)

for path in args.imagedir.glob("*.png"):
    outpath = outdir / path.name
    fig = image.thumbnail(path, outpath, scale=0.15)
    print(f"saved thumbnail of {path} to {outpath}")
Esempio n. 14
0
def gen_gallery(app, doctree):
    if app.builder.name != 'html':
        return

    outdir = app.builder.outdir
    rootdir = 'plot_directive/mpl_examples'

    # images we want to skip for the gallery because they are an unusual
    # size that doesn't layout well in a table, or because they may be
    # redundant with other images or uninteresting
    skips = set([
        'mathtext_examples',
        'matshow_02',
        'matshow_03',
        'matplotlib_icon',
    ])

    print
    print "generating gallery: ",
    data = []
    for subdir in ('api', 'pylab_examples', 'mplot3d', 'widgets', 'axes_grid'):
        origdir = os.path.join('build', rootdir, subdir)
        thumbdir = os.path.join(outdir, rootdir, subdir, 'thumbnails')
        if not os.path.exists(thumbdir):
            os.makedirs(thumbdir)
        print subdir,

        for filename in sorted(glob.glob(os.path.join(origdir, '*.png'))):
            if filename.endswith("hires.png"):
                continue

            path, filename = os.path.split(filename)
            basename, ext = os.path.splitext(filename)
            if basename in skips:
                sys.stdout.write('[skipping %s]' % basename)
                sys.stdout.flush()
                continue

            # Create thumbnails based on images in tmpdir, and place
            # them within the build tree
            image.thumbnail(str(os.path.join(origdir, filename)),
                            str(os.path.join(thumbdir, filename)),
                            scale=0.3)

            m = multiimage.match(basename)
            if m is None:
                pyfile = '%s.py' % basename
            else:
                basename = m.group(1)
                pyfile = '%s.py' % basename

            data.append((subdir, basename,
                         os.path.join(rootdir, subdir, 'thumbnails',
                                      filename)))

            sys.stdout.write(".")
            sys.stdout.flush()
        print

    link_template = """\
    <a href="%s"><img src="%s" border="0" alt="%s"/></a>
    """

    if len(data) == 0:
        warnings.warn("No thumbnails were found")

    rows = []
    for (subdir, basename, thumbfile) in data:
        if thumbfile is not None:
            link = 'examples/%s/%s.html' % (subdir, basename)
            rows.append(link_template % (link, thumbfile, basename))

    fh = file(os.path.join(app.builder.srcdir, '_templates', 'gallery.html'),
              'w')
    fh.write(template % '\n'.join(rows))
    fh.close()
y = open('number_labels.txt', 'x')
y.close()
y = open('number_labels.txt', 'w')

# Create a list to store all of the xs in (will convert to np array)
X_list = []

# Loop through each folder of numbers
for folder in range(6):
    # Loop through each image in that folder
    for filename in os.listdir('Numbers/%s' % folder):
        if filename.endswith('.png'):
            # Open the image, make it grayscale
            image = Image.open('Numbers/%d/%s' %
                               (folder, filename)).convert(mode='L')
            image.thumbnail((30, 30))  # Make the image smaller
            data = asarray(image)  # Turn the image into a matrix
            data = data.ravel()  # Unroll the image into a one-row vector

            X_list.append(data)

            y.write(str(folder))  # Write the number into a .txt file of ys
            y.write('\n')

X = np.array(X_list)  # Turn the X_list into a np matrix
print(X.shape)

savetxt(x, X.astype(int), fmt='%i')  # Save the matrix as integers

x.close()
y.close()
Esempio n. 16
0
def generate_file_rst(fname, target_dir, src_dir, plot_gallery):
    """ Generate the rst file for a given example.
    """
    base_image_name = os.path.splitext(fname)[0]
    image_fname = "%s_%%s.png" % base_image_name

    this_template = rst_template
    last_dir = os.path.split(src_dir)[-1]
    # to avoid leading . in file names, and wrong names in links
    if last_dir == "." or last_dir == "examples":
        last_dir = ""
    else:
        last_dir += "_"
    short_fname = last_dir + fname
    src_file = os.path.join(src_dir, fname)
    example_file = os.path.join(target_dir, fname)
    shutil.copyfile(src_file, example_file)

    # The following is a list containing all the figure names
    figure_list = []

    image_dir = os.path.join(target_dir, "images")
    thumb_dir = os.path.join(image_dir, "thumb")
    if not os.path.exists(image_dir):
        os.makedirs(image_dir)
    if not os.path.exists(thumb_dir):
        os.makedirs(thumb_dir)
    image_path = os.path.join(image_dir, image_fname)
    stdout_path = os.path.join(image_dir, "stdout_%s.txt" % base_image_name)
    time_path = os.path.join(image_dir, "time_%s.txt" % base_image_name)
    thumb_file = os.path.join(thumb_dir, fname[:-3] + ".png")
    time_elapsed = 0
    if plot_gallery and fname.startswith("plot"):
        # generate the plot as png image if file name
        # starts with plot and if it is more recent than an
        # existing image.
        first_image_file = image_path % 1
        if os.path.exists(stdout_path):
            stdout = open(stdout_path).read()
        else:
            stdout = ""
        if os.path.exists(time_path):
            time_elapsed = float(open(time_path).read())

        if not os.path.exists(first_image_file) or os.stat(first_image_file).st_mtime <= os.stat(src_file).st_mtime:
            # We need to execute the code
            print "plotting %s" % fname
            t0 = time()
            import matplotlib.pyplot as plt

            plt.close("all")
            cwd = os.getcwd()
            try:
                # First CD in the original example dir, so that any file
                # created by the example get created in this directory
                orig_stdout = sys.stdout
                os.chdir(os.path.dirname(src_file))
                my_buffer = StringIO()
                my_stdout = Tee(sys.stdout, my_buffer)
                sys.stdout = my_stdout
                my_globals = {"pl": plt}
                execfile(os.path.basename(src_file), my_globals)
                time_elapsed = time() - t0
                sys.stdout = orig_stdout
                my_stdout = my_buffer.getvalue()
                if "__doc__" in my_globals:
                    # The __doc__ is often printed in the example, we
                    # don't with to echo it
                    my_stdout = my_stdout.replace(my_globals["__doc__"], "")
                my_stdout = my_stdout.strip()
                if my_stdout:
                    stdout = "**Script output**::\n\n  %s\n\n" % ("\n  ".join(my_stdout.split("\n")))
                open(stdout_path, "w").write(stdout)
                open(time_path, "w").write("%f" % time_elapsed)
                os.chdir(cwd)

                # In order to save every figure we have two solutions :
                # * iterate from 1 to infinity and call plt.fignum_exists(n)
                #   (this requires the figures to be numbered
                #    incrementally: 1, 2, 3 and not 1, 2, 5)
                # * iterate over [fig_mngr.num for fig_mngr in
                #   matplotlib._pylab_helpers.Gcf.get_all_fig_managers()]
                for fig_num in (fig_mngr.num for fig_mngr in matplotlib._pylab_helpers.Gcf.get_all_fig_managers()):
                    # Set the fig_num figure as the current figure as we can't
                    # save a figure that's not the current figure.
                    plt.figure(fig_num)
                    plt.savefig(image_path % fig_num)
                    figure_list.append(image_fname % fig_num)
            except:
                print 80 * "_"
                print "%s is not compiling:" % fname
                traceback.print_exc()
                print 80 * "_"
            finally:
                os.chdir(cwd)
                sys.stdout = orig_stdout

            print " - time elapsed : %.2g sec" % time_elapsed
        else:
            figure_list = [f[len(image_dir) :] for f in glob.glob(image_path % "[1-9]")]
            # for f in glob.glob(image_path % '*')]

        # generate thumb file
        this_template = plot_rst_template
        from matplotlib import image

        if os.path.exists(first_image_file):
            image.thumbnail(first_image_file, thumb_file, 0.2)

    if not os.path.exists(thumb_file):
        # create something not to replace the thumbnail
        shutil.copy("images/blank_image.png", thumb_file)

    docstring, short_desc, end_row = extract_docstring(example_file)

    # Depending on whether we have one or more figures, we're using a
    # horizontal list or a single rst call to 'image'.
    if len(figure_list) == 1:
        figure_name = figure_list[0]
        image_list = SINGLE_IMAGE % figure_name.lstrip("/")
    else:
        image_list = HLIST_HEADER
        for figure_name in figure_list:
            image_list += HLIST_IMAGE_TEMPLATE % figure_name.lstrip("/")

    f = open(os.path.join(target_dir, fname[:-2] + "rst"), "w")
    f.write(this_template % locals())
    f.flush()
Esempio n. 17
0
# default value of colormap is used. 
lum = im[:, :, 0] 
# show image 
plt.imshow(lum) 


# importing pyplot and image from matplotlib 
import matplotlib.pyplot as plt 
import matplotlib.image as img 
  
# reading png image 
im = img.imread(r"C:\Users\fluXcapacit0r\Documents\Python\MITRC\javk.jpg") 
lum = im[:, :, 0] 
  
# setting colormap as hot 
plt.imshow(lum, cmap ='hot') 
plt.colorbar() 

############################################
# importing PIL and matplotlib 
from PIL import Image 
import matplotlib.pyplot as plt 

# reading png image file 
img = Image.open(r"C:\Users\fluXcapacit0r\Documents\Python\MITRC\javk.jpg") 

# resizing the image 
img.thumbnail((50, 50), Image.ANTIALIAS) 
imgplot = plt.imshow(img) 

Esempio n. 18
0
"""
You can use matplotlib to generate thumbnails from existing images.
matplotlib natively supports PNG files on the input side, and other
image types transparently if your have PIL installed
"""

# build thumbnails of all images in a directory
import sys, os, glob
import matplotlib.image as image


if len(sys.argv)!=2:
    print 'Usage: python %s IMAGEDIR'%__file__
    raise SystemExit
indir = sys.argv[1]
if not os.path.isdir(indir):
    print 'Could not find input directory "%s"'%indir
    raise SystemExit

outdir = 'thumbs'
if not os.path.exists(outdir):
    os.makedirs(outdir)

for fname in glob.glob(os.path.join(indir, '*.png')):
    basedir, basename = os.path.split(fname)
    outfile = os.path.join(outdir, basename)
    fig = image.thumbnail(fname, outfile, scale=0.15)
    print 'saved thumbnail of %s to %s'%(fname, outfile)

Esempio n. 19
0
def get_aou_logo_b64():
    logo_path = os.path.join(achilles_images_path, consts.AOU_LOGO_PNG)
    thumbnail_obj = BytesIO()
    mpimg.thumbnail(logo_path, thumbnail_obj, scale=0.15)
    logo_b64 = base64.b64encode(thumbnail_obj.getvalue()).decode()
    return logo_b64
Esempio n. 20
0
def gen_gallery(app, doctree):
    if app.builder.name != 'html':
        return

    outdir = app.builder.outdir

    rootdir = os.path.join('plot_directive', 'example_code')

    # images we want to skip for the gallery because they are an unusual
    # size that doesn't layout well in a table, or because they may be
    # redundant with other images or uninteresting
    skips = set([
        'mathtext_examples',
        'matshow_02',
        'matshow_03',
        'matplotlib_icon',
    ])

    data = []
    thumbnails = {}

    for subdir in ('graphics', ):
        origdir = os.path.join(os.path.dirname(outdir), rootdir, subdir)
        thumbdir = os.path.join(outdir, rootdir, subdir, 'thumbnails')
        if not os.path.exists(thumbdir):
            os.makedirs(thumbdir)
        for filename in sorted(glob.glob(os.path.join(origdir, '*.png'))):
            if filename.endswith("hires.png"):
                continue

            path, filename = os.path.split(filename)
            basename, ext = os.path.splitext(filename)
            if basename in skips:
                continue

            # Create thumbnails based on images in tmpdir, and place
            # them within the build tree
            orig_path = str(os.path.join(origdir, filename))
            thumb_path = str(os.path.join(thumbdir, filename))
            if out_of_date(orig_path, thumb_path) or True:
                thumbnails[orig_path] = thumb_path

            m = multiimage.match(basename)
            if m is None:
                pyfile = '%s.py' % basename
            else:
                basename = m.group(1)
                pyfile = '%s.py' % basename

            data.append((subdir, basename,
                         os.path.join(rootdir, subdir, 'thumbnails',
                                      filename)))

    link_template = """\
    <a href="%(href)s"><img src="%(thumb_file)s" border="0" alt="%(alternative_text)s"/></a>
    """

    random_image_content_template = '''
// This file was automatically generated by gen_gallery.py & should not be modified directly.

images = new Array();

%s

'''

    random_image_template = "['%(thumbfile)s', '%(full_image)s', '%(link)s', 'Iris examples.'];"
    random_image_join = 'images[%s] = %s'

    if len(data) == 0:
        warnings.warn("No thumbnails were found")
        return

    rows = []
    random_image = []
    for (subdir, basename, thumbfile) in data:
        if thumbfile is not None:
            link = 'examples/%s/%s.html#%s' % (
                subdir, basename, os.path.splitext(
                    os.path.basename(thumbfile))[0].replace('_', '-'))
            rows.append(
                link_template % {
                    'href': link,
                    'thumb_file': thumbfile,
                    'alternative_text': basename
                })
            random_image.append(
                random_image_template % {
                    'link': link,
                    'thumbfile': thumbfile,
                    'basename': basename,
                    'full_image': '_images/' + os.path.basename(thumbfile)
                })

    random_image_content = random_image_content_template % '\n'.join(
        [random_image_join % (i, line) for i, line in enumerate(random_image)])
    random_image_script_path = os.path.join(app.builder.srcdir, '_static',
                                            'random_image.js')
    file(random_image_script_path, 'w').write(random_image_content)

    # Only write out the file if the contents have actually changed.
    # Otherwise, this triggers a full rebuild of the docs
    content = template % '\n'.join(rows)
    gallery_path = os.path.join(app.builder.srcdir, '_templates',
                                'gallery.html')
    if os.path.exists(gallery_path):
        fh = file(gallery_path, 'r')
        regenerate = fh.read() != content
        fh.close()
    else:
        regenerate = True
    if regenerate:
        fh = file(gallery_path, 'w')
        fh.write(content)
        fh.close()

    try:
        import multiprocessing
        app.builder.info("generating thumbnails... ", nonl=True)
        pool = multiprocessing.Pool()
        pool.map(make_thumbnail, thumbnails.iteritems())
        pool.close()
        pool.join()
        app.builder.info("done")

    except ImportError:
        for key in app.builder.status_iterator(thumbnails.iterkeys(),
                                               "generating thumbnails... ",
                                               length=len(thumbnails)):
            image.thumbnail(key, thumbnails[key], 0.3)
Esempio n. 21
0
def taskC():
    image = Image.open('f1.jpg')
    print(image.size)
    image.thumbnail((100, 100))
    print(image.size)
    image.show()
Esempio n. 22
0
def heatmap_report(co2,
                   elec,
                   year=2021,
                   which="individual",
                   fig_folder=None,
                   tz_offset=6):
    """

    Parameters
    ----------
    co2: BaData
        carbon data
    elec: BaData
        electricity data
    which: str in ["individual", "group"]
    fig_folder: str, default None
    tz_offset: int, default 6
        offset to shift the time stamps (assumes data is provided in UTC)
        default is Mountain time
    """
    logger = logging.getLogger("gridemissions")
    start = pd.to_datetime(f"{year}0101T0000Z")
    end = pd.to_datetime(f"{int(year)+1}0101T0000Z")
    co2i = pd.DataFrame(
        {
            ba: (co2.df.loc[start:end,
                            co2.get_cols(ba, field="D")].values.flatten() /
                 elec.df.loc[start:end,
                             elec.get_cols(ba, field="D")].values.flatten())
            for ba in HEATMAP_BAS
        },
        index=co2.df.loc[start:end].index,
    )

    # Change timezone
    co2i.index -= pd.Timedelta(f"{tz_offset}h")

    free_folder = fig_folder / f"{year}" / "free_scale"
    fixed_folder = fig_folder / f"{year}" / "fixed_scale"
    free_folder.mkdir(parents=True, exist_ok=True)
    fixed_folder.mkdir(parents=True, exist_ok=True)

    for ba in HEATMAP_BAS:
        f, ax = plt.subplots(figsize=(PAGE_WIDTH, 1.5 * ROW_HEIGHT))
        heatmap(
            co2i[ba],
            fax=(f, ax),
            cmap="RdYlGn_r",
            cbar_label=f"kg/MWh",
            transpose=True,
        )
        add_watermark(ax)
        ax.set_title(f"{ba}: Consumption-based carbon intensity",
                     fontsize="large")
        f.tight_layout()
        if fig_folder is not None:
            f.savefig(free_folder / f"{ba}.pdf")
            f.savefig(free_folder / f"{ba}.png")
            image.thumbnail(
                free_folder / f"{ba}.png",
                free_folder / f"{ba}-thumbnail.png",
                scale=0.1,
            )
        plt.close(f)

        f, ax = plt.subplots(figsize=(PAGE_WIDTH, 1.5 * ROW_HEIGHT))
        heatmap(
            co2i[ba],
            fax=(f, ax),
            vmin=100,
            vmax=900,
            cmap="RdYlGn_r",
            cbar_label=f"kg/MWh",
            transpose=True,
        )
        add_watermark(ax)
        ax.set_title(f"{ba}: Consumption-based carbon intensity",
                     fontsize="large")
        f.tight_layout()
        if fig_folder is not None:
            f.savefig(fixed_folder / f"{ba}.pdf")
            f.savefig(fixed_folder / f"{ba}.png")
            image.thumbnail(
                fixed_folder / f"{ba}.png",
                fixed_folder / f"{ba}-thumbnail.png",
                scale=0.1,
            )
        plt.close(f)

    n = len(HEATMAP_BAS)
    nrows = n // 4
    f, ax = plt.subplots(nrows,
                         4,
                         figsize=(1.2 * PAGE_WIDTH, nrows / 2 * ROW_HEIGHT))
    ax = ax.flatten()

    for iba, ba in enumerate(HEATMAP_BAS[:-1]):
        ax[iba].set_title(ba)
        heatmap(
            co2i[ba],
            fax=(f, ax[iba]),
            vmin=100,
            vmax=900,
            cmap="RdYlGn_r",
            cbar_label="kg/MWh",
            with_cbar=False,
        )

    for a in ax:
        a.set_yticks([])
        a.set_xticks([])
        a.set_ylabel("")
        a.set_xlabel("")
    f.tight_layout()

    ba = HEATMAP_BAS[-1]
    iba = len(HEATMAP_BAS) - 1
    ax[iba].set_title(ba)
    heatmap(
        co2i[ba],
        fax=(f, ax[iba]),
        vmin=100,
        vmax=900,
        cmap="RdYlGn_r",
        cbar_label="kg/MWh",
        with_cbar=True,
        cbar_ax=[ax[-4:]],
    )
    for a in ax:
        a.set_yticks([])
        a.set_xticks([])
        a.set_ylabel("")
        a.set_xlabel("")
    add_watermark(ax[iba], y=-0.05)

    if fig_folder is not None:
        f.savefig(fig_folder / f"{year}" / f"Top {n} heatmaps.pdf")
        f.savefig(fig_folder / f"{year}" / f"Top {n} heatmaps.png")
Esempio n. 23
0
    def collect( self, 
                 blocks,
                 template_name, 
                 outdir, 
                 rstdir,
                 rst2rootdir, 
                 rst2builddir,
                 rst2srcdir,
                 content,
                 display_options,
                 linked_codename,
                 tracker_id):
        '''collect one or more matplotlib figures and 
        
        1. save as png, hires-png and pdf
        2. save thumbnail
        3. insert rendering code at placeholders in output

        returns a map of place holder to placeholder text.
        '''
        fig_managers = _pylab_helpers.Gcf.get_all_fig_managers()

        map_figure2text = {}

        # determine the image formats to create
        default_format, additional_formats = Utils.getImageFormats()
        all_formats = [default_format,] + additional_formats
        urls = Utils.asList( Utils.PARAMS["report_urls"] )

        # create all the images
        for figman in fig_managers:
            # create all images
            figid = figman.num
            for id, format, dpi in all_formats:

                outname = "%s_%02d" % (template_name, figid)

                outpath = os.path.join(outdir, '%s.%s' % (outname, format))

                try:
                    figman.canvas.figure.savefig( outpath, dpi=dpi )
                except:
                    s = exception_to_str("Exception running plot %s" % outpath)
                    warnings.warn(s)
                    return []

                if format=='png':
                    thumbdir = os.path.join(outdir, 'thumbnails')
                    try:
                        os.makedirs(thumbdir)
                    except OSError:
                        pass
                    thumbfile = str('%s.png' % os.path.join(thumbdir, outname) )
                    captionfile = str('%s.txt' % os.path.join(thumbdir, outname) )
                    if not os.path.exists(thumbfile):
                        # thumbnail only available in matplotlib >= 0.98.4
                        try:
                            figthumb = image.thumbnail(str(outpath), str(thumbfile), scale=0.3)
                        except AttributeError:
                            pass
                    outfile = open(captionfile,"w")
                    outfile.write( "\n".join( content ) + "\n" )
                    outfile.close()

            # create the text element
            rst_output = ""
            # for image directive - image path is relative from rst file to external build dir
            imagepath = re.sub( "\\\\", "/", os.path.join( rst2builddir, outname ) )

            # for links - path is from rst file to internal root dir
            # one rst file for all
            relative_imagepath_rst = re.sub( "\\\\", "/", os.path.join( rst2rootdir, template_name ) )
            relative_imagepath_img = re.sub( "\\\\", "/", os.path.join( rst2rootdir, outname ) )

            linked_text = relative_imagepath_rst + ".txt"

            if Config.HTML_IMAGE_FORMAT:
                id, format, dpi = Config.HTML_IMAGE_FORMAT
                template = '''
.. htmlonly::

   .. image:: %(linked_image)s
%(display_options)s

   [%(code_url)s %(rst_url)s %(data_url)s %(extra_images)s]
'''
                linked_image = imagepath + ".%s" % format

                extra_images=[]
                for id, format, dpi in additional_formats:
                    extra_images.append( "`%(id)s <%(relative_imagepath_img)s.%(format)s>`__" % locals())
                if extra_images: extra_images = " " + " ".join( extra_images)
                else: extra_images = ""

                # construct additional urls
                code_url, data_url, rst_url, table_url = "", "", "", ""
                if "code" in urls:
                    code_url = "`code <%(linked_codename)s>`__" % locals()

                if "data" in urls:
                    data_url = "`data </data/%(tracker_id)s>`__" % locals()

                if "rst" in urls:
                    rst_url = "`rst <%(linked_text)s>`__" % locals()

                rst_output += template % locals()

            # treat latex separately
            if Config.LATEX_IMAGE_FORMAT:
                id, format, dpi = Config.LATEX_IMAGE_FORMAT
                template = '''
.. latexonly::

   .. image:: %(linked_image)s
%(display_options)s
'''
                linked_image = imagepath + ".%s" % format
                rst_output += template % locals()

            map_figure2text[ "#$mpl %i$#" % figid] = rst_output

        return map_figure2text
Esempio n. 24
0
def generate_file_rst(fname, target_dir, src_dir, plot_gallery):
    """ Generate the rst file for a given example.
    """
    if not plot_gallery:
        print("gallery generation deactivated")
    base_image_name = os.path.splitext(fname)[0]
    image_fname = '%s_%%s.png' % base_image_name

    this_template = rst_template
    last_dir = os.path.split(src_dir)[-1]
    # to avoid leading . in file names, and wrong names in links
    if last_dir == '.' or last_dir == 'examples':
        last_dir = ''
    else:
        last_dir += '_'
    link_name = "example_" + last_dir + fname  # used in templates
    src_file = os.path.join(src_dir, fname)
    example_file = os.path.join(target_dir, fname)
    shutil.copyfile(src_file, example_file)

    # The following is a list containing all the figure names
    figure_list = []

    image_dir = os.path.join(target_dir, 'images')
    thumb_dir = os.path.join(image_dir, 'thumb')
    if not os.path.exists(image_dir):
        os.makedirs(image_dir)
    if not os.path.exists(thumb_dir):
        os.makedirs(thumb_dir)
    image_path = os.path.join(image_dir, image_fname)
    stdout_path = os.path.join(image_dir,
                               'stdout_%s.txt' % base_image_name)
    time_path = os.path.join(image_dir,
                               'time_%s.txt' % base_image_name)
    thumb_file = os.path.join(thumb_dir, fname[:-3] + '.png')
    time_elapsed = 0
    time_m = 0
    time_s = 0
    if plot_gallery and fname.startswith('plot'):
        # generate the plot as png images if file name
        # starts with "plot" and if it's more recent than an
        # existing image.
        first_image_file = image_path % 1
        if os.path.exists(stdout_path):
            stdout = open(stdout_path).read()
        else:
            stdout = ''
        if os.path.exists(time_path):
            time_elapsed = float(open(time_path).read())
        else:
            time_elapsed = -1

        if (not os.path.exists(first_image_file) or
                os.stat(first_image_file).st_mtime <=
                                    os.stat(src_file).st_mtime):

            print('Executing %s...' % fname)
            import matplotlib.pyplot as plt
            plt.close('all')
            cwd = os.getcwd()
            orig_stdout = sys.stdout
            try:
                # First CD in the original example dir, so that any file
                # created by the example get created in this directory
                os.chdir(os.path.dirname(src_file))
                my_buffer = StringIO()
                my_stdout = Tee(sys.stdout, my_buffer)

                sys.stdout = my_stdout
                my_globals = {'pl': plt}
                t0 = time()
                execfile(os.path.basename(src_file), my_globals)
                time_elapsed = time() - t0
                sys.stdout = orig_stdout

                my_stdout = my_buffer.getvalue()

                if '__doc__' in my_globals:
                    # The __doc__ is often printed in the example, so we
                    # don't echo it
                    my_stdout = my_stdout.replace(
                                            my_globals['__doc__'],
                                            '')
                my_stdout = my_stdout.strip()
                if my_stdout:
                    stdout = '**Script output**::\n\n  %s\n\n' % (
                        '\n  '.join(my_stdout.split('\n')))
                open(stdout_path, 'w').write(stdout)
                open(time_path, 'w').write('%f' % time_elapsed)
                os.chdir(cwd)

                # In order to save every figure we have two solutions :
                # * iterate from 1 to infinity and call plt.fignum_exists(n)
                #   (this requires the figures to be numbered
                #    incrementally: 1, 2, 3 and not 1, 2, 5)
                # * iterate over [fig_mngr.num for fig_mngr in
                #   matplotlib._pylab_helpers.Gcf.get_all_fig_managers()]
                for fig_num in (fig_mngr.num for fig_mngr in
                        matplotlib._pylab_helpers.Gcf.get_all_fig_managers()):
                    # Set the fig_num figure as the current figure as we can't
                    # save a figure that's not the current figure.
                    plt.figure(fig_num)
                    fig_file_name = image_path % fig_num
                    try:
                        plt.tight_layout(pad=.5)
                    except AttributeError:
                        # tight_layout is present only in recent versions
                        # of matplotlib
                        pass
                    plt.savefig(fig_file_name)
                    figure_list.append(image_fname % fig_num)
            except:
                print(80 * '_')
                print('execution of %s has failed:' % fname)
                traceback.print_exc()
                print(80 * '_')
            finally:
                os.chdir(cwd)
                sys.stdout = orig_stdout

            if time_elapsed >= 0:
                print(" - time elapsed : %.2g sec" % time_elapsed)

        else:
            print("Output of %s is up-to-date." % fname)
            figure_list = [f[len(image_dir):]
                            for f in glob.glob(image_path % '[1-9]')]
                            #for f in glob.glob(image_path % '*')]

        # generate thumb file
        this_template = plot_rst_template
        from matplotlib import image
        if os.path.exists(first_image_file):
            image.thumbnail(first_image_file, thumb_file, 0.2)

    if not os.path.exists(thumb_file):
        # create something not to replace the thumbnail
        shutil.copy('images/blank_image.png', thumb_file)

    docstring, short_desc, end_row = extract_docstring(example_file)

    # Depending on whether we have one or more figures, we're using a
    # horizontal list or a single rst call to 'image'.
    if len(figure_list) == 1:
        figure_name = figure_list[0]
        image_list = SINGLE_IMAGE % figure_name.lstrip('/')
    else:
        image_list = HLIST_HEADER
        for figure_name in figure_list:
            image_list += HLIST_IMAGE_TEMPLATE % figure_name.lstrip('/')

    time_m, time_s = divmod(time_elapsed, 60)
    f = open(os.path.join(target_dir, fname[:-2] + 'rst'), 'w')
    f.write(this_template % locals())
    f.flush()
    return link_name
Esempio n. 25
0
import os, numpy as np
from pymicro.xray.detectors import Mar165
from pymicro.file.file_utils import HST_read
from matplotlib import pyplot as plt, cm

if __name__ == '__main__':
    mar = Mar165()  # create a mar object
    mar.ucen = 981  # position of the direct beam on the x axis
    mar.vcen = 1060  # position of the direct beam on the y axis
    mar.calib = 495. / 15.  # identified on (111) ring of CeO2 powder
    mar.correction = 'bg'
    mar.compute_TwoTh_Psi_arrays()

    # load background for correction
    mar.bg = HST_read('../data/c1_exptime_bgair_5.raw',
                      data_type=np.uint16,
                      dims=(2048, 2048, 1))[:, :, 0].astype(np.float32)
    # load image
    mar.load_image('../data/c1_exptime_air_5.raw')

    image_name = os.path.splitext(__file__)[0] + '.png'
    print 'writting %s' % image_name
    plt.imsave(image_name, mar.corr_data, vmin=0, vmax=2000)

    from matplotlib import image

    image.thumbnail(image_name, 'thumb_' + image_name, scale=200. / 2048)
Esempio n. 26
0
    # build custom pole figures
    pf = PoleFigure(microstructure=micro)
    pf.mksize = 40
    pf.set_map_field('strain',
                     strain_field,
                     field_min_level=0.015,
                     field_max_level=0.025)
    fig = plt.figure()
    # direct PF
    ax1 = fig.add_axes([0.05, 0.05, 0.8, 0.9], aspect='equal')
    pf.plot_pf(ax=ax1)
    plt.title('111 pole figure, cubic elasticity')

    # to add the color bar
    ax2 = fig.add_axes([0.8, 0.05, 0.05, 0.9])
    norm = colors.Normalize(vmin=0.015, vmax=0.025)
    cb = colorbar.ColorbarBase(ax2,
                               cmap=cm.hot,
                               norm=norm,
                               orientation='vertical')
    cb.set_label('Average strain (mm/mm)')

    image_name = os.path.splitext(__file__)[0] + '.png'
    print('writting %s' % image_name)
    plt.savefig('%s' % image_name, format='png')

    from matplotlib import image

    image.thumbnail(image_name, 'thumb_' + image_name, 0.2)
Esempio n. 27
0
def gen_gallery(app, doctree):
    if app.builder.name != 'html':
        return

    outdir = app.builder.outdir
    rootdir = 'examples'

    # Images we want to skip for the gallery because they are an unusual
    # size that doesn't layout well in a table, or because they may be
    # redundant with other images or uninteresting.
    skips = set([
        'mathtext_examples',
        'matshow_02',
        'matshow_03',
        'matplotlib_icon',
        ])

    thumbnails = {}
    rows = []
    random_image = []
    toc_rows = []

    link_template = ('<a href="{href}">'
                     '<img src="{thumb_file}" border="0"'
                     ' alt="{alternative_text}"/>'
                     '</a>')

    header_template = ('<div class="section" id="{}">'
                       '<h4>{}'
                       '<a class="headerlink" href="#{}"'
                       ' title="Permalink to this headline">&para;</a>'
                       '</h4>')

    toc_template = ('<li>'
                    '<a class="reference internal" href="#{}">{}</a>'
                    '</li>')

    random_image_content_template = '''
// This file was automatically generated by gen_gallery.py & should not be
// modified directly.

images = new Array();

{}

'''

    random_image_template = "['{thumbfile}', '{full_image}', '{link}'];"
    random_image_join = 'images[{}] = {}'

    dirs = ('General', 'Meteorology', 'Oceanography')

    for subdir in dirs:
        rows.append(header_template.format(subdir, subdir, subdir))
        toc_rows.append(toc_template.format(subdir, subdir))

        origdir = os.path.join(os.path.dirname(outdir), rootdir, subdir)
        if not os.path.exists(origdir):
            origdir = os.path.join(os.path.dirname(outdir), 'plot_directive',
                                   rootdir, subdir)
        thumbdir = os.path.join(outdir, rootdir, subdir, 'thumbnails')
        if not os.path.exists(thumbdir):
            os.makedirs(thumbdir)

        data = []

        for filename in sorted(glob.glob(os.path.join(origdir, '*.png'))):
            if filename.endswith('hires.png'):
                continue

            path, filename = os.path.split(filename)
            basename, ext = os.path.splitext(filename)
            if basename in skips:
                continue

            # Create thumbnails based on images in tmpdir, and place them
            # within the build tree.
            orig_path = str(os.path.join(origdir, filename))
            thumb_path = str(os.path.join(thumbdir, filename))
            if out_of_date(orig_path, thumb_path) or True:
                thumbnails[orig_path] = thumb_path

            m = multiimage.match(basename)
            if m is not None:
                basename = m.group(1)

            data.append((subdir, basename,
                         os.path.join(rootdir, subdir, 'thumbnails',
                                      filename)))

        for (subdir, basename, thumbfile) in data:
            if thumbfile is not None:
                anchor = os.path.basename(thumbfile)
                anchor = os.path.splitext(anchor)[0].replace('_', '-')
                link = 'examples/{}/{}.html#{}'.format(
                    subdir,
                    basename,
                    anchor)
                rows.append(link_template.format(
                    href=link,
                    thumb_file=thumbfile,
                    alternative_text=basename))
                random_image.append(random_image_template.format(
                    link=link,
                    thumbfile=thumbfile,
                    basename=basename,
                    full_image='_images/' + os.path.basename(thumbfile)))

        if len(data) == 0:
            warnings.warn('No thumbnails were found in {}'.format(subdir))

        # Close out the <div> opened up at the top of this loop.
        rows.append('</div>')

    # Generate JS list of images for front page.
    random_image_content = '\n'.join([random_image_join.format(i, line)
                                      for i, line in enumerate(random_image)])
    random_image_content = random_image_content_template.format(
        random_image_content)
    random_image_script_path = os.path.join(app.builder.srcdir,
                                            '_static',
                                            'random_image.js')
    with open(random_image_script_path, 'w') as fh:
        fh.write(random_image_content)

    content = template.format('\n'.join(toc_rows),
                              '\n'.join(rows))

    # Only write out the file if the contents have actually changed.
    # Otherwise, this triggers a full rebuild of the docs.

    gallery_path = os.path.join(app.builder.srcdir,
                                '_templates',
                                'gallery.html')
    if os.path.exists(gallery_path):
        with open(gallery_path, 'r') as fh:
            regenerate = fh.read() != content
    else:
        regenerate = True
    if regenerate:
        with open(gallery_path, 'w') as fh:
            fh.write(content)

    for key in status_iterator(thumbnails,
                               'generating thumbnails... ',
                               length=len(thumbnails)):
        image.thumbnail(key, thumbnails[key], 0.3)
Esempio n. 28
0
def generate_file_rst(fname, target_dir, src_dir, plot_gallery):
    """ Generate the rst file for a given example.
    """
    base_image_name = os.path.splitext(fname)[0]
    image_fname = '%s_%%s.png' % base_image_name
    root_image_fname = 'root_%s_%%s.png' % base_image_name
    root_fig_num = 1

    this_template = rst_template
    last_dir = os.path.split(src_dir)[-1]
    # to avoid leading . in file names, and wrong names in links
    if last_dir == '.' or last_dir == 'examples':
        last_dir = ''
    else:
        last_dir += '_'
    short_fname = last_dir + fname
    src_file = os.path.join(src_dir, fname)
    example_file = os.path.join(target_dir, fname)
    shutil.copyfile(src_file, example_file)

    # The following is a list containing all the figure names
    figure_list = []

    image_dir = os.path.join(target_dir, 'images')
    thumb_dir = os.path.join(image_dir, 'thumb')
    if not os.path.exists(image_dir):
        os.makedirs(image_dir)
    if not os.path.exists(thumb_dir):
        os.makedirs(thumb_dir)
    image_path = os.path.join(image_dir, image_fname)
    root_image_path = os.path.join(image_dir, root_image_fname)

    stdout_path = os.path.join(image_dir, 'stdout_%s.txt' % base_image_name)
    time_path = os.path.join(image_dir, 'time_%s.txt' % base_image_name)
    thumb_file = os.path.join(thumb_dir, fname[:-3] + '.png')
    time_elapsed = 0
    if plot_gallery and fname.startswith('plot'):
        # generate the plot as png image if file name
        # starts with plot and if it is more recent than an
        # existing image.
        first_image_file = image_path % 1
        first_root_image_file = root_image_path % 1
        if os.path.exists(stdout_path):
            stdout = open(stdout_path).read()
        else:
            stdout = ''
        if os.path.exists(time_path):
            time_elapsed = float(open(time_path).read())

        if (not os.path.exists(first_image_file)
                or not os.path.exists(first_root_image_file)
                or os.stat(first_image_file).st_mtime <=
                os.stat(src_file).st_mtime):
            # We need to execute the code
            print('plotting %s' % fname)
            t0 = time()
            import matplotlib.pyplot as plt
            plt.close('all')
            cwd = os.getcwd()
            try:
                # First CD in the original example dir, so that any file
                # created by the example get created in this directory
                orig_stdout = sys.stdout
                os.chdir(os.path.dirname(src_file))
                my_buffer = StringIO()
                my_stdout = Tee(sys.stdout, my_buffer)
                sys.stdout = my_stdout
                my_globals = {'pl': plt}
                execfile(os.path.basename(src_file), my_globals)
                time_elapsed = time() - t0
                sys.stdout = orig_stdout
                my_stdout = my_buffer.getvalue()
                if '__doc__' in my_globals:
                    # The __doc__ is often printed in the example, we
                    # don't with to echo it
                    my_stdout = my_stdout.replace(my_globals['__doc__'], '')
                my_stdout = my_stdout.strip()
                if my_stdout:
                    stdout = '**Script output**::\n\n  %s\n\n' % ('\n  '.join(
                        my_stdout.split('\n')))
                open(stdout_path, 'w').write(stdout)
                open(time_path, 'w').write('%f' % time_elapsed)
                os.chdir(cwd)

                # In order to save every figure we have two solutions :
                # * iterate from 1 to infinity and call plt.fignum_exists(n)
                #   (this requires the figures to be numbered
                #    incrementally: 1, 2, 3 and not 1, 2, 5)
                # * iterate over [fig_mngr.num for fig_mngr in
                #   matplotlib._pylab_helpers.Gcf.get_all_fig_managers()]
                for fig_num in (fig_mngr.num for fig_mngr in matplotlib.
                                _pylab_helpers.Gcf.get_all_fig_managers()):
                    # Set the fig_num figure as the current figure as we can't
                    # save a figure that's not the current figure.
                    plt.figure(fig_num)
                    plt.savefig(image_path % fig_num)
                    figure_list.append(image_fname % fig_num)
                for canvas in ROOT.gROOT.GetListOfCanvases():
                    maybe_root_filename = os.path.join(
                        os.path.dirname(src_file), canvas.name)
                    if os.path.isfile(maybe_root_filename):
                        os.rename(maybe_root_filename,
                                  os.path.join(image_dir, canvas.name))
                        figure_list.append(canvas.name)
                        canvas.Close()
                    else:
                        canvas.SaveAs(root_image_path % root_fig_num)
                        canvas.Close()
                        figure_list.append(root_image_fname % root_fig_num)
                        root_fig_num += 1
            except:
                print(80 * '_')
                print('%s is not compiling:' % fname)
                traceback.print_exc()
                print(80 * '_')
            finally:
                os.chdir(cwd)
                sys.stdout = orig_stdout

            print(" - time elapsed : %.2g sec" % time_elapsed)
        else:
            figure_list = [
                f[len(image_dir):] for f in glob.glob(image_path % '[1-9]')
            ]
            #for f in glob.glob(image_path % '*')]

        # generate thumb file
        this_template = plot_rst_template
        from matplotlib import image
        if os.path.exists(first_image_file):
            image.thumbnail(first_image_file, thumb_file, 0.2)
        elif os.path.exists(first_root_image_file):
            image.thumbnail(first_root_image_file, thumb_file, 0.2)

    if not os.path.exists(thumb_file):
        # create something not to replace the thumbnail
        shutil.copy('images/blank_image.png', thumb_file)

    docstring, short_desc, end_row = extract_docstring(example_file)

    # Depending on whether we have one or more figures, we're using a
    # horizontal list or a single rst call to 'image'.
    if len(figure_list) == 1:
        figure_name = figure_list[0]
        image_list = SINGLE_IMAGE % figure_name.lstrip('/')
    else:
        image_list = HLIST_HEADER
        for figure_name in figure_list:
            image_list += HLIST_IMAGE_TEMPLATE % figure_name.lstrip('/')

    f = open(os.path.join(target_dir, fname[:-2] + 'rst'), 'w')
    f.write(this_template % locals())
    f.flush()
Esempio n. 29
0
print(image.mode)
print(image.size)
# show the image
image.show()

# B. Convert the image to a numpy array:

# load and display an image with Matplotlib
from matplotlib import image
from matplotlib import pyplot
# load image as pixel array
data = image.imread('f1.jpg')
# summarize shape of the pixel array
print(data.dtype)
print(data.shape)
# display the array of pixels as an image
pyplot.imshow(data)
pyplot.show()

# C. Resize an image to a specific dimension:

# create a thumbnail of an image
# load the image
image = Image.open('f1.jpg')
# report the size of the image
print(image.size)
# create a thumbnail and preserve aspect ratio
image.thumbnail((100, 100))
# report the size of the thumbnail
print(image.size)
Esempio n. 30
0
def makefig(fullpath, outdir):
    """
    run a pyplot script<t and save the low and high res PNGs and a PDF in _static
    """

    fullpath = str(fullpath)  # todo, why is unicode breaking this

    print '    makefig: fullpath=%s, outdir=%s'%( fullpath, outdir)
    formats = [('png', 80),
               ('hires.png', 200),
               ('pdf', 50),
               ]

    basedir, fname = os.path.split(fullpath)
    basename, ext = os.path.splitext(fname)
    all_exists = True

    if basedir != outdir:
        shutil.copyfile(fullpath, os.path.join(outdir, fname))

    # Look for single-figure output files first
    for format, dpi in formats:
        outname = os.path.join(outdir, '%s.%s' % (basename, format))
        if out_of_date(fullpath, outname):
            all_exists = False
            break

    if all_exists:
        print '    already have %s'%fullpath
        return 1

    # Then look for multi-figure output files, assuming
    # if we have some we have all...
    i = 0
    while True:
        all_exists = True
        for format, dpi in formats:
            outname = os.path.join(outdir, '%s_%02d.%s' % (basename, i, format))
            if out_of_date(fullpath, outname):
                all_exists = False
                break
        if all_exists:
            i += 1
        else:
            break

    if i != 0:
        print '    already have %d figures for %s' % (i, fullpath)
        return i

    # We didn't find the files, so build them

    print '    building %s'%fullpath
    plt.close('all')    # we need to clear between runs
    matplotlib.rcdefaults()
    # Set a figure size that doesn't overflow typical browser windows
    matplotlib.rcParams['figure.figsize'] = (5.5, 4.5)

    try:
        runfile(fullpath)
    except:
	s = cbook.exception_to_str("Exception running plot %s" % fullpath)
        warnings.warn(s)
        return 0

    fig_managers = _pylab_helpers.Gcf.get_all_fig_managers()
    for i, figman in enumerate(fig_managers):
        for format, dpi in formats:
            if len(fig_managers) == 1:
                outname = basename
            else:
                outname = "%s_%02d" % (basename, i)
            outpath = os.path.join(outdir, '%s.%s' % (outname, format))
            try:
                figman.canvas.figure.savefig(outpath, dpi=dpi)
            except:
                s = cbook.exception_to_str("Exception running plot %s" % fullpath)
                warnings.warn(s)
                return 0

            if format=='png':
                thumbdir = os.path.join(outdir, 'thumbnails')
                if not os.path.exists(thumbdir):
                    os.makedirs(thumbdir)
                thumbfile = str('%s.png'%os.path.join(thumbdir, outname)     )
                if not os.path.exists(thumbfile):
                    figthumb = image.thumbnail(str(outpath), str(thumbfile), scale=0.3)
                    print '    makefig: saved thumbnail of %s to %s'%(outpath, thumbfile)

    return len(fig_managers)
Esempio n. 31
0
def gen_gallery(app, doctree):
    if app.builder.name != 'html':
        return

    outdir = app.builder.outdir
    builddir = app.builder.builddir
    rootdir = 'plot_directive/examples'
#    rootdir = '../examples'

    # images we want to skip for the gallery because they are an unusual
    # size that doesn't layout well in a table, or because they may be
    # redundant with other images or uninteresting
    skips = set([
        'mathtext_examples',
        'matshow_02',
        'matshow_03',
        'matplotlib_icon',
        ])

    data = []
    thumbnails = {}

    for subdir in ('graphics',  ):
        origdir = os.path.join('build', rootdir, subdir)
        thumbdir = os.path.join('build', rootdir, subdir)
        if not os.path.exists(thumbdir):
            os.makedirs(thumbdir)

        for filename in sorted(glob.glob(os.path.join(origdir, '*.png'))):
            if filename.endswith(".hires.png") or filename.endswith(".thumb.png"):
                continue

            path, filename = os.path.split(filename)
            basename, ext = os.path.splitext(filename)
            if basename in skips:
                continue

            thumbnail_fname = lambda filename: os.path.splitext(filename)[0] + '.thumb.png'

            # Create thumbnails based on images in tmpdir, and place
            # them within the build tree
            orig_path = str(os.path.join(origdir, filename))
            thumb_path = str(os.path.join(thumbdir, thumbnail_fname(filename)))
            if out_of_date(orig_path, thumb_path) or True:
                thumbnails[orig_path] = thumb_path

            m = multiimage.match(basename)
            if m is None:
                pyfile = '%s.py'%basename
            else:
                basename = m.group(1)
                pyfile = '%s.py'%basename
            
            # Not sure what this does
            data.append((subdir, basename,
                         os.path.relpath(
                                         os.path.abspath(
                                                         os.path.join(thumbdir, thumbnail_fname(filename))
                                                         ), 
                                         builddir
                                         )
                         ))
            
    link_template = """\
    <a href="%s"><img src="%s" border="0" alt="%s"/></a>
    """

    if len(data) == 0:
        warnings.warn("No thumbnails were found")

    rows = []
    for (subdir, basename, thumbfile) in data:
        if thumbfile is not None:
            link = 'examples/%s/%s.html'%(subdir, basename)
            rows.append(link_template%(link, thumbfile, basename))

    # Only write out the file if the contents have actually changed.
    # Otherwise, this triggers a full rebuild of the docs
    content = template%'\n'.join(rows)
    gallery_path = os.path.join(app.builder.srcdir, '_templates', 'gallery.html')
    if os.path.exists(gallery_path):
        fh = file(gallery_path, 'r')
        regenerate = fh.read() != content
        fh.close()
    else:
        regenerate = True
    if regenerate:
        fh = file(gallery_path, 'w')
        fh.write(content)
        fh.close()
        
    try:
        raise ImportError()
        import multiprocessing
        app.builder.info("generating thumbnails... ", nonl=True)
        pool = multiprocessing.Pool()
        pool.map(make_thumbnail, thumbnails.iteritems())
        pool.close()
        pool.join()
        app.builder.info("done")

    except ImportError:
        for key in app.builder.status_iterator(
            thumbnails.iterkeys(), "generating thumbnails... ",
            length=len(thumbnails)):
            image.thumbnail(key, thumbnails[key], 0.3)
Esempio n. 32
0
def generate_file_rst(fname, target_dir, src_dir, plot_gallery):
    """ Generate the rst file for a given example.
    """
    base_image_name = os.path.splitext(fname)[0]
    image_fname = '%s_%%s.png' % base_image_name

    this_template = rst_template
    last_dir = os.path.split(src_dir)[-1]
    # to avoid leading . in file names, and wrong names in links
    if last_dir == '.' or last_dir == 'examples':
        last_dir = ''
    else:
        last_dir += '_'
    short_fname = last_dir + fname
    src_file = os.path.join(src_dir, fname)
    example_file = os.path.join(target_dir, fname)
    shutil.copyfile(src_file, example_file)

    # The following is a list containing all the figure names
    figure_list = []

    image_dir = os.path.join(target_dir, 'images')
    thumb_dir = os.path.join(image_dir, 'thumb')
    if not os.path.exists(image_dir):
        os.makedirs(image_dir)
    if not os.path.exists(thumb_dir):
        os.makedirs(thumb_dir)
    image_path = os.path.join(image_dir, image_fname)
    thumb_file = os.path.join(thumb_dir, fname[:-3] + '.png')
    if plot_gallery and fname.startswith('plot'):
        # generate the plot as png image if file name
        # starts with plot and if it is more recent than an
        # existing image.
        first_image_file = image_path % 1

        if (not os.path.exists(first_image_file)
                or os.stat(first_image_file).st_mtime <=
                os.stat(src_file).st_mtime):
            # We need to execute the code
            print 'plotting %s' % fname
            import matplotlib.pyplot as plt
            plt.close('all')
            cwd = os.getcwd()
            try:
                # First CD in the original example dir, so that any file created
                # by the example get created in this directory
                os.chdir(os.path.dirname(src_file))
                execfile(os.path.basename(src_file), {'pl': plt})
                os.chdir(cwd)

                # In order to save every figure we have two solutions :
                # * iterate from 1 to infinity and call plt.fignum_exists(n)
                #   (this requires the figures to be numbered
                #    incrementally: 1, 2, 3 and not 1, 2, 5)
                # * iterate over [fig_mngr.num for fig_mngr in
                #   matplotlib._pylab_helpers.Gcf.get_all_fig_managers()]
                for fig_num in (fig_mngr.num for fig_mngr in matplotlib.
                                _pylab_helpers.Gcf.get_all_fig_managers()):
                    # Set the fig_num figure as the current figure as we can't
                    # save a figure that's not the current figure.
                    plt.figure(fig_num)
                    plt.savefig(image_path % fig_num)
                    figure_list.append(image_fname % fig_num)
            except:
                print 80 * '_'
                print '%s is not compiling:' % fname
                traceback.print_exc()
                print 80 * '_'
            finally:
                os.chdir(cwd)
        else:
            figure_list = [
                f[len(image_dir):] for f in glob.glob(image_path % '[1-9]')
            ]
            #for f in glob.glob(image_path % '*')]

        # generate thumb file
        this_template = plot_rst_template
        from matplotlib import image
        if os.path.exists(first_image_file):
            image.thumbnail(first_image_file, thumb_file, 0.2)

    if not os.path.exists(thumb_file):
        # create something not to replace the thumbnail
        shutil.copy('images/blank_image.png', thumb_file)

    docstring, short_desc, end_row = extract_docstring(example_file)

    # Depending on whether we have one or more figures, we're using a
    # horizontal list or a single rst call to 'image'.
    if len(figure_list) == 1:
        figure_name = figure_list[0]
        image_list = SINGLE_IMAGE % figure_name.lstrip('/')
    else:
        image_list = HLIST_HEADER
        for figure_name in figure_list:
            image_list += HLIST_IMAGE_TEMPLATE % figure_name.lstrip('/')

    f = open(os.path.join(target_dir, fname[:-2] + 'rst'), 'w')
    f.write(this_template % locals())
    f.flush()
Esempio n. 33
0
def generate_file_rst(fname, target_dir, src_dir, plot_gallery):
    """ Generate the rst file for a given example.
    """
    base_image_name = os.path.splitext(fname)[0]
    image_fname = '%s_%%s.png' % base_image_name

    this_template = rst_template
    last_dir = os.path.split(src_dir)[-1]
    # to avoid leading . in file names, and wrong names in links
    if last_dir == '.' or last_dir == 'examples':
        last_dir = ''
    else:
        last_dir += '_'
    short_fname = last_dir + fname
    src_file = os.path.join(src_dir, fname)
    example_file = os.path.join(target_dir, fname)
    shutil.copyfile(src_file, example_file)

    # The following is a list containing all the figure names
    figure_list = []

    image_dir = os.path.join(target_dir, 'images')
    thumb_dir = os.path.join(image_dir, 'thumb')
    if not os.path.exists(image_dir):
        os.makedirs(image_dir)
    if not os.path.exists(thumb_dir):
        os.makedirs(thumb_dir)
    image_path = os.path.join(image_dir, image_fname)
    stdout_path = os.path.join(image_dir,
                               'stdout_%s.txt' % base_image_name)
    time_path = os.path.join(image_dir,
                               'time_%s.txt' % base_image_name)
    thumb_file = os.path.join(thumb_dir, fname[:-3] + '.png')
    time_elapsed = 0
    if plot_gallery and fname.startswith('plot'):
        # generate the plot as png image if file name
        # starts with plot and if it is more recent than an
        # existing image.
        first_image_file = image_path % 1
        if os.path.exists(stdout_path):
            stdout = open(stdout_path).read()
        else:
            stdout = ''
        if os.path.exists(time_path):
            time_elapsed = float(open(time_path).read())

        if (not os.path.exists(first_image_file) or
                os.stat(first_image_file).st_mtime <=
                                    os.stat(src_file).st_mtime):
            # We need to execute the code
            print 'plotting %s' % fname
            t0 = time()
            import matplotlib.pyplot as plt
            plt.close('all')

            try:
                from mayavi import mlab
            except Exception, e:
                from enthought.mayavi import mlab
            mlab.close(all=True)

            cwd = os.getcwd()
            try:
                # First CD in the original example dir, so that any file
                # created by the example get created in this directory
                orig_stdout = sys.stdout
                os.chdir(os.path.dirname(src_file))
                my_buffer = StringIO()
                my_stdout = Tee(sys.stdout, my_buffer)
                sys.stdout = my_stdout
                my_globals = {'pl': plt}
                execfile(os.path.basename(src_file), my_globals)
                time_elapsed = time() - t0
                sys.stdout = orig_stdout
                my_stdout = my_buffer.getvalue()
                if '__doc__' in my_globals:
                    # The __doc__ is often printed in the example, we
                    # don't with to echo it
                    my_stdout = my_stdout.replace(
                                            my_globals['__doc__'],
                                            '')
                my_stdout = my_stdout.strip()
                if my_stdout:
                    output_lines = my_stdout.split('\n')
                    if len(output_lines) > MAX_NB_LINES_STDOUT:
                        output_lines = output_lines[:MAX_NB_LINES_STDOUT]
                        output_lines.append('...')
                    stdout = '**Script output**::\n\n  %s\n\n' % (
                      '\n  '.join(output_lines))
                open(stdout_path, 'w').write(stdout)
                open(time_path, 'w').write('%f' % time_elapsed)
                os.chdir(cwd)

                # In order to save every figure we have two solutions :
                # * iterate from 1 to infinity and call plt.fignum_exists(n)
                #   (this requires the figures to be numbered
                #    incrementally: 1, 2, 3 and not 1, 2, 5)
                # * iterate over [fig_mngr.num for fig_mngr in
                #   matplotlib._pylab_helpers.Gcf.get_all_fig_managers()]
                last_fig_num = 0
                for fig_num in (fig_mngr.num for fig_mngr in
                        matplotlib._pylab_helpers.Gcf.get_all_fig_managers()):
                    # Set the fig_num figure as the current figure as we can't
                    # save a figure that's not the current figure.
                    plt.figure(fig_num)
                    facecolor = plt.gcf().get_facecolor() # hack to keep black bg
                    if facecolor == (0.0, 0.0, 0.0, 1.0):
                        plt.savefig(image_path % fig_num, facecolor='black')
                    else:
                        plt.savefig(image_path % fig_num)
                    figure_list.append(image_fname % fig_num)
                    last_fig_num = fig_num

                e = mlab.get_engine()
                for scene in e.scenes:
                    last_fig_num += 1
                    mlab.savefig(image_path % last_fig_num)
                    figure_list.append(image_fname % last_fig_num)
                    mlab.close(scene)

            except:
                print 80 * '_'
                print '%s is not compiling:' % fname
                traceback.print_exc()
                print 80 * '_'
            finally:
                os.chdir(cwd)
                sys.stdout = orig_stdout

            print " - time elapsed : %.2g sec" % time_elapsed
        else:
            figure_list = [f[len(image_dir):]
                            for f in glob.glob(image_path % '[1-9]')]
                            #for f in glob.glob(image_path % '*')]

        # generate thumb file
        this_template = plot_rst_template
        from matplotlib import image
        if os.path.exists(first_image_file):
            image.thumbnail(first_image_file, thumb_file, 0.2)
Esempio n. 34
0
def generate_file_rst(fname, target_dir, src_dir, plot_gallery):
    """ Generate the rst file for a given example.
    """
    base_image_name = os.path.splitext(fname)[0]
    image_fname = '%s_%%s.png' % base_image_name
    root_image_fname = 'root_%s_%%s.png' % base_image_name
    root_fig_num = 1

    this_template = rst_template
    last_dir = os.path.split(src_dir)[-1]
    # to avoid leading . in file names, and wrong names in links
    if last_dir == '.' or last_dir == 'examples':
        last_dir = ''
    else:
        last_dir += '_'
    short_fname = last_dir + fname
    src_file = os.path.join(src_dir, fname)
    example_file = os.path.join(target_dir, fname)
    shutil.copyfile(src_file, example_file)

    # The following is a list containing all the figure names
    figure_list = []

    image_dir = os.path.join(target_dir, 'images')
    thumb_dir = os.path.join(image_dir, 'thumb')
    if not os.path.exists(image_dir):
        os.makedirs(image_dir)
    if not os.path.exists(thumb_dir):
        os.makedirs(thumb_dir)
    image_path = os.path.join(image_dir, image_fname)
    root_image_path = os.path.join(image_dir, root_image_fname)

    stdout_path = os.path.join(image_dir,
                               'stdout_%s.txt' % base_image_name)
    time_path = os.path.join(image_dir,
                               'time_%s.txt' % base_image_name)
    thumb_file = os.path.join(thumb_dir, fname[:-3] + '.png')
    time_elapsed = 0
    if plot_gallery and fname.startswith('plot'):
        # generate the plot as png image if file name
        # starts with plot and if it is more recent than an
        # existing image.
        first_image_file = image_path % 1
        first_root_image_file = root_image_path % 1
        if os.path.exists(stdout_path):
            stdout = open(stdout_path).read()
        else:
            stdout = ''
        if os.path.exists(time_path):
            time_elapsed = float(open(time_path).read())

        if (not os.path.exists(first_image_file) or
            not os.path.exists(first_root_image_file) or
                os.stat(first_image_file).st_mtime <=
                                    os.stat(src_file).st_mtime):
            # We need to execute the code
            print('plotting %s' % fname)
            t0 = time()
            import matplotlib.pyplot as plt
            plt.close('all')
            cwd = os.getcwd()
            try:
                # First CD in the original example dir, so that any file
                # created by the example get created in this directory
                orig_stdout = sys.stdout
                os.chdir(os.path.dirname(src_file))
                my_buffer = StringIO()
                my_stdout = Tee(sys.stdout, my_buffer)
                sys.stdout = my_stdout
                my_globals = {'pl': plt}
                execfile(os.path.basename(src_file), my_globals)
                time_elapsed = time() - t0
                sys.stdout = orig_stdout
                my_stdout = my_buffer.getvalue()
                if '__doc__' in my_globals:
                    # The __doc__ is often printed in the example, we
                    # don't with to echo it
                    my_stdout = my_stdout.replace(
                                            my_globals['__doc__'],
                                            '')
                my_stdout = my_stdout.strip()
                if my_stdout:
                    stdout = '**Script output**::\n\n  %s\n\n' % (
                        '\n  '.join(my_stdout.split('\n')))
                open(stdout_path, 'w').write(stdout)
                open(time_path, 'w').write('%f' % time_elapsed)
                os.chdir(cwd)

                # In order to save every figure we have two solutions :
                # * iterate from 1 to infinity and call plt.fignum_exists(n)
                #   (this requires the figures to be numbered
                #    incrementally: 1, 2, 3 and not 1, 2, 5)
                # * iterate over [fig_mngr.num for fig_mngr in
                #   matplotlib._pylab_helpers.Gcf.get_all_fig_managers()]
                for fig_num in (fig_mngr.num for fig_mngr in
                        matplotlib._pylab_helpers.Gcf.get_all_fig_managers()):
                    # Set the fig_num figure as the current figure as we can't
                    # save a figure that's not the current figure.
                    plt.figure(fig_num)
                    plt.savefig(image_path % fig_num)
                    figure_list.append(image_fname % fig_num)
                for canvas in ROOT.gROOT.GetListOfCanvases():
                    maybe_root_filename = os.path.join(os.path.dirname(src_file), canvas.name)
                    if os.path.isfile(maybe_root_filename):
                        os.rename(maybe_root_filename, os.path.join(image_dir, canvas.name))
                        figure_list.append(canvas.name)
                        canvas.Close()
                    else:
                        canvas.SaveAs(root_image_path % root_fig_num)
                        canvas.Close()
                        figure_list.append(root_image_fname % root_fig_num)
                        root_fig_num += 1
            except:
                print(80 * '_')
                print('%s is not compiling:' % fname)
                traceback.print_exc()
                print(80 * '_')
            finally:
                os.chdir(cwd)
                sys.stdout = orig_stdout

            print(" - time elapsed : %.2g sec" % time_elapsed)
        else:
            figure_list = [f[len(image_dir):]
                            for f in glob.glob(image_path % '[1-9]')]
                            #for f in glob.glob(image_path % '*')]

        # generate thumb file
        this_template = plot_rst_template
        from matplotlib import image
        if os.path.exists(first_image_file):
            image.thumbnail(first_image_file, thumb_file, 0.2)
        elif os.path.exists(first_root_image_file):
            image.thumbnail(first_root_image_file, thumb_file, 0.2)

    if not os.path.exists(thumb_file):
        # create something not to replace the thumbnail
        shutil.copy('images/blank_image.png', thumb_file)

    docstring, short_desc, end_row = extract_docstring(example_file)

    # Depending on whether we have one or more figures, we're using a
    # horizontal list or a single rst call to 'image'.
    if len(figure_list) == 1:
        figure_name = figure_list[0]
        image_list = SINGLE_IMAGE % figure_name.lstrip('/')
    else:
        image_list = HLIST_HEADER
        for figure_name in figure_list:
            image_list += HLIST_IMAGE_TEMPLATE % figure_name.lstrip('/')

    f = open(os.path.join(target_dir, fname[:-2] + 'rst'), 'w')
    f.write(this_template % locals())
    f.flush()
Esempio n. 35
0
def makefig(fullpath, outdir):
    """
    run a pyplot script<t and save the low and high res PNGs and a PDF in _static
    """

    fullpath = str(fullpath)  # todo, why is unicode breaking this

    print '    makefig: fullpath=%s, outdir=%s' % (fullpath, outdir)
    formats = [
        ('png', 80),
        ('hires.png', 200),
        ('pdf', 50),
    ]

    basedir, fname = os.path.split(fullpath)
    basename, ext = os.path.splitext(fname)
    all_exists = True

    if basedir != outdir:
        shutil.copyfile(fullpath, os.path.join(outdir, fname))

    # Look for single-figure output files first
    for format, dpi in formats:
        outname = os.path.join(outdir, '%s.%s' % (basename, format))
        if out_of_date(fullpath, outname):
            all_exists = False
            break

    if all_exists:
        print '    already have %s' % fullpath
        return 1

    # Then look for multi-figure output files, assuming
    # if we have some we have all...
    i = 0
    while True:
        all_exists = True
        for format, dpi in formats:
            outname = os.path.join(outdir,
                                   '%s_%02d.%s' % (basename, i, format))
            if out_of_date(fullpath, outname):
                all_exists = False
                break
        if all_exists:
            i += 1
        else:
            break

    if i != 0:
        print '    already have %d figures for %s' % (i, fullpath)
        return i

    # We didn't find the files, so build them

    print '    building %s' % fullpath
    plt.close('all')  # we need to clear between runs
    matplotlib.rcdefaults()
    # Set a figure size that doesn't overflow typical browser windows
    matplotlib.rcParams['figure.figsize'] = (5.5, 4.5)

    try:
        runfile(fullpath)
    except:
        s = cbook.exception_to_str("Exception running plot %s" % fullpath)
        warnings.warn(s)
        return 0

    fig_managers = _pylab_helpers.Gcf.get_all_fig_managers()
    for i, figman in enumerate(fig_managers):
        for format, dpi in formats:
            if len(fig_managers) == 1:
                outname = basename
            else:
                outname = "%s_%02d" % (basename, i)
            outpath = os.path.join(outdir, '%s.%s' % (outname, format))
            try:
                figman.canvas.figure.savefig(outpath, dpi=dpi)
            except:
                s = cbook.exception_to_str("Exception running plot %s" %
                                           fullpath)
                warnings.warn(s)
                return 0

            if format == 'png':
                thumbdir = os.path.join(outdir, 'thumbnails')
                if not os.path.exists(thumbdir):
                    os.makedirs(thumbdir)
                thumbfile = str('%s.png' % os.path.join(thumbdir, outname))
                if not os.path.exists(thumbfile):
                    figthumb = image.thumbnail(str(outpath),
                                               str(thumbfile),
                                               scale=0.3)
                    print '    makefig: saved thumbnail of %s to %s' % (
                        outpath, thumbfile)

    return len(fig_managers)
Esempio n. 36
0
def gen_gallery(app, doctree):
    if app.builder.name != 'html':
        return

    outdir = app.builder.outdir

    rootdir = os.path.join('plot_directive','example_code')

    # images we want to skip for the gallery because they are an unusual
    # size that doesn't layout well in a table, or because they may be
    # redundant with other images or uninteresting
    skips = set([
        'mathtext_examples',
        'matshow_02',
        'matshow_03',
        'matplotlib_icon',
        ])

    data = []
    thumbnails = {}

    for subdir in ('graphics', ):
        origdir = os.path.join(os.path.dirname(outdir), rootdir, subdir)
        thumbdir = os.path.join(outdir, rootdir, subdir, 'thumbnails')
        if not os.path.exists(thumbdir):
            os.makedirs(thumbdir)
        for filename in sorted(glob.glob(os.path.join(origdir, '*.png'))):
            if filename.endswith("hires.png"):
                continue

            path, filename = os.path.split(filename)
            basename, ext = os.path.splitext(filename)
            if basename in skips:
                continue

            # Create thumbnails based on images in tmpdir, and place
            # them within the build tree
            orig_path = str(os.path.join(origdir, filename))
            thumb_path = str(os.path.join(thumbdir, filename))
            if out_of_date(orig_path, thumb_path) or True:
                thumbnails[orig_path] = thumb_path

            m = multiimage.match(basename)
            if m is None:
                pyfile = '%s.py'%basename
            else:
                basename = m.group(1)
                pyfile = '%s.py'%basename

            data.append((subdir, basename,
                         os.path.join(rootdir, subdir, 'thumbnails', filename)))

    link_template = """\
    <a href="%(href)s"><img src="%(thumb_file)s" border="0" alt="%(alternative_text)s"/></a>
    """

    random_image_content_template = '''
// This file was automatically generated by gen_gallery.py & should not be modified directly.

images = new Array();

%s

'''

    random_image_template = "['%(thumbfile)s', '%(full_image)s', '%(link)s', 'Iris examples.'];"
    random_image_join = 'images[%s] = %s'

    if len(data) == 0:
        warnings.warn("No thumbnails were found")
        return

    rows = []
    random_image = []
    for (subdir, basename, thumbfile) in data:
        if thumbfile is not None:
            link = 'examples/%s/%s.html#%s'%(subdir, basename, os.path.splitext(os.path.basename(thumbfile))[0].replace('_', '-'))
            rows.append(link_template % {'href': link, 'thumb_file': thumbfile, 'alternative_text': basename})
            random_image.append(random_image_template % {'link':link, 'thumbfile':thumbfile, 'basename':basename, 'full_image':'_images/' + os.path.basename(thumbfile)} )

    random_image_content = random_image_content_template % '\n'.join([random_image_join % (i, line) for i, line in enumerate(random_image)])
    random_image_script_path = os.path.join(app.builder.srcdir, '_static', 'random_image.js')
    file(random_image_script_path, 'w').write(random_image_content)


    # Only write out the file if the contents have actually changed.
    # Otherwise, this triggers a full rebuild of the docs
    content = template%'\n'.join(rows)
    gallery_path = os.path.join(app.builder.srcdir, '_templates', 'gallery.html')
    if os.path.exists(gallery_path):
        fh = file(gallery_path, 'r')
        regenerate = fh.read() != content
        fh.close()
    else:
        regenerate = True
    if regenerate:
        fh = file(gallery_path, 'w')
        fh.write(content)
        fh.close()

    try:
        import multiprocessing
        app.builder.info("generating thumbnails... ", nonl=True)
        pool = multiprocessing.Pool()
        pool.map(make_thumbnail, thumbnails.iteritems())
        pool.close()
        pool.join()
        app.builder.info("done")

    except ImportError:
        for key in app.builder.status_iterator(
            thumbnails.iterkeys(), "generating thumbnails... ",
            length=len(thumbnails)):
            image.thumbnail(key, thumbnails[key], 0.3)
Esempio n. 37
0
def gen_gallery(app, doctree):
    if app.builder.name != "html":
        return

    htmldir = app.builder.outdir
    srcdir = app.builder.srcdir

    # From config
    rootdir = app.config.gen_gallery_root
    paths = app.config.gen_gallery_paths
    skips = set(app.config.gen_gallery_skips)
    nmax = app.config.gen_gallery_nmax

    # Inits
    thumbnails = {}
    rows = []
    toc_rows = []

    # Loop on subdirs
    for subdir, conf in paths.items():

        # Check dirs
        figdir = conf.get("figdir", subdir)
        rstdir = conf.get("rstdir", figdir)
        pngdir = os.path.join(srcdir, figdir)
        thumbdir = os.path.join(htmldir, rootdir, subdir, "thumbnails")
        if not os.path.exists(thumbdir):
            os.makedirs(thumbdir)

        # Headers
        title = conf.get("title", subdir.title())
        rows.append(header_template % locals())
        toc_rows.append(toc_template % locals())

        # Loop on png files
        data = []
        basenames = []
        for filename in sorted(glob.glob(os.path.join(pngdir, "*.png"))):
            # if filename.endswith("hires.png"):
            # continue

            # Basename
            path, filename = os.path.split(filename)
            basename, ext = os.path.splitext(filename)
            basename = basename.replace("-", ".")
            if basename in skips:
                continue
            m = multiimage_match(basename)
            if m is not None:
                basename = m.group(1)

            # Check if rst file exists
            if not os.path.exists(os.path.join(srcdir, rstdir, basename + ".rst")):

                # Ok loof for a rst file whose basename matches end for png basename
                rstfiles = filter(
                    lambda fname: basename.endswith(os.path.splitext(os.path.basename(fname))[0]),
                    glob.glob(os.path.join(srcdir, rstdir, "*.rst")),
                )
                if not rstfiles:
                    continue
                basename = os.path.splitext(os.path.basename(rstfiles[0]))[0]

            # Check nmax
            if len([True for bn in basenames if bn == basename]) > nmax:
                continue

            # Is thumbnail out of date?
            orig_path = str(os.path.join(pngdir, filename))
            thumb_path = str(os.path.join(thumbdir, filename))
            if out_of_date(orig_path, thumb_path):
                thumbnails[orig_path] = thumb_path

            # Register
            basenames.append(basename)
            data.append((rstdir, basename, os.path.join(rootdir, subdir, "thumbnails", filename)))

        # Append html rows
        for (rstdir, basename, thumbfile) in data:
            if thumbfile is not None:
                link = "%s/%s.html" % (rstdir, basename)
                rows.append(link_template % locals())

        if len(data) == 0:
            app.warn("no thumbnails were found in %s" % subdir)

        # Close out the <div> opened up at the top of this loop
        rows.append("</div>")

    content = template % ("\n".join(toc_rows), "\n".join(rows))

    # Only write out the file if the contents have actually changed.
    # Otherwise, this triggers a full rebuild of the docs
    gallery_path = os.path.join(srcdir, "templates", "gallery.html")
    if os.path.exists(gallery_path):
        fh = open(gallery_path, "r", "utf8")
        regenerate = fh.read() != content
        fh.close()
    else:
        regenerate = True
    if regenerate:
        fh = open(gallery_path, "w", "utf8")
        fh.write(content)
        fh.close()

    # Inform about thumbnail generation
    old = rcParams["image.origin"]
    rcParams["image.origin"] = "upper"
    for key in app.builder.status_iterator(thumbnails.iterkeys(), "generating thumbnails... ", length=len(thumbnails)):
        image.thumbnail(key, thumbnails[key], 0.3)
    rcParams["image.origin"] = old
Esempio n. 38
0
def gen_gallery(app, doctree):
    if app.builder.name != 'html':
        return

    outdir = app.builder.outdir
    rootdir = 'examples'

    # Images we want to skip for the gallery because they are an unusual
    # size that doesn't layout well in a table, or because they may be
    # redundant with other images or uninteresting.
    skips = set([
        'mathtext_examples',
        'matshow_02',
        'matshow_03',
        'matplotlib_icon',
    ])

    thumbnails = {}
    rows = []
    random_image = []
    toc_rows = []

    link_template = ('<a href="{href}">'
                     '<img src="{thumb_file}" border="0"'
                     ' alt="{alternative_text}"/>'
                     '</a>')

    header_template = ('<div class="section" id="{}">'
                       '<h4>{}'
                       '<a class="headerlink" href="#{}"'
                       ' title="Permalink to this headline">&para;</a>'
                       '</h4>')

    toc_template = ('<li>'
                    '<a class="reference internal" href="#{}">{}</a>'
                    '</li>')

    random_image_content_template = '''
// This file was automatically generated by gen_gallery.py & should not be
// modified directly.

images = new Array();

{}

'''

    random_image_template = "['{thumbfile}', '{full_image}', '{link}'];"
    random_image_join = 'images[{}] = {}'

    dirs = ('General', 'Meteorology', 'Oceanography')

    for subdir in dirs:
        rows.append(header_template.format(subdir, subdir, subdir))
        toc_rows.append(toc_template.format(subdir, subdir))

        origdir = os.path.join(os.path.dirname(outdir), rootdir, subdir)
        if not os.path.exists(origdir):
            origdir = os.path.join(os.path.dirname(outdir), 'plot_directive',
                                   rootdir, subdir)
        thumbdir = os.path.join(outdir, rootdir, subdir, 'thumbnails')
        if not os.path.exists(thumbdir):
            os.makedirs(thumbdir)

        data = []

        for filename in sorted(glob.glob(os.path.join(origdir, '*.png'))):
            if filename.endswith('hires.png'):
                continue

            path, filename = os.path.split(filename)
            basename, ext = os.path.splitext(filename)
            if basename in skips:
                continue

            # Create thumbnails based on images in tmpdir, and place them
            # within the build tree.
            orig_path = str(os.path.join(origdir, filename))
            thumb_path = str(os.path.join(thumbdir, filename))
            if out_of_date(orig_path, thumb_path) or True:
                thumbnails[orig_path] = thumb_path

            m = multiimage.match(basename)
            if m is not None:
                basename = m.group(1)

            data.append((subdir, basename,
                         os.path.join(rootdir, subdir, 'thumbnails',
                                      filename)))

        for (subdir, basename, thumbfile) in data:
            if thumbfile is not None:
                anchor = os.path.basename(thumbfile)
                anchor = os.path.splitext(anchor)[0].replace('_', '-')
                link = 'examples/{}/{}.html#{}'.format(subdir, basename,
                                                       anchor)
                rows.append(
                    link_template.format(href=link,
                                         thumb_file=thumbfile,
                                         alternative_text=basename))
                random_image.append(
                    random_image_template.format(link=link,
                                                 thumbfile=thumbfile,
                                                 basename=basename,
                                                 full_image='_images/' +
                                                 os.path.basename(thumbfile)))

        if len(data) == 0:
            warnings.warn('No thumbnails were found in {}'.format(subdir))

        # Close out the <div> opened up at the top of this loop.
        rows.append('</div>')

    # Generate JS list of images for front page.
    random_image_content = '\n'.join([
        random_image_join.format(i, line)
        for i, line in enumerate(random_image)
    ])
    random_image_content = random_image_content_template.format(
        random_image_content)
    random_image_script_path = os.path.join(app.builder.srcdir, '_static',
                                            'random_image.js')
    with open(random_image_script_path, 'w') as fh:
        fh.write(random_image_content)

    content = template.format('\n'.join(toc_rows), '\n'.join(rows))

    # Only write out the file if the contents have actually changed.
    # Otherwise, this triggers a full rebuild of the docs.

    gallery_path = os.path.join(app.builder.srcdir, '_templates',
                                'gallery.html')
    if os.path.exists(gallery_path):
        with open(gallery_path, 'r') as fh:
            regenerate = fh.read() != content
    else:
        regenerate = True
    if regenerate:
        with open(gallery_path, 'w') as fh:
            fh.write(content)

    for key in app.builder.status_iterator(thumbnails,
                                           'generating thumbnails... ',
                                           length=len(thumbnails)):
        image.thumbnail(key, thumbnails[key], 0.3)
Esempio n. 39
0
def gen_gallery(app, doctree):
    if app.builder.name != 'html':
        return

    outdir = app.builder.outdir
    rootdir = 'plot_directive/amuse_examples'

    # images we want to skip for the gallery because they are an unusual
    # size that doesn't layout well in a table, or because they may be
    # redundant with other images or uninteresting
    skips = set([])

    data = []
    thumbnails = {}

    for subdir in ('simple', ):
        origdir = os.path.join('build', rootdir, subdir)
        thumbdir = os.path.join(outdir, rootdir, subdir, 'thumbnails')
        if not os.path.exists(thumbdir):
            os.makedirs(thumbdir)

        for filename in sorted(glob.glob(os.path.join(origdir, '*.png'))):
            if filename.endswith("hires.png"):
                continue

            path, filename = os.path.split(filename)
            basename, ext = os.path.splitext(filename)
            if basename in skips:
                continue

            # Create thumbnails based on images in tmpdir, and place
            # them within the build tree
            orig_path = str(os.path.join(origdir, filename))
            thumb_path = str(os.path.join(thumbdir, filename))
            if out_of_date(orig_path, thumb_path) or True:
                thumbnails[orig_path] = thumb_path

            m = multiimage.match(basename)
            if m is None:
                pyfile = '%s.py' % basename
            else:
                basename = m.group(1)
                pyfile = '%s.py' % basename

            data.append((subdir, basename,
                         os.path.join(rootdir, subdir, 'thumbnails',
                                      filename)))

    link_template = """\
    <a href="%s"><img src="%s" border="0" alt="%s"/></a>
    """

    if len(data) == 0:
        warnings.warn("No thumbnails were found")

    rows = []
    for (subdir, basename, thumbfile) in data:
        if thumbfile is not None:
            link = 'examples/%s/%s.html' % (subdir, basename)
            rows.append(link_template % (link, thumbfile, basename))

    # Only write out the file if the contents have actually changed.
    # Otherwise, this triggers a full rebuild of the docs
    content = template % '\n'.join(rows)
    gallery_path = os.path.join(app.builder.srcdir, '_templates',
                                'gallery.html')
    if os.path.exists(gallery_path):
        fh = file(gallery_path, 'r')
        regenerate = fh.read() != content
        fh.close()
    else:
        regenerate = True
    if regenerate:
        fh = file(gallery_path, 'w')
        fh.write(content)
        fh.close()

    if len(data) > 0:
        try:
            import multiprocessing
            app.builder.info("generating thumbnails... ", nonl=True)
            print list(thumbnails.iteritems())
            pool = multiprocessing.Pool()
            pool.map(make_thumbnail, thumbnails.iteritems())
            pool.close()
            pool.join()
            app.builder.info("done")

        except ImportError:
            for key in app.builder.status_iterator(thumbnails.iterkeys(),
                                                   "generating thumbnails... ",
                                                   length=len(thumbnails)):
                image.thumbnail(key, thumbnails[key], 0.3)
Esempio n. 40
0
def gen_gallery(app, doctree):
    if app.builder.name != 'html':
        return

    outdir = app.builder.outdir
    rootdir = 'plot_directive/mpl_examples'

    # images we want to skip for the gallery because they are an unusual
    # size that doesn't layout well in a table, or because they may be
    # redundant with other images or uninteresting
    skips = set([
        'mathtext_examples',
        'matshow_02',
        'matshow_03',
        'matplotlib_icon',
    ])

    thumbnails = {}
    rows = []
    toc_rows = []

    link_template = """\
    <a href="%s"><img src="%s" border="0" alt="%s"/></a>
    """

    header_template = """<div class="section" id="%s">\
    <h4>%s<a class="headerlink" href="#%s" title="Permalink to this headline">¶</a></h4>"""

    toc_template = """\
    <li><a class="reference internal" href="#%s">%s</a></li>"""

    dirs = ('api', 'pylab_examples', 'mplot3d', 'widgets', 'axes_grid')

    for subdir in dirs:
        rows.append(header_template % (subdir, subdir, subdir))
        toc_rows.append(toc_template % (subdir, subdir))

        origdir = os.path.join('build', rootdir, subdir)
        thumbdir = os.path.join(outdir, rootdir, subdir, 'thumbnails')
        if not os.path.exists(thumbdir):
            os.makedirs(thumbdir)

        data = []

        for filename in sorted(glob.glob(os.path.join(origdir, '*.png'))):
            if filename.endswith("hires.png"):
                continue

            path, filename = os.path.split(filename)
            basename, ext = os.path.splitext(filename)
            if basename in skips:
                continue

            # Create thumbnails based on images in tmpdir, and place
            # them within the build tree
            orig_path = str(os.path.join(origdir, filename))
            thumb_path = str(os.path.join(thumbdir, filename))
            if out_of_date(orig_path, thumb_path) or True:
                thumbnails[orig_path] = thumb_path

            m = multiimage.match(basename)
            if m is not None:
                basename = m.group(1)

            data.append((subdir, basename,
                         os.path.join(rootdir, subdir, 'thumbnails',
                                      filename)))

        for (subdir, basename, thumbfile) in data:
            if thumbfile is not None:
                link = 'examples/%s/%s.html' % (subdir, basename)
                rows.append(link_template % (link, thumbfile, basename))

        if len(data) == 0:
            warnings.warn("No thumbnails were found in %s" % subdir)

        # Close out the <div> opened up at the top of this loop
        rows.append("</div>")

    content = template % ('\n'.join(toc_rows), '\n'.join(rows))

    # Only write out the file if the contents have actually changed.
    # Otherwise, this triggers a full rebuild of the docs

    gallery_path = os.path.join(app.builder.srcdir, '_templates',
                                'gallery.html')
    if os.path.exists(gallery_path):
        fh = open(gallery_path, 'r')
        regenerate = fh.read() != content
        fh.close()
    else:
        regenerate = True
    if regenerate:
        fh = open(gallery_path, 'w')
        fh.write(content)
        fh.close()

    for key in app.builder.status_iterator(iter(thumbnails.keys()),
                                           "generating thumbnails... ",
                                           length=len(thumbnails)):
        image.thumbnail(key, thumbnails[key], 0.3)
Esempio n. 41
0
def gen_gallery(app, doctree):
    if app.builder.name != 'html':
        return

    outdir = app.builder.outdir
    rootdir = 'plot_directive/mpl_examples'

    # images we want to skip for the gallery because they are an unusual
    # size that doesn't layout well in a table, or because they may be
    # redundant with other images or uninteresting
    skips = set([
        'mathtext_examples',
        'matshow_02',
        'matshow_03',
        'matplotlib_icon',
        ])

    data = []
    thumbnails = {}

    for subdir in ('api', 'pylab_examples', 'mplot3d', 'widgets', 'axes_grid' ):
        origdir = os.path.join('build', rootdir, subdir)
        thumbdir = os.path.join(outdir, rootdir, subdir, 'thumbnails')
        if not os.path.exists(thumbdir):
            os.makedirs(thumbdir)

        for filename in sorted(glob.glob(os.path.join(origdir, '*.png'))):
            if filename.endswith("hires.png"):
                continue

            path, filename = os.path.split(filename)
            basename, ext = os.path.splitext(filename)
            if basename in skips:
                continue

            # Create thumbnails based on images in tmpdir, and place
            # them within the build tree
            orig_path = str(os.path.join(origdir, filename))
            thumb_path = str(os.path.join(thumbdir, filename))
            if out_of_date(orig_path, thumb_path) or True:
                thumbnails[orig_path] = thumb_path

            m = multiimage.match(basename)
            if m is not None:
                basename = m.group(1)

            data.append((subdir, basename,
                         os.path.join(rootdir, subdir, 'thumbnails', filename)))

    link_template = """\
    <a href="%s"><img src="%s" border="0" alt="%s"/></a>
    """

    if len(data) == 0:
        warnings.warn("No thumbnails were found")

    rows = []
    for (subdir, basename, thumbfile) in data:
        if thumbfile is not None:
            link = 'examples/%s/%s.html'%(subdir, basename)
            rows.append(link_template%(link, thumbfile, basename))

    # Only write out the file if the contents have actually changed.
    # Otherwise, this triggers a full rebuild of the docs
    content = template%'\n'.join(rows)
    gallery_path = os.path.join(app.builder.srcdir, '_templates', 'gallery.html')
    if os.path.exists(gallery_path):
        fh = file(gallery_path, 'r')
        regenerate = fh.read() != content
        fh.close()
    else:
        regenerate = True
    if regenerate:
        fh = file(gallery_path, 'w')
        fh.write(content)
        fh.close()

    for key in app.builder.status_iterator(
        thumbnails.iterkeys(), "generating thumbnails... ",
        length=len(thumbnails)):
        image.thumbnail(key, thumbnails[key], 0.3)
Esempio n. 42
0
def gen_gallery(app, doctree):
    if app.builder.name not in ('html', 'htmlhelp'):
        return

    outdir = app.builder.outdir
    rootdir = 'plot_directive/mpl_examples'

    example_sections = list(app.builder.config.mpl_example_sections)
    for i, (subdir, title) in enumerate(example_sections):
        if subdir in exclude_example_sections:
            example_sections.pop(i)

    # images we want to skip for the gallery because they are an unusual
    # size that doesn't layout well in a table, or because they may be
    # redundant with other images or uninteresting
    skips = set([
        'mathtext_examples',
        'matshow_02',
        'matshow_03',
        'matplotlib_icon',
    ])

    thumbnails = {}
    rows = []
    toc_rows = []

    for subdir, title in example_sections:
        rows.append(header_template.format(title=title, section=subdir))
        toc_rows.append(toc_template.format(title=title, section=subdir))

        origdir = os.path.join('build', rootdir, subdir)
        thumbdir = os.path.join(outdir, rootdir, subdir, 'thumbnails')
        if not os.path.exists(thumbdir):
            os.makedirs(thumbdir)

        data = []

        for filename in sorted(glob.glob(os.path.join(origdir, '*.png'))):
            if filename.endswith("hires.png"):
                continue

            path, filename = os.path.split(filename)
            basename, ext = os.path.splitext(filename)
            if basename in skips:
                continue

            # Create thumbnails based on images in tmpdir, and place
            # them within the build tree
            orig_path = str(os.path.join(origdir, filename))
            thumb_path = str(os.path.join(thumbdir, filename))
            if out_of_date(orig_path, thumb_path) or True:
                thumbnails[orig_path] = thumb_path

            m = multiimage.match(basename)
            if m is not None:
                basename = m.group(1)

            data.append((subdir, basename,
                         os.path.join(rootdir, subdir, 'thumbnails',
                                      filename)))

        for (subdir, basename, thumbfile) in data:
            if thumbfile is not None:
                link = 'examples/%s/%s.html' % (subdir, basename)
                rows.append(
                    link_template.format(link=link,
                                         thumb=thumbfile,
                                         basename=basename,
                                         title=basename))

        if len(data) == 0:
            warnings.warn("No thumbnails were found in %s" % subdir)

        # Close out the <div> opened up at the top of this loop
        rows.append("</div>")

    content = gallery_template.format(toc='\n'.join(toc_rows),
                                      gallery='\n'.join(rows))

    # Only write out the file if the contents have actually changed.
    # Otherwise, this triggers a full rebuild of the docs

    gallery_path = os.path.join(app.builder.srcdir, '_templates',
                                'gallery.html')
    if os.path.exists(gallery_path):
        fh = open(gallery_path, 'r')
        regenerate = fh.read() != content
        fh.close()
    else:
        regenerate = True

    if regenerate:
        fh = open(gallery_path, 'w')
        fh.write(content)
        fh.close()

    for key in app.builder.status_iterator(iter(thumbnails.keys()),
                                           "generating thumbnails... ",
                                           length=len(thumbnails)):
        if out_of_date(key, thumbnails[key]):
            image.thumbnail(key, thumbnails[key], 0.3)
Esempio n. 43
0
def write_example(src_name, src_dir, rst_dir, cfg):
    """Write rst file from a given python example.

    Parameters
    ----------
    src_name : str
        Name of example file.
    src_dir : 'str'
        Source directory for python examples.
    rst_dir : 'str'
        Destination directory for rst files generated from python examples.
    cfg : config object
        Sphinx config object created by Sphinx.
    """
    last_dir = src_dir.psplit()[-1]
    # to avoid leading . in file names, and wrong names in links
    if last_dir == "." or last_dir == "examples":
        last_dir = Path("")
    else:
        last_dir += "_"

    src_path = src_dir.pjoin(src_name)
    example_file = rst_dir.pjoin(src_name)
    shutil.copyfile(src_path, example_file)

    image_dir = rst_dir.pjoin("images")
    thumb_dir = image_dir.pjoin("thumb")
    image_dir.makedirs()
    thumb_dir.makedirs()

    base_image_name = os.path.splitext(src_name)[0]
    image_path = image_dir.pjoin(base_image_name + "_{0}.png")

    basename, py_ext = os.path.splitext(src_name)
    rst_path = rst_dir.pjoin(basename + cfg.source_suffix)

    if _plots_are_current(src_path, image_path) and rst_path.exists:
        return

    blocks = split_code_and_text_blocks(example_file)
    if blocks[0][2].startswith("#!"):
        blocks.pop(0)  # don't add shebang line to rst file.

    rst_link = ".. _example_%s:\n\n" % (last_dir + src_name)
    figure_list, rst = process_blocks(blocks, src_path, image_path, cfg)

    has_inline_plots = any(cfg.plot2rst_plot_tag in b[2] for b in blocks)
    if has_inline_plots:
        example_rst = "".join([rst_link, rst])
    else:
        # print first block of text, display all plots, then display code.
        first_text_block = [b for b in blocks if b[0] == "text"][0]
        label, (start, end), content = first_text_block
        figure_list = save_all_figures(image_path)
        rst_blocks = [IMAGE_TEMPLATE % f.lstrip("/") for f in figure_list]

        example_rst = rst_link
        example_rst += eval(content)
        example_rst += "".join(rst_blocks)
        code_info = dict(src_name=src_name, code_start=end)
        example_rst += LITERALINCLUDE.format(**code_info)

    example_rst += CODE_LINK.format(src_name)

    f = open(rst_path, "w")
    f.write(example_rst)
    f.flush()

    thumb_path = thumb_dir.pjoin(src_name[:-3] + ".png")
    first_image_file = image_dir.pjoin(figure_list[0].lstrip("/"))
    if first_image_file.exists:
        image.thumbnail(first_image_file, thumb_path, cfg.plot2rst_thumb_scale)

    if not thumb_path.exists:
        if cfg.plot2rst_default_thumb is None:
            print "WARNING: No plots found and default thumbnail not defined."
            print "Specify 'plot2rst_default_thumb' in Sphinx config file."
        else:
            shutil.copy(cfg.plot2rst_default_thumb, thumb_path)
Esempio n. 44
0
def gen_gallery(app, doctree):
    if app.builder.name != 'html':
        return

    outdir = app.builder.outdir
    rootdir = 'plot_directive/mpl_examples'

    # images we want to skip for the gallery because they are an unusual
    # size that doesn't layout well in a table, or because they may be
    # redundant with other images or uninteresting
    skips = set([
        'mathtext_examples',
        'matshow_02',
        'matshow_03',
        'matplotlib_icon',
        ])

    print
    print "generating gallery: ",
    data = []
    for subdir in ('api', 'pylab_examples', 'widgets'):
        origdir = os.path.join('build', rootdir, subdir)
        thumbdir = os.path.join(outdir, rootdir, subdir, 'thumbnails')
        if not os.path.exists(thumbdir):
            os.makedirs(thumbdir)
        print subdir,

        for filename in sorted(glob.glob(os.path.join(origdir, '*.png'))):
            if filename.endswith("hires.png"):
                continue

            path, filename = os.path.split(filename)
            basename, ext = os.path.splitext(filename)
            if basename in skips:
                sys.stdout.write('[skipping %s]' % basename)
                sys.stdout.flush()
                continue

            # Create thumbnails based on images in tmpdir, and place
            # them within the build tree
            image.thumbnail(
                str(os.path.join(origdir, filename)),
                str(os.path.join(thumbdir, filename)),
                scale=0.3)

            m = multiimage.match(basename)
            if m is None:
                pyfile = '%s.py'%basename
            else:
                basename = m.group(1)
                pyfile = '%s.py'%basename

            data.append((subdir, basename,
                         os.path.join(rootdir, subdir, 'thumbnails', filename)))

            sys.stdout.write(".")
            sys.stdout.flush()
        print

    link_template = """\
    <a href="%s"><img src="%s" border="0" alt="%s"/></a>
    """

    if len(data) == 0:
        warnings.warn("No thumbnails were found")

    rows = []
    for (subdir, basename, thumbfile) in data:
        if thumbfile is not None:
            link = 'examples/%s/%s.html'%(subdir, basename)
            rows.append(link_template%(link, thumbfile, basename))

    fh = file(os.path.join(app.builder.srcdir, '_templates', 'gallery.html'),
              'w')
    fh.write(template%'\n'.join(rows))
    fh.close()
Esempio n. 45
0
def gen_gallery(app, doctree):
    if app.builder.name != 'html':
        return
    outdir = app.builder.outdir
    rootdir = 'plot_directive/mpl_examples'
    skips = set([
        'mathtext_examples',
        'matshow_02',
        'matshow_03',
        'matplotlib_icon',
        ])
    data = []
    thumbnails = {}
    for subdir in ('api', 'pylab_examples', 'mplot3d', 'widgets', 'axes_grid' ):
        origdir = os.path.join('build', rootdir, subdir)
        thumbdir = os.path.join(outdir, rootdir, subdir, 'thumbnails')
        if not os.path.exists(thumbdir):
            os.makedirs(thumbdir)
        for filename in sorted(glob.glob(os.path.join(origdir, '*.png'))):
            if filename.endswith("hires.png"):
                continue
            path, filename = os.path.split(filename)
            basename, ext = os.path.splitext(filename)
            if basename in skips:
                continue
            orig_path = str(os.path.join(origdir, filename))
            thumb_path = str(os.path.join(thumbdir, filename))
            if out_of_date(orig_path, thumb_path) or True:
                thumbnails[orig_path] = thumb_path
            m = multiimage.match(basename)
            if m is None:
                pyfile = '%s.py'%basename
            else:
                basename = m.group(1)
                pyfile = '%s.py'%basename
            data.append((subdir, basename,
                         os.path.join(rootdir, subdir, 'thumbnails', filename)))
    link_template = """\
    <a href="%s"><img src="%s" border="0" alt="%s"/></a>
    """
    if len(data) == 0:
        warnings.warn("No thumbnails were found")
    rows = []
    for (subdir, basename, thumbfile) in data:
        if thumbfile is not None:
            link = 'examples/%s/%s.html'%(subdir, basename)
            rows.append(link_template%(link, thumbfile, basename))
    content = template%'\n'.join(rows)
    gallery_path = os.path.join(app.builder.srcdir, '_templates', 'gallery.html')
    if os.path.exists(gallery_path):
        fh = file(gallery_path, 'r')
        regenerate = fh.read() != content
        fh.close()
    else:
        regenerate = True
    if regenerate:
        fh = file(gallery_path, 'w')
        fh.write(content)
        fh.close()
    try:
        import multiprocessing
        app.builder.info("generating thumbnails... ", nonl=True)
        pool = multiprocessing.Pool()
        pool.map(make_thumbnail, thumbnails.iteritems())
        pool.close()
        pool.join()
        app.builder.info("done")
    except ImportError:
        for key in app.builder.status_iterator(
            thumbnails.iterkeys(), "generating thumbnails... ",
            length=len(thumbnails)):
            image.thumbnail(key, thumbnails[key], 0.3)
Esempio n. 46
0
def gen_gallery(app, doctree):
    if app.builder.name not in ('html', 'htmlhelp'):
        return

    outdir = app.builder.outdir
    rootdir = 'plot_directive/mpl_examples'

    example_sections = list(app.builder.config.mpl_example_sections)
    for i, (subdir, title) in enumerate(example_sections):
        if subdir in exclude_example_sections:
            example_sections.pop(i)

    # images we want to skip for the gallery because they are an unusual
    # size that doesn't layout well in a table, or because they may be
    # redundant with other images or uninteresting
    skips = {'mathtext_examples',
             'matshow_02',
             'matshow_03',
             'matplotlib_icon'}

    thumbnails = {}
    rows = []
    toc_rows = []

    for subdir, title in example_sections:
        rows.append(header_template.format(title=title, section=subdir))
        toc_rows.append(toc_template.format(title=title, section=subdir))

        origdir = os.path.join('build', rootdir, subdir)
        thumbdir = os.path.join(outdir, rootdir, subdir, 'thumbnails')
        if not os.path.exists(thumbdir):
            os.makedirs(thumbdir)

        data = []

        for filename in sorted(glob.glob(os.path.join(origdir, '*.png'))):
            if filename.endswith("hires.png"):
                continue

            path, filename = os.path.split(filename)
            basename, ext = os.path.splitext(filename)
            if basename in skips:
                continue

            # Create thumbnails based on images in tmpdir, and place
            # them within the build tree
            orig_path = str(os.path.join(origdir, filename))
            thumb_path = str(os.path.join(thumbdir, filename))
            if out_of_date(orig_path, thumb_path) or True:
                thumbnails[orig_path] = thumb_path

            m = multiimage.match(basename)
            if m is not None:
                basename = m.group(1)

            data.append((subdir, basename,
                         os.path.join(rootdir, subdir, 'thumbnails', filename)))

        for (subdir, basename, thumbfile) in data:
            if thumbfile is not None:
                link = 'examples/%s/%s.html'%(subdir, basename)
                rows.append(link_template.format(link=link,
                                                 thumb=thumbfile,
                                                 basename=basename,
                                                 title=basename))

        if len(data) == 0:
            warnings.warn("No thumbnails were found in %s" % subdir)

        # Close out the <div> opened up at the top of this loop
        rows.append(u"</div>")

    content = gallery_template.format(toc=u'\n'.join(toc_rows),
                                      gallery=u'\n'.join(rows))

    # Only write out the file if the contents have actually changed.
    # Otherwise, this triggers a full rebuild of the docs

    gallery_path = os.path.join(app.builder.srcdir,
                                '_templates', 'gallery.html')
    if os.path.exists(gallery_path):
        with codecs.open(gallery_path, 'r', encoding='utf-8') as fh:
            regenerate = fh.read() != content
    else:
        regenerate = True

    if regenerate:
        with codecs.open(gallery_path, 'w', encoding='utf-8') as fh:
            fh.write(content)

    for key in app.builder.status_iterator(
            iter(thumbnails), "generating thumbnails... ",
            length=len(thumbnails)):
        if out_of_date(key, thumbnails[key]):
            image.thumbnail(key, thumbnails[key], 0.3)
def _do_prev(obs_id, working_dir, plane, cadc_client, stream, observable):
    """Retrieve the preview file, so that a thumbnail can be made,
    store the preview if necessary, and the thumbnail, to ad.
    Then augment the CAOM observation with the two additional artifacts.
    """
    count = 0
    gem_name = GemName(obs_id=obs_id, file_id=plane.product_id)
    preview = gem_name.prev
    if observable.rejected.is_no_preview(preview):
        logging.info(
            f'Stopping visit because no preview exists for {preview} in '
            f'observation {obs_id}.'
        )
        observable.rejected.record(mc.Rejected.NO_PREVIEW, preview)
        count += _check_for_delete(
            preview, gem_name.prev_uri, observable, plane
        )
    else:
        preview_fqn = os.path.join(working_dir, preview)
        thumb = gem_name.thumb
        thumb_fqn = os.path.join(working_dir, thumb)
        new_retrieval = False

        # Get the file - try disk first, then CADC, then Gemini.
        # Only try to retrieve from Gemini if the eventual purpose is
        # storage, though
        if not os.access(preview_fqn, 0) and cadc_client is not None:
            try:
                mc.data_get(
                    cadc_client,
                    working_dir,
                    preview,
                    ARCHIVE,
                    observable.metrics,
                )
            except mc.CadcException:
                new_retrieval = _retrieve_from_gemini(
                    gem_name,
                    observable,
                    plane,
                    preview_fqn,
                )

        if os.path.exists(preview_fqn):
            # in case TaskType == SCRAPE + MODIFY
            # always generate the thumbnails, but only store it if it's a
            # new retrieval from archive.gemini.edu
            try:
                fp = open(preview_fqn, 'r')
            except PermissionError as e:
                raise mc.CadcException(
                    f'Should not have reached this point in thumbnail '
                    f'generation for {plane.product_id}'
                )

            logging.debug(f'Generate thumbnail for file id {plane.product_id}')
            if os.access(thumb_fqn, 0):
                os.remove(thumb_fqn)
            try:
                image.thumbnail(preview_fqn, thumb_fqn, scale=0.25)
            except ValueError as e:
                # probably the jpg did not transfer properly from
                # archive.gemini.edu, so try to retrieve it one more time,
                # but ignore the count, because the count before this is
                # wrong
                #
                # have a retry here, because otherwise there's no way to
                # update the file in CADC storage without intervention
                # from Ops - i.e. the file will retrieve from CADC, so
                # there will be no succeeding attempt to retrieve from Gemini
                # that might otherwise fix the value
                logging.debug(traceback.format_exc())
                logging.warning(
                    f'matplotlib error handling {gem_name.prev}.Try to '
                    f'retrieve from {PREVIEW_URL} one more time.'
                )
                new_retrieval = _retrieve_from_gemini(
                    gem_name,
                    observable,
                    plane,
                    preview_fqn,
                )
                image.thumbnail(preview_fqn, thumb_fqn, scale=0.25)

            _augment(
                plane, gem_name.prev_uri, preview_fqn, ProductType.PREVIEW
            )
            count = 1

            if cadc_client is not None and new_retrieval:
                # if the thumbnail could be generated from the preview,
                # the preview is probably not corrupted
                mc.data_put(
                    cadc_client,
                    working_dir,
                    gem_name.prev,
                    ARCHIVE,
                    stream,
                    MIME_TYPE,
                    mime_encoding=None,
                    metrics=observable.metrics,
                )
            _augment(
                plane, gem_name.thumb_uri, thumb_fqn, ProductType.THUMBNAIL
            )
            if cadc_client is not None and new_retrieval:
                mc.data_put(
                    cadc_client,
                    working_dir,
                    thumb,
                    ARCHIVE,
                    stream,
                    MIME_TYPE,
                    mime_encoding=None,
                    metrics=observable.metrics,
                )
            count += 1
    return count
Esempio n. 48
0
def _do_prev(obs_id, working_dir, plane, clients, observable, gem_name):
    """Retrieve the preview file, so that a thumbnail can be made,
    store the preview if necessary, and the thumbnail, to CADC storage.
    Then augment the CAOM observation with the two additional artifacts.
    """
    count = 0

    if observable.rejected.is_no_preview(gem_name.prev):
        logging.info(
            f'Stopping visit because no preview exists for {gem_name.prev} '
            f'in observation {obs_id}.')
        observable.rejected.record(mc.Rejected.NO_PREVIEW, gem_name.prev)
        count += _check_for_delete(gem_name.prev, gem_name.prev_uri,
                                   observable, plane)
    else:
        preview_fqn = join(working_dir, gem_name.prev)
        thumb = gem_name.thumb
        thumb_fqn = join(working_dir, thumb)

        # get the file - try disk first, then CADC, then Gemini
        # Only try to retrieve from Gemini if the eventual purpose is
        # storage (i.e. cadc_client is not None), though
        if (not access(preview_fqn, 0) and clients is not None
                and clients.data_client is not None):
            try:
                logging.debug(f'Check CADC for {gem_name.prev_uri}.')
                clients.data_client.get(working_dir, gem_name.prev_uri)
            except exceptions.UnexpectedException:
                logging.debug(
                    f'Retrieve {gem_name.prev} from archive.gemini.edu.')
                _retrieve_from_gemini(
                    gem_name,
                    observable,
                    plane,
                    preview_fqn,
                )

        if exists(preview_fqn):
            # in case TaskType == SCRAPE + MODIFY
            try:
                fp = open(preview_fqn, 'r')
            except PermissionError as e:
                raise mc.CadcException(
                    f'Should not have reached this point in thumbnail '
                    f'generation for {plane.product_id}')

            logging.debug(f'Generate thumbnail for file id {plane.product_id}')
            if access(thumb_fqn, 0):
                remove(thumb_fqn)
            try:
                image.thumbnail(preview_fqn, thumb_fqn, scale=0.25)
            except ValueError as e:
                # probably the jpg did not transfer properly from
                # archive.gemini.edu, so try to retrieve it one more time,
                # but ignore the count, because the count before this is
                # wrong
                #
                # have a retry here, because otherwise there's no way to
                # update the file in CADC storage without intervention
                # from Ops - i.e. the file will retrieve from CADC, so
                # there will be no succeeding attempt to retrieve from Gemini
                # that might otherwise fix the value
                logging.debug(traceback.format_exc())
                logging.warning(
                    f'matplotlib error handling {gem_name.prev}.Try to '
                    f'retrieve from {PREVIEW_URL} one more time.')
                _retrieve_from_gemini(
                    gem_name,
                    observable,
                    plane,
                    preview_fqn,
                )
                image.thumbnail(preview_fqn, thumb_fqn, scale=0.25)

            count += _augment(plane, gem_name.prev_uri, preview_fqn,
                              ProductType.PREVIEW)
            if clients is not None and clients.data_client is not None:
                clients.data_client.put(working_dir, gem_name.prev_uri)
            count += 1

            count += _augment(plane, gem_name.thumb_uri, thumb_fqn,
                              ProductType.THUMBNAIL)
            if clients is not None and clients.data_client is not None:
                clients.data_client.put(working_dir, gem_name.thumb_uri)
            count += 1
    return count
Esempio n. 49
0
    def collect(self, blocks, figure_key="", subfig=0):
        '''collect one or more R figures.

        Plots are collected from all active devices.
        Plots are also collected from result-blocks
        containing a 'ggplot' attribute.

        1. save as png, hires-png and pdf
        2. save thumbnail
        3. insert rendering code at placeholders in output

        returns a map of place holder to placeholder text.
        '''
        # disable plotting if no rpy installed
        if R is None:
            return {}

        map_figure2text = {}

        # determine the image formats to create
        default_format, additional_formats = Utils.getImageFormats(
            self.display_options)
        all_formats = [default_format, ] + additional_formats
        image_options = Utils.getImageOptions(self.display_options)

        ##########################################
        ##########################################
        ##########################################
        # iterate over devices
        devices = R["dev.list"]()
        try:
            maxid = max(R["dev.list"]())
        except TypeError:
            maxid = 0

        for figid in range(2, maxid + 1):

            for id, format, dpi in all_formats:

                R["dev.set"](figid)

                outname = "%s_%02d" % (self.template_name, figid)
                outpath = os.path.join(self.outdir, '%s.%s' % (outname, format))

                if format.endswith("png"):
                    # for busy images there is a problem with figure margins
                    # simply increase dpi until it works.
                    R["dev.set"](figid)

                    width = height = 480 * dpi / 80
                    x = 0
                    while 1:
                        try:
                            R["dev.copy"](device=R.png,
                                          filename=outpath,
                                          res=dpi,
                                          width=width,
                                          height=height)
                            R["dev.off"]()
                        except rpy2.rinterface.RRuntimeError:
                            width *= 2
                            height *= 2
                            if x < 5:
                                continue
                        break

                elif format.endswith("svg"):
                    R["dev.copy"](device=R.svg,
                                  filename=outpath)
                    R["dev.off"]()

                elif format.endswith("eps"):
                    R["dev.copy"](device=R.postscript,
                                  paper='special',
                                  width=6,
                                  height=6,
                                  file=outpath,
                                  onefile=True)
                    R["dev.off"]()
                elif format.endswith("pdf"):
                    R["dev.copy"](device=R.pdf,
                                  paper='special',
                                  width=6,
                                  height=6,
                                  file=outpath,
                                  onefile=True)
                    R["dev.off"]()
                else:
                    raise ValueError("format '%s' not supported" % format)

                if not os.path.exists(outpath):
                    continue
                    # raise ValueError("rendering problem: image file was not be created: %s" % outpath)

                if format == 'png':
                    thumbdir = os.path.join(self.outdir, 'thumbnails')
                    try:
                        os.makedirs(thumbdir)
                    except OSError:
                        pass
                    thumbfile = str('%s.png' % os.path.join(thumbdir, outname))
                    captionfile = str(
                        '%s.txt' % os.path.join(thumbdir, outname))
                    if not os.path.exists(thumbfile):
                        # thumbnail only available in matplotlib >= 0.98.4
                        try:
                            figthumb = image.thumbnail(
                                str(outpath), str(thumbfile), scale=0.3)
                        except AttributeError:
                            pass

                    outfile = open(captionfile, "w")
                    outfile.write("\n".join(self.content) + "\n")
                    outfile.close()

                R["dev.off"](figid)

            # create the text element
            rst_output = Utils.buildRstWithImage(outname,
                                                 self.outdir,
                                                 self.rstdir,
                                                 self.builddir,
                                                 self.srcdir,
                                                 additional_formats,
                                                 self.tracker_id,
                                                 self.links,
                                                 self.display_options)

            map_figure2text["#$rpl %i$#" % figid] = rst_output

        figid = maxid
        ##########################################
        ##########################################
        ##########################################
        # iterate over ggplot plots
        for block in blocks:
            if not hasattr(block, "rggplot"):
                continue
            pp = block.rggplot
            figname = block.figname

            outname = "%s_%s" % (self.template_name, figname)

            for id, format, dpi in all_formats:
                outpath = os.path.join(self.outdir, '%s.%s' % (outname, format))

                try:
                    R.ggsave(outpath, plot=pp, dpi=dpi)
                except rpy2.rinterface.RRuntimeError as msg:
                    raise

                # width, height = 3 * dpi, 3 * dpi
                # if format.endswith("png"):
                #     R.png(outpath,
                #            width = width,
                #            height = height)
                # elif format.endswith("svg"):
                #     R.svg(outpath)
                # elif format.endswith("eps"):
                #     R.postscript(outpath)
                # elif format.endswith("pdf"):
                #     R.pdf(outpath)
                # R.plot(pp)
                # R["dev.off"]()

            # create the text element
            rst_output = Utils.buildRstWithImage(outname,
                                                 self.outdir,
                                                 self.rstdir,
                                                 self.builddir,
                                                 self.srcdir,
                                                 additional_formats,
                                                 self.tracker_id,
                                                 self.links,
                                                 self.display_options)

            map_figure2text["#$ggplot %s$#" % figname] = rst_output

        return map_figure2text
Esempio n. 50
0
def make_thumbnail(args):
    image.thumbnail(args[0], args[1], 0.3)
Esempio n. 51
0
    def collect( self, 
                 blocks,
                 template_name, 
                 outdir, 
                 rstdir,
                 rst2rootdir, 
                 rst2builddir,
                 rst2srcdir,
                 content,
                 display_options,
                 linked_codename,
                 tracker_id):
        '''collect one or more R figures from all active devices
        and
        
        1. save as png, hires-png and pdf
        2. save thumbnail
        3. insert rendering code at placeholders in output

        returns a map of place holder to placeholder text.
        '''
        
        map_figure2text = {}

        # determine the image formats to create
        default_format, additional_formats = Utils.getImageFormats()
        all_formats = [default_format,] + additional_formats
        urls = Utils.asList( Utils.PARAMS["report_urls"] )

        devices = R["dev.list"]()
        try:
            maxid = max( R["dev.list"]() )
        except TypeError:
            return map_figure2text
        
        for figid in range( 2, maxid+1 ):

            for id, format, dpi in all_formats:

                R["dev.set"]( figid )

                outname = "%s_%02d" % (template_name, figid)

                outpath = os.path.join(outdir, '%s.%s' % (outname, format))

                if format.endswith( "png" ):
                    R["dev.copy"]( device = R.png,
                                   filename = outpath,
                                   res = dpi )
                    R["dev.off"]()

                elif format.endswith( "svg" ):
                    R["dev.copy"]( device = R.svg,
                                   filename = outpath )
                    R["dev.off"]()

                elif format.endswith( "eps" ):
                    R["dev.copy"]( device = R.postscript,
                                   file = outpath,
                                   onefile = True )
                    R["dev.off"]()
                else:
                    raise ValueError( "format '%s' not supported" % format )

                if not os.path.exists( outpath ):
                    continue
                    # raise ValueError( "rendering problem: image file was not be created: %s" % outpath )

                if format=='png':
                    thumbdir = os.path.join(outdir, 'thumbnails')
                    try:
                        os.makedirs(thumbdir)
                    except OSError:
                        pass
                    thumbfile = str('%s.png' % os.path.join(thumbdir, outname) )
                    captionfile = str('%s.txt' % os.path.join(thumbdir, outname) )
                    if not os.path.exists(thumbfile):
                        # thumbnail only available in matplotlib >= 0.98.4
                        try:
                            figthumb = image.thumbnail(str(outpath), str(thumbfile), scale=0.3)
                        except AttributeError:
                            pass
                    outfile = open(captionfile,"w")
                    outfile.write( "\n".join( content ) + "\n" )
                    outfile.close()

            R["dev.off"](figid)

            # create the text element
            rst_output = ""
            # for image diretive - image path is relative from rst file to external build dir
            imagepath = re.sub( "\\\\", "/", os.path.join( rst2builddir, outname ) )
            # for links - path is from rst file to internal root dir
            relative_imagepath = re.sub( "\\\\", "/", os.path.join( rst2rootdir, outname ) )

            linked_text = relative_imagepath + ".txt"

            if Config.HTML_IMAGE_FORMAT:
                id, format, dpi = Config.HTML_IMAGE_FORMAT
                template = '''
.. htmlonly::

   .. image:: %(linked_image)s
%(display_options)s

   [%(code_url)s %(rst_url)s %(data_url)s  %(extra_images)s]
'''

                linked_image = imagepath + ".%s" % format

                extra_images=[]
                for id, format, dpi in additional_formats:
                    extra_images.append( "`%(id)s <%(relative_imagepath)s.%(format)s>`__" % locals())
                if extra_images: extra_images = " " + " ".join( extra_images)
                else: extra_images = ""

                # construct additional urls
                code_url, data_url, rst_url = "", "", ""
                if "code" in urls:
                    code_url = "`code <%(linked_codename)s>`__" % locals()

                if "data" in urls:
                    data_url = "`data </data/%(tracker_id)s>`__" % locals()

                if "rst" in urls:
                    rst_url = "`rst <%(linked_text)s>`__" % locals()

                rst_output += template % locals()

            # treat latex separately
            if Config.LATEX_IMAGE_FORMAT:
                id, format, dpi = Config.LATEX_IMAGE_FORMAT
                template = '''
.. latexonly::

   .. image:: %(linked_image)s
%(display_options)s
'''
                linked_image = imagepath + ".%s" % format
                rst_output += template % locals()

            map_figure2text[ "#$rpl %i$#" % figid] = rst_output

        return map_figure2text
Esempio n. 52
0
def make_thumbnail(args):
    image.thumbnail(args[0], args[1], 0.3)
Esempio n. 53
0
def write_example(src_name, src_dir, rst_dir, cfg):
    """Write rst file from a given python example.

    Parameters
    ----------
    src_name : str
        Name of example file.
    src_dir : 'str'
        Source directory for python examples.
    rst_dir : 'str'
        Destination directory for rst files generated from python examples.
    cfg : config object
        Sphinx config object created by Sphinx.
    """
    last_dir = src_dir.psplit()[-1]
    # to avoid leading . in file names, and wrong names in links
    if last_dir == '.' or last_dir == 'examples':
        last_dir = Path('')
    else:
        last_dir += '_'

    src_path = src_dir.pjoin(src_name)
    example_file = rst_dir.pjoin(src_name)
    shutil.copyfile(src_path, example_file)

    image_dir = rst_dir.pjoin('images')
    thumb_dir = image_dir.pjoin('thumb')
    image_dir.makedirs()
    thumb_dir.makedirs()

    # copy static files
    static_src = src_dir.pjoin('static')
    if static_src.exists:
        static_dir = rst_dir.pjoin('static')
        if static_dir.exists:
            shutil.rmtree(static_dir)
        shutil.copytree(static_src, static_dir)

    base_image_name = os.path.splitext(src_name)[0]
    image_path = image_dir.pjoin(base_image_name + '_{0}.png')

    basename, py_ext = os.path.splitext(src_name)
    rst_path = rst_dir.pjoin(basename + cfg.main_source_suffix)

    if _plots_are_current(src_path, image_path) and rst_path.exists:
        return

    flags = cfg.plot2rst_flags.copy()
    blocks, new_flags = split_code_and_text_blocks(example_file)
    flags.update(new_flags)

    while True:
        head = blocks[0][2]
        if head.startswith('#!') or head.startswith(FLAG_PREFIX):
            blocks.pop(0)  # don't add shebangs or flags to rst file.
        else:
            break

    # Note that `process_blocks` executes the source, so plots are now 'active'
    figure_list, rst = process_blocks(blocks, src_path, image_path, cfg)

    rst_link = '.. _example_%s:\n\n' % (last_dir + src_name)
    example_rst = ''.join([rst_link, rst])

    has_inline_plots = any(cfg.plot2rst_plot_tag in b[2] for b in blocks)

    if not has_inline_plots and flags['auto_plots']:
        # Show all plots at the end of the example
        if len(plt.get_fignums()) > 0:
            figure_list = save_all_figures(image_path)
            img_blocks = [IMAGE_TEMPLATE % f.lstrip('/') for f in figure_list]
            example_rst += ''.join(img_blocks)
    plt.close('all')

    example_rst += CODE_LINK.format(src_name)

    f = open(rst_path, 'w')
    f.write(example_rst)
    f.flush()

    thumb_path = thumb_dir.pjoin(src_name[:-3] + '.png')

    # Modified to take last image rather than first
    if figure_list:
        last_image_file = image_dir.pjoin(figure_list[-1].lstrip('/'))
        if last_image_file.exists:
            thumb_scale = cfg.plot2rst_thumb_scale
            image.thumbnail(last_image_file, thumb_path, thumb_scale)

    if not thumb_path.exists:
        if cfg.plot2rst_default_thumb is None:
            print("WARNING: No plots found and default thumbnail not defined.")
            print("Specify 'plot2rst_default_thumb' in Sphinx config file.")
        else:
            shutil.copy(cfg.plot2rst_default_thumb, thumb_path)
Esempio n. 54
0
matplotlib natively supports PNG files on the input side, and other
image types transparently if your have PIL installed


"""

from __future__ import print_function
# build thumbnails of all images in a directory
import sys
import os
import glob
import matplotlib.image as image

if len(sys.argv) != 2:
    print('Usage: python %s IMAGEDIR' % __file__)
    raise SystemExit
indir = sys.argv[1]
if not os.path.isdir(indir):
    print('Could not find input directory "%s"' % indir)
    raise SystemExit

outdir = 'thumbs'
if not os.path.exists(outdir):
    os.makedirs(outdir)

for fname in glob.glob(os.path.join(indir, '*.png')):
    basedir, basename = os.path.split(fname)
    outfile = os.path.join(outdir, basename)
    fig = image.thumbnail(fname, outfile, scale=0.15)
    print('saved thumbnail of %s to %s' % (fname, outfile))
Esempio n. 55
0
def generate_file_rst(fname, target_dir, src_dir, plot_gallery):
    """ Generate the rst file for a given example.
    """
    base_image_name = os.path.splitext(fname)[0]
    image_fname = '%s_%%s.png' % base_image_name

    this_template = rst_template
    last_dir = os.path.split(src_dir)[-1]
    # to avoid leading . in file names, and wrong names in links
    if last_dir == '.' or last_dir == 'examples':
        last_dir = ''
    else:
        last_dir += '_'
    short_fname = last_dir + fname
    src_file = os.path.join(src_dir, fname)
    example_file = os.path.join(target_dir, fname)
    shutil.copyfile(src_file, example_file)

    # The following is a list containing all the figure names
    figure_list = []

    image_dir = os.path.join(target_dir, 'images')
    thumb_dir = os.path.join(image_dir, 'thumb')
    if not os.path.exists(image_dir):
        os.makedirs(image_dir)
    if not os.path.exists(thumb_dir):
        os.makedirs(thumb_dir)
    image_path = os.path.join(image_dir, image_fname)
    thumb_file = os.path.join(thumb_dir, fname[:-3] + '.png')
    if plot_gallery and fname.startswith('plot'):
        # generate the plot as png image if file name
        # starts with plot and if it is more recent than an
        # existing image.
        first_image_file = image_path % 1

        if (not os.path.exists(first_image_file) or
                os.stat(first_image_file).st_mtime <=
                                    os.stat(src_file).st_mtime):
            # We need to execute the code
            print 'plotting %s' % fname
            import matplotlib.pyplot as plt
            plt.close('all')
            cwd = os.getcwd()
            try:
                # First CD in the original example dir, so that any file created
                # by the example get created in this directory
                os.chdir(os.path.dirname(src_file))
                execfile(os.path.basename(src_file), {'pl' : plt})
                os.chdir(cwd)

                # In order to save every figure we have two solutions :
                # * iterate from 1 to infinity and call plt.fignum_exists(n)
                #   (this requires the figures to be numbered
                #    incrementally: 1, 2, 3 and not 1, 2, 5)
                # * iterate over [fig_mngr.num for fig_mngr in
                #   matplotlib._pylab_helpers.Gcf.get_all_fig_managers()]
                for fig_num in (fig_mngr.num for fig_mngr in
                          matplotlib._pylab_helpers.Gcf.get_all_fig_managers()):
                    # Set the fig_num figure as the current figure as we can't
                    # save a figure that's not the current figure.
                    plt.figure(fig_num)
                    plt.savefig(image_path % fig_num)
                    figure_list.append(image_fname % fig_num)
            except:
                print 80*'_'
                print '%s is not compiling:' % fname
                traceback.print_exc()
                print 80*'_'
            finally:
                os.chdir(cwd)
        else:
            figure_list = [f[len(image_dir):]
                            for f in glob.glob(image_path % '[1-9]')]
                            #for f in glob.glob(image_path % '*')]

        # generate thumb file
        this_template = plot_rst_template
        from matplotlib import image
        if os.path.exists(first_image_file):
            image.thumbnail(first_image_file, thumb_file, 0.2)

    if not os.path.exists(thumb_file):
        # create something not to replace the thumbnail
        shutil.copy('source/auto_examples/images/blank_image.png', thumb_file)

    docstring, short_desc, end_row = extract_docstring(example_file)

    # Depending on whether we have one or more figures, we're using a
    # horizontal list or a single rst call to 'image'.
    if len(figure_list) == 1:
        figure_name = figure_list[0]
        image_list = SINGLE_IMAGE % figure_name.lstrip('/')
    else:
        image_list = HLIST_HEADER
        for figure_name in figure_list:
            image_list += HLIST_IMAGE_TEMPLATE % figure_name.lstrip('/')

    f = open(os.path.join(target_dir, fname[:-2] + 'txt'),'w')
    f.write(this_template % locals())
    f.flush()