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

plot :-
  biocham_command,
  doc('plots the current trace.'),
Thierry Martinez's avatar
Thierry Martinez committed
16 17 18 19 20 21 22 23 24 25 26 27 28
  get_plot_driver(Driver),
  Driver.


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


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


gnu_plot :-
Thierry Martinez's avatar
Thierry Martinez committed
29
  export_plot(plot),
Thierry Martinez's avatar
Thierry Martinez committed
30 31 32
  execute_plot(['-persist', 'plot.plot']).


Thierry Martinez's avatar
Thierry Martinez committed
33
gnu_plot_png :-
Thierry Martinez's avatar
Thierry Martinez committed
34 35 36
  count(plot_png, Index),
  format(atom(Filename), 'plot~d.png', [Index]),
  export_plot_to_png(Filename),
Thierry Martinez's avatar
SBML  
Thierry Martinez committed
37
  view_image(Filename).
Thierry Martinez's avatar
Thierry Martinez committed
38 39


40 41 42 43 44 45 46 47
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).


48 49 50 51 52 53 54
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
55 56
execute_plot(Options) :-
  process_create(path(gnuplot), Options, []).
Thierry Martinez's avatar
Thierry Martinez committed
57 58 59 60 61


export_plot(FileTemplate) :-
  biocham_command,
  doc('
Thierry Martinez's avatar
Thierry Martinez committed
62
    saves the current trace into two files:
Thierry Martinez's avatar
Thierry Martinez committed
63 64
    \\argument{FileTemplate}\\texttt{.csv} and \\texttt{.plot}.
  '),
Thierry Martinez's avatar
Thierry Martinez committed
65 66 67 68 69 70 71 72 73 74 75
  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']).


76 77 78 79 80 81 82 83
export_plot_to_canvas(OutputFile, BaseOutputFile) :-
  biocham_command,
  type(OutputFile, 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
84
export_plot(FileTemplate, Options) :-
Thierry Martinez's avatar
Thierry Martinez committed
85 86
  format(atom(PlotFile), '~a.plot', [FileTemplate]),
  format(atom(CsvFile), '~a.csv', [FileTemplate]),
Thierry Martinez's avatar
FOLTL  
Thierry Martinez committed
87
  export_table(CsvFile),
88
  get_table_headers([_Time | Headers]),
Thierry Martinez's avatar
Thierry Martinez committed
89
  absolute_file_name(PlotFile, AbsolutePlotFile),
Thierry Martinez's avatar
Thierry Martinez committed
90
  setup_call_cleanup(
Thierry Martinez's avatar
Thierry Martinez committed
91
    open(AbsolutePlotFile, write, Stream),
Thierry Martinez's avatar
Thierry Martinez committed
92
    export_plot_stream(Stream, CsvFile, Headers, Options),
Thierry Martinez's avatar
Thierry Martinez committed
93 94 95
    close(Stream)
  ).

Thierry Martinez's avatar
Thierry Martinez committed
96 97 98 99 100 101 102 103
export_plot_stream(Stream, CsvFile, Headers, Options) :-
  (
    member(png(Filename), Options)
  ->
    format(Stream, '\c
set term png
set output "~a"
', [Filename])
104 105 106 107 108 109 110
  ;
    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
111 112 113
  ;
    true
  ),
Thierry Martinez's avatar
Plot  
Thierry Martinez committed
114
  write(Stream, '\c
Thierry Martinez's avatar
Thierry Martinez committed
115 116
set style data lines
set datafile separator ","
Thierry Martinez's avatar
Plot  
Thierry Martinez committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
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').