# HG changeset patch # User Ben Abbott # Date 1327285390 18000 # Node ID c7467316175273567019f7e2682825b25a0cf7bd # Parent 85d04dfdeac3994f9533670b43e4572b5a0eb27b Add scripts to help validate plot results. * html_plot_demos_template.html, html_compare_plot_demos.m, compare_plot_demos.m, dump_demos.m: New files. diff -r 85d04dfdeac3 -r c74673161752 scripts/testfun/compare_plot_demos.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/testfun/compare_plot_demos.m Sun Jan 22 21:23:10 2012 -0500 @@ -0,0 +1,119 @@ +## Copyright (C) 2012 Ben Abbott +## +## This file is part of Octave. +## +## Octave is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 3 of the License, or (at +## your option) any later version. +## +## Octave is distributed in the hope that it will be useful, but +## WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with Octave; see the file COPYING. If not, see +## . + +## -*- texinfo -*- +## @deftypefn {Function File} {} compare_plot_demos () +## +## Uses @code{dump_demos} and @code{html_compare} to produce an html +## comparison of the plot demos for each of Octave's graphics +## toolkits. +## +## A m-file names `dump_plots.m' will be created in the pwd. This function +## will be used to render and save the plot demo images. +## +## If they do not already exist, directories for each graphics toolkit +## are created. Each toolkit's directory will be populated with images +## of each plot demo in the png format. +## +## Finally, an htlm document named `compare_plot_demos.html' is produced. +## This page places each toolkits images side by side for a conventient +## comparison of the results. +## +## @deftypefnx {Function File} {} compare_plot_demos (@var{toolkits}) +## @var{toolkits} is used to specify a subset of the available graphics +## toolkits. This list may also include `matlab'. +## +## @end deftypefn + +## Author: Ben Abbott + +function compare_plot_demos (varargin) + + in.toolkits = available_graphics_toolkits (); + in.directory = "plot"; + in.fmt = "png"; + in.fcn_file = ""; + in.replace_images = false; + + for n = 1:2:numel(varargin) + if (! ischar (varargin{n})) + print_usage (); + else + in.(varargin{n}) = varargin{n+1}; + endif + endfor + + if (ischar (in.toolkits)) + in.toolkits = {in.toolkits}; + elseif (! iscellstr (in.toolkits)) + error ("compare_plot_demos: Invalid value for ""toolkits""") + endif + + if (! ischar (in.directory)) + error ("compare_plot_demos: Invalid value for ""directory""") + endif + + if (! ischar (in.fmt)) + error ("compare_plot_demos: Invalid value for ""fmt""") + endif + + if (isempty (in.fcn_file)) + in.fcn_file = sprintf ("dump_%s_demos.m", in.directory); + endif + + ## Generate "dump_plots.m" for rendering/saving the plot demo images + dump_demos ("plot", in.fcn_file, in.fmt) + + [~, fcn_name] = fileparts (in.fcn_file); + + ## Generate the plot demo images for each toolkit + cwd = pwd (); + unwind_protect + addpath (pwd); + for n = 1:numel(in.toolkits) + dirs = dir (); + dirs = dirs([dirs.isdir]); + if (! any (strcmp ({dirs.name}, in.toolkits{n}))) + mkdir (in.toolkits{n}) + endif + cd (in.toolkits{n}) + if (! isempty (dir (strcat ("*.", in.fmt))) && in.replace_images) + delete (strcat ("*.", in.fmt)) + endif + if (! strcmp (in.toolkits{n}, "matlab")) + close all + graphics_toolkit (in.toolkits{n}); + try + eval (fcn_name); + catch + fprintf ("Error running plot demos for ""%s"" toolkit\n", in.toolkits{n}) + disp (lasterror) + end_try_catch + endif + cd (cwd) + endfor + unwind_protect_cleanup + rmpath (cwd); + end_unwind_protect + + ## Generate the html comparison of the images + ## TODO - pass the toolkits{} to allow num of columns to be formated + html_compare_plot_demos ("output", "compare_plot_demos.html") + +endfunction + diff -r 85d04dfdeac3 -r c74673161752 scripts/testfun/dump_demos.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/testfun/dump_demos.m Sun Jan 22 21:23:10 2012 -0500 @@ -0,0 +1,195 @@ +## Copyright (C) 2010 Søren Hauberg +## +## This file is part of Octave. +## +## Octave is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 3 of the License, or (at +## your option) any later version. +## +## Octave is distributed in the hope that it will be useful, but +## WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with Octave; see the file COPYING. If not, see +## . + +## -*- texinfo -*- +## @deftypefn {Function File} {} dump_demos () +## @deftypefnx {Function File} {} dump_demos (@var{DIR}) +## @deftypefnx {Function File} {} dump_demos (@var{DIR}, @var{mfile}) +## @deftypefnx {Function File} {} dump_demos (@var{DIR}, @var{mfile}, @var{fmt}) +## Produces a script, with the name specified by @var{mfile}, containing +## the demos in the directory, @var{DIR}. The demos are assumed to produce +## graphical output, whose renderings are saved with specified format, +## @var{fmt}. +## +## The defaults for each input are; +## +## @table @samp +## @item @var{DIR} +## @code{"plot"} +## @item @var{mfile} +## @code{"dump.m"} +## @item @var{fmt} +## @code{"png"} +## @end table +## +## For example, to produce PNG output for all demos of the functions +## in the plot directory; +## +## @example +## @group +## dump_demos plot dump.m png +## @end group +## @end example +## @seealso{fntests, test, demo} +## @end deftypefn + +## Author: Søren Hauberg + +function dump_demos (directory="plot", output="dump.m", fmt="png") + + ## Get path + if (nargin < 4) + if (is_absolute_filename (directory)) + dirs = {directory}; + else + fullname = find_dir_in_path (directory); + if (! isempty (fullname)) + dirs = {fullname}; + else + error ("print_demos: expecting argument to be a directory name"); + endif + endif + else + print_usage (); + endif + + if (strfind (output, ".m") != numel (output) - 1) + output = strcat (output, ".m"); + endif + + ## Create script beginning (close figures, etc.) + fid = fopen (output, "w"); + n = find (output == ".", 1, "last"); + if (isempty (n)) + n = numel (output); + else + n = n - 1; + endif + fprintf (fid, "function %s ()\n", output(1:n)); + fprintf (fid, "close all\n"); + fprintf (fid, "more off\n"); + + ## Run and print the demos in each directory + for i = 1:numel (dirs) + d = dirs{i}; + dump_all_demos (d, fid, fmt); + endfor + + ## Create script ending + fprintf (fid, "end\n\n") + + ## TODO - Should dump_demos() attempt to convert the demos to traditional + ## sytax. + ## (1) oct2mat() to convert some Octave specific syntax. + ## (2) Embed sombrero(), vec(), cstrcat() and assert() in demos ? + + for mfile = {"sombrero"} + f = which (mfile{1}); + fid2 = fopen (f); + code = char (fread (fid2)); + code = oct2mat (code); + fprintf (fid, "%s", code); + fclose (fid2); + endfor + + fprintf (fid, "function x = vec (x)\n") + fprintf (fid, " x = x(:);\n") + fprintf (fid, "end\n") + + fprintf (fid, "function str = cstrcat (varargin)\n") + fprintf (fid, " str = [varargin{:}];\n") + fprintf (fid, "end\n") + + fprintf (fid, "function assert (varargin)\n") + fprintf (fid, "% Do nothing.\n") + fprintf (fid, "end\n") + + ## Close script + fclose (fid); +endfunction + +function dump_all_demos (directory, fid, fmt) + dirinfo = dir (fullfile (directory, "*.m")); + flist = {dirinfo.name}; + ## Remove uigetdir, uigetfile, uiputfile, etc. + flist = flist(! strncmp (flist, "ui", 2)); + for i = 1:numel (flist) + fun = flist{i}; + fun (end-1:end) = []; # remove .m + demos = get_demos (fun); + for d = 1:numel (demos) + if (d < 10) + idx = sprintf ("0%d", d); + else + idx = sprintf ("%d", d); + end + fprintf (fid, "\ntry\n"); + fprintf (fid, " %s\n\n", demos {d}); + fprintf (fid, " drawnow\n"); + fprintf (fid, " num_figures = get(0, 'currentfigure');\n"); + fprintf (fid, " for fig = 1:num_figures\n"); + fprintf (fid, " figure (fig);\n"); + fprintf (fid, " name = sprintf ('%s_%s_%%d.%s', fig);\n", fun, idx, fmt); + fprintf (fid, " if (numel (dir (name)) == 0)\n"); + fprintf (fid, " fprintf ('Printing ""%%s"" ... ', name);\n") + fprintf (fid, " print ('-d%s', name);\n", fmt); + fprintf (fid, " pause (1);\n"); + fprintf (fid, " fprintf ('done\\n');\n"); + fprintf (fid, " else\n"); + fprintf (fid, " fprintf ('File ""%%s"" exists.\\n', name);\n") + fprintf (fid, " end\n"); + fprintf (fid, " drawnow ();\n"); + fprintf (fid, " end\n"); + fprintf (fid, " close (2:num_figures)\n"); + fprintf (fid, "catch\n"); + fprintf (fid, " fprintf ('%s_%s: %%s\\n', lasterr ());\n", fun, idx); + fprintf (fid, "end\n\n"); + endfor + endfor + fprintf (fid, "close all\n"); +endfunction + +function retval = get_demos (fun) + [code, idx] = test (fun, "grabdemo"); + num_demos = length (idx) - 1; + retval = cell (1, num_demos); + ## Now split the demos into a cell array + for k = 1:num_demos + retval {k} = oct2mat (code (idx (k):idx (k+1)-1)); + endfor +endfunction + +function code = oct2mat (code) + ## Simple hacks to make things Matlab compatible + code = strrep (code, "%!", "%%"); + code = strrep (code, "!", "~"); + code = strrep (code, "\"", "'"); + code = strrep (code, "#", "%"); + ## Fix the format specs for the errobar demos + code = strrep (code, "%r", "#r"); + code = strrep (code, "%~", "#~"); + endkeywords = {"endfor", "endif", "endwhile", "end_try_catch", ... + "endfunction", "end_unwind_protect"}; + for k = 1:numel (endkeywords) + code = strrep (code, endkeywords{k}, "end"); + endfor + commentkeywords = {"unwind_proect"}; + for k = 1:numel (commentkeywords) + code = strrep (code, commentkeywords{k}, strcat ("%", commentkeywords{k})); + endfor +endfunction diff -r 85d04dfdeac3 -r c74673161752 scripts/testfun/html_compare_plot_demos.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/testfun/html_compare_plot_demos.m Sun Jan 22 21:23:10 2012 -0500 @@ -0,0 +1,179 @@ +## Copyright (C) 2010 Ben Abbott +## +## This file is part of Octave. +## +## Octave is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 3 of the License, or (at +## your option) any later version. +## +## Octave is distributed in the hope that it will be useful, but +## WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with Octave; see the file COPYING. If not, see +## . + +## -*- texinfo -*- +## @deftypefn {Function File} {} html_compare () +## @deftypefnx {Function File} {} html_compare (@var{name}, @var{value}, @dots{}) +## +## Produces an html document to compare the plot demos produced by Octave's +## gnuplot and FLTK toolkits with those produced by Matalb. +## +## Valid property names, and their defaults, are; +## +## @table @samp +## @item date +## @code{datestr (now (), 0)} +## @item fmt +## @code{"png"} +## @item mfiles +## @code{sort (cellstr (ls ("gnuplot")))} +## @item gnuplot +## @code{sprintf ("Gnuplot %s", __gnuplot_version__ ())} +## @item matlab +## @code{"Matlab Version 7.11.0.584 (R2010b)"} +## @item octave +## @code{sprintf ("Octave %s", version)} +## @item output +## @code{"compare_plots.html"} +## @item template +## @code{"html_template.html"} +## @end table +## +## The template parameter refers to a specially formatted html file +## which accompanies this m-file script. +## +## This script may be used to generate the index.html page loaded by +## the link below. +## +## @example +## @group +## http://octave.sourceforge.net/compare_plots +## @end group +## @end example +## +## This is done by following the instructions below. +## +## @enumerate +## @item Begin by downloading a local copy of the web page. +## +## @example +## @group +## $ wget -vcr --level 1 http://octave.sourceforge.net/compare_plots/ +## @end group +## @end example +## +## @item Use @code{dump_demos} to produce the script used to produce the +## demo plots. In this example the script is @code{dump.m}. +## +## @example +## @group +## octave:1> dump_demos plot dump.m png +## @end group +## @end example +## +## @item Produce the gnuplot and fltk plots, and place them in their +## designated directories. +## +## @example +## @group +## octave:2> dump +## octave:3> movefile *.png octave.sourceforge.net/compare_plots/gnuplot/. +## octave:4> graphics_toolkit fltk +## octave:5> close all +## octave:6> dump +## octave:7> movefile *.png octave.sourceforge.net/compare_plots/fltk/. +## @end group +## @end example +## +## @item Start Matlab and edit the script @code{dump.m} to correct the +## remaining Octave specific syntax so that it will run under Matlab. +## +## @item Run @code{dump.m} under Matlab, and place the plots in the +## designated matlab directory. +## +## @example +## @group +## >> dump +## >> movefile *.png octave.sourceforge.net/compare_plots/matlab/. +## @end group +## @end example +## +## @item The web page comparing all plot demos is created by +## +## @example +## @group +## octave:8> cd octave.sourceforge.net/compare_plots +## octave:9> html_compare output index.html +## @end group +## @end example +## +## @item Finally, the new page may be loaded into your browser. +## +## @seealso{dump_demos, demo} +## @end deftypefn + +## Author: Ben Abbott + +function html_compare_plot_demos (varargin) + + ## TODO - Names of the toolkits should be input + ## Set defaults + in.date = datestr (now (), 0); + in.fmt = "png"; + in.figfiles = {}; + in.octave = sprintf ("Octave %s", version); + in.output= "compare_plots.html"; + in.template = "html_plot_demos_template.html"; + in.toolkits = {"gnuplot", "matlab", "fltk"}; + + ## Parse inputs + for n = 1:2:numel(varargin) + in.(lower(varargin{n})) = varargin{n+1}; + endfor + + for t = 1:numel(in.toolkits) + if (! isfield (in, in.toolkits{t})) + ## Column headers for the html page + in.(in.toolkits{t}) = upper (in.toolkits{t}); + endif + ## Compile a list of all files for all toolkits + if (t == 1) + in.figfiles = {dir(sprintf ("gnuplot/*.%s", in.fmt)).name}; + else + filter = sprintf ("%s/*.%s", in.toolkits{t}, in.fmt); + in.figfiles = union (in.figfiles, {dir(filter).name}); + endif + endfor + + fid = fopen (which (in.template), "r"); + template = char (fread (fid)) .'; + fclose (fid); + + template = strrep (template, "##OCTAVE##", in.octave); + template = strrep (template, "##GNUPLOT##", in.gnuplot); + template = strrep (template, "##MATLAB##", in.matlab); + + n1 = findstr (template, ""); + n2 = findstr (template, ""); + header = template(1:n1-1); + middle = template(n1+18:n2-1); + trailer = template(n2+15:end); + + fid = fopen (in.output, "w"); + unwind_protect + fputs (fid, header); + for m = 1:numel(in.figfiles) + [~, file] = fileparts (in.figfiles{m}); + fputs (fid, strrep (middle, "##PLOT##", strcat (file, ".", in.fmt))); + endfor + fputs (fid, trailer); + unwind_protect_cleanup + fclose (fid); + end_unwind_protect + +endfunction diff -r 85d04dfdeac3 -r c74673161752 scripts/testfun/html_plot_demos_template.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/testfun/html_plot_demos_template.html Sun Jan 22 21:23:10 2012 -0500 @@ -0,0 +1,90 @@ + + + + + + + + + Comparision of plots + + + + + + + + + +
+Select plots to show
+
+ ##OCTAVE## using ##GNUPLOT## +
+ ##MATLAB## +
+ ##OCTAVE## using FLTK +
+ Image width: + +
+ + + + + + + + + + + + + + + + +
Octave using Gnuplot +Matlab +Octave using FLTK +
##PLOT##
##PLOT##
##PLOT##
+ + +
+ + +