Mercurial > octave-dspies
comparison scripts/testfun/private/dump_demos.m @ 18895:b8934a57e13e
Improve plot comparison scripts.
* compare_plot_demos.m: Rewrite docstring. Use all three plot directories in
comparison ("appearance", "draw", "util"). Rename input variable structure
"in" to "arg". Terminate lines of
code with semicolon.
* dump_demos.m: Rewrite docstring. Use fileparts to simplify checking of
output script file name. Accept a single directory or a cell string array
of directories for processing. Revamp test for non-existent directory.
Remove unused code providing replacement Octave functions for Matlab.
Remove linkaxes, linkprops, and colormap from list of demos to process
since they have interactive elements. Use sprintf format string to avoid
if/else structure. Simplify generated code for each plot comparison.
Use 'close all' at the end of each demo to work around issues with accumulating
cruft. Follow Octave coding conventions for cuddling parentheses.
* dump_demos.m (oct2mat): Use regexprep() to convert double quotes in Octave
demos to single quotes for Matlab. Comment out unwind_protect blocks since
Matlab has no knowledge of these. Fix up calls to sombrero without an argument
since Matlab doesn't have a default for this.
author | Rik <rik@octave.org> |
---|---|
date | Fri, 04 Jul 2014 14:32:49 -0700 |
parents | 1b02bfff39d0 |
children | f9cf5ae6b8a2 |
comparison
equal
deleted
inserted
replaced
18894:24332f256940 | 18895:b8934a57e13e |
---|---|
19 ## -*- texinfo -*- | 19 ## -*- texinfo -*- |
20 ## @deftypefn {Function File} {} dump_demos () | 20 ## @deftypefn {Function File} {} dump_demos () |
21 ## @deftypefnx {Function File} {} dump_demos (@var{dirs}) | 21 ## @deftypefnx {Function File} {} dump_demos (@var{dirs}) |
22 ## @deftypefnx {Function File} {} dump_demos (@var{dirs}, @var{mfile}) | 22 ## @deftypefnx {Function File} {} dump_demos (@var{dirs}, @var{mfile}) |
23 ## @deftypefnx {Function File} {} dump_demos (@var{dirs}, @var{mfile}, @var{fmt}) | 23 ## @deftypefnx {Function File} {} dump_demos (@var{dirs}, @var{mfile}, @var{fmt}) |
24 ## Produces a script, with the name specified by @var{mfile}, containing | 24 ## Produce a script, with the name specified by @var{mfile}, containing |
25 ## the demos in the directories, @var{dirs}. The demos are assumed to produce | 25 ## the demos in the directories, @var{dirs}. The demos are assumed to produce |
26 ## graphical output, whose renderings are saved with specified format, | 26 ## graphical output, whose renderings are saved with the specified format, |
27 ## @var{fmt}. | 27 ## @var{fmt}. |
28 ## | 28 ## |
29 ## The defaults for each input are; | 29 ## The defaults for each input are; |
30 ## | 30 ## |
31 ## @table @samp | 31 ## @table @var |
32 ## @item @var{dirs} | 32 ## @item @var{dirs} |
33 ## @code{@{"plot/appearance", "plot/draw", "plot/util"@}} | 33 ## @code{@{"plot/appearance", "plot/draw", "plot/util"@}} |
34 ## @item @var{mfile} | 34 ## @item @var{mfile} |
35 ## @code{"dump.m"} | 35 ## @code{"dump.m"} |
36 ## @item @var{fmt} | 36 ## @item @var{fmt} |
39 ## | 39 ## |
40 ## For example, to produce PNG output for all demos of the functions | 40 ## For example, to produce PNG output for all demos of the functions |
41 ## in the plot directory; | 41 ## in the plot directory; |
42 ## | 42 ## |
43 ## @example | 43 ## @example |
44 ## @group | |
45 ## dump_demos plot dump.m png | 44 ## dump_demos plot dump.m png |
46 ## @end group | |
47 ## @end example | 45 ## @end example |
48 ## @seealso{fntests, test, demo} | 46 ## @seealso{fntests, test, demo} |
49 ## @end deftypefn | 47 ## @end deftypefn |
50 | 48 |
51 ## Author: Søren Hauberg <soren@hauberg.org> | 49 ## Author: Søren Hauberg <soren@hauberg.org> |
52 | 50 |
53 function dump_demos (dirs= {"plot/appearance", "plot/draw", "plot/util"}, output="dump.m", fmt="png") | 51 function dump_demos (dirs={"plot/appearance", "plot/draw", "plot/util"}, output="dump.m", fmt="png") |
54 | 52 |
55 if (nargin > 3) | 53 if (nargin > 3) |
56 print_usage (); | 54 print_usage (); |
57 endif | 55 endif |
58 | 56 |
59 if (strfind (output, ".m") != numel (output) - 1) | 57 if (ischar (dirs)) |
60 output = strcat (output, ".m"); | 58 dirs = {dirs}; |
59 elseif (! iscellstr (dirs)) | |
60 error ("dump_demos: DIRS must be a cell array of strings with directory names"); | |
61 endif | |
62 | |
63 [~, funcname, ext] = fileparts (output); | |
64 if (isempty (ext)) | |
65 output = [output ".m"]; | |
61 endif | 66 endif |
62 | 67 |
63 ## Create script beginning (close figures, etc.) | 68 ## Create script beginning (close figures, etc.) |
64 fid = fopen (output, "w"); | 69 fid = fopen (output, "w"); |
65 n = find (output == ".", 1, "last"); | |
66 if (isempty (n)) | |
67 n = numel (output); | |
68 else | |
69 n = n - 1; | |
70 endif | |
71 fprintf (fid, "%% DO NOT EDIT! Generated automatically by dump_demos.m\n"); | 70 fprintf (fid, "%% DO NOT EDIT! Generated automatically by dump_demos.m\n"); |
72 fprintf (fid, "function %s ()\n", output(1:n)); | 71 fprintf (fid, "function %s ()\n", funcname); |
73 fprintf (fid, "close all\n"); | 72 fprintf (fid, "close all\n"); |
74 fprintf (fid, "more off\n"); | 73 fprintf (fid, "more off\n"); |
75 | 74 |
76 ## Run and print the demos in each directory | 75 ## Run and print the demos in each directory |
77 for i = 1:numel (dirs) | 76 for i = 1:numel (dirs) |
78 if (!is_absolute_filename (dirs{i})) | 77 d = dirs{i}; |
79 fullname = dir_in_loadpath (dirs{i}); | 78 if (! is_absolute_filename (d)) |
80 if (! isempty (fullname)) | 79 d = dir_in_loadpath (d); |
81 dirs{i} = fullname; | |
82 else | |
83 error ("dump_demos: expecting DIRS argument to be a cell arrays of strings with directory names"); | |
84 endif | |
85 endif | 80 endif |
86 d = dirs{i}; | 81 if (! exist (d, "dir")) |
87 if (!exist (d, "dir")) | 82 error ("dump_demos: directory %s does not exist", d); |
88 error ("dump_demos: directory %s doesn't exist", d); | |
89 endif | 83 endif |
90 dump_all_demos (d, fid, fmt); | 84 dump_all_demos (d, fid, fmt); |
91 endfor | 85 endfor |
92 | 86 |
93 ## Create script ending | 87 ## Create script ending |
94 fprintf (fid, "end\n\n") | 88 fprintf (fid, "end\n\n") |
95 | |
96 ## TODO - Should dump_demos() attempt to convert the demos to traditional | |
97 ## syntax. | |
98 ## (1) oct2mat() to convert some Octave specific syntax. | |
99 ## (2) Embed sombrero(), vec(), cstrcat() and assert() in demos ? | |
100 | |
101 ## sombrero has now a default argument which isn't supported from matlab | |
102 ## http://octave.1599824.n4.nabble.com/sombrero-default-argument-matlab-compatibility-td4665016.html | |
103 ## TODO: we need to change it prior running | |
104 ## -function [x, y, z] = sombrero (n = 41) | |
105 ## +function [x, y, z] = sombrero (n) | |
106 ## + | |
107 ## + if (nargin == 0) | |
108 ## + n = 41; | |
109 ## + endif | |
110 | |
111 for mfile = {"sombrero"} | |
112 f = which (mfile{1}); | |
113 fid2 = fopen (f); | |
114 code = char (fread (fid2)); | |
115 code = oct2mat (code); | |
116 fprintf (fid, "%s", code); | |
117 fclose (fid2); | |
118 endfor | |
119 %~ | |
120 %~ fprintf (fid, "function x = vec (x)\n") | |
121 %~ fprintf (fid, " x = x(:);\n") | |
122 %~ fprintf (fid, "end\n") | |
123 %~ | |
124 %~ fprintf (fid, "function str = cstrcat (varargin)\n") | |
125 %~ fprintf (fid, " str = [varargin{:}];\n") | |
126 %~ fprintf (fid, "end\n") | |
127 %~ | |
128 %~ fprintf (fid, "function assert (varargin)\n") | |
129 %~ fprintf (fid, "%% Do nothing.\n") | |
130 %~ fprintf (fid, "end\n") | |
131 | 89 |
132 ## Close script | 90 ## Close script |
133 fclose (fid); | 91 fclose (fid); |
134 endfunction | 92 endfunction |
135 | 93 |
136 function dump_all_demos (directory, fid, fmt) | 94 function dump_all_demos (directory, fid, fmt) |
137 dirinfo = dir (fullfile (directory, "*.m")); | 95 dirinfo = dir (fullfile (directory, "*.m")); |
138 flist = {dirinfo.name}; | 96 flist = {dirinfo.name}; |
139 ## Remove uigetdir, uigetfile, uiputfile, etc. | 97 ## Remove uigetdir, uigetfile, uiputfile, etc. |
140 flist = flist(! strncmp (flist, "ui", 2)); | 98 flist = flist(! strncmp (flist, "ui", 2)); |
99 ## Remove linkaxes, linkprops | |
100 flist = flist(! strncmp (flist, "link", 4)); | |
101 ## Remove colormap | |
102 flist = flist(! strncmp (flist, "colormap", 8)); | |
141 for i = 1:numel (flist) | 103 for i = 1:numel (flist) |
142 fun = flist{i}; | 104 fcn = flist{i}; |
143 fun (end-1:end) = []; # remove .m | 105 fcn(end-1:end) = []; # remove .m |
144 demos = get_demos (fun); | 106 demos = get_demos (fcn); |
145 for d = 1:numel (demos) | 107 for d = 1:numel (demos) |
146 if (d < 10) | 108 idx = sprintf ("%02d", d); |
147 idx = sprintf ("0%d", d); | |
148 else | |
149 idx = sprintf ("%d", d); | |
150 end | |
151 fprintf (fid, "\ntry\n"); | 109 fprintf (fid, "\ntry\n"); |
152 fprintf (fid, " %s\n\n", demos {d}); | 110 fprintf (fid, " %s\n\n", demos{d}); |
153 fprintf (fid, " drawnow\n"); | 111 fprintf (fid, " drawnow;\n"); |
154 fprintf (fid, " num_figures = get(0, 'currentfigure');\n"); | 112 fprintf (fid, " fig = (get (0, 'currentfigure'));\n"); |
155 fprintf (fid, " for fig = 1:num_figures\n"); | 113 fprintf (fid, " if (~ isempty (fig))\n"); |
156 fprintf (fid, " figure (fig);\n"); | 114 fprintf (fid, " figure (fig);\n"); |
157 fprintf (fid, " name = sprintf ('%s_%s_%%d.%s', fig);\n", fun, idx, fmt); | 115 fprintf (fid, " name = '%s_%s.%s';\n", fcn, idx, fmt); |
158 fprintf (fid, " if (numel (dir (name)) == 0)\n"); | 116 fprintf (fid, " if (isempty (dir (name)))\n"); |
159 fprintf (fid, " fprintf ('Printing ""%%s"" ... ', name);\n") | 117 fprintf (fid, " fprintf ('Printing ""%%s"" ... ', name);\n") |
160 fprintf (fid, " print ('-d%s', name);\n", fmt); | 118 fprintf (fid, " print ('-d%s', name);\n", fmt); |
161 # fprintf (fid, " pause (1);\n"); | |
162 fprintf (fid, " fprintf ('done\\n');\n"); | 119 fprintf (fid, " fprintf ('done\\n');\n"); |
163 fprintf (fid, " else\n"); | 120 fprintf (fid, " else\n"); |
164 fprintf (fid, " fprintf ('File ""%%s"" exists.\\n', name);\n") | 121 fprintf (fid, " fprintf ('File ""%%s"" exists.\\n', name);\n") |
165 fprintf (fid, " end\n"); | 122 fprintf (fid, " end\n"); |
166 # fprintf (fid, " drawnow ();\n"); | |
167 fprintf (fid, " end\n"); | 123 fprintf (fid, " end\n"); |
168 fprintf (fid, " close (2:num_figures)\n"); | 124 # Temporary fix for cruft accumulating in figure window. |
125 fprintf (fid, " close ('all');\n"); | |
169 fprintf (fid, "catch\n"); | 126 fprintf (fid, "catch\n"); |
170 fprintf (fid, " fprintf ('ERROR in %s_%s: %%s\\n', lasterr ());\n", fun, idx); | 127 fprintf (fid, " fprintf ('ERROR in %s_%s: %%s\\n', lasterr ());\n", fcn, idx); |
171 fprintf (fid, "end\n\n"); | 128 fprintf (fid, "end\n\n"); |
172 endfor | 129 endfor |
173 endfor | 130 endfor |
174 fprintf (fid, "close all\n"); | 131 fprintf (fid, "close all\n"); |
175 endfunction | 132 endfunction |
176 | 133 |
177 function retval = get_demos (fun) | 134 function retval = get_demos (fcn) |
178 [code, idx] = test (fun, "grabdemo"); | 135 [code, idx] = test (fcn, "grabdemo"); |
179 num_demos = length (idx) - 1; | 136 num_demos = length (idx) - 1; |
180 retval = cell (1, num_demos); | 137 retval = cell (1, num_demos); |
181 ## Now split the demos into a cell array | 138 ## Now split the demos into a cell array |
182 for k = 1:num_demos | 139 for k = 1:num_demos |
183 retval {k} = oct2mat (code (idx (k):idx (k+1)-1)); | 140 retval{k} = oct2mat (code(idx(k):idx(k+1)-1)); |
184 endfor | 141 endfor |
185 endfunction | 142 endfunction |
186 | 143 |
187 function code = oct2mat (code) | 144 function code = oct2mat (code) |
188 ## Simple hacks to make things Matlab compatible | 145 ## Simple hacks to make things Matlab compatible |
189 code = strrep (code, "%!", "%%"); | 146 code = strrep (code, "%!", "%%"); |
190 code = strrep (code, "!", "~"); | 147 code = strrep (code, "!", "~"); |
191 ## Simple replacing double quotes with single quotes | 148 ## Simply replacing double quotes with single quotes |
192 ## causes problems with strings like 'hello "world"' | 149 ## causes problems with strings like 'hello "world"' |
193 #code = strrep (code, "\"", "'"); | 150 ## More complicated regexprep targets only full double quoted strings |
151 code = regexprep (code, "^([^']*)\"(.*)\"", "$1'$2'", | |
152 "lineanchors", "dotexceptnewline"); | |
194 code = strrep (code, "#", "%"); | 153 code = strrep (code, "#", "%"); |
195 ## Fix the format specs for the errobar demos | 154 ## Fix the format specs for the errorbar demos changed by the line above |
196 code = strrep (code, "%r", "#r"); | 155 code = strrep (code, "%r", "#r"); |
197 code = strrep (code, "%~", "#~"); | 156 code = strrep (code, "%~", "#~"); |
198 endkeywords = {"endfor", "endif", "endwhile", "end_try_catch", ... | 157 endkeywords = {"endfor", "endfunction", "endif", "endwhile", "end_try_catch"}; |
199 "endfunction", "end_unwind_protect"}; | |
200 for k = 1:numel (endkeywords) | 158 for k = 1:numel (endkeywords) |
201 code = strrep (code, endkeywords{k}, "end"); | 159 code = strrep (code, endkeywords{k}, "end"); |
202 endfor | 160 endfor |
203 commentkeywords = {"unwind_proect"}; | 161 commentkeywords = {"unwind_protect", "end_unwind_protect"}; |
204 for k = 1:numel (commentkeywords) | 162 for k = 1:numel (commentkeywords) |
205 code = strrep (code, commentkeywords{k}, strcat ("%", commentkeywords{k})); | 163 code = strrep (code, commentkeywords{k}, ["%" commentkeywords{k}]); |
206 endfor | 164 endfor |
165 | |
166 ## Fix up sombrero which now has default argument in Octave | |
167 code = strrep (code, "sombrero ()", "sombrero (41)"); | |
207 endfunction | 168 endfunction |
169 |