plot.pl 3.19 KB
Newer Older
Thierry Martinez's avatar
Thierry Martinez committed
1 2 3
:- module(
  plot,
  [
4
    % Commands
Thierry Martinez's avatar
Thierry Martinez committed
5
    plot/0,
Thierry Martinez's avatar
Thierry Martinez committed
6
    export_plot/1,
7 8 9 10 11
    export_plot_to_png/1,
    export_plot_to_canvas/2,
    % Public API
    set_plot_driver/1,
    gnu_plot/0
Thierry Martinez's avatar
Thierry Martinez committed
12 13 14
  ]
).

15 16 17 18

:- devdoc('\\section{Commands}').


Thierry Martinez's avatar
Thierry Martinez committed
19 20
plot :-
  biocham_command,
21 22 23 24 25 26 27 28 29 30 31
  doc('
    plots the current trace.
    \\begin{example}'
  ),
  biocham_silent(clear_model),
  biocham(load(library:examples/mapk/mapk)),
  biocham(numerical_simulation(20, method: msbdf)),
  biocham(plot),
  doc('
    \\end{example}'
  ),
Thierry Martinez's avatar
Thierry Martinez committed
32 33 34
  get_plot_driver(Driver),
  Driver.

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
export_plot(FileTemplate) :-
  biocham_command,
  doc('
    saves the current trace into two files:
    \\argument{FileTemplate}\\texttt{.csv} and \\texttt{.plot}.
  '),
  export_plot(FileTemplate, []).


export_plot_to_png(OutputFile) :-
  biocham_command,
  type(OutputFile, output_file),
  doc('plots the current trace in a PNG file'),
  export_plot(plot, [png(OutputFile)]),
  execute_plot(['plot.plot']).


export_plot_to_canvas(OutputFile, BaseOutputFile) :-
  biocham_command,
  type(OutputFile, output_file),
  type(BaseOutputFile, output_file),
  doc('plots the current trace as a canvas element in an HTML file'),
  export_plot(plot, [canvas(OutputFile, BaseOutputFile)]),
  execute_plot(['plot.plot']).
Thierry Martinez's avatar
Thierry Martinez committed
59

60 61

:- devdoc('\\section{Public API}').
Thierry Martinez's avatar
Thierry Martinez committed
62 63 64 65 66 67 68


set_plot_driver(Driver) :-
  nb_setval(plot_driver, Driver).


gnu_plot :-
Thierry Martinez's avatar
Thierry Martinez committed
69
  export_plot(plot),
Thierry Martinez's avatar
Thierry Martinez committed
70 71 72
  execute_plot(['-persist', 'plot.plot']).


73 74 75 76 77 78 79
:- devdoc('\\section{Private predicates}').


get_plot_driver(Driver) :-
  nb_getval(plot_driver, Driver).


Thierry Martinez's avatar
Thierry Martinez committed
80
gnu_plot_png :-
Thierry Martinez's avatar
Thierry Martinez committed
81 82 83
  count(plot_png, Index),
  format(atom(Filename), 'plot~d.png', [Index]),
  export_plot_to_png(Filename),
Thierry Martinez's avatar
SBML  
Thierry Martinez committed
84
  view_image(Filename).
Thierry Martinez's avatar
Thierry Martinez committed
85 86


87 88 89 90 91 92 93 94
gnu_plot_canvas :-
  count(plot_canvas, Index),
  format(atom(Filename), 'plot~d.js', [Index]),
  format(atom(BaseFilename), 'plot~d', [Index]),
  export_plot_to_canvas(Filename, BaseFilename),
  view_image(Filename).


95 96 97 98 99 100 101
gnu_plot_csv :-
  count(plot_csv, Index),
  format(atom(Filename), 'plot~d.csv', [Index]),
  export_table(Filename),
  view_image(Filename).


Thierry Martinez's avatar
Thierry Martinez committed
102 103
execute_plot(Options) :-
  process_create(path(gnuplot), Options, []).
Thierry Martinez's avatar
Thierry Martinez committed
104 105


Thierry Martinez's avatar
Thierry Martinez committed
106
export_plot(FileTemplate, Options) :-
Thierry Martinez's avatar
Thierry Martinez committed
107 108
  format(atom(PlotFile), '~a.plot', [FileTemplate]),
  format(atom(CsvFile), '~a.csv', [FileTemplate]),
Thierry Martinez's avatar
FOLTL  
Thierry Martinez committed
109
  export_table(CsvFile),
110
  get_table_headers([_Time | Headers]),
Thierry Martinez's avatar
Thierry Martinez committed
111
  absolute_file_name(PlotFile, AbsolutePlotFile),
Thierry Martinez's avatar
Thierry Martinez committed
112
  setup_call_cleanup(
Thierry Martinez's avatar
Thierry Martinez committed
113
    open(AbsolutePlotFile, write, Stream),
Thierry Martinez's avatar
Thierry Martinez committed
114
    export_plot_stream(Stream, CsvFile, Headers, Options),
Thierry Martinez's avatar
Thierry Martinez committed
115 116 117
    close(Stream)
  ).

Thierry Martinez's avatar
Thierry Martinez committed
118 119 120 121 122 123 124 125
export_plot_stream(Stream, CsvFile, Headers, Options) :-
  (
    member(png(Filename), Options)
  ->
    format(Stream, '\c
set term png
set output "~a"
', [Filename])
126 127 128 129 130 131 132
  ;
    member(canvas(Filename, BaseFilename), Options)
  ->
    format(Stream, '\c
set term canvas name "~a"
set output "~a"
', [BaseFilename, Filename])
Thierry Martinez's avatar
Thierry Martinez committed
133 134 135
  ;
    true
  ),
Thierry Martinez's avatar
Plot  
Thierry Martinez committed
136
  write(Stream, '\c
Thierry Martinez's avatar
Thierry Martinez committed
137 138
set style data lines
set datafile separator ","
Thierry Martinez's avatar
Plot  
Thierry Martinez committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
plot '),
  \+ (
    nth1(Index, Headers, Header),
    \+ (
      (
        Index > 1
      ->
        write(Stream, ', ')
      ;
        true
      ),
      ColumnIndex is Index + 1,
      format(
        Stream,
        '"~a" using 1:~d title "~a"',
        [CsvFile, ColumnIndex, Header]
      )
    )
  ),
  write(Stream, '\n').