comparison cpack/dml/components/matlab.pl @ 0:718306e29690 tip

commiting public release
author Daniel Wolff
date Tue, 09 Feb 2016 21:05:06 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:718306e29690
1 /* Part of DML (Digital Music Laboratory)
2 Copyright 2014-2015 Samer Abdallah, University of London
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 :- module(cp_matlab,
20 [ figure//4
21 , figure//3
22 , term_rendering//3
23 ]).
24
25 :- meta_predicate figure(0,+,+,+,?,?).
26 :- meta_predicate figure(0,+,+,?,?).
27 :- meta_predicate figure(0,+,?,?).
28
29 :- use_module(library(http/http_dispatch),[http_link_to_id/3]).
30 :- use_module(library(http/html_write)).
31 :- use_module(library(http/html_head)).
32 :- use_module(library(dcg_core)).
33 :- use_module(library(listutils)).
34 :- use_module(library(optutils)).
35 :- use_module(library(pengines_io)).
36 :- use_module(library(swish/render)).
37 :- use_module(library(mlserver)).
38 :- use_module(library(code_cache)).
39 :- use_module(library(insist)).
40 :- use_module(components(smartimg)).
41
42 :- register_renderer(matlab,"Render Matlab figures").
43
44 :- html_resource(js('smartimg.js'), []). % no requirements
45
46 %% term_rendering(FigSpec,Something,Opts)// is det.
47 %
48 % Matlab term rendering.
49 % Output from Matlab code is send to current_output of Matlab server thread (usually user_output).
50 % Options are:
51 % * timeout(T:float)
52 % * format(F:oneof([svg,png]))
53 % And any accepted by api_matlab:figure_render/1.r
54 term_rendering(fig(Code),_,Opts) -->
55 {rendering_options(T,Opts,Opts1)},
56 figure(mlserver:ml_async(exec(Code),T), Opts1).
57 term_rendering(fig(Code,W,H),_,Opts) --> term_rendering(fig(Code),_,[width(W),height(H)|Opts]).
58 term_rendering(fig(Code,FOpts),_,Opts) -->
59 {merge_options(FOpts,Opts,Opts1)},
60 term_rendering(fig(Code),_,Opts1).
61
62 rendering_options(T) -->
63 seqmap(option_default_select,[module(_),numbervars(_),quoted(_)],_),
64 option_default_select(timeout(T),60).
65
66 user:portray(ws(X)) :- ml_ws_name(X,Var,Eng), format('ws(|~w:~w|)',[Eng,Var]).
67 pengines_io:blob_rendering(ws,_Blob,_) -->
68 html(span(class='pl-blob','ws:_')).
69
70 %% figure(+Code:callable, +Width:natural, +Height:natural, +Params:list)// is det.
71 %% figure(+Code:callable, +Width:natural, +Height:natural)// is det.
72 %
73 % Component to render a Matlab figure into a HTML code. Code must be a Prolog
74 % goal that renders the desired figure into Matlab's current figure.
75 % The default figure format (eg SVG or PNG) is taken from the default_figure_format
76 % setting. Options are
77 % * cache(boolean)
78 % If true (default: false), put the code in a cache and use its unique id to call
79 % it. Useful for code that is to long to encode in a URL.
80 % * downloadable(boolean)
81 % If true (default: false), add download links (for PDF and EPS formats) below the image.
82 % * smart(boolean)
83 % If true (default: false), then use Javascript image loading code instead of a standard HTML
84 % <IMG> tag. If the server returns an error HTML page, this is displayed
85 % instead of the image.
86 % See api_matlab:figure_render/1 for other valid options.
87 figure(Code,W,H) --> figure(Code,[width(W),height(H)]).
88 figure(Code,W,H,Params) --> figure(Code,[width(W),height(H)|Params]).
89 figure(Code,Params) -->
90 { debug(cp_matlab,"figure(~q,~q)",[Code,Params]),
91 process_options(Download,Smart,Code,Params,Params1),
92 insist(option(width(W),Params1), missing_parameter(width)),
93 insist(option(height(H),Params1), missing_parameter(height)),
94 http_link_to_id(figure_render, Params1,URL)
95 },
96 ( {Download=true}
97 -> { option_default_select(format(_),_,Params1,Params2)},
98 html(div([\image(Smart,URL,W,H),br([]), 'Download as:', \download(pdf,Params2), \download(eps,Params2)]))
99 ; image(Smart,URL,W,H)
100 ).
101
102 download(F,Params) -->
103 {http_link_to_id(figure_render,[format(F)|Params],URL)},
104 html([' ',a([href(URL),download],F)]).
105
106 process_options(Download,Smart,Code) -->
107 {setting(matlab:default_figure_format,Fmt0)},
108 seqmap(option_default_select, [format(Fmt), cache(Cache), downloadable(Download), smart(Smart)],
109 [Fmt0,false,false,false]),
110 (select_option(color_map(CM)) -> {term_to_atom(CM,CMA)}, cons(color_map(CMA)); []),
111 (select_option(size(W,H)) -> cons(width(W)), cons(height(H)); []),
112 {Cache=true -> cache_code(Download,Code,Code1); Code=Code1},
113 {term_to_atom(Code1,CodeAtom)},
114 cons(format(Fmt)),
115 cons(code(CodeAtom)).